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 <cstdlib>
24 #include "string_ex.h"
25 #ifdef OHOS_RSS_CLIENT
26 #include <unordered_map>
27 #endif // OHOS_RSS_CLIENT
28
29 #include "ability_manager_client.h"
30 #include "anr_manager.h"
31 #include "app_debug_listener.h"
32 #include "app_state_observer.h"
33 #include "device_event_monitor.h"
34 #include "dfx_define.h"
35 #include "dfx_dump_catcher.h"
36 #include "dfx_hisysevent.h"
37 #include "dfx_json_formatter.h"
38
39 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
40 #include "display_event_monitor.h"
41 #endif // OHOS_BUILD_ENABLE_KEYBOARD
42 #include "event_dump.h"
43 #include "event_statistic.h"
44 #include "event_log_helper.h"
45 #include "ffrt.h"
46 #ifdef OHOS_BUILD_ENABLE_FINGERSENSE_WRAPPER
47 #include "fingersense_wrapper.h"
48 #endif // OHOS_BUILD_ENABLE_FINGERSENSE_WRAPPER
49 #ifdef OHOS_BUILD_ENABLE_GESTURESENSE_WRAPPER
50 #include "gesturesense_wrapper.h"
51 #endif // OHOS_BUILD_ENABLE_GESTURESENSE_WRAPPER
52 #include "infrared_emitter_controller.h"
53 #include "input_device_manager.h"
54 #include "ipc_skeleton.h"
55 #include "i_input_windows_manager.h"
56 #include "i_pointer_drawing_manager.h"
57 #include "i_preference_manager.h"
58 #include "key_auto_repeat.h"
59 #include "key_command_handler.h"
60 #include "key_map_manager.h"
61 #ifdef SHORTCUT_KEY_MANAGER_ENABLED
62 #include "key_shortcut_manager.h"
63 #endif // SHORTCUT_KEY_MANAGER_ENABLED
64 #include "mmi_log.h"
65 #include "multimodal_input_connect_def_parcel.h"
66 #include "permission_helper.h"
67 #include "timer_manager.h"
68 #include "tokenid_kit.h"
69 #include "touch_event_normalize.h"
70 #include "touch_gesture_adapter.h"
71 #include "util.h"
72 #include "util_ex.h"
73 #include "watchdog_task.h"
74 #include "xcollie/watchdog.h"
75 #include "xcollie/xcollie.h"
76 #include "xcollie/xcollie_define.h"
77
78 #ifdef OHOS_RSS_CLIENT
79 #include "res_sched_client.h"
80 #include "res_type.h"
81 #include "system_ability_definition.h"
82 #endif // OHOS_RSS_CLIENT
83 #include "setting_datashare.h"
84 #ifdef OHOS_BUILD_ENABLE_ANCO
85 #include "app_mgr_client.h"
86 #include "running_process_info.h"
87 #endif // OHOS_BUILD_ENABLE_ANCO
88
89 #ifdef PLAYER_FRAMEWORK_EXISTS
90 #include "input_screen_capture_agent.h"
91 #endif // PLAYER_FRAMEWORK_EXISTS
92
93 #ifdef OHOS_BUILD_ENABLE_VKEYBOARD
94 #include "vkeyboard.h"
95 #endif // OHOS_BUILD_ENABLE_VKEYBOARD
96
97 #undef MMI_LOG_TAG
98 #define MMI_LOG_TAG "MMIService"
99 #undef MMI_LOG_DOMAIN
100 #define MMI_LOG_DOMAIN MMI_LOG_SERVER
101
102 namespace OHOS {
103 namespace MMI {
104 namespace {
105 std::mutex g_instanceMutex;
106 MMIService* g_MMIService;
107 const std::string DEF_INPUT_SEAT { "seat0" };
108 const std::string THREAD_NAME { "mmi-service" };
109 constexpr int32_t WATCHDOG_INTERVAL_TIME { 30000 };
110 [[ maybe_unused ]] constexpr int32_t WATCHDOG_DELAY_TIME { 40000 };
111 constexpr int32_t RELOAD_DEVICE_TIME { 2000 };
112 [[ maybe_unused ]] constexpr int32_t WATCHDOG_WARNTIME { 6000 };
113 [[ maybe_unused ]] constexpr int32_t WATCHDOG_BLOCKTIME { 3000 };
114 constexpr int32_t REMOVE_OBSERVER { -2 };
115 constexpr int32_t REPEAT_COUNT { 2 };
116 constexpr int32_t UNSUBSCRIBED { -1 };
117 constexpr int32_t UNOBSERVED { -1 };
118 constexpr int32_t SUBSCRIBED { 1 };
119 [[ maybe_unused ]] constexpr int32_t DISTRIBUTE_TIME { 1000 }; // 1000ms
120 constexpr int32_t COMMON_PARAMETER_ERROR { 401 };
121 constexpr size_t MAX_FRAME_NUMS { 100 };
122 constexpr int32_t THREAD_BLOCK_TIMER_SPAN_S { 3 };
123 constexpr int32_t PRINT_INTERVAL_TIME { 30000 };
124 const std::set<int32_t> g_keyCodeValueSet = {
125 KeyEvent::KEYCODE_FN, KeyEvent::KEYCODE_DPAD_UP, KeyEvent::KEYCODE_DPAD_DOWN, KeyEvent::KEYCODE_DPAD_LEFT,
126 KeyEvent::KEYCODE_DPAD_RIGHT, KeyEvent::KEYCODE_ALT_LEFT, KeyEvent::KEYCODE_ALT_RIGHT,
127 KeyEvent::KEYCODE_SHIFT_LEFT, KeyEvent::KEYCODE_SHIFT_RIGHT, KeyEvent::KEYCODE_TAB, KeyEvent::KEYCODE_ENTER,
128 KeyEvent::KEYCODE_DEL, KeyEvent::KEYCODE_MENU, KeyEvent::KEYCODE_PAGE_UP, KeyEvent::KEYCODE_PAGE_DOWN,
129 KeyEvent::KEYCODE_ESCAPE, KeyEvent::KEYCODE_FORWARD_DEL, KeyEvent::KEYCODE_CTRL_LEFT, KeyEvent::KEYCODE_CTRL_RIGHT,
130 KeyEvent::KEYCODE_CAPS_LOCK, KeyEvent::KEYCODE_SCROLL_LOCK, KeyEvent::KEYCODE_META_LEFT,
131 KeyEvent::KEYCODE_META_RIGHT, KeyEvent::KEYCODE_SYSRQ, KeyEvent::KEYCODE_BREAK, KeyEvent::KEYCODE_MOVE_HOME,
132 KeyEvent::KEYCODE_MOVE_END, KeyEvent::KEYCODE_INSERT, KeyEvent::KEYCODE_F1, KeyEvent::KEYCODE_F2,
133 KeyEvent::KEYCODE_F3, KeyEvent::KEYCODE_F4, KeyEvent::KEYCODE_F5, KeyEvent::KEYCODE_F6, KeyEvent::KEYCODE_F7,
134 KeyEvent::KEYCODE_F8, KeyEvent::KEYCODE_F9, KeyEvent::KEYCODE_F10, KeyEvent::KEYCODE_F11, KeyEvent::KEYCODE_F12,
135 KeyEvent::KEYCODE_NUM_LOCK
136 };
137 #ifdef OHOS_BUILD_ENABLE_ANCO
138 constexpr int32_t DEFAULT_USER_ID { 100 };
139 #endif // OHOS_BUILD_ENABLE_ANCO
140 #ifdef OHOS_BUILD_ENABLE_VKEYBOARD
141 const std::string DEVICE_TYPE_HPR { "HPR" };
142 const std::string PRODUCT_TYPE = OHOS::system::GetParameter("const.build.product", "HYM");
143 // Define vkeyboard functions from vendor
144 const std::string VKEYBOARD_PATH { "libvkeyboard.z.so" };
145 void* g_VKeyboardHandle = nullptr;
146 typedef void (*ALGORITHM_KEYDOWN_TYPE)(
147 double screenX, double screenY, int touchId, bool tipDown, string buttonName);
148 ALGORITHM_KEYDOWN_TYPE algorithm_keydown_ = nullptr;
149 typedef void (*ALGORITHM_KEYUP_TYPE)(
150 double screenX, double screenY, int touchId, bool tipDown, string buttonName);
151 ALGORITHM_KEYUP_TYPE algorithm_keyup_ = nullptr;
152 typedef int32_t (*GAUSSIANKEYBOARD_GETKEYCODEBYKEYNAME_TYPE)(string keyName);
153 GAUSSIANKEYBOARD_GETKEYCODEBYKEYNAME_TYPE gaussiankeyboard_getKeyCodeByKeyName_ = nullptr;
154 typedef int32_t (*GAUSSIANKEYBOARD_GETKEYCODEBYKEYNAMEANDSHIFT_TYPE)(string keyName, bool& useShift);
155 GAUSSIANKEYBOARD_GETKEYCODEBYKEYNAMEANDSHIFT_TYPE gaussiankeyboard_getKeyCodeByKeyNameAndShift_ = nullptr;
156 typedef void (*GAUSSIANKEYBOARD_UPDATEMOTIONSPACE_TYPE)(
157 string keyName, bool useShift, std::vector<int32_t>& pattern);
158 GAUSSIANKEYBOARD_UPDATEMOTIONSPACE_TYPE gaussiankeyboard_updateMotionSpace_ = nullptr;
159 typedef void (*GAUSSIANKEYBOARD_SETVKEYBOARDAREA_TYPE)(
160 double topLeftX, double topLeftY, double bottomRightX, double bottomRightY);
161 GAUSSIANKEYBOARD_SETVKEYBOARDAREA_TYPE gaussiankeyboard_setVKeyboardArea_ = nullptr;
162 typedef void (*BAYESIANENGINE_MAPTOUCHTOBUTTON_TYPE)(
163 double screenX, double screenY, int touchId, bool tipDown, string& buttonName,
164 long long timestamp, bool updateDynamicGaussian, vector<pair<string, double>>& sortedNegLogProb);
165 BAYESIANENGINE_MAPTOUCHTOBUTTON_TYPE bayesianengine_mapTouchToButton_ = nullptr;
166 // Return message type.
167 typedef int32_t (*STATEMACINEMESSAGQUEUE_GETMESSAGE_TYPE)(
168 string& buttonName, string& toggleButtonName, int& buttonMode, string& RestList);
169 STATEMACINEMESSAGQUEUE_GETMESSAGE_TYPE statemachineMessageQueue_getMessage_ = nullptr;
170 typedef void (*STATEMACINEMESSAGQUEUE_CLEARMESSAGE_TYPE)();
171 STATEMACINEMESSAGQUEUE_CLEARMESSAGE_TYPE statemachineMessageQueue_clearMessage_ = nullptr;
172 typedef bool (*GAUSSIANKEYBOARD_ISINSIDEVKEYBOARDAREA_TYPE)(double x, double y);
173 GAUSSIANKEYBOARD_ISINSIDEVKEYBOARDAREA_TYPE gaussiankeyboard_isInsideVKeyboardArea_ = nullptr;
174 typedef bool (*GAUSSIANKEYBOARD_ISVKEYBOARDVISIBLE_TYPE)();
175 GAUSSIANKEYBOARD_ISVKEYBOARDVISIBLE_TYPE gaussiankeyboard_isVKeyboardVisible_ = nullptr;
176 typedef bool (*ALGORITHM_ISKEYDOWNINKEYBOARD_TYPE)(int touchId);
177 ALGORITHM_ISKEYDOWNINKEYBOARD_TYPE algorithm_isKeyDownInKeyboard_ = nullptr;
178 std::vector<int32_t> g_VKeyDownSet;
179 std::unordered_set<int32_t> g_VKeyModifiersDownSet;
180 // Shared key event for key injection for printing.
181 std::shared_ptr<KeyEvent> g_VKeySharedKeyEvent { nullptr };
182 // Shared key event for UI rendering.
183 std::shared_ptr<KeyEvent> g_VKeySharedUIKeyEvent { nullptr };
184 #endif // OHOS_BUILD_ENABLE_VKEYBOARD
185 } // namespace
186
187 const bool REGISTER_RESULT = SystemAbility::MakeAndRegisterAbility(MMIService::GetInstance());
188
CheckDefineOutput(const char *fmt, Ts... args)189 template <class... Ts> void CheckDefineOutput(const char *fmt, Ts... args)
190 {
191 CHKPV(fmt);
192 char buf[MAX_PACKET_BUF_SIZE] = {};
193 int32_t ret = snprintf_s(buf, MAX_PACKET_BUF_SIZE, MAX_PACKET_BUF_SIZE - 1, fmt, args...);
194 if (ret == -1) {
195 KMSG_LOGE("Call snprintf_s failed.ret = %d", ret);
196 return;
197 }
198 KMSG_LOGI("%s", buf);
199 MMI_HILOGI("%{public}s", buf);
200 }
201
CheckDefine()202 static void CheckDefine()
203 {
204 CheckDefineOutput("ChkDefs:");
205 #ifdef OHOS_BUILD_ENABLE_POINTER_DRAWING
206 CheckDefineOutput("%-40s", "OHOS_BUILD_ENABLE_POINTER_DRAWING");
207 #endif // OHOS_BUILD_ENABLE_POINTER_DRAWING
208 #ifdef OHOS_BUILD_ENABLE_INTERCEPTOR
209 CheckDefineOutput("%-40s", "OHOS_BUILD_ENABLE_INTERCEPTOR");
210 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR
211 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
212 CheckDefineOutput("%-40s", "OHOS_BUILD_ENABLE_KEYBOARD");
213 #endif // OHOS_BUILD_ENABLE_KEYBOARD
214 #ifdef OHOS_BUILD_ENABLE_POINTER
215 CheckDefineOutput("%-40s", "OHOS_BUILD_ENABLE_POINTER");
216 #endif // OHOS_BUILD_ENABLE_POINTER
217 #ifdef OHOS_BUILD_ENABLE_TOUCH
218 CheckDefineOutput("%-40s", "OHOS_BUILD_ENABLE_TOUCH");
219 #endif // OHOS_BUILD_ENABLE_TOUCH
220 #ifdef OHOS_BUILD_ENABLE_MONITOR
221 CheckDefineOutput("%-40s", "OHOS_BUILD_ENABLE_MONITOR");
222 #endif // OHOS_BUILD_ENABLE_MONITOR
223 }
224
MMIService()225 MMIService::MMIService() : SystemAbility(MULTIMODAL_INPUT_CONNECT_SERVICE_ID, true) {}
226
~MMIService()227 MMIService::~MMIService()
228 {
229 if (g_MMIService != nullptr) {
230 g_MMIService = nullptr;
231 }
232 MMI_HILOGI("~MMIService");
233 }
234
GetInstance()235 MMIService* MMIService::GetInstance()
236 {
237 if (g_MMIService == nullptr) {
238 std::lock_guard<std::mutex> lock(g_instanceMutex);
239 if (g_MMIService == nullptr) {
240 MMI_HILOGI("New MMIService");
241 g_MMIService = new MMIService();
242 }
243 }
244 return g_MMIService;
245 }
246
AddEpoll(EpollEventType type, int32_t fd)247 int32_t MMIService::AddEpoll(EpollEventType type, int32_t fd)
248 {
249 if (type < EPOLL_EVENT_BEGIN || type >= EPOLL_EVENT_END) {
250 MMI_HILOGE("Invalid param type");
251 return RET_ERR;
252 }
253 if (fd < 0) {
254 MMI_HILOGE("Invalid param fd_");
255 return RET_ERR;
256 }
257 if (mmiFd_ < 0) {
258 MMI_HILOGE("Invalid param mmiFd_");
259 return RET_ERR;
260 }
261 auto eventData = std::make_shared<mmi_epoll_event>();
262 eventData->fd = fd;
263 eventData->event_type = type;
264 MMI_HILOGI("userdata:[fd:%{public}d, type:%{public}d]", eventData->fd, eventData->event_type);
265
266 struct epoll_event ev = {};
267 ev.events = EPOLLIN;
268 ev.data.fd = fd;
269 auto ret = EpollCtl(fd, EPOLL_CTL_ADD, ev, mmiFd_);
270 if (ret < 0) {
271 eventData = nullptr;
272 ev.data.ptr = nullptr;
273 return ret;
274 }
275 AddEpollEvent(fd, eventData);
276 return RET_OK;
277 }
278
DelEpoll(EpollEventType type, int32_t fd)279 int32_t MMIService::DelEpoll(EpollEventType type, int32_t fd)
280 {
281 if (type < EPOLL_EVENT_BEGIN || type >= EPOLL_EVENT_END) {
282 MMI_HILOGE("Invalid param type");
283 return RET_ERR;
284 }
285 if (fd < 0) {
286 MMI_HILOGE("Invalid param fd_");
287 return RET_ERR;
288 }
289 if (mmiFd_ < 0) {
290 MMI_HILOGE("Invalid param mmiFd_");
291 return RET_ERR;
292 }
293 struct epoll_event ev = {};
294 auto ret = EpollCtl(fd, EPOLL_CTL_DEL, ev, mmiFd_);
295 if (ret < 0) {
296 MMI_HILOGE("DelEpoll failed");
297 return ret;
298 }
299 return RET_OK;
300 }
301
IsRunning() const302 bool MMIService::IsRunning() const
303 {
304 return (state_ == ServiceRunningState::STATE_RUNNING);
305 }
306
InitLibinputService()307 bool MMIService::InitLibinputService()
308 {
309 if (!(libinputAdapter_.Init([] (void *event, int64_t frameTime) {
310 ::OHOS::DelayedSingleton<InputEventHandler>::GetInstance()->OnEvent(event, frameTime);
311 }
312 ))) {
313 MMI_HILOGE("Libinput init, bind failed");
314 return false;
315 }
316 auto inputFds = libinputAdapter_.GetInputFds();
317 for (auto fd : inputFds) {
318 auto ret = AddEpoll(EPOLL_EVENT_INPUT, fd);
319 if (ret < 0) {
320 MMI_HILOGE("AddEpoll error ret:%{public}d", ret);
321 EpollClose();
322 return false;
323 }
324 MMI_HILOGD("AddEpoll, epollfd:%{public}d, fd:%{public}d", mmiFd_, fd);
325 }
326 return true;
327 }
328
InitService()329 bool MMIService::InitService()
330 {
331 MMI_HILOGD("Server msg handler Init");
332 sMsgHandler_.Init(*this);
333 if (state_ != ServiceRunningState::STATE_NOT_START) {
334 MMI_HILOGE("Service running status is not enabled");
335 return false;
336 }
337 if (EpollCreate(MAX_EVENT_SIZE) < 0) {
338 MMI_HILOGE("Create epoll failed");
339 return false;
340 }
341 auto ret = AddEpoll(EPOLL_EVENT_SOCKET, epollFd_);
342 if (ret < 0) {
343 MMI_HILOGE("AddEpoll error ret:%{public}d", ret);
344 EpollClose();
345 return false;
346 }
347 state_ = ServiceRunningState::STATE_RUNNING;
348 if (!(Publish(this))) {
349 state_ = ServiceRunningState::STATE_NOT_START;
350 MMI_HILOGE("Service initialization failed");
351 EpollClose();
352 return false;
353 }
354 MMI_HILOGI("AddEpoll, epollfd:%{public}d, fd:%{public}d", mmiFd_, epollFd_);
355 return true;
356 }
357
InitDelegateTasks()358 bool MMIService::InitDelegateTasks()
359 {
360 CALL_DEBUG_ENTER;
361 if (!delegateTasks_.Init()) {
362 MMI_HILOGE("The delegate task init failed");
363 return false;
364 }
365 auto ret = AddEpoll(EPOLL_EVENT_ETASK, delegateTasks_.GetReadFd());
366 if (ret < 0) {
367 MMI_HILOGE("AddEpoll error ret:%{public}d", ret);
368 EpollClose();
369 return false;
370 }
371 std::function<int32_t(DTaskCallback)> fun = [this](DTaskCallback cb) -> int32_t {
372 return delegateTasks_.PostSyncTask(cb);
373 };
374 delegateInterface_ = std::make_shared<DelegateInterface>(fun);
375 delegateInterface_->Init();
376 MMI_HILOGI("AddEpoll, epollfd:%{public}d, fd:%{public}d", mmiFd_, delegateTasks_.GetReadFd());
377 return true;
378 }
379 __attribute__((no_sanitize("cfi")))
Init()380 int32_t MMIService::Init()
381 {
382 CheckDefine();
383 MMI_HILOGD("WindowsManager Init");
384 WIN_MGR->Init(*this);
385 MMI_HILOGD("NapProcess Init");
386 NapProcess::GetInstance()->Init(*this);
387 MMI_HILOGD("ANRManager Init");
388 ANRMgr->Init(*this);
389 MMI_HILOGI("PointerDrawingManager Init");
390 #ifdef OHOS_BUILD_ENABLE_POINTER
391 if (!IPointerDrawingManager::GetInstance()->Init()) {
392 MMI_HILOGE("Pointer draw init failed");
393 return POINTER_DRAW_INIT_FAIL;
394 }
395 #endif // OHOS_BUILD_ENABLE_POINTER
396 mmiFd_ = EpollCreate(MAX_EVENT_SIZE);
397 if (mmiFd_ < 0) {
398 MMI_HILOGE("Create epoll failed");
399 return EPOLL_CREATE_FAIL;
400 }
401 MMI_HILOGD("Input msg handler init");
402 InputHandler->Init(*this);
403 MMI_HILOGD("Libinput service init");
404 if (!InitLibinputService()) {
405 MMI_HILOGE("Libinput init failed");
406 return LIBINPUT_INIT_FAIL;
407 }
408 MMI_HILOGD("Init DelegateTasks init");
409 if (!InitDelegateTasks()) {
410 MMI_HILOGE("Delegate tasks init failed");
411 return ETASKS_INIT_FAIL;
412 }
413 SetRecvFun([this] (SessionPtr sess, NetPacket& pkt) {sMsgHandler_.OnMsgHandler(sess, pkt);});
414 KeyMapMgr->GetConfigKeyValue("default_keymap", KeyMapMgr->GetDefaultKeyId());
415 OHOS::system::SetParameter(INPUT_POINTER_DEVICES, "false");
416 if (!InitService()) {
417 MMI_HILOGE("Saservice init failed");
418 return SASERVICE_INIT_FAIL;
419 }
420 MMI_HILOGI("Set para input.pointer.device false");
421 return RET_OK;
422 }
423
424 #ifdef OHOS_BUILD_ENABLE_VKEYBOARD
IsEightFingersDown(int32_t pointerId, int32_t pointerAction)425 bool IsEightFingersDown(int32_t pointerId, int32_t pointerAction)
426 {
427 if (pointerAction == MMI::PointerEvent::POINTER_ACTION_UP) {
428 std::vector<int32_t>::iterator ite = std::find(g_VKeyDownSet.begin(), g_VKeyDownSet.end(), pointerId);
429 if (ite != g_VKeyDownSet.end()) {
430 g_VKeyDownSet.erase(ite);
431 }
432 } else if (pointerAction == MMI::PointerEvent::POINTER_ACTION_DOWN) {
433 if (std::find(g_VKeyDownSet.begin(), g_VKeyDownSet.end(), pointerId) == g_VKeyDownSet.end()) {
434 g_VKeyDownSet.push_back(pointerId);
435 }
436 }
437 const int32_t totalFingerNum = 8;
438 return g_VKeyDownSet.size() == totalFingerNum;
439 }
440
HandleKeyActionHelper(int32_t action, int32_t keyCode, OHOS::MMI::KeyEvent::KeyItem &item)441 void HandleKeyActionHelper(int32_t action, int32_t keyCode, OHOS::MMI::KeyEvent::KeyItem &item)
442 {
443 if (action == OHOS::MMI::KeyEvent::KEY_ACTION_DOWN) {
444 g_VKeySharedKeyEvent->AddPressedKeyItems(item);
445 }
446 if (action == OHOS::MMI::KeyEvent::KEY_ACTION_UP) {
447 std::optional<OHOS::MMI::KeyEvent::KeyItem> pressedKeyItem = g_VKeySharedKeyEvent->GetKeyItem(keyCode);
448 if (pressedKeyItem) {
449 item.SetDownTime(pressedKeyItem->GetDownTime());
450 } else {
451 MMI_HILOGW("VKeyboard find pressed key failed");
452 }
453 g_VKeySharedKeyEvent->RemoveReleasedKeyItems(item);
454 g_VKeySharedKeyEvent->AddPressedKeyItems(item);
455 }
456 }
457
458 // Ref: oh_input_manager.
459 // Receive: action and keyCode.
460 // send out modified global g_VKeySharedKeyEvent to the event normalizer.
HandleKeyInjectEventHelper(std::shared_ptr<EventNormalizeHandler> eventNormalizeHandler, int32_t action, int32_t keyCode)461 int32_t HandleKeyInjectEventHelper(std::shared_ptr<EventNormalizeHandler> eventNormalizeHandler,
462 int32_t action, int32_t keyCode)
463 {
464 MMI_HILOGI("VKeyboard HandleKeyInjectEventHelper, injectEvent");
465 if (keyCode < 0) {
466 MMI_HILOGE("VKeyboard keyCode is less 0, can not process");
467 return COMMON_PARAMETER_ERROR;
468 }
469 CHKPR(g_VKeySharedKeyEvent, ERROR_NULL_POINTER);
470 g_VKeySharedKeyEvent->ClearFlag();
471 if (g_VKeySharedKeyEvent->GetAction() == OHOS::MMI::KeyEvent::KEY_ACTION_UP) {
472 std::optional<OHOS::MMI::KeyEvent::KeyItem> preUpKeyItem = g_VKeySharedKeyEvent->GetKeyItem();
473 if (preUpKeyItem) {
474 g_VKeySharedKeyEvent->RemoveReleasedKeyItems(*preUpKeyItem);
475 } else {
476 MMI_HILOGE("VKeyboard the preUpKeyItem is nullopt");
477 }
478 }
479 int64_t time = OHOS::MMI::GetSysClockTime();
480 g_VKeySharedKeyEvent->SetActionTime(time);
481 g_VKeySharedKeyEvent->SetRepeat(true);
482 g_VKeySharedKeyEvent->SetKeyCode(keyCode);
483 bool isKeyPressed = false;
484 if (action == OHOS::MMI::KeyEvent::KEY_ACTION_DOWN) {
485 g_VKeySharedKeyEvent->SetAction(OHOS::MMI::KeyEvent::KEY_ACTION_DOWN);
486 g_VKeySharedKeyEvent->SetKeyAction(OHOS::MMI::KeyEvent::KEY_ACTION_DOWN);
487 isKeyPressed = true;
488 } else if (action == OHOS::MMI::KeyEvent::KEY_ACTION_UP) {
489 g_VKeySharedKeyEvent->SetAction(OHOS::MMI::KeyEvent::KEY_ACTION_UP);
490 g_VKeySharedKeyEvent->SetKeyAction(OHOS::MMI::KeyEvent::KEY_ACTION_UP);
491 isKeyPressed = false;
492 }
493 OHOS::MMI::KeyEvent::KeyItem item;
494 item.SetDownTime(time);
495 item.SetKeyCode(keyCode);
496 item.SetPressed(isKeyPressed);
497 HandleKeyActionHelper(action, keyCode, item);
498 g_VKeySharedKeyEvent->AddFlag(OHOS::MMI::InputEvent::EVENT_FLAG_SIMULATE);
499
500 eventNormalizeHandler->HandleKeyEvent(g_VKeySharedKeyEvent);
501 return RET_OK;
502 }
503
504 // @brief Send out combination key press.
505 // Note that toggleBtn may contain more than one modifiers, like Ctrl+Shift.
SendCombinationKeyPress(std::vector<int32_t>& toggleKeyCodes, int32_t triggerKeyCode)506 int32_t SendCombinationKeyPress(std::vector<int32_t>& toggleKeyCodes, int32_t triggerKeyCode)
507 {
508 std::shared_ptr<EventNormalizeHandler> eventNormalizeHandler = InputHandler->GetEventNormalizeHandler();
509 CHKPR(eventNormalizeHandler, ERROR_NULL_POINTER);
510 // Trigger all modifier(s), if not done already.
511 for (auto& toggleCode: toggleKeyCodes) {
512 if (g_VKeyModifiersDownSet.count(toggleCode) == 0) {
513 // Not exist, then trigger the modifier.
514 HandleKeyInjectEventHelper(eventNormalizeHandler, OHOS::MMI::KeyEvent::KEY_ACTION_DOWN,
515 toggleCode);
516 g_VKeyModifiersDownSet.insert(toggleCode);
517 }
518 }
519 // Trigger key.
520 HandleKeyInjectEventHelper(eventNormalizeHandler, OHOS::MMI::KeyEvent::KEY_ACTION_DOWN,
521 triggerKeyCode);
522 // Release key.
523 HandleKeyInjectEventHelper(eventNormalizeHandler, OHOS::MMI::KeyEvent::KEY_ACTION_UP,
524 triggerKeyCode);
525 return RET_OK;
526 }
527
528 // @brief Release (remove from key event handler) and reset visual state on UI.
SendKeyRelease(int32_t keyCode)529 int32_t SendKeyRelease(int32_t keyCode)
530 {
531 std::shared_ptr<EventNormalizeHandler> eventNormalizeHandler = InputHandler->GetEventNormalizeHandler();
532 CHKPR(eventNormalizeHandler, ERROR_NULL_POINTER);
533 // Release key.
534 HandleKeyInjectEventHelper(eventNormalizeHandler, OHOS::MMI::KeyEvent::KEY_ACTION_UP,
535 keyCode);
536 g_VKeyModifiersDownSet.erase(keyCode);
537 return RET_OK;
538 }
539
540 // @brief Print (inject key code) and reset visual state on UI.
SendKeyPress(int32_t keyCode)541 int32_t SendKeyPress(int32_t keyCode)
542 {
543 std::shared_ptr<EventNormalizeHandler> eventNormalizeHandler = InputHandler->GetEventNormalizeHandler();
544 CHKPR(eventNormalizeHandler, ERROR_NULL_POINTER);
545 // Trigger key.
546 HandleKeyInjectEventHelper(eventNormalizeHandler, OHOS::MMI::KeyEvent::KEY_ACTION_DOWN,
547 keyCode);
548 // Release key.
549 HandleKeyInjectEventHelper(eventNormalizeHandler, OHOS::MMI::KeyEvent::KEY_ACTION_UP,
550 keyCode);
551 g_VKeyModifiersDownSet.erase(keyCode);
552 return RET_OK;
553 }
554
555 // @brief Only toggle visual state on UI without changing key event handler.
ToggleKeyVisualState(std::string& keyName, int32_t keyCode, bool visualPressed)556 int32_t ToggleKeyVisualState(std::string& keyName, int32_t keyCode, bool visualPressed)
557 {
558 std::shared_ptr<EventNormalizeHandler> eventNormalizeHandler = InputHandler->GetEventNormalizeHandler();
559 CHKPR(eventNormalizeHandler, ERROR_NULL_POINTER);
560 g_VKeySharedUIKeyEvent->SetKeyAction(KeyEvent::KEY_ACTION_UNKNOWN);
561 // If this shared event's previous action is up, it means
562 // there is a key up event left in the pressed seq. remove it now.
563 if (g_VKeySharedUIKeyEvent->GetVKeyboardAction() == KeyEvent::VKeyboardAction::VKEY_UP) {
564 std::optional<OHOS::MMI::KeyEvent::KeyItem> preUpKeyItem = g_VKeySharedUIKeyEvent->GetKeyItem();
565 if (preUpKeyItem) {
566 g_VKeySharedUIKeyEvent->RemoveReleasedKeyItems(*preUpKeyItem);
567 } else {
568 MMI_HILOGE("VKeyboard the preUpKeyItem is nullopt");
569 }
570 }
571 int64_t time = OHOS::MMI::GetSysClockTime();
572 g_VKeySharedUIKeyEvent->SetActionTime(time);
573
574 KeyEvent::KeyItem keyItem;
575 keyItem.SetDownTime(time);
576 keyItem.SetKeyCode(keyCode);
577 keyItem.SetPressed(visualPressed);
578
579 g_VKeySharedUIKeyEvent->SetKeyCode(keyCode);
580 g_VKeySharedUIKeyEvent->SetKeyName(keyName);
581
582 if (visualPressed) {
583 g_VKeySharedUIKeyEvent->SetVKeyboardAction(KeyEvent::VKeyboardAction::VKEY_DOWN);
584 g_VKeySharedUIKeyEvent->AddPressedKeyItems(keyItem);
585 } else {
586 g_VKeySharedUIKeyEvent->SetVKeyboardAction(KeyEvent::VKeyboardAction::VKEY_UP);
587 // Get the correct down time from pressed keys (when it is down)
588 std::optional<OHOS::MMI::KeyEvent::KeyItem> pressedKeyItem = g_VKeySharedUIKeyEvent->GetKeyItem(keyCode);
589 if (pressedKeyItem) {
590 keyItem.SetDownTime(pressedKeyItem->GetDownTime());
591 } else {
592 MMI_HILOGW("VKeyboard find pressed key failed");
593 }
594 // Remove the old ones (down key item) and add the new one (up key item)
595 g_VKeySharedUIKeyEvent->RemoveReleasedKeyItems(keyItem);
596 g_VKeySharedUIKeyEvent->AddPressedKeyItems(keyItem);
597 }
598 eventNormalizeHandler->HandleKeyEvent(g_VKeySharedUIKeyEvent);
599 return RET_OK;
600 }
601
602 // Use temporary events for vk-UI related communications.
SendKeyboardAction(KeyEvent::VKeyboardAction action)603 int32_t SendKeyboardAction(KeyEvent::VKeyboardAction action)
604 {
605 std::shared_ptr<EventNormalizeHandler> eventNormalizeHandler = InputHandler->GetEventNormalizeHandler();
606 CHKPR(eventNormalizeHandler, ERROR_NULL_POINTER);
607 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
608 CHKPR(keyEvent, ERROR_NULL_POINTER);
609 int32_t keyEventId = 1234;
610 int32_t keyEventDeviceId = 99;
611 keyEvent->SetId(keyEventId);
612 keyEvent->SetDeviceId(keyEventDeviceId);
613 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_UNKNOWN);
614 keyEvent->SetVKeyboardAction(action);
615 KeyEvent::KeyItem keyItem;
616 keyEvent->AddKeyItem(keyItem);
617 eventNormalizeHandler->HandleKeyEvent(keyEvent);
618 return RET_OK;
619 }
620
PointerEventHandler(std::shared_ptr<PointerEvent> pointerEvent)621 int32_t PointerEventHandler(std::shared_ptr<PointerEvent> pointerEvent)
622 {
623 int32_t pointerAction = pointerEvent->GetPointerAction();
624 if (pointerAction != MMI::PointerEvent::POINTER_ACTION_UP &&
625 pointerAction != MMI::PointerEvent::POINTER_ACTION_DOWN &&
626 pointerAction != MMI::PointerEvent::POINTER_ACTION_MOVE) {
627 return 0;
628 }
629 // Set touch point
630 int32_t pointerId = pointerEvent->GetPointerId();
631 PointerEvent::PointerItem pointerItem;
632 if (!pointerEvent->GetPointerItem(pointerId, pointerItem)) {
633 MMI_HILOGE("VKeyboard can't find pointer item");
634 return RET_ERR;
635 }
636 // Note: make sure this logic happens before the range-checking logic.
637 // Check if pointer action is not POINTER_ACTION_MOVE and 8 fingers down
638 if (pointerAction != MMI::PointerEvent::POINTER_ACTION_MOVE && IsEightFingersDown(pointerId, pointerAction)) {
639 SendKeyboardAction(KeyEvent::VKeyboardAction::ACTIVATE_KEYBOARD);
640 return RET_OK;
641 }
642 double physicalX = pointerItem.GetDisplayXPos();
643 double physicalY = pointerItem.GetDisplayYPos();
644 // Note:
645 // for delete gestures, it must begin from the backspace key, but stops when the finger moves out of range.
646 // for pinch gestures, we allow out-of-range gestures (e.g., for floating -> full,
647 // it's okay if the fingers are moving out of the boundary during the gestures since the kbd is very small)
648 bool insideVKeyboardArea = gaussiankeyboard_isInsideVKeyboardArea_(physicalX, physicalY);
649 bool isVkeyboardVisible = gaussiankeyboard_isVKeyboardVisible_();
650 bool isTouchInVKeyboard = algorithm_isKeyDownInKeyboard_(pointerId);
651 // Note: during layout switch, it's possible that a continuous movement
652 // happens outside of keyboard (e.g., keyboard already switched)
653 // or happens when kbd is not visible (e.g., when the prev layout dismissed but new one hasn't shown yet).
654 // if (!isTouchInVKeyboard && (!isVkeyboardVisible || (!insideVKeyboardArea && !insideVTrackpadArea)))
655 if (!isTouchInVKeyboard && (!isVkeyboardVisible || !insideVKeyboardArea)) {
656 // no unhanded touch points AND (kbd not visible) OR (kbd visible and out of range).
657 // i.e., we still want to process the touch move outside of
658 // kbd range if at least one point is already on the kbd.
659 return RET_OK;
660 }
661
662 TOUCHPOINT tp;
663 tp.InKeyboard = insideVKeyboardArea;
664 // When track pad is integrated the tp should be set as tp.InTrackpad = insideVTrackPadArea;
665 tp.ScreenX = physicalX;
666 tp.ScreenY = physicalY;
667 tp.TouchId = pointerId;
668 // Note: Down & Move treated as down.
669 tp.TipDown = (pointerAction != MMI::PointerEvent::POINTER_ACTION_UP);
670
671 std::vector<pair<string, double>> sortedNegLogProb;
672 string buttonName = "";
673 // Note: only Down will update dynamic gaussian.
674 bool updateDynamicGaussian = (pointerAction == MMI::PointerEvent::POINTER_ACTION_DOWN);
675
676 // NOTE: we don't run MapTouchToButton for all touchpoints -- only if the touchpoints fall within the kbd range.
677 // for out-of-range points, we still continue to process potential out-of-range
678 // gestures (e.g., fingers move out of range during pinch or move up/down gesture)
679 // in such case, isTouchInVKeyboard == true means it origins from inside kbd so a cache has been found.
680 if (insideVKeyboardArea || isTouchInVKeyboard) {
681 // Need to add valid time here.
682 bayesianengine_mapTouchToButton_(tp.ScreenX, tp.ScreenY, tp.TouchId, tp.TipDown, buttonName,
683 0, updateDynamicGaussian, sortedNegLogProb);
684 tp.ButtonName = buttonName;
685 if (!tp.ButtonName.empty()) {
686 MMI_HILOGI("VKeyboard touch name %{private}s", buttonName.c_str());
687 } else {
688 MMI_HILOGE("VKeyboard button name null");
689 return RET_ERR;
690 }
691 }
692 if (pointerAction == MMI::PointerEvent::POINTER_ACTION_DOWN) {
693 algorithm_keydown_(tp.ScreenX, tp.ScreenY, tp.TouchId, tp.TipDown, tp.ButtonName);
694 if (insideVKeyboardArea) {
695 int32_t keyCode = gaussiankeyboard_getKeyCodeByKeyName_(buttonName);
696 if (keyCode >= 0) {
697 ToggleKeyVisualState(buttonName, keyCode, true);
698 } else {
699 MMI_HILOGW("VKeyboard PointerEventHandler, key code not found for %{private}s", buttonName.c_str());
700 }
701 }
702 } else if (pointerAction == MMI::PointerEvent::POINTER_ACTION_UP) {
703 algorithm_keyup_(tp.ScreenX, tp.ScreenY, tp.TouchId, tp.TipDown, tp.ButtonName);
704 int32_t keyCode = gaussiankeyboard_getKeyCodeByKeyName_(buttonName);
705 if (keyCode >= 0) {
706 SendKeyRelease(keyCode);
707 ToggleKeyVisualState(buttonName, keyCode, false);
708 } else {
709 MMI_HILOGW("VKeyboard PointerEventHandler, key code not found for %{private}s", buttonName.c_str());
710 }
711 } else {
712 // New touch move logic: turn to touch down to support gestures.
713 algorithm_keydown_(tp.ScreenX, tp.ScreenY, tp.TouchId, tp.TipDown, tp.ButtonName);
714 }
715
716 std::vector<int32_t> toggleKeyCodes;
717
718 while (true) {
719 string buttonName;
720 string toggleButtonName;
721 int buttonMode;
722 string restList;
723 StateMachineMessageType type = (StateMachineMessageType)statemachineMessageQueue_getMessage_(
724 buttonName, toggleButtonName, buttonMode, restList);
725 if (type == StateMachineMessageType::NoMessage) {
726 break;
727 }
728 switch (type) {
729 case StateMachineMessageType::KeyPressed: {
730 // See if this key can be directly printed or not.
731 bool useShift = false;
732 int32_t code = gaussiankeyboard_getKeyCodeByKeyNameAndShift_(buttonName, useShift);
733 if (code >= 0) {
734 if (!useShift) {
735 SendKeyPress(code);
736 } else {
737 toggleKeyCodes.clear();
738 toggleKeyCodes.push_back(KeyEvent::KEYCODE_SHIFT_LEFT);
739 SendCombinationKeyPress(toggleKeyCodes, code);
740 // If this key is triggered with use shift ON, then it shall be resumed after use.
741 SendKeyRelease(KeyEvent::KEYCODE_SHIFT_LEFT);
742 }
743 } else {
744 MMI_HILOGW("VKeyboard key code not found.");
745 }
746 break;
747 }
748 case StateMachineMessageType::ButtonSound: {
749 break;
750 }
751 case StateMachineMessageType::ResetButtonColor: {
752 SendKeyboardAction(KeyEvent::VKeyboardAction::RESET_BUTTON_COLOR);
753 break;
754 }
755 case StateMachineMessageType::CombinationKeyPressed: {
756 toggleKeyCodes.clear();
757 std::string remainStr = toggleButtonName;
758 int32_t toggleCode(-1), triggerCode(-1);
759 while (remainStr.find(';') != std::string::npos) {
760 // still has more than one
761 size_t pos = remainStr.find(';');
762 toggleCode = gaussiankeyboard_getKeyCodeByKeyName_(remainStr.substr(0, pos));
763 if (toggleCode >= 0) {
764 toggleKeyCodes.push_back(toggleCode);
765 }
766 remainStr = remainStr.substr(pos + 1);
767 }
768 // Add the last piece.
769 toggleCode = gaussiankeyboard_getKeyCodeByKeyName_(remainStr);
770 if (toggleCode >= 0) {
771 toggleKeyCodes.push_back(toggleCode);
772 }
773 // Trigger code:
774 triggerCode = gaussiankeyboard_getKeyCodeByKeyName_(buttonName);
775 if (toggleKeyCodes.size() > 0 && triggerCode >= 0) {
776 // valid toggle key code(s) and trigger key code
777 SendCombinationKeyPress(toggleKeyCodes, triggerCode);
778 } else {
779 MMI_HILOGW("VKeyboard combination keycodes not found for %{private}s + %{private}s",
780 toggleButtonName.c_str(), buttonName.c_str());
781 }
782 break;
783 }
784 case StateMachineMessageType::BackSwipeLeft: {
785 // Send Shift+Left.
786 toggleKeyCodes.clear();
787 toggleKeyCodes.push_back(KeyEvent::KEYCODE_SHIFT_LEFT);
788 SendCombinationKeyPress(toggleKeyCodes, KeyEvent::KEYCODE_DPAD_LEFT);
789 break;
790 }
791 case StateMachineMessageType::BackSwipeRight: {
792 // Send Shift+Right
793 toggleKeyCodes.clear();
794 toggleKeyCodes.push_back(KeyEvent::KEYCODE_SHIFT_LEFT);
795 SendCombinationKeyPress(toggleKeyCodes, KeyEvent::KEYCODE_DPAD_RIGHT);
796 break;
797 }
798 case StateMachineMessageType::BackspaceSwipeRelease: {
799 SendKeyRelease(KeyEvent::KEYCODE_SHIFT_LEFT);
800 int swipeCharCounter(buttonMode);
801 if (swipeCharCounter < 0) {
802 // Backspace character
803 SendKeyPress(KeyEvent::KEYCODE_DEL);
804 } else if (swipeCharCounter > 0) {
805 // del character. (note: actually there is no difference when the text is selected)
806 SendKeyPress(KeyEvent::KEYCODE_FORWARD_DEL);
807 } else {
808 // No char deleted. just release the shift if it was pressed down before.
809 MMI_HILOGI("VKeyboard SendSwipeDeleteMsg, swipeCharCounter = %{private}d. Release Shift.",
810 swipeCharCounter);
811 }
812 break;
813 }
814 case StateMachineMessageType::SwitchLayout: {
815 int gestureId = buttonMode;
816 auto gestureType = static_cast<VGestureMode>(gestureId);
817 // Note: this LayoutAction is used within backend algorithm,
818 // which may be different from the protocol with front end (VKeyboardAction)
819 switch (gestureType) {
820 case VGestureMode::TWO_HANDS_UP: {
821 MMI_HILOGI("VKeyboard 8-finger move up to enable trackpad (not linked yet).");
822 // If we are sure that only full keyboard is kept, then we no longer need this move up/down gesture.
823 break;
824 }
825 case VGestureMode::TWO_HANDS_DOWN: {
826 MMI_HILOGI("VKeyboard 8-finger move down to disable trackpad (not linked yet).");
827 // If we are sure that only full keyboard is kept, then we no longer need this move up/down gesture.
828 break;
829 }
830 case VGestureMode::TWO_HANDS_INWARDS: {
831 MMI_HILOGI("VKeyboard 2-finger move inwards to switch to floating kbd.");
832 SendKeyboardAction(KeyEvent::VKeyboardAction::TWO_FINGERS_IN);
833 break;
834 }
835 case VGestureMode::TWO_HANDS_OUTWARDS: {
836 MMI_HILOGI("VKeyboard 2-finger move outwards to switch to standard/full kbd.");
837 // Note: if we have both standard and full kdb, then the front
838 // end shall track and resume user's previous choice of layout.
839 SendKeyboardAction(KeyEvent::VKeyboardAction::TWO_FINGERS_OUT);
840 break;
841 }
842 default: {
843 // other gestures not implemented/supported yet.
844 MMI_HILOGW("VKeyboard gesture not implemented or supported, gestureId: %{private}d", gestureId);
845 }
846 }
847 SendKeyboardAction(KeyEvent::VKeyboardAction::RESET_BUTTON_COLOR);
848 break;
849 }
850 default:
851 break;
852 }
853 }
854 return RET_OK;
855 }
856 #endif // OHOS_BUILD_ENABLE_VKEYBOARD
857
OnStart()858 void MMIService::OnStart()
859 {
860 CHK_PID_AND_TID();
861 int32_t ret = Init();
862 CHKNOKRV(ret, "Init mmi_service failed");
863 MMI_HILOGD("Started successfully");
864 AddReloadDeviceTimer();
865 t_ = std::thread([this] {this->OnThread();});
866 pthread_setname_np(t_.native_handle(), THREAD_NAME.c_str());
867 eventMonitorThread_ = std::thread(&EventStatistic::WriteEventFile);
868 pthread_setname_np(eventMonitorThread_.native_handle(), "event-monitor");
869 #ifdef OHOS_RSS_CLIENT
870 MMI_HILOGI("Add system ability listener start");
871 AddSystemAbilityListener(RES_SCHED_SYS_ABILITY_ID);
872 MMI_HILOGI("Add system ability listener success");
873 #endif // OHOS_RSS_CLIENT
874 #if defined(OHOS_BUILD_ENABLE_FINGERSENSE_WRAPPER) && defined(OHOS_BUILD_ENABLE_KEYBOARD)
875 FINGERSENSE_WRAPPER->InitFingerSenseWrapper();
876 MMI_HILOGI("Add system ability listener start");
877 AddSystemAbilityListener(COMMON_EVENT_SERVICE_ID);
878 MMI_HILOGI("Add system ability listener success");
879 AddSystemAbilityListener(RENDER_SERVICE);
880 DISPLAY_MONITOR->InitCommonEventSubscriber();
881 #endif // OHOS_BUILD_ENABLE_FINGERSENSE_WRAPPER && OHOS_BUILD_ENABLE_KEYBOARD
882 #ifdef OHOS_BUILD_ENABLE_GESTURESENSE_WRAPPER
883 GESTURESENSE_WRAPPER->InitGestureSenseWrapper();
884 #endif // OHOS_BUILD_ENABLE_GESTURESENSE_WRAPPER
885 MMI_HILOGI("Add app manager service listener start");
886 AddSystemAbilityListener(APP_MGR_SERVICE_ID);
887 APP_OBSERVER_MGR->InitAppStateObserver();
888 MMI_HILOGI("Add app manager service listener end");
889 AddAppDebugListener();
890 AddSystemAbilityListener(DISPLAY_MANAGER_SERVICE_SA_ID);
891 #ifdef OHOS_BUILD_ENABLE_ANCO
892 InitAncoUds();
893 #endif // OHOS_BUILD_ENABLE_ANCO
894 #ifdef OHOS_BUILD_ENABLE_VKEYBOARD
895 isHPR_ = PRODUCT_TYPE == DEVICE_TYPE_HPR;
896 if (isHPR_) {
897 DelegateInterface::HandlerSummary summary = {};
898 summary.handlerName = "VKeyboard";
899 summary.eventType = HANDLE_EVENT_TYPE_POINTER;
900 summary.mode = HandlerMode::SYNC;
901 summary.priority = 0;
902 summary.deviceTags = 0;
903 summary.cb = PointerEventHandler;
904 delegateInterface_->AddHandler(InputHandlerType::MONITOR, summary);
905
906 // Initialize vkeyboard handler
907 g_VKeyboardHandle = dlopen(VKEYBOARD_PATH.c_str(), RTLD_NOW);
908 if (g_VKeyboardHandle != nullptr) {
909 algorithm_keydown_ = (ALGORITHM_KEYDOWN_TYPE)dlsym(g_VKeyboardHandle, "AlgorithmKeyDown");
910 algorithm_keyup_ = (ALGORITHM_KEYUP_TYPE)dlsym(g_VKeyboardHandle, "AlgorithmKeyUp");
911 gaussiankeyboard_getKeyCodeByKeyName_ = (GAUSSIANKEYBOARD_GETKEYCODEBYKEYNAME_TYPE)dlsym(
912 g_VKeyboardHandle, "GaussianKeyboardGetKeyCodeByKeyName");
913 gaussiankeyboard_getKeyCodeByKeyNameAndShift_ = (GAUSSIANKEYBOARD_GETKEYCODEBYKEYNAMEANDSHIFT_TYPE)dlsym(
914 g_VKeyboardHandle, "GaussianKeyboardGetKeyCodeByKeyNameAndShift");
915 gaussiankeyboard_updateMotionSpace_ = (GAUSSIANKEYBOARD_UPDATEMOTIONSPACE_TYPE)dlsym(
916 g_VKeyboardHandle, "GaussianKeyboardUpdateMotionSpace");
917 gaussiankeyboard_setVKeyboardArea_ = (GAUSSIANKEYBOARD_SETVKEYBOARDAREA_TYPE)dlsym(
918 g_VKeyboardHandle, "GaussianKeyboardSetVKeyboardArea");
919 bayesianengine_mapTouchToButton_ = (BAYESIANENGINE_MAPTOUCHTOBUTTON_TYPE)dlsym(
920 g_VKeyboardHandle, "BayesianEngineMapTouchToButton");
921 statemachineMessageQueue_getMessage_ = (STATEMACINEMESSAGQUEUE_GETMESSAGE_TYPE)dlsym(
922 g_VKeyboardHandle, "StateMachineMessageQueueGetMessage");
923 gaussiankeyboard_isInsideVKeyboardArea_ = (GAUSSIANKEYBOARD_ISINSIDEVKEYBOARDAREA_TYPE)dlsym(
924 g_VKeyboardHandle, "GaussianKeyboardIsInsideVKeyboardArea");
925 gaussiankeyboard_isVKeyboardVisible_ = (GAUSSIANKEYBOARD_ISVKEYBOARDVISIBLE_TYPE)dlsym(
926 g_VKeyboardHandle, "GaussianKeyboardIsVKeyboardVisible");
927 algorithm_isKeyDownInKeyboard_ = (ALGORITHM_ISKEYDOWNINKEYBOARD_TYPE)dlsym(
928 g_VKeyboardHandle, "AlgorithmIsKeyDownInKeyboard");
929 }
930 }
931 #endif // OHOS_BUILD_ENABLE_VKEYBOARD
932 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
933 IPointerDrawingManager::GetInstance()->InitPointerObserver();
934 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
935 InitPreferences();
936 TimerMgr->AddTimer(WATCHDOG_INTERVAL_TIME, -1, [this]() {
937 MMI_HILOGI("Set thread status flag to true");
938 threadStatusFlag_ = true;
939 });
940 [[ maybe_unused ]] auto taskFunc = [this]() {
941 if (threadStatusFlag_) {
942 MMI_HILOGI("Set thread status flag to false");
943 threadStatusFlag_ = false;
944 } else {
945 MMI_HILOGI("Mmi-server Timeout");
946 }
947 };
948 MMI_HILOGI("Run periodical task success");
949 InitPrintClientInfo();
950 }
951
OnStop()952 void MMIService::OnStop()
953 {
954 CHK_PID_AND_TID();
955 UdsStop();
956 libinputAdapter_.Stop();
957 state_ = ServiceRunningState::STATE_NOT_START;
958 #ifdef OHOS_RSS_CLIENT
959 MMI_HILOGI("Remove system ability listener start");
960 RemoveSystemAbilityListener(RES_SCHED_SYS_ABILITY_ID);
961 MMI_HILOGI("Remove system ability listener success");
962 #endif // OHOS_RSS_CLIENT
963 #ifdef OHOS_BUILD_ENABLE_FINGERSENSE_WRAPPER
964 RemoveSystemAbilityListener(COMMON_EVENT_SERVICE_ID);
965 #endif // OHOS_BUILD_ENABLE_FINGERSENSE_WRAPPER
966 RemoveSystemAbilityListener(APP_MGR_SERVICE_ID);
967 RemoveSystemAbilityListener(RENDER_SERVICE);
968 RemoveAppDebugListener();
969 RemoveSystemAbilityListener(DISPLAY_MANAGER_SERVICE_SA_ID);
970 #ifdef OHOS_BUILD_ENABLE_ANCO
971 StopAncoUds();
972 #endif // OHOS_BUILD_ENABLE_ANCO
973 }
974
AddAppDebugListener()975 void MMIService::AddAppDebugListener()
976 {
977 CALL_DEBUG_ENTER;
978 appDebugListener_ = AppDebugListener::GetInstance();
979 auto errCode =
980 AAFwk::AbilityManagerClient::GetInstance()->RegisterAppDebugListener(appDebugListener_);
981 if (errCode != RET_OK) {
982 MMI_HILOGE("Call RegisterAppDebugListener failed, errCode:%{public}d", errCode);
983 }
984 }
985
RemoveAppDebugListener()986 void MMIService::RemoveAppDebugListener()
987 {
988 CALL_DEBUG_ENTER;
989 CHKPV(appDebugListener_);
990 auto errCode =
991 AAFwk::AbilityManagerClient::GetInstance()->UnregisterAppDebugListener(appDebugListener_);
992 if (errCode != RET_OK) {
993 MMI_HILOGE("Call UnregisterAppDebugListener failed, errCode:%{public}d", errCode);
994 }
995 }
996
AllocSocketFd(const std::string &programName, const int32_t moduleType, int32_t &toReturnClientFd, int32_t &tokenType)997 int32_t MMIService::AllocSocketFd(const std::string &programName, const int32_t moduleType, int32_t &toReturnClientFd,
998 int32_t &tokenType)
999 {
1000 toReturnClientFd = IMultimodalInputConnect::INVALID_SOCKET_FD;
1001 int32_t serverFd = IMultimodalInputConnect::INVALID_SOCKET_FD;
1002 int32_t pid = GetCallingPid();
1003 int32_t uid = GetCallingUid();
1004 MMI_HILOGI("Enter, programName:%{public}s, moduleType:%{public}d, pid:%{public}d",
1005 programName.c_str(), moduleType, pid);
1006 int32_t ret = delegateTasks_.PostSyncTask(
1007 [this, &programName, moduleType, uid, pid, &serverFd, &toReturnClientFd, &tokenType] {
1008 return this->AddSocketPairInfo(programName, moduleType, uid, pid, serverFd, toReturnClientFd, tokenType);
1009 }
1010 );
1011 DfxHisysevent::ClientConnectData data = {
1012 .pid = pid,
1013 .uid = uid,
1014 .moduleType = moduleType,
1015 .programName = programName,
1016 .serverFd = serverFd
1017 };
1018 if (ret != RET_OK) {
1019 MMI_HILOGE("Call AddSocketPairInfo failed, return:%{public}d", ret);
1020 DfxHisysevent::OnClientConnect(data, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT);
1021 return ret;
1022 }
1023 MMI_HILOGIK("Leave, programName:%{public}s, moduleType:%{public}d, alloc success", programName.c_str(),
1024 moduleType);
1025 DfxHisysevent::OnClientConnect(data, OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR);
1026 return RET_OK;
1027 }
1028
AddInputEventFilter(sptr<IEventFilter> filter, int32_t filterId, int32_t priority, uint32_t deviceTags)1029 int32_t MMIService::AddInputEventFilter(sptr<IEventFilter> filter, int32_t filterId, int32_t priority,
1030 uint32_t deviceTags)
1031 {
1032 CALL_INFO_TRACE;
1033 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH) || defined(OHOS_BUILD_ENABLE_KEYBOARD)
1034 CHKPR(filter, ERROR_NULL_POINTER);
1035 int32_t clientPid = GetCallingPid();
1036 int32_t ret = delegateTasks_.PostSyncTask(
1037 [this, filter, filterId, priority, deviceTags, clientPid] {
1038 return sMsgHandler_.AddInputEventFilter(filter, filterId, priority, deviceTags, clientPid);
1039 }
1040 );
1041 if (ret != RET_OK) {
1042 MMI_HILOGE("Add event filter failed, return:%{public}d", ret);
1043 return ret;
1044 }
1045 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH || OHOS_BUILD_ENABLE_KEYBOARD
1046 return RET_OK;
1047 }
1048
RemoveInputEventFilter(int32_t filterId)1049 int32_t MMIService::RemoveInputEventFilter(int32_t filterId)
1050 {
1051 CALL_INFO_TRACE;
1052 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH) || defined(OHOS_BUILD_ENABLE_KEYBOARD)
1053 int32_t clientPid = GetCallingPid();
1054 int32_t ret = delegateTasks_.PostSyncTask(
1055 [this, filterId, clientPid] {
1056 return sMsgHandler_.RemoveInputEventFilter(filterId, clientPid);
1057 }
1058 );
1059 if (ret != RET_OK) {
1060 MMI_HILOGE("Remove event filter failed, return:%{public}d", ret);
1061 return ret;
1062 }
1063 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH || OHOS_BUILD_ENABLE_KEYBOARD
1064 return RET_OK;
1065 }
1066
OnConnected(SessionPtr s)1067 void MMIService::OnConnected(SessionPtr s)
1068 {
1069 CHKPV(s);
1070 MMI_HILOGI("fd:%{public}d", s->GetFd());
1071 #ifdef OHOS_BUILD_ENABLE_ANCO
1072 if (s->GetProgramName() != SHELL_ASSISTANT) {
1073 return;
1074 }
1075 auto appMgrClient = DelayedSingleton<AppExecFwk::AppMgrClient>::GetInstance();
1076 if (appMgrClient == nullptr) {
1077 return;
1078 }
1079 int32_t userid = WIN_MGR->GetCurrentUserId();
1080 if (userid < 0) {
1081 userid = DEFAULT_USER_ID;
1082 }
1083 std::vector<AppExecFwk::RunningProcessInfo> info;
1084 appMgrClient->GetProcessRunningInfosByUserId(info, userid);
1085 for (auto &item : info) {
1086 if (item.bundleNames.empty()) {
1087 continue;
1088 }
1089 if (SHELL_ASSISTANT == item.bundleNames[0].c_str()) {
1090 MMI_HILOGW("Record client processes pid %{public}d", item.pid_);
1091 shellAssitentPid_ = item.pid_;
1092 }
1093 }
1094 #endif // OHOS_BUILD_ENABLE_ANCO
1095 }
1096
OnDisconnected(SessionPtr s)1097 void MMIService::OnDisconnected(SessionPtr s)
1098 {
1099 CHKPV(s);
1100 MMI_HILOGW("Enter, session desc:%{public}s, fd:%{public}d", s->GetDescript().c_str(), s->GetFd());
1101 auto ret = RemoveInputEventFilter(-1);
1102 if (ret != RET_OK) {
1103 MMI_HILOGF("Remove all filter failed, ret:%{public}d", ret);
1104 }
1105 #ifdef OHOS_BUILD_ENABLE_ANCO
1106 if (s->GetProgramName() == SHELL_ASSISTANT && shellAssitentPid_ == s->GetPid()) {
1107 MMI_HILOGW("Clean all shell windows pid: %{public}d", s->GetPid());
1108 shellAssitentPid_ = -1;
1109 IInputWindowsManager::GetInstance()->CleanShellWindowIds();
1110 }
1111 #endif // OHOS_BUILD_ENABLE_ANCO
1112 #ifdef OHOS_BUILD_ENABLE_POINTER
1113 IPointerDrawingManager::GetInstance()->DeletePointerVisible(s->GetPid());
1114 #endif // OHOS_BUILD_ENABLE_POINTER
1115 }
1116
SetMouseScrollRows(int32_t rows)1117 int32_t MMIService::SetMouseScrollRows(int32_t rows)
1118 {
1119 CALL_INFO_TRACE;
1120 #if defined OHOS_BUILD_ENABLE_POINTER
1121 int32_t ret = delegateTasks_.PostSyncTask(
1122 [rows] {
1123 return ::OHOS::DelayedSingleton<MouseEventNormalize>::GetInstance()->SetMouseScrollRows(rows);
1124 }
1125 );
1126 if (ret != RET_OK) {
1127 MMI_HILOGE("Set the number of mouse scrolling rows failed, return:%{public}d", ret);
1128 return ret;
1129 }
1130 #endif // OHOS_BUILD_ENABLE_POINTER
1131 return RET_OK;
1132 }
1133
SetCustomCursor(int32_t pid, int32_t windowId, int32_t focusX, int32_t focusY, void* pixelMap)1134 int32_t MMIService::SetCustomCursor(int32_t pid, int32_t windowId, int32_t focusX, int32_t focusY, void* pixelMap)
1135 {
1136 CALL_INFO_TRACE;
1137 #if defined OHOS_BUILD_ENABLE_POINTER
1138 int32_t ret = CheckPidPermission(pid);
1139 if (ret != RET_OK) {
1140 MMI_HILOGE("Check pid permission failed");
1141 return ret;
1142 }
1143 ret = delegateTasks_.PostSyncTask(std::bind(
1144 [pixelMap, pid, windowId, focusX, focusY] {
1145 return IPointerDrawingManager::GetInstance()->SetCustomCursor(pixelMap, pid, windowId, focusX, focusY);
1146 }
1147 ));
1148 if (ret != RET_OK) {
1149 MMI_HILOGE("Set the custom cursor failed, ret:%{public}d", ret);
1150 return ret;
1151 }
1152 #endif // OHOS_BUILD_ENABLE_POINTER
1153 return RET_OK;
1154 }
1155
SetMouseIcon(int32_t windowId, void* pixelMap)1156 int32_t MMIService::SetMouseIcon(int32_t windowId, void* pixelMap)
1157 {
1158 CALL_INFO_TRACE;
1159 #if defined OHOS_BUILD_ENABLE_POINTER
1160 int32_t pid = GetCallingPid();
1161 int32_t ret = CheckPidPermission(pid);
1162 if (ret != RET_OK) {
1163 MMI_HILOGE("Check pid permission failed");
1164 return ret;
1165 }
1166 ret = delegateTasks_.PostSyncTask(std::bind(
1167 [pid, windowId, pixelMap] {
1168 return IPointerDrawingManager::GetInstance()->SetMouseIcon(pid, windowId, pixelMap);
1169 }
1170 ));
1171 if (ret != RET_OK) {
1172 MMI_HILOGE("Set the mouse icon failed, return:%{public}d", ret);
1173 return ret;
1174 }
1175 #endif // OHOS_BUILD_ENABLE_POINTER
1176 return RET_OK;
1177 }
1178
SetMouseHotSpot(int32_t pid, int32_t windowId, int32_t hotSpotX, int32_t hotSpotY)1179 int32_t MMIService::SetMouseHotSpot(int32_t pid, int32_t windowId, int32_t hotSpotX, int32_t hotSpotY)
1180 {
1181 CALL_INFO_TRACE;
1182 #if defined OHOS_BUILD_ENABLE_POINTER
1183 int32_t ret = CheckPidPermission(pid);
1184 if (ret != RET_OK) {
1185 MMI_HILOGE("Check pid permission failed");
1186 return ret;
1187 }
1188 ret = delegateTasks_.PostSyncTask(
1189 [pid, windowId, hotSpotX, hotSpotY] {
1190 return IPointerDrawingManager::GetInstance()->SetMouseHotSpot(pid, windowId, hotSpotX, hotSpotY);
1191 }
1192 );
1193 if (ret != RET_OK) {
1194 MMI_HILOGE("Set the mouse hot spot failed, return:%{public}d", ret);
1195 return ret;
1196 }
1197 #endif // OHOS_BUILD_ENABLE_POINTER
1198 return RET_OK;
1199 }
1200
SetNapStatus(int32_t pid, int32_t uid, std::string bundleName, int32_t napStatus)1201 int32_t MMIService::SetNapStatus(int32_t pid, int32_t uid, std::string bundleName, int32_t napStatus)
1202 {
1203 CALL_INFO_TRACE;
1204 int32_t ret = CheckPidPermission(pid);
1205 if (ret != RET_OK) {
1206 MMI_HILOGE("Check pid permission failed");
1207 return ret;
1208 }
1209 NapProcess::GetInstance()->SetNapStatus(pid, uid, bundleName, napStatus);
1210 return RET_OK;
1211 }
1212
1213 #ifdef OHOS_BUILD_ENABLE_POINTER
ReadMouseScrollRows(int32_t &rows)1214 int32_t MMIService::ReadMouseScrollRows(int32_t &rows)
1215 {
1216 rows = MouseEventHdr->GetMouseScrollRows();
1217 return RET_OK;
1218 }
1219 #endif // OHOS_BUILD_ENABLE_POINTER
1220
GetMouseScrollRows(int32_t &rows)1221 int32_t MMIService::GetMouseScrollRows(int32_t &rows)
1222 {
1223 CALL_INFO_TRACE;
1224 #ifdef OHOS_BUILD_ENABLE_POINTER
1225 int32_t ret = delegateTasks_.PostSyncTask(
1226 [this, &rows] {
1227 return this->ReadMouseScrollRows(rows);
1228 }
1229 );
1230 if (ret != RET_OK) {
1231 MMI_HILOGE("Get the number of mouse scrolling rows failed, ret:%{public}d", ret);
1232 return ret;
1233 }
1234 #endif // OHOS_BUILD_ENABLE_POINTER
1235 return RET_OK;
1236 }
1237
SetPointerSize(int32_t size)1238 int32_t MMIService::SetPointerSize(int32_t size)
1239 {
1240 CALL_INFO_TRACE;
1241 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
1242 int32_t ret = delegateTasks_.PostSyncTask(
1243 [size] {
1244 return IPointerDrawingManager::GetInstance()->SetPointerSize(size);
1245 }
1246 );
1247 if (ret != RET_OK) {
1248 MMI_HILOGE("Set pointer size failed, return:%{public}d", ret);
1249 return ret;
1250 }
1251 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
1252 return RET_OK;
1253 }
1254
1255 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
ReadPointerSize(int32_t &size)1256 int32_t MMIService::ReadPointerSize(int32_t &size)
1257 {
1258 size = IPointerDrawingManager::GetInstance()->GetPointerSize();
1259 return RET_OK;
1260 }
1261 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
1262
GetPointerSize(int32_t &size)1263 int32_t MMIService::GetPointerSize(int32_t &size)
1264 {
1265 CALL_INFO_TRACE;
1266 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
1267 int32_t ret = delegateTasks_.PostSyncTask(
1268 [this, &size] {
1269 return this->ReadPointerSize(size);
1270 }
1271 );
1272 if (ret != RET_OK) {
1273 MMI_HILOGE("Get pointer size failed, return:%{public}d", ret);
1274 return ret;
1275 }
1276 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
1277 return RET_OK;
1278 }
1279
SetMousePrimaryButton(int32_t primaryButton)1280 int32_t MMIService::SetMousePrimaryButton(int32_t primaryButton)
1281 {
1282 CALL_INFO_TRACE;
1283 #if defined OHOS_BUILD_ENABLE_POINTER
1284 int32_t ret = delegateTasks_.PostSyncTask(
1285 [primaryButton] {
1286 return ::OHOS::DelayedSingleton<MouseEventNormalize>::GetInstance()->SetMousePrimaryButton(primaryButton);
1287 }
1288 );
1289 if (ret != RET_OK) {
1290 MMI_HILOGE("Set mouse primary button failed, return:%{public}d", ret);
1291 return ret;
1292 }
1293 #endif // OHOS_BUILD_ENABLE_POINTER
1294 return RET_OK;
1295 }
1296
1297 #ifdef OHOS_BUILD_ENABLE_POINTER
ReadMousePrimaryButton(int32_t &primaryButton)1298 int32_t MMIService::ReadMousePrimaryButton(int32_t &primaryButton)
1299 {
1300 primaryButton = MouseEventHdr->GetMousePrimaryButton();
1301 return RET_OK;
1302 }
1303 #endif // OHOS_BUILD_ENABLE_POINTER
1304
GetMousePrimaryButton(int32_t &primaryButton)1305 int32_t MMIService::GetMousePrimaryButton(int32_t &primaryButton)
1306 {
1307 CALL_INFO_TRACE;
1308 #ifdef OHOS_BUILD_ENABLE_POINTER
1309 int32_t ret = delegateTasks_.PostSyncTask(
1310 [this, &primaryButton] {
1311 return this->ReadMousePrimaryButton(primaryButton);
1312 }
1313 );
1314 if (ret != RET_OK) {
1315 MMI_HILOGE("Get mouse primary button failed, return:%{public}d", ret);
1316 return ret;
1317 }
1318 #endif // OHOS_BUILD_ENABLE_POINTER
1319 return RET_OK;
1320 }
1321
SetPointerVisible(bool visible, int32_t priority)1322 int32_t MMIService::SetPointerVisible(bool visible, int32_t priority)
1323 {
1324 CALL_INFO_TRACE;
1325 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
1326 auto tokenId = IPCSkeleton::GetCallingTokenID();
1327 auto tokenType = OHOS::Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(tokenId);
1328 bool isHap = false;
1329 if (tokenType == OHOS::Security::AccessToken::TOKEN_HAP) {
1330 isHap = true;
1331 }
1332 int32_t clientPid = GetCallingPid();
1333 int32_t ret = delegateTasks_.PostSyncTask(
1334 [clientPid, visible, priority, isHap] {
1335 return IPointerDrawingManager::GetInstance()->SetPointerVisible(clientPid, visible, priority, isHap);
1336 }
1337 );
1338 if (ret != RET_OK) {
1339 MMI_HILOGE("Set pointer visible failed, return:%{public}d", ret);
1340 return ret;
1341 }
1342 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
1343 return RET_OK;
1344 }
1345
1346 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
CheckPointerVisible(bool &visible)1347 int32_t MMIService::CheckPointerVisible(bool &visible)
1348 {
1349 visible = IPointerDrawingManager::GetInstance()->IsPointerVisible();
1350 return RET_OK;
1351 }
1352 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
1353
IsPointerVisible(bool &visible)1354 int32_t MMIService::IsPointerVisible(bool &visible)
1355 {
1356 CALL_DEBUG_ENTER;
1357 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
1358 int32_t ret = delegateTasks_.PostSyncTask(
1359 [this, &visible] {
1360 return this->CheckPointerVisible(visible);
1361 }
1362 );
1363 if (ret != RET_OK) {
1364 MMI_HILOGE("Is pointer visible failed, return:%{public}d", ret);
1365 return ret;
1366 }
1367 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
1368 return RET_OK;
1369 }
1370
MarkProcessed(int32_t eventType, int32_t eventId)1371 int32_t MMIService::MarkProcessed(int32_t eventType, int32_t eventId)
1372 {
1373 CALL_DEBUG_ENTER;
1374 CHKPR(ANRMgr, RET_ERR);
1375 int32_t clientPid = GetCallingPid();
1376 int32_t ret = delegateTasks_.PostSyncTask(
1377 [clientPid, eventType, eventId] {
1378 return ::OHOS::DelayedSingleton<ANRManager>::GetInstance()->MarkProcessed(clientPid, eventType, eventId);
1379 }
1380 );
1381 if (ret != RET_OK) {
1382 MMI_HILOGD("Mark event processed failed, ret:%{public}d", ret);
1383 return ret;
1384 }
1385 return RET_OK;
1386 }
1387
SetPointerColor(int32_t color)1388 int32_t MMIService::SetPointerColor(int32_t color)
1389 {
1390 CALL_INFO_TRACE;
1391 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
1392 int32_t ret = delegateTasks_.PostSyncTask(
1393 [color] {
1394 return IPointerDrawingManager::GetInstance()->SetPointerColor(color);
1395 }
1396 );
1397 if (ret != RET_OK) {
1398 MMI_HILOGE("Set pointer color failed, return:%{public}d", ret);
1399 return ret;
1400 }
1401 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
1402 return RET_OK;
1403 }
1404
1405 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
ReadPointerColor(int32_t &color)1406 int32_t MMIService::ReadPointerColor(int32_t &color)
1407 {
1408 color = IPointerDrawingManager::GetInstance()->GetPointerColor();
1409 return RET_OK;
1410 }
1411 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
1412
GetPointerColor(int32_t &color)1413 int32_t MMIService::GetPointerColor(int32_t &color)
1414 {
1415 CALL_INFO_TRACE;
1416 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
1417 int32_t ret = delegateTasks_.PostSyncTask(
1418 [this, &color] {
1419 return this->ReadPointerColor(color);
1420 }
1421 );
1422 if (ret != RET_OK) {
1423 MMI_HILOGE("Get pointer color failed, return:%{public}d", ret);
1424 return ret;
1425 }
1426 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
1427 return RET_OK;
1428 }
1429
SetPointerSpeed(int32_t speed)1430 int32_t MMIService::SetPointerSpeed(int32_t speed)
1431 {
1432 CALL_INFO_TRACE;
1433 #ifdef OHOS_BUILD_ENABLE_POINTER
1434 int32_t ret = delegateTasks_.PostSyncTask(
1435 [speed] {
1436 return ::OHOS::DelayedSingleton<MouseEventNormalize>::GetInstance()->SetPointerSpeed(speed);
1437 }
1438 );
1439 if (ret != RET_OK) {
1440 MMI_HILOGE("Set pointer speed failed, return:%{public}d", ret);
1441 return ret;
1442 }
1443 #endif // OHOS_BUILD_ENABLE_POINTER
1444 return RET_OK;
1445 }
1446
1447 #ifdef OHOS_BUILD_ENABLE_POINTER
ReadPointerSpeed(int32_t &speed)1448 int32_t MMIService::ReadPointerSpeed(int32_t &speed)
1449 {
1450 speed = MouseEventHdr->GetPointerSpeed();
1451 return RET_OK;
1452 }
1453 #endif // OHOS_BUILD_ENABLE_POINTER
1454
GetPointerSpeed(int32_t &speed)1455 int32_t MMIService::GetPointerSpeed(int32_t &speed)
1456 {
1457 CALL_INFO_TRACE;
1458 #ifdef OHOS_BUILD_ENABLE_POINTER
1459 int32_t ret = delegateTasks_.PostSyncTask(
1460 [this, &speed] {
1461 return this->ReadPointerSpeed(speed);
1462 }
1463 );
1464 if (ret != RET_OK) {
1465 MMI_HILOGE("Get pointer speed failed, return:%{public}d", ret);
1466 return ret;
1467 }
1468 #endif // OHOS_BUILD_ENABLE_POINTER
1469 return RET_OK;
1470 }
1471
NotifyNapOnline()1472 int32_t MMIService::NotifyNapOnline()
1473 {
1474 CALL_DEBUG_ENTER;
1475 NapProcess::GetInstance()->NotifyNapOnline();
1476 return RET_OK;
1477 }
1478
RemoveInputEventObserver()1479 int32_t MMIService::RemoveInputEventObserver()
1480 {
1481 CALL_DEBUG_ENTER;
1482 NapProcess::GetInstance()->RemoveInputEventObserver();
1483 return RET_OK;
1484 }
1485
SetPointerStyle(int32_t windowId, PointerStyle pointerStyle, bool isUiExtension)1486 int32_t MMIService::SetPointerStyle(int32_t windowId, PointerStyle pointerStyle, bool isUiExtension)
1487 {
1488 CALL_INFO_TRACE;
1489 #ifdef OHOS_BUILD_ENABLE_POINTER
1490 int32_t clientPid = GetCallingPid();
1491 int32_t ret = delegateTasks_.PostSyncTask(
1492 [clientPid, windowId, pointerStyle, isUiExtension] {
1493 return IPointerDrawingManager::GetInstance()->SetPointerStyle(
1494 clientPid, windowId, pointerStyle, isUiExtension);
1495 }
1496 );
1497 if (ret != RET_OK) {
1498 MMI_HILOGE("Set pointer style failed, return:%{public}d", ret);
1499 return ret;
1500 }
1501 #endif // OHOS_BUILD_ENABLE_POINTER
1502 return RET_OK;
1503 }
1504
ClearWindowPointerStyle(int32_t pid, int32_t windowId)1505 int32_t MMIService::ClearWindowPointerStyle(int32_t pid, int32_t windowId)
1506 {
1507 CALL_DEBUG_ENTER;
1508 #ifdef OHOS_BUILD_ENABLE_POINTER
1509 int32_t ret = CheckPidPermission(pid);
1510 if (ret != RET_OK) {
1511 MMI_HILOGE("Check pid permission failed");
1512 return ret;
1513 }
1514 ret = delegateTasks_.PostSyncTask(
1515 [pid, windowId] {
1516 return IPointerDrawingManager::GetInstance()->ClearWindowPointerStyle(pid, windowId);
1517 }
1518 );
1519 if (ret != RET_OK) {
1520 MMI_HILOGE("Set pointer style failed, return:%{public}d", ret);
1521 return ret;
1522 }
1523 #endif // OHOS_BUILD_ENABLE_POINTER
1524 return RET_OK;
1525 }
1526
GetPointerStyle(int32_t windowId, PointerStyle &pointerStyle, bool isUiExtension)1527 int32_t MMIService::GetPointerStyle(int32_t windowId, PointerStyle &pointerStyle, bool isUiExtension)
1528 {
1529 CALL_DEBUG_ENTER;
1530 #ifdef OHOS_BUILD_ENABLE_POINTER
1531 int32_t clientPid = GetCallingPid();
1532 int32_t ret = delegateTasks_.PostSyncTask(
1533 [clientPid, windowId, &pointerStyle, isUiExtension] {
1534 return IPointerDrawingManager::GetInstance()->GetPointerStyle(
1535 clientPid, windowId, pointerStyle, isUiExtension);
1536 }
1537 );
1538 if (ret != RET_OK) {
1539 MMI_HILOGE("Get pointer style failed, return:%{public}d", ret);
1540 return ret;
1541 }
1542 #endif // OHOS_BUILD_ENABLE_POINTER
1543 return RET_OK;
1544 }
1545
SetHoverScrollState(bool state)1546 int32_t MMIService::SetHoverScrollState(bool state)
1547 {
1548 CALL_INFO_TRACE;
1549 #if defined OHOS_BUILD_ENABLE_POINTER
1550 int32_t ret = delegateTasks_.PostSyncTask(
1551 [state] {
1552 return ::OHOS::MMI::IInputWindowsManager::GetInstance()->SetHoverScrollState(state);
1553 }
1554 );
1555 if (ret != RET_OK) {
1556 MMI_HILOGE("Set mouse hover scroll state failed, return:%{public}d", ret);
1557 return ret;
1558 }
1559 #endif // OHOS_BUILD_ENABLE_POINTER
1560 return RET_OK;
1561 }
1562
1563 #ifdef OHOS_BUILD_ENABLE_POINTER
ReadHoverScrollState(bool &state)1564 int32_t MMIService::ReadHoverScrollState(bool &state)
1565 {
1566 state = WIN_MGR->GetHoverScrollState();
1567 return RET_OK;
1568 }
1569 #endif // OHOS_BUILD_ENABLE_POINTER
1570
GetHoverScrollState(bool &state)1571 int32_t MMIService::GetHoverScrollState(bool &state)
1572 {
1573 CALL_INFO_TRACE;
1574 #ifdef OHOS_BUILD_ENABLE_POINTER
1575 int32_t ret = delegateTasks_.PostSyncTask(
1576 [this, &state] {
1577 return this->ReadHoverScrollState(state);
1578 }
1579 );
1580 if (ret != RET_OK) {
1581 MMI_HILOGE("Get mouse hover scroll state, return:%{public}d", ret);
1582 return ret;
1583 }
1584 #endif // OHOS_BUILD_ENABLE_POINTER
1585 return RET_OK;
1586 }
1587
OnSupportKeys(int32_t deviceId, std::vector<int32_t> &keys, std::vector<bool> &keystroke)1588 int32_t MMIService::OnSupportKeys(int32_t deviceId, std::vector<int32_t> &keys, std::vector<bool> &keystroke)
1589 {
1590 CALL_DEBUG_ENTER;
1591 int32_t ret = INPUT_DEV_MGR->SupportKeys(deviceId, keys, keystroke);
1592 if (ret != RET_OK) {
1593 MMI_HILOGE("Device id not support");
1594 return ret;
1595 }
1596 if (keystroke.size() > MAX_SUPPORT_KEY) {
1597 MMI_HILOGE("Device exceeds the max range");
1598 return RET_ERR;
1599 }
1600 return RET_OK;
1601 }
1602
SupportKeys(int32_t deviceId, std::vector<int32_t> &keys, std::vector<bool> &keystroke)1603 int32_t MMIService::SupportKeys(int32_t deviceId, std::vector<int32_t> &keys, std::vector<bool> &keystroke)
1604 {
1605 CALL_DEBUG_ENTER;
1606 int32_t ret = delegateTasks_.PostSyncTask(
1607 [this, deviceId, &keys, &keystroke] {
1608 return this->OnSupportKeys(deviceId, keys, keystroke);
1609 }
1610 );
1611 if (ret != RET_OK) {
1612 MMI_HILOGE("Support keys info process failed, ret:%{public}d", ret);
1613 return ret;
1614 }
1615 return RET_OK;
1616 }
1617
OnGetDeviceIds(std::vector<int32_t> &ids)1618 int32_t MMIService::OnGetDeviceIds(std::vector<int32_t> &ids)
1619 {
1620 CALL_DEBUG_ENTER;
1621 ids = INPUT_DEV_MGR->GetInputDeviceIds();
1622 return RET_OK;
1623 }
1624
GetDeviceIds(std::vector<int32_t> &ids)1625 int32_t MMIService::GetDeviceIds(std::vector<int32_t> &ids)
1626 {
1627 CALL_DEBUG_ENTER;
1628 int32_t ret = delegateTasks_.PostSyncTask(
1629 [this, &ids] {
1630 return this->OnGetDeviceIds(ids);
1631 }
1632 );
1633 if (ret != RET_OK) {
1634 MMI_HILOGE("Get deviceids failed, ret:%{public}d", ret);
1635 return ret;
1636 }
1637 return RET_OK;
1638 }
1639
OnGetDevice(int32_t deviceId, std::shared_ptr<InputDevice> &inputDevice)1640 int32_t MMIService::OnGetDevice(int32_t deviceId, std::shared_ptr<InputDevice> &inputDevice)
1641 {
1642 CALL_DEBUG_ENTER;
1643 if (INPUT_DEV_MGR->GetInputDevice(deviceId) == nullptr) {
1644 MMI_HILOGE("Input device not found");
1645 return COMMON_PARAMETER_ERROR;
1646 }
1647 inputDevice = INPUT_DEV_MGR->GetInputDevice(deviceId);
1648 return RET_OK;
1649 }
1650
GetDevice(int32_t deviceId, std::shared_ptr<InputDevice> &inputDevice)1651 int32_t MMIService::GetDevice(int32_t deviceId, std::shared_ptr<InputDevice> &inputDevice)
1652 {
1653 CALL_DEBUG_ENTER;
1654 int32_t ret = delegateTasks_.PostSyncTask(
1655 [this, deviceId, &inputDevice] {
1656 return this->OnGetDevice(deviceId, inputDevice);
1657 }
1658 );
1659 if (ret != RET_OK) {
1660 MMI_HILOGE("Get input device info failed, ret:%{public}d", ret);
1661 return ret;
1662 }
1663 return RET_OK;
1664 }
1665
OnRegisterDevListener(int32_t pid)1666 int32_t MMIService::OnRegisterDevListener(int32_t pid)
1667 {
1668 auto sess = GetSession(GetClientFd(pid));
1669 CHKPR(sess, RET_ERR);
1670 INPUT_DEV_MGR->AddDevListener(sess);
1671 return RET_OK;
1672 }
1673
RegisterDevListener()1674 int32_t MMIService::RegisterDevListener()
1675 {
1676 CALL_DEBUG_ENTER;
1677 int32_t pid = GetCallingPid();
1678 int32_t ret = delegateTasks_.PostSyncTask(
1679 [this, pid] {
1680 return this->OnRegisterDevListener(pid);
1681 }
1682 );
1683 if (ret != RET_OK) {
1684 MMI_HILOGE("Register device listener failed, ret:%{public}d", ret);
1685 return ret;
1686 }
1687 return RET_OK;
1688 }
1689
OnUnregisterDevListener(int32_t pid)1690 int32_t MMIService::OnUnregisterDevListener(int32_t pid)
1691 {
1692 auto sess = GetSession(GetClientFd(pid));
1693 INPUT_DEV_MGR->RemoveDevListener(sess);
1694 return RET_OK;
1695 }
1696
UnregisterDevListener()1697 int32_t MMIService::UnregisterDevListener()
1698 {
1699 CALL_DEBUG_ENTER;
1700 int32_t pid = GetCallingPid();
1701 int32_t ret = delegateTasks_.PostSyncTask(
1702 [this, pid] {
1703 return this->OnUnregisterDevListener(pid);
1704 }
1705 );
1706 if (ret != RET_OK) {
1707 MMI_HILOGE("Unregister device listener failed failed, ret:%{public}d", ret);
1708 return ret;
1709 }
1710 return RET_OK;
1711 }
1712
OnGetKeyboardType(int32_t deviceId, int32_t &keyboardType)1713 int32_t MMIService::OnGetKeyboardType(int32_t deviceId, int32_t &keyboardType)
1714 {
1715 CALL_DEBUG_ENTER;
1716 int32_t ret = INPUT_DEV_MGR->GetKeyboardType(deviceId, keyboardType);
1717 if (ret != RET_OK) {
1718 MMI_HILOGD("GetKeyboardType call failed");
1719 return ret;
1720 }
1721 return RET_OK;
1722 }
1723
GetKeyboardType(int32_t deviceId, int32_t &keyboardType)1724 int32_t MMIService::GetKeyboardType(int32_t deviceId, int32_t &keyboardType)
1725 {
1726 CALL_DEBUG_ENTER;
1727 int32_t ret = delegateTasks_.PostSyncTask(
1728 [this, deviceId, &keyboardType] {
1729 return this->OnGetKeyboardType(deviceId, keyboardType);
1730 }
1731 );
1732 if (ret != RET_OK) {
1733 MMI_HILOGD("Get keyboard type failed, ret:%{public}d", ret);
1734 return ret;
1735 }
1736 return ret;
1737 }
1738
SetKeyboardRepeatDelay(int32_t delay)1739 int32_t MMIService::SetKeyboardRepeatDelay(int32_t delay)
1740 {
1741 CALL_INFO_TRACE;
1742 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
1743 int32_t ret = delegateTasks_.PostSyncTask(
1744 [delay] {
1745 return ::OHOS::DelayedSingleton<KeyAutoRepeat>::GetInstance()->SetKeyboardRepeatDelay(delay);
1746 }
1747 );
1748 if (ret != RET_OK) {
1749 MMI_HILOGE("Set keyboard repeat delay failed, ret:%{public}d", ret);
1750 return ret;
1751 }
1752 #endif // OHOS_BUILD_ENABLE_KEYBOARD
1753 return RET_OK;
1754 }
1755
SetKeyboardRepeatRate(int32_t rate)1756 int32_t MMIService::SetKeyboardRepeatRate(int32_t rate)
1757 {
1758 CALL_INFO_TRACE;
1759 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
1760 int32_t ret = delegateTasks_.PostSyncTask(
1761 [rate] {
1762 return ::OHOS::DelayedSingleton<KeyAutoRepeat>::GetInstance()->SetKeyboardRepeatRate(rate);
1763 }
1764 );
1765 if (ret != RET_OK) {
1766 MMI_HILOGE("Set keyboard repeat rate failed, ret:%{public}d", ret);
1767 return ret;
1768 }
1769 #endif // OHOS_BUILD_ENABLE_KEYBOARD
1770 return RET_OK;
1771 }
1772
GetKeyboardRepeatDelay(int32_t &delay)1773 int32_t MMIService::GetKeyboardRepeatDelay(int32_t &delay)
1774 {
1775 CALL_INFO_TRACE;
1776 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
1777 int32_t ret = delegateTasks_.PostSyncTask(
1778 [&delay] {
1779 return ::OHOS::DelayedSingleton<KeyAutoRepeat>::GetInstance()->GetKeyboardRepeatDelay(delay);
1780 }
1781 );
1782 if (ret != RET_OK) {
1783 MMI_HILOGE("Get keyboard repeat delay failed, ret:%{public}d", ret);
1784 return ret;
1785 }
1786 #endif // OHOS_BUILD_ENABLE_KEYBOARD
1787 return RET_OK;
1788 }
1789
GetKeyboardRepeatRate(int32_t &rate)1790 int32_t MMIService::GetKeyboardRepeatRate(int32_t &rate)
1791 {
1792 CALL_INFO_TRACE;
1793 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
1794 int32_t ret = delegateTasks_.PostSyncTask(
1795 [&rate] {
1796 return ::OHOS::DelayedSingleton<KeyAutoRepeat>::GetInstance()->GetKeyboardRepeatRate(rate);
1797 }
1798 );
1799 if (ret != RET_OK) {
1800 MMI_HILOGE("Get keyboard repeat rate failed, ret:%{public}d", ret);
1801 return ret;
1802 }
1803 #endif // OHOS_BUILD_ENABLE_KEYBOARD
1804 return RET_OK;
1805 }
1806
1807 #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)1808 int32_t MMIService::CheckAddInput(int32_t pid, InputHandlerType handlerType, HandleEventType eventType,
1809 int32_t priority, uint32_t deviceTags)
1810 {
1811 auto sess = GetSessionByPid(pid);
1812 CHKPR(sess, ERROR_NULL_POINTER);
1813 return sMsgHandler_.OnAddInputHandler(sess, handlerType, eventType, priority, deviceTags);
1814 }
1815
CheckAddInput(int32_t pid, InputHandlerType handlerType, std::vector<int32_t> actionsType)1816 int32_t MMIService::CheckAddInput(int32_t pid, InputHandlerType handlerType, std::vector<int32_t> actionsType)
1817 {
1818 auto sess = GetSessionByPid(pid);
1819 CHKPR(sess, ERROR_NULL_POINTER);
1820 return sMsgHandler_.OnAddInputHandler(sess, handlerType, actionsType);
1821 }
1822
1823 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR || OHOS_BUILD_ENABLE_MONITOR
1824
AddInputHandler(InputHandlerType handlerType, HandleEventType eventType, int32_t priority, uint32_t deviceTags, std::vector<int32_t> actionsType)1825 int32_t MMIService::AddInputHandler(InputHandlerType handlerType, HandleEventType eventType, int32_t priority,
1826 uint32_t deviceTags, std::vector<int32_t> actionsType)
1827 {
1828 CALL_INFO_TRACE;
1829 #if defined(OHOS_BUILD_ENABLE_MONITOR) && defined(PLAYER_FRAMEWORK_EXISTS)
1830 bool isRegisterCaptureCb = false;
1831 if (!PER_HELPER->VerifySystemApp() && handlerType == InputHandlerType::MONITOR) {
1832 isRegisterCaptureCb = true;
1833 }
1834 #endif // OHOS_BUILD_ENABLE_MONITOR && PLAYER_FRAMEWORK_EXISTS
1835 #if defined(OHOS_BUILD_ENABLE_INTERCEPTOR) || defined(OHOS_BUILD_ENABLE_MONITOR)
1836 int32_t pid = GetCallingPid();
1837 int32_t ret = RET_ERR;
1838 if (actionsType.empty()) {
1839 ret = delegateTasks_.PostSyncTask(
1840 [this, pid, handlerType, eventType, priority, deviceTags, isRegisterCaptureCb] {
1841 #if defined(OHOS_BUILD_ENABLE_MONITOR) && defined(PLAYER_FRAMEWORK_EXISTS)
1842 if (isRegisterCaptureCb) {
1843 RegisterScreenCaptureCallback();
1844 }
1845 #endif // OHOS_BUILD_ENABLE_MONITOR && PLAYER_FRAMEWORK_EXISTS
1846 return this->CheckAddInput(pid, handlerType, eventType, priority, deviceTags);
1847 }
1848 );
1849 } else {
1850 ret = delegateTasks_.PostSyncTask(
1851 [this, pid, handlerType, actionsType, isRegisterCaptureCb] {
1852 #if defined(OHOS_BUILD_ENABLE_MONITOR) && defined(PLAYER_FRAMEWORK_EXISTS)
1853 if (isRegisterCaptureCb) {
1854 RegisterScreenCaptureCallback();
1855 }
1856 #endif // OHOS_BUILD_ENABLE_MONITOR && PLAYER_FRAMEWORK_EXISTS
1857 return this->CheckAddInput(pid, handlerType, actionsType);
1858 }
1859 );
1860 }
1861 if (ret != RET_OK) {
1862 MMI_HILOGE("Add input handler failed, ret:%{public}d", ret);
1863 return ret;
1864 }
1865 ret = ObserverAddInputHandler(pid);
1866 if (ret != RET_OK) {
1867 MMI_HILOGE("AddInputHandler info to observer failed, ret:%{public}d", ret);
1868 return ret;
1869 }
1870 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR || OHOS_BUILD_ENABLE_MONITOR
1871 return RET_OK;
1872 }
1873
1874 #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)1875 int32_t MMIService::CheckRemoveInput(int32_t pid, InputHandlerType handlerType, HandleEventType eventType,
1876 int32_t priority, uint32_t deviceTags)
1877 {
1878 auto sess = GetSessionByPid(pid);
1879 CHKPR(sess, ERROR_NULL_POINTER);
1880 return sMsgHandler_.OnRemoveInputHandler(sess, handlerType, eventType, priority, deviceTags);
1881 }
1882
CheckRemoveInput(int32_t pid, InputHandlerType handlerType, std::vector<int32_t> actionsType)1883 int32_t MMIService::CheckRemoveInput(int32_t pid, InputHandlerType handlerType, std::vector<int32_t> actionsType)
1884 {
1885 auto sess = GetSessionByPid(pid);
1886 CHKPR(sess, ERROR_NULL_POINTER);
1887 return sMsgHandler_.OnRemoveInputHandler(sess, handlerType, actionsType);
1888 }
1889
ObserverAddInputHandler(int32_t pid)1890 int32_t MMIService::ObserverAddInputHandler(int32_t pid)
1891 {
1892 if (NapProcess::GetInstance()->GetNapClientPid() != REMOVE_OBSERVER) {
1893 OHOS::MMI::NapProcess::NapStatusData napData;
1894 napData.pid = GetCallingPid();
1895 napData.uid = GetCallingUid();
1896 auto sess = GetSessionByPid(pid);
1897 CHKPR(sess, ERROR_NULL_POINTER);
1898 napData.bundleName = sess->GetProgramName();
1899 int32_t syncState = SUBSCRIBED;
1900 MMI_HILOGD("AddInputHandler info to observer : pid:%{public}d, uid:%d, bundleName:%{public}s",
1901 napData.pid, napData.uid, napData.bundleName.c_str());
1902 NapProcess::GetInstance()->AddMmiSubscribedEventData(napData, syncState);
1903 if (NapProcess::GetInstance()->GetNapClientPid() != UNOBSERVED) {
1904 NapProcess::GetInstance()->NotifyBundleName(napData, syncState);
1905 }
1906 }
1907 return RET_OK;
1908 }
1909 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR || OHOS_BUILD_ENABLE_MONITOR
1910
RemoveInputHandler(InputHandlerType handlerType, HandleEventType eventType, int32_t priority, uint32_t deviceTags, std::vector<int32_t> actionsType)1911 int32_t MMIService::RemoveInputHandler(InputHandlerType handlerType, HandleEventType eventType, int32_t priority,
1912 uint32_t deviceTags, std::vector<int32_t> actionsType)
1913 {
1914 CALL_INFO_TRACE;
1915 #if defined(OHOS_BUILD_ENABLE_INTERCEPTOR) || defined(OHOS_BUILD_ENABLE_MONITOR)
1916 int32_t pid = GetCallingPid();
1917 int32_t ret = RET_ERR;
1918 if (actionsType.empty()) {
1919 ret = delegateTasks_.PostSyncTask(
1920 [this, pid, handlerType, eventType, priority, deviceTags] {
1921 return this->CheckRemoveInput(pid, handlerType, eventType, priority, deviceTags);
1922 }
1923 );
1924 } else {
1925 ret = delegateTasks_.PostSyncTask(
1926 [this, pid, handlerType, actionsType] {
1927 return this->CheckRemoveInput(pid, handlerType, actionsType);
1928 }
1929 );
1930 }
1931 if (ret != RET_OK) {
1932 MMI_HILOGE("Remove input handler failed, ret:%{public}d", ret);
1933 return ret;
1934 }
1935 if (NapProcess::GetInstance()->GetNapClientPid() != REMOVE_OBSERVER) {
1936 OHOS::MMI::NapProcess::NapStatusData napData;
1937 napData.pid = GetCallingPid();
1938 napData.uid = GetCallingUid();
1939 auto sess = GetSessionByPid(pid);
1940 CHKPR(sess, ERROR_NULL_POINTER);
1941 napData.bundleName = sess->GetProgramName();
1942 int32_t syncState = UNSUBSCRIBED;
1943 MMI_HILOGD("RemoveInputHandler info to observer : pid:%{public}d, uid:%{public}d, bundleName:%{public}s",
1944 napData.pid, napData.uid, napData.bundleName.c_str());
1945 NapProcess::GetInstance()->AddMmiSubscribedEventData(napData, syncState);
1946 if (NapProcess::GetInstance()->GetNapClientPid() != UNOBSERVED) {
1947 NapProcess::GetInstance()->NotifyBundleName(napData, syncState);
1948 }
1949 }
1950 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR || OHOS_BUILD_ENABLE_MONITOR
1951 return RET_OK;
1952 }
1953
AddGestureMonitor(InputHandlerType handlerType, HandleEventType eventType, TouchGestureType gestureType, int32_t fingers)1954 int32_t MMIService::AddGestureMonitor(InputHandlerType handlerType,
1955 HandleEventType eventType, TouchGestureType gestureType, int32_t fingers)
1956 {
1957 CALL_INFO_TRACE;
1958 #if defined(OHOS_BUILD_ENABLE_TOUCH) && defined(OHOS_BUILD_ENABLE_MONITOR)
1959 int32_t pid = GetCallingPid();
1960 int32_t ret = delegateTasks_.PostSyncTask(
1961 [this, pid, handlerType, eventType, gestureType, fingers]() -> int32_t {
1962 if (((eventType & HANDLE_EVENT_TYPE_TOUCH_GESTURE) != HANDLE_EVENT_TYPE_TOUCH_GESTURE)) {
1963 MMI_HILOGE("Illegal type:%{public}d", eventType);
1964 return RET_ERR;
1965 }
1966 if (!GestureMonitorHandler::CheckMonitorValid(gestureType, fingers)) {
1967 MMI_HILOGE("Wrong number of fingers:%{public}d", fingers);
1968 return RET_ERR;
1969 }
1970 if (touchGestureAdapter_ == nullptr) {
1971 touchGestureAdapter_ = TouchGestureAdapter::GetGestureFactory();
1972 }
1973 if (touchGestureAdapter_ != nullptr) {
1974 touchGestureAdapter_->SetGestureCondition(true, gestureType, fingers);
1975 }
1976 if (delegateInterface_ != nullptr && !delegateInterface_->HasHandler("touchGesture")) {
1977 auto fun = [this](std::shared_ptr<PointerEvent> event) -> int32_t {
1978 CHKPR(touchGestureAdapter_, ERROR_NULL_POINTER);
1979 touchGestureAdapter_->process(event);
1980 return RET_OK;
1981 };
1982 int32_t ret = delegateInterface_->AddHandler(InputHandlerType::MONITOR,
1983 {"touchGesture", HANDLE_EVENT_TYPE_POINTER, HandlerMode::SYNC, 0, 0, fun});
1984 if (ret != RET_OK) {
1985 MMI_HILOGE("Failed to add gesture recognizer, ret:%{public}d", ret);
1986 return ret;
1987 }
1988 }
1989 auto sess = GetSessionByPid(pid);
1990 CHKPR(sess, ERROR_NULL_POINTER);
1991 return sMsgHandler_.OnAddGestureMonitor(sess, handlerType, eventType, gestureType, fingers);
1992 });
1993 if (ret != RET_OK) {
1994 MMI_HILOGE("Add gesture handler failed, ret:%{public}d", ret);
1995 return ret;
1996 }
1997 #endif // OHOS_BUILD_ENABLE_TOUCH && OHOS_BUILD_ENABLE_MONITOR
1998 return RET_OK;
1999 }
2000
RemoveGestureMonitor(InputHandlerType handlerType, HandleEventType eventType, TouchGestureType gestureType, int32_t fingers)2001 int32_t MMIService::RemoveGestureMonitor(InputHandlerType handlerType,
2002 HandleEventType eventType, TouchGestureType gestureType, int32_t fingers)
2003 {
2004 CALL_INFO_TRACE;
2005 #if defined(OHOS_BUILD_ENABLE_TOUCH) && defined(OHOS_BUILD_ENABLE_MONITOR)
2006 int32_t pid = GetCallingPid();
2007 int32_t ret = delegateTasks_.PostSyncTask(
2008 [this, pid, handlerType, eventType, gestureType, fingers]() -> int32_t {
2009 auto sess = GetSessionByPid(pid);
2010 CHKPR(sess, ERROR_NULL_POINTER);
2011 int32_t ret = sMsgHandler_.OnRemoveGestureMonitor(sess, handlerType, eventType, gestureType, fingers);
2012 if (ret != RET_OK) {
2013 MMI_HILOGE("Failed to remove gesture recognizer, ret:%{public}d", ret);
2014 return ret;
2015 }
2016 auto monitorHandler = InputHandler->GetMonitorHandler();
2017 if (monitorHandler && !monitorHandler->CheckHasInputHandler(HANDLE_EVENT_TYPE_TOUCH_GESTURE)) {
2018 if (delegateInterface_ && delegateInterface_->HasHandler("touchGesture")) {
2019 delegateInterface_->RemoveHandler(InputHandlerType::MONITOR, "touchGesture");
2020 }
2021 }
2022 if (touchGestureAdapter_) {
2023 touchGestureAdapter_->SetGestureCondition(false, gestureType, fingers);
2024 }
2025 return RET_OK;
2026 });
2027 if (ret != RET_OK) {
2028 MMI_HILOGE("Remove gesture handler failed, ret:%{public}d", ret);
2029 return ret;
2030 }
2031 #endif // OHOS_BUILD_ENABLE_TOUCH && OHOS_BUILD_ENABLE_MONITOR
2032 return RET_OK;
2033 }
2034
2035 #ifdef OHOS_BUILD_ENABLE_MONITOR
CheckMarkConsumed(int32_t pid, int32_t eventId)2036 int32_t MMIService::CheckMarkConsumed(int32_t pid, int32_t eventId)
2037 {
2038 auto sess = GetSessionByPid(pid);
2039 CHKPR(sess, ERROR_NULL_POINTER);
2040 return sMsgHandler_.OnMarkConsumed(sess, eventId);
2041 }
2042 #endif // OHOS_BUILD_ENABLE_MONITOR
2043
MarkEventConsumed(int32_t eventId)2044 int32_t MMIService::MarkEventConsumed(int32_t eventId)
2045 {
2046 CALL_DEBUG_ENTER;
2047 #ifdef OHOS_BUILD_ENABLE_MONITOR
2048 int32_t pid = GetCallingPid();
2049 int32_t ret = delegateTasks_.PostSyncTask(
2050 [this, pid, eventId] {
2051 return this->CheckMarkConsumed(pid, eventId);
2052 }
2053 );
2054 if (ret != RET_OK) {
2055 MMI_HILOGE("Mark event consumed failed, ret:%{public}d", ret);
2056 return ret;
2057 }
2058 #endif // OHOS_BUILD_ENABLE_MONITOR
2059 return RET_OK;
2060 }
2061
MoveMouseEvent(int32_t offsetX, int32_t offsetY)2062 int32_t MMIService::MoveMouseEvent(int32_t offsetX, int32_t offsetY)
2063 {
2064 CALL_DEBUG_ENTER;
2065 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
2066 int32_t ret =
2067 delegateTasks_.PostSyncTask(
2068 [this, offsetX, offsetY] {
2069 return sMsgHandler_.OnMoveMouse(offsetX, offsetY);
2070 }
2071 );
2072 if (ret != RET_OK) {
2073 MMI_HILOGE("The movemouse event processed failed, ret:%{public}d", ret);
2074 return ret;
2075 }
2076 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
2077 return RET_OK;
2078 }
2079
InjectKeyEvent(const std::shared_ptr<KeyEvent> keyEvent, bool isNativeInject)2080 int32_t MMIService::InjectKeyEvent(const std::shared_ptr<KeyEvent> keyEvent, bool isNativeInject)
2081 {
2082 CALL_DEBUG_ENTER;
2083 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
2084 int32_t ret;
2085 int32_t pid = GetCallingPid();
2086 #ifdef OHOS_BUILD_ENABLE_ANCO
2087 ret = InjectKeyEventExt(keyEvent, pid, isNativeInject);
2088 #else
2089 ret = delegateTasks_.PostSyncTask(
2090 [this, keyEvent, pid, isNativeInject] {
2091 return this->CheckInjectKeyEvent(keyEvent, pid, isNativeInject);
2092 }
2093 );
2094 #endif // OHOS_BUILD_ENABLE_ANCO
2095 if (ret != RET_OK) {
2096 MMI_HILOGE("Inject key event failed, ret:%{public}d", ret);
2097 return ret;
2098 }
2099 #endif // OHOS_BUILD_ENABLE_KEYBOARD
2100 return RET_OK;
2101 }
2102
CheckInjectKeyEvent(const std::shared_ptr<KeyEvent> keyEvent, int32_t pid, bool isNativeInject)2103 int32_t MMIService::CheckInjectKeyEvent(const std::shared_ptr<KeyEvent> keyEvent, int32_t pid, bool isNativeInject)
2104 {
2105 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
2106 CHKPR(keyEvent, ERROR_NULL_POINTER);
2107 LogTracer lt(keyEvent->GetId(), keyEvent->GetEventType(), keyEvent->GetKeyAction());
2108 return sMsgHandler_.OnInjectKeyEvent(keyEvent, pid, isNativeInject);
2109 #else
2110 return RET_OK;
2111 #endif // OHOS_BUILD_ENABLE_KEYBOARD
2112 }
2113
2114 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
OnGetKeyState(std::vector<int32_t> &pressedKeys, std::map<int32_t, int32_t> &specialKeysState)2115 int32_t MMIService::OnGetKeyState(std::vector<int32_t> &pressedKeys, std::map<int32_t, int32_t> &specialKeysState)
2116 {
2117 auto keyEvent = KeyEventHdr->GetKeyEvent();
2118 CHKPR(keyEvent, ERROR_NULL_POINTER);
2119 pressedKeys = keyEvent->GetPressedKeys();
2120 for (auto iter = pressedKeys.begin(); iter != pressedKeys.end();) {
2121 if (g_keyCodeValueSet.find(*iter) == g_keyCodeValueSet.end()) {
2122 iter = pressedKeys.erase(iter);
2123 continue;
2124 }
2125 ++iter;
2126 }
2127 specialKeysState[KeyEvent::KEYCODE_CAPS_LOCK] =
2128 static_cast<int32_t>(keyEvent->GetFunctionKey(KeyEvent::CAPS_LOCK_FUNCTION_KEY));
2129 specialKeysState[KeyEvent::KEYCODE_SCROLL_LOCK] =
2130 static_cast<int32_t>(keyEvent->GetFunctionKey(KeyEvent::SCROLL_LOCK_FUNCTION_KEY));
2131 specialKeysState[KeyEvent::KEYCODE_NUM_LOCK] =
2132 static_cast<int32_t>(keyEvent->GetFunctionKey(KeyEvent::NUM_LOCK_FUNCTION_KEY));
2133 return RET_OK;
2134 }
2135 #endif // OHOS_BUILD_ENABLE_KEYBOARD
2136
CheckInjectPointerEvent(const std::shared_ptr<PointerEvent> pointerEvent, int32_t pid, bool isNativeInject, bool isShell)2137 int32_t MMIService::CheckInjectPointerEvent(const std::shared_ptr<PointerEvent> pointerEvent,
2138 int32_t pid, bool isNativeInject, bool isShell)
2139 {
2140 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
2141 CHKPR(pointerEvent, ERROR_NULL_POINTER);
2142 LogTracer lt(pointerEvent->GetId(), pointerEvent->GetEventType(), pointerEvent->GetPointerAction());
2143 return sMsgHandler_.OnInjectPointerEvent(pointerEvent, pid, isNativeInject, isShell);
2144 #else
2145 return RET_OK;
2146 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
2147 }
2148
InjectPointerEvent(const std::shared_ptr<PointerEvent> pointerEvent, bool isNativeInject)2149 int32_t MMIService::InjectPointerEvent(const std::shared_ptr<PointerEvent> pointerEvent, bool isNativeInject)
2150 {
2151 CALL_DEBUG_ENTER;
2152 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
2153 int32_t ret;
2154 int32_t pid = GetCallingPid();
2155 bool isShell = PER_HELPER->RequestFromShell();
2156 #ifdef OHOS_BUILD_ENABLE_ANCO
2157 ret = InjectPointerEventExt(pointerEvent, pid, isNativeInject, isShell);
2158 #else
2159 ret = delegateTasks_.PostSyncTask(
2160 [this, pointerEvent, pid, isNativeInject, isShell] {
2161 return this->CheckInjectPointerEvent(pointerEvent, pid, isNativeInject, isShell);
2162 }
2163 );
2164 #endif // OHOS_BUILD_ENABLE_ANCO
2165 if (ret != RET_OK) {
2166 MMI_HILOGE("Inject pointer event failed, ret:%{public}d", ret);
2167 return ret;
2168 }
2169 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
2170 return RET_OK;
2171 }
2172
2173 #ifdef OHOS_RSS_CLIENT
OnAddResSchedSystemAbility(int32_t systemAbilityId, const std::string &deviceId)2174 void MMIService::OnAddResSchedSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
2175 {
2176 int sleepSeconds = 1;
2177 sleep(sleepSeconds);
2178 uint64_t tid = tid_.load();
2179 int32_t userInteraction = 2;
2180 std::unordered_map<std::string, std::string> payload;
2181 payload["uid"] = std::to_string(getuid());
2182 payload["pid"] = std::to_string(getpid());
2183 payload["extType"] = "10002";
2184 payload["tid"] = std::to_string(tid);
2185 payload["isSa"] = "1";
2186 payload["cgroupPrio"] = "1";
2187 payload["threadName"] = "mmi_service";
2188 ResourceSchedule::ResSchedClient::GetInstance().ReportData(
2189 ResourceSchedule::ResType::RES_TYPE_KEY_PERF_SCENE, userInteraction, payload);
2190 }
2191 #endif // OHOS_RSS_CLIENT
2192
OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)2193 void MMIService::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
2194 {
2195 CALL_INFO_TRACE;
2196 MMI_HILOGI("systemAbilityId is %{public}d", systemAbilityId);
2197 #ifdef OHOS_RSS_CLIENT
2198 if (systemAbilityId == RES_SCHED_SYS_ABILITY_ID) {
2199 OnAddResSchedSystemAbility(systemAbilityId, deviceId);
2200 }
2201 #endif // OHOS_RSS_CLIENT
2202 #ifdef OHOS_BUILD_ENABLE_FINGERSENSE_WRAPPER
2203 if (systemAbilityId == COMMON_EVENT_SERVICE_ID) {
2204 isCesStart_ = true;
2205 }
2206 #endif // OHOS_BUILD_ENABLE_FINGERSENSE_WRAPPER
2207 if (systemAbilityId == APP_MGR_SERVICE_ID) {
2208 APP_OBSERVER_MGR->InitAppStateObserver();
2209 }
2210 if (systemAbilityId == COMMON_EVENT_SERVICE_ID) {
2211 DEVICE_MONITOR->InitCommonEventSubscriber();
2212 #if defined(OHOS_BUILD_ENABLE_KEYBOARD) && defined(OHOS_BUILD_ENABLE_FINGERSENSE_WRAPPER)
2213 DISPLAY_MONITOR->InitCommonEventSubscriber();
2214 #endif // OHOS_BUILD_ENABLE_KEYBOARD && OHOS_BUILD_ENABLE_FINGERSENSE_WRAPPER
2215 }
2216 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
2217 if (systemAbilityId == RENDER_SERVICE) {
2218 IPointerDrawingManager::GetInstance()->InitPointerCallback();
2219 }
2220 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
2221 if (systemAbilityId == DISPLAY_MANAGER_SERVICE_SA_ID) {
2222 WIN_MGR->SetFoldState();
2223 }
2224 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
2225 if (systemAbilityId == DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID) {
2226 if (SettingDataShare::GetInstance(DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID).CheckIfSettingsDataReady()) {
2227 IPointerDrawingManager::GetInstance()->InitPointerObserver();
2228 auto keyHandler = InputHandler->GetKeyCommandHandler();
2229 if (keyHandler != nullptr) {
2230 keyHandler->InitKeyObserver();
2231 }
2232 }
2233 }
2234 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
2235 }
2236
2237 #if defined(OHOS_BUILD_ENABLE_MONITOR) && defined(PLAYER_FRAMEWORK_EXISTS)
ScreenCaptureCallback(int32_t pid, bool isStart)2238 void MMIService::ScreenCaptureCallback(int32_t pid, bool isStart)
2239 {
2240 auto service = MMIService::GetInstance();
2241 CHKPV(service);
2242 int32_t ret = service->delegateTasks_.PostSyncTask(
2243 [pid, isStart] {
2244 auto monitorHandler = InputHandler->GetMonitorHandler();
2245 CHKPR(monitorHandler, RET_ERR);
2246 monitorHandler->ProcessScreenCapture(pid, isStart);
2247 return RET_OK;
2248 });
2249 }
2250
RegisterScreenCaptureCallback()2251 void MMIService::RegisterScreenCaptureCallback()
2252 {
2253 if (hasRegisterListener_) {
2254 return;
2255 }
2256 InputScreenCaptureAgent::GetInstance().RegisterListener(ScreenCaptureCallback);
2257 hasRegisterListener_ = true;
2258 }
2259 #endif // OHOS_BUILD_ENABLE_MONITOR && PLAYER_FRAMEWORK_EXISTS
2260
SubscribeKeyEvent(int32_t subscribeId, const std::shared_ptr<KeyOption> option)2261 int32_t MMIService::SubscribeKeyEvent(int32_t subscribeId, const std::shared_ptr<KeyOption> option)
2262 {
2263 CALL_DEBUG_ENTER;
2264 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
2265 int32_t pid = GetCallingPid();
2266 int32_t ret = delegateTasks_.PostSyncTask(
2267 [this, pid, subscribeId, option] {
2268 return sMsgHandler_.OnSubscribeKeyEvent(this, pid, subscribeId, option);
2269 });
2270 if (ret != RET_OK) {
2271 MMI_HILOGE("The subscribe key event processed failed, ret:%{public}d", ret);
2272 return ret;
2273 }
2274 if (NapProcess::GetInstance()->GetNapClientPid() != REMOVE_OBSERVER) {
2275 OHOS::MMI::NapProcess::NapStatusData napData;
2276 napData.pid = GetCallingPid();
2277 napData.uid = GetCallingUid();
2278 auto sess = GetSessionByPid(pid);
2279 CHKPR(sess, ERROR_NULL_POINTER);
2280 napData.bundleName = sess->GetProgramName();
2281 int32_t syncState = SUBSCRIBED;
2282 MMI_HILOGD("SubscribeKeyEvent info to observer : pid:%{public}d, uid:%{public}d, bundleName:%{public}s",
2283 napData.pid, napData.uid, napData.bundleName.c_str());
2284 NapProcess::GetInstance()->AddMmiSubscribedEventData(napData, syncState);
2285 if (NapProcess::GetInstance()->GetNapClientPid() != UNOBSERVED) {
2286 NapProcess::GetInstance()->NotifyBundleName(napData, syncState);
2287 }
2288 }
2289 #endif // OHOS_BUILD_ENABLE_KEYBOARD
2290 return RET_OK;
2291 }
2292
UnsubscribeKeyEvent(int32_t subscribeId)2293 int32_t MMIService::UnsubscribeKeyEvent(int32_t subscribeId)
2294 {
2295 CALL_INFO_TRACE;
2296 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
2297 int32_t pid = GetCallingPid();
2298 int32_t ret = delegateTasks_.PostSyncTask(
2299 [this, pid, subscribeId] {
2300 return sMsgHandler_.OnUnsubscribeKeyEvent(this, pid, subscribeId);
2301 });
2302 if (ret != RET_OK) {
2303 MMI_HILOGE("The unsubscribe key event processed failed, ret:%{public}d", ret);
2304 return ret;
2305 }
2306 if (NapProcess::GetInstance()->GetNapClientPid() != REMOVE_OBSERVER) {
2307 OHOS::MMI::NapProcess::NapStatusData napData;
2308 napData.pid = GetCallingPid();
2309 napData.uid = GetCallingUid();
2310 auto sess = GetSessionByPid(pid);
2311 CHKPR(sess, ERROR_NULL_POINTER);
2312 napData.bundleName = sess->GetProgramName();
2313 int32_t syncState = UNSUBSCRIBED;
2314 MMI_HILOGD("UnsubscribeKeyEvent info to observer : pid:%{public}d, uid:%{public}d, bundleName:%{public}s",
2315 napData.pid, napData.uid, napData.bundleName.c_str());
2316 NapProcess::GetInstance()->AddMmiSubscribedEventData(napData, syncState);
2317 if (NapProcess::GetInstance()->GetNapClientPid() != UNOBSERVED) {
2318 NapProcess::GetInstance()->NotifyBundleName(napData, syncState);
2319 }
2320 }
2321 #endif // OHOS_BUILD_ENABLE_KEYBOARD
2322 return RET_OK;
2323 }
2324
SubscribeHotkey(int32_t subscribeId, const std::shared_ptr<KeyOption> option)2325 int32_t MMIService::SubscribeHotkey(int32_t subscribeId, const std::shared_ptr<KeyOption> option)
2326 {
2327 CALL_DEBUG_ENTER;
2328 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
2329 int32_t pid = GetCallingPid();
2330 int32_t ret = delegateTasks_.PostSyncTask(
2331 [this, pid, subscribeId, option] {
2332 return sMsgHandler_.OnSubscribeHotkey(this, pid, subscribeId, option);
2333 });
2334 if (ret != RET_OK) {
2335 MMI_HILOGE("ServerMsgHandler::OnSubscribeHotkey fail, error:%{public}d", ret);
2336 return ret;
2337 }
2338 if (NapProcess::GetInstance()->GetNapClientPid() != REMOVE_OBSERVER) {
2339 OHOS::MMI::NapProcess::NapStatusData napData;
2340 napData.pid = GetCallingPid();
2341 napData.uid = GetCallingUid();
2342 auto sess = GetSessionByPid(pid);
2343 CHKPR(sess, ERROR_NULL_POINTER);
2344 napData.bundleName = sess->GetProgramName();
2345 int32_t syncState = SUBSCRIBED;
2346 MMI_HILOGD("SubscribeHotkey info to observer : pid:%{public}d, bundleName:%{public}s",
2347 napData.pid, napData.bundleName.c_str());
2348 NapProcess::GetInstance()->AddMmiSubscribedEventData(napData, syncState);
2349 if (NapProcess::GetInstance()->GetNapClientPid() != UNOBSERVED) {
2350 NapProcess::GetInstance()->NotifyBundleName(napData, syncState);
2351 }
2352 }
2353 #endif // OHOS_BUILD_ENABLE_KEYBOARD
2354 return RET_OK;
2355 }
2356
UnsubscribeHotkey(int32_t subscribeId)2357 int32_t MMIService::UnsubscribeHotkey(int32_t subscribeId)
2358 {
2359 CALL_INFO_TRACE;
2360 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
2361 int32_t pid = GetCallingPid();
2362 int32_t ret = delegateTasks_.PostSyncTask(
2363 [this, pid, subscribeId] {
2364 return sMsgHandler_.OnUnsubscribeHotkey(this, pid, subscribeId);
2365 });
2366 if (ret != RET_OK) {
2367 MMI_HILOGE("ServerMsgHandler::OnUnsubscribeHotkey fail, error:%{public}d", ret);
2368 return ret;
2369 }
2370 if (NapProcess::GetInstance()->GetNapClientPid() != REMOVE_OBSERVER) {
2371 OHOS::MMI::NapProcess::NapStatusData napData;
2372 napData.pid = GetCallingPid();
2373 napData.uid = GetCallingUid();
2374 auto sess = GetSessionByPid(pid);
2375 CHKPR(sess, ERROR_NULL_POINTER);
2376 napData.bundleName = sess->GetProgramName();
2377 int32_t syncState = UNSUBSCRIBED;
2378 MMI_HILOGD("UnsubscribeHotkey info to observer : pid:%{public}d, bundleName:%{public}s",
2379 napData.pid, napData.bundleName.c_str());
2380 NapProcess::GetInstance()->AddMmiSubscribedEventData(napData, syncState);
2381 if (NapProcess::GetInstance()->GetNapClientPid() != UNOBSERVED) {
2382 NapProcess::GetInstance()->NotifyBundleName(napData, syncState);
2383 }
2384 }
2385 #endif // OHOS_BUILD_ENABLE_KEYBOARD
2386 return RET_OK;
2387 }
2388
SubscribeSwitchEvent(int32_t subscribeId, int32_t switchType)2389 int32_t MMIService::SubscribeSwitchEvent(int32_t subscribeId, int32_t switchType)
2390 {
2391 CALL_INFO_TRACE;
2392 #ifdef OHOS_BUILD_ENABLE_SWITCH
2393 int32_t pid = GetCallingPid();
2394 int32_t ret = delegateTasks_.PostSyncTask(
2395 [this, pid, subscribeId, switchType] {
2396 return sMsgHandler_.OnSubscribeSwitchEvent(this, pid, subscribeId, switchType);
2397 }
2398 );
2399 if (ret != RET_OK) {
2400 MMI_HILOGE("The subscribe switch event processed failed, ret:%{public}d", ret);
2401 return ret;
2402 }
2403 #endif // OHOS_BUILD_ENABLE_SWITCH
2404 return RET_OK;
2405 }
2406
UnsubscribeSwitchEvent(int32_t subscribeId)2407 int32_t MMIService::UnsubscribeSwitchEvent(int32_t subscribeId)
2408 {
2409 CALL_INFO_TRACE;
2410 #ifdef OHOS_BUILD_ENABLE_SWITCH
2411 int32_t pid = GetCallingPid();
2412 int32_t ret = delegateTasks_.PostSyncTask(
2413 [this, pid, subscribeId] {
2414 return sMsgHandler_.OnUnsubscribeSwitchEvent(this, pid, subscribeId);
2415 }
2416 );
2417 if (ret != RET_OK) {
2418 MMI_HILOGE("The unsubscribe switch event processed failed, ret:%{public}d", ret);
2419 return ret;
2420 }
2421 #endif // OHOS_BUILD_ENABLE_SWITCH
2422 return RET_OK;
2423 }
2424
SetAnrObserver()2425 int32_t MMIService::SetAnrObserver()
2426 {
2427 CALL_INFO_TRACE;
2428 int32_t pid = GetCallingPid();
2429 int32_t ret = delegateTasks_.PostSyncTask(
2430 [pid] {
2431 return ::OHOS::DelayedSingleton<ANRManager>::GetInstance()->SetANRNoticedPid(pid);
2432 }
2433 );
2434 if (ret != RET_OK) {
2435 MMI_HILOGE("Set ANRNoticed pid failed, ret:%{public}d", ret);
2436 return ret;
2437 }
2438 return RET_OK;
2439 }
2440
GetDisplayBindInfo(DisplayBindInfos &infos)2441 int32_t MMIService::GetDisplayBindInfo(DisplayBindInfos &infos)
2442 {
2443 CALL_INFO_TRACE;
2444 int32_t ret = delegateTasks_.PostSyncTask(
2445 [&infos] {
2446 return ::OHOS::MMI::IInputWindowsManager::GetInstance()->GetDisplayBindInfo(infos);
2447 }
2448 );
2449 if (ret != RET_OK) {
2450 MMI_HILOGE("GetDisplayBindInfo pid failed, ret:%{public}d", ret);
2451 return ret;
2452 }
2453 return RET_OK;
2454 }
2455
GetAllMmiSubscribedEvents(std::map<std::tuple<int32_t, int32_t, std::string>, int32_t> &datas)2456 int32_t MMIService::GetAllMmiSubscribedEvents(std::map<std::tuple<int32_t, int32_t, std::string>, int32_t> &datas)
2457 {
2458 CALL_INFO_TRACE;
2459 NapProcess::GetInstance()->GetAllMmiSubscribedEvents(datas);
2460 return RET_OK;
2461 }
2462
SetDisplayBind(int32_t deviceId, int32_t displayId, std::string &msg)2463 int32_t MMIService::SetDisplayBind(int32_t deviceId, int32_t displayId, std::string &msg)
2464 {
2465 CALL_INFO_TRACE;
2466 int32_t ret = delegateTasks_.PostSyncTask(
2467 [deviceId, displayId, &msg] {
2468 return ::OHOS::MMI::IInputWindowsManager::GetInstance()->SetDisplayBind(deviceId, displayId, msg);
2469 }
2470 );
2471 if (ret != RET_OK) {
2472 MMI_HILOGE("SetDisplayBind pid failed, ret:%{public}d", ret);
2473 return ret;
2474 }
2475 return RET_OK;
2476 }
2477
GetFunctionKeyState(int32_t funcKey, bool &state)2478 int32_t MMIService::GetFunctionKeyState(int32_t funcKey, bool &state)
2479 {
2480 CALL_INFO_TRACE;
2481 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
2482 int32_t ret = delegateTasks_.PostSyncTask(
2483 [this, funcKey, &state] {
2484 return sMsgHandler_.OnGetFunctionKeyState(funcKey, state);
2485 }
2486 );
2487 if (ret != RET_OK) {
2488 MMI_HILOGE("Failed to get the keyboard status, ret:%{public}d", ret);
2489 return ret;
2490 }
2491 #endif // OHOS_BUILD_ENABLE_KEYBOARD
2492 return RET_OK;
2493 }
2494
SetFunctionKeyState(int32_t funcKey, bool enable)2495 int32_t MMIService::SetFunctionKeyState(int32_t funcKey, bool enable)
2496 {
2497 CALL_INFO_TRACE;
2498 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
2499 int32_t ret = delegateTasks_.PostSyncTask(
2500 [this, funcKey, enable] {
2501 return sMsgHandler_.OnSetFunctionKeyState(funcKey, enable);
2502 }
2503 );
2504 if (ret != RET_OK) {
2505 MMI_HILOGE("Failed to update the keyboard status, ret:%{public}d", ret);
2506 return ret;
2507 }
2508 #endif // OHOS_BUILD_ENABLE_KEYBOARD
2509 return RET_OK;
2510 }
2511
SetPointerLocation(int32_t x, int32_t y)2512 int32_t MMIService::SetPointerLocation(int32_t x, int32_t y)
2513 {
2514 CALL_INFO_TRACE;
2515 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
2516 int32_t ret = delegateTasks_.PostSyncTask(
2517 [x, y] {
2518 return ::OHOS::DelayedSingleton<MouseEventNormalize>::GetInstance()->SetPointerLocation(x, y);
2519 }
2520 );
2521 if (ret != RET_OK) {
2522 MMI_HILOGE("Set pointer location failed, ret:%{public}d", ret);
2523 return ret;
2524 }
2525 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
2526 return RET_OK;
2527 }
2528
OnDelegateTask(epoll_event &ev)2529 void MMIService::OnDelegateTask(epoll_event &ev)
2530 {
2531 if ((ev.events & EPOLLIN) == 0) {
2532 MMI_HILOGW("Not epollin");
2533 return;
2534 }
2535 DelegateTasks::TaskData data = {};
2536 auto res = read(delegateTasks_.GetReadFd(), &data, sizeof(data));
2537 if (res == -1) {
2538 MMI_HILOGW("Read failed erron:%{public}d", errno);
2539 }
2540 MMI_HILOGD("RemoteRequest notify td:%{public}" PRId64 ",std:%{public}" PRId64 ""
2541 ",taskId:%{public}d",
2542 GetThisThreadId(), data.tid, data.taskId);
2543 delegateTasks_.ProcessTasks();
2544 }
2545
OnThread()2546 void MMIService::OnThread()
2547 {
2548 SetThreadName(std::string("mmi_service"));
2549 uint64_t tid = GetThisThreadId();
2550 delegateTasks_.SetWorkerThreadId(tid);
2551 MMI_HILOGI("Main worker thread start. tid:%{public}" PRId64 "", tid);
2552 #ifdef OHOS_RSS_CLIENT
2553 tid_.store(tid);
2554 #endif // OHOS_RSS_CLIENT
2555 libinputAdapter_.ProcessPendingEvents();
2556 while (state_ == ServiceRunningState::STATE_RUNNING) {
2557 #if defined(OHOS_BUILD_ENABLE_FINGERSENSE_WRAPPER) && defined(OHOS_BUILD_ENABLE_KEYBOARD)
2558 if (isCesStart_ && !DISPLAY_MONITOR->IsCommonEventSubscriberInit()) {
2559 DISPLAY_MONITOR->InitCommonEventSubscriber();
2560 }
2561 #endif // OHOS_BUILD_ENABLE_FINGERSENSE_WRAPPER && OHOS_BUILD_ENABLE_KEYBOARD
2562 epoll_event ev[MAX_EVENT_SIZE] = {};
2563 int32_t timeout = TimerMgr->CalcNextDelay();
2564 MMI_HILOGD("timeout:%{public}d", timeout);
2565 int32_t count = EpollWait(ev[0], MAX_EVENT_SIZE, timeout, mmiFd_);
2566 for (int32_t i = 0; i < count && state_ == ServiceRunningState::STATE_RUNNING; i++) {
2567 auto mmiEdIter = epollEventMap_.find(ev[i].data.fd);
2568 if (mmiEdIter == epollEventMap_.end()) {
2569 MMI_HILOGW("Invalid event %{public}d %{public}d", ev[i].data.fd, count);
2570 continue;
2571 }
2572 std::shared_ptr<mmi_epoll_event> mmiEd = mmiEdIter->second;
2573 CHKPC(mmiEd);
2574 epoll_event event = ev[i];
2575 if (mmiEd->event_type == EPOLL_EVENT_INPUT) {
2576 CalculateFuntionRunningTime([this, &mmiEd] () { libinputAdapter_.EventDispatch(mmiEd->fd); },
2577 "EPOLL_EVENT_INPUT");
2578 } else if (mmiEd->event_type == EPOLL_EVENT_SOCKET) {
2579 CalculateFuntionRunningTime([this, &event]() { this->OnEpollEvent(event); }, "MMI:EPOLL_EVENT_SOCKET");
2580 } else if (mmiEd->event_type == EPOLL_EVENT_SIGNAL) {
2581 OnSignalEvent(mmiEd->fd);
2582 } else if (mmiEd->event_type == EPOLL_EVENT_ETASK) {
2583 CalculateFuntionRunningTime([this, &event]() { this->OnDelegateTask(event); }, "MMI:EPOLL_EVENT_ETASK");
2584 } else {
2585 MMI_HILOGW("Unknown epoll event type:%{public}d", mmiEd->event_type);
2586 }
2587 }
2588 TimerMgr->ProcessTimers();
2589 if (state_ != ServiceRunningState::STATE_RUNNING) {
2590 break;
2591 }
2592 }
2593 MMI_HILOGI("Main worker thread stop. tid:%{public}" PRId64 "", tid);
2594 }
2595
InitSignalHandler()2596 bool MMIService::InitSignalHandler()
2597 {
2598 CALL_DEBUG_ENTER;
2599 sigset_t mask = { 0 };
2600 int32_t retCode = sigfillset(&mask);
2601 if (retCode < 0) {
2602 MMI_HILOGE("Fill signal set failed:%{public}d", errno);
2603 return false;
2604 }
2605
2606 retCode = sigprocmask(SIG_SETMASK, &mask, nullptr);
2607 if (retCode < 0) {
2608 MMI_HILOGE("Sigprocmask failed:%{public}d", errno);
2609 return false;
2610 }
2611
2612 int32_t fdSignal = signalfd(-1, &mask, SFD_NONBLOCK | SFD_CLOEXEC);
2613 if (fdSignal < 0) {
2614 MMI_HILOGE("Signal fd failed:%{public}d", errno);
2615 return false;
2616 }
2617
2618 retCode = AddEpoll(EPOLL_EVENT_SIGNAL, fdSignal);
2619 if (retCode < 0) {
2620 MMI_HILOGE("AddEpoll signalFd failed:%{public}d", retCode);
2621 close(fdSignal);
2622 return false;
2623 }
2624 return true;
2625 }
2626
OnSignalEvent(int32_t signalFd)2627 void MMIService::OnSignalEvent(int32_t signalFd)
2628 {
2629 CALL_DEBUG_ENTER;
2630 signalfd_siginfo sigInfo;
2631 int32_t size = ::read(signalFd, &sigInfo, sizeof(signalfd_siginfo));
2632 if (size != static_cast<int32_t>(sizeof(signalfd_siginfo))) {
2633 MMI_HILOGE("Read signal info failed, invalid size:%{public}d, errno:%{public}d", size, errno);
2634 return;
2635 }
2636 int32_t signo = static_cast<int32_t>(sigInfo.ssi_signo);
2637 MMI_HILOGD("Receive signal:%{public}d", signo);
2638 switch (signo) {
2639 case SIGINT:
2640 case SIGQUIT:
2641 case SIGILL:
2642 case SIGABRT:
2643 case SIGBUS:
2644 case SIGFPE:
2645 case SIGKILL:
2646 case SIGSEGV:
2647 case SIGTERM: {
2648 state_ = ServiceRunningState::STATE_EXIT;
2649 break;
2650 }
2651 default: {
2652 break;
2653 }
2654 }
2655 }
2656
AddReloadDeviceTimer()2657 void MMIService::AddReloadDeviceTimer()
2658 {
2659 CALL_DEBUG_ENTER;
2660 TimerMgr->AddTimer(RELOAD_DEVICE_TIME, REPEAT_COUNT, [this]() {
2661 auto deviceIds = INPUT_DEV_MGR->GetInputDeviceIds();
2662 if (deviceIds.empty()) {
2663 libinputAdapter_.ReloadDevice();
2664 }
2665 });
2666 }
2667
Dump(int32_t fd, const std::vector<std::u16string> &args)2668 int32_t MMIService::Dump(int32_t fd, const std::vector<std::u16string> &args)
2669 {
2670 CALL_DEBUG_ENTER;
2671 if (fd < 0) {
2672 MMI_HILOGE("The fd is invalid");
2673 return DUMP_PARAM_ERR;
2674 }
2675 if (args.empty()) {
2676 MMI_HILOGE("The args cannot be empty");
2677 mprintf(fd, "args cannot be empty\n");
2678 MMIEventDump->DumpHelp(fd);
2679 return DUMP_PARAM_ERR;
2680 }
2681 std::vector<std::string> argList = { "" };
2682 std::transform(args.begin(), args.end(), std::back_inserter(argList),
2683 [](const std::u16string &arg) { return Str16ToStr8(arg); });
2684 MMIEventDump->ParseCommand(fd, argList);
2685 return RET_OK;
2686 }
2687
SetMouseCaptureMode(int32_t windowId, bool isCaptureMode)2688 int32_t MMIService::SetMouseCaptureMode(int32_t windowId, bool isCaptureMode)
2689 {
2690 CALL_INFO_TRACE;
2691 int32_t ret = delegateTasks_.PostSyncTask(
2692 [windowId, isCaptureMode] {
2693 return ::OHOS::MMI::IInputWindowsManager::GetInstance()->SetMouseCaptureMode(windowId, isCaptureMode);
2694 }
2695 );
2696 if (ret != RET_OK) {
2697 MMI_HILOGE("Set capture failed, return:%{public}d", ret);
2698 return ret;
2699 }
2700 return RET_OK;
2701 }
2702
OnGetWindowPid(int32_t windowId, int32_t &windowPid)2703 int32_t MMIService::OnGetWindowPid(int32_t windowId, int32_t &windowPid)
2704 {
2705 CALL_DEBUG_ENTER;
2706 windowPid = WIN_MGR->GetWindowPid(windowId);
2707 if (windowPid == RET_ERR) {
2708 MMI_HILOGE("Get window pid failed");
2709 return RET_ERR;
2710 }
2711 MMI_HILOGD("windowpid is %{public}d", windowPid);
2712 return RET_OK;
2713 }
2714
GetWindowPid(int32_t windowId)2715 int32_t MMIService::GetWindowPid(int32_t windowId)
2716 {
2717 CALL_INFO_TRACE;
2718 int32_t windowPid = INVALID_PID;
2719 int32_t ret = delegateTasks_.PostSyncTask(
2720 [this, windowId, &windowPid] {
2721 return this->OnGetWindowPid(windowId, windowPid);
2722 }
2723 );
2724 if (ret != RET_OK) {
2725 MMI_HILOGE("OnGetWindowPid failed, ret:%{public}d", ret);
2726 return ret;
2727 }
2728 MMI_HILOGD("windowpid is %{public}d", windowPid);
2729 return windowPid;
2730 }
2731
AppendExtraData(const ExtraData &extraData)2732 int32_t MMIService::AppendExtraData(const ExtraData &extraData)
2733 {
2734 CALL_DEBUG_ENTER;
2735 int32_t ret = delegateTasks_.PostSyncTask(
2736 [extraData] {
2737 return ::OHOS::MMI::IInputWindowsManager::GetInstance()->AppendExtraData(extraData);
2738 }
2739 );
2740 if (ret != RET_OK) {
2741 MMI_HILOGE("Append extra data failed:%{public}d", ret);
2742 }
2743 return ret;
2744 }
2745
EnableInputDevice(bool enable)2746 int32_t MMIService::EnableInputDevice(bool enable)
2747 {
2748 CALL_DEBUG_ENTER;
2749 int32_t ret = delegateTasks_.PostSyncTask(
2750 [enable] {
2751 return ::OHOS::MMI::InputDeviceManager::GetInstance()->OnEnableInputDevice(enable);
2752 }
2753 );
2754 if (ret != RET_OK) {
2755 MMI_HILOGE("OnEnableInputDevice failed:%{public}d", ret);
2756 }
2757 return ret;
2758 }
2759
2760 #if defined(OHOS_BUILD_ENABLE_KEYBOARD) && defined(OHOS_BUILD_ENABLE_COMBINATION_KEY)
UpdateCombineKeyState(bool enable)2761 int32_t MMIService::UpdateCombineKeyState(bool enable)
2762 {
2763 auto eventSubscriberHandler = InputHandler->GetSubscriberHandler();
2764 CHKPR(eventSubscriberHandler, RET_ERR);
2765 int32_t ret = eventSubscriberHandler->EnableCombineKey(enable);
2766 if (ret != RET_OK) {
2767 MMI_HILOGE("EnableCombineKey is failed in key command:%{public}d", ret);
2768 }
2769
2770 auto eventKeyCommandHandler = InputHandler->GetKeyCommandHandler();
2771 CHKPR(eventKeyCommandHandler, RET_ERR);
2772 ret = eventKeyCommandHandler->EnableCombineKey(enable);
2773 if (ret != RET_OK) {
2774 MMI_HILOGE("EnableCombineKey is failed in key command:%{public}d", ret);
2775 }
2776 return ret;
2777 }
2778 #endif // OHOS_BUILD_ENABLE_KEYBOARD && OHOS_BUILD_ENABLE_COMBINATION_KEY
2779
CheckPidPermission(int32_t pid)2780 int32_t MMIService::CheckPidPermission(int32_t pid)
2781 {
2782 CALL_DEBUG_ENTER;
2783 int32_t checkingPid = GetCallingPid();
2784 if (checkingPid != pid) {
2785 MMI_HILOGE("check pid failed, input pid:%{public}d, but checking pid:%{public}d", pid, checkingPid);
2786 return RET_ERR;
2787 }
2788 return RET_OK;
2789 }
2790
EnableCombineKey(bool enable)2791 int32_t MMIService::EnableCombineKey(bool enable)
2792 {
2793 CALL_DEBUG_ENTER;
2794 #if defined(OHOS_BUILD_ENABLE_KEYBOARD) && defined(OHOS_BUILD_ENABLE_COMBINATION_KEY)
2795 int32_t ret = delegateTasks_.PostSyncTask(
2796 [this, enable] {
2797 return this->UpdateCombineKeyState(enable);
2798 }
2799 );
2800 if (ret != RET_OK) {
2801 MMI_HILOGE("Set key down duration failed:%{public}d", ret);
2802 return ret;
2803 }
2804 #endif // OHOS_BUILD_ENABLE_KEYBOARD && OHOS_BUILD_ENABLE_COMBINATION_KEY
2805 return RET_OK;
2806 }
2807
2808 #if defined(OHOS_BUILD_ENABLE_KEYBOARD) && defined(OHOS_BUILD_ENABLE_COMBINATION_KEY)
UpdateSettingsXml(const std::string &businessId, int32_t delay)2809 int32_t MMIService::UpdateSettingsXml(const std::string &businessId, int32_t delay)
2810 {
2811 std::shared_ptr<KeyCommandHandler> eventKeyCommandHandler = InputHandler->GetKeyCommandHandler();
2812 CHKPR(eventKeyCommandHandler, RET_ERR);
2813 return eventKeyCommandHandler->UpdateSettingsXml(businessId, delay);
2814 }
2815 #endif // OHOS_BUILD_ENABLE_KEYBOARD && OHOS_BUILD_ENABLE_COMBINATION_KEY
2816
SetKeyDownDuration(const std::string &businessId, int32_t delay)2817 int32_t MMIService::SetKeyDownDuration(const std::string &businessId, int32_t delay)
2818 {
2819 CALL_INFO_TRACE;
2820 #if defined(OHOS_BUILD_ENABLE_KEYBOARD) && defined(OHOS_BUILD_ENABLE_COMBINATION_KEY)
2821 int32_t ret = delegateTasks_.PostSyncTask(
2822 [this, businessId, delay] {
2823 return this->UpdateSettingsXml(businessId, delay);
2824 }
2825 );
2826 if (ret != RET_OK) {
2827 MMI_HILOGE("Set key down duration failed:%{public}d", ret);
2828 return ret;
2829 }
2830 #endif // OHOS_BUILD_ENABLE_KEYBOARD && OHOS_BUILD_ENABLE_COMBINATION_KEY
2831 return RET_OK;
2832 }
2833
2834 #ifdef OHOS_BUILD_ENABLE_POINTER
ReadTouchpadScrollSwich(bool &switchFlag)2835 int32_t MMIService::ReadTouchpadScrollSwich(bool &switchFlag)
2836 {
2837 MouseEventHdr->GetTouchpadScrollSwitch(switchFlag);
2838 return RET_OK;
2839 }
2840
ReadTouchpadScrollDirection(bool &switchFlag)2841 int32_t MMIService::ReadTouchpadScrollDirection(bool &switchFlag)
2842 {
2843 MouseEventHdr->GetTouchpadScrollDirection(switchFlag);
2844 return RET_OK;
2845 }
2846
ReadTouchpadTapSwitch(bool &switchFlag)2847 int32_t MMIService::ReadTouchpadTapSwitch(bool &switchFlag)
2848 {
2849 MouseEventHdr->GetTouchpadTapSwitch(switchFlag);
2850 return RET_OK;
2851 }
2852
ReadTouchpadPointerSpeed(int32_t &speed)2853 int32_t MMIService::ReadTouchpadPointerSpeed(int32_t &speed)
2854 {
2855 MouseEventHdr->GetTouchpadPointerSpeed(speed);
2856 return RET_OK;
2857 }
2858
ReadTouchpadPinchSwitch(bool &switchFlag)2859 int32_t MMIService::ReadTouchpadPinchSwitch(bool &switchFlag)
2860 {
2861 TOUCH_EVENT_HDR->GetTouchpadPinchSwitch(switchFlag);
2862 return RET_OK;
2863 }
2864
ReadTouchpadSwipeSwitch(bool &switchFlag)2865 int32_t MMIService::ReadTouchpadSwipeSwitch(bool &switchFlag)
2866 {
2867 TOUCH_EVENT_HDR->GetTouchpadSwipeSwitch(switchFlag);
2868 return RET_OK;
2869 }
2870
ReadTouchpadRightMenuType(int32_t &type)2871 int32_t MMIService::ReadTouchpadRightMenuType(int32_t &type)
2872 {
2873 MouseEventHdr->GetTouchpadRightClickType(type);
2874 return RET_OK;
2875 }
2876
ReadTouchpadRotateSwitch(bool &rotateSwitch)2877 int32_t MMIService::ReadTouchpadRotateSwitch(bool &rotateSwitch)
2878 {
2879 TOUCH_EVENT_HDR->GetTouchpadRotateSwitch(rotateSwitch);
2880 return RET_OK;
2881 }
2882
2883 #endif // OHOS_BUILD_ENABLE_POINTER
2884
SetTouchpadScrollSwitch(bool switchFlag)2885 int32_t MMIService::SetTouchpadScrollSwitch(bool switchFlag)
2886 {
2887 CALL_INFO_TRACE;
2888 #if defined OHOS_BUILD_ENABLE_POINTER
2889 int32_t ret = delegateTasks_.PostSyncTask(
2890 [switchFlag] {
2891 return ::OHOS::DelayedSingleton<MouseEventNormalize>::GetInstance()->SetTouchpadScrollSwitch(switchFlag);
2892 }
2893 );
2894 if (ret != RET_OK) {
2895 MMI_HILOGE("Set touchpad scroll switch failed, return:%{public}d", ret);
2896 return ret;
2897 }
2898 #endif // OHOS_BUILD_ENABLE_POINTER
2899 return RET_OK;
2900 }
2901
GetTouchpadScrollSwitch(bool &switchFlag)2902 int32_t MMIService::GetTouchpadScrollSwitch(bool &switchFlag)
2903 {
2904 CALL_INFO_TRACE;
2905 #ifdef OHOS_BUILD_ENABLE_POINTER
2906 int32_t ret = delegateTasks_.PostSyncTask(
2907 [this, &switchFlag] {
2908 return this->ReadTouchpadScrollSwich(switchFlag);
2909 }
2910 );
2911 if (ret != RET_OK) {
2912 MMI_HILOGE("Get touchpad scroll switch failed, return:%{public}d", ret);
2913 return ret;
2914 }
2915 #endif // OHOS_BUILD_ENABLE_POINTER
2916 return RET_OK;
2917 }
2918
SetTouchpadScrollDirection(bool state)2919 int32_t MMIService::SetTouchpadScrollDirection(bool state)
2920 {
2921 CALL_INFO_TRACE;
2922 #if defined OHOS_BUILD_ENABLE_POINTER
2923 int32_t ret = delegateTasks_.PostSyncTask(
2924 [state] {
2925 return ::OHOS::DelayedSingleton<MouseEventNormalize>::GetInstance()->SetTouchpadScrollDirection(state);
2926 }
2927 );
2928 if (ret != RET_OK) {
2929 MMI_HILOGE("Set touchpad scroll direction switch failed, return:%{public}d", ret);
2930 return ret;
2931 }
2932 #endif // OHOS_BUILD_ENABLE_POINTER
2933 return RET_OK;
2934 }
2935
GetTouchpadScrollDirection(bool &state)2936 int32_t MMIService::GetTouchpadScrollDirection(bool &state)
2937 {
2938 CALL_INFO_TRACE;
2939 #ifdef OHOS_BUILD_ENABLE_POINTER
2940 int32_t ret = delegateTasks_.PostSyncTask(
2941 [this, &state] {
2942 return this->ReadTouchpadScrollDirection(state);
2943 }
2944 );
2945 if (ret != RET_OK) {
2946 MMI_HILOGE("Get touchpad scroll direction switch failed, return:%{public}d", ret);
2947 return ret;
2948 }
2949 #endif // OHOS_BUILD_ENABLE_POINTER
2950 return RET_OK;
2951 }
2952
SetTouchpadTapSwitch(bool switchFlag)2953 int32_t MMIService::SetTouchpadTapSwitch(bool switchFlag)
2954 {
2955 CALL_INFO_TRACE;
2956 #if defined OHOS_BUILD_ENABLE_POINTER
2957 int32_t ret = delegateTasks_.PostSyncTask(
2958 [switchFlag] {
2959 return ::OHOS::DelayedSingleton<MouseEventNormalize>::GetInstance()->SetTouchpadTapSwitch(switchFlag);
2960 }
2961 );
2962 if (ret != RET_OK) {
2963 MMI_HILOGE("Set touchpad tap switch failed, return:%{public}d", ret);
2964 return ret;
2965 }
2966 #endif // OHOS_BUILD_ENABLE_POINTER
2967 return RET_OK;
2968 }
2969
GetTouchpadTapSwitch(bool &switchFlag)2970 int32_t MMIService::GetTouchpadTapSwitch(bool &switchFlag)
2971 {
2972 CALL_INFO_TRACE;
2973 #ifdef OHOS_BUILD_ENABLE_POINTER
2974 int32_t ret = delegateTasks_.PostSyncTask(
2975 [this, &switchFlag] {
2976 return this->ReadTouchpadTapSwitch(switchFlag);
2977 }
2978 );
2979 if (ret != RET_OK) {
2980 MMI_HILOGE("Get touchpad tap switch failed, return:%{public}d", ret);
2981 return ret;
2982 }
2983 #endif // OHOS_BUILD_ENABLE_POINTER
2984 return RET_OK;
2985 }
2986
SetTouchpadPointerSpeed(int32_t speed)2987 int32_t MMIService::SetTouchpadPointerSpeed(int32_t speed)
2988 {
2989 CALL_INFO_TRACE;
2990 #if defined OHOS_BUILD_ENABLE_POINTER
2991 int32_t ret = delegateTasks_.PostSyncTask(
2992 [speed] {
2993 return ::OHOS::DelayedSingleton<MouseEventNormalize>::GetInstance()->SetTouchpadPointerSpeed(speed);
2994 }
2995 );
2996 if (ret != RET_OK) {
2997 MMI_HILOGE("Set touchpad speed failed, return:%{public}d", ret);
2998 return ret;
2999 }
3000 #endif // OHOS_BUILD_ENABLE_POINTER
3001 return RET_OK;
3002 }
3003
GetTouchpadPointerSpeed(int32_t &speed)3004 int32_t MMIService::GetTouchpadPointerSpeed(int32_t &speed)
3005 {
3006 CALL_INFO_TRACE;
3007 #ifdef OHOS_BUILD_ENABLE_POINTER
3008 int32_t ret = delegateTasks_.PostSyncTask(
3009 [this, &speed] {
3010 return this->ReadTouchpadPointerSpeed(speed);
3011 }
3012 );
3013 if (ret != RET_OK) {
3014 MMI_HILOGE("Get touchpad speed failed, return:%{public}d", ret);
3015 return ret;
3016 }
3017 #endif // OHOS_BUILD_ENABLE_POINTER
3018 return RET_OK;
3019 }
3020
SetTouchpadPinchSwitch(bool switchFlag)3021 int32_t MMIService::SetTouchpadPinchSwitch(bool switchFlag)
3022 {
3023 CALL_INFO_TRACE;
3024 #if defined OHOS_BUILD_ENABLE_POINTER
3025 int32_t ret = delegateTasks_.PostSyncTask(
3026 [switchFlag] {
3027 return ::OHOS::DelayedSingleton<TouchEventNormalize>::GetInstance()->SetTouchpadPinchSwitch(switchFlag);
3028 }
3029 );
3030 if (ret != RET_OK) {
3031 MMI_HILOGE("Set touch pad pinch switch failed, return:%{public}d", ret);
3032 return ret;
3033 }
3034 #endif // OHOS_BUILD_ENABLE_POINTER
3035 return RET_OK;
3036 }
3037
GetTouchpadPinchSwitch(bool &switchFlag)3038 int32_t MMIService::GetTouchpadPinchSwitch(bool &switchFlag)
3039 {
3040 CALL_INFO_TRACE;
3041 #ifdef OHOS_BUILD_ENABLE_POINTER
3042 int32_t ret = delegateTasks_.PostSyncTask(
3043 [this, &switchFlag] {
3044 return this->ReadTouchpadPinchSwitch(switchFlag);
3045 }
3046 );
3047 if (ret != RET_OK) {
3048 MMI_HILOGE("Get touch pad pinch switch failed, return:%{public}d", ret);
3049 return ret;
3050 }
3051 #endif // OHOS_BUILD_ENABLE_POINTER
3052 return RET_OK;
3053 }
3054
SetTouchpadSwipeSwitch(bool switchFlag)3055 int32_t MMIService::SetTouchpadSwipeSwitch(bool switchFlag)
3056 {
3057 CALL_INFO_TRACE;
3058 #if defined OHOS_BUILD_ENABLE_POINTER
3059 int32_t ret = delegateTasks_.PostSyncTask(
3060 [switchFlag] {
3061 return ::OHOS::DelayedSingleton<TouchEventNormalize>::GetInstance()->SetTouchpadSwipeSwitch(switchFlag);
3062 }
3063 );
3064 if (ret != RET_OK) {
3065 MMI_HILOGE("Set touchpad swipe switch failed, return:%{public}d", ret);
3066 return ret;
3067 }
3068 #endif // OHOS_BUILD_ENABLE_POINTER
3069 return RET_OK;
3070 }
3071
GetTouchpadSwipeSwitch(bool &switchFlag)3072 int32_t MMIService::GetTouchpadSwipeSwitch(bool &switchFlag)
3073 {
3074 CALL_INFO_TRACE;
3075 #ifdef OHOS_BUILD_ENABLE_POINTER
3076 int32_t ret = delegateTasks_.PostSyncTask(
3077 [this, &switchFlag] {
3078 return this->ReadTouchpadSwipeSwitch(switchFlag);
3079 }
3080 );
3081 if (ret != RET_OK) {
3082 MMI_HILOGE("Get touchpad swipe switch failed, return:%{public}d", ret);
3083 return ret;
3084 }
3085 #endif // OHOS_BUILD_ENABLE_POINTER
3086 return RET_OK;
3087 }
3088
SetTouchpadRightClickType(int32_t type)3089 int32_t MMIService::SetTouchpadRightClickType(int32_t type)
3090 {
3091 CALL_INFO_TRACE;
3092 #if defined OHOS_BUILD_ENABLE_POINTER
3093 int32_t ret = delegateTasks_.PostSyncTask(
3094 [type] {
3095 return ::OHOS::DelayedSingleton<MouseEventNormalize>::GetInstance()->SetTouchpadRightClickType(type);
3096 }
3097 );
3098 if (ret != RET_OK) {
3099 MMI_HILOGE("Set touchpad right button menu type failed, return:%{public}d", ret);
3100 return ret;
3101 }
3102 #endif // OHOS_BUILD_ENABLE_POINTER
3103 return RET_OK;
3104 }
3105
GetTouchpadRightClickType(int32_t &type)3106 int32_t MMIService::GetTouchpadRightClickType(int32_t &type)
3107 {
3108 CALL_INFO_TRACE;
3109 #ifdef OHOS_BUILD_ENABLE_POINTER
3110 int32_t ret = delegateTasks_.PostSyncTask(
3111 [this, &type] {
3112 return this->ReadTouchpadRightMenuType(type);
3113 }
3114 );
3115 if (ret != RET_OK) {
3116 MMI_HILOGE("Get touchpad right button menu type failed, return:%{public}d", ret);
3117 return ret;
3118 }
3119 #endif // OHOS_BUILD_ENABLE_POINTER
3120 return RET_OK;
3121 }
3122
SetTouchpadRotateSwitch(bool rotateSwitch)3123 int32_t MMIService::SetTouchpadRotateSwitch(bool rotateSwitch)
3124 {
3125 CALL_INFO_TRACE;
3126 #if defined OHOS_BUILD_ENABLE_POINTER
3127 int32_t ret = delegateTasks_.PostSyncTask(
3128 [rotateSwitch] {
3129 return ::OHOS::DelayedSingleton<TouchEventNormalize>::GetInstance()->SetTouchpadRotateSwitch(rotateSwitch);
3130 }
3131 );
3132 if (ret != RET_OK) {
3133 MMI_HILOGE("Set touchpad rotate switch failed, ret:%{public}d", ret);
3134 return ret;
3135 }
3136 #endif // OHOS_BUILD_ENABLE_POINTER
3137 return RET_OK;
3138 }
3139
GetTouchpadRotateSwitch(bool &rotateSwitch)3140 int32_t MMIService::GetTouchpadRotateSwitch(bool &rotateSwitch)
3141 {
3142 CALL_INFO_TRACE;
3143 #ifdef OHOS_BUILD_ENABLE_POINTER
3144 int32_t ret = delegateTasks_.PostSyncTask(
3145 [this, &rotateSwitch] {
3146 return this->ReadTouchpadRotateSwitch(rotateSwitch);
3147 }
3148 );
3149 if (ret != RET_OK) {
3150 MMI_HILOGE("Get touchpad rotate switch failed, ret:%{public}d", ret);
3151 return ret;
3152 }
3153 #endif // OHOS_BUILD_ENABLE_POINTER
3154 return RET_OK;
3155 }
3156
SetShieldStatus(int32_t shieldMode, bool isShield)3157 int32_t MMIService::SetShieldStatus(int32_t shieldMode, bool isShield)
3158 {
3159 CALL_INFO_TRACE;
3160 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
3161 int32_t ret = delegateTasks_.PostSyncTask(
3162 [this, shieldMode, isShield] {
3163 return sMsgHandler_.SetShieldStatus(shieldMode, isShield);
3164 }
3165 );
3166 if (ret != RET_OK) {
3167 MMI_HILOGE("Set shield event interception state failed, return:%{public}d", ret);
3168 return ret;
3169 }
3170 #endif // OHOS_BUILD_ENABLE_KEYBOARD
3171 return RET_OK;
3172 }
3173
GetShieldStatus(int32_t shieldMode, bool &isShield)3174 int32_t MMIService::GetShieldStatus(int32_t shieldMode, bool &isShield)
3175 {
3176 CALL_INFO_TRACE;
3177 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
3178 int32_t ret = delegateTasks_.PostSyncTask(
3179 [this, shieldMode, &isShield] {
3180 return sMsgHandler_.GetShieldStatus(shieldMode, isShield);
3181 }
3182 );
3183 if (ret != RET_OK) {
3184 MMI_HILOGE("Failed to set shield event interception status, ret:%{public}d", ret);
3185 return ret;
3186 }
3187 #endif // OHOS_BUILD_ENABLE_KEYBOARD
3188 return RET_OK;
3189 }
3190
GetKeyState(std::vector<int32_t> &pressedKeys, std::map<int32_t, int32_t> &specialKeysState)3191 int32_t MMIService::GetKeyState(std::vector<int32_t> &pressedKeys, std::map<int32_t, int32_t> &specialKeysState)
3192 {
3193 CALL_INFO_TRACE;
3194 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
3195 int32_t ret = delegateTasks_.PostSyncTask(
3196 [this, &pressedKeys, &specialKeysState] {
3197 return this->OnGetKeyState(pressedKeys, specialKeysState);
3198 }
3199 );
3200 if (ret != RET_OK) {
3201 MMI_HILOGE("Get pressed keys failed, return:%{public}d", ret);
3202 return ret;
3203 }
3204 #endif // OHOS_BUILD_ENABLE_KEYBOARD
3205 return RET_OK;
3206 }
3207
Authorize(bool isAuthorize)3208 int32_t MMIService::Authorize(bool isAuthorize)
3209 {
3210 CALL_DEBUG_ENTER;
3211 int32_t ret = delegateTasks_.PostSyncTask(
3212 [this, isAuthorize] {
3213 return this->OnAuthorize(isAuthorize);
3214 }
3215 );
3216 if (ret != RET_OK) {
3217 MMI_HILOGE("OnAuthorize failed, ret:%{public}d", ret);
3218 return ret;
3219 }
3220 return RET_OK;
3221 }
3222
OnAuthorize(bool isAuthorize)3223 int32_t MMIService::OnAuthorize(bool isAuthorize)
3224 {
3225 return sMsgHandler_.OnAuthorize(isAuthorize);
3226 }
3227
CancelInjection()3228 int32_t MMIService::CancelInjection()
3229 {
3230 CALL_DEBUG_ENTER;
3231 int32_t ret = delegateTasks_.PostSyncTask(
3232 [this] {
3233 return this->OnCancelInjection();
3234 }
3235 );
3236 if (ret != RET_OK) {
3237 MMI_HILOGE("OnCancelInjection failed, ret:%{public}d", ret);
3238 return ret;
3239 }
3240 return RET_OK;
3241 }
3242
OnCancelInjection()3243 int32_t MMIService::OnCancelInjection()
3244 {
3245 return sMsgHandler_.OnCancelInjection();
3246 }
3247
HasIrEmitter(bool &hasIrEmitter)3248 int32_t MMIService::HasIrEmitter(bool &hasIrEmitter)
3249 {
3250 CALL_DEBUG_ENTER;
3251 int32_t ret = delegateTasks_.PostSyncTask(
3252 [this, &hasIrEmitter] {
3253 return this->OnHasIrEmitter(hasIrEmitter);
3254 }
3255 );
3256 if (ret != RET_OK) {
3257 MMI_HILOGE("OnHasIrEmitter failed, ret:%{public}d", ret);
3258 return ret;
3259 }
3260 return RET_OK;
3261 }
3262
GetInfraredFrequencies(std::vector<InfraredFrequency>& frequencies)3263 int32_t MMIService::GetInfraredFrequencies(std::vector<InfraredFrequency>& frequencies)
3264 {
3265 CALL_DEBUG_ENTER;
3266 MMI_HILOGI("Start get infrared frequency");
3267 std::vector<InfraredFrequencyInfo> infos;
3268 if (!InfraredEmitterController::GetInstance()->GetFrequencies(infos)) {
3269 MMI_HILOGE("Failed to get frequencies");
3270 return RET_ERR;
3271 }
3272 for (auto &item : infos) {
3273 InfraredFrequency info;
3274 info.min_ = item.min_;
3275 info.max_ = item.max_;
3276 frequencies.push_back(info);
3277 }
3278 std::string context = "";
3279 int32_t size = static_cast<int32_t>(frequencies.size());
3280 for (int32_t i = 0; i < size; i++) {
3281 context = context + "frequencies[" + std::to_string(i) + "]. max=" + std::to_string(frequencies[i].max_) +
3282 ",min=" + std::to_string(frequencies[i].min_) + ";";
3283 }
3284 MMI_HILOGD("Data from hdf context:%{public}s", context.c_str());
3285 return RET_OK;
3286 }
3287
TransmitInfrared(int64_t number, std::vector<int64_t>& pattern)3288 int32_t MMIService::TransmitInfrared(int64_t number, std::vector<int64_t>& pattern)
3289 {
3290 CALL_DEBUG_ENTER;
3291 std::string context = "infraredFrequency:" + std::to_string(number) + ";";
3292 int32_t size = static_cast<int32_t>(pattern.size());
3293 for (int32_t i = 0; i < size; i++) {
3294 context = context + "index:" + std::to_string(i) + ": pattern:" + std::to_string(pattern[i]) + ";";
3295 }
3296 MMI_HILOGI("TransmitInfrared para context:%{public}s", context.c_str());
3297 if (!InfraredEmitterController::GetInstance()->Transmit(number, pattern)) {
3298 MMI_HILOGE("Failed to transmit");
3299 return RET_ERR;
3300 }
3301 return RET_OK;
3302 }
3303
3304 #ifdef OHOS_BUILD_ENABLE_VKEYBOARD
SetVKeyboardArea(double topLeftX, double topLeftY, double bottomRightX, double bottomRightY)3305 int32_t MMIService::SetVKeyboardArea(double topLeftX, double topLeftY, double bottomRightX, double bottomRightY)
3306 {
3307 CALL_INFO_TRACE;
3308 if (!isHPR_) {
3309 MMI_HILOGE("Failed to set virtual keyboard area, feature not supported");
3310 return RET_ERR;
3311 }
3312 int32_t ret = delegateTasks_.PostSyncTask(
3313 [this, topLeftX, topLeftY, bottomRightX, bottomRightY] {
3314 return this->OnSetVKeyboardArea(topLeftX, topLeftY, bottomRightX, bottomRightY);
3315 }
3316 );
3317 if (ret != RET_OK) {
3318 MMI_HILOGE("Failed to set virtual keyboard area, ret:%{public}d", ret);
3319 }
3320 return ret;
3321 }
3322
OnSetVKeyboardArea(double topLeftX, double topLeftY, double bottomRightX, double bottomRightY)3323 int32_t MMIService::OnSetVKeyboardArea(double topLeftX, double topLeftY, double bottomRightX, double bottomRightY)
3324 {
3325 gaussiankeyboard_setVKeyboardArea_(topLeftX, topLeftY, bottomRightX, bottomRightY);
3326 int32_t sKeyEventID = 1234;
3327 int32_t sKeyEventDeviceId = 99;
3328 // Init the shared key event used by later key injection module and set common fields.
3329 g_VKeySharedKeyEvent = KeyEvent::Create();
3330 CHKPR(g_VKeySharedKeyEvent, ERROR_NULL_POINTER);
3331 g_VKeySharedKeyEvent->SetId(sKeyEventID);
3332 g_VKeySharedKeyEvent->SetDeviceId(sKeyEventDeviceId);
3333 // Init the shared UI key event for UI rendering.
3334 g_VKeySharedUIKeyEvent = KeyEvent::Create();
3335 CHKPR(g_VKeySharedUIKeyEvent, ERROR_NULL_POINTER);
3336 g_VKeySharedUIKeyEvent->SetId(sKeyEventID);
3337 g_VKeySharedUIKeyEvent->SetDeviceId(sKeyEventDeviceId);
3338 return RET_OK;
3339 }
3340
SetMotionSpace(std::string& keyName, bool useShift, std::vector<int32_t>& pattern)3341 int32_t MMIService::SetMotionSpace(std::string& keyName, bool useShift, std::vector<int32_t>& pattern)
3342 {
3343 CALL_INFO_TRACE;
3344 if (!isHPR_) {
3345 MMI_HILOGE("Failed to set motion space, feature not supported");
3346 return RET_ERR;
3347 }
3348 int32_t ret = delegateTasks_.PostSyncTask(
3349 [this, &keyName, useShift, &pattern] {
3350 return this->OnSetMotionSpace(keyName, useShift, pattern);
3351 }
3352 );
3353 if (ret != RET_OK) {
3354 MMI_HILOGE("Failed to set motion space, ret:%{public}d", ret);
3355 }
3356 return ret;
3357 }
3358
OnSetMotionSpace(std::string& keyName, bool useShift, std::vector<int32_t>& pattern)3359 int32_t MMIService::OnSetMotionSpace(std::string& keyName, bool useShift, std::vector<int32_t>& pattern)
3360 {
3361 if (pattern.size() == MotionSpacePatternIndex::PATTERN_SIZE) {
3362 auto motionSpaceType = static_cast<MotionSpaceType>(pattern[MotionSpacePatternIndex::PATTERN_MST_ID]);
3363 if (motionSpaceType != MotionSpaceType::TRACKPAD) {
3364 gaussiankeyboard_updateMotionSpace_(keyName, useShift, pattern);
3365 }
3366 return RET_OK;
3367 } else {
3368 return COMMON_PARAMETER_ERROR;
3369 }
3370 }
3371 #endif // OHOS_BUILD_ENABLE_VKEYBOARD
3372
OnHasIrEmitter(bool &hasIrEmitter)3373 int32_t MMIService::OnHasIrEmitter(bool &hasIrEmitter)
3374 {
3375 hasIrEmitter = false;
3376 return RET_OK;
3377 }
3378
SetPixelMapData(int32_t infoId, void* pixelMap)3379 int32_t MMIService::SetPixelMapData(int32_t infoId, void* pixelMap)
3380 {
3381 CALL_DEBUG_ENTER;
3382 CHKPR(pixelMap, ERROR_NULL_POINTER);
3383 int32_t ret = delegateTasks_.PostSyncTask(
3384 [this, infoId, pixelMap] {
3385 return sMsgHandler_.SetPixelMapData(infoId, pixelMap);
3386 }
3387 );
3388 if (ret != RET_OK) {
3389 MMI_HILOGE("Failed to set pixelmap, ret:%{public}d", ret);
3390 return ret;
3391 }
3392 return RET_OK;
3393 }
3394
InitPreferences()3395 void MMIService::InitPreferences()
3396 {
3397 PREFERENCES_MGR->InitPreferences();
3398 #ifdef OHOS_BUILD_ENABLE_MOVE_EVENT_FILTERS
3399 int32_t ret = SetMoveEventFilters(PREFERENCES_MGR->GetBoolValue("moveEventFilterFlag", false));
3400 if (ret != RET_OK) {
3401 MMI_HILOGE("Failed to read moveEventFilterFlag, ret:%{public}d", ret);
3402 }
3403 #endif // OHOS_BUILD_ENABLE_MOVE_EVENT_FILTERS
3404 }
3405
SetMoveEventFilters(bool flag)3406 int32_t MMIService::SetMoveEventFilters(bool flag)
3407 {
3408 CALL_DEBUG_ENTER;
3409 #ifdef OHOS_BUILD_ENABLE_MOVE_EVENT_FILTERS
3410 int32_t ret = delegateTasks_.PostSyncTask(
3411 std::bind(&InputEventHandler::SetMoveEventFilters, InputHandler, flag));
3412 if (ret != RET_OK) {
3413 MMI_HILOGE("Failed to set move event filter flag, ret:%{public}d", ret);
3414 return ret;
3415 }
3416 #endif // OHOS_BUILD_ENABLE_MOVE_EVENT_FILTERS
3417 return RET_OK;
3418 }
3419
SetCurrentUser(int32_t userId)3420 int32_t MMIService::SetCurrentUser(int32_t userId)
3421 {
3422 CALL_DEBUG_ENTER;
3423 int32_t ret = delegateTasks_.PostSyncTask(
3424 [userId] {
3425 return ::OHOS::MMI::IInputWindowsManager::GetInstance()->SetCurrentUser(userId);
3426 }
3427 );
3428 if (ret != RET_OK) {
3429 MMI_HILOGE("Failed to set current user, ret:%{public}d", ret);
3430 return ret;
3431 }
3432 return RET_OK;
3433 }
3434
SetTouchpadThreeFingersTapSwitch(bool switchFlag)3435 int32_t MMIService::SetTouchpadThreeFingersTapSwitch(bool switchFlag)
3436 {
3437 CALL_INFO_TRACE;
3438 #ifdef OHOS_BUILD_ENABLE_POINTER
3439 int32_t ret = delegateTasks_.PostSyncTask(
3440 [switchFlag] {
3441 return ::OHOS::DelayedSingleton<TouchEventNormalize>::GetInstance()->SetTouchpadThreeFingersTapSwitch(
3442 switchFlag);
3443 }
3444 );
3445 if (ret != RET_OK) {
3446 MMI_HILOGE("Failed to SetTouchpadThreeFingersTapSwitch status, ret:%{public}d", ret);
3447 return ret;
3448 }
3449 #endif // OHOS_BUILD_ENABLE_POINTER
3450 return RET_OK;
3451 }
3452
GetTouchpadThreeFingersTapSwitch(bool &switchFlag)3453 int32_t MMIService::GetTouchpadThreeFingersTapSwitch(bool &switchFlag)
3454 {
3455 CALL_INFO_TRACE;
3456 #ifdef OHOS_BUILD_ENABLE_POINTER
3457 int32_t ret = delegateTasks_.PostSyncTask(
3458 [&switchFlag] {
3459 return ::OHOS::DelayedSingleton<TouchEventNormalize>::GetInstance()->GetTouchpadThreeFingersTapSwitch(
3460 switchFlag);
3461 }
3462 );
3463 if (ret != RET_OK) {
3464 MMI_HILOGE("Failed to GetTouchpadThreeFingersTapSwitch status, ret:%{public}d", ret);
3465 return ret;
3466 }
3467 #endif // OHOS_BUILD_ENABLE_POINTER
3468 return RET_OK;
3469 }
3470
AddVirtualInputDevice(std::shared_ptr<InputDevice> device, int32_t &deviceId)3471 int32_t MMIService::AddVirtualInputDevice(std::shared_ptr<InputDevice> device, int32_t &deviceId)
3472 {
3473 CALL_DEBUG_ENTER;
3474 CHKPR(device, ERROR_NULL_POINTER);
3475 int32_t ret = delegateTasks_.PostSyncTask(
3476 [device, &deviceId] {
3477 return ::OHOS::MMI::InputDeviceManager::GetInstance()->AddVirtualInputDevice(device, deviceId);
3478 }
3479 );
3480 if (ret != RET_OK) {
3481 MMI_HILOGE("AddVirtualInputDevice failed:%{public}d", ret);
3482 }
3483 return ret;
3484 }
3485
RemoveVirtualInputDevice(int32_t deviceId)3486 int32_t MMIService::RemoveVirtualInputDevice(int32_t deviceId)
3487 {
3488 CALL_DEBUG_ENTER;
3489 int32_t ret = delegateTasks_.PostSyncTask(
3490 [deviceId] {
3491 return ::OHOS::MMI::InputDeviceManager::GetInstance()->RemoveVirtualInputDevice(deviceId);
3492 }
3493 );
3494 if (ret != RET_OK) {
3495 MMI_HILOGE("RemoveVirtualInputDevice failed:%{public}d", ret);
3496 }
3497 return ret;
3498 }
3499
EnableHardwareCursorStats(bool enable)3500 int32_t MMIService::EnableHardwareCursorStats(bool enable)
3501 {
3502 CALL_DEBUG_ENTER;
3503 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
3504 int32_t pid = GetCallingPid();
3505 int32_t ret = delegateTasks_.PostSyncTask(
3506 [pid, enable] {
3507 return IPointerDrawingManager::GetInstance()->EnableHardwareCursorStats(pid, enable);
3508 }
3509 );
3510 if (ret != RET_OK) {
3511 MMI_HILOGE("Enable hardware cursor stats failed, ret:%{public}d", ret);
3512 return ret;
3513 }
3514 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
3515 return RET_OK;
3516 }
3517
GetHardwareCursorStats(uint32_t &frameCount, uint32_t &vsyncCount)3518 int32_t MMIService::GetHardwareCursorStats(uint32_t &frameCount, uint32_t &vsyncCount)
3519 {
3520 CALL_DEBUG_ENTER;
3521 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
3522 int32_t pid = GetCallingPid();
3523 int32_t ret = delegateTasks_.PostSyncTask(
3524 [pid, &frameCount, &vsyncCount] {
3525 return IPointerDrawingManager::GetInstance()->GetHardwareCursorStats(pid, frameCount, vsyncCount);
3526 }
3527 );
3528 if (ret != RET_OK) {
3529 MMI_HILOGE("Get hardware cursor stats failed, ret:%{public}d", ret);
3530 return ret;
3531 }
3532 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
3533 return RET_OK;
3534 }
3535
3536 #ifdef OHOS_BUILD_ENABLE_MAGICCURSOR
GetPointerSnapshot(void *pixelMapPtr)3537 int32_t MMIService::GetPointerSnapshot(void *pixelMapPtr)
3538 {
3539 CALL_INFO_TRACE;
3540 #if defined OHOS_BUILD_ENABLE_POINTER
3541 MMI_HILOGI("Get pointer snapshot from process(%{public}d)", GetCallingPid());
3542 int32_t ret = delegateTasks_.PostSyncTask(std::bind(
3543 std::bind(&IPointerDrawingManager::GetPointerSnapshot, IPointerDrawingManager::GetInstance(), pixelMapPtr)));
3544 if (ret != RET_OK) {
3545 MMI_HILOGE("Get the pointer snapshot failed, ret: %{public}d", ret);
3546 return ret;
3547 }
3548 #endif // OHOS_BUILD_ENABLE_POINTER
3549 return RET_OK;
3550 }
3551 #endif // OHOS_BUILD_ENABLE_MAGICCURSOR
3552
SetTouchpadScrollRows(int32_t rows)3553 int32_t MMIService::SetTouchpadScrollRows(int32_t rows)
3554 {
3555 CALL_INFO_TRACE;
3556 #ifdef OHOS_BUILD_ENABLE_POINTER
3557 int32_t ret = delegateTasks_.PostSyncTask(
3558 [rows] {
3559 return ::OHOS::DelayedSingleton<TouchEventNormalize>::GetInstance()->SetTouchpadScrollRows(rows);
3560 }
3561 );
3562 if (ret != RET_OK) {
3563 MMI_HILOGE("Set the number of touchpad scrolling rows failed, return %{public}d", ret);
3564 return ret;
3565 }
3566 #endif // OHOS_BUILD_ENABLE_POINTER
3567 return RET_OK;
3568 }
3569
3570 #ifdef OHOS_BUILD_ENABLE_POINTER
ReadTouchpadScrollRows(int32_t &rows)3571 int32_t MMIService::ReadTouchpadScrollRows(int32_t &rows)
3572 {
3573 rows = TOUCH_EVENT_HDR->GetTouchpadScrollRows();
3574 return RET_OK;
3575 }
3576 #endif // OHOS_BUILD_ENABLE_POINTER
3577
GetTouchpadScrollRows(int32_t &rows)3578 int32_t MMIService::GetTouchpadScrollRows(int32_t &rows)
3579 {
3580 CALL_INFO_TRACE;
3581 #ifdef OHOS_BUILD_ENABLE_POINTER
3582 int32_t ret = delegateTasks_.PostSyncTask(
3583 [this, &rows] {
3584 return this->ReadTouchpadScrollRows(rows);
3585 }
3586 );
3587 if (ret != RET_OK) {
3588 MMI_HILOGE("Get the number of touchpad scrolling rows failed, return %{public}d, pid:%{public}d", ret,
3589 GetCallingPid());
3590 return ret;
3591 }
3592 #endif // OHOS_BUILD_ENABLE_POINTER
3593 return RET_OK;
3594 }
3595
3596 #ifdef OHOS_BUILD_ENABLE_ANCO
AncoAddChannel(sptr<IAncoChannel> channel)3597 int32_t MMIService::AncoAddChannel(sptr<IAncoChannel> channel)
3598 {
3599 int32_t ret = delegateTasks_.PostSyncTask([channel]() {
3600 return WIN_MGR->AncoAddChannel(channel);
3601 });
3602 if (ret != RET_OK) {
3603 MMI_HILOGE("AncoAddChannel fail, error:%{public}d", ret);
3604 }
3605 return ret;
3606 }
3607
AncoRemoveChannel(sptr<IAncoChannel> channel)3608 int32_t MMIService::AncoRemoveChannel(sptr<IAncoChannel> channel)
3609 {
3610 int32_t ret = delegateTasks_.PostSyncTask([channel]() {
3611 return WIN_MGR->AncoRemoveChannel(channel);
3612 });
3613 if (ret != RET_OK) {
3614 MMI_HILOGE("AncoRemoveChannel fail, error:%{public}d", ret);
3615 }
3616 return ret;
3617 }
3618 #endif // OHOS_BUILD_ENABLE_ANCO
3619
TransferBinderClientSrv(const sptr<IRemoteObject> &binderClientObject)3620 int32_t MMIService::TransferBinderClientSrv(const sptr<IRemoteObject> &binderClientObject)
3621 {
3622 CALL_DEBUG_ENTER;
3623 int32_t pid = GetCallingPid();
3624 int32_t ret =
3625 delegateTasks_.PostSyncTask(
3626 [this, pid, binderClientObject] {
3627 return sMsgHandler_.OnTransferBinderClientSrv(binderClientObject, pid);
3628 }
3629 );
3630 MMI_HILOGI("TransferBinderClientSrv result:%{public}d", ret);
3631 return ret;
3632 }
3633
CalculateFuntionRunningTime(std::function<void()> func, const std::string &flag)3634 void MMIService::CalculateFuntionRunningTime(std::function<void()> func, const std::string &flag)
3635 {
3636 std::function<void (void *)> printLog = std::bind(&MMIService::PrintLog, this, flag, THREAD_BLOCK_TIMER_SPAN_S,
3637 getpid(), gettid());
3638 int32_t id = HiviewDFX::XCollie::GetInstance().SetTimer(flag, THREAD_BLOCK_TIMER_SPAN_S, printLog, nullptr,
3639 HiviewDFX::XCOLLIE_FLAG_NOOP);
3640 func();
3641 HiviewDFX::XCollie::GetInstance().CancelTimer(id);
3642 }
3643
PrintLog(const std::string &flag, int32_t duration, int32_t pid, int32_t tid)3644 void MMIService::PrintLog(const std::string &flag, int32_t duration, int32_t pid, int32_t tid)
3645 {
3646 std::string dfxThreadBlockMsg { "MMIBlockTask name:" };
3647 dfxThreadBlockMsg += flag;
3648 dfxThreadBlockMsg += ", duration time:";
3649 dfxThreadBlockMsg += std::to_string(duration);
3650 dfxThreadBlockMsg += ", pid:";
3651 dfxThreadBlockMsg += std::to_string(pid);
3652 dfxThreadBlockMsg += ", tid:";
3653 dfxThreadBlockMsg += std::to_string(tid);
3654 MMI_HILOGW("DfxThreadBlockMsg:%{public}s", dfxThreadBlockMsg.c_str());
3655 OHOS::HiviewDFX::DfxDumpCatcher dumpCatcher;
3656 dumpCatcher.DumpCatch(pid, tid, dfxThreadBlockMsg, MAX_FRAME_NUMS, false);
3657 }
3658
SkipPointerLayer(bool isSkip)3659 int32_t MMIService::SkipPointerLayer(bool isSkip)
3660 {
3661 CALL_INFO_TRACE;
3662 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
3663 int32_t ret = delegateTasks_.PostSyncTask(
3664 [isSkip] {
3665 return IPointerDrawingManager::GetInstance()->SkipPointerLayer(isSkip);
3666 }
3667 );
3668 if (ret != RET_OK) {
3669 MMI_HILOGE("Skip pointer layerfailed, return:%{public}d", ret);
3670 return ret;
3671 }
3672 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
3673 return RET_OK;
3674 }
3675
OnSessionDelete(SessionPtr session)3676 void MMIService::OnSessionDelete(SessionPtr session)
3677 {
3678 CALL_DEBUG_ENTER;
3679 CHKPV(session);
3680 std::string programName = session->GetProgramName();
3681 std::lock_guard<std::mutex> guard(mutex_);
3682 auto it = clientInfos_.find(programName);
3683 if (it != clientInfos_.end()) {
3684 clientInfos_.erase(it);
3685 MMI_HILOGD("Clear the client info, programName:%{public}s", programName.c_str());
3686 }
3687 }
3688
SetClientInfo(int32_t pid, uint64_t readThreadId)3689 int32_t MMIService::SetClientInfo(int32_t pid, uint64_t readThreadId)
3690 {
3691 CALL_DEBUG_ENTER;
3692 auto sess = GetSessionByPid(pid);
3693 CHKPR(sess, ERROR_NULL_POINTER);
3694 std::string programName = sess->GetProgramName();
3695 std::lock_guard<std::mutex> guard(mutex_);
3696 if (clientInfos_.find(programName) != clientInfos_.end()) {
3697 clientInfos_[programName].pid = pid;
3698 clientInfos_[programName].readThreadId = readThreadId;
3699 return RET_OK;
3700 }
3701 ClientInfo clientInfo {
3702 .pid = pid,
3703 .readThreadId = readThreadId
3704 };
3705 clientInfos_[programName] = clientInfo;
3706 return RET_OK;
3707 }
3708
InitPrintClientInfo()3709 void MMIService::InitPrintClientInfo()
3710 {
3711 CALL_DEBUG_ENTER;
3712 TimerMgr->AddLongTimer(PRINT_INTERVAL_TIME, -1, [this]() {
3713 ffrt::submit([this] {
3714 for (const auto &info : clientInfos_) {
3715 if (static_cast<uint64_t>(info.second.pid) == info.second.readThreadId) {
3716 MMI_HILOGW("The application main thread and event reading thread are combined, such as:"
3717 "programName:%{public}s, pid:%{public}d, mainThreadId:%{public}d, readThreadId:%{public}" PRIu64,
3718 info.first.c_str(), info.second.pid, info.second.pid, info.second.readThreadId);
3719 return;
3720 }
3721 }
3722 if (!clientInfos_.empty()) {
3723 auto it = clientInfos_.begin();
3724 MMI_HILOGI("The application main thread and event reading thread are separated, such as:"
3725 "programName:%{public}s, pid:%{public}d, mainThreadId:%{public}d, readThreadId:%{public}" PRIu64,
3726 it->first.c_str(), it->second.pid, it->second.pid, it->second.readThreadId);
3727 }
3728 });
3729 });
3730 std::function<void(SessionPtr)> callback = [this](SessionPtr sess) {
3731 return this->OnSessionDelete(sess);
3732 };
3733 AddSessionDeletedCallback(callback);
3734 }
3735
GetIntervalSinceLastInput(int64_t &timeInterval)3736 int32_t MMIService::GetIntervalSinceLastInput(int64_t &timeInterval)
3737 {
3738 CALL_INFO_TRACE;
3739 int32_t ret = delegateTasks_.PostSyncTask(std::bind(&InputEventHandler::GetIntervalSinceLastInput,
3740 InputHandler, std::ref(timeInterval)));
3741 MMI_HILOGD("timeInterval:%{public}" PRId64, timeInterval);
3742 if (ret != RET_OK) {
3743 MMI_HILOGE("Failed to GetIntervalSinceLastInput, ret:%{public}d", ret);
3744 }
3745 return ret;
3746 }
3747
GetAllSystemHotkeys(std::vector<std::unique_ptr<KeyOption>> &keyOptions)3748 int32_t MMIService::GetAllSystemHotkeys(std::vector<std::unique_ptr<KeyOption>> &keyOptions)
3749 {
3750 CALL_DEBUG_ENTER;
3751 int32_t ret = delegateTasks_.PostSyncTask(
3752 [this, &keyOptions] {
3753 return this->OnGetAllSystemHotkey(keyOptions);
3754 }
3755 );
3756 if (ret != RET_OK) {
3757 MMI_HILOGD("Get all system hot key, ret:%{public}d", ret);
3758 return ret;
3759 }
3760 return RET_OK;
3761 }
3762
OnGetAllSystemHotkey(std::vector<std::unique_ptr<KeyOption>> &keyOptions)3763 int32_t MMIService::OnGetAllSystemHotkey(std::vector<std::unique_ptr<KeyOption>> &keyOptions)
3764 {
3765 CALL_DEBUG_ENTER;
3766 #ifdef SHORTCUT_KEY_MANAGER_ENABLED
3767 return KEY_SHORTCUT_MGR->GetAllSystemHotkeys(keyOptions);
3768 #endif // SHORTCUT_KEY_MANAGER_ENABLED
3769 MMI_HILOGI("OnGetAllSystemHotkey function does not support");
3770 return ERROR_UNSUPPORT;
3771 }
3772 } // namespace MMI
3773 } // namespace OHOS
3774