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 #ifndef OHOS_INPUT_DEVICE_EVENT_H 16 #define OHOS_INPUT_DEVICE_EVENT_H 17 18 #include <functional> 19 #include <list> 20 #include <map> 21 #include <mutex> 22 #include <vector> 23 24 #include "nocopyable.h" 25 26 #include "i_input_device_listener.h" 27 #include "input_device.h" 28 29 namespace OHOS { 30 namespace MMI { 31 class NetPacket; 32 class InputDeviceImpl { 33 public: 34 static InputDeviceImpl& GetInstance(); 35 DISALLOW_COPY_AND_MOVE(InputDeviceImpl); 36 ~InputDeviceImpl() = default; 37 38 using FunInputDevInfo = std::function<void(std::shared_ptr<InputDevice>)>; 39 using FunInputDevIds = std::function<void(std::vector<int32_t>&)>; 40 using FunInputDevKeys = std::function<void(std::vector<bool>&)>; 41 using FunKeyboardTypes = std::function<void(int32_t)>; 42 struct InputDeviceData { 43 FunInputDevInfo inputDevice; 44 FunInputDevIds ids; 45 FunInputDevKeys keys; 46 FunKeyboardTypes kbTypes; 47 }; 48 using InputDevListenerPtr = std::shared_ptr<IInputDeviceListener>; 49 50 int32_t RegisterDevListener(const std::string &type, InputDevListenerPtr listener); 51 int32_t UnregisterDevListener(const std::string &type, InputDevListenerPtr listener = nullptr); 52 int32_t GetInputDeviceIdsAsync(FunInputDevIds callback); 53 int32_t GetInputDeviceAsync(int32_t deviceId, FunInputDevInfo callback); 54 int32_t SupportKeys(int32_t deviceId, std::vector<int32_t> keyCodes, FunInputDevKeys callback); 55 int32_t GetKeyboardType(int32_t deviceId, FunKeyboardTypes callback); 56 void OnInputDevice(int32_t userData, std::shared_ptr<InputDevice> devData); 57 void OnInputDeviceIds(int32_t userData, std::vector<int32_t> &ids); 58 void OnSupportKeys(int32_t userData, std::vector<bool> &keystrokeAbility); 59 void OnDevListener(int32_t deviceId, const std::string &type); 60 void OnKeyboardType(int32_t userData, int32_t keyboardType); 61 int32_t GetUserData(); 62 std::shared_ptr<InputDevice> DevDataUnmarshalling(NetPacket &pkt); 63 64 private: 65 const FunInputDevInfo* GetDeviceInfo(int32_t) const; 66 const FunInputDevIds* GetDeviceIds(int32_t) const; 67 const FunInputDevKeys* GetDeviceKeys(int32_t) const; 68 const FunKeyboardTypes* GetKeyboardTypes(int32_t) const; 69 private: 70 InputDeviceImpl() = default; 71 std::map<int32_t, InputDeviceData> inputDevices_; 72 std::map<std::string, std::list<InputDevListenerPtr>> devListener_ = { { "change", {} } }; 73 int32_t userData_ { 0 }; 74 bool isListeningProcess_ { false }; 75 std::mutex mtx_; 76 }; 77 } // namespace MMI 78 } // namespace OHOS 79 #define InputDevImpl OHOS::MMI::InputDeviceImpl::GetInstance() 80 #endif // OHOS_INPUT_DEVICE_EVENT_H