• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #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 "napi_constants.h"
24 #include "net_packet.h"
25 
26 namespace OHOS {
27 namespace MMI {
28 namespace {
29 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "InputDeviceImpl" };
30 } // namespace
31 
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 = MultimodalInputConnMgr->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 MultimodalInputConnMgr->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         MMI_HILOGI("Report device change task, event type:%{public}s", type.c_str());
116         if (type == "add") {
117             item->OnDeviceAdded(deviceId, type);
118             continue;
119         }
120         item->OnDeviceRemoved(deviceId, type);
121     }
122 }
123 
GetInputDeviceIds(FunInputDevIds callback)124 int32_t InputDeviceImpl::GetInputDeviceIds(FunInputDevIds callback)
125 {
126     CALL_DEBUG_ENTER;
127     CHKPR(callback, RET_ERR);
128     std::vector<int32_t> ids;
129     int32_t ret = MultimodalInputConnMgr->GetDeviceIds(ids);
130     if (ret != RET_OK) {
131         MMI_HILOGE("GetInputDeviceIds failed");
132         return RET_ERR;
133     }
134     callback(ids);
135     return ret;
136 }
137 
GetInputDevice(int32_t deviceId,FunInputDevInfo callback)138 int32_t InputDeviceImpl::GetInputDevice(int32_t deviceId, FunInputDevInfo callback)
139 {
140     CALL_DEBUG_ENTER;
141     CHKPR(callback, RET_ERR);
142     std::shared_ptr<InputDevice> inputDevice = std::make_shared<InputDevice>();
143     int32_t ret = MultimodalInputConnMgr->GetDevice(deviceId, inputDevice);
144     if (ret != 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     int32_t ret = MultimodalInputConnMgr->SupportKeys(deviceId, keyCodes, keystroke);
158     if (ret != RET_OK) {
159         MMI_HILOGE("SupportKeys failed");
160         return RET_ERR;
161     }
162     callback(keystroke);
163     return RET_OK;
164 }
165 
GetKeyboardType(int32_t deviceId,FunKeyboardTypes callback)166 int32_t InputDeviceImpl::GetKeyboardType(int32_t deviceId, FunKeyboardTypes callback)
167 {
168     CALL_DEBUG_ENTER;
169     CHKPR(callback, RET_ERR);
170     int32_t keyboardType = 0;
171     int32_t ret = MultimodalInputConnMgr->GetKeyboardType(deviceId, keyboardType);
172     if (ret != RET_OK) {
173         MMI_HILOGE("GetKeyboardType failed");
174         return RET_ERR;
175     }
176     callback(keyboardType);
177     return RET_OK;
178 }
179 
SetKeyboardRepeatDelay(int32_t delay)180 int32_t InputDeviceImpl::SetKeyboardRepeatDelay(int32_t delay)
181 {
182     CALL_DEBUG_ENTER;
183     int32_t ret = MultimodalInputConnMgr->SetKeyboardRepeatDelay(delay);
184     if (ret != RET_OK) {
185         MMI_HILOGE("SetKeyboardRepeatDelay failed");
186         return RET_ERR;
187     }
188     return RET_OK;
189 }
190 
SetKeyboardRepeatRate(int32_t rate)191 int32_t InputDeviceImpl::SetKeyboardRepeatRate(int32_t rate)
192 {
193     CALL_DEBUG_ENTER;
194     int32_t ret = MultimodalInputConnMgr->SetKeyboardRepeatRate(rate);
195     if (ret != RET_OK) {
196         MMI_HILOGE("SetKeyboardRepeatRate failed");
197         return RET_ERR;
198     }
199     return RET_OK;
200 }
201 
GetKeyboardRepeatDelay(std::function<void (int32_t)> callback)202 int32_t InputDeviceImpl::GetKeyboardRepeatDelay(std::function<void(int32_t)> callback)
203 {
204     CALL_DEBUG_ENTER;
205     CHKPR(callback, RET_ERR);
206     int32_t repeatDelay = 0;
207     int32_t ret = MultimodalInputConnMgr->GetKeyboardRepeatDelay(repeatDelay);
208     if (ret != RET_OK) {
209         MMI_HILOGE("GetKeyboardRepeatDelay failed");
210         return RET_ERR;
211     }
212     callback(repeatDelay);
213     return RET_OK;
214 }
215 
GetKeyboardRepeatRate(std::function<void (int32_t)> callback)216 int32_t InputDeviceImpl::GetKeyboardRepeatRate(std::function<void(int32_t)> callback)
217 {
218     CALL_DEBUG_ENTER;
219     int32_t repeatRate = 0;
220     int32_t ret = MultimodalInputConnMgr->GetKeyboardRepeatRate(repeatRate);
221     if (ret != RET_OK) {
222         MMI_HILOGE("GetKeyboardRepeatRate failed");
223         return RET_ERR;
224     }
225     callback(repeatRate);
226     return RET_OK;
227 }
228 
GetUserData()229 int32_t InputDeviceImpl::GetUserData()
230 {
231     return userData_;
232 }
233 } // namespace MMI
234 } // namespace OHOS
235