1 /*
2  * Copyright (c) 2021-2024 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_HANDLER_H
17 #define DISTRIBUTED_INPUT_HANDLER_H
18 
19 #include <functional>
20 #include <map>
21 #include <mutex>
22 #include <string>
23 
24 #include <sys/epoll.h>
25 #include <linux/input.h>
26 
27 #include "ihardware_handler.h"
28 #include "single_instance.h"
29 
30 #include "constants_dinput.h"
31 #include "input_hub.h"
32 
33 #ifndef API_EXPORT
34 #define API_EXPORT __attribute__((visibility("default")))
35 #endif
36 
37 namespace OHOS {
38 namespace DistributedHardware {
39 namespace DistributedInput {
40 class DistributedInputHandler : public IHardwareHandler {
41 DECLARE_SINGLE_INSTANCE_BASE(DistributedInputHandler);
42 public:
43     API_EXPORT int32_t Initialize() override;
44     API_EXPORT virtual std::vector<DHItem> QueryMeta() override;
45     API_EXPORT virtual std::vector<DHItem> Query() override;
46     API_EXPORT virtual std::map<std::string, std::string> QueryExtraInfo() override;
47     API_EXPORT bool IsSupportPlugin() override;
48     API_EXPORT void RegisterPluginListener(std::shared_ptr<PluginListener> listener) override;
49     API_EXPORT void UnRegisterPluginListener() override;
50 
51     API_EXPORT void FindDevicesInfoByType(const uint32_t inputTypes, std::map<int32_t, std::string> &datas);
52     API_EXPORT void FindDevicesInfoByDhId(std::vector<std::string> dhidsVec, std::map<int32_t, std::string> &datas);
53 private:
54     DistributedInputHandler();
55     ~DistributedInputHandler();
56     void StructTransJson(const InputDevice &pBuf, std::string &strDescriptor);
57     std::shared_ptr<PluginListener> m_listener;
58     bool InitCollectEventsThread();
59     void NotifyHardWare(int iCnt);
60 
61     pthread_t collectThreadID_;
62     bool isCollectingEvents_;
63     bool isStartCollectEventThread_;
64     static void *CollectEventsThread(void *param);
65     void StartInputMonitorDeviceThread();
66     void StopInputMonitorDeviceThread();
67 
68     // The event queue.
69     static const int inputDeviceBufferSize_ = 32;
70     InputDeviceEvent mEventBuffer_[inputDeviceBufferSize_] = {};
71     std::mutex operationMutex_;
72     std::unique_ptr<InputHub> inputHub_;
73 };
74 
75 #ifdef __cplusplus
76 extern "C" {
77 #endif
78 API_EXPORT IHardwareHandler* GetHardwareHandler();
79 #ifdef __cplusplus
80 }
81 #endif
82 } // namespace DistributedInput
83 } // namespace DistributedHardware
84 } // namespace OHOS
85 
86 #endif // DISTRIBUTED_INPUT_HANDLER_H
87