• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_NODE_MANAGER_H
17 #define DISTRIBUTED_INPUT_NODE_MANAGER_H
18 
19 #include <atomic>
20 #include <condition_variable>
21 #include <map>
22 #include <mutex>
23 #include <queue>
24 #include <string>
25 #include <thread>
26 
27 #include "event_handler.h"
28 #include "nlohmann/json.hpp"
29 
30 #include "constants_dinput.h"
31 #include "input_hub.h"
32 #include "virtual_device.h"
33 
34 namespace OHOS {
35 namespace DistributedHardware {
36 namespace DistributedInput {
37 const uint32_t DINPUT_NODE_MANAGER_SCAN_ALL_NODE = 1;
38 const std::string INPUT_NODE_DHID = "dhId";
39 class DistributedInputNodeManager {
40 public:
41     DistributedInputNodeManager();
42     ~DistributedInputNodeManager();
43 
44     int32_t OpenDevicesNode(const std::string &devId, const std::string &dhId, const std::string &parameters);
45 
46     int32_t GetDevice(const std::string &dhId, VirtualDevice *&device);
47     void ReportEvent(const RawEvent rawEvent);
48     int32_t CloseDeviceLocked(const std::string &dhId);
49     void StartInjectThread();
50     void StopInjectThread();
51     int32_t CreateVirtualTouchScreenNode(const std::string &devId, const std::string &dhId, const uint64_t srcWinId,
52         const uint32_t sourcePhyWidth, const uint32_t sourcePhyHeight);
53     int32_t RemoveVirtualTouchScreenNode(const std::string &dhId);
54     int32_t GetVirtualTouchScreenFd();
55 
56     int32_t GetDeviceInfo(std::string &deviceId);
57     void GetDevicesInfoByType(const std::string &networkId, uint32_t inputTypes, std::map<int32_t, std::string> &datas);
58     void GetDevicesInfoByDhId(std::vector<std::string> dhidsVec, std::map<int32_t, std::string> &datas);
59     void ProcessInjectEvent(const std::shared_ptr<RawEvent> &rawEvent);
60 
61     void GetVirtualKeyboardPathsByDhIds(const std::vector<std::string> &dhIds,
62         std::vector<std::string> &shareDhidsPaths, std::vector<std::string> &shareDhIds);
63     void NotifyNodeMgrScanVirNode(const std::string &dhId);
64 
65     class DInputNodeManagerEventHandler : public AppExecFwk::EventHandler {
66     public:
67         DInputNodeManagerEventHandler(const std::shared_ptr<AppExecFwk::EventRunner> &runner,
68             DistributedInputNodeManager *manager);
69         ~DInputNodeManagerEventHandler() override;
70 
71         void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override;
72     private:
73         void ScanAllNode(const AppExecFwk::InnerEvent::Pointer &event);
74 
75         using nodeMgrFunc = void (DInputNodeManagerEventHandler::*)(
76             const AppExecFwk::InnerEvent::Pointer &event);
77         std::map<int32_t, nodeMgrFunc> eventFuncMap_;
78         DistributedInputNodeManager *nodeManagerObj_;
79     };
80 
81 private:
82     void AddDeviceLocked(const std::string &dhId, std::unique_ptr<VirtualDevice> device);
83     int32_t CreateHandle(const InputDevice &inputDevice, const std::string &devId, const std::string &dhId);
84     void ParseInputDeviceJson(const std::string &str, InputDevice &pBuf);
85     void VerifyInputDevice(const nlohmann::json &inputDeviceJson, InputDevice &pBuf);
86     void InjectEvent();
87 
88     void ScanSinkInputDevices(const std::string &dhId);
89     void OpenInputDevice(const std::string &devicePath, const std::string &dhId);
90     bool IsVirtualDev(int fd);
91     bool GetDevDhIdByFd(int fd, std::string &dhId, std::string &physicalPath);
92     void SetPathForDevMap(std::string &dhId, const std::string &devicePath);
93 
94     /* the key is dhId, and the value is virtualDevice */
95     std::map<std::string, std::unique_ptr<VirtualDevice>> virtualDeviceMap_;
96     std::mutex virtualDeviceMapMutex_;
97     std::atomic<bool> isInjectThreadCreated_;
98     std::atomic<bool> isInjectThreadRunning_;
99     std::mutex operationMutex_;
100     std::thread eventInjectThread_;
101     std::mutex injectThreadMutex_;
102     std::condition_variable conditionVariable_;
103     std::queue<std::shared_ptr<RawEvent>> injectQueue_;
104     std::unique_ptr<InputHub> inputHub_;
105     int32_t virtualTouchScreenFd_;
106     std::once_flag callOnceFlag_;
107     std::shared_ptr<DInputNodeManagerEventHandler> callBackHandler_;
108 };
109 } // namespace DistributedInput
110 } // namespace DistributedHardware
111 } // namespace OHOS
112 
113 #endif // DISTRIBUTED_INPUT_INJECT_H