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 #include "input_device_impl.h"
17 
18 #include <algorithm>
19 
20 #include "mmi_log.h"
21 #include "multimodal_event_handler.h"
22 #include "multimodal_input_connect_manager.h"
23 #include "bytrace_adapter.h"
24 #include "napi_constants.h"
25 #include "net_packet.h"
26 
27 #undef MMI_LOG_TAG
28 #define MMI_LOG_TAG "InputDeviceImpl"
29 
30 namespace OHOS {
31 namespace MMI {
GetInstance()32 InputDeviceImpl& InputDeviceImpl::GetInstance()
33 {
34     static InputDeviceImpl instance;
35     return instance;
36 }
37 
RegisterDevListener(const std::string & type,InputDevListenerPtr listener)38 int32_t InputDeviceImpl::RegisterDevListener(const std::string &type, InputDevListenerPtr listener)
39 {
40     CALL_DEBUG_ENTER;
41     CHKPR(listener, RET_ERR);
42     if (type != CHANGED_TYPE) {
43         MMI_HILOGE("Failed to register, listener event must be \"change\"");
44         return RET_ERR;
45     }
46     auto iter = devListener_.find(CHANGED_TYPE);
47     if (iter == devListener_.end()) {
48         MMI_HILOGE("Find change failed");
49         return RET_ERR;
50     }
51     auto &listeners = iter->second;
52 
53     if (!isListeningProcess_) {
54         MMI_HILOGI("Start monitoring");
55         isListeningProcess_ = true;
56         int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->RegisterDevListener();
57         if (ret != RET_OK) {
58             MMI_HILOGE("Failed to register");
59             return ret;
60         }
61     }
62     if (std::all_of(listeners.cbegin(), listeners.cend(),
63                     [listener](InputDevListenerPtr tListener) {
64                         return (tListener != listener);
65                     })) {
66         MMI_HILOGI("Add device listener");
67         listeners.push_back(listener);
68     } else {
69         MMI_HILOGW("The listener already exists");
70     }
71     return RET_OK;
72 }
73 
UnregisterDevListener(const std::string & type,InputDevListenerPtr listener)74 int32_t InputDeviceImpl::UnregisterDevListener(const std::string &type, InputDevListenerPtr listener)
75 {
76     CALL_DEBUG_ENTER;
77     if (type != CHANGED_TYPE) {
78         MMI_HILOGE("Failed to cancel registration, listener event must be \"change\"");
79         return RET_ERR;
80     }
81     auto iter = devListener_.find(CHANGED_TYPE);
82     if (iter == devListener_.end()) {
83         MMI_HILOGE("Find change failed");
84         return RET_ERR;
85     }
86     if (listener == nullptr) {
87         iter->second.clear();
88         goto listenerLabel;
89     }
90     for (auto it = iter->second.begin(); it != iter->second.end(); ++it) {
91         if (*it == listener) {
92             iter->second.erase(it);
93             goto listenerLabel;
94         }
95     }
96 
97 listenerLabel:
98     if (isListeningProcess_ && iter->second.empty()) {
99         isListeningProcess_ = false;
100         return MULTIMODAL_INPUT_CONNECT_MGR->UnregisterDevListener();
101     }
102     return RET_OK;
103 }
104 
OnDevListener(int32_t deviceId,const std::string & type)105 void InputDeviceImpl::OnDevListener(int32_t deviceId, const std::string &type)
106 {
107     CALL_DEBUG_ENTER;
108     std::lock_guard<std::mutex> guard(mtx_);
109     auto iter = devListener_.find("change");
110     if (iter == devListener_.end()) {
111         MMI_HILOGE("Find change failed");
112         return;
113     }
114     for (const auto &item : iter->second) {
115         if (type == "add") {
116             item->OnDeviceAdded(deviceId, type);
117             continue;
118         }
119         item->OnDeviceRemoved(deviceId, type);
120         BytraceAdapter::StartDevListener(type, deviceId);
121         MMI_HILOGI("Report device change task, event type:%{public}s, eventid:%{public}d", type.c_str(), deviceId);
122         BytraceAdapter::StopDevListener();
123     }
124 }
125 
GetInputDeviceIds(FunInputDevIds callback)126 int32_t InputDeviceImpl::GetInputDeviceIds(FunInputDevIds callback)
127 {
128     CALL_DEBUG_ENTER;
129     CHKPR(callback, RET_ERR);
130     std::vector<int32_t> ids;
131     if (MULTIMODAL_INPUT_CONNECT_MGR->GetDeviceIds(ids) != RET_OK) {
132         MMI_HILOGE("GetInputDeviceIds failed");
133         return RET_ERR;
134     }
135     callback(ids);
136     return RET_OK;
137 }
138 
GetInputDevice(int32_t deviceId,FunInputDevInfo callback)139 int32_t InputDeviceImpl::GetInputDevice(int32_t deviceId, FunInputDevInfo callback)
140 {
141     CALL_DEBUG_ENTER;
142     CHKPR(callback, RET_ERR);
143     std::shared_ptr<InputDevice> inputDevice = std::make_shared<InputDevice>();
144     if (MULTIMODAL_INPUT_CONNECT_MGR->GetDevice(deviceId, inputDevice) != RET_OK) {
145         MMI_HILOGE("GetDevice failed");
146         return RET_ERR;
147     }
148     callback(inputDevice);
149     return RET_OK;
150 }
151 
SupportKeys(int32_t deviceId,std::vector<int32_t> keyCodes,FunInputDevKeys callback)152 int32_t InputDeviceImpl::SupportKeys(int32_t deviceId, std::vector<int32_t> keyCodes, FunInputDevKeys callback)
153 {
154     CALL_DEBUG_ENTER;
155     CHKPR(callback, RET_ERR);
156     std::vector<bool> keystroke;
157     if (MULTIMODAL_INPUT_CONNECT_MGR->SupportKeys(deviceId, keyCodes, keystroke) != RET_OK) {
158         MMI_HILOGE("SupportKeys failed");
159         return RET_ERR;
160     }
161     callback(keystroke);
162     return RET_OK;
163 }
164 
GetKeyboardType(int32_t deviceId,FunKeyboardTypes callback)165 int32_t InputDeviceImpl::GetKeyboardType(int32_t deviceId, FunKeyboardTypes callback)
166 {
167     CALL_DEBUG_ENTER;
168     CHKPR(callback, RET_ERR);
169     int32_t keyboardType = 0;
170     if (MULTIMODAL_INPUT_CONNECT_MGR->GetKeyboardType(deviceId, keyboardType) != RET_OK) {
171         MMI_HILOGE("GetKeyboardType failed");
172         return RET_ERR;
173     }
174     callback(keyboardType);
175     return RET_OK;
176 }
177 
SetKeyboardRepeatDelay(int32_t delay)178 int32_t InputDeviceImpl::SetKeyboardRepeatDelay(int32_t delay)
179 {
180     CALL_DEBUG_ENTER;
181     if (MULTIMODAL_INPUT_CONNECT_MGR->SetKeyboardRepeatDelay(delay) != RET_OK) {
182         MMI_HILOGE("SetKeyboardRepeatDelay failed");
183         return RET_ERR;
184     }
185     return RET_OK;
186 }
187 
SetKeyboardRepeatRate(int32_t rate)188 int32_t InputDeviceImpl::SetKeyboardRepeatRate(int32_t rate)
189 {
190     CALL_DEBUG_ENTER;
191     if (MULTIMODAL_INPUT_CONNECT_MGR->SetKeyboardRepeatRate(rate) != RET_OK) {
192         MMI_HILOGE("SetKeyboardRepeatRate failed");
193         return RET_ERR;
194     }
195     return RET_OK;
196 }
197 
GetKeyboardRepeatDelay(std::function<void (int32_t)> callback)198 int32_t InputDeviceImpl::GetKeyboardRepeatDelay(std::function<void(int32_t)> callback)
199 {
200     CALL_DEBUG_ENTER;
201     CHKPR(callback, RET_ERR);
202     int32_t repeatDelay = 0;
203     if (MULTIMODAL_INPUT_CONNECT_MGR->GetKeyboardRepeatDelay(repeatDelay) != RET_OK) {
204         MMI_HILOGE("GetKeyboardRepeatDelay failed");
205         return RET_ERR;
206     }
207     callback(repeatDelay);
208     return RET_OK;
209 }
210 
GetKeyboardRepeatRate(std::function<void (int32_t)> callback)211 int32_t InputDeviceImpl::GetKeyboardRepeatRate(std::function<void(int32_t)> callback)
212 {
213     CALL_DEBUG_ENTER;
214     int32_t repeatRate = 0;
215     if (MULTIMODAL_INPUT_CONNECT_MGR->GetKeyboardRepeatRate(repeatRate) != RET_OK) {
216         MMI_HILOGE("GetKeyboardRepeatRate failed");
217         return RET_ERR;
218     }
219     callback(repeatRate);
220     return RET_OK;
221 }
222 
GetUserData()223 int32_t InputDeviceImpl::GetUserData()
224 {
225     return userData_;
226 }
227 } // namespace MMI
228 } // namespace OHOS
229