• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 INPUT_DEVICE_MANAGER_H
17 #define INPUT_DEVICE_MANAGER_H
18 
19 #include "device_config_file_parser.h"
20 #include "device_observer.h"
21 #include "input_device.h"
22 #include "uds_session.h"
23 #include <shared_mutex>
24 
25 namespace OHOS {
26 namespace MMI {
27 class InputDeviceManager final {
28 private:
29     struct InputDeviceInfo {
30         struct libinput_device *inputDeviceOrigin { nullptr };
31         std::string networkIdOrigin;
32         bool isRemote { false };
33         bool isPointerDevice { false };
34         bool isTouchableDevice { false };
35         bool enable { false };
36         std::string dhid;
37         std::string sysUid;
38         VendorConfig vendorConfig;
39     };
40 
41 public:
42     InputDeviceManager() = default;
43     ~InputDeviceManager() = default;
44     DISALLOW_COPY_AND_MOVE(InputDeviceManager);
45 
46     void OnInputDeviceAdded(struct libinput_device *inputDevice);
47     void OnInputDeviceRemoved(struct libinput_device *inputDevice);
48     int32_t AddVirtualInputDevice(std::shared_ptr<InputDevice> device, int32_t &deviceId);
49     int32_t RemoveVirtualInputDevice(int32_t deviceId);
50     std::vector<int32_t> GetInputDeviceIds() const;
51     std::shared_ptr<InputDevice> GetInputDevice(int32_t deviceId, bool checked = true) const;
52     int32_t SupportKeys(int32_t deviceId, std::vector<int32_t> &keyCodes, std::vector<bool> &keystroke);
53     int32_t FindInputDeviceId(struct libinput_device* inputDevice);
54     int32_t GetKeyboardBusMode(int32_t deviceId);
55     bool GetDeviceConfig(int32_t deviceId, int32_t &KeyboardType);
56     int32_t GetDeviceSupportKey(int32_t deviceId, int32_t &keyboardType);
57     int32_t GetKeyboardType(int32_t deviceId, int32_t &keyboardType);
58     void Attach(std::shared_ptr<IDeviceObserver> observer);
59     void Detach(std::shared_ptr<IDeviceObserver> observer);
60     void NotifyPointerDevice(bool hasPointerDevice, bool isVisible, bool isHotPlug);
61     void AddDevListener(SessionPtr sess);
62     void RemoveDevListener(SessionPtr sess);
63     void Dump(int32_t fd, const std::vector<std::string> &args);
64     void DumpDeviceList(int32_t fd, const std::vector<std::string> &args);
65     bool IsRemote(struct libinput_device *inputDevice) const;
66     bool IsRemote(int32_t id) const;
67     bool IsKeyboardDevice(struct libinput_device* device) const;
68     bool IsPointerDevice(struct libinput_device* device) const;
69     bool IsTouchDevice(struct libinput_device* device) const;
70     bool IsTouchPadDevice(struct libinput_device *device) const;
71     struct libinput_device* GetKeyboardDevice() const;
72     void GetMultiKeyboardDevice(std::vector<struct libinput_device*> &inputDevice);
73 #ifdef OHOS_BUILD_ENABLE_POINTER_DRAWING
74     bool HasPointerDevice();
75     bool HasVirtualPointerDevice();
76 #endif // OHOS_BUILD_ENABLE_POINTER_DRAWING
77 #ifdef OHOS_BUILD_ENABLE_VKEYBOARD
78     bool HasVirtualKeyboardDevice();
79 #endif // OHOS_BUILD_ENABLE_VKEYBOARD
80     bool CheckDuplicateInputDevice(struct libinput_device *inputDevice);
81     bool CheckDuplicateInputDevice(std::shared_ptr<InputDevice> inputDevice);
82     void AddPhysicalInputDeviceInner(int32_t deviceId, const struct InputDeviceInfo& info);
83     void AddVirtualInputDeviceInner(int32_t deviceId, std::shared_ptr<InputDevice> inputDevice);
84     void RemovePhysicalInputDeviceInner(struct libinput_device *inputDevice, int32_t &deviceId, bool &enable);
85     int32_t RemoveVirtualInputDeviceInner(int32_t deviceId, struct InputDeviceInfo& info);
86     bool HasEnabledPhysicalPointerDevice();
87     void NotifyAddDeviceListeners(int32_t deviceId);
88     void NotifyRemoveDeviceListeners(int32_t deviceId);
89     void NotifyAddPointerDevice(bool addNewPointerDevice, bool existEnabledPointerDevice);
90     void NotifyRemovePointerDevice(bool removePointerDevice);
91     bool HasTouchDevice();
92     const std::string& GetScreenId(int32_t deviceId) const;
93     using inputDeviceCallback =
94         std::function<void(int32_t deviceId, std::string nodeName, std::string devName, std::string devStatus)>;
95     void SetInputStatusChangeCallback(inputDeviceCallback callback);
96     VendorConfig GetVendorConfig(int32_t deviceId) const;
97     int32_t OnEnableInputDevice(bool enable);
98     std::vector<int32_t> GetTouchPadIds();
99     std::vector<libinput_device*> GetTouchPadDeviceOrigins();
100     int32_t SetInputDeviceEnabled(int32_t deviceId, bool enable, int32_t index, int32_t pid, SessionPtr session);
101     static std::shared_ptr<InputDeviceManager> GetInstance();
102     bool IsInputDeviceEnable(int32_t deviceId);
103 #ifdef OHOS_BUILD_ENABLE_KEYBOARD_EXT_FLAG
104     bool IsAddKeyboardExtFlag(int32_t deviceId, uint32_t &flag);
105 #endif // OHOS_BUILD_ENABLE_KEYBOARD_EXT_FLAG
106     bool IsLocalDevice(int32_t deviceId);
107 
108 private:
109     int32_t ParseDeviceId(struct libinput_device *inputDevice);
110     void MakeDeviceInfo(struct libinput_device *inputDevice, struct InputDeviceInfo& info);
111     bool IsMatchKeys(struct libinput_device* device, const std::vector<int32_t> &keyCodes) const;
112     void ScanPointerDevice();
113     void FillInputDevice(std::shared_ptr<InputDevice> inputDevice, libinput_device *deviceOrigin) const;
114     void FillInputDeviceWithVirtualCapability(
115         std::shared_ptr<InputDevice> inputDevice, const InputDeviceInfo &deviceInfo) const;
116     std::string GetInputIdentification(struct libinput_device* inputDevice);
117     void NotifyDevCallback(int32_t deviceId, struct InputDeviceInfo inDevice);
118     void NotifyDevRemoveCallback(int32_t deviceId, const InputDeviceInfo &deviceInfo);
119     int32_t NotifyMessage(SessionPtr sess, int32_t id, const std::string &type);
120     void InitSessionLostCallback();
121     void OnSessionLost(SessionPtr session);
122     int32_t MakeVirtualDeviceInfo(std::shared_ptr<InputDevice> device, InputDeviceInfo &deviceInfo);
123     int32_t GenerateVirtualDeviceId(int32_t &deviceId);
124     bool IsPointerDevice(std::shared_ptr<InputDevice> inputDevice) const;
125     bool IsTouchableDevice(std::shared_ptr<InputDevice> inputDevice) const;
126     bool IsKeyboardDevice(std::shared_ptr<InputDevice> inputDevice) const;
127     void RecoverInputDeviceEnabled(SessionPtr session);
128     int32_t NotifyInputdeviceMessage(SessionPtr session, int32_t index, int32_t result);
129 #ifdef OHOS_BUILD_ENABLE_KEYBOARD_EXT_FLAG
130     void NotifyDevCallbackExt(int32_t deviceId, struct libinput_device *inputDevice);
131     void NotifyDevRemoveCallbackExt(int32_t deviceId);
132 #endif // OHOS_BUILD_ENABLE_KEYBOARD_EXT_FLAG
133     int32_t GetTouchscreenKeyboardType(const InputDeviceInfo &deviceInfo, int32_t &keyboardType);
134     int32_t GetVirtualKeyboardType(int32_t deviceId, int32_t &keyboardType);
135     void PointerDeviceInit();
136     void NotifyDeviceAdded(int32_t deviceId) const;
137     void NotifyDeviceRemoved(int32_t deviceId) const;
138 
139 private:
140     std::map<int32_t, struct InputDeviceInfo> inputDevice_;
141     std::map<int32_t, int32_t> recoverList_;
142     std::map<int32_t, std::shared_ptr<InputDevice>> virtualInputDevices_;
143     std::map<std::string, std::string> inputDeviceScreens_;
144     std::list<std::shared_ptr<IDeviceObserver>> observers_;
145     std::list<SessionPtr> devListeners_;
146     inputDeviceCallback devCallbacks_ { nullptr };
147     std::map<int32_t, std::string> displayInputBindInfos_;
148 #ifndef OHOS_BUILD_ENABLE_WATCH
149     DeviceConfigManagement configManagement_;
150 #endif // OHOS_BUILD_ENABLE_WATCH
151     bool sessionLostCallbackInitialized_ { false };
152 
153     static std::shared_ptr<InputDeviceManager> instance_;
154     static std::mutex mutex_;
155 };
156 
157 #define INPUT_DEV_MGR ::OHOS::MMI::InputDeviceManager::GetInstance()
158 } // namespace MMI
159 } // namespace OHOS
160 #endif // INPUT_DEVICE_MANAGER_H
161