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