• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 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 OHOS_INPUT_DEVICE_EVENT_H
17 #define OHOS_INPUT_DEVICE_EVENT_H
18 
19 #include <list>
20 #include <map>
21 #include <mutex>
22 
23 #include "i_input_device_listener.h"
24 #include "input_device.h"
25 #include "napi_constants.h"
26 
27 namespace OHOS {
28 namespace MMI {
29 class InputDeviceImpl final {
30 public:
31     static InputDeviceImpl& GetInstance();
32     DISALLOW_COPY_AND_MOVE(InputDeviceImpl);
33     ~InputDeviceImpl() = default;
34 
35     using FunInputDevInfo = std::function<void(std::shared_ptr<InputDevice>)>;
36     using FunInputDevIds = std::function<void(std::vector<int32_t>&)>;
37     using FunInputDevKeys = std::function<void(std::vector<bool>&)>;
38     using FunKeyboardTypes = std::function<void(int32_t)>;
39     using InputDevListenerPtr = std::shared_ptr<IInputDeviceListener>;
40     using FunSetInputDeviceAck = std::function<void(int32_t)>;
41 
42     int32_t RegisterDevListener(const std::string &type, InputDevListenerPtr listener);
43     int32_t UnregisterDevListener(const std::string &type, InputDevListenerPtr listener = nullptr);
44     int32_t GetInputDeviceIds(FunInputDevIds callback);
45     int32_t GetInputDevice(int32_t deviceId, FunInputDevInfo callback);
46     int32_t SupportKeys(int32_t deviceId, std::vector<int32_t> keyCodes, FunInputDevKeys callback);
47     int32_t GetKeyboardType(int32_t deviceId, FunKeyboardTypes callback);
48     int32_t SetKeyboardRepeatDelay(int32_t delay);
49     int32_t SetKeyboardRepeatRate(int32_t rate);
50     int32_t GetKeyboardRepeatDelay(std::function<void(int32_t)> callback);
51     int32_t GetKeyboardRepeatRate(std::function<void(int32_t)> callback);
52     void OnInputDevice(int32_t userData, std::shared_ptr<InputDevice> devData);
53     void OnInputDeviceIds(int32_t userData, std::vector<int32_t> &ids);
54     void OnSupportKeys(int32_t userData, std::vector<bool> &keystrokeAbility);
55     void OnDevListener(int32_t deviceId, const std::string &type);
56     void OnKeyboardType(int32_t userData, int32_t keyboardType);
57     int32_t GetUserData();
58     int32_t RegisterInputdevice(int32_t deviceId, bool enable, FunSetInputDeviceAck callback);
59     void OnSetInputDeviceAck(int32_t index, int32_t result);
60 
61     void OnConnected();
62     void OnDisconnected();
63 
64 private:
65     InputDeviceImpl() = default;
66     int32_t StartListeningToServer();
67     void StopListeningToServer();
68 
69     std::map<std::string, std::list<InputDevListenerPtr>> devListener_ {
70         { CHANGED_TYPE, {} }
71     };
72     int32_t userData_ { 0 };
73     bool isListeningProcess_ { false };
74     std::mutex mtx_;
75     std::map<int32_t, FunSetInputDeviceAck> inputdeviceList_;
76     int32_t operationIndex_ { 0 };
77 };
78 } // namespace MMI
79 } // namespace OHOS
80 #define INPUT_DEVICE_IMPL OHOS::MMI::InputDeviceImpl::GetInstance()
81 #endif // OHOS_INPUT_DEVICE_EVENT_H