• 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_manager_impl.h"
17 
18 #include <cinttypes>
19 
20 #include <unistd.h>
21 
22 #ifdef OHOS_BUILD_ENABLE_ANCO
23 #include "anco_channel.h"
24 #endif // OHOS_BUILD_ENABLE_ANCO
25 #include "anr_handler.h"
26 #include "bytrace_adapter.h"
27 #include "define_multimodal.h"
28 #include "error_multimodal.h"
29 #include "event_filter_service.h"
30 #include "event_log_helper.h"
31 #include "input_scene_board_judgement.h"
32 #include "mmi_client.h"
33 #include "multimodal_event_handler.h"
34 #include "multimodal_input_connect_manager.h"
35 #include "oh_input_manager.h"
36 #include "pixel_map.h"
37 #include "switch_event_input_subscribe_manager.h"
38 
39 #undef MMI_LOG_TAG
40 #define MMI_LOG_TAG "InputManagerImpl"
41 
42 namespace OHOS {
43 namespace MMI {
44 namespace {
45 constexpr size_t MAX_FILTER_NUM { 4 };
46 constexpr int32_t MAX_DELAY { 4000 };
47 constexpr int32_t MIN_DELAY { 0 };
48 constexpr int32_t SIMULATE_EVENT_START_ID { 10000 };
49 constexpr int32_t EVENT_TYPE { 0 };
50 constexpr uint8_t LOOP_COND { 2 };
51 constexpr int32_t MAX_PKT_SIZE { 8 * 1024 };
52 constexpr int32_t WINDOWINFO_RECT_COUNT { 2 };
53 constexpr int32_t DISPLAY_STRINGS_MAX_SIZE { 27 * 2 };
54 constexpr int32_t INVALID_KEY_ACTION { -1 };
55 constexpr int32_t MAX_WINDOW_SIZE { 15 };
56 const std::map<int32_t, int32_t> g_keyActionMap = {
57     {KeyEvent::KEY_ACTION_DOWN, KEY_ACTION_DOWN},
58     {KeyEvent::KEY_ACTION_UP, KEY_ACTION_UP},
59     {KeyEvent::KEY_ACTION_CANCEL, KEY_ACTION_CANCEL}
60 };
61 } // namespace
62 
63 struct MonitorEventConsumer : public IInputEventConsumer {
MonitorEventConsumerOHOS::MMI::MonitorEventConsumer64     explicit MonitorEventConsumer(const std::function<void(std::shared_ptr<PointerEvent>)> &monitor)
65         : monitor_ (monitor) {}
66 
MonitorEventConsumerOHOS::MMI::MonitorEventConsumer67     explicit MonitorEventConsumer(const std::function<void(std::shared_ptr<KeyEvent>)> &monitor)
68         : keyMonitor_ (monitor) {}
69 
OnInputEventOHOS::MMI::MonitorEventConsumer70     void OnInputEvent(std::shared_ptr<KeyEvent> keyEvent) const
71     {
72         CHKPV(keyEvent);
73         CHKPV(keyMonitor_);
74         keyMonitor_(keyEvent);
75     }
76 
OnInputEventOHOS::MMI::MonitorEventConsumer77     void OnInputEvent(std::shared_ptr<PointerEvent> pointerEvent) const
78     {
79         CHKPV(pointerEvent);
80         CHKPV(monitor_);
81         monitor_(pointerEvent);
82     }
83 
OnInputEventOHOS::MMI::MonitorEventConsumer84     void OnInputEvent(std::shared_ptr<AxisEvent> axisEvent) const
85     {
86         CHKPV(axisEvent);
87         CHKPV(axisMonitor_);
88         axisMonitor_(axisEvent);
89     }
90 
91 private:
92     std::function<void(std::shared_ptr<PointerEvent>)> monitor_;
93     std::function<void(std::shared_ptr<KeyEvent>)> keyMonitor_;
94     std::function<void(std::shared_ptr<AxisEvent>)> axisMonitor_;
95 };
96 
InputManagerImpl()97 InputManagerImpl::InputManagerImpl() {}
~InputManagerImpl()98 InputManagerImpl::~InputManagerImpl() {}
99 
GetDisplayBindInfo(DisplayBindInfos & infos)100 int32_t InputManagerImpl::GetDisplayBindInfo(DisplayBindInfos &infos)
101 {
102     CALL_INFO_TRACE;
103     std::lock_guard<std::mutex> guard(mtx_);
104     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->GetDisplayBindInfo(infos);
105     if (ret != RET_OK) {
106         MMI_HILOGE("GetDisplayBindInfo failed, ret:%{public}d", ret);
107         return RET_ERR;
108     }
109     return RET_OK;
110 }
111 
GetAllMmiSubscribedEvents(std::map<std::tuple<int32_t,int32_t,std::string>,int32_t> & datas)112 int32_t InputManagerImpl::GetAllMmiSubscribedEvents(std::map<std::tuple<int32_t, int32_t, std::string>, int32_t> &datas)
113 {
114     CALL_INFO_TRACE;
115     std::lock_guard<std::mutex> guard(mtx_);
116     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->GetAllMmiSubscribedEvents(datas);
117     if (ret != RET_OK) {
118         MMI_HILOGE("GetDisplayBindInfo failed, ret:%{public}d", ret);
119     }
120     return ret;
121 }
122 
SetDisplayBind(int32_t deviceId,int32_t displayId,std::string & msg)123 int32_t InputManagerImpl::SetDisplayBind(int32_t deviceId, int32_t displayId, std::string &msg)
124 {
125     CALL_INFO_TRACE;
126     std::lock_guard<std::mutex> guard(mtx_);
127     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->SetDisplayBind(deviceId, displayId, msg);
128     if (ret != RET_OK) {
129         MMI_HILOGE("SetDisplayBind failed, ret:%{public}d", ret);
130         return RET_ERR;
131     }
132     return RET_OK;
133 }
134 
GetWindowPid(int32_t windowId)135 int32_t InputManagerImpl::GetWindowPid(int32_t windowId)
136 {
137     CALL_INFO_TRACE;
138     std::lock_guard<std::mutex> guard(mtx_);
139     return MULTIMODAL_INPUT_CONNECT_MGR->GetWindowPid(windowId);
140 }
141 
UpdateDisplayInfo(const DisplayGroupInfo & displayGroupInfo)142 int32_t InputManagerImpl::UpdateDisplayInfo(const DisplayGroupInfo &displayGroupInfo)
143 {
144     CALL_DEBUG_ENTER;
145     std::lock_guard<std::mutex> guard(mtx_);
146     if (displayGroupInfo.windowsInfo.size() < MAX_WINDOW_SIZE) {
147         windowGroupInfo_.windowsInfo.clear();
148     }
149     if (!MMIEventHdl.InitClient()) {
150         MMI_HILOGE("Failed to initialize MMI client");
151         return RET_ERR;
152     }
153     displayGroupInfo_ = displayGroupInfo;
154     int32_t ret = SendDisplayInfo();
155     if (ret != RET_OK) {
156         MMI_HILOGE("Failed to send display information to service");
157         return ret;
158     }
159     PrintDisplayInfo();
160     return RET_OK;
161 }
162 
UpdateWindowInfo(const WindowGroupInfo & windowGroupInfo)163 int32_t InputManagerImpl::UpdateWindowInfo(const WindowGroupInfo &windowGroupInfo)
164 {
165     CALL_DEBUG_ENTER;
166     std::lock_guard<std::mutex> guard(mtx_);
167     if (!MMIEventHdl.InitClient()) {
168         MMI_HILOGE("Failed to initialize MMI client");
169         return RET_ERR;
170     }
171     if (!IsValiadWindowAreas(windowGroupInfo.windowsInfo)) {
172         MMI_HILOGE("Invalid window information");
173         return PARAM_INPUT_INVALID;
174     }
175     windowGroupInfo_ = windowGroupInfo;
176     int32_t ret = SendWindowInfo();
177     if (ret != RET_OK) {
178         MMI_HILOGE("Failed to send window information to service");
179         return ret;
180     }
181     PrintWindowGroupInfo();
182     return RET_OK;
183 }
184 
IsValiadWindowAreas(const std::vector<WindowInfo> & windows)185 bool InputManagerImpl::IsValiadWindowAreas(const std::vector<WindowInfo> &windows)
186 {
187     CALL_DEBUG_ENTER;
188 #ifdef OHOS_BUILD_ENABLE_ANCO
189     if (IsValidAncoWindow(windows)) {
190         return true;
191     }
192 #endif // OHOS_BUILD_ENABLE_ANCO
193     for (const auto &window : windows) {
194         if (window.action == WINDOW_UPDATE_ACTION::DEL) {
195             continue;
196         }
197         if (window.defaultHotAreas.empty() || window.pointerHotAreas.empty() ||
198             (!window.pointerChangeAreas.empty() &&
199             window.pointerChangeAreas.size() != WindowInfo::POINTER_CHANGEAREA_COUNT) ||
200             (!window.transform.empty() && window.transform.size() != WindowInfo::WINDOW_TRANSFORM_SIZE)) {
201             MMI_HILOGE("Hot areas check failed! defaultHotAreas:size:%{public}zu,"
202                 "pointerHotAreas:size:%{public}zu, pointerChangeAreas:size:%{public}zu,"
203                 "transform:size:%{public}zu", window.defaultHotAreas.size(),
204                 window.pointerHotAreas.size(), window.pointerChangeAreas.size(),
205                 window.transform.size());
206             return false;
207         }
208     }
209     return true;
210 }
211 
GetDisplayMaxSize()212 int32_t InputManagerImpl::GetDisplayMaxSize()
213 {
214     return sizeof(DisplayInfo) + DISPLAY_STRINGS_MAX_SIZE;
215 }
216 
GetWindowMaxSize(int32_t maxAreasCount)217 int32_t InputManagerImpl::GetWindowMaxSize(int32_t maxAreasCount)
218 {
219     return sizeof(WindowInfo) + sizeof(Rect) * maxAreasCount * WINDOWINFO_RECT_COUNT
220            + sizeof(int32_t) * WindowInfo::POINTER_CHANGEAREA_COUNT
221            + sizeof(float) * WindowInfo::WINDOW_TRANSFORM_SIZE;
222 }
223 
224 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
SetEnhanceConfig(uint8_t * cfg,uint32_t cfgLen)225 void InputManagerImpl::SetEnhanceConfig(uint8_t *cfg, uint32_t cfgLen)
226 {
227     CALL_INFO_TRACE;
228     if (cfg == nullptr || cfgLen == 0) {
229         MMI_HILOGE("SecCompEnhance cfg info is empty");
230         return;
231     }
232     enhanceCfg_ = new (std::nothrow) uint8_t[cfgLen];
233     CHKPV(enhanceCfg_);
234     errno_t ret = memcpy_s(enhanceCfg_, cfgLen, cfg, cfgLen);
235     if (ret != EOK) {
236         MMI_HILOGE("cfg memcpy failed");
237         return;
238     }
239     enhanceCfgLen_ = cfgLen;
240     std::lock_guard<std::mutex> guard(mtx_);
241     if (!MMIEventHdl.InitClient()) {
242         MMI_HILOGE("Get mmi client is nullptr");
243         return;
244     }
245     SendEnhanceConfig();
246     PrintEnhanceConfig();
247 }
248 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
249 
AddInputEventFilter(std::shared_ptr<IInputEventFilter> filter,int32_t priority,uint32_t deviceTags)250 int32_t InputManagerImpl::AddInputEventFilter(std::shared_ptr<IInputEventFilter> filter, int32_t priority,
251     uint32_t deviceTags)
252 {
253     CALL_INFO_TRACE;
254     std::lock_guard<std::mutex> guard(mtx_);
255     CHKPR(filter, RET_ERR);
256     if (eventFilterServices_.size() >= MAX_FILTER_NUM) {
257         MMI_HILOGE("Too many filters, size:%{public}zu", eventFilterServices_.size());
258         return RET_ERR;
259     }
260     sptr<IEventFilter> service = new (std::nothrow) EventFilterService(filter);
261     CHKPR(service, RET_ERR);
262     const int32_t filterId = EventFilterService::GetNextId();
263     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->AddInputEventFilter(service, filterId, priority, deviceTags);
264     if (ret != RET_OK) {
265         MMI_HILOGE("AddInputEventFilter has send to server failed, priority:%{public}d, ret:%{public}d", priority, ret);
266         service = nullptr;
267         return RET_ERR;
268     }
269     auto it = eventFilterServices_.emplace(filterId, std::make_tuple(service, priority, deviceTags));
270     if (!it.second) {
271         MMI_HILOGW("filterId duplicate");
272     }
273     return filterId;
274 }
275 
AddInputEventObserver(std::shared_ptr<MMIEventObserver> observer)276 int32_t InputManagerImpl::AddInputEventObserver(std::shared_ptr<MMIEventObserver> observer)
277 {
278     CALL_INFO_TRACE;
279     std::lock_guard<std::mutex> guard(mtx_);
280     CHKPR(observer, RET_ERR);
281     if (!MMIEventHdl.InitClient()) {
282         MMI_HILOGE("Get mmi client is nullptr");
283         return RET_ERR;
284     }
285     eventObserver_ = observer;
286     NotifyNapOnline();
287     return RET_OK;
288 }
289 
RemoveInputEventObserver(std::shared_ptr<MMIEventObserver> observer)290 int32_t InputManagerImpl::RemoveInputEventObserver(std::shared_ptr<MMIEventObserver> observer)
291 {
292     CALL_INFO_TRACE;
293     std::lock_guard<std::mutex> guard(mtx_);
294     eventObserver_ = nullptr;
295     return MULTIMODAL_INPUT_CONNECT_MGR->RemoveInputEventObserver();
296 }
297 
NotifyNapOnline()298 int32_t InputManagerImpl::NotifyNapOnline()
299 {
300     CALL_DEBUG_ENTER;
301     return MULTIMODAL_INPUT_CONNECT_MGR->NotifyNapOnline();
302 }
303 
RemoveInputEventFilter(int32_t filterId)304 int32_t InputManagerImpl::RemoveInputEventFilter(int32_t filterId)
305 {
306     CALL_DEBUG_ENTER;
307     std::lock_guard<std::mutex> guard(mtx_);
308     if (eventFilterServices_.empty()) {
309         MMI_HILOGE("Filters is empty, size:%{public}zu", eventFilterServices_.size());
310         return RET_OK;
311     }
312     std::map<int32_t, std::tuple<sptr<IEventFilter>, int32_t, uint32_t>>::iterator it;
313     if (filterId != -1) {
314         it = eventFilterServices_.find(filterId);
315         if (it == eventFilterServices_.end()) {
316             MMI_HILOGE("Filter not found");
317             return RET_OK;
318         }
319     }
320     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->RemoveInputEventFilter(filterId);
321     if (ret != RET_OK) {
322         MMI_HILOGE("Remove filter failed, filter id:%{public}d, ret:%{public}d", filterId, ret);
323         return RET_ERR;
324     }
325     if (filterId != -1) {
326         eventFilterServices_.erase(it);
327     } else {
328         eventFilterServices_.clear();
329     }
330     MMI_HILOGI("Filter remove success");
331     return RET_OK;
332 }
333 
SetWindowInputEventConsumer(std::shared_ptr<IInputEventConsumer> inputEventConsumer,std::shared_ptr<AppExecFwk::EventHandler> eventHandler)334 void InputManagerImpl::SetWindowInputEventConsumer(std::shared_ptr<IInputEventConsumer> inputEventConsumer,
335     std::shared_ptr<AppExecFwk::EventHandler> eventHandler)
336 {
337     CALL_INFO_TRACE;
338     CHK_PID_AND_TID();
339     CHKPV(inputEventConsumer);
340     CHKPV(eventHandler);
341     {
342         std::lock_guard<std::mutex> guard(mtx_);
343         if (!MMIEventHdl.InitClient(eventHandler)) {
344             MMI_HILOGE("Client init failed");
345             return;
346         }
347     }
348     std::lock_guard<std::mutex> guard(resourceMtx_);
349     consumer_ = inputEventConsumer;
350     eventHandler_ = eventHandler;
351 }
352 
SubscribeKeyEvent(std::shared_ptr<KeyOption> keyOption,std::function<void (std::shared_ptr<KeyEvent>)> callback)353 int32_t InputManagerImpl::SubscribeKeyEvent(std::shared_ptr<KeyOption> keyOption,
354     std::function<void(std::shared_ptr<KeyEvent>)> callback)
355 {
356     CALL_INFO_TRACE;
357     CHK_PID_AND_TID();
358     std::lock_guard<std::mutex> guard(mtx_);
359 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
360     CHKPR(keyOption, RET_ERR);
361     CHKPR(callback, RET_ERR);
362     return KeyEventInputSubscribeMgr.SubscribeKeyEvent(keyOption, callback);
363 #else
364     MMI_HILOGW("Keyboard device does not support");
365     return ERROR_UNSUPPORT;
366 #endif // OHOS_BUILD_ENABLE_KEYBOARD
367 }
368 
UnsubscribeKeyEvent(int32_t subscriberId)369 void InputManagerImpl::UnsubscribeKeyEvent(int32_t subscriberId)
370 {
371     CALL_INFO_TRACE;
372     CHK_PID_AND_TID();
373     std::lock_guard<std::mutex> guard(mtx_);
374 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
375     KeyEventInputSubscribeMgr.UnsubscribeKeyEvent(subscriberId);
376 #else
377     MMI_HILOGW("Keyboard device does not support");
378 #endif // OHOS_BUILD_ENABLE_KEYBOARD
379 }
380 
SubscribeSwitchEvent(int32_t switchType,std::function<void (std::shared_ptr<SwitchEvent>)> callback)381 int32_t InputManagerImpl::SubscribeSwitchEvent(int32_t switchType,
382     std::function<void(std::shared_ptr<SwitchEvent>)> callback)
383 {
384     CALL_INFO_TRACE;
385     CHK_PID_AND_TID();
386     std::lock_guard<std::mutex> guard(mtx_);
387 #ifdef OHOS_BUILD_ENABLE_SWITCH
388     CHKPR(callback, RET_ERR);
389     if (switchType < SwitchEvent::SwitchType::SWITCH_DEFAULT) {
390         MMI_HILOGE("switch type error, switchType:%{public}d", switchType);
391         return RET_ERR;
392     }
393     return SWITCH_EVENT_INPUT_SUBSCRIBE_MGR.SubscribeSwitchEvent(switchType, callback);
394 #else
395     MMI_HILOGW("switch device does not support");
396     return ERROR_UNSUPPORT;
397 #endif // OHOS_BUILD_ENABLE_SWITCH
398 }
399 
UnsubscribeSwitchEvent(int32_t subscriberId)400 void InputManagerImpl::UnsubscribeSwitchEvent(int32_t subscriberId)
401 {
402     CALL_INFO_TRACE;
403     CHK_PID_AND_TID();
404 #ifdef OHOS_BUILD_ENABLE_SWITCH
405     SWITCH_EVENT_INPUT_SUBSCRIBE_MGR.UnsubscribeSwitchEvent(subscriberId);
406 #else
407     MMI_HILOGW("switch device does not support");
408 #endif // OHOS_BUILD_ENABLE_SWITCH
409 }
410 
411 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
OnKeyEventTask(std::shared_ptr<IInputEventConsumer> consumer,std::shared_ptr<KeyEvent> keyEvent)412 void InputManagerImpl::OnKeyEventTask(std::shared_ptr<IInputEventConsumer> consumer,
413     std::shared_ptr<KeyEvent> keyEvent)
414 {
415     CALL_DEBUG_ENTER;
416     LogTracer lt(keyEvent->GetId(), keyEvent->GetEventType(), keyEvent->GetKeyAction());
417     CHK_PID_AND_TID();
418     CHKPV(consumer);
419     BytraceAdapter::StartConsumer(keyEvent);
420     consumer->OnInputEvent(keyEvent);
421     BytraceAdapter::StopConsumer();
422     MMI_HILOGD("Key event callback keyCode:%{private}d", keyEvent->GetKeyCode());
423 }
424 
OnKeyEvent(std::shared_ptr<KeyEvent> keyEvent)425 void InputManagerImpl::OnKeyEvent(std::shared_ptr<KeyEvent> keyEvent)
426 {
427     CALL_DEBUG_ENTER;
428     CHK_PID_AND_TID();
429     CHKPV(keyEvent);
430     std::shared_ptr<AppExecFwk::EventHandler> eventHandler = nullptr;
431     std::shared_ptr<IInputEventConsumer> inputConsumer = nullptr;
432     {
433         std::lock_guard<std::mutex> guard(resourceMtx_);
434         CHKPV(eventHandler_);
435         CHKPV(consumer_);
436         eventHandler = eventHandler_;
437         inputConsumer = consumer_;
438     }
439     MMI_HILOG_DISPATCHI("id:%{public}d recv", keyEvent->GetId());
440     BytraceAdapter::StartBytrace(keyEvent, BytraceAdapter::TRACE_STOP, BytraceAdapter::KEY_DISPATCH_EVENT);
441     MMIClientPtr client = MMIEventHdl.GetMMIClient();
442     CHKPV(client);
443     if (client->IsEventHandlerChanged()) {
444         BytraceAdapter::StartPostTaskEvent(keyEvent);
445         if (!eventHandler->PostTask([this, inputConsumer, keyEvent] {
446                 return this->OnKeyEventTask(inputConsumer, keyEvent);
447             },
448             std::string("MMI::OnKeyEvent"), 0, AppExecFwk::EventHandler::Priority::VIP)) {
449             MMI_HILOG_DISPATCHE("Post task failed");
450             BytraceAdapter::StopPostTaskEvent();
451             return;
452         }
453         BytraceAdapter::StopPostTaskEvent();
454     } else {
455         BytraceAdapter::StartConsumer(keyEvent);
456         inputConsumer->OnInputEvent(keyEvent);
457         BytraceAdapter::StopConsumer();
458         MMI_HILOG_DISPATCHD("Key event report keyCode:%{private}d", keyEvent->GetKeyCode());
459     }
460     MMI_HILOG_DISPATCHD("Key event keyCode:%{private}d", keyEvent->GetKeyCode());
461 }
462 #endif // OHOS_BUILD_ENABLE_KEYBOARD
463 
464 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
OnPointerEventTask(std::shared_ptr<IInputEventConsumer> consumer,std::shared_ptr<PointerEvent> pointerEvent)465 void InputManagerImpl::OnPointerEventTask(std::shared_ptr<IInputEventConsumer> consumer,
466     std::shared_ptr<PointerEvent> pointerEvent)
467 {
468     CALL_DEBUG_ENTER;
469     LogTracer lt(pointerEvent->GetId(), pointerEvent->GetEventType(), pointerEvent->GetPointerAction());
470     CHK_PID_AND_TID();
471     CHKPV(consumer);
472     CHKPV(pointerEvent);
473     BytraceAdapter::StartConsumer(pointerEvent);
474     consumer->OnInputEvent(pointerEvent);
475     BytraceAdapter::StopConsumer();
476     MMI_HILOG_DISPATCHD("Pointer event callback pointerId:%{public}d",
477         pointerEvent->GetPointerId());
478 }
479 
OnPointerEvent(std::shared_ptr<PointerEvent> pointerEvent)480 void InputManagerImpl::OnPointerEvent(std::shared_ptr<PointerEvent> pointerEvent)
481 {
482     CALL_DEBUG_ENTER;
483     CHK_PID_AND_TID();
484     CHKPV(pointerEvent);
485     std::shared_ptr<AppExecFwk::EventHandler> eventHandler = nullptr;
486     std::shared_ptr<IInputEventConsumer> inputConsumer = nullptr;
487     {
488         std::lock_guard<std::mutex> guard(resourceMtx_);
489         CHKPV(eventHandler_);
490         CHKPV(consumer_);
491         eventHandler = eventHandler_;
492         inputConsumer = consumer_;
493         lastPointerEvent_ = std::make_shared<PointerEvent>(*pointerEvent);
494     }
495     BytraceAdapter::StartBytrace(pointerEvent, BytraceAdapter::TRACE_STOP, BytraceAdapter::POINT_DISPATCH_EVENT);
496     MMIClientPtr client = MMIEventHdl.GetMMIClient();
497     CHKPV(client);
498     if (pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_MOVE &&
499         pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_AXIS_UPDATE &&
500         pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_ROTATE_UPDATE &&
501         pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_PULL_MOVE) {
502         MMI_HILOG_FREEZEI("id:%{public}d recv", pointerEvent->GetId());
503     }
504     if (client->IsEventHandlerChanged()) {
505         BytraceAdapter::StartPostTaskEvent(pointerEvent);
506         if (!eventHandler->PostTask([this, inputConsumer, pointerEvent] {
507                 return this->OnPointerEventTask(inputConsumer, pointerEvent);
508             },
509             std::string("MMI::OnPointerEvent"), 0, AppExecFwk::EventHandler::Priority::VIP)) {
510             MMI_HILOG_DISPATCHE("Post task failed");
511             BytraceAdapter::StopPostTaskEvent();
512             return;
513         }
514         BytraceAdapter::StopPostTaskEvent();
515     } else {
516         BytraceAdapter::StartConsumer(pointerEvent);
517         inputConsumer->OnInputEvent(pointerEvent);
518         BytraceAdapter::StopConsumer();
519     }
520     MMI_HILOG_DISPATCHD("Pointer event pointerId:%{public}d",
521         pointerEvent->GetPointerId());
522 }
523 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
524 
PackDisplayData(NetPacket & pkt)525 int32_t InputManagerImpl::PackDisplayData(NetPacket &pkt)
526 {
527     pkt << displayGroupInfo_.width << displayGroupInfo_.height
528         << displayGroupInfo_.focusWindowId << displayGroupInfo_.currentUserId;
529     if (pkt.ChkRWError()) {
530         MMI_HILOGE("Packet write logical data failed");
531         return RET_ERR;
532     }
533     if (PackWindowInfo(pkt) != RET_OK) {
534         MMI_HILOGE("Packet write windows info failed");
535         return RET_ERR;
536     }
537     return PackDisplayInfo(pkt);
538 }
539 
PackWindowGroupInfo(NetPacket & pkt)540 int32_t InputManagerImpl::PackWindowGroupInfo(NetPacket &pkt)
541 {
542     CALL_DEBUG_ENTER;
543     pkt << windowGroupInfo_.focusWindowId << windowGroupInfo_.displayId;
544     if (pkt.ChkRWError()) {
545         MMI_HILOGE("Packet write windowGroupInfo data failed");
546         return RET_ERR;
547     }
548     uint32_t num = static_cast<uint32_t>(windowGroupInfo_.windowsInfo.size());
549     pkt << num;
550     for (const auto &item : windowGroupInfo_.windowsInfo) {
551         pkt << item.id << item.pid << item.uid << item.area
552             << item.defaultHotAreas << item.pointerHotAreas
553             << item.agentWindowId << item.flags << item.action
554             << item.displayId << item.zOrder << item.pointerChangeAreas
555             << item.transform << item.windowInputType << item.privacyMode << item.windowType;
556         uint32_t uiExtentionWindowInfoNum = static_cast<uint32_t>(item.uiExtentionWindowInfo.size());
557         pkt << uiExtentionWindowInfoNum;
558         MMI_HILOGD("uiExtentionWindowInfoNum:%{public}u", uiExtentionWindowInfoNum);
559         if (!item.uiExtentionWindowInfo.empty()) {
560             PackUiExtentionWindowInfo(item.uiExtentionWindowInfo, pkt);
561             PrintWindowInfo(item.uiExtentionWindowInfo);
562         }
563         pkt << item.rectChangeBySystem;
564     }
565     if (pkt.ChkRWError()) {
566         MMI_HILOGE("Packet write windows data failed");
567         return RET_ERR;
568     }
569     return RET_OK;
570 }
571 
572 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
PackEnhanceConfig(NetPacket & pkt)573 int32_t InputManagerImpl::PackEnhanceConfig(NetPacket &pkt)
574 {
575     CALL_INFO_TRACE;
576     CHKPR(enhanceCfg_, RET_ERR);
577     pkt << enhanceCfgLen_;
578     for (uint32_t i = 0; i < enhanceCfgLen_; i++) {
579         pkt << enhanceCfg_[i];
580     }
581     if (pkt.ChkRWError()) {
582         MMI_HILOGE("Packet write security info config failed");
583         return RET_ERR;
584     }
585     return RET_OK;
586 }
587 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
588 
PackUiExtentionWindowInfo(const std::vector<WindowInfo> & windowsInfo,NetPacket & pkt)589 int32_t InputManagerImpl::PackUiExtentionWindowInfo(const std::vector<WindowInfo>& windowsInfo, NetPacket &pkt)
590 {
591     CALL_DEBUG_ENTER;
592     for (const auto &item : windowsInfo) {
593         pkt << item.id << item.pid << item.uid << item.area
594             << item.defaultHotAreas << item.pointerHotAreas
595             << item.agentWindowId << item.flags << item.action
596             << item.displayId << item.zOrder << item.pointerChangeAreas
597             << item.transform << item.windowInputType << item.privacyMode
598             << item.windowType << item.privacyUIFlag << item.rectChangeBySystem;
599     }
600     if (pkt.ChkRWError()) {
601         MMI_HILOGE("Packet write windows data failed");
602         return RET_ERR;
603     }
604     return RET_OK;
605 }
606 
PackWindowInfo(NetPacket & pkt)607 int32_t InputManagerImpl::PackWindowInfo(NetPacket &pkt) __attribute__((no_sanitize("cfi")))
608 {
609     CALL_DEBUG_ENTER;
610     uint32_t num = static_cast<uint32_t>(displayGroupInfo_.windowsInfo.size());
611     pkt << num;
612     for (const auto &item : displayGroupInfo_.windowsInfo) {
613         int32_t byteCount = 0;
614         pkt << item.id << item.pid << item.uid << item.area << item.defaultHotAreas
615             << item.pointerHotAreas << item.agentWindowId << item.flags << item.action
616             << item.displayId << item.zOrder << item.pointerChangeAreas << item.transform
617             << item.windowInputType << item.privacyMode << item.windowType;
618 
619         if (item.pixelMap == nullptr) {
620             pkt << byteCount;
621             uint32_t uiExtentionWindowInfoNum = static_cast<uint32_t>(item.uiExtentionWindowInfo.size());
622             pkt << uiExtentionWindowInfoNum;
623             MMI_HILOGD("uiExtentionWindowInfoNum:%{public}u", uiExtentionWindowInfoNum);
624             if (!item.uiExtentionWindowInfo.empty()) {
625                 PackUiExtentionWindowInfo(item.uiExtentionWindowInfo, pkt);
626                 PrintWindowInfo(item.uiExtentionWindowInfo);
627             }
628             pkt << item.rectChangeBySystem;
629             continue;
630         }
631         OHOS::Media::PixelMap* pixelMapPtr = static_cast<OHOS::Media::PixelMap*>(item.pixelMap);
632         byteCount = pixelMapPtr->GetByteCount();
633         int32_t ret = SetPixelMapData(item.id, item.pixelMap);
634         if (ret != RET_OK) {
635             byteCount = 0;
636             MMI_HILOGE("Failed to set pixel map, byteCount:%{public}d", byteCount);
637         }
638         pkt << byteCount;
639         uint32_t uiExtentionWindowInfoNum = static_cast<uint32_t>(item.uiExtentionWindowInfo.size());
640         pkt << uiExtentionWindowInfoNum;
641         MMI_HILOGD("uiExtentionWindowInfoNum:%{public}u", uiExtentionWindowInfoNum);
642         if (!item.uiExtentionWindowInfo.empty()) {
643             PackUiExtentionWindowInfo(item.uiExtentionWindowInfo, pkt);
644             PrintWindowInfo(item.uiExtentionWindowInfo);
645         }
646         pkt << item.rectChangeBySystem;
647     }
648     if (pkt.ChkRWError()) {
649         MMI_HILOGE("Packet write windows data failed");
650         return RET_ERR;
651     }
652     return RET_OK;
653 }
654 
PackDisplayInfo(NetPacket & pkt)655 int32_t InputManagerImpl::PackDisplayInfo(NetPacket &pkt)
656 {
657     CALL_DEBUG_ENTER;
658     uint32_t num = static_cast<uint32_t>(displayGroupInfo_.displaysInfo.size());
659     pkt << num;
660     for (const auto &item : displayGroupInfo_.displaysInfo) {
661         pkt << item.id << item.x << item.y << item.width
662             << item.height << item.dpi << item.name << item.uniq << item.direction
663             << item.displayDirection << item.displayMode << item.transform;
664     }
665     if (pkt.ChkRWError()) {
666         MMI_HILOGE("Packet write display data failed");
667         return RET_ERR;
668     }
669     return RET_OK;
670 }
671 
PrintWindowInfo(const std::vector<WindowInfo> & windowsInfo)672 void InputManagerImpl::PrintWindowInfo(const std::vector<WindowInfo> &windowsInfo)
673 {
674     if (!HiLogIsLoggable(MMI_LOG_DOMAIN, MMI_LOG_TAG, LOG_DEBUG)) {
675         return;
676     }
677     for (const auto &item : windowsInfo) {
678         MMI_HILOGD("windowsInfos,id:%{public}d,pid:%{public}d,uid:%{public}d,"
679             "area.x:%{public}d,area.y:%{public}d,area.width:%{public}d,area.height:%{public}d,"
680             "defaultHotAreas.size:%{public}zu,pointerHotAreas.size:%{public}zu,"
681             "agentWindowId:%{public}d,flags:%{public}d,action:%{public}d,displayId:%{public}d,"
682             "zOrder:%{public}f,privacyMode:%{public}d",
683             item.id, item.pid, item.uid, item.area.x, item.area.y, item.area.width,
684             item.area.height, item.defaultHotAreas.size(), item.pointerHotAreas.size(),
685             item.agentWindowId, item.flags, item.action, item.displayId, item.zOrder, item.privacyMode);
686         for (const auto &win : item.defaultHotAreas) {
687             MMI_HILOGD("defaultHotAreas:x:%{public}d,y:%{public}d,width:%{public}d,height:%{public}d",
688                 win.x, win.y, win.width, win.height);
689         }
690         for (const auto &pointer : item.pointerHotAreas) {
691             MMI_HILOGD("pointerHotAreas:x:%{public}d,y:%{public}d,width:%{public}d,height:%{public}d",
692                 pointer.x, pointer.y, pointer.width, pointer.height);
693         }
694 
695         std::string dump;
696         dump += StringPrintf("pointChangeAreas:[");
697         for (auto it : item.pointerChangeAreas) {
698             dump += StringPrintf("%d,", it);
699         }
700         dump += StringPrintf("] transform:[");
701         for (auto it : item.transform) {
702             dump += StringPrintf("%f,", it);
703         }
704         dump += StringPrintf("]\n");
705         std::istringstream stream(dump);
706         std::string line;
707         while (std::getline(stream, line, '\n')) {
708             MMI_HILOGD("%{public}s", line.c_str());
709         }
710     }
711 }
712 
PrintForemostThreeWindowInfo(const std::vector<WindowInfo> & windowsInfo)713 void InputManagerImpl::PrintForemostThreeWindowInfo(const std::vector<WindowInfo> &windowsInfo)
714 {
715     uint8_t times = 0;
716     for (const auto &item : windowsInfo) {
717         if (times > LOOP_COND) {
718             return;
719         }
720         MMI_HILOGD("WindowInfo[%{public}d,%{public}d,%{public}d,%{public}d,%{public}d,%{public}d,%{public}f]",
721             item.id, item.pid, item.area.x, item.area.y, item.area.width, item.area.height, item.zOrder);
722         for (const auto &pointer : item.pointerHotAreas) {
723             MMI_HILOGD("pointerHotAreas:x:%{public}d,y:%{public}d,width:%{public}d,height:%{public}d",
724                 pointer.x, pointer.y, pointer.width, pointer.height);
725         }
726         times++;
727     }
728 }
729 
PrintDisplayInfo()730 void InputManagerImpl::PrintDisplayInfo()
731 {
732     MMI_HILOGD("windowsInfos,num:%{public}zu,focusWindowId:%{public}d", displayGroupInfo_.windowsInfo.size(),
733         displayGroupInfo_.focusWindowId);
734     PrintForemostThreeWindowInfo(displayGroupInfo_.windowsInfo);
735     if (!HiLogIsLoggable(MMI_LOG_DOMAIN, MMI_LOG_TAG, LOG_DEBUG)) {
736         return;
737     }
738     MMI_HILOGD("logicalInfo,width:%{public}d,height:%{public}d,focusWindowId:%{public}d",
739         displayGroupInfo_.width, displayGroupInfo_.height, displayGroupInfo_.focusWindowId);
740     PrintWindowInfo(displayGroupInfo_.windowsInfo);
741 
742     MMI_HILOGD("displayInfos,num:%{public}zu", displayGroupInfo_.displaysInfo.size());
743     for (const auto &item : displayGroupInfo_.displaysInfo) {
744         MMI_HILOGD("displayInfos,id:%{public}d,x:%{public}d,y:%{public}d,"
745             "width:%{public}d,height:%{public}d,dpi:%{public}d,name:%{public}s,"
746             "uniq:%{public}s,direction:%{public}d,displayDirection:%{public}d,displayMode:%{public}d",
747             item.id, item.x, item.y, item.width, item.height, item.dpi, item.name.c_str(),
748             item.uniq.c_str(), item.direction, item.displayDirection, item.displayMode);
749     }
750 }
751 
PrintWindowGroupInfo()752 void InputManagerImpl::PrintWindowGroupInfo()
753 {
754     if (!HiLogIsLoggable(MMI_LOG_DOMAIN, MMI_LOG_TAG, LOG_DEBUG)) {
755         return;
756     }
757     MMI_HILOGD("windowsGroupInfo,focusWindowId:%{public}d,displayId:%{public}d",
758         windowGroupInfo_.focusWindowId, windowGroupInfo_.displayId);
759     PrintWindowInfo(windowGroupInfo_.windowsInfo);
760 }
761 
762 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
PrintEnhanceConfig()763 void InputManagerImpl::PrintEnhanceConfig()
764 {
765     CHKPV(enhanceCfg_);
766     MMI_HILOGD("securityConfigInfo, cfg len:%{public}d", enhanceCfgLen_);
767 }
768 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
769 
AddMonitor(std::function<void (std::shared_ptr<KeyEvent>)> monitor)770 int32_t InputManagerImpl::AddMonitor(std::function<void(std::shared_ptr<KeyEvent>)> monitor)
771 {
772     CALL_DEBUG_ENTER;
773 #if defined(OHOS_BUILD_ENABLE_KEYBOARD) && defined(OHOS_BUILD_ENABLE_MONITOR)
774     CHKPR(monitor, INVALID_HANDLER_ID);
775     auto consumer = std::make_shared<MonitorEventConsumer>(monitor);
776     return AddMonitor(consumer, HANDLE_EVENT_TYPE_KEY);
777 #else
778     MMI_HILOGW("Keyboard device or monitor function does not support");
779     return ERROR_UNSUPPORT;
780 #endif // OHOS_BUILD_ENABLE_KEYBOARD || OHOS_BUILD_ENABLE_MONITOR
781 }
782 
AddMonitor(std::function<void (std::shared_ptr<PointerEvent>)> monitor)783 int32_t InputManagerImpl::AddMonitor(std::function<void(std::shared_ptr<PointerEvent>)> monitor)
784 {
785     CALL_DEBUG_ENTER;
786 #if (defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)) && defined(OHOS_BUILD_ENABLE_MONITOR)
787     CHKPR(monitor, INVALID_HANDLER_ID);
788     auto consumer = std::make_shared<MonitorEventConsumer>(monitor);
789     return AddMonitor(consumer, HANDLE_EVENT_TYPE_POINTER);
790 #else
791     MMI_HILOGW("Pointer/touchscreen device or monitor function does not support");
792     return ERROR_UNSUPPORT;
793 #endif // OHOS_BUILD_ENABLE_MONITOR ||  OHOS_BUILD_ENABLE_TOUCH && OHOS_BUILD_ENABLE_MONITOR
794 }
795 
AddMonitor(std::shared_ptr<IInputEventConsumer> consumer,HandleEventType eventType)796 int32_t InputManagerImpl::AddMonitor(std::shared_ptr<IInputEventConsumer> consumer, HandleEventType eventType)
797 {
798     CALL_DEBUG_ENTER;
799 #ifdef OHOS_BUILD_ENABLE_MONITOR
800     CHKPR(consumer, INVALID_HANDLER_ID);
801     std::lock_guard<std::mutex> guard(mtx_);
802     if (!MMIEventHdl.InitClient()) {
803         MMI_HILOGE("Client init failed");
804         return RET_ERR;
805     }
806     return IMonitorMgr->AddMonitor(consumer, eventType);
807 #else
808     MMI_HILOGI("Monitor function does not support");
809     return ERROR_UNSUPPORT;
810 #endif // OHOS_BUILD_ENABLE_MONITOR
811 }
812 
RemoveMonitor(int32_t monitorId)813 int32_t InputManagerImpl::RemoveMonitor(int32_t monitorId)
814 {
815     CALL_DEBUG_ENTER;
816 #ifdef OHOS_BUILD_ENABLE_MONITOR
817     std::lock_guard<std::mutex> guard(mtx_);
818     if (!MMIEventHdl.InitClient()) {
819         MMI_HILOGE("Client init failed");
820         return RET_ERR;
821     }
822     return IMonitorMgr->RemoveMonitor(monitorId);
823 #else
824     MMI_HILOGI("Monitor function does not support");
825     return ERROR_UNSUPPORT;
826 #endif // OHOS_BUILD_ENABLE_MONITOR
827 }
828 
MarkConsumed(int32_t monitorId,int32_t eventId)829 void InputManagerImpl::MarkConsumed(int32_t monitorId, int32_t eventId)
830 {
831     CALL_INFO_TRACE;
832 #ifdef OHOS_BUILD_ENABLE_MONITOR
833     std::lock_guard<std::mutex> guard(mtx_);
834     if (!MMIEventHdl.InitClient()) {
835         MMI_HILOGE("Client init failed");
836         return;
837     }
838     IMonitorMgr->MarkConsumed(monitorId, eventId);
839 #else
840     MMI_HILOGI("Monitor function does not support");
841 #endif // OHOS_BUILD_ENABLE_MONITOR
842 }
843 
MoveMouse(int32_t offsetX,int32_t offsetY)844 void InputManagerImpl::MoveMouse(int32_t offsetX, int32_t offsetY)
845 {
846     CALL_INFO_TRACE;
847 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
848     std::lock_guard<std::mutex> guard(mtx_);
849     if (MMIEventHdl.MoveMouseEvent(offsetX, offsetY) != RET_OK) {
850         MMI_HILOGE("Failed to inject move mouse offset event");
851     }
852 #else
853     MMI_HILOGW("Pointer device or pointer drawing module does not support");
854 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
855 }
856 
AddInterceptor(std::shared_ptr<IInputEventConsumer> interceptor,int32_t priority,uint32_t deviceTags)857 int32_t InputManagerImpl::AddInterceptor(std::shared_ptr<IInputEventConsumer> interceptor,
858     int32_t priority, uint32_t deviceTags)
859 {
860     CALL_DEBUG_ENTER;
861 #ifdef OHOS_BUILD_ENABLE_INTERCEPTOR
862     CHKPR(interceptor, INVALID_HANDLER_ID);
863     std::lock_guard<std::mutex> guard(mtx_);
864     if (!MMIEventHdl.InitClient()) {
865         MMI_HILOGE("Client init failed");
866         return RET_ERR;
867     }
868     return InputInterMgr->AddInterceptor(interceptor, HANDLE_EVENT_TYPE_ALL, priority, deviceTags);
869 #else
870     MMI_HILOGW("Interceptor function does not support");
871     return ERROR_UNSUPPORT;
872 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR
873 }
874 
AddInterceptor(std::function<void (std::shared_ptr<KeyEvent>)> interceptor,int32_t priority,uint32_t deviceTags)875 int32_t InputManagerImpl::AddInterceptor(std::function<void(std::shared_ptr<KeyEvent>)> interceptor,
876     int32_t priority, uint32_t deviceTags)
877 {
878     CALL_DEBUG_ENTER;
879 #if defined(OHOS_BUILD_ENABLE_KEYBOARD) && defined(OHOS_BUILD_ENABLE_INTERCEPTOR)
880     CHKPR(interceptor, INVALID_HANDLER_ID);
881     std::lock_guard<std::mutex> guard(mtx_);
882     auto consumer = std::make_shared<MonitorEventConsumer>(interceptor);
883     if (!MMIEventHdl.InitClient()) {
884         MMI_HILOGE("Client init failed");
885         return RET_ERR;
886     }
887     return InputInterMgr->AddInterceptor(consumer, HANDLE_EVENT_TYPE_KEY, priority, deviceTags);
888 #else
889     MMI_HILOGW("Keyboard device or interceptor function does not support");
890     return ERROR_UNSUPPORT;
891 #endif // OHOS_BUILD_ENABLE_KEYBOARD && OHOS_BUILD_ENABLE_INTERCEPTOR
892 }
893 
RemoveInterceptor(int32_t interceptorId)894 int32_t InputManagerImpl::RemoveInterceptor(int32_t interceptorId)
895 {
896     CALL_DEBUG_ENTER;
897 #ifdef OHOS_BUILD_ENABLE_INTERCEPTOR
898     std::lock_guard<std::mutex> guard(mtx_);
899     if (!MMIEventHdl.InitClient()) {
900         MMI_HILOGE("Client init failed");
901         return RET_ERR;
902     }
903     return InputInterMgr->RemoveInterceptor(interceptorId);
904 #else
905     MMI_HILOGW("Interceptor function does not support");
906     return ERROR_UNSUPPORT;
907 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR
908 }
909 
SimulateInputEvent(std::shared_ptr<KeyEvent> keyEvent,bool isNativeInject)910 void InputManagerImpl::SimulateInputEvent(std::shared_ptr<KeyEvent> keyEvent, bool isNativeInject)
911 {
912     CALL_DEBUG_ENTER;
913 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
914     CHKPV(keyEvent);
915     if (!EventLogHelper::IsBetaVersion()) {
916         MMI_HILOGI("action:%{public}d", keyEvent->GetKeyAction());
917     } else {
918         MMI_HILOGI("action:%{public}d", keyEvent->GetKeyAction());
919     }
920     if (MMIEventHdl.InjectEvent(keyEvent, isNativeInject) != RET_OK) {
921         MMI_HILOGE("Failed to inject keyEvent");
922     }
923 #else
924     MMI_HILOGW("Keyboard device does not support");
925 #endif // OHOS_BUILD_ENABLE_KEYBOARD
926 }
927 
HandleSimulateInputEvent(std::shared_ptr<PointerEvent> pointerEvent)928 void InputManagerImpl::HandleSimulateInputEvent(std::shared_ptr<PointerEvent> pointerEvent)
929 {
930     CALL_DEBUG_ENTER;
931     int maxPointerId = SIMULATE_EVENT_START_ID;
932     std::list<PointerEvent::PointerItem> pointerItems = pointerEvent->GetAllPointerItems();
933     for (auto &pointerItem : pointerItems) {
934         int32_t pointerId = pointerItem.GetPointerId();
935         if (pointerId != -1) {
936             maxPointerId = (maxPointerId > pointerId) ? maxPointerId : pointerId;
937             continue;
938         }
939         maxPointerId += 1;
940         pointerItem.SetPointerId(maxPointerId);
941     }
942     for (auto &pointerItem : pointerItems) {
943         int32_t pointerId = pointerItem.GetPointerId();
944         if (pointerId < SIMULATE_EVENT_START_ID) {
945             pointerItem.SetOriginPointerId(pointerId);
946             pointerItem.SetPointerId(pointerId + SIMULATE_EVENT_START_ID);
947         }
948     }
949     pointerEvent->RemoveAllPointerItems();
950     for (auto &pointerItem : pointerItems) {
951         pointerEvent->AddPointerItem(pointerItem);
952     }
953     if ((pointerEvent->GetPointerId() < 0) && !pointerItems.empty()) {
954         pointerEvent->SetPointerId(pointerItems.front().GetPointerId());
955         MMI_HILOGD("Simulate pointer event id:%{public}d", pointerEvent->GetPointerId());
956     }
957     if (pointerEvent->GetPointerId() < SIMULATE_EVENT_START_ID) {
958         pointerEvent->SetPointerId(pointerEvent->GetPointerId() + SIMULATE_EVENT_START_ID);
959     }
960 }
961 
SimulateInputEvent(std::shared_ptr<PointerEvent> pointerEvent,bool isNativeInject)962 void InputManagerImpl::SimulateInputEvent(std::shared_ptr<PointerEvent> pointerEvent, bool isNativeInject)
963 {
964     CALL_DEBUG_ENTER;
965 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
966     CHKPV(pointerEvent);
967     if (pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_MOVE &&
968         pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_PULL_MOVE &&
969         pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_HOVER_MOVE &&
970         pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_AXIS_UPDATE &&
971         pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_SWIPE_UPDATE &&
972         pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_ROTATE_UPDATE &&
973         pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_FINGERPRINT_SLIDE) {
974         MMI_HILOGI("Pointer event action:%{public}d", pointerEvent->GetPointerAction());
975     }
976     if (pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_MOUSE ||
977         pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_TOUCHPAD) {
978 #ifndef OHOS_BUILD_ENABLE_POINTER
979         MMI_HILOGW("Pointer device does not support");
980         return;
981 #endif
982     }
983     if (pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_TOUCHSCREEN) {
984 #ifndef OHOS_BUILD_ENABLE_TOUCH
985         MMI_HILOGW("Touchscreen device does not support");
986         return;
987 #endif
988     }
989 #ifndef OHOS_BUILD_ENABLE_JOYSTICK
990     if (pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_JOYSTICK) {
991         MMI_HILOGW("Joystick device does not support");
992         return;
993     }
994 #endif // OHOS_BUILD_ENABLE_JOYSTICK
995     HandleSimulateInputEvent(pointerEvent);
996     if (MMIEventHdl.InjectPointerEvent(pointerEvent, isNativeInject) != RET_OK) {
997         MMI_HILOGE("Failed to inject pointer event");
998     }
999 #else
1000     MMI_HILOGW("Pointer and touchscreen device does not support");
1001 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
1002 }
1003 
SetMouseScrollRows(int32_t rows)1004 int32_t InputManagerImpl::SetMouseScrollRows(int32_t rows)
1005 {
1006     CALL_INFO_TRACE;
1007 #if defined OHOS_BUILD_ENABLE_POINTER
1008     std::lock_guard<std::mutex> guard(mtx_);
1009     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->SetMouseScrollRows(rows);
1010     if (ret != RET_OK) {
1011         MMI_HILOGE("Set the number of mouse scrolling rows failed, ret:%{public}d", ret);
1012     }
1013     return ret;
1014 #else
1015     MMI_HILOGW("Pointer device module does not support");
1016     return ERROR_UNSUPPORT;
1017 #endif // OHOS_BUILD_ENABLE_POINTER
1018 }
1019 
SetCustomCursor(int32_t windowId,int32_t focusX,int32_t focusY,void * pixelMap)1020 int32_t InputManagerImpl::SetCustomCursor(int32_t windowId, int32_t focusX, int32_t focusY, void* pixelMap)
1021 {
1022     CALL_INFO_TRACE;
1023 #if defined OHOS_BUILD_ENABLE_POINTER
1024     int32_t winPid = GetWindowPid(windowId);
1025     if (winPid == -1) {
1026         MMI_HILOGE("winPid is invalid");
1027         return RET_ERR;
1028     }
1029     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->SetCustomCursor(winPid, windowId, focusX, focusY, pixelMap);
1030     if (ret != RET_OK) {
1031         MMI_HILOGE("Set custom cursor failed, ret:%{public}d", ret);
1032     }
1033     return ret;
1034 #else
1035     MMI_HILOGW("Pointer device module does not support");
1036     return ERROR_UNSUPPORT;
1037 #endif // OHOS_BUILD_ENABLE_POINTER
1038 }
1039 
SetMouseIcon(int32_t windowId,void * pixelMap)1040 int32_t InputManagerImpl::SetMouseIcon(int32_t windowId, void* pixelMap)
1041 {
1042     CALL_INFO_TRACE;
1043 #if defined OHOS_BUILD_ENABLE_POINTER
1044     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->SetMouseIcon(windowId, pixelMap);
1045     if (ret != RET_OK) {
1046         MMI_HILOGE("Set the number of mouse scrolling rows failed, ret:%{public}d", ret);
1047     }
1048     return ret;
1049 #else
1050     MMI_HILOGW("Pointer device module does not support");
1051     return ERROR_UNSUPPORT;
1052 #endif // OHOS_BUILD_ENABLE_POINTER
1053 }
1054 
SetMouseHotSpot(int32_t windowId,int32_t hotSpotX,int32_t hotSpotY)1055 int32_t InputManagerImpl::SetMouseHotSpot(int32_t windowId, int32_t hotSpotX, int32_t hotSpotY)
1056 {
1057     CALL_INFO_TRACE;
1058 #if defined OHOS_BUILD_ENABLE_POINTER
1059     int32_t winPid = GetWindowPid(windowId);
1060     if (winPid == -1) {
1061         MMI_HILOGE("winPid is invalid return -1");
1062         return RET_ERR;
1063     }
1064     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->SetMouseHotSpot(winPid, windowId, hotSpotX, hotSpotY);
1065     if (ret != RET_OK) {
1066         MMI_HILOGE("Set mouse hot spot failed, ret:%{public}d", ret);
1067     }
1068     return ret;
1069 #else
1070     MMI_HILOGW("Pointer device module does not support");
1071     return ERROR_UNSUPPORT;
1072 #endif // OHOS_BUILD_ENABLE_POINTER
1073 }
1074 
GetMouseScrollRows(int32_t & rows)1075 int32_t InputManagerImpl::GetMouseScrollRows(int32_t &rows)
1076 {
1077     CALL_INFO_TRACE;
1078 #ifdef OHOS_BUILD_ENABLE_POINTER
1079     std::lock_guard<std::mutex> guard(mtx_);
1080     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->GetMouseScrollRows(rows);
1081     if (ret != RET_OK) {
1082         MMI_HILOGE("Get the number of mouse scrolling rows failed");
1083     }
1084     return ret;
1085 #else
1086     MMI_HILOGW("Pointer device does not support");
1087     return ERROR_UNSUPPORT;
1088 #endif // OHOS_BUILD_ENABLE_POINTER
1089 }
1090 
SetPointerSize(int32_t size)1091 int32_t InputManagerImpl::SetPointerSize(int32_t size)
1092 {
1093     CALL_INFO_TRACE;
1094 #if defined OHOS_BUILD_ENABLE_POINTER
1095     std::lock_guard<std::mutex> guard(mtx_);
1096     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->SetPointerSize(size);
1097     if (ret != RET_OK) {
1098         MMI_HILOGE("Set pointer size failed, ret:%{public}d", ret);
1099     }
1100     return ret;
1101 #else
1102     MMI_HILOGW("Pointer device module does not support");
1103     return ERROR_UNSUPPORT;
1104 #endif // OHOS_BUILD_ENABLE_POINTER
1105 }
1106 
GetPointerSize(int32_t & size)1107 int32_t InputManagerImpl::GetPointerSize(int32_t &size)
1108 {
1109     CALL_INFO_TRACE;
1110 #ifdef OHOS_BUILD_ENABLE_POINTER
1111     std::lock_guard<std::mutex> guard(mtx_);
1112     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->GetPointerSize(size);
1113     if (ret != RET_OK) {
1114         MMI_HILOGE("Get pointer size failed");
1115     }
1116     return ret;
1117 #else
1118     MMI_HILOGW("Pointer device does not support");
1119     return ERROR_UNSUPPORT;
1120 #endif // OHOS_BUILD_ENABLE_POINTER
1121 }
1122 
SetMousePrimaryButton(int32_t primaryButton)1123 int32_t InputManagerImpl::SetMousePrimaryButton(int32_t primaryButton)
1124 {
1125     CALL_INFO_TRACE;
1126 #if defined OHOS_BUILD_ENABLE_POINTER
1127     std::lock_guard<std::mutex> guard(mtx_);
1128     if (primaryButton != LEFT_BUTTON && primaryButton != RIGHT_BUTTON) {
1129         MMI_HILOGE("primaryButton is invalid");
1130         return RET_ERR;
1131     }
1132     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->SetMousePrimaryButton(primaryButton);
1133     if (ret != RET_OK) {
1134         MMI_HILOGE("Set mouse primary button failed, ret:%{public}d", ret);
1135     }
1136     return ret;
1137 #else
1138     MMI_HILOGW("Pointer device module does not support");
1139     return ERROR_UNSUPPORT;
1140 #endif // OHOS_BUILD_ENABLE_POINTER
1141 }
1142 
GetMousePrimaryButton(int32_t & primaryButton)1143 int32_t InputManagerImpl::GetMousePrimaryButton(int32_t &primaryButton)
1144 {
1145     CALL_INFO_TRACE;
1146 #ifdef OHOS_BUILD_ENABLE_POINTER
1147     std::lock_guard<std::mutex> guard(mtx_);
1148     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->GetMousePrimaryButton(primaryButton);
1149     if (ret != RET_OK) {
1150         MMI_HILOGE("Get mouse primary button failed");
1151     }
1152     return ret;
1153 #else
1154     MMI_HILOGW("Pointer device does not support");
1155     return ERROR_UNSUPPORT;
1156 #endif // OHOS_BUILD_ENABLE_POINTER
1157 }
1158 
SetHoverScrollState(bool state)1159 int32_t InputManagerImpl::SetHoverScrollState(bool state)
1160 {
1161     CALL_INFO_TRACE;
1162 #if defined OHOS_BUILD_ENABLE_POINTER
1163     std::lock_guard<std::mutex> guard(mtx_);
1164     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->SetHoverScrollState(state);
1165     if (ret != RET_OK) {
1166         MMI_HILOGE("Set mouse hover scroll state failed, ret:%{public}d", ret);
1167     }
1168     return ret;
1169 #else
1170     MMI_HILOGW("Pointer device module does not support");
1171     return ERROR_UNSUPPORT;
1172 #endif // OHOS_BUILD_ENABLE_POINTER
1173 }
1174 
GetHoverScrollState(bool & state)1175 int32_t InputManagerImpl::GetHoverScrollState(bool &state)
1176 {
1177     CALL_INFO_TRACE;
1178 #ifdef OHOS_BUILD_ENABLE_POINTER
1179     std::lock_guard<std::mutex> guard(mtx_);
1180     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->GetHoverScrollState(state);
1181     if (ret != RET_OK) {
1182         MMI_HILOGE("Get mouse hover scroll state failed, ret:%{public}d", ret);
1183     }
1184     return ret;
1185 #else
1186     MMI_HILOGW("Pointer device does not support");
1187     return ERROR_UNSUPPORT;
1188 #endif // OHOS_BUILD_ENABLE_POINTER
1189 }
1190 
SetPointerVisible(bool visible,int32_t priority)1191 int32_t InputManagerImpl::SetPointerVisible(bool visible, int32_t priority)
1192 {
1193     CALL_INFO_TRACE;
1194 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
1195     std::lock_guard<std::mutex> guard(mtx_);
1196     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->SetPointerVisible(visible, priority);
1197     if (ret != RET_OK) {
1198         MMI_HILOGE("Set pointer visible failed, ret:%{public}d", ret);
1199     }
1200     return ret;
1201 #else
1202     MMI_HILOGW("Pointer device or pointer drawing module does not support");
1203     return ERROR_UNSUPPORT;
1204 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
1205 }
1206 
IsPointerVisible()1207 bool InputManagerImpl::IsPointerVisible()
1208 {
1209     CALL_INFO_TRACE;
1210 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
1211     std::lock_guard<std::mutex> guard(mtx_);
1212     bool visible;
1213     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->IsPointerVisible(visible);
1214     if (ret != 0) {
1215         MMI_HILOGE("Get pointer visible failed, ret:%{public}d", ret);
1216     }
1217     return visible;
1218 #else
1219     MMI_HILOGW("Pointer device or pointer drawing module does not support");
1220     return false;
1221 #endif // OHOS_BUILD_ENABLE_POINTER_DRAWING
1222 }
1223 
SetPointerColor(int32_t color)1224 int32_t InputManagerImpl::SetPointerColor(int32_t color)
1225 {
1226     CALL_INFO_TRACE;
1227 #if defined OHOS_BUILD_ENABLE_POINTER
1228     std::lock_guard<std::mutex> guard(mtx_);
1229     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->SetPointerColor(color);
1230     if (ret != RET_OK) {
1231         MMI_HILOGE("Set pointer color failed, ret:%{public}d", ret);
1232     }
1233     return ret;
1234 #else
1235     MMI_HILOGW("Pointer device module does not support");
1236     return ERROR_UNSUPPORT;
1237 #endif // OHOS_BUILD_ENABLE_POINTER
1238 }
1239 
GetPointerColor(int32_t & color)1240 int32_t InputManagerImpl::GetPointerColor(int32_t &color)
1241 {
1242     CALL_INFO_TRACE;
1243 #ifdef OHOS_BUILD_ENABLE_POINTER
1244     std::lock_guard<std::mutex> guard(mtx_);
1245     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->GetPointerColor(color);
1246     if (ret != RET_OK) {
1247         MMI_HILOGE("Get pointer color failed");
1248     }
1249     return ret;
1250 #else
1251     MMI_HILOGW("Pointer device does not support");
1252     return ERROR_UNSUPPORT;
1253 #endif // OHOS_BUILD_ENABLE_POINTER
1254 }
1255 
EnableCombineKey(bool enable)1256 int32_t InputManagerImpl::EnableCombineKey(bool enable)
1257 {
1258     CALL_INFO_TRACE;
1259     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->EnableCombineKey(enable);
1260     if (ret != RET_OK) {
1261         MMI_HILOGE("Enable combine key failed, ret:%{public}d", ret);
1262     }
1263     return ret;
1264 }
1265 
SetPointerSpeed(int32_t speed)1266 int32_t InputManagerImpl::SetPointerSpeed(int32_t speed)
1267 {
1268     CALL_INFO_TRACE;
1269 #ifdef OHOS_BUILD_ENABLE_POINTER
1270     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->SetPointerSpeed(speed);
1271     if (ret != RET_OK) {
1272         MMI_HILOGE("Failed to set pointer speed");
1273         return RET_ERR;
1274     }
1275     return RET_OK;
1276 #else
1277     MMI_HILOGW("Pointer device does not support");
1278     return ERROR_UNSUPPORT;
1279 #endif // OHOS_BUILD_ENABLE_POINTER
1280 }
1281 
GetPointerSpeed(int32_t & speed)1282 int32_t InputManagerImpl::GetPointerSpeed(int32_t &speed)
1283 {
1284     CALL_INFO_TRACE;
1285 #ifdef OHOS_BUILD_ENABLE_POINTER
1286     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->GetPointerSpeed(speed);
1287     if (ret != RET_OK) {
1288         MMI_HILOGE("Get pointer speed failed");
1289         return RET_ERR;
1290     }
1291     return RET_OK;
1292 #else
1293     return ERROR_UNSUPPORT;
1294     MMI_HILOGW("Pointer device does not support");
1295 #endif // OHOS_BUILD_ENABLE_POINTER
1296 }
1297 
SetPointerStyle(int32_t windowId,const PointerStyle & pointerStyle,bool isUiExtension)1298 int32_t InputManagerImpl::SetPointerStyle(int32_t windowId, const PointerStyle& pointerStyle, bool isUiExtension)
1299 {
1300     CALL_INFO_TRACE;
1301     if (pointerStyle.id < 0) {
1302         MMI_HILOGE("The param is invalid");
1303         return RET_ERR;
1304     }
1305 
1306     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->SetPointerStyle(windowId, pointerStyle, isUiExtension);
1307     if (ret != RET_OK) {
1308         MMI_HILOGE("Set pointer style failed, ret:%{public}d", ret);
1309         return ret;
1310     }
1311     return RET_OK;
1312 }
1313 
GetPointerStyle(int32_t windowId,PointerStyle & pointerStyle,bool isUiExtension)1314 int32_t InputManagerImpl::GetPointerStyle(int32_t windowId, PointerStyle &pointerStyle, bool isUiExtension)
1315 {
1316     CALL_DEBUG_ENTER;
1317     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->GetPointerStyle(windowId, pointerStyle, isUiExtension);
1318     if (ret != RET_OK) {
1319         MMI_HILOGE("Get pointer style failed, ret:%{public}d", ret);
1320         return ret;
1321     }
1322     return RET_OK;
1323 }
1324 
OnConnected()1325 void InputManagerImpl::OnConnected()
1326 {
1327     CALL_INFO_TRACE;
1328     ReAddInputEventFilter();
1329     if (!displayGroupInfo_.windowsInfo.empty() && !displayGroupInfo_.displaysInfo.empty()) {
1330         MMI_HILOGD("displayGroupInfo_: windowsInfo size: %{public}zu, displaysInfo size: %{public}zu",
1331             displayGroupInfo_.windowsInfo.size(), displayGroupInfo_.displaysInfo.size());
1332         SendDisplayInfo();
1333         PrintDisplayInfo();
1334     }
1335     if (!windowGroupInfo_.windowsInfo.empty()) {
1336         MMI_HILOGD("windowGroupInfo_: windowsInfo size: %{public}zu", windowGroupInfo_.windowsInfo.size());
1337         SendWindowInfo();
1338     }
1339 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
1340     SendEnhanceConfig();
1341     PrintEnhanceConfig();
1342 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
1343 
1344     if (windowStatecallback_ != nullptr) {
1345         MMIClientPtr client = MMIEventHdl.GetMMIClient();
1346         if (client != nullptr) {
1347             NetPacket pkt(MmiMessageId::WINDOW_STATE_ERROR_CALLBACK);
1348             if (!client->SendMessage(pkt)) {
1349                 MMI_HILOGE("Send message failed, errCode:%{public}d", MSG_SEND_FAIL);
1350             }
1351         } else {
1352             MMI_HILOGE("Get client failed");
1353         }
1354     }
1355     if (anrObservers_.empty()) {
1356         return;
1357     }
1358     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->SetAnrObserver();
1359     if (ret != RET_OK) {
1360         MMI_HILOGE("Set anr observer failed, ret:%{public}d", ret);
1361     }
1362 }
1363 
1364 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
1365 template<typename T>
RecoverPointerEvent(std::initializer_list<T> pointerActionEvents,T pointerActionEvent)1366 bool InputManagerImpl::RecoverPointerEvent(std::initializer_list<T> pointerActionEvents, T pointerActionEvent)
1367 {
1368     CALL_INFO_TRACE;
1369     std::shared_ptr<PointerEvent> currentPointerEvent = nullptr;
1370     {
1371         std::lock_guard<std::mutex> guard(resourceMtx_);
1372         CHKPF(lastPointerEvent_);
1373         currentPointerEvent = std::make_shared<PointerEvent>(*lastPointerEvent_);
1374     }
1375 
1376     CHKPF(currentPointerEvent);
1377     int32_t pointerAction = currentPointerEvent->GetPointerAction();
1378     for (const auto &it : pointerActionEvents) {
1379         if (pointerAction == it) {
1380             PointerEvent::PointerItem item;
1381             int32_t pointerId = currentPointerEvent->GetPointerId();
1382             if (!currentPointerEvent->GetPointerItem(pointerId, item)) {
1383                 MMI_HILOG_DISPATCHD("Get pointer item failed. pointer:%{public}d",
1384                     pointerId);
1385                 return false;
1386             }
1387             item.SetPressed(false);
1388             currentPointerEvent->UpdatePointerItem(pointerId, item);
1389             currentPointerEvent->SetPointerAction(pointerActionEvent);
1390             OnPointerEvent(currentPointerEvent);
1391             return true;
1392         }
1393     }
1394     return false;
1395 }
1396 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
1397 
OnDisconnected()1398 void InputManagerImpl::OnDisconnected()
1399 {
1400     CALL_INFO_TRACE;
1401 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
1402     std::initializer_list<int32_t> pointerActionEvents { PointerEvent::POINTER_ACTION_MOVE,
1403         PointerEvent::POINTER_ACTION_DOWN };
1404     std::initializer_list<int32_t> pointerActionPullEvents { PointerEvent::POINTER_ACTION_PULL_MOVE,
1405         PointerEvent::POINTER_ACTION_PULL_DOWN };
1406     if (RecoverPointerEvent(pointerActionEvents, PointerEvent::POINTER_ACTION_UP)) {
1407         MMI_HILOGE("Up event for service exception re-sending");
1408         return;
1409     }
1410 
1411     if (RecoverPointerEvent(pointerActionPullEvents, PointerEvent::POINTER_ACTION_PULL_UP)) {
1412         MMI_HILOGE("Pull up event for service exception re-sending");
1413         return;
1414     }
1415 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
1416 }
1417 
SendDisplayInfo()1418 int32_t InputManagerImpl::SendDisplayInfo()
1419 {
1420     CALL_DEBUG_ENTER;
1421     MMIClientPtr client = MMIEventHdl.GetMMIClient();
1422     CHKPR(client, RET_ERR);
1423     NetPacket pkt(MmiMessageId::DISPLAY_INFO);
1424     int32_t ret = PackDisplayData(pkt);
1425     if (ret != RET_OK) {
1426         MMI_HILOGE("Pack display info failed");
1427         return ret;
1428     }
1429     if (!client->SendMessage(pkt)) {
1430         MMI_HILOGE("Send message failed, errCode:%{public}d", MSG_SEND_FAIL);
1431         return MSG_SEND_FAIL;
1432     }
1433     return RET_OK;
1434 }
1435 
SendWindowInfo()1436 int32_t InputManagerImpl::SendWindowInfo()
1437 {
1438     CALL_DEBUG_ENTER;
1439     MMIClientPtr client = MMIEventHdl.GetMMIClient();
1440     CHKPR(client, RET_ERR);
1441     NetPacket pkt(MmiMessageId::WINDOW_INFO);
1442     int32_t ret = PackWindowGroupInfo(pkt);
1443     if (ret != RET_OK) {
1444         MMI_HILOGE("Pack window group info failed");
1445         return ret;
1446     }
1447     if (!client->SendMessage(pkt)) {
1448         MMI_HILOGE("Send message failed, errCode:%{public}d", MSG_SEND_FAIL);
1449         return MSG_SEND_FAIL;
1450     }
1451     return RET_OK;
1452 }
1453 
RegisterWindowStateErrorCallback(std::function<void (int32_t,int32_t)> callback)1454 int32_t InputManagerImpl::RegisterWindowStateErrorCallback(std::function<void(int32_t, int32_t)> callback)
1455 {
1456     CALL_DEBUG_ENTER;
1457     const std::string sceneboard = "com.ohos.sceneboard";
1458     const std::string programName(GetProgramName());
1459     if (programName != sceneboard) {
1460         MMI_HILOGE("Not sceneboard paogramName");
1461         return RET_ERR;
1462     }
1463     CHKPR(callback, RET_ERR);
1464     windowStatecallback_ = callback;
1465     std::lock_guard<std::mutex> guard(mtx_);
1466     if (!MMIEventHdl.InitClient()) {
1467         MMI_HILOGE("Client init failed");
1468         return RET_ERR;
1469     }
1470     MMIClientPtr client = MMIEventHdl.GetMMIClient();
1471     CHKPR(client, RET_ERR);
1472     NetPacket pkt(MmiMessageId::WINDOW_STATE_ERROR_CALLBACK);
1473     if (!client->SendMessage(pkt)) {
1474         MMI_HILOGE("Send message failed, errCode:%{public}d", MSG_SEND_FAIL);
1475         return MSG_SEND_FAIL;
1476     }
1477     return RET_OK;
1478 }
1479 
1480 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
SendEnhanceConfig()1481 void InputManagerImpl::SendEnhanceConfig()
1482 {
1483     MMIClientPtr client = MMIEventHdl.GetMMIClient();
1484     CHKPV(client);
1485     NetPacket pkt(MmiMessageId::SCINFO_CONFIG);
1486     if (PackEnhanceConfig(pkt) == RET_ERR) {
1487         return;
1488     }
1489     if (!client->SendMessage(pkt)) {
1490         MMI_HILOGE("Send message failed, errCode:%{public}d", MSG_SEND_FAIL);
1491     }
1492 }
1493 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
1494 
ReAddInputEventFilter()1495 void InputManagerImpl::ReAddInputEventFilter()
1496 {
1497     CALL_INFO_TRACE;
1498     if (eventFilterServices_.size() > MAX_FILTER_NUM) {
1499         MMI_HILOGE("Too many filters, size:%{public}zu", eventFilterServices_.size());
1500         return;
1501     }
1502     for (const auto &[filterId, t] : eventFilterServices_) {
1503         const auto &[service, priority, deviceTags] = t;
1504         int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->AddInputEventFilter(service, filterId, priority, deviceTags);
1505         if (ret != RET_OK) {
1506             MMI_HILOGE("AddInputEventFilter has send to server failed, filterId:%{public}d, priority:%{public}d,"
1507                 "deviceTags:%{public}u, ret:%{public}d", filterId, priority, deviceTags, ret);
1508         }
1509     }
1510 }
1511 
RegisterDevListener(std::string type,std::shared_ptr<IInputDeviceListener> listener)1512 int32_t InputManagerImpl::RegisterDevListener(std::string type, std::shared_ptr<IInputDeviceListener> listener)
1513 {
1514     CALL_INFO_TRACE;
1515     std::lock_guard<std::mutex> guard(mtx_);
1516     if (!MMIEventHdl.InitClient()) {
1517         MMI_HILOGE("Client init failed");
1518         return RET_ERR;
1519     }
1520     return INPUT_DEVICE_IMPL.RegisterDevListener(type, listener);
1521 }
1522 
UnregisterDevListener(std::string type,std::shared_ptr<IInputDeviceListener> listener)1523 int32_t InputManagerImpl::UnregisterDevListener(std::string type,
1524     std::shared_ptr<IInputDeviceListener> listener)
1525 {
1526     CALL_INFO_TRACE;
1527     std::lock_guard<std::mutex> guard(mtx_);
1528     if (!MMIEventHdl.InitClient()) {
1529         MMI_HILOGE("Client init failed");
1530         return RET_ERR;
1531     }
1532     return INPUT_DEVICE_IMPL.UnregisterDevListener(type, listener);
1533 }
1534 
GetDeviceIds(std::function<void (std::vector<int32_t> &)> callback)1535 int32_t InputManagerImpl::GetDeviceIds(std::function<void(std::vector<int32_t>&)> callback)
1536 {
1537     CALL_DEBUG_ENTER;
1538     std::lock_guard<std::mutex> guard(mtx_);
1539     if (!MMIEventHdl.InitClient()) {
1540         MMI_HILOGE("Client init failed");
1541         return RET_ERR;
1542     }
1543     return INPUT_DEVICE_IMPL.GetInputDeviceIds(callback);
1544 }
1545 
GetDevice(int32_t deviceId,std::function<void (std::shared_ptr<InputDevice>)> callback)1546 int32_t InputManagerImpl::GetDevice(int32_t deviceId,
1547     std::function<void(std::shared_ptr<InputDevice>)> callback)
1548 {
1549     CALL_DEBUG_ENTER;
1550     std::lock_guard<std::mutex> guard(mtx_);
1551     if (!MMIEventHdl.InitClient()) {
1552         MMI_HILOGE("Client init failed");
1553         return RET_ERR;
1554     }
1555     return INPUT_DEVICE_IMPL.GetInputDevice(deviceId, callback);
1556 }
1557 
SupportKeys(int32_t deviceId,std::vector<int32_t> & keyCodes,std::function<void (std::vector<bool> &)> callback)1558 int32_t InputManagerImpl::SupportKeys(int32_t deviceId, std::vector<int32_t> &keyCodes,
1559     std::function<void(std::vector<bool>&)> callback)
1560 {
1561     CALL_INFO_TRACE;
1562     std::lock_guard<std::mutex> guard(mtx_);
1563     if (!MMIEventHdl.InitClient()) {
1564         MMI_HILOGE("Client init failed");
1565         return RET_ERR;
1566     }
1567     return INPUT_DEVICE_IMPL.SupportKeys(deviceId, keyCodes, callback);
1568 }
1569 
GetKeyboardType(int32_t deviceId,std::function<void (int32_t)> callback)1570 int32_t InputManagerImpl::GetKeyboardType(int32_t deviceId, std::function<void(int32_t)> callback)
1571 {
1572     CALL_DEBUG_ENTER;
1573     std::lock_guard<std::mutex> guard(mtx_);
1574     if (!MMIEventHdl.InitClient()) {
1575         MMI_HILOGE("Client init failed");
1576         return RET_ERR;
1577     }
1578     return INPUT_DEVICE_IMPL.GetKeyboardType(deviceId, callback);
1579 }
1580 
SetKeyboardRepeatDelay(int32_t delay)1581 int32_t InputManagerImpl::SetKeyboardRepeatDelay(int32_t delay)
1582 {
1583     CALL_INFO_TRACE;
1584     std::lock_guard<std::mutex> guard(mtx_);
1585     if (!MMIEventHdl.InitClient()) {
1586         MMI_HILOGE("Client init failed");
1587         return RET_ERR;
1588     }
1589     return INPUT_DEVICE_IMPL.SetKeyboardRepeatDelay(delay);
1590 }
1591 
SetKeyboardRepeatRate(int32_t rate)1592 int32_t InputManagerImpl::SetKeyboardRepeatRate(int32_t rate)
1593 {
1594     CALL_INFO_TRACE;
1595     std::lock_guard<std::mutex> guard(mtx_);
1596     if (!MMIEventHdl.InitClient()) {
1597         MMI_HILOGE("Client init failed");
1598         return RET_ERR;
1599     }
1600     return INPUT_DEVICE_IMPL.SetKeyboardRepeatRate(rate);
1601 }
1602 
GetKeyboardRepeatDelay(std::function<void (int32_t)> callback)1603 int32_t InputManagerImpl::GetKeyboardRepeatDelay(std::function<void(int32_t)> callback)
1604 {
1605     CALL_INFO_TRACE;
1606     std::lock_guard<std::mutex> guard(mtx_);
1607     if (!MMIEventHdl.InitClient()) {
1608         MMI_HILOGE("Client init failed");
1609         return RET_ERR;
1610     }
1611     return INPUT_DEVICE_IMPL.GetKeyboardRepeatDelay(callback);
1612 }
1613 
GetKeyboardRepeatRate(std::function<void (int32_t)> callback)1614 int32_t InputManagerImpl::GetKeyboardRepeatRate(std::function<void(int32_t)> callback)
1615 {
1616     CALL_INFO_TRACE;
1617     std::lock_guard<std::mutex> guard(mtx_);
1618     if (!MMIEventHdl.InitClient()) {
1619         MMI_HILOGE("Client init failed");
1620         return RET_ERR;
1621     }
1622     return INPUT_DEVICE_IMPL.GetKeyboardRepeatRate(callback);
1623 }
1624 
SetAnrObserver(std::shared_ptr<IAnrObserver> observer)1625 void InputManagerImpl::SetAnrObserver(std::shared_ptr<IAnrObserver> observer)
1626 {
1627     CALL_INFO_TRACE;
1628     std::lock_guard<std::mutex> guard(mtx_);
1629     if (!MMIEventHdl.InitClient()) {
1630         MMI_HILOG_ANRDETECTE("Client init failed");
1631         return;
1632     }
1633     for (auto iter = anrObservers_.begin(); iter != anrObservers_.end(); ++iter) {
1634         if (*iter == observer) {
1635             MMI_HILOG_ANRDETECTE("Observer already exist");
1636             return;
1637         }
1638     }
1639     anrObservers_.push_back(observer);
1640     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->SetAnrObserver();
1641     if (ret != RET_OK) {
1642         MMI_HILOG_ANRDETECTE("Set anr observer failed, ret:%{public}d", ret);
1643     }
1644 }
1645 
OnAnr(int32_t pid,int32_t eventId)1646 void InputManagerImpl::OnAnr(int32_t pid, int32_t eventId)
1647 {
1648     CALL_INFO_TRACE;
1649     CHK_PID_AND_TID();
1650     {
1651         std::lock_guard<std::mutex> guard(mtx_);
1652         for (const auto &observer : anrObservers_) {
1653             CHKPC(observer);
1654             observer->OnAnr(pid, eventId);
1655         }
1656     }
1657     MMI_HILOG_ANRDETECTI("ANR noticed pid:%{public}d eventId:%{public}d", pid, eventId);
1658 }
1659 
GetFunctionKeyState(int32_t funcKey)1660 bool InputManagerImpl::GetFunctionKeyState(int32_t funcKey)
1661 {
1662     CALL_INFO_TRACE;
1663 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
1664     bool state { false };
1665     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->GetFunctionKeyState(funcKey, state);
1666     if (ret != RET_OK) {
1667         MMI_HILOGE("Send to server failed, ret:%{public}d", ret);
1668     }
1669     return state;
1670 #else
1671     MMI_HILOGW("Keyboard device does not support");
1672     return false;
1673 #endif // OHOS_BUILD_ENABLE_KEYBOARD
1674 }
1675 
SetFunctionKeyState(int32_t funcKey,bool enable)1676 int32_t InputManagerImpl::SetFunctionKeyState(int32_t funcKey, bool enable)
1677 {
1678     CALL_INFO_TRACE;
1679 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
1680     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->SetFunctionKeyState(funcKey, enable);
1681     if (ret != RET_OK) {
1682         MMI_HILOGE("Send to server failed, ret:%{public}d", ret);
1683         return RET_ERR;
1684     }
1685     return RET_OK;
1686 #else
1687     MMI_HILOGW("Keyboard device does not support");
1688     return ERROR_UNSUPPORT;
1689 #endif // OHOS_BUILD_ENABLE_KEYBOARD
1690 }
1691 
SetPointerLocation(int32_t x,int32_t y)1692 int32_t InputManagerImpl::SetPointerLocation(int32_t x, int32_t y)
1693 {
1694     CALL_INFO_TRACE;
1695 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
1696     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->SetPointerLocation(x, y);
1697     if (ret != RET_OK) {
1698         MMI_HILOGE("Set Pointer Location failed, ret:%{public}d", ret);
1699     }
1700     return ret;
1701 #else
1702     MMI_HILOGW("Pointer device or pointer drawing module does not support");
1703     return ERROR_UNSUPPORT;
1704 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
1705 }
1706 
EnterCaptureMode(int32_t windowId)1707 int32_t InputManagerImpl::EnterCaptureMode(int32_t windowId)
1708 {
1709     CALL_INFO_TRACE;
1710 #if defined(OHOS_BUILD_ENABLE_POINTER)
1711     std::lock_guard<std::mutex> guard(mtx_);
1712     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->SetMouseCaptureMode(windowId, true);
1713     if (ret != RET_OK) {
1714         MMI_HILOGE("Enter captrue mode failed");
1715     }
1716     return ret;
1717 #else
1718     MMI_HILOGW("Pointer device module does not support");
1719     return ERROR_UNSUPPORT;
1720 #endif // OHOS_BUILD_ENABLE_POINTER
1721 }
1722 
LeaveCaptureMode(int32_t windowId)1723 int32_t InputManagerImpl::LeaveCaptureMode(int32_t windowId)
1724 {
1725     CALL_INFO_TRACE;
1726 #if defined(OHOS_BUILD_ENABLE_POINTER)
1727     std::lock_guard<std::mutex> guard(mtx_);
1728     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->SetMouseCaptureMode(windowId, false);
1729     if (ret != RET_OK) {
1730         MMI_HILOGE("Leave captrue mode failed");
1731     }
1732     return ret;
1733 #else
1734     MMI_HILOGW("Pointer device module does not support");
1735     return ERROR_UNSUPPORT;
1736 #endif // OHOS_BUILD_ENABLE_POINTER
1737 }
1738 
AppendExtraData(const ExtraData & extraData)1739 void InputManagerImpl::AppendExtraData(const ExtraData& extraData)
1740 {
1741     CALL_INFO_TRACE;
1742     if (extraData.buffer.size() > ExtraData::MAX_BUFFER_SIZE) {
1743         MMI_HILOGE("Append extra data failed, buffer is oversize:%{public}zu", extraData.buffer.size());
1744         return;
1745     }
1746     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->AppendExtraData(extraData);
1747     if (ret != RET_OK) {
1748         MMI_HILOGE("Append extra data failed:%{public}d", ret);
1749     }
1750 }
1751 
EnableInputDevice(bool enable)1752 int32_t InputManagerImpl::EnableInputDevice(bool enable)
1753 {
1754     CALL_INFO_TRACE;
1755     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->EnableInputDevice(enable);
1756     if (ret != RET_OK) {
1757         MMI_HILOGE("Enable input device failed, ret:%{public}d", ret);
1758     }
1759     return ret;
1760 }
1761 
SetKeyDownDuration(const std::string & businessId,int32_t delay)1762 int32_t InputManagerImpl::SetKeyDownDuration(const std::string &businessId, int32_t delay)
1763 {
1764     CALL_INFO_TRACE;
1765     if (delay < MIN_DELAY || delay > MAX_DELAY) {
1766         MMI_HILOGE("The param is invalid");
1767         return RET_ERR;
1768     }
1769     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->SetKeyDownDuration(businessId, delay);
1770     if (ret != RET_OK) {
1771         MMI_HILOGE("Set Key down duration failed, ret:%{public}d", ret);
1772         return ret;
1773     }
1774     return RET_OK;
1775 }
1776 
SetTouchpadScrollSwitch(bool switchFlag)1777 int32_t InputManagerImpl::SetTouchpadScrollSwitch(bool switchFlag)
1778 {
1779     CALL_INFO_TRACE;
1780 #if defined OHOS_BUILD_ENABLE_POINTER
1781     std::lock_guard<std::mutex> guard(mtx_);
1782     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->SetTouchpadScrollSwitch(switchFlag);
1783     if (ret != RET_OK) {
1784         MMI_HILOGE("Set the touchpad scroll switch failed, ret:%{public}d", ret);
1785     }
1786     return ret;
1787 #else
1788     MMI_HILOGW("Pointer device module does not support");
1789     return ERROR_UNSUPPORT;
1790 #endif // OHOS_BUILD_ENABLE_POINTER
1791 }
1792 
GetTouchpadScrollSwitch(bool & switchFlag)1793 int32_t InputManagerImpl::GetTouchpadScrollSwitch(bool &switchFlag)
1794 {
1795     CALL_INFO_TRACE;
1796 #ifdef OHOS_BUILD_ENABLE_POINTER
1797     std::lock_guard<std::mutex> guard(mtx_);
1798     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->GetTouchpadScrollSwitch(switchFlag);
1799     if (ret != RET_OK) {
1800         MMI_HILOGE("Get the touchpad scroll switch failed, ret:%{public}d", ret);
1801     }
1802     return ret;
1803 #else
1804     MMI_HILOGW("Pointer device does not support");
1805     return ERROR_UNSUPPORT;
1806 #endif // OHOS_BUILD_ENABLE_POINTER
1807 }
1808 
SetTouchpadScrollDirection(bool state)1809 int32_t InputManagerImpl::SetTouchpadScrollDirection(bool state)
1810 {
1811     CALL_INFO_TRACE;
1812 #if defined OHOS_BUILD_ENABLE_POINTER
1813     std::lock_guard<std::mutex> guard(mtx_);
1814     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->SetTouchpadScrollDirection(state);
1815     if (ret != RET_OK) {
1816         MMI_HILOGE("Set the touchpad scroll direction switch failed, ret:%{public}d", ret);
1817     }
1818     return ret;
1819 #else
1820     MMI_HILOGW("Pointer device module does not support");
1821     return ERROR_UNSUPPORT;
1822 #endif // OHOS_BUILD_ENABLE_POINTER
1823 }
1824 
GetTouchpadScrollDirection(bool & state)1825 int32_t InputManagerImpl::GetTouchpadScrollDirection(bool &state)
1826 {
1827     CALL_INFO_TRACE;
1828 #ifdef OHOS_BUILD_ENABLE_POINTER
1829     std::lock_guard<std::mutex> guard(mtx_);
1830     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->GetTouchpadScrollDirection(state);
1831     if (ret != RET_OK) {
1832         MMI_HILOGE("Get the touchpad scroll direction switch failed, ret:%{public}d", ret);
1833     }
1834     return ret;
1835 #else
1836     MMI_HILOGW("Pointer device does not support");
1837     return ERROR_UNSUPPORT;
1838 #endif // OHOS_BUILD_ENABLE_POINTER
1839 }
1840 
SetTouchpadTapSwitch(bool switchFlag)1841 int32_t InputManagerImpl::SetTouchpadTapSwitch(bool switchFlag)
1842 {
1843     CALL_INFO_TRACE;
1844 #if defined OHOS_BUILD_ENABLE_POINTER
1845     std::lock_guard<std::mutex> guard(mtx_);
1846     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->SetTouchpadTapSwitch(switchFlag);
1847     if (ret != RET_OK) {
1848         MMI_HILOGE("Set the touchpad tap switch failed, ret:%{public}d", ret);
1849     }
1850     return ret;
1851 #else
1852     MMI_HILOGW("Pointer device module does not support");
1853     return ERROR_UNSUPPORT;
1854 #endif // OHOS_BUILD_ENABLE_POINTER
1855 }
1856 
GetTouchpadTapSwitch(bool & switchFlag)1857 int32_t InputManagerImpl::GetTouchpadTapSwitch(bool &switchFlag)
1858 {
1859     CALL_INFO_TRACE;
1860 #ifdef OHOS_BUILD_ENABLE_POINTER
1861     std::lock_guard<std::mutex> guard(mtx_);
1862     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->GetTouchpadTapSwitch(switchFlag);
1863     if (ret != RET_OK) {
1864         MMI_HILOGE("Get the touchpad tap switch failed");
1865     }
1866     return ret;
1867 #else
1868     MMI_HILOGW("Pointer device does not support");
1869     return ERROR_UNSUPPORT;
1870 #endif // OHOS_BUILD_ENABLE_POINTER
1871 }
1872 
SetTouchpadPointerSpeed(int32_t speed)1873 int32_t InputManagerImpl::SetTouchpadPointerSpeed(int32_t speed)
1874 {
1875     CALL_INFO_TRACE;
1876 #if defined OHOS_BUILD_ENABLE_POINTER
1877     std::lock_guard<std::mutex> guard(mtx_);
1878     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->SetTouchpadPointerSpeed(speed);
1879     if (ret != RET_OK) {
1880         MMI_HILOGE("Set the touchpad pointer speed failed, ret:%{public}d", ret);
1881     }
1882     return ret;
1883 #else
1884     MMI_HILOGW("Pointer device module does not support");
1885     return ERROR_UNSUPPORT;
1886 #endif // OHOS_BUILD_ENABLE_POINTER
1887 }
1888 
GetTouchpadPointerSpeed(int32_t & speed)1889 int32_t InputManagerImpl::GetTouchpadPointerSpeed(int32_t &speed)
1890 {
1891     CALL_INFO_TRACE;
1892 #ifdef OHOS_BUILD_ENABLE_POINTER
1893     std::lock_guard<std::mutex> guard(mtx_);
1894     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->GetTouchpadPointerSpeed(speed);
1895     if (ret != RET_OK) {
1896         MMI_HILOGE("Get the touchpad pointer speed failed");
1897     }
1898     return ret;
1899 #else
1900     MMI_HILOGW("Pointer device does not support");
1901     return ERROR_UNSUPPORT;
1902 #endif // OHOS_BUILD_ENABLE_POINTER
1903 }
1904 
SetTouchpadPinchSwitch(bool switchFlag)1905 int32_t InputManagerImpl::SetTouchpadPinchSwitch(bool switchFlag)
1906 {
1907     CALL_INFO_TRACE;
1908 #if defined OHOS_BUILD_ENABLE_POINTER
1909     std::lock_guard<std::mutex> guard(mtx_);
1910     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->SetTouchpadPinchSwitch(switchFlag);
1911     if (ret != RET_OK) {
1912         MMI_HILOGE("Set the touchpad pinch switch failed, ret:%{public}d", ret);
1913     }
1914     return ret;
1915 #else
1916     MMI_HILOGW("Pointer device module does not support");
1917     return ERROR_UNSUPPORT;
1918 #endif // OHOS_BUILD_ENABLE_POINTER
1919 }
1920 
GetTouchpadPinchSwitch(bool & switchFlag)1921 int32_t InputManagerImpl::GetTouchpadPinchSwitch(bool &switchFlag)
1922 {
1923     CALL_INFO_TRACE;
1924 #ifdef OHOS_BUILD_ENABLE_POINTER
1925     std::lock_guard<std::mutex> guard(mtx_);
1926     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->GetTouchpadPinchSwitch(switchFlag);
1927     if (ret != RET_OK) {
1928         MMI_HILOGE("Get the touchpad pinch switch failed");
1929     }
1930     return ret;
1931 #else
1932     MMI_HILOGW("Pointer device does not support");
1933     return ERROR_UNSUPPORT;
1934 #endif // OHOS_BUILD_ENABLE_POINTER
1935 }
1936 
SetTouchpadSwipeSwitch(bool switchFlag)1937 int32_t InputManagerImpl::SetTouchpadSwipeSwitch(bool switchFlag)
1938 {
1939     CALL_INFO_TRACE;
1940 #if defined OHOS_BUILD_ENABLE_POINTER
1941     std::lock_guard<std::mutex> guard(mtx_);
1942     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->SetTouchpadSwipeSwitch(switchFlag);
1943     if (ret != RET_OK) {
1944         MMI_HILOGE("Set the touchpad swipe switch failed, ret:%{public}d", ret);
1945     }
1946     return ret;
1947 #else
1948     MMI_HILOGW("Pointer device module does not support");
1949     return ERROR_UNSUPPORT;
1950 #endif // OHOS_BUILD_ENABLE_POINTER
1951 }
1952 
GetTouchpadSwipeSwitch(bool & switchFlag)1953 int32_t InputManagerImpl::GetTouchpadSwipeSwitch(bool &switchFlag)
1954 {
1955     CALL_INFO_TRACE;
1956 #ifdef OHOS_BUILD_ENABLE_POINTER
1957     std::lock_guard<std::mutex> guard(mtx_);
1958     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->GetTouchpadSwipeSwitch(switchFlag);
1959     if (ret != RET_OK) {
1960         MMI_HILOGE("Get the touchpad swipe switch failed");
1961     }
1962     return ret;
1963 #else
1964     MMI_HILOGW("Pointer device does not support");
1965     return ERROR_UNSUPPORT;
1966 #endif // OHOS_BUILD_ENABLE_POINTER
1967 }
1968 
SetTouchpadRightClickType(int32_t type)1969 int32_t InputManagerImpl::SetTouchpadRightClickType(int32_t type)
1970 {
1971     CALL_INFO_TRACE;
1972 #if defined OHOS_BUILD_ENABLE_POINTER
1973     std::lock_guard<std::mutex> guard(mtx_);
1974     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->SetTouchpadRightClickType(type);
1975     if (ret != RET_OK) {
1976         MMI_HILOGE("Set the touchpad right click type failed, ret:%{public}d", ret);
1977     }
1978     return ret;
1979 #else
1980     MMI_HILOGW("Pointer device module does not support");
1981     return ERROR_UNSUPPORT;
1982 #endif // OHOS_BUILD_ENABLE_POINTER
1983 }
1984 
GetTouchpadRightClickType(int32_t & type)1985 int32_t InputManagerImpl::GetTouchpadRightClickType(int32_t &type)
1986 {
1987     CALL_INFO_TRACE;
1988 #ifdef OHOS_BUILD_ENABLE_POINTER
1989     std::lock_guard<std::mutex> guard(mtx_);
1990     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->GetTouchpadRightClickType(type);
1991     if (ret != RET_OK) {
1992         MMI_HILOGE("Get the touchpad right click failed");
1993     }
1994     return ret;
1995 #else
1996     MMI_HILOGW("Pointer device does not support");
1997     return ERROR_UNSUPPORT;
1998 #endif // OHOS_BUILD_ENABLE_POINTER
1999 }
2000 
SetTouchpadRotateSwitch(bool rotateSwitch)2001 int32_t InputManagerImpl::SetTouchpadRotateSwitch(bool rotateSwitch)
2002 {
2003     CALL_INFO_TRACE;
2004 #if defined OHOS_BUILD_ENABLE_POINTER
2005     std::lock_guard<std::mutex> guard(mtx_);
2006     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->SetTouchpadRotateSwitch(rotateSwitch);
2007     if (ret != RET_OK) {
2008         MMI_HILOGE("Set touchpad rotate switch failed, ret:%{public}d", ret);
2009     }
2010     return ret;
2011 #else
2012     MMI_HILOGW("Pointer device module does not support");
2013     return ERROR_UNSUPPORT;
2014 #endif // OHOS_BUILD_ENABLE_POINTER
2015 }
2016 
GetTouchpadRotateSwitch(bool & rotateSwitch)2017 int32_t InputManagerImpl::GetTouchpadRotateSwitch(bool &rotateSwitch)
2018 {
2019     CALL_INFO_TRACE;
2020 #ifdef OHOS_BUILD_ENABLE_POINTER
2021     std::lock_guard<std::mutex> guard(mtx_);
2022     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->GetTouchpadRotateSwitch(rotateSwitch);
2023     if (ret != RET_OK) {
2024         MMI_HILOGE("Get touchpad rotate switch failed");
2025     }
2026     return ret;
2027 #else
2028     MMI_HILOGW("Pointer device does not support");
2029     return ERROR_UNSUPPORT;
2030 #endif // OHOS_BUILD_ENABLE_POINTER
2031 }
2032 
EnableHardwareCursorStats(bool enable)2033 int32_t InputManagerImpl::EnableHardwareCursorStats(bool enable)
2034 {
2035     CALL_INFO_TRACE;
2036 #ifdef OHOS_BUILD_ENABLE_POINTER
2037     std::lock_guard<std::mutex> guard(mtx_);
2038     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->EnableHardwareCursorStats(enable);
2039     if (ret != RET_OK) {
2040         MMI_HILOGE("Enable hardware cursor stats failed");
2041     }
2042     return ret;
2043 #else
2044     MMI_HILOGW("Pointer device does not support");
2045     return ERROR_UNSUPPORT;
2046 #endif // OHOS_BUILD_ENABLE_POINTER
2047 }
2048 
GetHardwareCursorStats(uint32_t & frameCount,uint32_t & vsyncCount)2049 int32_t InputManagerImpl::GetHardwareCursorStats(uint32_t &frameCount, uint32_t &vsyncCount)
2050 {
2051     CALL_INFO_TRACE;
2052 #ifdef OHOS_BUILD_ENABLE_POINTER
2053     std::lock_guard<std::mutex> guard(mtx_);
2054     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->GetHardwareCursorStats(frameCount, vsyncCount);
2055     if (ret != RET_OK) {
2056         MMI_HILOGE("Get hardware cursor stats failed");
2057     }
2058     MMI_HILOGD("GetHardwareCursorStats, frameCount:%{public}d, vsyncCount:%{public}d", frameCount, vsyncCount);
2059     return ret;
2060 #else
2061     MMI_HILOGW("Pointer device does not support");
2062     return ERROR_UNSUPPORT;
2063 #endif // OHOS_BUILD_ENABLE_POINTER
2064 }
2065 
GetPointerSnapshot(void * pixelMapPtr)2066 int32_t InputManagerImpl::GetPointerSnapshot(void *pixelMapPtr)
2067 {
2068     CALL_DEBUG_ENTER;
2069 #if defined(OHOS_BUILD_ENABLE_POINTER) && (OHOS_BUILD_ENABLE_MAGICCURSOR)
2070     std::lock_guard<std::mutex> guard(mtx_);
2071     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->GetPointerSnapshot(pixelMapPtr);
2072     if (ret != RET_OK) {
2073         MMI_HILOGE("Get the pointer snapshot failed, ret:%{public}d", ret);
2074     }
2075     return ret;
2076 #else
2077     MMI_HILOGW("Pointer device module does not support");
2078     return ERROR_UNSUPPORT;
2079 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_MAGICCURSOR
2080 }
2081 
SetNapStatus(int32_t pid,int32_t uid,const std::string & bundleName,int32_t napStatus)2082 int32_t InputManagerImpl::SetNapStatus(int32_t pid, int32_t uid, const std::string &bundleName, int32_t napStatus)
2083 {
2084     CALL_INFO_TRACE;
2085     std::lock_guard<std::mutex> guard(mtx_);
2086     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->SetNapStatus(pid, uid, bundleName, napStatus);
2087     if (ret != RET_OK) {
2088         MMI_HILOGE("Set napStatus failed, ret:%{public}d", ret);
2089     }
2090     return ret;
2091 }
2092 
NotifyBundleName(int32_t pid,int32_t uid,const std::string & bundleName,int32_t syncStatus)2093 void InputManagerImpl::NotifyBundleName(int32_t pid, int32_t uid, const std::string &bundleName, int32_t syncStatus)
2094 {
2095     CALL_INFO_TRACE;
2096     CHKPV(eventObserver_);
2097     eventObserver_->SyncBundleName(pid, uid, bundleName, syncStatus);
2098 }
2099 
SetWindowPointerStyle(WindowArea area,int32_t pid,int32_t windowId)2100 void InputManagerImpl::SetWindowPointerStyle(WindowArea area, int32_t pid, int32_t windowId)
2101 {
2102     CALL_INFO_TRACE;
2103 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
2104     std::lock_guard<std::mutex> guard(mtx_);
2105     if (!MMIEventHdl.InitClient()) {
2106         MMI_HILOGE("Get mmi client is nullptr");
2107         return;
2108     }
2109     SendWindowAreaInfo(area, pid, windowId);
2110 #else
2111     MMI_HILOGW("Pointer device or pointer drawing module does not support");
2112 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
2113 }
2114 
SendWindowAreaInfo(WindowArea area,int32_t pid,int32_t windowId)2115 void InputManagerImpl::SendWindowAreaInfo(WindowArea area, int32_t pid, int32_t windowId)
2116 {
2117     CALL_INFO_TRACE;
2118     MMIClientPtr client = MMIEventHdl.GetMMIClient();
2119     CHKPV(client);
2120     NetPacket pkt(MmiMessageId::WINDOW_AREA_INFO);
2121     pkt << area << pid << windowId;
2122     if (pkt.ChkRWError()) {
2123         MMI_HILOGE("Packet write logical data failed");
2124         return;
2125     }
2126     if (!client->SendMessage(pkt)) {
2127         MMI_HILOGE("Send message failed, errCode:%{public}d", MSG_SEND_FAIL);
2128     }
2129 }
2130 
ClearWindowPointerStyle(int32_t pid,int32_t windowId)2131 void InputManagerImpl::ClearWindowPointerStyle(int32_t pid, int32_t windowId)
2132 {
2133     CALL_INFO_TRACE;
2134     std::lock_guard<std::mutex> guard(mtx_);
2135     if (!MMIEventHdl.InitClient()) {
2136         MMI_HILOGE("Get mmi client is nullptr");
2137         return;
2138     }
2139     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->ClearWindowPointerStyle(pid, windowId);
2140     if (ret != RET_OK) {
2141         MMI_HILOGE("ClearWindowPointerStyle failed, ret:%{public}d", ret);
2142         return;
2143     }
2144 }
2145 
SetShieldStatus(int32_t shieldMode,bool isShield)2146 int32_t InputManagerImpl::SetShieldStatus(int32_t shieldMode, bool isShield)
2147 {
2148     CALL_INFO_TRACE;
2149     std::lock_guard<std::mutex> guard(mtx_);
2150 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
2151     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->SetShieldStatus(shieldMode, isShield);
2152     if (ret != RET_OK) {
2153         MMI_HILOGE("Set shield event interception status failed, ret:%{public}d", ret);
2154     }
2155     return ret;
2156 #else
2157     MMI_HILOGW("Keyboard device does not support");
2158     return ERROR_UNSUPPORT;
2159 #endif // OHOS_BUILD_ENABLE_KEYBOARD
2160 }
2161 
GetShieldStatus(int32_t shieldMode,bool & isShield)2162 int32_t InputManagerImpl::GetShieldStatus(int32_t shieldMode, bool &isShield)
2163 {
2164     CALL_INFO_TRACE;
2165     std::lock_guard<std::mutex> guard(mtx_);
2166 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
2167     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->GetShieldStatus(shieldMode, isShield);
2168     if (ret != RET_OK) {
2169         MMI_HILOGE("Get shield event interception status failed, ret:%{public}d", ret);
2170     }
2171     return ret;
2172 #else
2173     MMI_HILOGW("Keyboard device does not support");
2174     return ERROR_UNSUPPORT;
2175 #endif // OHOS_BUILD_ENABLE_KEYBOARD
2176 }
2177 
AddServiceWatcher(std::shared_ptr<IInputServiceWatcher> watcher)2178 void InputManagerImpl::AddServiceWatcher(std::shared_ptr<IInputServiceWatcher> watcher)
2179 {
2180     CALL_INFO_TRACE;
2181     MULTIMODAL_INPUT_CONNECT_MGR->AddServiceWatcher(watcher);
2182 }
2183 
RemoveServiceWatcher(std::shared_ptr<IInputServiceWatcher> watcher)2184 void InputManagerImpl::RemoveServiceWatcher(std::shared_ptr<IInputServiceWatcher> watcher)
2185 {
2186     CALL_INFO_TRACE;
2187     MULTIMODAL_INPUT_CONNECT_MGR->RemoveServiceWatcher(watcher);
2188 }
2189 
MarkProcessed(int32_t eventId,int64_t actionTime)2190 int32_t InputManagerImpl::MarkProcessed(int32_t eventId, int64_t actionTime)
2191 {
2192     ANRHDL->SetLastProcessedEventId(EVENT_TYPE, eventId, actionTime);
2193     return RET_OK;
2194 }
2195 
GetKeyState(std::vector<int32_t> & pressedKeys,std::map<int32_t,int32_t> & specialKeysState)2196 int32_t InputManagerImpl::GetKeyState(std::vector<int32_t> &pressedKeys, std::map<int32_t, int32_t> &specialKeysState)
2197 {
2198     CALL_DEBUG_ENTER;
2199     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->GetKeyState(pressedKeys, specialKeysState);
2200     if (ret != RET_OK) {
2201         MMI_HILOGE("Get key state failed, ret:%{public}d", ret);
2202         return ret;
2203     }
2204     return RET_OK;
2205 }
2206 
Authorize(bool isAuthorize)2207 void InputManagerImpl::Authorize(bool isAuthorize)
2208 {
2209     if (MMIEventHdl.Authorize(isAuthorize) != RET_OK) {
2210         MMI_HILOGE("Failed to authorize");
2211     }
2212 }
2213 
CancelInjection()2214 int32_t InputManagerImpl::CancelInjection()
2215 {
2216     if (MMIEventHdl.CancelInjection() != RET_OK) {
2217         MMI_HILOGE("CancelInjection failed");
2218         return RET_ERR;
2219     }
2220     return RET_OK;
2221 }
2222 
SetPixelMapData(int32_t infoId,void * pixelMap)2223 int32_t InputManagerImpl::SetPixelMapData(int32_t infoId, void* pixelMap)
2224 {
2225     CALL_DEBUG_ENTER;
2226     if (infoId < 0 || pixelMap == nullptr) {
2227         MMI_HILOGE("Invalid infoId or pixelMap");
2228         return RET_ERR;
2229     }
2230     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->SetPixelMapData(infoId, pixelMap);
2231     if (ret != RET_OK) {
2232         MMI_HILOGE("Failed to set pixel map, ret:%{public}d", ret);
2233     }
2234     return ret;
2235 }
2236 
HasIrEmitter(bool & hasIrEmitter)2237 int32_t InputManagerImpl::HasIrEmitter(bool &hasIrEmitter)
2238 {
2239     CALL_INFO_TRACE;
2240     return MULTIMODAL_INPUT_CONNECT_MGR->HasIrEmitter(hasIrEmitter);
2241 }
2242 
GetInfraredFrequencies(std::vector<InfraredFrequency> & requencys)2243 int32_t InputManagerImpl::GetInfraredFrequencies(std::vector<InfraredFrequency>& requencys)
2244 {
2245     CALL_INFO_TRACE;
2246     return MULTIMODAL_INPUT_CONNECT_MGR->GetInfraredFrequencies(requencys);
2247 }
2248 
TransmitInfrared(int64_t number,std::vector<int64_t> & pattern)2249 int32_t InputManagerImpl::TransmitInfrared(int64_t number, std::vector<int64_t>& pattern)
2250 {
2251     CALL_INFO_TRACE;
2252     return MULTIMODAL_INPUT_CONNECT_MGR->TransmitInfrared(number, pattern);
2253 }
2254 
SetCurrentUser(int32_t userId)2255 int32_t InputManagerImpl::SetCurrentUser(int32_t userId)
2256 {
2257     CALL_DEBUG_ENTER;
2258     if (userId < 0) {
2259         MMI_HILOGE("Invalid userId");
2260         return RET_ERR;
2261     }
2262     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->SetCurrentUser(userId);
2263     if (ret != RET_OK) {
2264         MMI_HILOGE("Failed to set userId, ret:%{public}d", ret);
2265     }
2266     return ret;
2267 }
2268 
GetWinSyncBatchSize(int32_t maxAreasCount,int32_t displayCount)2269 int32_t InputManagerImpl::GetWinSyncBatchSize(int32_t maxAreasCount, int32_t displayCount)
2270 {
2271     return (MAX_PKT_SIZE - GetDisplayMaxSize() * displayCount) / GetWindowMaxSize(maxAreasCount);
2272 }
2273 
AddVirtualInputDevice(std::shared_ptr<InputDevice> device,int32_t & deviceId)2274 int32_t InputManagerImpl::AddVirtualInputDevice(std::shared_ptr<InputDevice> device, int32_t &deviceId)
2275 {
2276     return MULTIMODAL_INPUT_CONNECT_MGR->AddVirtualInputDevice(device, deviceId);
2277 }
2278 
RemoveVirtualInputDevice(int32_t deviceId)2279 int32_t InputManagerImpl::RemoveVirtualInputDevice(int32_t deviceId)
2280 {
2281     return MULTIMODAL_INPUT_CONNECT_MGR->RemoveVirtualInputDevice(deviceId);
2282 }
2283 
AncoAddChannel(std::shared_ptr<IAncoConsumer> consumer)2284 int32_t InputManagerImpl::AncoAddChannel(std::shared_ptr<IAncoConsumer> consumer)
2285 {
2286 #ifdef OHOS_BUILD_ENABLE_ANCO
2287     std::lock_guard<std::mutex> guard(mtx_);
2288     if (ancoChannels_.find(consumer) != ancoChannels_.end()) {
2289         return RET_OK;
2290     }
2291     sptr<IAncoChannel> tChannel = sptr<AncoChannel>::MakeSptr(consumer);
2292     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->AncoAddChannel(tChannel);
2293     if (ret != RET_OK) {
2294         MMI_HILOGE("AncoAddChannel fail, error:%{public}d", ret);
2295         return ret;
2296     }
2297     ancoChannels_.emplace(consumer, tChannel);
2298     return RET_OK;
2299 #endif // OHOS_BUILD_ENABLE_ANCO
2300     MMI_HILOGI("AncoAddChannel function does not support");
2301     return ERROR_UNSUPPORT;
2302 }
2303 
AncoRemoveChannel(std::shared_ptr<IAncoConsumer> consumer)2304 int32_t InputManagerImpl::AncoRemoveChannel(std::shared_ptr<IAncoConsumer> consumer)
2305 {
2306 #ifdef OHOS_BUILD_ENABLE_ANCO
2307     std::lock_guard<std::mutex> guard(mtx_);
2308     auto iter = ancoChannels_.find(consumer);
2309     if (iter == ancoChannels_.end()) {
2310         MMI_HILOGI("Not associated with any channel");
2311         return RET_OK;
2312     }
2313     int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->AncoRemoveChannel(iter->second);
2314     if (ret != RET_OK) {
2315         MMI_HILOGE("AncoRemoveChannel fail, error:%{public}d", ret);
2316         return ret;
2317     }
2318     ancoChannels_.erase(iter);
2319     return RET_OK;
2320 #endif // OHOS_BUILD_ENABLE_ANCO
2321     MMI_HILOGI("AncoRemoveChannel function does not support");
2322     return ERROR_UNSUPPORT;
2323 }
2324 
SkipPointerLayer(bool isSkip)2325 int32_t InputManagerImpl::SkipPointerLayer(bool isSkip)
2326 {
2327     return MULTIMODAL_INPUT_CONNECT_MGR->SkipPointerLayer(isSkip);
2328 }
2329 
OnWindowStateError(int32_t pid,int32_t windowId)2330 void InputManagerImpl::OnWindowStateError(int32_t pid, int32_t windowId)
2331 {
2332     if (windowStatecallback_ != nullptr) {
2333         windowStatecallback_(pid, windowId);
2334     } else {
2335         MMI_HILOGE("windowStatecallback_ is nullptr");
2336     }
2337 }
2338 
ConvertToCapiKeyAction(int32_t keyAction)2339 int32_t InputManagerImpl::ConvertToCapiKeyAction(int32_t keyAction)
2340 {
2341     auto iter = g_keyActionMap.find(keyAction);
2342     if (iter == g_keyActionMap.end()) {
2343         MMI_HILOGE("Convert keyAction:%{public}d to capi failed", keyAction);
2344         return INVALID_KEY_ACTION;
2345     }
2346     return iter->second;
2347 }
2348 } // namespace MMI
2349 } // namespace OHOS
2350