1 /*
2  * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef DISTRIBUTED_INPUT_COLLECTOR_H
17 #define DISTRIBUTED_INPUT_COLLECTOR_H
18 
19 #include <atomic>
20 #include <map>
21 #include <mutex>
22 #include <set>
23 #include <string>
24 
25 #include <sys/epoll.h>
26 #include <linux/input.h>
27 
28 #include "event_handler.h"
29 #include "refbase.h"
30 
31 #include "constants_dinput.h"
32 #include "input_hub.h"
33 #include "i_sharing_dhid_listener.h"
34 
35 namespace OHOS {
36 namespace DistributedHardware {
37 namespace DistributedInput {
38 class DistributedInputCollector {
39 public:
40     static DistributedInputCollector &GetInstance();
41     // PreInit for get the local input devices basic info.
42     // Collect all the local input basic info cost too much time(200+ ms).
43     void PreInit();
44     int32_t StartCollectionThread(std::shared_ptr<AppExecFwk::EventHandler> sinkHandler);
45     void StopCollectionThread();
46     AffectDhIds SetSharingTypes(bool enabled, const uint32_t &inputType);
47     AffectDhIds SetSharingDhIds(bool enabled, std::vector<std::string> dhIds);
48     std::vector<std::string> GetSharingDhIds();
49     void GetMouseNodePath(const std::vector<std::string> &dhIds, std::string &mouseNodePath, std::string &dhid);
50     void GetSharedKeyboardPathsByDhIds(const std::vector<std::string> &dhIds,
51         std::vector<std::string> &sharedKeyboardPaths, std::vector<std::string> &sharedKeyboardDhIds);
52     // false for sharing device exist, true for all devices stop sharing
53     bool IsAllDevicesStoped();
54     int32_t RegisterSharingDhIdListener(sptr<ISharingDhIdListener> sharingDhIdListener);
55     void ReportDhIdSharingState(const AffectDhIds &dhIds);
56     void GetDeviceInfoByType(const uint32_t inputTypes, std::map<int32_t, std::string> &deviceInfo);
57     void ResetSpecEventStatus();
58     void ClearSkipDevicePaths();
59 
60 private:
61     DistributedInputCollector();
62     ~DistributedInputCollector();
63 
64     bool InitCollectEventsThread();
65     static void *CollectEventsThread(void *param);
66     void StartCollectEventsThread();
67     void StopCollectEventsThread();
68 
69     RawEvent mEventBuffer[INPUT_EVENT_BUFFER_SIZE];
70     pthread_t collectThreadID_;
71     bool isCollectingEvents_;
72     bool isStartGetDeviceHandlerThread;
73     std::unique_ptr<InputHub> inputHub_;
74     std::shared_ptr<AppExecFwk::EventHandler> sinkHandler_;
75     uint32_t inputTypes_;
76 
77     std::mutex sharingDhIdListenerMtx_;
78     std::set<sptr<ISharingDhIdListener>> sharingDhIdListeners_;
79 };
80 } // namespace DistributedInput
81 } // namespace DistributedHardware
82 } // namespace OHOS
83 
84 #endif // DISTRIBUTED_INPUT_COLLECTOR_H