• 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 #include "input_device_impl.h"
17 
18 #include "multimodal_input_connect_manager.h"
19 #include "bytrace_adapter.h"
20 
21 #undef MMI_LOG_TAG
22 #define MMI_LOG_TAG "InputDeviceImpl"
23 
24 namespace OHOS {
25 namespace MMI {
26 namespace {
27 constexpr std::string_view INPUT_DEV_CHANGE_ADD_DEV { "add" };
28 constexpr std::string_view INPUT_DEV_CHANGE_REMOVE_DEV { "remove" };
29 }
30 
GetInstance()31 InputDeviceImpl& InputDeviceImpl::GetInstance()
32 {
33     static InputDeviceImpl instance;
34     return instance;
35 }
36 
RegisterDevListener(const std::string & type,InputDevListenerPtr listener)37 int32_t InputDeviceImpl::RegisterDevListener(const std::string &type, InputDevListenerPtr listener)
38 {
39     CHKPR(listener, RET_ERR);
40     MMI_HILOGI("Register listener of change of input devices");
41     std::lock_guard guard(mtx_);
42 
43     auto iter = devListener_.find(type);
44     if (iter == devListener_.end()) {
45         MMI_HILOGE("Type of listener (%{public}s) is not supported", type.c_str());
46         return RET_ERR;
47     }
48     auto &listeners = iter->second;
49 
50     auto ret = StartListeningToServer();
51     if (ret != RET_OK) {
52         MMI_HILOGE("StartListeningToServer fail, error:%{public}d", ret);
53         return ret;
54     }
55     bool isNew = std::all_of(listeners.cbegin(), listeners.cend(),
56         [listener](InputDevListenerPtr tListener) {
57             return (tListener != listener);
58         });
59     if (isNew) {
60         listeners.push_back(listener);
61     }
62     MMI_HILOGI("Succeed to register listener of change of input devices");
63     return RET_OK;
64 }
65 
UnregisterDevListener(const std::string & type,InputDevListenerPtr listener)66 int32_t InputDeviceImpl::UnregisterDevListener(const std::string &type, InputDevListenerPtr listener)
67 {
68     std::lock_guard guard(mtx_);
69     auto iter = devListener_.find(type);
70     if (iter == devListener_.end()) {
71         MMI_HILOGE("Type of listener (%{public}s) is not supported", type.c_str());
72         return RET_ERR;
73     }
74     auto &listeners = iter->second;
75 
76     if (listener == nullptr) {
77         MMI_HILOGI("Unregister all listeners of change of input devices");
78         listeners.clear();
79     } else {
80         MMI_HILOGI("Unregister listener of change of input devices");
81         listeners.remove_if([listener](const auto &item) {
82             return (item == listener);
83         });
84     }
85     if (listeners.empty()) {
86         StopListeningToServer();
87     }
88     return RET_OK;
89 }
90 
OnDevListener(int32_t deviceId,const std::string & type)91 void InputDeviceImpl::OnDevListener(int32_t deviceId, const std::string &type)
92 {
93     CALL_DEBUG_ENTER;
94     MMI_HILOGI("Change(%{public}s) of input device(%{public}d)", type.c_str(), deviceId);
95     std::lock_guard guard(mtx_);
96     auto iter = devListener_.find(CHANGED_TYPE);
97     if (iter == devListener_.end()) {
98         MMI_HILOGE("Find change failed");
99         return;
100     }
101     BytraceAdapter::StartDevListener(type, deviceId);
102 
103     for (const auto &item : iter->second) {
104         if (type == INPUT_DEV_CHANGE_ADD_DEV) {
105             item->OnDeviceAdded(deviceId, type);
106         } else if (type == INPUT_DEV_CHANGE_REMOVE_DEV) {
107             item->OnDeviceRemoved(deviceId, type);
108         }
109     }
110     BytraceAdapter::StopDevListener();
111 }
112 
GetInputDeviceIds(FunInputDevIds callback)113 int32_t InputDeviceImpl::GetInputDeviceIds(FunInputDevIds callback)
114 {
115     CALL_DEBUG_ENTER;
116     CHKPR(callback, RET_ERR);
117     std::vector<int32_t> ids;
118     if (MULTIMODAL_INPUT_CONNECT_MGR->GetDeviceIds(ids) != RET_OK) {
119         MMI_HILOGE("GetInputDeviceIds failed");
120         return RET_ERR;
121     }
122     callback(ids);
123     return RET_OK;
124 }
125 
GetInputDevice(int32_t deviceId,FunInputDevInfo callback)126 int32_t InputDeviceImpl::GetInputDevice(int32_t deviceId, FunInputDevInfo callback)
127 {
128     CALL_DEBUG_ENTER;
129     CHKPR(callback, RET_ERR);
130     std::shared_ptr<InputDevice> inputDevice = std::make_shared<InputDevice>();
131     if (MULTIMODAL_INPUT_CONNECT_MGR->GetDevice(deviceId, inputDevice) != RET_OK) {
132         MMI_HILOGE("GetDevice failed");
133         return RET_ERR;
134     }
135     callback(inputDevice);
136     return RET_OK;
137 }
138 
SupportKeys(int32_t deviceId,std::vector<int32_t> keyCodes,FunInputDevKeys callback)139 int32_t InputDeviceImpl::SupportKeys(int32_t deviceId, std::vector<int32_t> keyCodes, FunInputDevKeys callback)
140 {
141     CALL_DEBUG_ENTER;
142     CHKPR(callback, RET_ERR);
143     std::vector<bool> keystroke;
144     if (MULTIMODAL_INPUT_CONNECT_MGR->SupportKeys(deviceId, keyCodes, keystroke) != RET_OK) {
145         MMI_HILOGE("SupportKeys failed");
146         return RET_ERR;
147     }
148     callback(keystroke);
149     return RET_OK;
150 }
151 
GetKeyboardType(int32_t deviceId,FunKeyboardTypes callback)152 int32_t InputDeviceImpl::GetKeyboardType(int32_t deviceId, FunKeyboardTypes callback)
153 {
154     CALL_DEBUG_ENTER;
155     CHKPR(callback, RET_ERR);
156     int32_t keyboardType = 0;
157     if (MULTIMODAL_INPUT_CONNECT_MGR->GetKeyboardType(deviceId, keyboardType) != RET_OK) {
158         MMI_HILOGE("GetKeyboardType failed");
159         return RET_ERR;
160     }
161     callback(keyboardType);
162     return RET_OK;
163 }
164 
SetKeyboardRepeatDelay(int32_t delay)165 int32_t InputDeviceImpl::SetKeyboardRepeatDelay(int32_t delay)
166 {
167     CALL_DEBUG_ENTER;
168     if (MULTIMODAL_INPUT_CONNECT_MGR->SetKeyboardRepeatDelay(delay) != RET_OK) {
169         MMI_HILOGE("SetKeyboardRepeatDelay failed");
170         return RET_ERR;
171     }
172     return RET_OK;
173 }
174 
SetKeyboardRepeatRate(int32_t rate)175 int32_t InputDeviceImpl::SetKeyboardRepeatRate(int32_t rate)
176 {
177     CALL_DEBUG_ENTER;
178     if (MULTIMODAL_INPUT_CONNECT_MGR->SetKeyboardRepeatRate(rate) != RET_OK) {
179         MMI_HILOGE("SetKeyboardRepeatRate failed");
180         return RET_ERR;
181     }
182     return RET_OK;
183 }
184 
GetKeyboardRepeatDelay(std::function<void (int32_t)> callback)185 int32_t InputDeviceImpl::GetKeyboardRepeatDelay(std::function<void(int32_t)> callback)
186 {
187     CALL_DEBUG_ENTER;
188     CHKPR(callback, RET_ERR);
189     int32_t repeatDelay = 0;
190     if (MULTIMODAL_INPUT_CONNECT_MGR->GetKeyboardRepeatDelay(repeatDelay) != RET_OK) {
191         MMI_HILOGE("GetKeyboardRepeatDelay failed");
192         return RET_ERR;
193     }
194     callback(repeatDelay);
195     return RET_OK;
196 }
197 
GetKeyboardRepeatRate(std::function<void (int32_t)> callback)198 int32_t InputDeviceImpl::GetKeyboardRepeatRate(std::function<void(int32_t)> callback)
199 {
200     CALL_DEBUG_ENTER;
201     int32_t repeatRate = 0;
202     if (MULTIMODAL_INPUT_CONNECT_MGR->GetKeyboardRepeatRate(repeatRate) != RET_OK) {
203         MMI_HILOGE("GetKeyboardRepeatRate failed");
204         return RET_ERR;
205     }
206     callback(repeatRate);
207     return RET_OK;
208 }
209 
GetUserData()210 int32_t InputDeviceImpl::GetUserData()
211 {
212     return userData_;
213 }
214 
RegisterInputdevice(int32_t deviceId,bool enable,std::function<void (int32_t)> callback)215 int32_t InputDeviceImpl::RegisterInputdevice(int32_t deviceId, bool enable, std::function<void(int32_t)> callback)
216 {
217     CALL_DEBUG_ENTER;
218     CHKPR(callback, RET_ERR);
219     int32_t _id = operationIndex_++;
220     inputdeviceList_[_id] = callback;
221     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->SetInputDeviceEnabled(deviceId, enable, _id);
222     if (ret != RET_OK) {
223         MMI_HILOGE("Failed to register");
224         return ret;
225     }
226     return RET_OK;
227 }
228 
OnSetInputDeviceAck(int32_t index,int32_t result)229 void InputDeviceImpl::OnSetInputDeviceAck(int32_t index, int32_t result)
230 {
231     CALL_DEBUG_ENTER;
232     std::lock_guard guard(mtx_);
233     auto iter = inputdeviceList_.find(index);
234     if (iter == inputdeviceList_.end()) {
235         MMI_HILOGE("Find index failed");
236         return;
237     }
238     iter->second(result);
239     inputdeviceList_.erase(index);
240 }
241 
OnConnected()242 void InputDeviceImpl::OnConnected()
243 {
244     std::lock_guard guard(mtx_);
245     auto iter = devListener_.find(CHANGED_TYPE);
246     if ((iter == devListener_.end()) || iter->second.empty()) {
247         return;
248     }
249     auto ret = StartListeningToServer();
250     if (ret != RET_OK) {
251         MMI_HILOGE("StartListeningToServer fail, error:%{public}d", ret);
252     }
253 }
254 
OnDisconnected()255 void InputDeviceImpl::OnDisconnected()
256 {
257     MMI_HILOGI("Disconnected from server");
258     std::lock_guard guard(mtx_);
259     isListeningProcess_ = false;
260 }
261 
StartListeningToServer()262 int32_t InputDeviceImpl::StartListeningToServer()
263 {
264     if (isListeningProcess_) {
265         return RET_OK;
266     }
267     MMI_HILOGI("Start monitoring changes of input devices");
268     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->RegisterDevListener();
269     if (ret != RET_OK) {
270         MMI_HILOGE("RegisterDevListener to server fail, error:%{public}d", ret);
271         return ret;
272     }
273     isListeningProcess_ = true;
274     return RET_OK;
275 }
276 
StopListeningToServer()277 void InputDeviceImpl::StopListeningToServer()
278 {
279     if (!isListeningProcess_) {
280         return;
281     }
282     MMI_HILOGI("Stop monitoring changes of input devices");
283     auto ret = MULTIMODAL_INPUT_CONNECT_MGR->UnregisterDevListener();
284     if (ret != RET_OK) {
285         MMI_HILOGE("UnregisterDevListener from server fail, error:%{public}d", ret);
286     }
287     isListeningProcess_ = false;
288 }
289 } // namespace MMI
290 } // namespace OHOS
291