• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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_manager_impl.h"
17 
18 #include <cinttypes>
19 
20 #include "define_multimodal.h"
21 #include "error_multimodal.h"
22 
23 #include "bytrace_adapter.h"
24 #include "event_filter_service.h"
25 #include "input_device_cooperate_impl.h"
26 #include "mmi_client.h"
27 #include "multimodal_event_handler.h"
28 #include "multimodal_input_connect_manager.h"
29 
30 namespace OHOS {
31 namespace MMI {
32 namespace {
33 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "InputManagerImpl" };
34 } // namespace
35 
36 struct MonitorEventConsumer : public IInputEventConsumer {
MonitorEventConsumerOHOS::MMI::MonitorEventConsumer37     explicit MonitorEventConsumer(const std::function<void(std::shared_ptr<PointerEvent>)> &monitor)
38         : monitor_ (monitor) {}
39 
MonitorEventConsumerOHOS::MMI::MonitorEventConsumer40     explicit MonitorEventConsumer(const std::function<void(std::shared_ptr<KeyEvent>)> &monitor)
41         : keyMonitor_ (monitor) {}
42 
OnInputEventOHOS::MMI::MonitorEventConsumer43     void OnInputEvent(std::shared_ptr<KeyEvent> keyEvent) const
44     {
45         CHKPV(keyEvent);
46         CHKPV(keyMonitor_);
47         keyMonitor_(keyEvent);
48     }
49 
OnInputEventOHOS::MMI::MonitorEventConsumer50     void OnInputEvent(std::shared_ptr<PointerEvent> pointerEvent) const
51     {
52         CHKPV(pointerEvent);
53         CHKPV(monitor_);
54         monitor_(pointerEvent);
55     }
56 
OnInputEventOHOS::MMI::MonitorEventConsumer57     void OnInputEvent(std::shared_ptr<AxisEvent> axisEvent) const
58     {
59         CHKPV(axisEvent);
60         CHKPV(axisMonitor_);
61         axisMonitor_(axisEvent);
62     }
63 
64 private:
65     std::function<void(std::shared_ptr<PointerEvent>)> monitor_;
66     std::function<void(std::shared_ptr<KeyEvent>)> keyMonitor_;
67     std::function<void(std::shared_ptr<AxisEvent>)> axisMonitor_;
68 };
69 
InputManagerImpl()70 InputManagerImpl::InputManagerImpl() {}
~InputManagerImpl()71 InputManagerImpl::~InputManagerImpl() {}
72 
UpdateDisplayInfo(const DisplayGroupInfo & displayGroupInfo)73 void InputManagerImpl::UpdateDisplayInfo(const DisplayGroupInfo &displayGroupInfo)
74 {
75     CALL_DEBUG_ENTER;
76     std::lock_guard<std::mutex> guard(mtx_);
77     if (!MMIEventHdl.InitClient()) {
78         MMI_HILOGE("Get mmi client is nullptr");
79         return;
80     }
81     if (displayGroupInfo.windowsInfo.empty() || displayGroupInfo.displaysInfo.empty()) {
82         MMI_HILOGE("The windows info or display info is empty!");
83         return;
84     }
85     for (const auto &item : displayGroupInfo.windowsInfo) {
86         if ((item.defaultHotAreas.size() > WindowInfo::MAX_HOTAREA_COUNT) ||
87             (item.pointerHotAreas.size() > WindowInfo::MAX_HOTAREA_COUNT) ||
88             item.defaultHotAreas.empty() || item.pointerHotAreas.empty()) {
89             MMI_HILOGE("Hot areas check failed! defaultHotAreas:size:%{public}zu,"
90                        "pointerHotAreas:size:%{public}zu",
91                        item.defaultHotAreas.size(), item.pointerHotAreas.size());
92             return;
93         }
94     }
95     displayGroupInfo_ = displayGroupInfo;
96     SendDisplayInfo();
97     PrintDisplayInfo();
98 }
99 
AddInputEventFilter(std::function<bool (std::shared_ptr<PointerEvent>)> filter)100 int32_t InputManagerImpl::AddInputEventFilter(std::function<bool(std::shared_ptr<PointerEvent>)> filter)
101 {
102     CALL_INFO_TRACE;
103 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
104     std::lock_guard<std::mutex> guard(mtx_);
105     bool hasSendToMmiServer = true;
106     if (eventFilterService_ == nullptr) {
107         hasSendToMmiServer = false;
108         eventFilterService_ = new (std::nothrow) EventFilterService();
109         CHKPR(eventFilterService_, RET_ERR);
110     }
111 
112     eventFilterService_->SetPointerEventPtr(filter);
113     if (!hasSendToMmiServer) {
114         int32_t ret = MultimodalInputConnMgr->AddInputEventFilter(eventFilterService_);
115         if (ret != RET_OK) {
116             MMI_HILOGE("AddInputEventFilter has send to server failed, ret:%{public}d", ret);
117             eventFilterService_ = nullptr;
118             return RET_ERR;
119         }
120         return RET_OK;
121     }
122     return RET_OK;
123 #else
124     MMI_HILOGW("Pointer and touchscreen device does not support");
125     return ERROR_UNSUPPORT;
126 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
127 }
128 
SetWindowInputEventConsumer(std::shared_ptr<IInputEventConsumer> inputEventConsumer,std::shared_ptr<AppExecFwk::EventHandler> eventHandler)129 void InputManagerImpl::SetWindowInputEventConsumer(std::shared_ptr<IInputEventConsumer> inputEventConsumer,
130     std::shared_ptr<AppExecFwk::EventHandler> eventHandler)
131 {
132     CALL_INFO_TRACE;
133     CHK_PID_AND_TID();
134     CHKPV(inputEventConsumer);
135     CHKPV(eventHandler);
136     std::lock_guard<std::mutex> guard(mtx_);
137     if (!MMIEventHdl.InitClient(eventHandler)) {
138         MMI_HILOGE("Client init failed");
139         return;
140     }
141     consumer_ = inputEventConsumer;
142     eventHandler_ = eventHandler;
143 }
144 
SubscribeKeyEvent(std::shared_ptr<KeyOption> keyOption,std::function<void (std::shared_ptr<KeyEvent>)> callback)145 int32_t InputManagerImpl::SubscribeKeyEvent(std::shared_ptr<KeyOption> keyOption,
146     std::function<void(std::shared_ptr<KeyEvent>)> callback)
147 {
148     CALL_INFO_TRACE;
149     CHK_PID_AND_TID();
150     std::lock_guard<std::mutex> guard(mtx_);
151 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
152     CHKPR(keyOption, RET_ERR);
153     CHKPR(callback, RET_ERR);
154     return KeyEventInputSubscribeMgr.SubscribeKeyEvent(keyOption, callback);
155 #else
156     MMI_HILOGW("Keyboard device does not support");
157     return ERROR_UNSUPPORT;
158 #endif // OHOS_BUILD_ENABLE_KEYBOARD
159 }
160 
UnsubscribeKeyEvent(int32_t subscriberId)161 void InputManagerImpl::UnsubscribeKeyEvent(int32_t subscriberId)
162 {
163     CALL_INFO_TRACE;
164     CHK_PID_AND_TID();
165     std::lock_guard<std::mutex> guard(mtx_);
166 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
167     KeyEventInputSubscribeMgr.UnsubscribeKeyEvent(subscriberId);
168 #else
169     MMI_HILOGW("Keyboard device does not support");
170 #endif // OHOS_BUILD_ENABLE_KEYBOARD
171 }
172 
173 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
OnKeyEventTask(std::shared_ptr<IInputEventConsumer> consumer,std::shared_ptr<KeyEvent> keyEvent)174 void InputManagerImpl::OnKeyEventTask(std::shared_ptr<IInputEventConsumer> consumer,
175     std::shared_ptr<KeyEvent> keyEvent)
176 {
177     CHK_PID_AND_TID();
178     CHKPV(consumer);
179     consumer->OnInputEvent(keyEvent);
180     MMI_HILOGD("Key event callback keyCode:%{public}d", keyEvent->GetKeyCode());
181 }
182 
OnKeyEvent(std::shared_ptr<KeyEvent> keyEvent)183 void InputManagerImpl::OnKeyEvent(std::shared_ptr<KeyEvent> keyEvent)
184 {
185     CHK_PID_AND_TID();
186     CHKPV(keyEvent);
187     CHKPV(eventHandler_);
188     CHKPV(consumer_);
189     std::shared_ptr<AppExecFwk::EventHandler> eventHandler = nullptr;
190     std::shared_ptr<IInputEventConsumer> inputConsumer = nullptr;
191     {
192         std::lock_guard<std::mutex> guard(mtx_);
193         eventHandler = eventHandler_;
194         inputConsumer = consumer_;
195     }
196     BytraceAdapter::StartBytrace(keyEvent, BytraceAdapter::TRACE_STOP, BytraceAdapter::KEY_DISPATCH_EVENT);
197     MMIClientPtr client = MMIEventHdl.GetMMIClient();
198     CHKPV(client);
199     if (client->IsEventHandlerChanged()) {
200         if (!eventHandler->PostHighPriorityTask(std::bind(&InputManagerImpl::OnKeyEventTask,
201             this, inputConsumer, keyEvent))) {
202             MMI_HILOGE("Post task failed");
203             return;
204         }
205     } else {
206         inputConsumer->OnInputEvent(keyEvent);
207         MMI_HILOGD("Key event report keyCode:%{public}d", keyEvent->GetKeyCode());
208     }
209     MMI_HILOGD("Key event keyCode:%{public}d", keyEvent->GetKeyCode());
210 }
211 #endif // OHOS_BUILD_ENABLE_KEYBOARD
212 
213 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
OnPointerEventTask(std::shared_ptr<IInputEventConsumer> consumer,std::shared_ptr<PointerEvent> pointerEvent)214 void InputManagerImpl::OnPointerEventTask(std::shared_ptr<IInputEventConsumer> consumer,
215     std::shared_ptr<PointerEvent> pointerEvent)
216 {
217     CHK_PID_AND_TID();
218     CHKPV(consumer);
219     CHKPV(pointerEvent);
220     consumer->OnInputEvent(pointerEvent);
221     MMI_HILOGD("Pointer event callback pointerId:%{public}d", pointerEvent->GetPointerId());
222 }
223 
OnPointerEvent(std::shared_ptr<PointerEvent> pointerEvent)224 void InputManagerImpl::OnPointerEvent(std::shared_ptr<PointerEvent> pointerEvent)
225 {
226     CALL_DEBUG_ENTER;
227     CHK_PID_AND_TID();
228     CHKPV(pointerEvent);
229     CHKPV(eventHandler_);
230     CHKPV(consumer_);
231     std::shared_ptr<AppExecFwk::EventHandler> eventHandler = nullptr;
232     std::shared_ptr<IInputEventConsumer> inputConsumer = nullptr;
233     {
234         std::lock_guard<std::mutex> guard(mtx_);
235         eventHandler = eventHandler_;
236         inputConsumer = consumer_;
237     }
238     BytraceAdapter::StartBytrace(pointerEvent, BytraceAdapter::TRACE_STOP, BytraceAdapter::POINT_DISPATCH_EVENT);
239     MMIClientPtr client = MMIEventHdl.GetMMIClient();
240     CHKPV(client);
241     if (client->IsEventHandlerChanged()) {
242         if (!eventHandler->PostHighPriorityTask(std::bind(&InputManagerImpl::OnPointerEventTask,
243             this, inputConsumer, pointerEvent))) {
244             MMI_HILOGE("Post task failed");
245             return;
246         }
247     } else {
248         inputConsumer->OnInputEvent(pointerEvent);
249         MMI_HILOGD("Pointer event report pointerId:%{public}d", pointerEvent->GetPointerId());
250     }
251     MMI_HILOGD("Pointer event pointerId:%{public}d", pointerEvent->GetPointerId());
252 }
253 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
254 
PackDisplayData(NetPacket & pkt)255 int32_t InputManagerImpl::PackDisplayData(NetPacket &pkt)
256 {
257     pkt << displayGroupInfo_.width << displayGroupInfo_.height << displayGroupInfo_.focusWindowId;
258     if (pkt.ChkRWError()) {
259         MMI_HILOGE("Packet write logical data failed");
260         return RET_ERR;
261     }
262     if (PackWindowInfo(pkt) == RET_ERR) {
263         MMI_HILOGE("Packet write windows info failed");
264         return RET_ERR;
265     }
266     return PackDisplayInfo(pkt);
267 }
268 
PackWindowInfo(NetPacket & pkt)269 int32_t InputManagerImpl::PackWindowInfo(NetPacket &pkt)
270 {
271     uint32_t num = static_cast<uint32_t>(displayGroupInfo_.windowsInfo.size());
272     pkt << num;
273     for (const auto &item : displayGroupInfo_.windowsInfo) {
274         pkt << item.id << item.pid << item.uid << item.area
275             << item.defaultHotAreas << item.pointerHotAreas
276             << item.agentWindowId << item.flags;
277     }
278     if (pkt.ChkRWError()) {
279         MMI_HILOGE("Packet write windows data failed");
280         return RET_ERR;
281     }
282     return RET_OK;
283 }
284 
PackDisplayInfo(NetPacket & pkt)285 int32_t InputManagerImpl::PackDisplayInfo(NetPacket &pkt)
286 {
287     uint32_t num = static_cast<uint32_t>(displayGroupInfo_.displaysInfo.size());
288     pkt << num;
289     for (const auto &item : displayGroupInfo_.displaysInfo) {
290         pkt << item.id << item.x << item.y << item.width
291             << item.height << item.name << item.uniq << item.direction;
292     }
293     if (pkt.ChkRWError()) {
294         MMI_HILOGE("Packet write display data failed");
295         return RET_ERR;
296     }
297     return RET_OK;
298 }
299 
PrintDisplayInfo()300 void InputManagerImpl::PrintDisplayInfo()
301 {
302     MMI_HILOGI("logicalInfo,width:%{public}d,height:%{public}d,focusWindowId:%{public}d",
303         displayGroupInfo_.width, displayGroupInfo_.height, displayGroupInfo_.focusWindowId);
304     MMI_HILOGI("windowsInfos,num:%{public}zu", displayGroupInfo_.windowsInfo.size());
305     for (const auto &item : displayGroupInfo_.windowsInfo) {
306         MMI_HILOGI("windowsInfos,id:%{public}d,pid:%{public}d,uid:%{public}d,"
307             "area.x:%{public}d,area.y:%{public}d,area.width:%{public}d,area.height:%{public}d,"
308             "defaultHotAreas.size:%{public}zu,pointerHotAreas.size:%{public}zu,"
309             "agentWindowId:%{public}d,flags:%{public}d",
310             item.id, item.pid, item.uid, item.area.x, item.area.y, item.area.width,
311             item.area.height, item.defaultHotAreas.size(), item.pointerHotAreas.size(),
312             item.agentWindowId, item.flags);
313         for (const auto &win : item.defaultHotAreas) {
314             MMI_HILOGI("defaultHotAreas:x:%{public}d,y:%{public}d,width:%{public}d,height:%{public}d",
315                 win.x, win.y, win.width, win.height);
316         }
317         for (const auto &pointer : item.pointerHotAreas) {
318             MMI_HILOGI("pointerHotAreas:x:%{public}d,y:%{public}d,width:%{public}d,height:%{public}d",
319                 pointer.x, pointer.y, pointer.width, pointer.height);
320         }
321     }
322 
323     MMI_HILOGI("displayInfos,num:%{public}zu", displayGroupInfo_.displaysInfo.size());
324     for (const auto &item : displayGroupInfo_.displaysInfo) {
325         MMI_HILOGI("displayInfos,id:%{public}d,x:%{public}d,y:%{public}d,"
326             "width:%{public}d,height:%{public}d,name:%{public}s,"
327             "uniq:%{public}s,direction:%{public}d",
328             item.id, item.x, item.y, item.width, item.height, item.name.c_str(),
329             item.uniq.c_str(), item.direction);
330     }
331 }
332 
AddMonitor(std::function<void (std::shared_ptr<KeyEvent>)> monitor)333 int32_t InputManagerImpl::AddMonitor(std::function<void(std::shared_ptr<KeyEvent>)> monitor)
334 {
335     CALL_INFO_TRACE;
336 #if defined(OHOS_BUILD_ENABLE_KEYBOARD) && defined(OHOS_BUILD_ENABLE_MONITOR)
337     CHKPR(monitor, INVALID_HANDLER_ID);
338     auto consumer = std::make_shared<MonitorEventConsumer>(monitor);
339     return AddMonitor(consumer);
340 #else
341     MMI_HILOGW("Keyboard device or monitor function does not support");
342     return ERROR_UNSUPPORT;
343 #endif // OHOS_BUILD_ENABLE_KEYBOARD || OHOS_BUILD_ENABLE_MONITOR
344 }
345 
AddMonitor(std::function<void (std::shared_ptr<PointerEvent>)> monitor)346 int32_t InputManagerImpl::AddMonitor(std::function<void(std::shared_ptr<PointerEvent>)> monitor)
347 {
348     CALL_INFO_TRACE;
349 #if (defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)) && defined(OHOS_BUILD_ENABLE_MONITOR)
350     CHKPR(monitor, INVALID_HANDLER_ID);
351     auto consumer = std::make_shared<MonitorEventConsumer>(monitor);
352     return AddMonitor(consumer);
353 #else
354     MMI_HILOGW("Pointer/touchscreen device or monitor function does not support");
355     return ERROR_UNSUPPORT;
356 #endif // OHOS_BUILD_ENABLE_MONITOR ||  OHOS_BUILD_ENABLE_TOUCH && OHOS_BUILD_ENABLE_MONITOR
357 }
358 
AddMonitor(std::shared_ptr<IInputEventConsumer> consumer)359 int32_t InputManagerImpl::AddMonitor(std::shared_ptr<IInputEventConsumer> consumer)
360 {
361     CALL_INFO_TRACE;
362 #ifdef OHOS_BUILD_ENABLE_MONITOR
363     CHKPR(consumer, INVALID_HANDLER_ID);
364     std::lock_guard<std::mutex> guard(mtx_);
365     if (!MMIEventHdl.InitClient()) {
366         MMI_HILOGE("Client init failed");
367         return RET_ERR;
368     }
369     return IMonitorMgr->AddMonitor(consumer);
370 #else
371     MMI_HILOGI("Monitor function does not support");
372     return ERROR_UNSUPPORT;
373 #endif // OHOS_BUILD_ENABLE_MONITOR
374 }
375 
RemoveMonitor(int32_t monitorId)376 void InputManagerImpl::RemoveMonitor(int32_t monitorId)
377 {
378     CALL_INFO_TRACE;
379 #ifdef OHOS_BUILD_ENABLE_MONITOR
380     std::lock_guard<std::mutex> guard(mtx_);
381     if (!MMIEventHdl.InitClient()) {
382         MMI_HILOGE("Client init failed");
383         return;
384     }
385     IMonitorMgr->RemoveMonitor(monitorId);
386 #else
387     MMI_HILOGI("Monitor function does not support");
388 #endif // OHOS_BUILD_ENABLE_MONITOR
389 }
390 
MarkConsumed(int32_t monitorId,int32_t eventId)391 void InputManagerImpl::MarkConsumed(int32_t monitorId, int32_t eventId)
392 {
393     CALL_INFO_TRACE;
394 #ifdef OHOS_BUILD_ENABLE_MONITOR
395     std::lock_guard<std::mutex> guard(mtx_);
396     if (!MMIEventHdl.InitClient()) {
397         MMI_HILOGE("Client init failed");
398         return;
399     }
400     IMonitorMgr->MarkConsumed(monitorId, eventId);
401 #else
402     MMI_HILOGI("Monitor function does not support");
403 #endif // OHOS_BUILD_ENABLE_MONITOR
404 }
405 
MoveMouse(int32_t offsetX,int32_t offsetY)406 void InputManagerImpl::MoveMouse(int32_t offsetX, int32_t offsetY)
407 {
408 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
409     std::lock_guard<std::mutex> guard(mtx_);
410     if (MMIEventHdl.MoveMouseEvent(offsetX, offsetY) != RET_OK) {
411         MMI_HILOGE("Failed to inject move mouse offset event");
412     }
413 #else
414     MMI_HILOGW("Pointer device or pointer drawing module does not support");
415 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
416 }
417 
AddInterceptor(std::shared_ptr<IInputEventConsumer> interceptor)418 int32_t InputManagerImpl::AddInterceptor(std::shared_ptr<IInputEventConsumer> interceptor)
419 {
420     CALL_INFO_TRACE;
421 #ifdef OHOS_BUILD_ENABLE_INTERCEPTOR
422     CHKPR(interceptor, INVALID_HANDLER_ID);
423     std::lock_guard<std::mutex> guard(mtx_);
424     if (!MMIEventHdl.InitClient()) {
425         MMI_HILOGE("Client init failed");
426         return RET_ERR;
427     }
428     return InputInterMgr->AddInterceptor(interceptor, HANDLE_EVENT_TYPE_ALL);
429 #else
430     MMI_HILOGW("Interceptor function does not support");
431     return ERROR_UNSUPPORT;
432 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR
433 }
434 
AddInterceptor(std::function<void (std::shared_ptr<KeyEvent>)> interceptor)435 int32_t InputManagerImpl::AddInterceptor(std::function<void(std::shared_ptr<KeyEvent>)> interceptor)
436 {
437     CALL_INFO_TRACE;
438 #if defined(OHOS_BUILD_ENABLE_KEYBOARD) && defined(OHOS_BUILD_ENABLE_INTERCEPTOR)
439     CHKPR(interceptor, INVALID_HANDLER_ID);
440     std::lock_guard<std::mutex> guard(mtx_);
441     auto consumer = std::make_shared<MonitorEventConsumer>(interceptor);
442     if (!MMIEventHdl.InitClient()) {
443         MMI_HILOGE("Client init failed");
444         return RET_ERR;
445     }
446     return InputInterMgr->AddInterceptor(consumer, HANDLE_EVENT_TYPE_KEY);
447 #else
448     MMI_HILOGW("Keyboard device or interceptor function does not support");
449     return ERROR_UNSUPPORT;
450 #endif // OHOS_BUILD_ENABLE_KEYBOARD && OHOS_BUILD_ENABLE_INTERCEPTOR
451 }
452 
RemoveInterceptor(int32_t interceptorId)453 void InputManagerImpl::RemoveInterceptor(int32_t interceptorId)
454 {
455     CALL_INFO_TRACE;
456 #ifdef OHOS_BUILD_ENABLE_INTERCEPTOR
457     std::lock_guard<std::mutex> guard(mtx_);
458     if (!MMIEventHdl.InitClient()) {
459         MMI_HILOGE("Client init failed");
460         return;
461     }
462     InputInterMgr->RemoveInterceptor(interceptorId);
463 #else
464     MMI_HILOGW("Interceptor function does not support");
465 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR
466 }
467 
SimulateInputEvent(std::shared_ptr<KeyEvent> keyEvent)468 void InputManagerImpl::SimulateInputEvent(std::shared_ptr<KeyEvent> keyEvent)
469 {
470     CALL_INFO_TRACE;
471 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
472     CHKPV(keyEvent);
473     std::lock_guard<std::mutex> guard(mtx_);
474     if (MMIEventHdl.InjectEvent(keyEvent) != RET_OK) {
475         MMI_HILOGE("Failed to inject keyEvent");
476     }
477 #else
478     MMI_HILOGW("Keyboard device does not support");
479 #endif // OHOS_BUILD_ENABLE_KEYBOARD
480 }
481 
SimulateInputEvent(std::shared_ptr<PointerEvent> pointerEvent)482 void InputManagerImpl::SimulateInputEvent(std::shared_ptr<PointerEvent> pointerEvent)
483 {
484     CALL_INFO_TRACE;
485 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
486     CHKPV(pointerEvent);
487     if (pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_MOUSE ||
488         pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_TOUCHPAD) {
489 #ifndef OHOS_BUILD_ENABLE_POINTER
490         MMI_HILOGW("Pointer device does not support");
491         return;
492 #endif
493     }
494     if (pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_TOUCHSCREEN) {
495 #ifndef OHOS_BUILD_ENABLE_TOUCH
496         MMI_HILOGW("Touchscreen device does not support");
497         return;
498 #endif
499     }
500 #ifndef OHOS_BUILD_ENABLE_JOYSTICK
501     if (pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_JOYSTICK) {
502         MMI_HILOGW("Joystick device does not support");
503         return;
504     }
505 #endif
506     std::lock_guard<std::mutex> guard(mtx_);
507     if (MMIEventHdl.InjectPointerEvent(pointerEvent) != RET_OK) {
508         MMI_HILOGE("Failed to inject pointer event");
509     }
510 #else
511     MMI_HILOGW("Pointer and touchscreen device does not support");
512 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
513 }
514 
SetPointerVisible(bool visible)515 int32_t InputManagerImpl::SetPointerVisible(bool visible)
516 {
517 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
518     CALL_DEBUG_ENTER;
519     int32_t ret = MultimodalInputConnMgr->SetPointerVisible(visible);
520     if (ret != RET_OK) {
521         MMI_HILOGE("Set pointer visible failed, ret:%{public}d", ret);
522     }
523     return ret;
524 #else
525     MMI_HILOGW("Pointer device or pointer drawing module does not support");
526     return ERROR_UNSUPPORT;
527 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
528 }
529 
IsPointerVisible()530 bool InputManagerImpl::IsPointerVisible()
531 {
532 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
533     CALL_DEBUG_ENTER;
534     bool visible;
535     int32_t ret = MultimodalInputConnMgr->IsPointerVisible(visible);
536     if (ret != 0) {
537         MMI_HILOGE("Get pointer visible failed, ret:%{public}d", ret);
538     }
539     return visible;
540 #else
541     MMI_HILOGW("Pointer device or pointer drawing module does not support");
542     return false;
543 #endif // OHOS_BUILD_ENABLE_POINTER_DRAWING
544 }
545 
SetPointerSpeed(int32_t speed)546 int32_t InputManagerImpl::SetPointerSpeed(int32_t speed)
547 {
548     CALL_DEBUG_ENTER;
549 #ifdef OHOS_BUILD_ENABLE_POINTER
550     int32_t ret = MultimodalInputConnMgr->SetPointerSpeed(speed);
551     if (ret != RET_OK) {
552         MMI_HILOGE("Failed to set pointer speed");
553         return RET_ERR;
554     }
555     return RET_OK;
556 #else
557     MMI_HILOGW("Pointer device does not support");
558     return ERROR_UNSUPPORT;
559 #endif // OHOS_BUILD_ENABLE_POINTER
560 }
561 
GetPointerSpeed(int32_t & speed)562 int32_t InputManagerImpl::GetPointerSpeed(int32_t &speed)
563 {
564     CALL_DEBUG_ENTER;
565 #ifdef OHOS_BUILD_ENABLE_POINTER
566     int32_t ret = MultimodalInputConnMgr->GetPointerSpeed(speed);
567     if (ret != RET_OK) {
568         MMI_HILOGE("Get pointer speed failed");
569         return RET_ERR;
570     }
571     return RET_OK;
572 #else
573     return ERROR_UNSUPPORT;
574     MMI_HILOGW("Pointer device does not support");
575 #endif // OHOS_BUILD_ENABLE_POINTER
576 }
577 
SetPointerStyle(int32_t windowId,int32_t pointerStyle)578 int32_t InputManagerImpl::SetPointerStyle(int32_t windowId, int32_t pointerStyle)
579 {
580     CALL_DEBUG_ENTER;
581     if (windowId < 0 || pointerStyle < 0) {
582         MMI_HILOGE("The param is invalid");
583         return RET_ERR;
584     }
585     int32_t ret = MultimodalInputConnMgr->SetPointerStyle(windowId, pointerStyle);
586     if (ret != RET_OK) {
587         MMI_HILOGE("Set pointer style failed, ret:%{public}d", ret);
588         return ret;
589     }
590     return RET_OK;
591 }
592 
GetPointerStyle(int32_t windowId,int32_t & pointerStyle)593 int32_t InputManagerImpl::GetPointerStyle(int32_t windowId, int32_t &pointerStyle)
594 {
595     CALL_DEBUG_ENTER;
596     if (windowId < 0) {
597         MMI_HILOGE("The param is invalid");
598         return RET_ERR;
599     }
600     int32_t ret = MultimodalInputConnMgr->GetPointerStyle(windowId, pointerStyle);
601     if (ret != RET_OK) {
602         MMI_HILOGE("Get pointer style failed, ret:%{public}d", ret);
603         return ret;
604     }
605     return RET_OK;
606 }
607 
OnConnected()608 void InputManagerImpl::OnConnected()
609 {
610     CALL_DEBUG_ENTER;
611     if (displayGroupInfo_.windowsInfo.empty() || displayGroupInfo_.displaysInfo.empty()) {
612         MMI_HILOGI("The windows info or display info is empty");
613         return;
614     }
615     SendDisplayInfo();
616     PrintDisplayInfo();
617     if (anrObservers_.empty()) {
618         return;
619     }
620     int32_t ret = MultimodalInputConnMgr->SetAnrObserver();
621     if (ret != RET_OK) {
622         MMI_HILOGE("Set anr observerfailed, ret:%{public}d", ret);
623     }
624 }
625 
SendDisplayInfo()626 void InputManagerImpl::SendDisplayInfo()
627 {
628     MMIClientPtr client = MMIEventHdl.GetMMIClient();
629     CHKPV(client);
630     NetPacket pkt(MmiMessageId::DISPLAY_INFO);
631     if (PackDisplayData(pkt) == RET_ERR) {
632         MMI_HILOGE("Pack display info failed");
633         return;
634     }
635     if (!client->SendMessage(pkt)) {
636         MMI_HILOGE("Send message failed, errCode:%{public}d", MSG_SEND_FAIL);
637     }
638 }
639 
RegisterDevListener(std::string type,std::shared_ptr<IInputDeviceListener> listener)640 int32_t InputManagerImpl::RegisterDevListener(std::string type, std::shared_ptr<IInputDeviceListener> listener)
641 {
642     std::lock_guard<std::mutex> guard(mtx_);
643     if (!MMIEventHdl.InitClient()) {
644         MMI_HILOGE("Client init failed");
645         return RET_ERR;
646     }
647     return InputDevImpl.RegisterDevListener(type, listener);
648 }
649 
UnregisterDevListener(std::string type,std::shared_ptr<IInputDeviceListener> listener)650 int32_t InputManagerImpl::UnregisterDevListener(std::string type,
651     std::shared_ptr<IInputDeviceListener> listener)
652 {
653     std::lock_guard<std::mutex> guard(mtx_);
654     if (!MMIEventHdl.InitClient()) {
655         MMI_HILOGE("Client init failed");
656         return RET_ERR;
657     }
658     return InputDevImpl.UnregisterDevListener(type, listener);
659 }
660 
GetDeviceIds(std::function<void (std::vector<int32_t> &)> callback)661 int32_t InputManagerImpl::GetDeviceIds(std::function<void(std::vector<int32_t>&)> callback)
662 {
663     std::lock_guard<std::mutex> guard(mtx_);
664     if (!MMIEventHdl.InitClient()) {
665         MMI_HILOGE("Client init failed");
666         return RET_ERR;
667     }
668     return InputDevImpl.GetInputDeviceIdsAsync(callback);
669 }
670 
GetDevice(int32_t deviceId,std::function<void (std::shared_ptr<InputDevice>)> callback)671 int32_t InputManagerImpl::GetDevice(int32_t deviceId,
672     std::function<void(std::shared_ptr<InputDevice>)> callback)
673 {
674     std::lock_guard<std::mutex> guard(mtx_);
675     if (!MMIEventHdl.InitClient()) {
676         MMI_HILOGE("Client init failed");
677         return RET_ERR;
678     }
679     return InputDevImpl.GetInputDeviceAsync(deviceId, callback);
680 }
681 
SupportKeys(int32_t deviceId,std::vector<int32_t> & keyCodes,std::function<void (std::vector<bool> &)> callback)682 int32_t InputManagerImpl::SupportKeys(int32_t deviceId, std::vector<int32_t> &keyCodes,
683     std::function<void(std::vector<bool>&)> callback)
684 {
685     CALL_DEBUG_ENTER;
686     std::lock_guard<std::mutex> guard(mtx_);
687     if (!MMIEventHdl.InitClient()) {
688         MMI_HILOGE("Client init failed");
689         return RET_ERR;
690     }
691     return InputDevImpl.SupportKeys(deviceId, keyCodes, callback);
692 }
693 
GetKeyboardType(int32_t deviceId,std::function<void (int32_t)> callback)694 int32_t InputManagerImpl::GetKeyboardType(int32_t deviceId, std::function<void(int32_t)> callback)
695 {
696     CALL_DEBUG_ENTER;
697     std::lock_guard<std::mutex> guard(mtx_);
698     if (!MMIEventHdl.InitClient()) {
699         MMI_HILOGE("Client init failed");
700         return RET_ERR;
701     }
702     return InputDevImpl.GetKeyboardType(deviceId, callback);
703 }
704 
SetAnrObserver(std::shared_ptr<IAnrObserver> observer)705 void InputManagerImpl::SetAnrObserver(std::shared_ptr<IAnrObserver> observer)
706 {
707     CALL_DEBUG_ENTER;
708     std::lock_guard<std::mutex> guard(mtx_);
709     if (!MMIEventHdl.InitClient()) {
710         MMI_HILOGE("Client init failed");
711         return;
712     }
713     for (auto iter = anrObservers_.begin(); iter != anrObservers_.end(); ++iter) {
714         if (*iter == observer) {
715             MMI_HILOGE("Observer already exist");
716             return;
717         }
718     }
719     anrObservers_.push_back(observer);
720     int32_t ret = MultimodalInputConnMgr->SetAnrObserver();
721     if (ret != RET_OK) {
722         MMI_HILOGE("Set anr observer failed, ret:%{public}d", ret);
723     }
724 }
725 
OnAnr(int32_t pid)726 void InputManagerImpl::OnAnr(int32_t pid)
727 {
728     CALL_DEBUG_ENTER;
729     CHK_PID_AND_TID();
730     {
731         std::lock_guard<std::mutex> guard(mtx_);
732         for (const auto &observer : anrObservers_) {
733             CHKPC(observer);
734             observer->OnAnr(pid);
735         }
736     }
737     MMI_HILOGI("ANR noticed pid:%{public}d", pid);
738 }
739 
SetInputDevice(const std::string & dhid,const std::string & screenId)740 int32_t InputManagerImpl::SetInputDevice(const std::string &dhid, const std::string &screenId)
741 {
742     CALL_DEBUG_ENTER;
743 #ifdef OHOS_BUILD_ENABLE_COOPERATE
744     std::lock_guard<std::mutex> guard(mtx_);
745     int32_t ret = MultimodalInputConnMgr->SetInputDevice(dhid, screenId);
746     if (ret != RET_OK) {
747         MMI_HILOGE("Send to server failed, ret:%{public}d", ret);
748     }
749     return ret;
750 #else
751     (void)(dhid);
752     (void)(screenId);
753     MMI_HILOGW("Enable input device cooperate does not support");
754     return ERROR_UNSUPPORT;
755 #endif // OHOS_BUILD_ENABLE_COOPERATE
756 }
757 
RegisterCooperateListener(std::shared_ptr<IInputDeviceCooperateListener> listener)758 int32_t InputManagerImpl::RegisterCooperateListener(std::shared_ptr<IInputDeviceCooperateListener> listener)
759 {
760     CALL_DEBUG_ENTER;
761 #ifdef OHOS_BUILD_ENABLE_COOPERATE
762     std::lock_guard<std::mutex> guard(mtx_);
763     if (!MMIEventHdl.InitClient()) {
764         MMI_HILOGE("client init failed");
765         return RET_ERR;
766     }
767     return InputDevCooperateImpl.RegisterCooperateListener(listener);
768 #else
769     MMI_HILOGW("Cooperate does not support");
770     (void)(listener);
771     return ERROR_UNSUPPORT;
772 #endif // OHOS_BUILD_ENABLE_COOPERATE
773 }
774 
UnregisterCooperateListener(std::shared_ptr<IInputDeviceCooperateListener> listener)775 int32_t InputManagerImpl::UnregisterCooperateListener(std::shared_ptr<IInputDeviceCooperateListener> listener)
776 {
777     CALL_DEBUG_ENTER;
778 #ifdef OHOS_BUILD_ENABLE_COOPERATE
779     std::lock_guard<std::mutex> guard(mtx_);
780     if (!MMIEventHdl.InitClient()) {
781         MMI_HILOGE("client init failed");
782         return RET_ERR;
783     }
784     return InputDevCooperateImpl.UnregisterCooperateListener(listener);
785 #else
786     MMI_HILOGW("Cooperate does not support");
787     (void)(listener);
788     return ERROR_UNSUPPORT;
789 #endif // OHOS_BUILD_ENABLE_COOPERATE
790 }
791 
EnableInputDeviceCooperate(bool enabled,std::function<void (std::string,CooperationMessage)> callback)792 int32_t InputManagerImpl::EnableInputDeviceCooperate(bool enabled,
793     std::function<void(std::string, CooperationMessage)> callback)
794 {
795     CALL_DEBUG_ENTER;
796 #ifdef OHOS_BUILD_ENABLE_COOPERATE
797     std::lock_guard<std::mutex> guard(mtx_);
798     if (!MMIEventHdl.InitClient()) {
799         MMI_HILOGE("client init failed");
800         return RET_ERR;
801     }
802     return InputDevCooperateImpl.EnableInputDeviceCooperate(enabled, callback);
803 #else
804     MMI_HILOGW("Cooperate does not support");
805     (void)(enabled);
806     (void)(callback);
807     return ERROR_UNSUPPORT;
808 #endif // OHOS_BUILD_ENABLE_COOPERATE
809 }
810 
StartInputDeviceCooperate(const std::string & sinkDeviceId,int32_t srcInputDeviceId,std::function<void (std::string,CooperationMessage)> callback)811 int32_t InputManagerImpl::StartInputDeviceCooperate(const std::string &sinkDeviceId, int32_t srcInputDeviceId,
812     std::function<void(std::string, CooperationMessage)> callback)
813 {
814     CALL_DEBUG_ENTER;
815 #ifdef OHOS_BUILD_ENABLE_COOPERATE
816     std::lock_guard<std::mutex> guard(mtx_);
817     if (!MMIEventHdl.InitClient()) {
818         MMI_HILOGE("client init failed");
819         return RET_ERR;
820     }
821     return InputDevCooperateImpl.StartInputDeviceCooperate(sinkDeviceId, srcInputDeviceId, callback);
822 #else
823     MMI_HILOGW("Cooperate does not support");
824     (void)(sinkDeviceId);
825     (void)(srcInputDeviceId);
826     (void)(callback);
827     return ERROR_UNSUPPORT;
828 #endif // OHOS_BUILD_ENABLE_COOPERATE
829 }
830 
StopDeviceCooperate(std::function<void (std::string,CooperationMessage)> callback)831 int32_t InputManagerImpl::StopDeviceCooperate(std::function<void(std::string, CooperationMessage)> callback)
832 {
833     CALL_DEBUG_ENTER;
834 #ifdef OHOS_BUILD_ENABLE_COOPERATE
835     std::lock_guard<std::mutex> guard(mtx_);
836     if (!MMIEventHdl.InitClient()) {
837         MMI_HILOGE("client init failed");
838         return RET_ERR;
839     }
840     return InputDevCooperateImpl.StopDeviceCooperate(callback);
841 #else
842     MMI_HILOGW("Cooperate does not support");
843     (void)(callback);
844     return ERROR_UNSUPPORT;
845 #endif // OHOS_BUILD_ENABLE_COOPERATE
846 }
847 
GetInputDeviceCooperateState(const std::string & deviceId,std::function<void (bool)> callback)848 int32_t InputManagerImpl::GetInputDeviceCooperateState(const std::string &deviceId, std::function<void(bool)> callback)
849 {
850     CALL_DEBUG_ENTER;
851 #ifdef OHOS_BUILD_ENABLE_COOPERATE
852     std::lock_guard<std::mutex> guard(mtx_);
853     if (!MMIEventHdl.InitClient()) {
854         MMI_HILOGE("client init failed");
855         return RET_ERR;
856     }
857     return InputDevCooperateImpl.GetInputDeviceCooperateState(deviceId, callback);
858 #else
859     MMI_HILOGW("Cooperate does not support");
860     (void)(deviceId);
861     (void)(callback);
862     return ERROR_UNSUPPORT;
863 #endif // OHOS_BUILD_ENABLE_COOPERATE
864 }
865 
GetFunctionKeyState(int32_t funcKey)866 bool InputManagerImpl::GetFunctionKeyState(int32_t funcKey)
867 {
868 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
869     CALL_DEBUG_ENTER;
870     bool state { false };
871     int32_t ret = MultimodalInputConnMgr->GetFunctionKeyState(funcKey, state);
872     if (ret != RET_OK) {
873         MMI_HILOGE("Send to server failed, ret:%{public}d", ret);
874     }
875     return state;
876 #else
877     MMI_HILOGW("Keyboard device does not support");
878     return false;
879 #endif // OHOS_BUILD_ENABLE_KEYBOARD
880 }
881 
SetFunctionKeyState(int32_t funcKey,bool enable)882 int32_t InputManagerImpl::SetFunctionKeyState(int32_t funcKey, bool enable)
883 {
884 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
885     CALL_DEBUG_ENTER;
886     int32_t ret = MultimodalInputConnMgr->SetFunctionKeyState(funcKey, enable);
887     if (ret != RET_OK) {
888         MMI_HILOGE("Send to server failed, ret:%{public}d", ret);
889         return RET_ERR;
890     }
891     return RET_OK;
892 #else
893     MMI_HILOGW("Keyboard device does not support");
894     return ERROR_UNSUPPORT;
895 #endif // OHOS_BUILD_ENABLE_KEYBOARD
896 }
897 } // namespace MMI
898 } // namespace OHOS
899