1 /*
2  * Copyright (c) 2021-2023 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 "mmi_service.h"
17 
18 #include <parameters.h>
19 #include <sys/signalfd.h>
20 
21 #include <cinttypes>
22 #include <csignal>
23 #include "string_ex.h"
24 #ifdef OHOS_RSS_CLIENT
25 #include <unordered_map>
26 #endif
27 
28 #include "ability_manager_client.h"
29 #include "anr_manager.h"
30 #include "app_debug_listener.h"
31 #include "app_state_observer.h"
32 #include "device_event_monitor.h"
33 #include "dfx_define.h"
34 #include "dfx_dump_catcher.h"
35 #include "dfx_hisysevent.h"
36 #include "dfx_json_formatter.h"
37 
38 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
39 #include "display_event_monitor.h"
40 #endif // OHOS_BUILD_ENABLE_KEYBOARD
41 #include "event_dump.h"
42 #include "event_log_helper.h"
43 #ifdef OHOS_BUILD_ENABLE_FINGERSENSE_WRAPPER
44 #include "fingersense_wrapper.h"
45 #endif // OHOS_BUILD_ENABLE_FINGERSENSE_WRAPPER
46 #ifdef OHOS_BUILD_ENABLE_GESTURESENSE_WRAPPER
47 #include "gesturesense_wrapper.h"
48 #endif // OHOS_BUILD_ENABLE_GESTURESENSE_WRAPPER
49 #include "infrared_emitter_controller.h"
50 #include "input_device_manager.h"
51 #include "ipc_skeleton.h"
52 #include "i_input_windows_manager.h"
53 #include "i_pointer_drawing_manager.h"
54 #include "i_preference_manager.h"
55 #include "key_auto_repeat.h"
56 #include "key_command_handler.h"
57 #include "key_map_manager.h"
58 #include "mmi_log.h"
59 #include "multimodal_input_connect_def_parcel.h"
60 #include "permission_helper.h"
61 #include "timer_manager.h"
62 #include "tokenid_kit.h"
63 #include "touch_event_normalize.h"
64 #include "util.h"
65 #include "util_ex.h"
66 #include "watchdog_task.h"
67 #include "xcollie/watchdog.h"
68 #include "xcollie/xcollie.h"
69 #include "xcollie/xcollie_define.h"
70 
71 #ifdef OHOS_RSS_CLIENT
72 #include "res_sched_client.h"
73 #include "res_type.h"
74 #include "system_ability_definition.h"
75 #endif // OHOS_RSS_CLIENT
76 #include "setting_datashare.h"
77 #ifdef OHOS_BUILD_ENABLE_ANCO
78 #include "app_mgr_client.h"
79 #include "running_process_info.h"
80 #endif // OHOS_BUILD_ENABLE_ANCO
81 
82 #undef MMI_LOG_TAG
83 #define MMI_LOG_TAG "MMIService"
84 #undef MMI_LOG_DOMAIN
85 #define MMI_LOG_DOMAIN MMI_LOG_SERVER
86 
87 namespace OHOS {
88 namespace MMI {
89 namespace {
90 std::mutex g_instanceMutex;
91 MMIService* g_MMIService;
92 const std::string DEF_INPUT_SEAT { "seat0" };
93 const std::string THREAD_NAME { "mmi-service" };
94 const std::string SHELL_ASSISTANT { "com.shell_assistant" };
95 constexpr int32_t WATCHDOG_INTERVAL_TIME { 30000 };
96 constexpr int32_t WATCHDOG_DELAY_TIME { 40000 };
97 constexpr int32_t RELOAD_DEVICE_TIME { 2000 };
98 constexpr int32_t REMOVE_OBSERVER { -2 };
99 constexpr int32_t REPEAT_COUNT { 2 };
100 constexpr int32_t UNSUBSCRIBED { -1 };
101 constexpr int32_t UNOBSERVED { -1 };
102 constexpr int32_t SUBSCRIBED { 1 };
103 constexpr int32_t DISTRIBUTE_TIME { 1000 }; // 1000ms
104 constexpr int32_t COMMON_PARAMETER_ERROR { 401 };
105 constexpr size_t MAX_FRAME_NUMS { 100 };
106 constexpr int32_t THREAD_BLOCK_TIMER_SPAN_S { 3 };
107 const std::set<int32_t> g_keyCodeValueSet = {
108     KeyEvent::KEYCODE_FN, KeyEvent::KEYCODE_DPAD_UP, KeyEvent::KEYCODE_DPAD_DOWN, KeyEvent::KEYCODE_DPAD_LEFT,
109     KeyEvent::KEYCODE_DPAD_RIGHT, KeyEvent::KEYCODE_ALT_LEFT, KeyEvent::KEYCODE_ALT_RIGHT,
110     KeyEvent::KEYCODE_SHIFT_LEFT, KeyEvent::KEYCODE_SHIFT_RIGHT, KeyEvent::KEYCODE_TAB, KeyEvent::KEYCODE_ENTER,
111     KeyEvent::KEYCODE_DEL, KeyEvent::KEYCODE_MENU, KeyEvent::KEYCODE_PAGE_UP, KeyEvent::KEYCODE_PAGE_DOWN,
112     KeyEvent::KEYCODE_ESCAPE, KeyEvent::KEYCODE_FORWARD_DEL, KeyEvent::KEYCODE_CTRL_LEFT, KeyEvent::KEYCODE_CTRL_RIGHT,
113     KeyEvent::KEYCODE_CAPS_LOCK, KeyEvent::KEYCODE_SCROLL_LOCK, KeyEvent::KEYCODE_META_LEFT,
114     KeyEvent::KEYCODE_META_RIGHT, KeyEvent::KEYCODE_SYSRQ, KeyEvent::KEYCODE_BREAK, KeyEvent::KEYCODE_MOVE_HOME,
115     KeyEvent::KEYCODE_MOVE_END, KeyEvent::KEYCODE_INSERT, KeyEvent::KEYCODE_F1, KeyEvent::KEYCODE_F2,
116     KeyEvent::KEYCODE_F3, KeyEvent::KEYCODE_F4, KeyEvent::KEYCODE_F5, KeyEvent::KEYCODE_F6, KeyEvent::KEYCODE_F7,
117     KeyEvent::KEYCODE_F8, KeyEvent::KEYCODE_F9, KeyEvent::KEYCODE_F10, KeyEvent::KEYCODE_F11, KeyEvent::KEYCODE_F12,
118     KeyEvent::KEYCODE_NUM_LOCK
119 };
120 #ifdef OHOS_BUILD_ENABLE_ANCO
121 constexpr int32_t DEFAULT_USER_ID { 100 };
122 #endif // OHOS_BUILD_ENABLE_ANCO
123 } // namespace
124 
125 const bool REGISTER_RESULT = SystemAbility::MakeAndRegisterAbility(MMIService::GetInstance());
126 
CheckDefineOutput(const char * fmt,Ts...args)127 template <class... Ts> void CheckDefineOutput(const char *fmt, Ts... args)
128 {
129     CHKPV(fmt);
130     char buf[MAX_PACKET_BUF_SIZE] = {};
131     int32_t ret = snprintf_s(buf, MAX_PACKET_BUF_SIZE, MAX_PACKET_BUF_SIZE - 1, fmt, args...);
132     if (ret == -1) {
133         KMSG_LOGE("Call snprintf_s failed.ret = %d", ret);
134         return;
135     }
136     KMSG_LOGI("%s", buf);
137     MMI_HILOGI("%{public}s", buf);
138 }
139 
CheckDefine()140 static void CheckDefine()
141 {
142     CheckDefineOutput("ChkDefs:");
143 #ifdef OHOS_BUILD_ENABLE_POINTER_DRAWING
144     CheckDefineOutput("%-40s", "OHOS_BUILD_ENABLE_POINTER_DRAWING");
145 #endif
146 #ifdef OHOS_BUILD_ENABLE_INTERCEPTOR
147     CheckDefineOutput("%-40s", "OHOS_BUILD_ENABLE_INTERCEPTOR");
148 #endif
149 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
150     CheckDefineOutput("%-40s", "OHOS_BUILD_ENABLE_KEYBOARD");
151 #endif
152 #ifdef OHOS_BUILD_ENABLE_POINTER
153     CheckDefineOutput("%-40s", "OHOS_BUILD_ENABLE_POINTER");
154 #endif
155 #ifdef OHOS_BUILD_ENABLE_TOUCH
156     CheckDefineOutput("%-40s", "OHOS_BUILD_ENABLE_TOUCH");
157 #endif
158 #ifdef OHOS_BUILD_ENABLE_MONITOR
159     CheckDefineOutput("%-40s", "OHOS_BUILD_ENABLE_MONITOR");
160 #endif
161 }
162 
MMIService()163 MMIService::MMIService() : SystemAbility(MULTIMODAL_INPUT_CONNECT_SERVICE_ID, true) {}
164 
~MMIService()165 MMIService::~MMIService()
166 {
167     if (g_MMIService != nullptr) {
168         g_MMIService = nullptr;
169     }
170     MMI_HILOGI("~MMIService");
171 }
172 
GetInstance()173 MMIService* MMIService::GetInstance()
174 {
175     if (g_MMIService == nullptr) {
176         std::lock_guard<std::mutex> lock(g_instanceMutex);
177         if (g_MMIService == nullptr) {
178             MMI_HILOGI("new MMIService");
179             g_MMIService = new MMIService();
180         }
181     }
182     return g_MMIService;
183 }
184 
AddEpoll(EpollEventType type,int32_t fd)185 int32_t MMIService::AddEpoll(EpollEventType type, int32_t fd)
186 {
187     if (type < EPOLL_EVENT_BEGIN || type >= EPOLL_EVENT_END) {
188         MMI_HILOGE("Invalid param type");
189         return RET_ERR;
190     }
191     if (fd < 0) {
192         MMI_HILOGE("Invalid param fd_");
193         return RET_ERR;
194     }
195     if (mmiFd_ < 0) {
196         MMI_HILOGE("Invalid param mmiFd_");
197         return RET_ERR;
198     }
199     auto eventData = std::make_shared<mmi_epoll_event>();
200     eventData->fd = fd;
201     eventData->event_type = type;
202     MMI_HILOGI("userdata:[fd:%{public}d, type:%{public}d]", eventData->fd, eventData->event_type);
203 
204     struct epoll_event ev = {};
205     ev.events = EPOLLIN;
206     ev.data.fd = fd;
207     auto ret = EpollCtl(fd, EPOLL_CTL_ADD, ev, mmiFd_);
208     if (ret < 0) {
209         eventData = nullptr;
210         ev.data.ptr = nullptr;
211         return ret;
212     }
213     AddEpollEvent(fd, eventData);
214     return RET_OK;
215 }
216 
DelEpoll(EpollEventType type,int32_t fd)217 int32_t MMIService::DelEpoll(EpollEventType type, int32_t fd)
218 {
219     if (type < EPOLL_EVENT_BEGIN || type >= EPOLL_EVENT_END) {
220         MMI_HILOGE("Invalid param type");
221         return RET_ERR;
222     }
223     if (fd < 0) {
224         MMI_HILOGE("Invalid param fd_");
225         return RET_ERR;
226     }
227     if (mmiFd_ < 0) {
228         MMI_HILOGE("Invalid param mmiFd_");
229         return RET_ERR;
230     }
231     struct epoll_event ev = {};
232     auto ret = EpollCtl(fd, EPOLL_CTL_DEL, ev, mmiFd_);
233     if (ret < 0) {
234         MMI_HILOGE("DelEpoll failed");
235         return ret;
236     }
237     return RET_OK;
238 }
239 
IsRunning() const240 bool MMIService::IsRunning() const
241 {
242     return (state_ == ServiceRunningState::STATE_RUNNING);
243 }
244 
InitLibinputService()245 bool MMIService::InitLibinputService()
246 {
247     if (!(libinputAdapter_.Init([] (void *event, int64_t frameTime) {
248         ::OHOS::DelayedSingleton<InputEventHandler>::GetInstance()->OnEvent(event, frameTime);
249         }
250         ))) {
251         MMI_HILOGE("Libinput init, bind failed");
252         return false;
253     }
254     auto inputFds = libinputAdapter_.GetInputFds();
255     for (auto fd : inputFds) {
256         auto ret = AddEpoll(EPOLL_EVENT_INPUT, fd);
257         if (ret < 0) {
258             MMI_HILOGE("AddEpoll error ret:%{public}d", ret);
259             EpollClose();
260             return false;
261         }
262         MMI_HILOGD("AddEpoll, epollfd:%{public}d, fd:%{public}d", mmiFd_, fd);
263     }
264     return true;
265 }
266 
InitService()267 bool MMIService::InitService()
268 {
269     MMI_HILOGD("Server msg handler Init");
270     sMsgHandler_.Init(*this);
271     if (state_ != ServiceRunningState::STATE_NOT_START) {
272         MMI_HILOGE("Service running status is not enabled");
273         return false;
274     }
275     if (EpollCreate(MAX_EVENT_SIZE) < 0) {
276         MMI_HILOGE("Create epoll failed");
277         return false;
278     }
279     auto ret = AddEpoll(EPOLL_EVENT_SOCKET, epollFd_);
280     if (ret < 0) {
281         MMI_HILOGE("AddEpoll error ret:%{public}d", ret);
282         EpollClose();
283         return false;
284     }
285     state_ = ServiceRunningState::STATE_RUNNING;
286     if (!(Publish(this))) {
287         state_ = ServiceRunningState::STATE_NOT_START;
288         MMI_HILOGE("Service initialization failed");
289         EpollClose();
290         return false;
291     }
292     MMI_HILOGI("AddEpoll, epollfd:%{public}d, fd:%{public}d", mmiFd_, epollFd_);
293     return true;
294 }
295 
InitDelegateTasks()296 bool MMIService::InitDelegateTasks()
297 {
298     CALL_DEBUG_ENTER;
299     if (!delegateTasks_.Init()) {
300         MMI_HILOGE("The delegate task init failed");
301         return false;
302     }
303     auto ret = AddEpoll(EPOLL_EVENT_ETASK, delegateTasks_.GetReadFd());
304     if (ret < 0) {
305         MMI_HILOGE("AddEpoll error ret:%{public}d", ret);
306         EpollClose();
307         return false;
308     }
309     std::function<int32_t(DTaskCallback)> fun = [this](DTaskCallback cb) -> int32_t {
310         return delegateTasks_.PostSyncTask(cb);
311     };
312     delegateInterface_ = std::make_shared<DelegateInterface>(fun);
313     delegateInterface_->Init();
314     MMI_HILOGI("AddEpoll, epollfd:%{public}d, fd:%{public}d", mmiFd_, delegateTasks_.GetReadFd());
315     return true;
316 }
317 
Init()318 int32_t MMIService::Init()
319 {
320     CheckDefine();
321     MMI_HILOGD("WindowsManager Init");
322     WIN_MGR->Init(*this);
323     MMI_HILOGD("NapProcess Init");
324     NapProcess::GetInstance()->Init(*this);
325     MMI_HILOGD("ANRManager Init");
326     ANRMgr->Init(*this);
327     MMI_HILOGI("PointerDrawingManager Init");
328 #ifdef OHOS_BUILD_ENABLE_POINTER
329     if (!IPointerDrawingManager::GetInstance()->Init()) {
330         MMI_HILOGE("Pointer draw init failed");
331         return POINTER_DRAW_INIT_FAIL;
332     }
333 #endif // OHOS_BUILD_ENABLE_POINTER
334     mmiFd_ = EpollCreate(MAX_EVENT_SIZE);
335     if (mmiFd_ < 0) {
336         MMI_HILOGE("Create epoll failed");
337         return EPOLL_CREATE_FAIL;
338     }
339     MMI_HILOGD("Input msg handler init");
340     InputHandler->Init(*this);
341     MMI_HILOGD("Libinput service init");
342     if (!InitLibinputService()) {
343         MMI_HILOGE("Libinput init failed");
344         return LIBINPUT_INIT_FAIL;
345     }
346     MMI_HILOGD("Init DelegateTasks init");
347     if (!InitDelegateTasks()) {
348         MMI_HILOGE("Delegate tasks init failed");
349         return ETASKS_INIT_FAIL;
350     }
351     SetRecvFun([this] (SessionPtr sess, NetPacket& pkt) {sMsgHandler_.OnMsgHandler(sess, pkt);});
352     KeyMapMgr->GetConfigKeyValue("default_keymap", KeyMapMgr->GetDefaultKeyId());
353     OHOS::system::SetParameter(INPUT_POINTER_DEVICES, "false");
354     if (!InitService()) {
355         MMI_HILOGE("Saservice init failed");
356         return SASERVICE_INIT_FAIL;
357     }
358     MMI_HILOGI("Set para input.pointer.device false");
359     return RET_OK;
360 }
361 
OnStart()362 void MMIService::OnStart()
363 {
364     CHK_PID_AND_TID();
365     int32_t ret = Init();
366     CHKNOKRV(ret, "Init mmi_service failed");
367     MMI_HILOGD("Started successfully");
368     AddReloadDeviceTimer();
369     t_ = std::thread([this] {this->OnThread();});
370     pthread_setname_np(t_.native_handle(), THREAD_NAME.c_str());
371     auto keyHandler = InputHandler->GetKeyCommandHandler();
372     if (keyHandler != nullptr) {
373         keyHandler->PreHandleEvent();
374     }
375 #ifdef OHOS_RSS_CLIENT
376     MMI_HILOGI("Add system ability listener start");
377     AddSystemAbilityListener(RES_SCHED_SYS_ABILITY_ID);
378     MMI_HILOGI("Add system ability listener success");
379 #endif // OHOS_RSS_CLIENT
380 #if defined(OHOS_BUILD_ENABLE_FINGERSENSE_WRAPPER) && defined(OHOS_BUILD_ENABLE_KEYBOARD)
381     FINGERSENSE_WRAPPER->InitFingerSenseWrapper();
382     MMI_HILOGI("Add system ability listener start");
383     AddSystemAbilityListener(COMMON_EVENT_SERVICE_ID);
384     MMI_HILOGI("Add system ability listener success");
385     DISPLAY_MONITOR->InitCommonEventSubscriber();
386 #endif // OHOS_BUILD_ENABLE_FINGERSENSE_WRAPPER && OHOS_BUILD_ENABLE_KEYBOARD
387 #ifdef OHOS_BUILD_ENABLE_GESTURESENSE_WRAPPER
388     GESTURESENSE_WRAPPER->InitGestureSenseWrapper();
389 #endif // OHOS_BUILD_ENABLE_GESTURESENSE_WRAPPER
390     MMI_HILOGI("Add app manager service listener start");
391     AddSystemAbilityListener(APP_MGR_SERVICE_ID);
392     APP_OBSERVER_MGR->InitAppStateObserver();
393     MMI_HILOGI("Add app manager service listener end");
394     AddSystemAbilityListener(RENDER_SERVICE);
395     AddAppDebugListener();
396     AddSystemAbilityListener(DISPLAY_MANAGER_SERVICE_SA_ID);
397 #if defined(OHOS_BUILD_ENABLE_MONITOR) && defined(PLAYER_FRAMEWORK_EXISTS)
398     MMI_HILOGI("Add system ability listener start");
399     AddSystemAbilityListener(PLAYER_DISTRIBUTED_SERVICE_ID);
400     MMI_HILOGI("Add system ability listener end");
401 #endif
402 #ifdef OHOS_BUILD_ENABLE_ANCO
403     InitAncoUds();
404 #endif // OHOS_BUILD_ENABLE_ANCO
405 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
406     IPointerDrawingManager::GetInstance()->InitPointerObserver();
407 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
408     PREFERENCES_MGR->InitPreferences();
409 #if OHOS_BUILD_ENABLE_POINTER
410     bool switchFlag = false;
411     TOUCH_EVENT_HDR->GetTouchpadDoubleTapAndDragState(switchFlag);
412     TOUCH_EVENT_HDR->SetTouchpadDoubleTapAndDragState(switchFlag);
413 #endif
414     TimerMgr->AddTimer(WATCHDOG_INTERVAL_TIME, -1, [this]() {
415         MMI_HILOGD("Set thread status flag to true");
416         threadStatusFlag_ = true;
417     });
418     auto taskFunc = [this]() {
419         if (threadStatusFlag_) {
420             MMI_HILOGD("Set thread status flag to false");
421             threadStatusFlag_ = false;
422         } else {
423             MMI_HILOGE("Timeout happened");
424         }
425     };
426     HiviewDFX::Watchdog::GetInstance().RunPeriodicalTask("MMIService", taskFunc, WATCHDOG_INTERVAL_TIME,
427         WATCHDOG_DELAY_TIME);
428     MMI_HILOGI("Run periodical task success");
429 }
430 
OnStop()431 void MMIService::OnStop()
432 {
433     CHK_PID_AND_TID();
434     UdsStop();
435     libinputAdapter_.Stop();
436     state_ = ServiceRunningState::STATE_NOT_START;
437 #ifdef OHOS_RSS_CLIENT
438     MMI_HILOGI("Remove system ability listener start");
439     RemoveSystemAbilityListener(RES_SCHED_SYS_ABILITY_ID);
440     MMI_HILOGI("Remove system ability listener success");
441 #endif
442 #ifdef OHOS_BUILD_ENABLE_FINGERSENSE_WRAPPER
443     RemoveSystemAbilityListener(COMMON_EVENT_SERVICE_ID);
444 #endif // OHOS_BUILD_ENABLE_FINGERSENSE_WRAPPER
445     RemoveSystemAbilityListener(APP_MGR_SERVICE_ID);
446     RemoveSystemAbilityListener(RENDER_SERVICE);
447     RemoveAppDebugListener();
448     RemoveSystemAbilityListener(DISPLAY_MANAGER_SERVICE_SA_ID);
449 #if defined(OHOS_BUILD_ENABLE_MONITOR) && defined(PLAYER_FRAMEWORK_EXISTS)
450     RemoveSystemAbilityListener(PLAYER_DISTRIBUTED_SERVICE_ID);
451 #endif
452 #ifdef OHOS_BUILD_ENABLE_ANCO
453     StopAncoUds();
454 #endif // OHOS_BUILD_ENABLE_ANCO
455 }
456 
AddAppDebugListener()457 void MMIService::AddAppDebugListener()
458 {
459     CALL_DEBUG_ENTER;
460     appDebugListener_ = AppDebugListener::GetInstance();
461     auto errCode =
462         AAFwk::AbilityManagerClient::GetInstance()->RegisterAppDebugListener(appDebugListener_);
463     if (errCode != RET_OK) {
464         MMI_HILOGE("Call RegisterAppDebugListener failed, errCode:%{public}d", errCode);
465     }
466 }
467 
RemoveAppDebugListener()468 void MMIService::RemoveAppDebugListener()
469 {
470     CALL_DEBUG_ENTER;
471     CHKPV(appDebugListener_);
472     auto errCode =
473         AAFwk::AbilityManagerClient::GetInstance()->UnregisterAppDebugListener(appDebugListener_);
474     if (errCode != RET_OK) {
475         MMI_HILOGE("Call UnregisterAppDebugListener failed, errCode:%{public}d", errCode);
476     }
477 }
478 
AllocSocketFd(const std::string & programName,const int32_t moduleType,int32_t & toReturnClientFd,int32_t & tokenType)479 int32_t MMIService::AllocSocketFd(const std::string &programName, const int32_t moduleType, int32_t &toReturnClientFd,
480     int32_t &tokenType)
481 {
482     toReturnClientFd = IMultimodalInputConnect::INVALID_SOCKET_FD;
483     int32_t serverFd = IMultimodalInputConnect::INVALID_SOCKET_FD;
484     int32_t pid = GetCallingPid();
485     int32_t uid = GetCallingUid();
486     MMI_HILOGI("Enter, programName:%{public}s, moduleType:%{public}d, pid:%{public}d",
487         programName.c_str(), moduleType, pid);
488     int32_t ret = delegateTasks_.PostSyncTask(
489         [this, &programName, moduleType, uid, pid, &serverFd, &toReturnClientFd, &tokenType] {
490             return this->AddSocketPairInfo(programName, moduleType, uid, pid, serverFd, toReturnClientFd, tokenType);
491         }
492         );
493     DfxHisysevent::ClientConnectData data = {
494         .pid = pid,
495         .uid = uid,
496         .moduleType = moduleType,
497         .programName = programName,
498         .serverFd = serverFd
499     };
500     if (ret != RET_OK) {
501         MMI_HILOGE("Call AddSocketPairInfo failed, return:%{public}d", ret);
502         DfxHisysevent::OnClientConnect(data, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT);
503         return ret;
504     }
505     MMI_HILOGIK("Leave, programName:%{public}s, moduleType:%{public}d, alloc success", programName.c_str(),
506                 moduleType);
507     DfxHisysevent::OnClientConnect(data, OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR);
508     return RET_OK;
509 }
510 
AddInputEventFilter(sptr<IEventFilter> filter,int32_t filterId,int32_t priority,uint32_t deviceTags)511 int32_t MMIService::AddInputEventFilter(sptr<IEventFilter> filter, int32_t filterId, int32_t priority,
512     uint32_t deviceTags)
513 {
514     CALL_INFO_TRACE;
515 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH) || defined(OHOS_BUILD_ENABLE_KEYBOARD)
516     CHKPR(filter, ERROR_NULL_POINTER);
517     int32_t clientPid = GetCallingPid();
518     int32_t ret = delegateTasks_.PostSyncTask(
519         [this, filter, filterId, priority, deviceTags, clientPid] {
520             return sMsgHandler_.AddInputEventFilter(filter, filterId, priority, deviceTags, clientPid);
521         }
522         );
523     if (ret != RET_OK) {
524         MMI_HILOGE("Add event filter failed, return:%{public}d", ret);
525         return ret;
526     }
527 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH || OHOS_BUILD_ENABLE_KEYBOARD
528     return RET_OK;
529 }
530 
RemoveInputEventFilter(int32_t filterId)531 int32_t MMIService::RemoveInputEventFilter(int32_t filterId)
532 {
533     CALL_INFO_TRACE;
534 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH) || defined(OHOS_BUILD_ENABLE_KEYBOARD)
535     int32_t clientPid = GetCallingPid();
536     int32_t ret = delegateTasks_.PostSyncTask(
537         [this, filterId, clientPid] {
538             return sMsgHandler_.RemoveInputEventFilter(filterId, clientPid);
539         }
540         );
541     if (ret != RET_OK) {
542         MMI_HILOGE("Remove event filter failed, return:%{public}d", ret);
543         return ret;
544     }
545 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH || OHOS_BUILD_ENABLE_KEYBOARD
546     return RET_OK;
547 }
548 
OnConnected(SessionPtr s)549 void MMIService::OnConnected(SessionPtr s)
550 {
551     CHKPV(s);
552     MMI_HILOGI("fd:%{public}d", s->GetFd());
553 #ifdef OHOS_BUILD_ENABLE_ANCO
554     if (s->GetProgramName() != SHELL_ASSISTANT) {
555         return;
556     }
557     auto appMgrClient = DelayedSingleton<AppExecFwk::AppMgrClient>::GetInstance();
558     if (appMgrClient == nullptr) {
559         return;
560     }
561     int32_t userid = WIN_MGR->GetCurrentUserId();
562     if (userid < 0) {
563         userid = DEFAULT_USER_ID;
564     }
565     std::vector<AppExecFwk::RunningProcessInfo> info;
566     appMgrClient->GetProcessRunningInfosByUserId(info, userid);
567     for (auto &item : info) {
568         if (item.bundleNames.empty()) {
569             continue;
570         }
571         if (SHELL_ASSISTANT == item.bundleNames[0].c_str()) {
572             MMI_HILOGW("record client processes pid %{public}d", item.pid_);
573             shellAssitentPid_ = item.pid_;
574         }
575     }
576 #endif // OHOS_BUILD_ENABLE_ANCO
577 }
578 
OnDisconnected(SessionPtr s)579 void MMIService::OnDisconnected(SessionPtr s)
580 {
581     CHKPV(s);
582     MMI_HILOGW("Enter, session desc:%{public}s, fd:%{public}d", s->GetDescript().c_str(), s->GetFd());
583     auto ret = RemoveInputEventFilter(-1);
584     if (ret != RET_OK) {
585         MMI_HILOGF("Remove all filter failed, ret:%{public}d", ret);
586     }
587 #ifdef OHOS_BUILD_ENABLE_ANCO
588     if (s->GetProgramName() == SHELL_ASSISTANT && shellAssitentPid_ == s->GetPid()) {
589         MMI_HILOGW("clean all shell windows pid: %{public}d", s->GetPid());
590         shellAssitentPid_ = -1;
591         IInputWindowsManager::GetInstance()->CleanShellWindowIds();
592     }
593 #endif // OHOS_BUILD_ENABLE_ANCO
594 #ifdef OHOS_BUILD_ENABLE_POINTER
595     IPointerDrawingManager::GetInstance()->DeletePointerVisible(s->GetPid());
596 #endif // OHOS_BUILD_ENABLE_POINTER
597 }
598 
SetMouseScrollRows(int32_t rows)599 int32_t MMIService::SetMouseScrollRows(int32_t rows)
600 {
601     CALL_INFO_TRACE;
602 #if defined OHOS_BUILD_ENABLE_POINTER
603     int32_t ret = delegateTasks_.PostSyncTask(
604         [rows] {
605             return ::OHOS::DelayedSingleton<MouseEventNormalize>::GetInstance()->SetMouseScrollRows(rows);
606         }
607         );
608     if (ret != RET_OK) {
609         MMI_HILOGE("Set the number of mouse scrolling rows failed, return:%{public}d", ret);
610         return ret;
611     }
612 #endif // OHOS_BUILD_ENABLE_POINTER
613     return RET_OK;
614 }
615 
SetCustomCursor(int32_t pid,int32_t windowId,int32_t focusX,int32_t focusY,void * pixelMap)616 int32_t MMIService::SetCustomCursor(int32_t pid, int32_t windowId, int32_t focusX, int32_t focusY, void* pixelMap)
617 {
618     CALL_INFO_TRACE;
619 #if defined OHOS_BUILD_ENABLE_POINTER
620     int32_t ret = CheckPidPermission(pid);
621     if (ret != RET_OK) {
622         MMI_HILOGE("Check pid permission failed");
623         return ret;
624     }
625     ret = delegateTasks_.PostSyncTask(std::bind(
626         [pixelMap, pid, windowId, focusX, focusY] {
627             return IPointerDrawingManager::GetInstance()->SetCustomCursor(pixelMap, pid, windowId, focusX, focusY);
628         }
629         ));
630     if (ret != RET_OK) {
631         MMI_HILOGE("Set the custom cursor failed, ret:%{public}d", ret);
632         return ret;
633     }
634 #endif // OHOS_BUILD_ENABLE_POINTER
635     return RET_OK;
636 }
637 
SetMouseIcon(int32_t windowId,void * pixelMap)638 int32_t MMIService::SetMouseIcon(int32_t windowId, void* pixelMap)
639 {
640     CALL_INFO_TRACE;
641 #if defined OHOS_BUILD_ENABLE_POINTER
642     int32_t pid = GetCallingPid();
643     int32_t ret = CheckPidPermission(pid);
644     if (ret != RET_OK) {
645         MMI_HILOGE("Check pid permission failed");
646         return ret;
647     }
648     ret = delegateTasks_.PostSyncTask(std::bind(
649         [pid, windowId, pixelMap] {
650             return IPointerDrawingManager::GetInstance()->SetMouseIcon(pid, windowId, pixelMap);
651         }
652         ));
653     if (ret != RET_OK) {
654         MMI_HILOGE("Set the mouse icon failed, return:%{public}d", ret);
655         return ret;
656     }
657 #endif // OHOS_BUILD_ENABLE_POINTER
658     return RET_OK;
659 }
660 
SetMouseHotSpot(int32_t pid,int32_t windowId,int32_t hotSpotX,int32_t hotSpotY)661 int32_t MMIService::SetMouseHotSpot(int32_t pid, int32_t windowId, int32_t hotSpotX, int32_t hotSpotY)
662 {
663     CALL_INFO_TRACE;
664 #if defined OHOS_BUILD_ENABLE_POINTER
665     int32_t ret = CheckPidPermission(pid);
666     if (ret != RET_OK) {
667         MMI_HILOGE("Check pid permission failed");
668         return ret;
669     }
670     ret = delegateTasks_.PostSyncTask(
671         [pid, windowId, hotSpotX, hotSpotY] {
672             return IPointerDrawingManager::GetInstance()->SetMouseHotSpot(pid, windowId, hotSpotX, hotSpotY);
673         }
674         );
675     if (ret != RET_OK) {
676         MMI_HILOGE("Set the mouse hot spot failed, return:%{public}d", ret);
677         return ret;
678     }
679 #endif // OHOS_BUILD_ENABLE_POINTER
680     return RET_OK;
681 }
682 
SetNapStatus(int32_t pid,int32_t uid,std::string bundleName,int32_t napStatus)683 int32_t MMIService::SetNapStatus(int32_t pid, int32_t uid, std::string bundleName, int32_t napStatus)
684 {
685     CALL_INFO_TRACE;
686     int32_t ret = CheckPidPermission(pid);
687     if (ret != RET_OK) {
688         MMI_HILOGE("Check pid permission failed");
689         return ret;
690     }
691     NapProcess::GetInstance()->SetNapStatus(pid, uid, bundleName, napStatus);
692     return RET_OK;
693 }
694 
695 #ifdef OHOS_BUILD_ENABLE_POINTER
ReadMouseScrollRows(int32_t & rows)696 int32_t MMIService::ReadMouseScrollRows(int32_t &rows)
697 {
698     rows = MouseEventHdr->GetMouseScrollRows();
699     return RET_OK;
700 }
701 #endif // OHOS_BUILD_ENABLE_POINTER
702 
GetMouseScrollRows(int32_t & rows)703 int32_t MMIService::GetMouseScrollRows(int32_t &rows)
704 {
705     CALL_INFO_TRACE;
706 #ifdef OHOS_BUILD_ENABLE_POINTER
707     int32_t ret = delegateTasks_.PostSyncTask(
708         [this, &rows] {
709             return this->ReadMouseScrollRows(rows);
710         }
711         );
712     if (ret != RET_OK) {
713         MMI_HILOGE("Get the number of mouse scrolling rows failed, ret:%{public}d", ret);
714         return ret;
715     }
716 #endif // OHOS_BUILD_ENABLE_POINTER
717     return RET_OK;
718 }
719 
SetPointerSize(int32_t size)720 int32_t MMIService::SetPointerSize(int32_t size)
721 {
722     CALL_INFO_TRACE;
723 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
724     int32_t ret = delegateTasks_.PostSyncTask(
725         [size] {
726             return IPointerDrawingManager::GetInstance()->SetPointerSize(size);
727         }
728         );
729     if (ret != RET_OK) {
730         MMI_HILOGE("Set pointer size failed, return:%{public}d", ret);
731         return ret;
732     }
733 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
734     return RET_OK;
735 }
736 
737 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
ReadPointerSize(int32_t & size)738 int32_t MMIService::ReadPointerSize(int32_t &size)
739 {
740     size = IPointerDrawingManager::GetInstance()->GetPointerSize();
741     return RET_OK;
742 }
743 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
744 
GetPointerSize(int32_t & size)745 int32_t MMIService::GetPointerSize(int32_t &size)
746 {
747     CALL_INFO_TRACE;
748 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
749     int32_t ret = delegateTasks_.PostSyncTask(
750         [this, &size] {
751             return this->ReadPointerSize(size);
752         }
753         );
754     if (ret != RET_OK) {
755         MMI_HILOGE("Get pointer size failed, return:%{public}d", ret);
756         return ret;
757     }
758 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
759     return RET_OK;
760 }
761 
SetMousePrimaryButton(int32_t primaryButton)762 int32_t MMIService::SetMousePrimaryButton(int32_t primaryButton)
763 {
764     CALL_INFO_TRACE;
765 #if defined OHOS_BUILD_ENABLE_POINTER
766     int32_t ret = delegateTasks_.PostSyncTask(
767         [primaryButton] {
768             return ::OHOS::DelayedSingleton<MouseEventNormalize>::GetInstance()->SetMousePrimaryButton(primaryButton);
769         }
770         );
771     if (ret != RET_OK) {
772         MMI_HILOGE("Set mouse primary button failed, return:%{public}d", ret);
773         return ret;
774     }
775 #endif // OHOS_BUILD_ENABLE_POINTER
776     return RET_OK;
777 }
778 
779 #ifdef OHOS_BUILD_ENABLE_POINTER
ReadMousePrimaryButton(int32_t & primaryButton)780 int32_t MMIService::ReadMousePrimaryButton(int32_t &primaryButton)
781 {
782     primaryButton = MouseEventHdr->GetMousePrimaryButton();
783     return RET_OK;
784 }
785 #endif // OHOS_BUILD_ENABLE_POINTER
786 
GetMousePrimaryButton(int32_t & primaryButton)787 int32_t MMIService::GetMousePrimaryButton(int32_t &primaryButton)
788 {
789     CALL_INFO_TRACE;
790 #ifdef OHOS_BUILD_ENABLE_POINTER
791     int32_t ret = delegateTasks_.PostSyncTask(
792         [this, &primaryButton] {
793             return this->ReadMousePrimaryButton(primaryButton);
794         }
795         );
796     if (ret != RET_OK) {
797         MMI_HILOGE("Get mouse primary button failed, return:%{public}d", ret);
798         return ret;
799     }
800 #endif // OHOS_BUILD_ENABLE_POINTER
801     return RET_OK;
802 }
803 
SetPointerVisible(bool visible,int32_t priority)804 int32_t MMIService::SetPointerVisible(bool visible, int32_t priority)
805 {
806     CALL_INFO_TRACE;
807 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
808     auto tokenId = IPCSkeleton::GetCallingTokenID();
809     auto tokenType = OHOS::Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(tokenId);
810     bool isHap = false;
811     if (tokenType == OHOS::Security::AccessToken::TOKEN_HAP) {
812         isHap = true;
813     }
814     int32_t clientPid = GetCallingPid();
815     int32_t ret = delegateTasks_.PostSyncTask(
816         [clientPid, visible, priority, isHap] {
817             return IPointerDrawingManager::GetInstance()->SetPointerVisible(clientPid, visible, priority, isHap);
818         }
819         );
820     if (ret != RET_OK) {
821         MMI_HILOGE("Set pointer visible failed, return:%{public}d", ret);
822         return ret;
823     }
824 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
825     return RET_OK;
826 }
827 
828 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
CheckPointerVisible(bool & visible)829 int32_t MMIService::CheckPointerVisible(bool &visible)
830 {
831     visible = IPointerDrawingManager::GetInstance()->IsPointerVisible();
832     return RET_OK;
833 }
834 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
835 
IsPointerVisible(bool & visible)836 int32_t MMIService::IsPointerVisible(bool &visible)
837 {
838     CALL_DEBUG_ENTER;
839 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
840     int32_t ret = delegateTasks_.PostSyncTask(
841         [this, &visible] {
842             return this->CheckPointerVisible(visible);
843         }
844         );
845     if (ret != RET_OK) {
846         MMI_HILOGE("Is pointer visible failed, return:%{public}d", ret);
847         return ret;
848     }
849 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
850     return RET_OK;
851 }
852 
MarkProcessed(int32_t eventType,int32_t eventId)853 int32_t MMIService::MarkProcessed(int32_t eventType, int32_t eventId)
854 {
855     CALL_DEBUG_ENTER;
856     CHKPR(ANRMgr, RET_ERR);
857     int32_t clientPid = GetCallingPid();
858     int32_t ret = delegateTasks_.PostSyncTask(
859         [clientPid, eventType, eventId] {
860             return ::OHOS::DelayedSingleton<ANRManager>::GetInstance()->MarkProcessed(clientPid, eventType, eventId);
861         }
862         );
863     if (ret != RET_OK) {
864         MMI_HILOGD("Mark event processed failed, ret:%{public}d", ret);
865         return ret;
866     }
867     return RET_OK;
868 }
869 
SetPointerColor(int32_t color)870 int32_t MMIService::SetPointerColor(int32_t color)
871 {
872     CALL_INFO_TRACE;
873 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
874     int32_t ret = delegateTasks_.PostSyncTask(
875         [color] {
876             return IPointerDrawingManager::GetInstance()->SetPointerColor(color);
877         }
878         );
879     if (ret != RET_OK) {
880         MMI_HILOGE("Set pointer color failed, return:%{public}d", ret);
881         return ret;
882     }
883 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
884     return RET_OK;
885 }
886 
887 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
ReadPointerColor(int32_t & color)888 int32_t MMIService::ReadPointerColor(int32_t &color)
889 {
890     color = IPointerDrawingManager::GetInstance()->GetPointerColor();
891     return RET_OK;
892 }
893 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
894 
GetPointerColor(int32_t & color)895 int32_t MMIService::GetPointerColor(int32_t &color)
896 {
897     CALL_INFO_TRACE;
898 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
899     int32_t ret = delegateTasks_.PostSyncTask(
900         [this, &color] {
901             return this->ReadPointerColor(color);
902         }
903         );
904     if (ret != RET_OK) {
905         MMI_HILOGE("Get pointer color failed, return:%{public}d", ret);
906         return ret;
907     }
908 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
909     return RET_OK;
910 }
911 
SetPointerSpeed(int32_t speed)912 int32_t MMIService::SetPointerSpeed(int32_t speed)
913 {
914     CALL_INFO_TRACE;
915 #ifdef OHOS_BUILD_ENABLE_POINTER
916     int32_t ret = delegateTasks_.PostSyncTask(
917         [speed] {
918             return ::OHOS::DelayedSingleton<MouseEventNormalize>::GetInstance()->SetPointerSpeed(speed);
919         }
920         );
921     if (ret != RET_OK) {
922         MMI_HILOGE("Set pointer speed failed, return:%{public}d", ret);
923         return ret;
924     }
925 #endif // OHOS_BUILD_ENABLE_POINTER
926     return RET_OK;
927 }
928 
929 #ifdef OHOS_BUILD_ENABLE_POINTER
ReadPointerSpeed(int32_t & speed)930 int32_t MMIService::ReadPointerSpeed(int32_t &speed)
931 {
932     speed = MouseEventHdr->GetPointerSpeed();
933     return RET_OK;
934 }
935 #endif // OHOS_BUILD_ENABLE_POINTER
936 
GetPointerSpeed(int32_t & speed)937 int32_t MMIService::GetPointerSpeed(int32_t &speed)
938 {
939     CALL_INFO_TRACE;
940 #ifdef OHOS_BUILD_ENABLE_POINTER
941     int32_t ret = delegateTasks_.PostSyncTask(
942         [this, &speed] {
943             return this->ReadPointerSpeed(speed);
944         }
945         );
946     if (ret != RET_OK) {
947         MMI_HILOGE("Get pointer speed failed, return:%{public}d", ret);
948         return ret;
949     }
950 #endif // OHOS_BUILD_ENABLE_POINTER
951     return RET_OK;
952 }
953 
NotifyNapOnline()954 int32_t MMIService::NotifyNapOnline()
955 {
956     CALL_DEBUG_ENTER;
957     NapProcess::GetInstance()->NotifyNapOnline();
958     return RET_OK;
959 }
960 
RemoveInputEventObserver()961 int32_t MMIService::RemoveInputEventObserver()
962 {
963     CALL_DEBUG_ENTER;
964     NapProcess::GetInstance()->RemoveInputEventObserver();
965     return RET_OK;
966 }
967 
SetPointerStyle(int32_t windowId,PointerStyle pointerStyle,bool isUiExtension)968 int32_t MMIService::SetPointerStyle(int32_t windowId, PointerStyle pointerStyle, bool isUiExtension)
969 {
970     CALL_INFO_TRACE;
971 #ifdef OHOS_BUILD_ENABLE_POINTER
972     int32_t clientPid = GetCallingPid();
973     int32_t ret = delegateTasks_.PostSyncTask(
974         [clientPid, windowId, pointerStyle, isUiExtension] {
975             return IPointerDrawingManager::GetInstance()->SetPointerStyle(
976                 clientPid, windowId, pointerStyle, isUiExtension);
977         }
978         );
979     if (ret != RET_OK) {
980         MMI_HILOGE("Set pointer style failed, return:%{public}d", ret);
981         return ret;
982     }
983 #endif // OHOS_BUILD_ENABLE_POINTER
984     return RET_OK;
985 }
986 
ClearWindowPointerStyle(int32_t pid,int32_t windowId)987 int32_t MMIService::ClearWindowPointerStyle(int32_t pid, int32_t windowId)
988 {
989     CALL_DEBUG_ENTER;
990 #ifdef OHOS_BUILD_ENABLE_POINTER
991     int32_t ret = CheckPidPermission(pid);
992     if (ret != RET_OK) {
993         MMI_HILOGE("Check pid permission failed");
994         return ret;
995     }
996     ret = delegateTasks_.PostSyncTask(
997         [pid, windowId] {
998             return IPointerDrawingManager::GetInstance()->ClearWindowPointerStyle(pid, windowId);
999         }
1000         );
1001     if (ret != RET_OK) {
1002         MMI_HILOGE("Set pointer style failed, return:%{public}d", ret);
1003         return ret;
1004     }
1005 #endif // OHOS_BUILD_ENABLE_POINTER
1006     return RET_OK;
1007 }
1008 
GetPointerStyle(int32_t windowId,PointerStyle & pointerStyle,bool isUiExtension)1009 int32_t MMIService::GetPointerStyle(int32_t windowId, PointerStyle &pointerStyle, bool isUiExtension)
1010 {
1011     CALL_DEBUG_ENTER;
1012 #ifdef OHOS_BUILD_ENABLE_POINTER
1013     int32_t clientPid = GetCallingPid();
1014     int32_t ret = delegateTasks_.PostSyncTask(
1015         [clientPid, windowId, &pointerStyle, isUiExtension] {
1016             return IPointerDrawingManager::GetInstance()->GetPointerStyle(
1017                 clientPid, windowId, pointerStyle, isUiExtension);
1018         }
1019         );
1020     if (ret != RET_OK) {
1021         MMI_HILOGE("Get pointer style failed, return:%{public}d", ret);
1022         return ret;
1023     }
1024 #endif // OHOS_BUILD_ENABLE_POINTER
1025     return RET_OK;
1026 }
1027 
SetHoverScrollState(bool state)1028 int32_t MMIService::SetHoverScrollState(bool state)
1029 {
1030     CALL_INFO_TRACE;
1031 #if defined OHOS_BUILD_ENABLE_POINTER
1032     int32_t ret = delegateTasks_.PostSyncTask(
1033         [state] {
1034             return ::OHOS::MMI::IInputWindowsManager::GetInstance()->SetHoverScrollState(state);
1035         }
1036         );
1037     if (ret != RET_OK) {
1038         MMI_HILOGE("Set mouse hover scroll state failed, return:%{public}d", ret);
1039         return ret;
1040     }
1041 #endif // OHOS_BUILD_ENABLE_POINTER
1042     return RET_OK;
1043 }
1044 
1045 #ifdef OHOS_BUILD_ENABLE_POINTER
ReadHoverScrollState(bool & state)1046 int32_t MMIService::ReadHoverScrollState(bool &state)
1047 {
1048     state = WIN_MGR->GetHoverScrollState();
1049     return RET_OK;
1050 }
1051 #endif // OHOS_BUILD_ENABLE_POINTER
1052 
GetHoverScrollState(bool & state)1053 int32_t MMIService::GetHoverScrollState(bool &state)
1054 {
1055     CALL_INFO_TRACE;
1056 #ifdef OHOS_BUILD_ENABLE_POINTER
1057     int32_t ret = delegateTasks_.PostSyncTask(
1058         [this, &state] {
1059             return this->ReadHoverScrollState(state);
1060         }
1061         );
1062     if (ret != RET_OK) {
1063         MMI_HILOGE("Get mouse hover scroll state, return:%{public}d", ret);
1064         return ret;
1065     }
1066 #endif // OHOS_BUILD_ENABLE_POINTER
1067     return RET_OK;
1068 }
1069 
OnSupportKeys(int32_t deviceId,std::vector<int32_t> & keys,std::vector<bool> & keystroke)1070 int32_t MMIService::OnSupportKeys(int32_t deviceId, std::vector<int32_t> &keys, std::vector<bool> &keystroke)
1071 {
1072     CALL_DEBUG_ENTER;
1073     int32_t ret = INPUT_DEV_MGR->SupportKeys(deviceId, keys, keystroke);
1074     if (ret != RET_OK) {
1075         MMI_HILOGE("Device id not support");
1076         return ret;
1077     }
1078     if (keystroke.size() > MAX_SUPPORT_KEY) {
1079         MMI_HILOGE("Device exceeds the max range");
1080         return RET_ERR;
1081     }
1082     return RET_OK;
1083 }
1084 
SupportKeys(int32_t deviceId,std::vector<int32_t> & keys,std::vector<bool> & keystroke)1085 int32_t MMIService::SupportKeys(int32_t deviceId, std::vector<int32_t> &keys, std::vector<bool> &keystroke)
1086 {
1087     CALL_DEBUG_ENTER;
1088     int32_t ret = delegateTasks_.PostSyncTask(
1089         [this, deviceId, &keys, &keystroke] {
1090             return this->OnSupportKeys(deviceId, keys, keystroke);
1091         }
1092         );
1093     if (ret != RET_OK) {
1094         MMI_HILOGE("Support keys info process failed, ret:%{public}d", ret);
1095         return ret;
1096     }
1097     return RET_OK;
1098 }
1099 
OnGetDeviceIds(std::vector<int32_t> & ids)1100 int32_t MMIService::OnGetDeviceIds(std::vector<int32_t> &ids)
1101 {
1102     CALL_DEBUG_ENTER;
1103     ids = INPUT_DEV_MGR->GetInputDeviceIds();
1104     return RET_OK;
1105 }
1106 
GetDeviceIds(std::vector<int32_t> & ids)1107 int32_t MMIService::GetDeviceIds(std::vector<int32_t> &ids)
1108 {
1109     CALL_DEBUG_ENTER;
1110     int32_t ret = delegateTasks_.PostSyncTask(
1111         [this, &ids] {
1112             return this->OnGetDeviceIds(ids);
1113         }
1114         );
1115     if (ret != RET_OK) {
1116         MMI_HILOGE("Get deviceids failed, ret:%{public}d", ret);
1117         return ret;
1118     }
1119     return RET_OK;
1120 }
1121 
OnGetDevice(int32_t deviceId,std::shared_ptr<InputDevice> inputDevice)1122 int32_t MMIService::OnGetDevice(int32_t deviceId, std::shared_ptr<InputDevice> inputDevice)
1123 {
1124     CALL_DEBUG_ENTER;
1125     if (INPUT_DEV_MGR->GetInputDevice(deviceId) == nullptr) {
1126         MMI_HILOGE("Input device not found");
1127         return COMMON_PARAMETER_ERROR;
1128     }
1129     auto tmpDevice = INPUT_DEV_MGR->GetInputDevice(deviceId);
1130     inputDevice->SetId(tmpDevice->GetId());
1131     inputDevice->SetType(tmpDevice->GetType());
1132     inputDevice->SetName(tmpDevice->GetName());
1133     inputDevice->SetBus(tmpDevice->GetBus());
1134     inputDevice->SetVersion(tmpDevice->GetVersion());
1135     inputDevice->SetProduct(tmpDevice->GetProduct());
1136     inputDevice->SetVendor(tmpDevice->GetVendor());
1137     inputDevice->SetPhys(tmpDevice->GetPhys());
1138     inputDevice->SetUniq(tmpDevice->GetUniq());
1139     inputDevice->SetCapabilities(tmpDevice->GetCapabilities());
1140     inputDevice->SetAxisInfo(tmpDevice->GetAxisInfo());
1141 
1142     return RET_OK;
1143 }
1144 
GetDevice(int32_t deviceId,std::shared_ptr<InputDevice> & inputDevice)1145 int32_t MMIService::GetDevice(int32_t deviceId, std::shared_ptr<InputDevice> &inputDevice)
1146 {
1147     CALL_DEBUG_ENTER;
1148     int32_t ret = delegateTasks_.PostSyncTask(
1149         [this, deviceId, inputDevice] {
1150             return this->OnGetDevice(deviceId, inputDevice);
1151         }
1152         );
1153     if (ret != RET_OK) {
1154         MMI_HILOGE("Get input device info failed, ret:%{public}d", ret);
1155         return ret;
1156     }
1157     return RET_OK;
1158 }
1159 
OnRegisterDevListener(int32_t pid)1160 int32_t MMIService::OnRegisterDevListener(int32_t pid)
1161 {
1162     auto sess = GetSession(GetClientFd(pid));
1163     CHKPR(sess, RET_ERR);
1164     INPUT_DEV_MGR->AddDevListener(sess);
1165     return RET_OK;
1166 }
1167 
RegisterDevListener()1168 int32_t MMIService::RegisterDevListener()
1169 {
1170     CALL_DEBUG_ENTER;
1171     int32_t pid = GetCallingPid();
1172     int32_t ret = delegateTasks_.PostSyncTask(
1173         [this, pid] {
1174             return this->OnRegisterDevListener(pid);
1175         }
1176         );
1177     if (ret != RET_OK) {
1178         MMI_HILOGE("Register device listener failed, ret:%{public}d", ret);
1179         return ret;
1180     }
1181     return RET_OK;
1182 }
1183 
OnUnregisterDevListener(int32_t pid)1184 int32_t MMIService::OnUnregisterDevListener(int32_t pid)
1185 {
1186     auto sess = GetSession(GetClientFd(pid));
1187     INPUT_DEV_MGR->RemoveDevListener(sess);
1188     return RET_OK;
1189 }
1190 
UnregisterDevListener()1191 int32_t MMIService::UnregisterDevListener()
1192 {
1193     CALL_DEBUG_ENTER;
1194     int32_t pid = GetCallingPid();
1195     int32_t ret = delegateTasks_.PostSyncTask(
1196         [this, pid] {
1197             return this->OnUnregisterDevListener(pid);
1198         }
1199         );
1200     if (ret != RET_OK) {
1201         MMI_HILOGE("Unregister device listener failed failed, ret:%{public}d", ret);
1202         return ret;
1203     }
1204     return RET_OK;
1205 }
1206 
OnGetKeyboardType(int32_t deviceId,int32_t & keyboardType)1207 int32_t MMIService::OnGetKeyboardType(int32_t deviceId, int32_t &keyboardType)
1208 {
1209     CALL_DEBUG_ENTER;
1210     int32_t ret = INPUT_DEV_MGR->GetKeyboardType(deviceId, keyboardType);
1211     if (ret != RET_OK) {
1212         MMI_HILOGD("GetKeyboardType call failed");
1213         return ret;
1214     }
1215     return RET_OK;
1216 }
1217 
GetKeyboardType(int32_t deviceId,int32_t & keyboardType)1218 int32_t MMIService::GetKeyboardType(int32_t deviceId, int32_t &keyboardType)
1219 {
1220     CALL_DEBUG_ENTER;
1221     int32_t ret = delegateTasks_.PostSyncTask(
1222         [this, deviceId, &keyboardType] {
1223             return this->OnGetKeyboardType(deviceId, keyboardType);
1224         }
1225         );
1226     if (ret != RET_OK) {
1227         MMI_HILOGD("Get keyboard type failed, ret:%{public}d", ret);
1228         return ret;
1229     }
1230     return ret;
1231 }
1232 
SetKeyboardRepeatDelay(int32_t delay)1233 int32_t MMIService::SetKeyboardRepeatDelay(int32_t delay)
1234 {
1235     CALL_INFO_TRACE;
1236 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
1237     int32_t ret = delegateTasks_.PostSyncTask(
1238         [delay] {
1239             return ::OHOS::DelayedSingleton<KeyAutoRepeat>::GetInstance()->SetKeyboardRepeatDelay(delay);
1240         }
1241         );
1242     if (ret != RET_OK) {
1243         MMI_HILOGE("Set keyboard repeat delay failed, ret:%{public}d", ret);
1244         return ret;
1245     }
1246 #endif  // OHOS_BUILD_ENABLE_KEYBOARD
1247     return RET_OK;
1248 }
1249 
SetKeyboardRepeatRate(int32_t rate)1250 int32_t MMIService::SetKeyboardRepeatRate(int32_t rate)
1251 {
1252     CALL_INFO_TRACE;
1253 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
1254     int32_t ret = delegateTasks_.PostSyncTask(
1255         [rate] {
1256             return ::OHOS::DelayedSingleton<KeyAutoRepeat>::GetInstance()->SetKeyboardRepeatRate(rate);
1257         }
1258         );
1259     if (ret != RET_OK) {
1260         MMI_HILOGE("Set keyboard repeat rate failed, ret:%{public}d", ret);
1261         return ret;
1262     }
1263 #endif  // OHOS_BUILD_ENABLE_KEYBOARD
1264     return RET_OK;
1265 }
1266 
GetKeyboardRepeatDelay(int32_t & delay)1267 int32_t MMIService::GetKeyboardRepeatDelay(int32_t &delay)
1268 {
1269     CALL_INFO_TRACE;
1270 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
1271     int32_t ret = delegateTasks_.PostSyncTask(
1272         [&delay] {
1273             return ::OHOS::DelayedSingleton<KeyAutoRepeat>::GetInstance()->GetKeyboardRepeatDelay(delay);
1274         }
1275         );
1276     if (ret != RET_OK) {
1277         MMI_HILOGE("Get keyboard repeat delay failed, ret:%{public}d", ret);
1278         return ret;
1279     }
1280 #endif  // OHOS_BUILD_ENABLE_KEYBOARD
1281     return RET_OK;
1282 }
1283 
GetKeyboardRepeatRate(int32_t & rate)1284 int32_t MMIService::GetKeyboardRepeatRate(int32_t &rate)
1285 {
1286     CALL_INFO_TRACE;
1287 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
1288     int32_t ret = delegateTasks_.PostSyncTask(
1289         [&rate] {
1290             return ::OHOS::DelayedSingleton<KeyAutoRepeat>::GetInstance()->GetKeyboardRepeatRate(rate);
1291         }
1292         );
1293     if (ret != RET_OK) {
1294         MMI_HILOGE("Get keyboard repeat rate failed, ret:%{public}d", ret);
1295         return ret;
1296     }
1297 #endif  // OHOS_BUILD_ENABLE_KEYBOARD
1298     return RET_OK;
1299 }
1300 
1301 #if defined(OHOS_BUILD_ENABLE_INTERCEPTOR) || defined(OHOS_BUILD_ENABLE_MONITOR)
CheckAddInput(int32_t pid,InputHandlerType handlerType,HandleEventType eventType,int32_t priority,uint32_t deviceTags)1302 int32_t MMIService::CheckAddInput(int32_t pid, InputHandlerType handlerType, HandleEventType eventType,
1303     int32_t priority, uint32_t deviceTags)
1304 {
1305     auto sess = GetSessionByPid(pid);
1306     CHKPR(sess, ERROR_NULL_POINTER);
1307     return sMsgHandler_.OnAddInputHandler(sess, handlerType, eventType, priority, deviceTags);
1308 }
1309 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR || OHOS_BUILD_ENABLE_MONITOR
1310 
AddInputHandler(InputHandlerType handlerType,HandleEventType eventType,int32_t priority,uint32_t deviceTags)1311 int32_t MMIService::AddInputHandler(InputHandlerType handlerType, HandleEventType eventType, int32_t priority,
1312     uint32_t deviceTags)
1313 {
1314     CALL_INFO_TRACE;
1315 #if defined(OHOS_BUILD_ENABLE_INTERCEPTOR) || defined(OHOS_BUILD_ENABLE_MONITOR)
1316     int32_t pid = GetCallingPid();
1317     int32_t ret = delegateTasks_.PostSyncTask(
1318         [this, pid, handlerType, eventType, priority, deviceTags] {
1319             return this->CheckAddInput(pid, handlerType, eventType, priority, deviceTags);
1320         }
1321         );
1322     if (ret != RET_OK) {
1323         MMI_HILOGE("Add input handler failed, ret:%{public}d", ret);
1324         return ret;
1325     }
1326     if (NapProcess::GetInstance()->GetNapClientPid() != REMOVE_OBSERVER) {
1327         OHOS::MMI::NapProcess::NapStatusData napData;
1328         napData.pid = GetCallingPid();
1329         napData.uid = GetCallingUid();
1330         auto sess = GetSessionByPid(pid);
1331         CHKPR(sess, ERROR_NULL_POINTER);
1332         napData.bundleName = sess->GetProgramName();
1333         int32_t syncState = SUBSCRIBED;
1334         MMI_HILOGD("AddInputHandler info to observer : pid:%{public}d, uid:%d, bundleName:%{public}s",
1335             napData.pid, napData.uid, napData.bundleName.c_str());
1336         NapProcess::GetInstance()->AddMmiSubscribedEventData(napData, syncState);
1337         if (NapProcess::GetInstance()->GetNapClientPid() != UNOBSERVED) {
1338             NapProcess::GetInstance()->NotifyBundleName(napData, syncState);
1339         }
1340     }
1341 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR || OHOS_BUILD_ENABLE_MONITOR
1342     return RET_OK;
1343 }
1344 
1345 #if defined(OHOS_BUILD_ENABLE_INTERCEPTOR) || defined(OHOS_BUILD_ENABLE_MONITOR)
CheckRemoveInput(int32_t pid,InputHandlerType handlerType,HandleEventType eventType,int32_t priority,uint32_t deviceTags)1346 int32_t MMIService::CheckRemoveInput(int32_t pid, InputHandlerType handlerType, HandleEventType eventType,
1347     int32_t priority, uint32_t deviceTags)
1348 {
1349     auto sess = GetSessionByPid(pid);
1350     CHKPR(sess, ERROR_NULL_POINTER);
1351     return sMsgHandler_.OnRemoveInputHandler(sess, handlerType, eventType, priority, deviceTags);
1352 }
1353 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR || OHOS_BUILD_ENABLE_MONITOR
1354 
RemoveInputHandler(InputHandlerType handlerType,HandleEventType eventType,int32_t priority,uint32_t deviceTags)1355 int32_t MMIService::RemoveInputHandler(InputHandlerType handlerType, HandleEventType eventType, int32_t priority,
1356     uint32_t deviceTags)
1357 {
1358     CALL_INFO_TRACE;
1359 #if defined(OHOS_BUILD_ENABLE_INTERCEPTOR) || defined(OHOS_BUILD_ENABLE_MONITOR)
1360     int32_t pid = GetCallingPid();
1361     int32_t ret = delegateTasks_.PostSyncTask(
1362         [this, pid, handlerType, eventType, priority, deviceTags] {
1363             return this->CheckRemoveInput(pid, handlerType, eventType, priority, deviceTags);
1364         }
1365         );
1366     if (ret != RET_OK) {
1367         MMI_HILOGE("Remove input handler failed, ret:%{public}d", ret);
1368         return ret;
1369     }
1370     if (NapProcess::GetInstance()->GetNapClientPid() != REMOVE_OBSERVER) {
1371         OHOS::MMI::NapProcess::NapStatusData napData;
1372         napData.pid = GetCallingPid();
1373         napData.uid = GetCallingUid();
1374         auto sess = GetSessionByPid(pid);
1375         CHKPR(sess, ERROR_NULL_POINTER);
1376         napData.bundleName = sess->GetProgramName();
1377         int32_t syncState = UNSUBSCRIBED;
1378         MMI_HILOGD("RemoveInputHandler info to observer : pid:%{public}d, uid:%{public}d, bundleName:%{public}s",
1379             napData.pid, napData.uid, napData.bundleName.c_str());
1380         NapProcess::GetInstance()->AddMmiSubscribedEventData(napData, syncState);
1381         if (NapProcess::GetInstance()->GetNapClientPid() != UNOBSERVED) {
1382             NapProcess::GetInstance()->NotifyBundleName(napData, syncState);
1383         }
1384     }
1385 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR || OHOS_BUILD_ENABLE_MONITOR
1386     return RET_OK;
1387 }
1388 
1389 #ifdef OHOS_BUILD_ENABLE_MONITOR
CheckMarkConsumed(int32_t pid,int32_t eventId)1390 int32_t MMIService::CheckMarkConsumed(int32_t pid, int32_t eventId)
1391 {
1392     auto sess = GetSessionByPid(pid);
1393     CHKPR(sess, ERROR_NULL_POINTER);
1394     return sMsgHandler_.OnMarkConsumed(sess, eventId);
1395 }
1396 #endif // OHOS_BUILD_ENABLE_MONITOR
1397 
MarkEventConsumed(int32_t eventId)1398 int32_t MMIService::MarkEventConsumed(int32_t eventId)
1399 {
1400     CALL_DEBUG_ENTER;
1401 #ifdef OHOS_BUILD_ENABLE_MONITOR
1402     int32_t pid = GetCallingPid();
1403     int32_t ret = delegateTasks_.PostSyncTask(
1404         [this, pid, eventId] {
1405             return this->CheckMarkConsumed(pid, eventId);
1406         }
1407         );
1408     if (ret != RET_OK) {
1409         MMI_HILOGE("Mark event consumed failed, ret:%{public}d", ret);
1410         return ret;
1411     }
1412 #endif // OHOS_BUILD_ENABLE_MONITOR
1413     return RET_OK;
1414 }
1415 
MoveMouseEvent(int32_t offsetX,int32_t offsetY)1416 int32_t MMIService::MoveMouseEvent(int32_t offsetX, int32_t offsetY)
1417 {
1418     CALL_DEBUG_ENTER;
1419 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
1420     int32_t ret =
1421         delegateTasks_.PostSyncTask(
1422             [this, offsetX, offsetY] {
1423                 return sMsgHandler_.OnMoveMouse(offsetX, offsetY);
1424             }
1425             );
1426     if (ret != RET_OK) {
1427         MMI_HILOGE("The movemouse event processed failed, ret:%{public}d", ret);
1428         return ret;
1429     }
1430 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
1431     return RET_OK;
1432 }
1433 
InjectKeyEvent(const std::shared_ptr<KeyEvent> keyEvent,bool isNativeInject)1434 int32_t MMIService::InjectKeyEvent(const std::shared_ptr<KeyEvent> keyEvent, bool isNativeInject)
1435 {
1436     CALL_DEBUG_ENTER;
1437 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
1438     int32_t ret;
1439     int32_t pid = GetCallingPid();
1440 #ifdef OHOS_BUILD_ENABLE_ANCO
1441     ret = InjectKeyEventExt(keyEvent, pid, isNativeInject);
1442 #else
1443     ret = delegateTasks_.PostSyncTask(
1444         [this, keyEvent, pid, isNativeInject] {
1445             return this->CheckInjectKeyEvent(keyEvent, pid, isNativeInject);
1446         }
1447         );
1448 #endif // OHOS_BUILD_ENABLE_ANCO
1449     if (ret != RET_OK) {
1450         MMI_HILOGE("Inject key event failed, ret:%{public}d", ret);
1451         return ret;
1452     }
1453 #endif // OHOS_BUILD_ENABLE_KEYBOARD
1454     return RET_OK;
1455 }
1456 
CheckInjectKeyEvent(const std::shared_ptr<KeyEvent> keyEvent,int32_t pid,bool isNativeInject)1457 int32_t MMIService::CheckInjectKeyEvent(const std::shared_ptr<KeyEvent> keyEvent, int32_t pid, bool isNativeInject)
1458 {
1459 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
1460     CHKPR(keyEvent, ERROR_NULL_POINTER);
1461     LogTracer lt(keyEvent->GetId(), keyEvent->GetEventType(), keyEvent->GetKeyAction());
1462     return sMsgHandler_.OnInjectKeyEvent(keyEvent, pid, isNativeInject);
1463 #else
1464     return RET_OK;
1465 #endif // OHOS_BUILD_ENABLE_KEYBOARD
1466 }
1467 
1468 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
OnGetKeyState(std::vector<int32_t> & pressedKeys,std::map<int32_t,int32_t> & specialKeysState)1469 int32_t MMIService::OnGetKeyState(std::vector<int32_t> &pressedKeys, std::map<int32_t, int32_t> &specialKeysState)
1470 {
1471     auto keyEvent = KeyEventHdr->GetKeyEvent();
1472     CHKPR(keyEvent, ERROR_NULL_POINTER);
1473     pressedKeys = keyEvent->GetPressedKeys();
1474     for (auto iter = pressedKeys.begin(); iter != pressedKeys.end();) {
1475         if (g_keyCodeValueSet.find(*iter) == g_keyCodeValueSet.end()) {
1476             iter = pressedKeys.erase(iter);
1477             continue;
1478         }
1479         ++iter;
1480     }
1481     specialKeysState[KeyEvent::KEYCODE_CAPS_LOCK] =
1482         static_cast<int32_t>(keyEvent->GetFunctionKey(KeyEvent::CAPS_LOCK_FUNCTION_KEY));
1483     specialKeysState[KeyEvent::KEYCODE_SCROLL_LOCK] =
1484         static_cast<int32_t>(keyEvent->GetFunctionKey(KeyEvent::SCROLL_LOCK_FUNCTION_KEY));
1485     specialKeysState[KeyEvent::KEYCODE_NUM_LOCK] =
1486         static_cast<int32_t>(keyEvent->GetFunctionKey(KeyEvent::NUM_LOCK_FUNCTION_KEY));
1487     return RET_OK;
1488 }
1489 #endif // OHOS_BUILD_ENABLE_KEYBOARD
1490 
CheckInjectPointerEvent(const std::shared_ptr<PointerEvent> pointerEvent,int32_t pid,bool isNativeInject,bool isShell)1491 int32_t MMIService::CheckInjectPointerEvent(const std::shared_ptr<PointerEvent> pointerEvent,
1492     int32_t pid, bool isNativeInject, bool isShell)
1493 {
1494 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
1495     CHKPR(pointerEvent, ERROR_NULL_POINTER);
1496     LogTracer lt(pointerEvent->GetId(), pointerEvent->GetEventType(), pointerEvent->GetPointerAction());
1497     return sMsgHandler_.OnInjectPointerEvent(pointerEvent, pid, isNativeInject, isShell);
1498 #else
1499     return RET_OK;
1500 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
1501 }
1502 
InjectPointerEvent(const std::shared_ptr<PointerEvent> pointerEvent,bool isNativeInject)1503 int32_t MMIService::InjectPointerEvent(const std::shared_ptr<PointerEvent> pointerEvent, bool isNativeInject)
1504 {
1505     CALL_DEBUG_ENTER;
1506 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
1507     int32_t ret;
1508     int32_t pid = GetCallingPid();
1509     bool isShell = PER_HELPER->RequestFromShell();
1510 #ifdef OHOS_BUILD_ENABLE_ANCO
1511     ret = InjectPointerEventExt(pointerEvent, pid, isNativeInject, isShell);
1512 #else
1513     ret = delegateTasks_.PostSyncTask(
1514         [this, pointerEvent, pid, isNativeInject, isShell] {
1515             return this->CheckInjectPointerEvent(pointerEvent, pid, isNativeInject, isShell);
1516         }
1517         );
1518 #endif // OHOS_BUILD_ENABLE_ANCO
1519     if (ret != RET_OK) {
1520         MMI_HILOGE("Inject pointer event failed, ret:%{public}d", ret);
1521         return ret;
1522     }
1523 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
1524     return RET_OK;
1525 }
1526 
1527 #ifdef OHOS_RSS_CLIENT
OnAddResSchedSystemAbility(int32_t systemAbilityId,const std::string & deviceId)1528 void MMIService::OnAddResSchedSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
1529 {
1530     int sleepSeconds = 1;
1531     sleep(sleepSeconds);
1532     uint64_t tid = tid_.load();
1533     int32_t userInteraction = 2;
1534     std::unordered_map<std::string, std::string> payload;
1535     payload["uid"] = std::to_string(getuid());
1536     payload["pid"] = std::to_string(getpid());
1537     payload["extType"] = "10002";
1538     payload["tid"] = std::to_string(tid);
1539     payload["isSa"] = "1";
1540     payload["cgroupPrio"] = "1";
1541     payload["threadName"] = "mmi_service";
1542     ResourceSchedule::ResSchedClient::GetInstance().ReportData(
1543         ResourceSchedule::ResType::RES_TYPE_KEY_PERF_SCENE, userInteraction, payload);
1544 }
1545 #endif // OHOS_RSS_CLIENT
1546 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)1547 void MMIService::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
1548 {
1549     CALL_INFO_TRACE;
1550     MMI_HILOGI("systemAbilityId is %{public}d", systemAbilityId);
1551 #ifdef OHOS_RSS_CLIENT
1552     if (systemAbilityId == RES_SCHED_SYS_ABILITY_ID) {
1553         OnAddResSchedSystemAbility(systemAbilityId, deviceId);
1554     }
1555 #endif // OHOS_RSS_CLIENT
1556 #ifdef OHOS_BUILD_ENABLE_FINGERSENSE_WRAPPER
1557     if (systemAbilityId == COMMON_EVENT_SERVICE_ID) {
1558         isCesStart_ = true;
1559     }
1560 #endif // OHOS_BUILD_ENABLE_FINGERSENSE_WRAPPER
1561     if (systemAbilityId == APP_MGR_SERVICE_ID) {
1562         APP_OBSERVER_MGR->InitAppStateObserver();
1563     }
1564     if (systemAbilityId == COMMON_EVENT_SERVICE_ID) {
1565         DEVICE_MONITOR->InitCommonEventSubscriber();
1566 #if defined(OHOS_BUILD_ENABLE_KEYBOARD) && defined(OHOS_BUILD_ENABLE_FINGERSENSE_WRAPPER)
1567         DISPLAY_MONITOR->InitCommonEventSubscriber();
1568 #endif // OHOS_BUILD_ENABLE_KEYBOARD && OHOS_BUILD_ENABLE_FINGERSENSE_WRAPPER
1569     }
1570 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
1571     if (systemAbilityId == RENDER_SERVICE) {
1572         IPointerDrawingManager::GetInstance()->InitPointerCallback();
1573     }
1574 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
1575     if (systemAbilityId == DISPLAY_MANAGER_SERVICE_SA_ID) {
1576         WIN_MGR->SetFoldState();
1577     }
1578 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
1579     if (systemAbilityId == DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID) {
1580         if (SettingDataShare::GetInstance(DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID).CheckIfSettingsDataReady()) {
1581             IPointerDrawingManager::GetInstance()->InitPointerObserver();
1582         }
1583     }
1584 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
1585 #if defined(OHOS_BUILD_ENABLE_MONITOR) && defined(PLAYER_FRAMEWORK_EXISTS)
1586     if (systemAbilityId == PLAYER_DISTRIBUTED_SERVICE_ID) {
1587         auto monitorHandler = InputHandler->GetMonitorHandler();
1588         CHKPV(monitorHandler);
1589         monitorHandler->RegisterScreenCaptureListener();
1590     }
1591 #endif
1592 }
1593 
SubscribeKeyEvent(int32_t subscribeId,const std::shared_ptr<KeyOption> option)1594 int32_t MMIService::SubscribeKeyEvent(int32_t subscribeId, const std::shared_ptr<KeyOption> option)
1595 {
1596     CALL_DEBUG_ENTER;
1597 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
1598     int32_t pid = GetCallingPid();
1599     int32_t ret = delegateTasks_.PostSyncTask(
1600         [this, pid, subscribeId, option] {
1601             return sMsgHandler_.OnSubscribeKeyEvent(this, pid, subscribeId, option);
1602         }
1603         );
1604     if (ret != RET_OK) {
1605         MMI_HILOGE("The subscribe key event processed failed, ret:%{public}d", ret);
1606         return ret;
1607     }
1608     if (NapProcess::GetInstance()->GetNapClientPid() != REMOVE_OBSERVER) {
1609         OHOS::MMI::NapProcess::NapStatusData napData;
1610         napData.pid = GetCallingPid();
1611         napData.uid = GetCallingUid();
1612         auto sess = GetSessionByPid(pid);
1613         CHKPR(sess, ERROR_NULL_POINTER);
1614         napData.bundleName = sess->GetProgramName();
1615         int32_t syncState = SUBSCRIBED;
1616         MMI_HILOGD("SubscribeKeyEvent info to observer : pid:%{public}d, uid:%{public}d, bundleName:%{public}s",
1617             napData.pid, napData.uid, napData.bundleName.c_str());
1618         NapProcess::GetInstance()->AddMmiSubscribedEventData(napData, syncState);
1619         if (NapProcess::GetInstance()->GetNapClientPid() != UNOBSERVED) {
1620             NapProcess::GetInstance()->NotifyBundleName(napData, syncState);
1621         }
1622     }
1623 #endif // OHOS_BUILD_ENABLE_KEYBOARD
1624     return RET_OK;
1625 }
1626 
UnsubscribeKeyEvent(int32_t subscribeId)1627 int32_t MMIService::UnsubscribeKeyEvent(int32_t subscribeId)
1628 {
1629     CALL_INFO_TRACE;
1630 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
1631     int32_t pid = GetCallingPid();
1632     int32_t ret = delegateTasks_.PostSyncTask(
1633         [this, pid, subscribeId] {
1634             return sMsgHandler_.OnUnsubscribeKeyEvent(this, pid, subscribeId);
1635         }
1636         );
1637     if (ret != RET_OK) {
1638         MMI_HILOGE("The unsubscribe key event processed failed, ret:%{public}d", ret);
1639         return ret;
1640     }
1641     if (NapProcess::GetInstance()->GetNapClientPid() != REMOVE_OBSERVER) {
1642         OHOS::MMI::NapProcess::NapStatusData napData;
1643         napData.pid = GetCallingPid();
1644         napData.uid = GetCallingUid();
1645         auto sess = GetSessionByPid(pid);
1646         CHKPR(sess, ERROR_NULL_POINTER);
1647         napData.bundleName = sess->GetProgramName();
1648         int32_t syncState = UNSUBSCRIBED;
1649         MMI_HILOGD("UnsubscribeKeyEvent info to observer : pid:%{public}d, uid:%{public}d, bundleName:%{public}s",
1650             napData.pid, napData.uid, napData.bundleName.c_str());
1651         NapProcess::GetInstance()->AddMmiSubscribedEventData(napData, syncState);
1652         if (NapProcess::GetInstance()->GetNapClientPid() != UNOBSERVED) {
1653             NapProcess::GetInstance()->NotifyBundleName(napData, syncState);
1654         }
1655     }
1656 #endif // OHOS_BUILD_ENABLE_KEYBOARD
1657     return RET_OK;
1658 }
1659 
SubscribeSwitchEvent(int32_t subscribeId,int32_t switchType)1660 int32_t MMIService::SubscribeSwitchEvent(int32_t subscribeId, int32_t switchType)
1661 {
1662     CALL_INFO_TRACE;
1663 #ifdef OHOS_BUILD_ENABLE_SWITCH
1664     int32_t pid = GetCallingPid();
1665     int32_t ret = delegateTasks_.PostSyncTask(
1666         [this, pid, subscribeId, switchType] {
1667             return sMsgHandler_.OnSubscribeSwitchEvent(this, pid, subscribeId, switchType);
1668         }
1669         );
1670     if (ret != RET_OK) {
1671         MMI_HILOGE("The subscribe switch event processed failed, ret:%{public}d", ret);
1672         return ret;
1673     }
1674 #endif // OHOS_BUILD_ENABLE_SWITCH
1675     return RET_OK;
1676 }
1677 
UnsubscribeSwitchEvent(int32_t subscribeId)1678 int32_t MMIService::UnsubscribeSwitchEvent(int32_t subscribeId)
1679 {
1680     CALL_INFO_TRACE;
1681 #ifdef OHOS_BUILD_ENABLE_SWITCH
1682     int32_t pid = GetCallingPid();
1683     int32_t ret = delegateTasks_.PostSyncTask(
1684         [this, pid, subscribeId] {
1685             return sMsgHandler_.OnUnsubscribeSwitchEvent(this, pid, subscribeId);
1686         }
1687         );
1688     if (ret != RET_OK) {
1689         MMI_HILOGE("The unsubscribe switch event processed failed, ret:%{public}d", ret);
1690         return ret;
1691     }
1692 #endif // OHOS_BUILD_ENABLE_SWITCH
1693     return RET_OK;
1694 }
1695 
SetAnrObserver()1696 int32_t MMIService::SetAnrObserver()
1697 {
1698     CALL_INFO_TRACE;
1699     int32_t pid = GetCallingPid();
1700     int32_t ret = delegateTasks_.PostSyncTask(
1701         [pid] {
1702             return ::OHOS::DelayedSingleton<ANRManager>::GetInstance()->SetANRNoticedPid(pid);
1703         }
1704         );
1705     if (ret != RET_OK) {
1706         MMI_HILOGE("Set ANRNoticed pid failed, ret:%{public}d", ret);
1707         return ret;
1708     }
1709     return RET_OK;
1710 }
1711 
GetDisplayBindInfo(DisplayBindInfos & infos)1712 int32_t MMIService::GetDisplayBindInfo(DisplayBindInfos &infos)
1713 {
1714     CALL_INFO_TRACE;
1715     int32_t ret = delegateTasks_.PostSyncTask(
1716         [&infos] {
1717             return ::OHOS::MMI::IInputWindowsManager::GetInstance()->GetDisplayBindInfo(infos);
1718         }
1719         );
1720     if (ret != RET_OK) {
1721         MMI_HILOGE("GetDisplayBindInfo pid failed, ret:%{public}d", ret);
1722         return ret;
1723     }
1724     return RET_OK;
1725 }
1726 
GetAllMmiSubscribedEvents(std::map<std::tuple<int32_t,int32_t,std::string>,int32_t> & datas)1727 int32_t MMIService::GetAllMmiSubscribedEvents(std::map<std::tuple<int32_t, int32_t, std::string>, int32_t> &datas)
1728 {
1729     CALL_INFO_TRACE;
1730     NapProcess::GetInstance()->GetAllMmiSubscribedEvents(datas);
1731     return RET_OK;
1732 }
1733 
SetDisplayBind(int32_t deviceId,int32_t displayId,std::string & msg)1734 int32_t MMIService::SetDisplayBind(int32_t deviceId, int32_t displayId, std::string &msg)
1735 {
1736     CALL_INFO_TRACE;
1737     int32_t ret = delegateTasks_.PostSyncTask(
1738         [deviceId, displayId, &msg] {
1739             return ::OHOS::MMI::IInputWindowsManager::GetInstance()->SetDisplayBind(deviceId, displayId, msg);
1740         }
1741         );
1742     if (ret != RET_OK) {
1743         MMI_HILOGE("SetDisplayBind pid failed, ret:%{public}d", ret);
1744         return ret;
1745     }
1746     return RET_OK;
1747 }
1748 
GetFunctionKeyState(int32_t funcKey,bool & state)1749 int32_t MMIService::GetFunctionKeyState(int32_t funcKey, bool &state)
1750 {
1751     CALL_INFO_TRACE;
1752 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
1753     int32_t ret = delegateTasks_.PostSyncTask(
1754         [this, funcKey, &state] {
1755             return sMsgHandler_.OnGetFunctionKeyState(funcKey, state);
1756         }
1757         );
1758     if (ret != RET_OK) {
1759         MMI_HILOGE("Failed to get the keyboard status, ret:%{public}d", ret);
1760         return ret;
1761     }
1762 #endif // OHOS_BUILD_ENABLE_KEYBOARD
1763     return RET_OK;
1764 }
1765 
SetFunctionKeyState(int32_t funcKey,bool enable)1766 int32_t MMIService::SetFunctionKeyState(int32_t funcKey, bool enable)
1767 {
1768     CALL_INFO_TRACE;
1769 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
1770     int32_t ret = delegateTasks_.PostSyncTask(
1771         [this, funcKey, enable] {
1772             return sMsgHandler_.OnSetFunctionKeyState(funcKey, enable);
1773         }
1774         );
1775     if (ret != RET_OK) {
1776         MMI_HILOGE("Failed to update the keyboard status, ret:%{public}d", ret);
1777         return ret;
1778     }
1779 #endif // OHOS_BUILD_ENABLE_KEYBOARD
1780     return RET_OK;
1781 }
1782 
SetPointerLocation(int32_t x,int32_t y)1783 int32_t MMIService::SetPointerLocation(int32_t x, int32_t y)
1784 {
1785     CALL_INFO_TRACE;
1786 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
1787     int32_t ret = delegateTasks_.PostSyncTask(
1788         [x, y] {
1789             return ::OHOS::DelayedSingleton<MouseEventNormalize>::GetInstance()->SetPointerLocation(x, y);
1790         }
1791         );
1792     if (ret != RET_OK) {
1793         MMI_HILOGE("Set pointer location failed, ret:%{public}d", ret);
1794         return ret;
1795     }
1796 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
1797     return RET_OK;
1798 }
1799 
OnDelegateTask(epoll_event & ev)1800 void MMIService::OnDelegateTask(epoll_event &ev)
1801 {
1802     if ((ev.events & EPOLLIN) == 0) {
1803         MMI_HILOGW("Not epollin");
1804         return;
1805     }
1806     DelegateTasks::TaskData data = {};
1807     auto res = read(delegateTasks_.GetReadFd(), &data, sizeof(data));
1808     if (res == -1) {
1809         MMI_HILOGW("Read failed erron:%{public}d", errno);
1810     }
1811     MMI_HILOGD("RemoteRequest notify td:%{public}" PRId64 ",std:%{public}" PRId64 ""
1812         ",taskId:%{public}d",
1813         GetThisThreadId(), data.tid, data.taskId);
1814     delegateTasks_.ProcessTasks();
1815 }
1816 
OnThread()1817 void MMIService::OnThread()
1818 {
1819     SetThreadName(std::string("mmi_service"));
1820     uint64_t tid = GetThisThreadId();
1821     delegateTasks_.SetWorkerThreadId(tid);
1822     MMI_HILOGI("Main worker thread start. tid:%{public}" PRId64 "", tid);
1823 #ifdef OHOS_RSS_CLIENT
1824     tid_.store(tid);
1825 #endif
1826     libinputAdapter_.ProcessPendingEvents();
1827     while (state_ == ServiceRunningState::STATE_RUNNING) {
1828 #if defined(OHOS_BUILD_ENABLE_FINGERSENSE_WRAPPER) && defined(OHOS_BUILD_ENABLE_KEYBOARD)
1829         if (isCesStart_ && !DISPLAY_MONITOR->IsCommonEventSubscriberInit()) {
1830             DISPLAY_MONITOR->InitCommonEventSubscriber();
1831         }
1832 #endif // OHOS_BUILD_ENABLE_FINGERSENSE_WRAPPER && OHOS_BUILD_ENABLE_KEYBOARD
1833         epoll_event ev[MAX_EVENT_SIZE] = {};
1834         int32_t timeout = TimerMgr->CalcNextDelay();
1835         MMI_HILOGD("timeout:%{public}d", timeout);
1836         int32_t count = EpollWait(ev[0], MAX_EVENT_SIZE, timeout, mmiFd_);
1837         for (int32_t i = 0; i < count && state_ == ServiceRunningState::STATE_RUNNING; i++) {
1838             auto mmiEdIter = epollEventMap_.find(ev[i].data.fd);
1839             if (mmiEdIter == epollEventMap_.end()) {
1840                 MMI_HILOGW("Invalid event %{public}d %{public}d", ev[i].data.fd, count);
1841                 continue;
1842             }
1843             std::shared_ptr<mmi_epoll_event> mmiEd = mmiEdIter->second;
1844             CHKPC(mmiEd);
1845             epoll_event event = ev[i];
1846             if (mmiEd->event_type == EPOLL_EVENT_INPUT) {
1847                 CalculateFuntionRunningTime([this, &mmiEd] () { libinputAdapter_.EventDispatch(mmiEd->fd); },
1848                     "EPOLL_EVENT_INPUT");
1849             } else if (mmiEd->event_type == EPOLL_EVENT_SOCKET) {
1850                 CalculateFuntionRunningTime([this, &event]() { this->OnEpollEvent(event); }, "EPOLL_EVENT_SOCKET");
1851             } else if (mmiEd->event_type == EPOLL_EVENT_SIGNAL) {
1852                 OnSignalEvent(mmiEd->fd);
1853             } else if (mmiEd->event_type == EPOLL_EVENT_ETASK) {
1854                 CalculateFuntionRunningTime([this, &event]() { this->OnDelegateTask(event); }, "EPOLL_EVENT_ETASK");
1855             } else {
1856                 MMI_HILOGW("Unknown epoll event type:%{public}d", mmiEd->event_type);
1857             }
1858         }
1859         TimerMgr->ProcessTimers();
1860         if (state_ != ServiceRunningState::STATE_RUNNING) {
1861             break;
1862         }
1863     }
1864     MMI_HILOGI("Main worker thread stop. tid:%{public}" PRId64 "", tid);
1865 }
1866 
InitSignalHandler()1867 bool MMIService::InitSignalHandler()
1868 {
1869     CALL_DEBUG_ENTER;
1870     sigset_t mask = { 0 };
1871     int32_t retCode = sigfillset(&mask);
1872     if (retCode < 0) {
1873         MMI_HILOGE("Fill signal set failed:%{public}d", errno);
1874         return false;
1875     }
1876 
1877     retCode = sigprocmask(SIG_SETMASK, &mask, nullptr);
1878     if (retCode < 0) {
1879         MMI_HILOGE("Sigprocmask failed:%{public}d", errno);
1880         return false;
1881     }
1882 
1883     int32_t fdSignal = signalfd(-1, &mask, SFD_NONBLOCK | SFD_CLOEXEC);
1884     if (fdSignal < 0) {
1885         MMI_HILOGE("Signal fd failed:%{public}d", errno);
1886         return false;
1887     }
1888 
1889     retCode = AddEpoll(EPOLL_EVENT_SIGNAL, fdSignal);
1890     if (retCode < 0) {
1891         MMI_HILOGE("AddEpoll signalFd failed:%{public}d", retCode);
1892         close(fdSignal);
1893         return false;
1894     }
1895     return true;
1896 }
1897 
OnSignalEvent(int32_t signalFd)1898 void MMIService::OnSignalEvent(int32_t signalFd)
1899 {
1900     CALL_DEBUG_ENTER;
1901     signalfd_siginfo sigInfo;
1902     int32_t size = ::read(signalFd, &sigInfo, sizeof(signalfd_siginfo));
1903     if (size != static_cast<int32_t>(sizeof(signalfd_siginfo))) {
1904         MMI_HILOGE("Read signal info failed, invalid size:%{public}d, errno:%{public}d", size, errno);
1905         return;
1906     }
1907     int32_t signo = static_cast<int32_t>(sigInfo.ssi_signo);
1908     MMI_HILOGD("Receive signal:%{public}d", signo);
1909     switch (signo) {
1910         case SIGINT:
1911         case SIGQUIT:
1912         case SIGILL:
1913         case SIGABRT:
1914         case SIGBUS:
1915         case SIGFPE:
1916         case SIGKILL:
1917         case SIGSEGV:
1918         case SIGTERM: {
1919             state_ = ServiceRunningState::STATE_EXIT;
1920             break;
1921         }
1922         default: {
1923             break;
1924         }
1925     }
1926 }
1927 
AddReloadDeviceTimer()1928 void MMIService::AddReloadDeviceTimer()
1929 {
1930     CALL_DEBUG_ENTER;
1931     TimerMgr->AddTimer(RELOAD_DEVICE_TIME, REPEAT_COUNT, [this]() {
1932         auto deviceIds = INPUT_DEV_MGR->GetInputDeviceIds();
1933         if (deviceIds.empty()) {
1934             libinputAdapter_.ReloadDevice();
1935         }
1936     });
1937 }
1938 
Dump(int32_t fd,const std::vector<std::u16string> & args)1939 int32_t MMIService::Dump(int32_t fd, const std::vector<std::u16string> &args)
1940 {
1941     CALL_DEBUG_ENTER;
1942     if (fd < 0) {
1943         MMI_HILOGE("The fd is invalid");
1944         return DUMP_PARAM_ERR;
1945     }
1946     if (args.empty()) {
1947         MMI_HILOGE("The args cannot be empty");
1948         mprintf(fd, "args cannot be empty\n");
1949         MMIEventDump->DumpHelp(fd);
1950         return DUMP_PARAM_ERR;
1951     }
1952     std::vector<std::string> argList = { "" };
1953     std::transform(args.begin(), args.end(), std::back_inserter(argList),
1954         [](const std::u16string &arg) { return Str16ToStr8(arg); });
1955     MMIEventDump->ParseCommand(fd, argList);
1956     return RET_OK;
1957 }
1958 
SetMouseCaptureMode(int32_t windowId,bool isCaptureMode)1959 int32_t MMIService::SetMouseCaptureMode(int32_t windowId, bool isCaptureMode)
1960 {
1961     CALL_INFO_TRACE;
1962     int32_t ret = delegateTasks_.PostSyncTask(
1963         [windowId, isCaptureMode] {
1964             return ::OHOS::MMI::IInputWindowsManager::GetInstance()->SetMouseCaptureMode(windowId, isCaptureMode);
1965         }
1966         );
1967     if (ret != RET_OK) {
1968         MMI_HILOGE("Set capture failed, return:%{public}d", ret);
1969         return ret;
1970     }
1971     return RET_OK;
1972 }
1973 
OnGetWindowPid(int32_t windowId,int32_t & windowPid)1974 int32_t MMIService::OnGetWindowPid(int32_t windowId, int32_t &windowPid)
1975 {
1976     CALL_DEBUG_ENTER;
1977     windowPid = WIN_MGR->GetWindowPid(windowId);
1978     if (windowPid == RET_ERR) {
1979         MMI_HILOGE("Get window pid failed");
1980         return RET_ERR;
1981     }
1982     MMI_HILOGD("windowpid is %{public}d", windowPid);
1983     return RET_OK;
1984 }
1985 
GetWindowPid(int32_t windowId)1986 int32_t MMIService::GetWindowPid(int32_t windowId)
1987 {
1988     CALL_INFO_TRACE;
1989     int32_t windowPid = INVALID_PID;
1990     int32_t ret = delegateTasks_.PostSyncTask(
1991         [this, windowId, &windowPid] {
1992             return this->OnGetWindowPid(windowId, windowPid);
1993         }
1994         );
1995     if (ret != RET_OK) {
1996         MMI_HILOGE("OnGetWindowPid failed, ret:%{public}d", ret);
1997         return ret;
1998     }
1999     MMI_HILOGD("windowpid is %{public}d", windowPid);
2000     return windowPid;
2001 }
2002 
AppendExtraData(const ExtraData & extraData)2003 int32_t MMIService::AppendExtraData(const ExtraData &extraData)
2004 {
2005     CALL_DEBUG_ENTER;
2006     int32_t ret = delegateTasks_.PostSyncTask(
2007         [extraData] {
2008             return ::OHOS::MMI::IInputWindowsManager::GetInstance()->AppendExtraData(extraData);
2009         }
2010         );
2011     if (ret != RET_OK) {
2012         MMI_HILOGE("Append extra data failed:%{public}d", ret);
2013     }
2014     return ret;
2015 }
2016 
EnableInputDevice(bool enable)2017 int32_t MMIService::EnableInputDevice(bool enable)
2018 {
2019     CALL_DEBUG_ENTER;
2020     int32_t ret = delegateTasks_.PostSyncTask(
2021         [enable] {
2022             return ::OHOS::MMI::InputDeviceManager::GetInstance()->OnEnableInputDevice(enable);
2023         }
2024         );
2025     if (ret != RET_OK) {
2026         MMI_HILOGE("OnEnableInputDevice failed:%{public}d", ret);
2027     }
2028     return ret;
2029 }
2030 
2031 #if defined(OHOS_BUILD_ENABLE_KEYBOARD) && defined(OHOS_BUILD_ENABLE_COMBINATION_KEY)
UpdateCombineKeyState(bool enable)2032 int32_t MMIService::UpdateCombineKeyState(bool enable)
2033 {
2034     auto eventSubscriberHandler = InputHandler->GetSubscriberHandler();
2035     CHKPR(eventSubscriberHandler, RET_ERR);
2036     int32_t ret = eventSubscriberHandler->EnableCombineKey(enable);
2037     if (ret != RET_OK) {
2038         MMI_HILOGE("EnableCombineKey is failed in key command:%{public}d", ret);
2039     }
2040 
2041     auto eventKeyCommandHandler = InputHandler->GetKeyCommandHandler();
2042     CHKPR(eventKeyCommandHandler, RET_ERR);
2043     ret = eventKeyCommandHandler->EnableCombineKey(enable);
2044     if (ret != RET_OK) {
2045         MMI_HILOGE("EnableCombineKey is failed in key command:%{public}d", ret);
2046     }
2047     return ret;
2048 }
2049 #endif // OHOS_BUILD_ENABLE_KEYBOARD && OHOS_BUILD_ENABLE_COMBINATION_KEY
2050 
CheckPidPermission(int32_t pid)2051 int32_t MMIService::CheckPidPermission(int32_t pid)
2052 {
2053     CALL_DEBUG_ENTER;
2054     int32_t checkingPid = GetCallingPid();
2055     if (checkingPid != pid) {
2056         MMI_HILOGE("check pid failed, input pid:%{public}d, but checking pid:%{public}d", pid, checkingPid);
2057         return RET_ERR;
2058     }
2059     return RET_OK;
2060 }
2061 
EnableCombineKey(bool enable)2062 int32_t MMIService::EnableCombineKey(bool enable)
2063 {
2064     CALL_DEBUG_ENTER;
2065 #if defined(OHOS_BUILD_ENABLE_KEYBOARD) && defined(OHOS_BUILD_ENABLE_COMBINATION_KEY)
2066     int32_t ret = delegateTasks_.PostSyncTask(
2067         [this, enable] {
2068             return this->UpdateCombineKeyState(enable);
2069         }
2070         );
2071     if (ret != RET_OK) {
2072         MMI_HILOGE("Set key down duration failed:%{public}d", ret);
2073         return ret;
2074     }
2075 #endif // OHOS_BUILD_ENABLE_KEYBOARD && OHOS_BUILD_ENABLE_COMBINATION_KEY
2076     return RET_OK;
2077 }
2078 
2079 #if defined(OHOS_BUILD_ENABLE_KEYBOARD) && defined(OHOS_BUILD_ENABLE_COMBINATION_KEY)
UpdateSettingsXml(const std::string & businessId,int32_t delay)2080 int32_t MMIService::UpdateSettingsXml(const std::string &businessId, int32_t delay)
2081 {
2082     std::shared_ptr<KeyCommandHandler> eventKeyCommandHandler = InputHandler->GetKeyCommandHandler();
2083     CHKPR(eventKeyCommandHandler, RET_ERR);
2084     return eventKeyCommandHandler->UpdateSettingsXml(businessId, delay);
2085 }
2086 #endif // OHOS_BUILD_ENABLE_KEYBOARD && OHOS_BUILD_ENABLE_COMBINATION_KEY
2087 
SetKeyDownDuration(const std::string & businessId,int32_t delay)2088 int32_t MMIService::SetKeyDownDuration(const std::string &businessId, int32_t delay)
2089 {
2090     CALL_INFO_TRACE;
2091 #if defined(OHOS_BUILD_ENABLE_KEYBOARD) && defined(OHOS_BUILD_ENABLE_COMBINATION_KEY)
2092     int32_t ret = delegateTasks_.PostSyncTask(
2093         [this, businessId, delay] {
2094             return this->UpdateSettingsXml(businessId, delay);
2095         }
2096         );
2097     if (ret != RET_OK) {
2098         MMI_HILOGE("Set key down duration failed:%{public}d", ret);
2099         return ret;
2100     }
2101 #endif // OHOS_BUILD_ENABLE_KEYBOARD && OHOS_BUILD_ENABLE_COMBINATION_KEY
2102     return RET_OK;
2103 }
2104 
2105 #ifdef OHOS_BUILD_ENABLE_POINTER
ReadTouchpadScrollSwich(bool & switchFlag)2106 int32_t MMIService::ReadTouchpadScrollSwich(bool &switchFlag)
2107 {
2108     MouseEventHdr->GetTouchpadScrollSwitch(switchFlag);
2109     return RET_OK;
2110 }
2111 
ReadTouchpadScrollDirection(bool & switchFlag)2112 int32_t MMIService::ReadTouchpadScrollDirection(bool &switchFlag)
2113 {
2114     MouseEventHdr->GetTouchpadScrollDirection(switchFlag);
2115     return RET_OK;
2116 }
2117 
ReadTouchpadTapSwitch(bool & switchFlag)2118 int32_t MMIService::ReadTouchpadTapSwitch(bool &switchFlag)
2119 {
2120     MouseEventHdr->GetTouchpadTapSwitch(switchFlag);
2121     return RET_OK;
2122 }
2123 
ReadTouchpadPointerSpeed(int32_t & speed)2124 int32_t MMIService::ReadTouchpadPointerSpeed(int32_t &speed)
2125 {
2126     MouseEventHdr->GetTouchpadPointerSpeed(speed);
2127     return RET_OK;
2128 }
2129 
ReadTouchpadPinchSwitch(bool & switchFlag)2130 int32_t MMIService::ReadTouchpadPinchSwitch(bool &switchFlag)
2131 {
2132     TOUCH_EVENT_HDR->GetTouchpadPinchSwitch(switchFlag);
2133     return RET_OK;
2134 }
2135 
ReadTouchpadSwipeSwitch(bool & switchFlag)2136 int32_t MMIService::ReadTouchpadSwipeSwitch(bool &switchFlag)
2137 {
2138     TOUCH_EVENT_HDR->GetTouchpadSwipeSwitch(switchFlag);
2139     return RET_OK;
2140 }
2141 
ReadTouchpadRightMenuType(int32_t & type)2142 int32_t MMIService::ReadTouchpadRightMenuType(int32_t &type)
2143 {
2144     MouseEventHdr->GetTouchpadRightClickType(type);
2145     return RET_OK;
2146 }
2147 
ReadTouchpadRotateSwitch(bool & rotateSwitch)2148 int32_t MMIService::ReadTouchpadRotateSwitch(bool &rotateSwitch)
2149 {
2150     TOUCH_EVENT_HDR->GetTouchpadRotateSwitch(rotateSwitch);
2151     return RET_OK;
2152 }
2153 
ReadTouchpadDoubleTapAndDragState(bool & switchFlag)2154 int32_t MMIService::ReadTouchpadDoubleTapAndDragState(bool &switchFlag)
2155 {
2156     TOUCH_EVENT_HDR->GetTouchpadDoubleTapAndDragState(switchFlag);
2157     return RET_OK;
2158 }
2159 
2160 #endif // OHOS_BUILD_ENABLE_POINTER
2161 
SetTouchpadScrollSwitch(bool switchFlag)2162 int32_t MMIService::SetTouchpadScrollSwitch(bool switchFlag)
2163 {
2164     CALL_INFO_TRACE;
2165 #if defined OHOS_BUILD_ENABLE_POINTER
2166     int32_t ret = delegateTasks_.PostSyncTask(
2167         [switchFlag] {
2168             return ::OHOS::DelayedSingleton<MouseEventNormalize>::GetInstance()->SetTouchpadScrollSwitch(switchFlag);
2169         }
2170         );
2171     if (ret != RET_OK) {
2172         MMI_HILOGE("Set touchpad scroll switch failed, return:%{public}d", ret);
2173         return ret;
2174     }
2175 #endif // OHOS_BUILD_ENABLE_POINTER
2176     return RET_OK;
2177 }
2178 
GetTouchpadScrollSwitch(bool & switchFlag)2179 int32_t MMIService::GetTouchpadScrollSwitch(bool &switchFlag)
2180 {
2181     CALL_INFO_TRACE;
2182 #ifdef OHOS_BUILD_ENABLE_POINTER
2183     int32_t ret = delegateTasks_.PostSyncTask(
2184         [this, &switchFlag] {
2185             return this->ReadTouchpadScrollSwich(switchFlag);
2186         }
2187         );
2188     if (ret != RET_OK) {
2189         MMI_HILOGE("Get touchpad scroll switch failed, return:%{public}d", ret);
2190         return ret;
2191     }
2192 #endif // OHOS_BUILD_ENABLE_POINTER
2193     return RET_OK;
2194 }
2195 
SetTouchpadScrollDirection(bool state)2196 int32_t MMIService::SetTouchpadScrollDirection(bool state)
2197 {
2198     CALL_INFO_TRACE;
2199 #if defined OHOS_BUILD_ENABLE_POINTER
2200     int32_t ret = delegateTasks_.PostSyncTask(
2201         [state] {
2202             return ::OHOS::DelayedSingleton<MouseEventNormalize>::GetInstance()->SetTouchpadScrollDirection(state);
2203         }
2204         );
2205     if (ret != RET_OK) {
2206         MMI_HILOGE("Set touchpad scroll direction switch failed, return:%{public}d", ret);
2207         return ret;
2208     }
2209 #endif // OHOS_BUILD_ENABLE_POINTER
2210     return RET_OK;
2211 }
2212 
GetTouchpadScrollDirection(bool & state)2213 int32_t MMIService::GetTouchpadScrollDirection(bool &state)
2214 {
2215     CALL_INFO_TRACE;
2216 #ifdef OHOS_BUILD_ENABLE_POINTER
2217     int32_t ret = delegateTasks_.PostSyncTask(
2218         [this, &state] {
2219             return this->ReadTouchpadScrollDirection(state);
2220         }
2221         );
2222     if (ret != RET_OK) {
2223         MMI_HILOGE("Get touchpad scroll direction switch failed, return:%{public}d", ret);
2224         return ret;
2225     }
2226 #endif // OHOS_BUILD_ENABLE_POINTER
2227     return RET_OK;
2228 }
2229 
SetTouchpadTapSwitch(bool switchFlag)2230 int32_t MMIService::SetTouchpadTapSwitch(bool switchFlag)
2231 {
2232     CALL_INFO_TRACE;
2233 #if defined OHOS_BUILD_ENABLE_POINTER
2234     int32_t ret = delegateTasks_.PostSyncTask(
2235         [switchFlag] {
2236             return ::OHOS::DelayedSingleton<MouseEventNormalize>::GetInstance()->SetTouchpadTapSwitch(switchFlag);
2237         }
2238         );
2239     if (ret != RET_OK) {
2240         MMI_HILOGE("Set touchpad tap switch failed, return:%{public}d", ret);
2241         return ret;
2242     }
2243 #endif // OHOS_BUILD_ENABLE_POINTER
2244     return RET_OK;
2245 }
2246 
GetTouchpadTapSwitch(bool & switchFlag)2247 int32_t MMIService::GetTouchpadTapSwitch(bool &switchFlag)
2248 {
2249     CALL_INFO_TRACE;
2250 #ifdef OHOS_BUILD_ENABLE_POINTER
2251     int32_t ret = delegateTasks_.PostSyncTask(
2252         [this, &switchFlag] {
2253             return this->ReadTouchpadTapSwitch(switchFlag);
2254         }
2255         );
2256     if (ret != RET_OK) {
2257         MMI_HILOGE("Get touchpad tap switch failed, return:%{public}d", ret);
2258         return ret;
2259     }
2260 #endif // OHOS_BUILD_ENABLE_POINTER
2261     return RET_OK;
2262 }
2263 
SetTouchpadPointerSpeed(int32_t speed)2264 int32_t MMIService::SetTouchpadPointerSpeed(int32_t speed)
2265 {
2266     CALL_INFO_TRACE;
2267 #if defined OHOS_BUILD_ENABLE_POINTER
2268     int32_t ret = delegateTasks_.PostSyncTask(
2269         [speed] {
2270             return ::OHOS::DelayedSingleton<MouseEventNormalize>::GetInstance()->SetTouchpadPointerSpeed(speed);
2271         }
2272         );
2273     if (ret != RET_OK) {
2274         MMI_HILOGE("Set touchpad speed failed, return:%{public}d", ret);
2275         return ret;
2276     }
2277 #endif // OHOS_BUILD_ENABLE_POINTER
2278     return RET_OK;
2279 }
2280 
GetTouchpadPointerSpeed(int32_t & speed)2281 int32_t MMIService::GetTouchpadPointerSpeed(int32_t &speed)
2282 {
2283     CALL_INFO_TRACE;
2284 #ifdef OHOS_BUILD_ENABLE_POINTER
2285     int32_t ret = delegateTasks_.PostSyncTask(
2286         [this, &speed] {
2287             return this->ReadTouchpadPointerSpeed(speed);
2288         }
2289         );
2290     if (ret != RET_OK) {
2291         MMI_HILOGE("Get touchpad speed failed, return:%{public}d", ret);
2292         return ret;
2293     }
2294 #endif // OHOS_BUILD_ENABLE_POINTER
2295     return RET_OK;
2296 }
2297 
SetTouchpadPinchSwitch(bool switchFlag)2298 int32_t MMIService::SetTouchpadPinchSwitch(bool switchFlag)
2299 {
2300     CALL_INFO_TRACE;
2301 #if defined OHOS_BUILD_ENABLE_POINTER
2302     int32_t ret = delegateTasks_.PostSyncTask(
2303         [switchFlag] {
2304             return ::OHOS::DelayedSingleton<TouchEventNormalize>::GetInstance()->SetTouchpadPinchSwitch(switchFlag);
2305         }
2306         );
2307     if (ret != RET_OK) {
2308         MMI_HILOGE("Set touch pad pinch switch failed, return:%{public}d", ret);
2309         return ret;
2310     }
2311 #endif // OHOS_BUILD_ENABLE_POINTER
2312     return RET_OK;
2313 }
2314 
GetTouchpadPinchSwitch(bool & switchFlag)2315 int32_t MMIService::GetTouchpadPinchSwitch(bool &switchFlag)
2316 {
2317     CALL_INFO_TRACE;
2318 #ifdef OHOS_BUILD_ENABLE_POINTER
2319     int32_t ret = delegateTasks_.PostSyncTask(
2320         [this, &switchFlag] {
2321             return this->ReadTouchpadPinchSwitch(switchFlag);
2322         }
2323         );
2324     if (ret != RET_OK) {
2325         MMI_HILOGE("Get touch pad pinch switch failed, return:%{public}d", ret);
2326         return ret;
2327     }
2328 #endif // OHOS_BUILD_ENABLE_POINTER
2329     return RET_OK;
2330 }
2331 
SetTouchpadSwipeSwitch(bool switchFlag)2332 int32_t MMIService::SetTouchpadSwipeSwitch(bool switchFlag)
2333 {
2334     CALL_INFO_TRACE;
2335 #if defined OHOS_BUILD_ENABLE_POINTER
2336     int32_t ret = delegateTasks_.PostSyncTask(
2337         [switchFlag] {
2338             return ::OHOS::DelayedSingleton<TouchEventNormalize>::GetInstance()->SetTouchpadSwipeSwitch(switchFlag);
2339         }
2340         );
2341     if (ret != RET_OK) {
2342         MMI_HILOGE("Set touchpad swipe switch failed, return:%{public}d", ret);
2343         return ret;
2344     }
2345 #endif // OHOS_BUILD_ENABLE_POINTER
2346     return RET_OK;
2347 }
2348 
GetTouchpadSwipeSwitch(bool & switchFlag)2349 int32_t MMIService::GetTouchpadSwipeSwitch(bool &switchFlag)
2350 {
2351     CALL_INFO_TRACE;
2352 #ifdef OHOS_BUILD_ENABLE_POINTER
2353     int32_t ret = delegateTasks_.PostSyncTask(
2354         [this, &switchFlag] {
2355             return this->ReadTouchpadSwipeSwitch(switchFlag);
2356         }
2357         );
2358     if (ret != RET_OK) {
2359         MMI_HILOGE("Get touchpad swipe switch failed, return:%{public}d", ret);
2360         return ret;
2361     }
2362 #endif // OHOS_BUILD_ENABLE_POINTER
2363     return RET_OK;
2364 }
2365 
SetTouchpadRightClickType(int32_t type)2366 int32_t MMIService::SetTouchpadRightClickType(int32_t type)
2367 {
2368     CALL_INFO_TRACE;
2369 #if defined OHOS_BUILD_ENABLE_POINTER
2370     int32_t ret = delegateTasks_.PostSyncTask(
2371         [type] {
2372             return ::OHOS::DelayedSingleton<MouseEventNormalize>::GetInstance()->SetTouchpadRightClickType(type);
2373         }
2374         );
2375     if (ret != RET_OK) {
2376         MMI_HILOGE("Set touchpad right button menu type failed, return:%{public}d", ret);
2377         return ret;
2378     }
2379 #endif // OHOS_BUILD_ENABLE_POINTER
2380     return RET_OK;
2381 }
2382 
GetTouchpadRightClickType(int32_t & type)2383 int32_t MMIService::GetTouchpadRightClickType(int32_t &type)
2384 {
2385     CALL_INFO_TRACE;
2386 #ifdef OHOS_BUILD_ENABLE_POINTER
2387     int32_t ret = delegateTasks_.PostSyncTask(
2388         [this, &type] {
2389             return this->ReadTouchpadRightMenuType(type);
2390         }
2391         );
2392     if (ret != RET_OK) {
2393         MMI_HILOGE("Get touchpad right button menu type failed, return:%{public}d", ret);
2394         return ret;
2395     }
2396 #endif // OHOS_BUILD_ENABLE_POINTER
2397     return RET_OK;
2398 }
2399 
SetTouchpadRotateSwitch(bool rotateSwitch)2400 int32_t MMIService::SetTouchpadRotateSwitch(bool rotateSwitch)
2401 {
2402     CALL_INFO_TRACE;
2403 #if defined OHOS_BUILD_ENABLE_POINTER
2404     int32_t ret = delegateTasks_.PostSyncTask(
2405         [rotateSwitch] {
2406             return ::OHOS::DelayedSingleton<TouchEventNormalize>::GetInstance()->SetTouchpadRotateSwitch(rotateSwitch);
2407         }
2408         );
2409     if (ret != RET_OK) {
2410         MMI_HILOGE("Set touchpad rotate switch failed, ret:%{public}d", ret);
2411         return ret;
2412     }
2413 #endif // OHOS_BUILD_ENABLE_POINTER
2414     return RET_OK;
2415 }
2416 
GetTouchpadRotateSwitch(bool & rotateSwitch)2417 int32_t MMIService::GetTouchpadRotateSwitch(bool &rotateSwitch)
2418 {
2419     CALL_INFO_TRACE;
2420 #ifdef OHOS_BUILD_ENABLE_POINTER
2421     int32_t ret = delegateTasks_.PostSyncTask(
2422         [this, &rotateSwitch] {
2423             return this->ReadTouchpadRotateSwitch(rotateSwitch);
2424         }
2425         );
2426     if (ret != RET_OK) {
2427         MMI_HILOGE("Get touchpad rotate switch failed, ret:%{public}d", ret);
2428         return RET_ERR;
2429     }
2430 #endif // OHOS_BUILD_ENABLE_POINTER
2431     return RET_OK;
2432 }
2433 
SetTouchpadDoubleTapAndDragState(bool switchFlag)2434 int32_t MMIService::SetTouchpadDoubleTapAndDragState(bool switchFlag)
2435 {
2436     CALL_INFO_TRACE;
2437 #ifdef OHOS_BUILD_ENABLE_POINTER
2438     int32_t ret = delegateTasks_.PostSyncTask(
2439         [switchFlag] {
2440             return ::OHOS::DelayedSingleton<TouchEventNormalize>::GetInstance()->SetTouchpadDoubleTapAndDragState(
2441                 switchFlag);
2442         }
2443         );
2444     if (ret != RET_OK) {
2445         MMI_HILOGE("Failed to SetTouchpadDoubleTapAndDragState status, ret:%{public}d", ret);
2446         return ret;
2447     }
2448 #endif // OHOS_BUILD_ENABLE_POINTER
2449     return RET_OK;
2450 }
2451 
GetTouchpadDoubleTapAndDragState(bool & switchFlag)2452 int32_t MMIService::GetTouchpadDoubleTapAndDragState(bool &switchFlag)
2453 {
2454     CALL_INFO_TRACE;
2455 #ifdef OHOS_BUILD_ENABLE_POINTER
2456     int32_t ret = delegateTasks_.PostSyncTask(
2457         [this, &switchFlag] {
2458             return this->ReadTouchpadDoubleTapAndDragState(switchFlag);
2459         }
2460         );
2461     if (ret != RET_OK) {
2462         MMI_HILOGE("Failed to GetTouchpadDoubleTapAndDragState status, ret:%{public}d", ret);
2463         return ret;
2464     }
2465 #endif // OHOS_BUILD_ENABLE_POINTER
2466     return RET_OK;
2467 }
2468 
SetShieldStatus(int32_t shieldMode,bool isShield)2469 int32_t MMIService::SetShieldStatus(int32_t shieldMode, bool isShield)
2470 {
2471     CALL_INFO_TRACE;
2472 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
2473     int32_t ret = delegateTasks_.PostSyncTask(
2474         [this, shieldMode, isShield] {
2475             return sMsgHandler_.SetShieldStatus(shieldMode, isShield);
2476         }
2477         );
2478     if (ret != RET_OK) {
2479         MMI_HILOGE("Set shield event interception state failed, return %{public}d", ret);
2480         return ret;
2481     }
2482 #endif // OHOS_BUILD_ENABLE_KEYBOARD
2483     return RET_OK;
2484 }
2485 
GetShieldStatus(int32_t shieldMode,bool & isShield)2486 int32_t MMIService::GetShieldStatus(int32_t shieldMode, bool &isShield)
2487 {
2488     CALL_INFO_TRACE;
2489 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
2490     int32_t ret = delegateTasks_.PostSyncTask(
2491         [this, shieldMode, &isShield] {
2492             return sMsgHandler_.GetShieldStatus(shieldMode, isShield);
2493         }
2494         );
2495     if (ret != RET_OK) {
2496         MMI_HILOGE("Failed to set shield event interception status, ret:%{public}d", ret);
2497         return ret;
2498     }
2499 #endif // OHOS_BUILD_ENABLE_KEYBOARD
2500     return RET_OK;
2501 }
2502 
GetKeyState(std::vector<int32_t> & pressedKeys,std::map<int32_t,int32_t> & specialKeysState)2503 int32_t MMIService::GetKeyState(std::vector<int32_t> &pressedKeys, std::map<int32_t, int32_t> &specialKeysState)
2504 {
2505     CALL_DEBUG_ENTER;
2506 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
2507     int32_t ret = delegateTasks_.PostSyncTask(
2508         [this, &pressedKeys, &specialKeysState] {
2509             return this->OnGetKeyState(pressedKeys, specialKeysState);
2510         }
2511         );
2512     if (ret != RET_OK) {
2513         MMI_HILOGE("Get pressed keys failed, return:%{public}d", ret);
2514         return ret;
2515     }
2516 #endif // OHOS_BUILD_ENABLE_KEYBOARD
2517     return RET_OK;
2518 }
2519 
Authorize(bool isAuthorize)2520 int32_t MMIService::Authorize(bool isAuthorize)
2521 {
2522     CALL_DEBUG_ENTER;
2523     int32_t ret = delegateTasks_.PostSyncTask(
2524         [this, isAuthorize] {
2525             return this->OnAuthorize(isAuthorize);
2526         }
2527         );
2528     if (ret != RET_OK) {
2529         MMI_HILOGE("OnAuthorize failed, ret:%{public}d", ret);
2530         return ret;
2531     }
2532     return RET_OK;
2533 }
2534 
OnAuthorize(bool isAuthorize)2535 int32_t MMIService::OnAuthorize(bool isAuthorize)
2536 {
2537     return sMsgHandler_.OnAuthorize(isAuthorize);
2538 }
2539 
CancelInjection()2540 int32_t MMIService::CancelInjection()
2541 {
2542     CALL_DEBUG_ENTER;
2543     int32_t callPid = GetCallingPid();
2544     int32_t ret = delegateTasks_.PostSyncTask(
2545         [this, callPid] {
2546             return this->OnCancelInjection(callPid);
2547         }
2548         );
2549     if (ret != RET_OK) {
2550         MMI_HILOGE("OnCancelInjection failed, ret:%{public}d", ret);
2551         return ret;
2552     }
2553     return RET_OK;
2554 }
2555 
OnCancelInjection(int32_t callPid)2556 int32_t MMIService::OnCancelInjection(int32_t callPid)
2557 {
2558     return sMsgHandler_.OnCancelInjection(callPid);
2559 }
2560 
HasIrEmitter(bool & hasIrEmitter)2561 int32_t MMIService::HasIrEmitter(bool &hasIrEmitter)
2562 {
2563     CALL_DEBUG_ENTER;
2564     int32_t ret = delegateTasks_.PostSyncTask(
2565         [this, &hasIrEmitter] {
2566             return this->OnHasIrEmitter(hasIrEmitter);
2567         }
2568         );
2569     if (ret != RET_OK) {
2570         MMI_HILOGE("OnHasIrEmitter failed, ret:%{public}d", ret);
2571         return ret;
2572     }
2573     return RET_OK;
2574 }
2575 
GetInfraredFrequencies(std::vector<InfraredFrequency> & requencys)2576 int32_t MMIService::GetInfraredFrequencies(std::vector<InfraredFrequency>& requencys)
2577 {
2578     CALL_DEBUG_ENTER;
2579     int32_t ret = delegateTasks_.PostSyncTask(
2580         [this, &requencys] {
2581             return this->OnGetInfraredFrequencies(requencys);
2582         }
2583         );
2584     if (ret != RET_OK) {
2585         MMI_HILOGE("OnGetInfraredFrequencies failed, returnCode:%{public}d", ret);
2586         return ret;
2587     }
2588     return RET_OK;
2589 }
2590 
TransmitInfrared(int64_t number,std::vector<int64_t> & pattern)2591 int32_t MMIService::TransmitInfrared(int64_t number, std::vector<int64_t>& pattern)
2592 {
2593     CALL_DEBUG_ENTER;
2594     int32_t ret = delegateTasks_.PostSyncTask(
2595         [this, number, &pattern] {
2596             return this->OnTransmitInfrared(number, pattern);
2597         }
2598         );
2599     if (ret != RET_OK) {
2600         MMI_HILOGE("OnTransmitInfrared failed, returnCode:%{public}d", ret);
2601         return ret;
2602     }
2603     return RET_OK;
2604 }
2605 
OnHasIrEmitter(bool & hasIrEmitter)2606 int32_t MMIService::OnHasIrEmitter(bool &hasIrEmitter)
2607 {
2608     hasIrEmitter = false;
2609     return RET_OK;
2610 }
2611 
OnGetInfraredFrequencies(std::vector<InfraredFrequency> & frequencies)2612 int32_t MMIService::OnGetInfraredFrequencies(std::vector<InfraredFrequency> &frequencies)
2613 {
2614     MMI_HILOGI("start get infrared frequency");
2615     std::vector<InfraredFrequencyInfo> infos;
2616     InfraredEmitterController::GetInstance()->GetFrequencies(infos);
2617     for (auto &item : infos) {
2618         InfraredFrequency info;
2619         info.min_ = item.min_;
2620         info.max_ = item.max_;
2621         frequencies.push_back(info);
2622     }
2623     std::string context = "";
2624     int32_t size = static_cast<int32_t>(frequencies.size());
2625     for (int32_t i = 0; i < size; i++) {
2626         context = context + "frequencies[" + std::to_string(i) + "]. max=" + std::to_string(frequencies[i].max_) +
2627         ",min=" + std::to_string(frequencies[i].min_) + ";";
2628     }
2629     MMI_HILOGD("data from hdf context:%{public}s", context.c_str());
2630     return RET_OK;
2631 }
2632 
OnTransmitInfrared(int64_t infraredFrequency,std::vector<int64_t> & pattern)2633 int32_t MMIService::OnTransmitInfrared(int64_t infraredFrequency, std::vector<int64_t> &pattern)
2634 {
2635     std::string context = "infraredFrequency:" + std::to_string(infraredFrequency) + ";";
2636     int32_t size = static_cast<int32_t>(pattern.size());
2637     for (int32_t i = 0; i < size; i++) {
2638         context = context + "index:" + std::to_string(i) + ": pattern:" + std::to_string(pattern[i]) + ";";
2639     }
2640     InfraredEmitterController::GetInstance()->Transmit(infraredFrequency, pattern);
2641     MMI_HILOGI("TransmitInfrared para context:%{public}s", context.c_str());
2642     return RET_OK;
2643 }
2644 
SetPixelMapData(int32_t infoId,void * pixelMap)2645 int32_t MMIService::SetPixelMapData(int32_t infoId, void* pixelMap)
2646 {
2647     CALL_DEBUG_ENTER;
2648     CHKPR(pixelMap, ERROR_NULL_POINTER);
2649     int32_t ret = delegateTasks_.PostSyncTask(
2650         [this, infoId, pixelMap] {
2651             return sMsgHandler_.SetPixelMapData(infoId, pixelMap);
2652         }
2653         );
2654     if (ret != RET_OK) {
2655         MMI_HILOGE("Failed to set pixelmap, ret:%{public}d", ret);
2656         return ret;
2657     }
2658     return RET_OK;
2659 }
2660 
SetCurrentUser(int32_t userId)2661 int32_t MMIService::SetCurrentUser(int32_t userId)
2662 {
2663     CALL_DEBUG_ENTER;
2664     int32_t ret = delegateTasks_.PostSyncTask(
2665         [userId] {
2666             return ::OHOS::MMI::IInputWindowsManager::GetInstance()->SetCurrentUser(userId);
2667         }
2668         );
2669     if (ret != RET_OK) {
2670         MMI_HILOGE("Failed to set current user, ret:%{public}d", ret);
2671         return ret;
2672     }
2673     return RET_OK;
2674 }
2675 
AddVirtualInputDevice(std::shared_ptr<InputDevice> device,int32_t & deviceId)2676 int32_t MMIService::AddVirtualInputDevice(std::shared_ptr<InputDevice> device, int32_t &deviceId)
2677 {
2678     CALL_DEBUG_ENTER;
2679     CHKPR(device, ERROR_NULL_POINTER);
2680     int32_t ret = delegateTasks_.PostSyncTask(
2681         [device, &deviceId] {
2682             return ::OHOS::MMI::InputDeviceManager::GetInstance()->AddVirtualInputDevice(device, deviceId);
2683         }
2684         );
2685     if (ret != RET_OK) {
2686         MMI_HILOGE("AddVirtualInputDevice failed:%{public}d", ret);
2687     }
2688     return ret;
2689 }
2690 
RemoveVirtualInputDevice(int32_t deviceId)2691 int32_t MMIService::RemoveVirtualInputDevice(int32_t deviceId)
2692 {
2693     CALL_DEBUG_ENTER;
2694     int32_t ret = delegateTasks_.PostSyncTask(
2695         [deviceId] {
2696             return ::OHOS::MMI::InputDeviceManager::GetInstance()->RemoveVirtualInputDevice(deviceId);
2697         }
2698         );
2699     if (ret != RET_OK) {
2700         MMI_HILOGE("RemoveVirtualInputDevice failed:%{public}d", ret);
2701     }
2702     return ret;
2703 }
2704 
EnableHardwareCursorStats(bool enable)2705 int32_t MMIService::EnableHardwareCursorStats(bool enable)
2706 {
2707     CALL_DEBUG_ENTER;
2708 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
2709     int32_t pid = GetCallingPid();
2710     int32_t ret = delegateTasks_.PostSyncTask(
2711         [pid, enable] {
2712             return IPointerDrawingManager::GetInstance()->EnableHardwareCursorStats(pid, enable);
2713         }
2714         );
2715     if (ret != RET_OK) {
2716         MMI_HILOGE("Enable hardware cursor stats failed, ret:%{public}d", ret);
2717         return ret;
2718     }
2719 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
2720     return RET_OK;
2721 }
2722 
GetHardwareCursorStats(uint32_t & frameCount,uint32_t & vsyncCount)2723 int32_t MMIService::GetHardwareCursorStats(uint32_t &frameCount, uint32_t &vsyncCount)
2724 {
2725     CALL_DEBUG_ENTER;
2726 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
2727     int32_t pid = GetCallingPid();
2728     int32_t ret = delegateTasks_.PostSyncTask(
2729         [pid, &frameCount, &vsyncCount] {
2730             return IPointerDrawingManager::GetInstance()->GetHardwareCursorStats(pid, frameCount, vsyncCount);
2731         }
2732         );
2733     if (ret != RET_OK) {
2734         MMI_HILOGE("Get hardware cursor stats failed, ret:%{public}d", ret);
2735         return ret;
2736     }
2737 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
2738     return RET_OK;
2739 }
2740 
2741 #ifdef OHOS_BUILD_ENABLE_MAGICCURSOR
GetPointerSnapshot(void * pixelMapPtr)2742 int32_t MMIService::GetPointerSnapshot(void *pixelMapPtr)
2743 {
2744     CALL_INFO_TRACE;
2745 #if defined OHOS_BUILD_ENABLE_POINTER
2746     MMI_HILOGI("Get pointer snapshot from process(%{public}d)", GetCallingPid());
2747     int32_t ret = delegateTasks_.PostSyncTask(std::bind(
2748         std::bind(&IPointerDrawingManager::GetPointerSnapshot, IPointerDrawingManager::GetInstance(), pixelMapPtr)));
2749     if (ret != RET_OK) {
2750         MMI_HILOGE("Get the pointer snapshot failed, ret: %{public}d", ret);
2751         return ret;
2752     }
2753 #endif // OHOS_BUILD_ENABLE_POINTER
2754     return RET_OK;
2755 }
2756 #endif // OHOS_BUILD_ENABLE_MAGICCURSOR
2757 
2758 #ifdef OHOS_BUILD_ENABLE_ANCO
AncoAddChannel(sptr<IAncoChannel> channel)2759 int32_t MMIService::AncoAddChannel(sptr<IAncoChannel> channel)
2760 {
2761     int32_t ret = delegateTasks_.PostSyncTask([channel]() {
2762         return WIN_MGR->AncoAddChannel(channel);
2763     });
2764     if (ret != RET_OK) {
2765         MMI_HILOGE("AncoAddChannel fail, error:%{public}d", ret);
2766     }
2767     return ret;
2768 }
2769 
AncoRemoveChannel(sptr<IAncoChannel> channel)2770 int32_t MMIService::AncoRemoveChannel(sptr<IAncoChannel> channel)
2771 {
2772     int32_t ret = delegateTasks_.PostSyncTask([channel]() {
2773         return WIN_MGR->AncoRemoveChannel(channel);
2774     });
2775     if (ret != RET_OK) {
2776         MMI_HILOGE("AncoRemoveChannel fail, error:%{public}d", ret);
2777     }
2778     return ret;
2779 }
2780 #endif // OHOS_BUILD_ENABLE_ANCO
2781 
TransferBinderClientSrv(const sptr<IRemoteObject> & binderClientObject)2782 int32_t MMIService::TransferBinderClientSrv(const sptr<IRemoteObject> &binderClientObject)
2783 {
2784     CALL_DEBUG_ENTER;
2785     int32_t pid = GetCallingPid();
2786     int32_t ret =
2787         delegateTasks_.PostSyncTask(
2788             [this, pid, binderClientObject] {
2789                 return sMsgHandler_.OnTransferBinderClientSrv(binderClientObject, pid);
2790             }
2791         );
2792     MMI_HILOGI("TransferBinderClientSrv result:%{public}d", ret);
2793     return ret;
2794 }
2795 
CalculateFuntionRunningTime(std::function<void ()> func,const std::string & flag)2796 void MMIService::CalculateFuntionRunningTime(std::function<void()> func, const std::string &flag)
2797 {
2798     std::function<void (void *)> printLog = std::bind(&MMIService::PrintLog, this, flag, THREAD_BLOCK_TIMER_SPAN_S,
2799         getpid(), gettid());
2800     int32_t id = HiviewDFX::XCollie::GetInstance().SetTimer(flag, THREAD_BLOCK_TIMER_SPAN_S, printLog, nullptr,
2801         HiviewDFX::XCOLLIE_FLAG_NOOP);
2802     func();
2803     HiviewDFX::XCollie::GetInstance().CancelTimer(id);
2804 }
2805 
PrintLog(const std::string & flag,int32_t duration,int32_t pid,int32_t tid)2806 void MMIService::PrintLog(const std::string &flag, int32_t duration, int32_t pid, int32_t tid)
2807 {
2808     std::string dfxThreadBlockMsg { "MMIBlockTask name:" };
2809     dfxThreadBlockMsg += flag;
2810     dfxThreadBlockMsg += ", duration time:";
2811     dfxThreadBlockMsg += std::to_string(duration);
2812     dfxThreadBlockMsg += ", pid:";
2813     dfxThreadBlockMsg += std::to_string(pid);
2814     dfxThreadBlockMsg += ", tid:";
2815     dfxThreadBlockMsg += std::to_string(tid);
2816     MMI_HILOGW("DfxThreadBlockMsg:%{public}s", dfxThreadBlockMsg.c_str());
2817     OHOS::HiviewDFX::DfxDumpCatcher dumpCatcher;
2818     dumpCatcher.DumpCatch(pid, tid, dfxThreadBlockMsg, MAX_FRAME_NUMS, false);
2819 }
2820 
SkipPointerLayer(bool isSkip)2821 int32_t MMIService::SkipPointerLayer(bool isSkip)
2822 {
2823     CALL_INFO_TRACE;
2824 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
2825     int32_t ret = delegateTasks_.PostSyncTask(
2826         [isSkip] {
2827             return IPointerDrawingManager::GetInstance()->SkipPointerLayer(isSkip);
2828         }
2829         );
2830     if (ret != RET_OK) {
2831         MMI_HILOGE("Skip pointer layerfailed, return:%{public}d", ret);
2832         return ret;
2833     }
2834 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
2835     return RET_OK;
2836 }
2837 
GetIntervalSinceLastInput(int64_t & timeInterval)2838 int32_t MMIService::GetIntervalSinceLastInput(int64_t &timeInterval)
2839 {
2840     CALL_INFO_TRACE;
2841     int32_t ret = delegateTasks_.PostSyncTask(std::bind(&InputEventHandler::GetIntervalSinceLastInput,
2842         InputHandler, std::ref(timeInterval)));
2843     MMI_HILOGD("timeInterval:%{public}" PRId64, timeInterval);
2844     if (ret != RET_OK) {
2845         MMI_HILOGE("Failed to GetIntervalSinceLastInput, ret:%{public}d", ret);
2846     }
2847     return ret;
2848 }
2849 } // namespace MMI
2850 } // namespace OHOS
2851