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 <list>
20 #include <string>
21 
22 #include "device_config_file_parser.h"
23 #include "device_observer.h"
24 #include "input_device.h"
25 #include "msg_handler.h"
26 #include "nocopyable.h"
27 #include "singleton.h"
28 #include "uds_session.h"
29 #include "util.h"
30 
31 namespace OHOS {
32 namespace MMI {
33 class InputDeviceManager final : public IDeviceObject {
34 private:
35     struct InputDeviceInfo {
36         struct libinput_device *inputDeviceOrigin { nullptr };
37         std::string networkIdOrigin;
38         bool isRemote { false };
39         bool isPointerDevice { false };
40         bool isTouchableDevice { false };
41         bool enable { false };
42         std::string dhid;
43         std::string sysUid;
44         VendorConfig vendorConfig;
45     };
46 
47 public:
48     InputDeviceManager() = default;
49     ~InputDeviceManager() = default;
50     DISALLOW_COPY_AND_MOVE(InputDeviceManager);
51 
52     void OnInputDeviceAdded(struct libinput_device *inputDevice);
53     void OnInputDeviceRemoved(struct libinput_device *inputDevice);
54     int32_t AddVirtualInputDevice(std::shared_ptr<InputDevice> device, int32_t &deviceId);
55     int32_t RemoveVirtualInputDevice(int32_t deviceId);
56     std::vector<int32_t> GetInputDeviceIds() const;
57     std::shared_ptr<InputDevice> GetInputDevice(int32_t deviceId, bool checked = true) const;
58     int32_t SupportKeys(int32_t deviceId, std::vector<int32_t> &keyCodes, std::vector<bool> &keystroke);
59     int32_t FindInputDeviceId(struct libinput_device* inputDevice);
60     int32_t GetKeyboardBusMode(int32_t deviceId);
61     bool GetDeviceConfig(int32_t deviceId, int32_t &KeyboardType);
62     int32_t GetDeviceSupportKey(int32_t deviceId, int32_t &keyboardType);
63     int32_t GetKeyboardType(int32_t deviceId, int32_t &keyboardType);
64     void Attach(std::shared_ptr<IDeviceObserver> observer);
65     void Detach(std::shared_ptr<IDeviceObserver> observer);
66     void NotifyPointerDevice(bool hasPointerDevice, bool isVisible, bool isHotPlug);
67     void AddDevListener(SessionPtr sess);
68     void RemoveDevListener(SessionPtr sess);
69     void Dump(int32_t fd, const std::vector<std::string> &args);
70     void DumpDeviceList(int32_t fd, const std::vector<std::string> &args);
71     bool IsRemote(struct libinput_device *inputDevice) const;
72     bool IsRemote(int32_t id) const;
73     bool IsKeyboardDevice(struct libinput_device* device) const;
74     bool IsPointerDevice(struct libinput_device* device) const;
75     bool IsTouchDevice(struct libinput_device* device) const;
76     struct libinput_device* GetKeyboardDevice() const;
77 #ifdef OHOS_BUILD_ENABLE_POINTER_DRAWING
78     bool HasPointerDevice();
79     bool HasVirtualPointerDevice();
80 #endif // OHOS_BUILD_ENABLE_POINTER_DRAWING
81     bool HasTouchDevice();
82     const std::string& GetScreenId(int32_t deviceId) const;
83     using inputDeviceCallback = std::function<void(int32_t deviceId, std::string devName, std::string devStatus)>;
84     void SetInputStatusChangeCallback(inputDeviceCallback callback);
85     VendorConfig GetVendorConfig(int32_t deviceId) const;
86     int32_t OnEnableInputDevice(bool enable);
87     std::vector<int32_t> GetTouchPadIds();
88     struct libinput_device *GetTouchPadDeviceOrigin();
89 
90     static std::shared_ptr<InputDeviceManager> GetInstance();
91 
92 private:
93     int32_t ParseDeviceId(struct libinput_device *inputDevice);
94     void MakeDeviceInfo(struct libinput_device *inputDevice, struct InputDeviceInfo& info);
95     bool IsMatchKeys(struct libinput_device* device, const std::vector<int32_t> &keyCodes) const;
96     void ScanPointerDevice();
97     void FillInputDevice(std::shared_ptr<InputDevice> inputDevice, libinput_device *deviceOrigin) const;
98     std::string GetInputIdentification(struct libinput_device* inputDevice);
99     void NotifyDevCallback(int32_t deviceId,  struct InputDeviceInfo inDevice);
100     void NotifyDevRemoveCallback(int32_t deviceId,  const InputDeviceInfo &deviceInfo);
101     int32_t NotifyMessage(SessionPtr sess, int32_t id, const std::string &type);
102     void InitSessionLostCallback();
103     void OnSessionLost(SessionPtr session);
104     int32_t MakeVirtualDeviceInfo(std::shared_ptr<InputDevice> device, InputDeviceInfo &deviceInfo);
105     int32_t GenerateVirtualDeviceId(int32_t &deviceId);
106     bool IsPointerDevice(std::shared_ptr<InputDevice> inputDevice) const;
107     bool IsTouchableDevice(std::shared_ptr<InputDevice> inputDevice) const;
108     bool IsKeyboardDevice(std::shared_ptr<InputDevice> inputDevice) const;
109     std::string GetMaskedDeviceId(const std::string& str);
110     std::string GetMaskedStr(const std::string& str, size_t reserveLen);
111 
112 private:
113     std::map<int32_t, struct InputDeviceInfo> inputDevice_;
114     std::map<int32_t, std::shared_ptr<InputDevice>> virtualInputDevices_;
115     std::map<std::string, std::string> inputDeviceScreens_;
116     std::list<std::shared_ptr<IDeviceObserver>> observers_;
117     std::list<SessionPtr> devListeners_;
118     inputDeviceCallback devCallbacks_ { nullptr };
119     std::map<int32_t, std::string> displayInputBindInfos_;
120     DeviceConfigManagement configManagement_;
121     bool sessionLostCallbackInitialized_ { false };
122 
123     static std::shared_ptr<InputDeviceManager> instance_;
124     static std::mutex mutex_;
125 };
126 
127 #define INPUT_DEV_MGR ::OHOS::MMI::InputDeviceManager::GetInstance()
128 } // namespace MMI
129 } // namespace OHOS
130 #endif // INPUT_DEVICE_MANAGER_H
131