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 void GetMultiKeyboardDevice(std::vector<struct libinput_device*> &inputDevice); 78 #ifdef OHOS_BUILD_ENABLE_POINTER_DRAWING 79 bool HasPointerDevice(); 80 bool HasVirtualPointerDevice(); 81 #endif // OHOS_BUILD_ENABLE_POINTER_DRAWING 82 bool HasTouchDevice(); 83 const std::string& GetScreenId(int32_t deviceId) const; 84 using inputDeviceCallback = std::function<void(int32_t deviceId, std::string devName, std::string devStatus)>; 85 void SetInputStatusChangeCallback(inputDeviceCallback callback); 86 VendorConfig GetVendorConfig(int32_t deviceId) const; 87 int32_t OnEnableInputDevice(bool enable); 88 std::vector<int32_t> GetTouchPadIds(); 89 struct libinput_device *GetTouchPadDeviceOrigin(); 90 91 static std::shared_ptr<InputDeviceManager> GetInstance(); 92 93 private: 94 int32_t ParseDeviceId(struct libinput_device *inputDevice); 95 void MakeDeviceInfo(struct libinput_device *inputDevice, struct InputDeviceInfo& info); 96 bool IsMatchKeys(struct libinput_device* device, const std::vector<int32_t> &keyCodes) const; 97 void ScanPointerDevice(); 98 void FillInputDevice(std::shared_ptr<InputDevice> inputDevice, libinput_device *deviceOrigin) const; 99 std::string GetInputIdentification(struct libinput_device* inputDevice); 100 void NotifyDevCallback(int32_t deviceId, struct InputDeviceInfo inDevice); 101 void NotifyDevRemoveCallback(int32_t deviceId, const InputDeviceInfo &deviceInfo); 102 int32_t NotifyMessage(SessionPtr sess, int32_t id, const std::string &type); 103 void InitSessionLostCallback(); 104 void OnSessionLost(SessionPtr session); 105 int32_t MakeVirtualDeviceInfo(std::shared_ptr<InputDevice> device, InputDeviceInfo &deviceInfo); 106 int32_t GenerateVirtualDeviceId(int32_t &deviceId); 107 bool IsPointerDevice(std::shared_ptr<InputDevice> inputDevice) const; 108 bool IsTouchableDevice(std::shared_ptr<InputDevice> inputDevice) const; 109 bool IsKeyboardDevice(std::shared_ptr<InputDevice> inputDevice) const; 110 std::string GetMaskedDeviceId(const std::string& str); 111 std::string GetMaskedStr(const std::string& str, size_t reserveLen); 112 113 private: 114 std::map<int32_t, struct InputDeviceInfo> inputDevice_; 115 std::map<int32_t, std::shared_ptr<InputDevice>> virtualInputDevices_; 116 std::map<std::string, std::string> inputDeviceScreens_; 117 std::list<std::shared_ptr<IDeviceObserver>> observers_; 118 std::list<SessionPtr> devListeners_; 119 inputDeviceCallback devCallbacks_ { nullptr }; 120 std::map<int32_t, std::string> displayInputBindInfos_; 121 DeviceConfigManagement configManagement_; 122 bool sessionLostCallbackInitialized_ { false }; 123 124 static std::shared_ptr<InputDeviceManager> instance_; 125 static std::mutex mutex_; 126 }; 127 128 #define INPUT_DEV_MGR ::OHOS::MMI::InputDeviceManager::GetInstance() 129 } // namespace MMI 130 } // namespace OHOS 131 #endif // INPUT_DEVICE_MANAGER_H 132