/* * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "mmi_service.h" #include #include #include #include #ifdef OHOS_RSS_CLIENT #include #endif #include "anr_manager.h" #include "dfx_hisysevent.h" #include "event_dump.h" #include "input_device_manager.h" #include "input_windows_manager.h" #include "i_pointer_drawing_manager.h" #include "key_map_manager.h" #include "multimodal_input_connect_def_parcel.h" #include "permission_helper.h" #include "string_ex.h" #ifdef OHOS_RSS_CLIENT #include "res_sched_client.h" #include "res_type.h" #include "system_ability_definition.h" #endif #include "mmi_log.h" #include "timer_manager.h" #include "util.h" #include "util_ex.h" #include "util_napi_error.h" #include "xcollie/watchdog.h" #include "key_auto_repeat.h" #include "key_command_handler.h" #include "touch_event_normalize.h" #include "display_event_monitor.h" #include "fingersense_wrapper.h" namespace OHOS { namespace MMI { namespace { constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "MMIService" }; const std::string DEF_INPUT_SEAT = "seat0"; constexpr int32_t WATCHDOG_INTERVAL_TIME = 10000; constexpr int32_t WATCHDOG_DELAY_TIME = 15000; } // namespace const bool REGISTER_RESULT = SystemAbility::MakeAndRegisterAbility(DelayedSingleton::GetInstance().get()); struct mmi_epoll_event { int32_t fd{ 0 }; EpollEventType event_type{ EPOLL_EVENT_BEGIN }; }; template void CheckDefineOutput(const char *fmt, Ts... args) { CHKPV(fmt); char buf[MAX_PACKET_BUF_SIZE] = {}; int32_t ret = snprintf_s(buf, MAX_PACKET_BUF_SIZE, MAX_PACKET_BUF_SIZE - 1, fmt, args...); if (ret == -1) { KMSG_LOGI("Call snprintf_s failed.ret = %d", ret); return; } KMSG_LOGI("%s", buf); MMI_HILOGI("%{public}s", buf); } static void CheckDefine() { CheckDefineOutput("ChkDefs:"); #ifdef OHOS_BUILD_ENABLE_POINTER_DRAWING CheckDefineOutput("%-40s", "OHOS_BUILD_ENABLE_POINTER_DRAWING"); #endif #ifdef OHOS_BUILD_ENABLE_INTERCEPTOR CheckDefineOutput("%-40s", "OHOS_BUILD_ENABLE_INTERCEPTOR"); #endif #ifdef OHOS_BUILD_ENABLE_KEYBOARD CheckDefineOutput("%-40s", "OHOS_BUILD_ENABLE_KEYBOARD"); #endif #ifdef OHOS_BUILD_ENABLE_POINTER CheckDefineOutput("%-40s", "OHOS_BUILD_ENABLE_POINTER"); #endif #ifdef OHOS_BUILD_ENABLE_TOUCH CheckDefineOutput("%-40s", "OHOS_BUILD_ENABLE_TOUCH"); #endif #ifdef OHOS_BUILD_ENABLE_MONITOR CheckDefineOutput("%-40s", "OHOS_BUILD_ENABLE_MONITOR"); #endif } MMIService::MMIService() : SystemAbility(MULTIMODAL_INPUT_CONNECT_SERVICE_ID, true) {} MMIService::~MMIService() {} int32_t MMIService::AddEpoll(EpollEventType type, int32_t fd) { if (!(type >= EPOLL_EVENT_BEGIN && type < EPOLL_EVENT_END)) { MMI_HILOGE("Invalid param type"); return RET_ERR; } if (fd < 0) { MMI_HILOGE("Invalid param fd_"); return RET_ERR; } if (mmiFd_ < 0) { MMI_HILOGE("Invalid param mmiFd_"); return RET_ERR; } auto eventData = static_cast(malloc(sizeof(mmi_epoll_event))); if (!eventData) { MMI_HILOGE("Malloc failed"); return RET_ERR; } eventData->fd = fd; eventData->event_type = type; MMI_HILOGI("userdata:[fd:%{public}d,type:%{public}d]", eventData->fd, eventData->event_type); struct epoll_event ev = {}; ev.events = EPOLLIN; ev.data.ptr = eventData; auto ret = EpollCtl(fd, EPOLL_CTL_ADD, ev, mmiFd_); if (ret < 0) { free(eventData); eventData = nullptr; ev.data.ptr = nullptr; return ret; } return RET_OK; } int32_t MMIService::DelEpoll(EpollEventType type, int32_t fd) { if (!(type >= EPOLL_EVENT_BEGIN && type < EPOLL_EVENT_END)) { MMI_HILOGE("Invalid param type"); return RET_ERR; } if (fd < 0) { MMI_HILOGE("Invalid param fd_"); return RET_ERR; } if (mmiFd_ < 0) { MMI_HILOGE("Invalid param mmiFd_"); return RET_ERR; } struct epoll_event ev = {}; auto ret = EpollCtl(fd, EPOLL_CTL_DEL, ev, mmiFd_); if (ret < 0) { MMI_HILOGE("DelEpoll failed"); return ret; } return RET_OK; } bool MMIService::IsRunning() const { return (state_ == ServiceRunningState::STATE_RUNNING); } bool MMIService::InitLibinputService() { if (!(libinputAdapter_.Init(std::bind(&InputEventHandler::OnEvent, InputHandler, std::placeholders::_1)))) { MMI_HILOGE("Libinput init, bind failed"); return false; } auto inputFds = libinputAdapter_.GetInputFds(); for (auto fd : inputFds) { auto ret = AddEpoll(EPOLL_EVENT_INPUT, fd); if (ret < 0) { MMI_HILOGE("AddEpoll error ret:%{public}d", ret); EpollClose(); return false; } MMI_HILOGI("AddEpoll, epollfd:%{public}d, fd:%{public}d", mmiFd_, fd); } return true; } bool MMIService::InitService() { MMI_HILOGD("Server msg handler Init"); sMsgHandler_.Init(*this); if (state_ != ServiceRunningState::STATE_NOT_START) { MMI_HILOGE("Service running status is not enabled"); return false; } if (EpollCreate(MAX_EVENT_SIZE) < 0) { MMI_HILOGE("Create epoll failed"); return false; } auto ret = AddEpoll(EPOLL_EVENT_SOCKET, epollFd_); if (ret < 0) { MMI_HILOGE("AddEpoll error ret:%{public}d", ret); EpollClose(); return false; } state_ = ServiceRunningState::STATE_RUNNING; if (!(Publish(this))) { state_ = ServiceRunningState::STATE_NOT_START; MMI_HILOGE("Service initialization failed"); return false; } MMI_HILOGI("AddEpoll, epollfd:%{public}d,fd:%{public}d", mmiFd_, epollFd_); return true; } bool MMIService::InitDelegateTasks() { CALL_DEBUG_ENTER; if (!delegateTasks_.Init()) { MMI_HILOGE("The delegate task init failed"); return false; } auto ret = AddEpoll(EPOLL_EVENT_ETASK, delegateTasks_.GetReadFd()); if (ret < 0) { MMI_HILOGE("AddEpoll error ret:%{public}d", ret); EpollClose(); return false; } MMI_HILOGI("AddEpoll, epollfd:%{public}d,fd:%{public}d", mmiFd_, delegateTasks_.GetReadFd()); return true; } int32_t MMIService::Init() { CheckDefine(); MMI_HILOGD("WindowsManager Init"); WinMgr->Init(*this); MMI_HILOGD("ANRManager Init"); ANRMgr->Init(*this); MMI_HILOGD("PointerDrawingManager Init"); #ifdef OHOS_BUILD_ENABLE_FINGERSENSE_WRAPPER FINGERSENSE_WRAPPER->InitFingerSenseWrapper(); DISPLAY_MONITOR->InitCommonEventSubscriber(); #endif // OHOS_BUILD_ENABLE_FINGERSENSE_WRAPPER #ifdef OHOS_BUILD_ENABLE_POINTER if (!IPointerDrawingManager::GetInstance()->Init()) { MMI_HILOGE("Pointer draw init failed"); return POINTER_DRAW_INIT_FAIL; } #endif // OHOS_BUILD_ENABLE_POINTER mmiFd_ = EpollCreate(MAX_EVENT_SIZE); if (mmiFd_ < 0) { MMI_HILOGE("Create epoll failed"); return EPOLL_CREATE_FAIL; } MMI_HILOGD("Input msg handler init"); InputHandler->Init(*this); if (!InitLibinputService()) { MMI_HILOGE("Libinput init failed"); return LIBINPUT_INIT_FAIL; } if (!InitDelegateTasks()) { MMI_HILOGE("Delegate tasks init failed"); return ETASKS_INIT_FAIL; } SetRecvFun(std::bind(&ServerMsgHandler::OnMsgHandler, &sMsgHandler_, std::placeholders::_1, std::placeholders::_2)); KeyMapMgr->GetConfigKeyValue("default_keymap", KeyMapMgr->GetDefaultKeyId()); OHOS::system::SetParameter(INPUT_POINTER_DEVICE, "false"); if (!InitService()) { MMI_HILOGE("Saservice init failed"); return SASERVICE_INIT_FAIL; } MMI_HILOGI("Set para input.pointer.device false"); return RET_OK; } void MMIService::OnStart() { int sleepSeconds = 3; std::string name = "mmi-service"; sleep(sleepSeconds); CHK_PID_AND_TID(); int32_t ret = Init(); if (RET_OK != ret) { MMI_HILOGE("Init mmi_service failed"); return; } MMI_HILOGD("Started successfully"); AddReloadDeviceTimer(); t_ = std::thread(std::bind(&MMIService::OnThread, this)); pthread_setname_np(t_.native_handle(), name.c_str()); #ifdef OHOS_RSS_CLIENT MMI_HILOGI("Add system ability listener start"); AddSystemAbilityListener(RES_SCHED_SYS_ABILITY_ID); MMI_HILOGI("Add system ability listener success"); #endif TimerMgr->AddTimer(WATCHDOG_INTERVAL_TIME, -1, [this]() { MMI_HILOGD("Set thread status flag to true"); threadStatusFlag_ = true; }); auto taskFunc = [this]() { if (threadStatusFlag_) { MMI_HILOGD("Set thread status flag to false"); threadStatusFlag_ = false; } else { MMI_HILOGE("Watchdog happened"); } }; MMI_HILOGI("Run periodical task start"); HiviewDFX::Watchdog::GetInstance().RunPeriodicalTask("MMIService", taskFunc, WATCHDOG_INTERVAL_TIME, WATCHDOG_DELAY_TIME); MMI_HILOGI("Run periodical task success"); } void MMIService::OnStop() { CHK_PID_AND_TID(); UdsStop(); libinputAdapter_.Stop(); state_ = ServiceRunningState::STATE_NOT_START; #ifdef OHOS_RSS_CLIENT MMI_HILOGI("Remove system ability listener start"); RemoveSystemAbilityListener(RES_SCHED_SYS_ABILITY_ID); MMI_HILOGI("Remove system ability listener success"); #endif } int32_t MMIService::AllocSocketFd(const std::string &programName, const int32_t moduleType, int32_t &toReturnClientFd, int32_t &tokenType) { MMI_HILOGI("Enter, programName:%{public}s,moduleType:%{public}d", programName.c_str(), moduleType); toReturnClientFd = IMultimodalInputConnect::INVALID_SOCKET_FD; int32_t serverFd = IMultimodalInputConnect::INVALID_SOCKET_FD; int32_t pid = GetCallingPid(); int32_t uid = GetCallingUid(); int32_t ret = delegateTasks_.PostSyncTask(std::bind(&UDSServer::AddSocketPairInfo, this, programName, moduleType, uid, pid, serverFd, std::ref(toReturnClientFd), tokenType)); DfxHisysevent::ClientConnectData data = { .pid = pid, .uid = uid, .moduleType = moduleType, .programName = programName, .serverFd = serverFd }; if (ret != RET_OK) { MMI_HILOGE("Call AddSocketPairInfo failed,return %{public}d", ret); DfxHisysevent::OnClientConnect(data, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT); return RET_ERR; } MMI_HILOGIK("Leave, programName:%{public}s,moduleType:%{public}d,alloc success", programName.c_str(), moduleType); DfxHisysevent::OnClientConnect(data, OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR); return RET_OK; } int32_t MMIService::AddInputEventFilter(sptr filter, int32_t filterId, int32_t priority, uint32_t deviceTags) { CALL_INFO_TRACE; #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH) || defined(OHOS_BUILD_ENABLE_KEYBOARD) CHKPR(filter, ERROR_NULL_POINTER); int32_t clientPid = GetCallingPid(); int32_t ret = delegateTasks_.PostSyncTask(std::bind(&ServerMsgHandler::AddInputEventFilter, &sMsgHandler_, filter, filterId, priority, deviceTags, clientPid)); if (ret != RET_OK) { MMI_HILOGE("Add event filter failed,return %{public}d", ret); return ret; } #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH || OHOS_BUILD_ENABLE_KEYBOARD return RET_OK; } int32_t MMIService::RemoveInputEventFilter(int32_t filterId) { CALL_INFO_TRACE; #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH) || defined(OHOS_BUILD_ENABLE_KEYBOARD) int32_t clientPid = GetCallingPid(); int32_t ret = delegateTasks_.PostSyncTask( std::bind(&ServerMsgHandler::RemoveInputEventFilter, &sMsgHandler_, filterId, clientPid)); if (ret != RET_OK) { MMI_HILOGE("Remove event filter failed,return %{public}d", ret); return ret; } #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH || OHOS_BUILD_ENABLE_KEYBOARD return RET_OK; } void MMIService::OnConnected(SessionPtr s) { CHKPV(s); MMI_HILOGI("fd:%{public}d", s->GetFd()); } void MMIService::OnDisconnected(SessionPtr s) { CHKPV(s); MMI_HILOGW("Enter, session desc:%{public}s, fd:%{public}d", s->GetDescript().c_str(), s->GetFd()); auto ret = RemoveInputEventFilter(-1); if (ret != RET_OK) { MMI_HILOGF("Remove all filter failed, ret:%{public}d", ret); } #ifdef OHOS_BUILD_ENABLE_POINTER IPointerDrawingManager::GetInstance()->DeletePointerVisible(s->GetPid()); #endif // OHOS_BUILD_ENABLE_POINTER } int32_t MMIService::SetMouseScrollRows(int32_t rows) { CALL_DEBUG_ENTER; #if defined OHOS_BUILD_ENABLE_POINTER int32_t ret = delegateTasks_.PostSyncTask(std::bind(&MouseEventNormalize::SetMouseScrollRows, MouseEventHdr, rows)); if (ret != RET_OK) { MMI_HILOGE("Set the number of mouse scrolling rows failed, return %{public}d", ret); return ret; } #endif // OHOS_BUILD_ENABLE_POINTER return RET_OK; } int32_t MMIService::SetMouseIcon(int32_t windowId, void* pixelMap) { CALL_DEBUG_ENTER; #if defined OHOS_BUILD_ENABLE_POINTER int32_t ret = delegateTasks_.PostSyncTask(std::bind(std::bind(&IPointerDrawingManager::SetMouseIcon, IPointerDrawingManager::GetInstance(), windowId, pixelMap))); if (ret != RET_OK) { MMI_HILOGE("Set the mouse icon failed, return %{public}d", ret); return ret; } #endif // OHOS_BUILD_ENABLE_POINTER return RET_OK; } int32_t MMIService::SetMouseHotSpot(int32_t windowId, int32_t hotSpotX, int32_t hotSpotY) { CALL_DEBUG_ENTER; #if defined OHOS_BUILD_ENABLE_POINTER int32_t ret = delegateTasks_.PostSyncTask(std::bind(std::bind(&IPointerDrawingManager::SetMouseHotSpot, IPointerDrawingManager::GetInstance(), windowId, hotSpotX, hotSpotY))); if (ret != RET_OK) { MMI_HILOGE("Set the mouse hot spot failed, return %{public}d", ret); return ret; } #endif // OHOS_BUILD_ENABLE_POINTER return RET_OK; } #ifdef OHOS_BUILD_ENABLE_POINTER int32_t MMIService::ReadMouseScrollRows(int32_t &rows) { rows = MouseEventHdr->GetMouseScrollRows(); return RET_OK; } #endif // OHOS_BUILD_ENABLE_POINTER int32_t MMIService::GetMouseScrollRows(int32_t &rows) { CALL_DEBUG_ENTER; #ifdef OHOS_BUILD_ENABLE_POINTER int32_t ret = delegateTasks_.PostSyncTask(std::bind(&MMIService::ReadMouseScrollRows, this, std::ref(rows))); if (ret != RET_OK) { MMI_HILOGE("Get the number of mouse scrolling rows failed, return %{public}d", ret); return RET_ERR; } #endif // OHOS_BUILD_ENABLE_POINTER return RET_OK; } int32_t MMIService::SetPointerSize(int32_t size) { CALL_DEBUG_ENTER; #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING) int32_t ret = delegateTasks_.PostSyncTask(std::bind(&IPointerDrawingManager::SetPointerSize, IPointerDrawingManager::GetInstance(), size)); if (ret != RET_OK) { MMI_HILOGE("Set pointer size failed,return %{public}d", ret); return ret; } #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING return RET_OK; } #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING) int32_t MMIService::ReadPointerSize(int32_t &size) { size = IPointerDrawingManager::GetInstance()->GetPointerSize(); return RET_OK; } #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING int32_t MMIService::GetPointerSize(int32_t &size) { CALL_DEBUG_ENTER; #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING) int32_t ret = delegateTasks_.PostSyncTask(std::bind(&MMIService::ReadPointerSize, this, std::ref(size))); if (ret != RET_OK) { MMI_HILOGE("Get pointer size failed, return %{public}d", ret); return ret; } #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING return RET_OK; } int32_t MMIService::SetMousePrimaryButton(int32_t primaryButton) { CALL_DEBUG_ENTER; #if defined OHOS_BUILD_ENABLE_POINTER int32_t ret = delegateTasks_.PostSyncTask( std::bind(&MouseEventNormalize::SetMousePrimaryButton, MouseEventHdr, primaryButton)); if (ret != RET_OK) { MMI_HILOGE("Set mouse primary button failed,return %{public}d", ret); return ret; } #endif // OHOS_BUILD_ENABLE_POINTER return RET_OK; } #ifdef OHOS_BUILD_ENABLE_POINTER int32_t MMIService::ReadMousePrimaryButton(int32_t &primaryButton) { primaryButton = MouseEventHdr->GetMousePrimaryButton(); return RET_OK; } #endif // OHOS_BUILD_ENABLE_POINTER int32_t MMIService::GetMousePrimaryButton(int32_t &primaryButton) { CALL_DEBUG_ENTER; #ifdef OHOS_BUILD_ENABLE_POINTER int32_t ret = delegateTasks_.PostSyncTask(std::bind(&MMIService::ReadMousePrimaryButton, this, std::ref(primaryButton))); if (ret != RET_OK) { MMI_HILOGE("Get mouse primary button failed,return %{public}d", ret); return RET_ERR; } #endif // OHOS_BUILD_ENABLE_POINTER return RET_OK; } int32_t MMIService::SetPointerVisible(bool visible) { CALL_DEBUG_ENTER; #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING) int32_t ret = delegateTasks_.PostSyncTask(std::bind(&IPointerDrawingManager::SetPointerVisible, IPointerDrawingManager::GetInstance(), GetCallingPid(), visible)); if (ret != RET_OK) { MMI_HILOGE("Set pointer visible failed,return %{public}d", ret); return ret; } #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING return RET_OK; } #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING) int32_t MMIService::CheckPointerVisible(bool &visible) { visible = IPointerDrawingManager::GetInstance()->IsPointerVisible(); return RET_OK; } #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING int32_t MMIService::IsPointerVisible(bool &visible) { CALL_DEBUG_ENTER; #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING) int32_t ret = delegateTasks_.PostSyncTask(std::bind(&MMIService::CheckPointerVisible, this, std::ref(visible))); if (ret != RET_OK) { MMI_HILOGE("Is pointer visible failed,return %{public}d", ret); return RET_ERR; } #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING return RET_OK; } int32_t MMIService::MarkProcessed(int32_t eventType, int32_t eventId) { CALL_DEBUG_ENTER; int32_t ret = delegateTasks_.PostSyncTask(std::bind(&ANRManager::MarkProcessed, ANRMgr, GetCallingPid(), eventType, eventId)); if (ret != RET_OK) { MMI_HILOGE("Mark event processed failed, ret:%{public}d", ret); return RET_ERR; } return RET_OK; } int32_t MMIService::SetPointerColor(int32_t color) { CALL_DEBUG_ENTER; #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING) int32_t ret = delegateTasks_.PostSyncTask(std::bind(&IPointerDrawingManager::SetPointerColor, IPointerDrawingManager::GetInstance(), color)); if (ret != RET_OK) { MMI_HILOGE("Set pointer color failed,return %{public}d", ret); return ret; } #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING return RET_OK; } #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING) int32_t MMIService::ReadPointerColor(int32_t &color) { color = IPointerDrawingManager::GetInstance()->GetPointerColor(); return RET_OK; } #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING int32_t MMIService::GetPointerColor(int32_t &color) { CALL_DEBUG_ENTER; #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING) int32_t ret = delegateTasks_.PostSyncTask(std::bind(&MMIService::ReadPointerColor, this, std::ref(color))); if (ret != RET_OK) { MMI_HILOGE("Get pointer color failed, return %{public}d", ret); return ret; } #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING return RET_OK; } int32_t MMIService::SetPointerSpeed(int32_t speed) { CALL_DEBUG_ENTER; #ifdef OHOS_BUILD_ENABLE_POINTER int32_t ret = delegateTasks_.PostSyncTask(std::bind(&MouseEventNormalize::SetPointerSpeed, MouseEventHdr, speed)); if (ret != RET_OK) { MMI_HILOGE("Set pointer speed failed,return %{public}d", ret); return RET_ERR; } #endif // OHOS_BUILD_ENABLE_POINTER return RET_OK; } #ifdef OHOS_BUILD_ENABLE_POINTER int32_t MMIService::ReadPointerSpeed(int32_t &speed) { speed = MouseEventHdr->GetPointerSpeed(); return RET_OK; } #endif // OHOS_BUILD_ENABLE_POINTER int32_t MMIService::GetPointerSpeed(int32_t &speed) { CALL_DEBUG_ENTER; #ifdef OHOS_BUILD_ENABLE_POINTER int32_t ret = delegateTasks_.PostSyncTask(std::bind(&MMIService::ReadPointerSpeed, this, std::ref(speed))); if (ret != RET_OK) { MMI_HILOGE("Get pointer speed failed,return %{public}d", ret); return RET_ERR; } #endif // OHOS_BUILD_ENABLE_POINTER return RET_OK; } int32_t MMIService::SetPointerStyle(int32_t windowId, PointerStyle pointerStyle) { CALL_DEBUG_ENTER; #ifdef OHOS_BUILD_ENABLE_POINTER int32_t ret = delegateTasks_.PostSyncTask(std::bind(&IPointerDrawingManager::SetPointerStyle, IPointerDrawingManager::GetInstance(), GetCallingPid(), windowId, pointerStyle)); if (ret != RET_OK) { MMI_HILOGE("Set pointer style failed,return %{public}d", ret); return ret; } #endif // OHOS_BUILD_ENABLE_POINTER return RET_OK; } int32_t MMIService::GetPointerStyle(int32_t windowId, PointerStyle &pointerStyle) { CALL_DEBUG_ENTER; #ifdef OHOS_BUILD_ENABLE_POINTER int32_t ret = delegateTasks_.PostSyncTask(std::bind(&IPointerDrawingManager::GetPointerStyle, IPointerDrawingManager::GetInstance(), GetCallingPid(), windowId, std::ref(pointerStyle))); if (ret != RET_OK) { MMI_HILOGE("Get pointer style failed,return %{public}d", ret); return ret; } #endif // OHOS_BUILD_ENABLE_POINTER return RET_OK; } int32_t MMIService::SetHoverScrollState(bool state) { CALL_DEBUG_ENTER; #if defined OHOS_BUILD_ENABLE_POINTER int32_t ret = delegateTasks_.PostSyncTask(std::bind(&InputWindowsManager::SetHoverScrollState, WinMgr, state)); if (ret != RET_OK) { MMI_HILOGE("Set mouse hover scroll state failed,return %{public}d", ret); return ret; } #endif // OHOS_BUILD_ENABLE_POINTER return RET_OK; } #ifdef OHOS_BUILD_ENABLE_POINTER int32_t MMIService::ReadHoverScrollState(bool &state) { state = WinMgr->GetHoverScrollState(); return RET_OK; } #endif // OHOS_BUILD_ENABLE_POINTER int32_t MMIService::GetHoverScrollState(bool &state) { CALL_DEBUG_ENTER; #ifdef OHOS_BUILD_ENABLE_POINTER int32_t ret = delegateTasks_.PostSyncTask(std::bind(&MMIService::ReadHoverScrollState, this, std::ref(state))); if (ret != RET_OK) { MMI_HILOGE("Get mouse hover scroll state, return %{public}d", ret); return ret; } #endif // OHOS_BUILD_ENABLE_POINTER return RET_OK; } int32_t MMIService::OnSupportKeys(int32_t deviceId, std::vector &keys, std::vector &keystroke) { CALL_DEBUG_ENTER; int32_t ret = InputDevMgr->SupportKeys(deviceId, keys, keystroke); if (keystroke.size() > MAX_SUPPORT_KEY) { MMI_HILOGE("Device exceeds the max range"); return RET_ERR; } if (ret != RET_OK) { MMI_HILOGE("Device id not support"); return ret; } return RET_OK; } int32_t MMIService::SupportKeys(int32_t deviceId, std::vector &keys, std::vector &keystroke) { CALL_DEBUG_ENTER; int32_t ret = delegateTasks_.PostSyncTask(std::bind(&MMIService::OnSupportKeys, this, deviceId, keys, std::ref(keystroke))); if (ret != RET_OK) { MMI_HILOGE("Support keys info process failed, ret:%{public}d", ret); return ret; } return RET_OK; } int32_t MMIService::OnGetDeviceIds(std::vector &ids) { CALL_DEBUG_ENTER; ids = InputDevMgr->GetInputDeviceIds(); return RET_OK; } int32_t MMIService::GetDeviceIds(std::vector &ids) { CALL_DEBUG_ENTER; int32_t ret = delegateTasks_.PostSyncTask(std::bind(&MMIService::OnGetDeviceIds, this, std::ref(ids))); if (ret != RET_OK) { MMI_HILOGE("Get deviceids failed, ret:%{public}d", ret); return RET_ERR; } return RET_OK; } int32_t MMIService::OnGetDevice(int32_t deviceId, std::shared_ptr &inputDevice) { CALL_DEBUG_ENTER; if (InputDevMgr->GetInputDevice(deviceId) == nullptr) { MMI_HILOGE("Input device not found"); return COMMON_PARAMETER_ERROR; } inputDevice = InputDevMgr->GetInputDevice(deviceId); return RET_OK; } int32_t MMIService::GetDevice(int32_t deviceId, std::shared_ptr &inputDevice) { CALL_DEBUG_ENTER; int32_t ret = delegateTasks_.PostSyncTask(std::bind(&MMIService::OnGetDevice, this, deviceId, std::ref(inputDevice))); if (ret != RET_OK) { MMI_HILOGE("Get input device info failed, ret:%{public}d", ret); return ret; } return RET_OK; } int32_t MMIService::OnRegisterDevListener(int32_t pid) { auto sess = GetSession(GetClientFd(pid)); CHKPR(sess, RET_ERR); InputDevMgr->AddDevListener(sess, [sess](int32_t id, const std::string &type) { CALL_DEBUG_ENTER; CHKPV(sess); NetPacket pkt(MmiMessageId::ADD_INPUT_DEVICE_LISTENER); pkt << type << id; if (pkt.ChkRWError()) { MMI_HILOGE("Packet write data failed"); return; } if (!sess->SendMsg(pkt)) { MMI_HILOGE("Sending failed"); return; } }); return RET_OK; } int32_t MMIService::RegisterDevListener() { CALL_DEBUG_ENTER; int32_t pid = GetCallingPid(); int32_t ret = delegateTasks_.PostSyncTask(std::bind(&MMIService::OnRegisterDevListener, this, pid)); if (ret != RET_OK) { MMI_HILOGE("Register device listener failed, ret:%{public}d", ret); return RET_ERR; } return RET_OK; } int32_t MMIService::OnUnregisterDevListener(int32_t pid) { auto sess = GetSession(GetClientFd(pid)); InputDevMgr->RemoveDevListener(sess); return RET_OK; } int32_t MMIService::UnregisterDevListener() { CALL_DEBUG_ENTER; int32_t pid = GetCallingPid(); int32_t ret = delegateTasks_.PostSyncTask(std::bind(&MMIService::OnUnregisterDevListener, this, pid)); if (ret != RET_OK) { MMI_HILOGE("Unregister device listener failed failed, ret:%{public}d", ret); return RET_ERR; } return RET_OK; } int32_t MMIService::OnGetKeyboardType(int32_t deviceId, int32_t &keyboardType) { int32_t ret = InputDevMgr->GetKeyboardType(deviceId, keyboardType); if (ret != RET_OK) { MMI_HILOGE("GetKeyboardType call failed"); return ret; } return RET_OK; } int32_t MMIService::GetKeyboardType(int32_t deviceId, int32_t &keyboardType) { CALL_DEBUG_ENTER; int32_t ret = delegateTasks_.PostSyncTask(std::bind(&MMIService::OnGetKeyboardType, this, deviceId, std::ref(keyboardType))); if (ret != RET_OK) { MMI_HILOGE("Get keyboard type failed, ret:%{public}d", ret); return ret; } return ret; } int32_t MMIService::SetKeyboardRepeatDelay(int32_t delay) { CALL_DEBUG_ENTER; #ifdef OHOS_BUILD_ENABLE_KEYBOARD int32_t ret = delegateTasks_.PostSyncTask(std::bind(&KeyAutoRepeat::SetKeyboardRepeatDelay, KeyRepeat, delay)); if (ret != RET_OK) { MMI_HILOGE("Set keyboard repeat delay failed, ret:%{public}d", ret); return ret; } #endif // OHOS_BUILD_ENABLE_KEYBOARD return RET_OK; } int32_t MMIService::SetKeyboardRepeatRate(int32_t rate) { CALL_DEBUG_ENTER; #ifdef OHOS_BUILD_ENABLE_KEYBOARD int32_t ret = delegateTasks_.PostSyncTask(std::bind(&KeyAutoRepeat::SetKeyboardRepeatRate, KeyRepeat, rate)); if (ret != RET_OK) { MMI_HILOGE("Set keyboard repeat rate failed, ret:%{public}d", ret); return ret; } #endif // OHOS_BUILD_ENABLE_KEYBOARD return RET_OK; } int32_t MMIService::GetKeyboardRepeatDelay(int32_t &delay) { CALL_DEBUG_ENTER; #ifdef OHOS_BUILD_ENABLE_KEYBOARD int32_t ret = delegateTasks_.PostSyncTask(std::bind(&KeyAutoRepeat::GetKeyboardRepeatDelay, KeyRepeat, std::ref(delay))); if (ret != RET_OK) { MMI_HILOGE("Get keyboard repeat delay failed, ret:%{public}d", ret); return ret; } #endif // OHOS_BUILD_ENABLE_KEYBOARD return RET_OK; } int32_t MMIService::GetKeyboardRepeatRate(int32_t &rate) { CALL_DEBUG_ENTER; #ifdef OHOS_BUILD_ENABLE_KEYBOARD int32_t ret = delegateTasks_.PostSyncTask(std::bind(&KeyAutoRepeat::GetKeyboardRepeatRate, KeyRepeat, std::ref(rate))); if (ret != RET_OK) { MMI_HILOGE("Get keyboard repeat rate failed, ret:%{public}d", ret); return ret; } #endif // OHOS_BUILD_ENABLE_KEYBOARD return RET_OK; } #if defined(OHOS_BUILD_ENABLE_INTERCEPTOR) || defined(OHOS_BUILD_ENABLE_MONITOR) int32_t MMIService::CheckAddInput(int32_t pid, InputHandlerType handlerType, HandleEventType eventType, int32_t priority, uint32_t deviceTags) { auto sess = GetSessionByPid(pid); CHKPR(sess, ERROR_NULL_POINTER); return sMsgHandler_.OnAddInputHandler(sess, handlerType, eventType, priority, deviceTags); } #endif // OHOS_BUILD_ENABLE_INTERCEPTOR || OHOS_BUILD_ENABLE_MONITOR int32_t MMIService::AddInputHandler(InputHandlerType handlerType, HandleEventType eventType, int32_t priority, uint32_t deviceTags) { CALL_INFO_TRACE; #if defined(OHOS_BUILD_ENABLE_INTERCEPTOR) || defined(OHOS_BUILD_ENABLE_MONITOR) int32_t pid = GetCallingPid(); int32_t ret = delegateTasks_.PostSyncTask( std::bind(&MMIService::CheckAddInput, this, pid, handlerType, eventType, priority, deviceTags)); if (ret != RET_OK) { MMI_HILOGE("Add input handler failed, ret:%{public}d", ret); return RET_ERR; } #endif // OHOS_BUILD_ENABLE_INTERCEPTOR || OHOS_BUILD_ENABLE_MONITOR return RET_OK; } #if defined(OHOS_BUILD_ENABLE_INTERCEPTOR) || defined(OHOS_BUILD_ENABLE_MONITOR) int32_t MMIService::CheckRemoveInput(int32_t pid, InputHandlerType handlerType, HandleEventType eventType, int32_t priority, uint32_t deviceTags) { auto sess = GetSessionByPid(pid); CHKPR(sess, ERROR_NULL_POINTER); return sMsgHandler_.OnRemoveInputHandler(sess, handlerType, eventType, priority, deviceTags); } #endif // OHOS_BUILD_ENABLE_INTERCEPTOR || OHOS_BUILD_ENABLE_MONITOR int32_t MMIService::RemoveInputHandler(InputHandlerType handlerType, HandleEventType eventType, int32_t priority, uint32_t deviceTags) { CALL_INFO_TRACE; #if defined(OHOS_BUILD_ENABLE_INTERCEPTOR) || defined(OHOS_BUILD_ENABLE_MONITOR) int32_t pid = GetCallingPid(); int32_t ret = delegateTasks_.PostSyncTask( std::bind(&MMIService::CheckRemoveInput, this, pid, handlerType, eventType, priority, deviceTags)); if (ret != RET_OK) { MMI_HILOGE("Remove input handler failed, ret:%{public}d", ret); return RET_ERR; } #endif // OHOS_BUILD_ENABLE_INTERCEPTOR || OHOS_BUILD_ENABLE_MONITOR return RET_OK; } #ifdef OHOS_BUILD_ENABLE_MONITOR int32_t MMIService::CheckMarkConsumed(int32_t pid, int32_t eventId) { auto sess = GetSessionByPid(pid); CHKPR(sess, ERROR_NULL_POINTER); return sMsgHandler_.OnMarkConsumed(sess, eventId); } #endif // OHOS_BUILD_ENABLE_MONITOR int32_t MMIService::MarkEventConsumed(int32_t eventId) { CALL_DEBUG_ENTER; #ifdef OHOS_BUILD_ENABLE_MONITOR int32_t pid = GetCallingPid(); int32_t ret = delegateTasks_.PostSyncTask(std::bind(&MMIService::CheckMarkConsumed, this, pid, eventId)); if (ret != RET_OK) { MMI_HILOGE("Mark event consumed failed, ret:%{public}d", ret); return RET_ERR; } #endif // OHOS_BUILD_ENABLE_MONITOR return RET_OK; } int32_t MMIService::MoveMouseEvent(int32_t offsetX, int32_t offsetY) { CALL_DEBUG_ENTER; #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING) int32_t ret = delegateTasks_.PostSyncTask(std::bind(&ServerMsgHandler::OnMoveMouse, &sMsgHandler_, offsetX, offsetY)); if (ret != RET_OK) { MMI_HILOGE("The movemouse event processed failed, ret:%{public}d", ret); return RET_ERR; } #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING return RET_OK; } int32_t MMIService::InjectKeyEvent(const std::shared_ptr keyEvent) { CALL_DEBUG_ENTER; #ifdef OHOS_BUILD_ENABLE_KEYBOARD int32_t ret = delegateTasks_.PostSyncTask(std::bind(&MMIService::CheckInjectKeyEvent, this, keyEvent)); if (ret != RET_OK) { MMI_HILOGE("Inject key event failed, ret:%{public}d", ret); return RET_ERR; } #endif // OHOS_BUILD_ENABLE_KEYBOARD return RET_OK; } #ifdef OHOS_BUILD_ENABLE_KEYBOARD int32_t MMIService::CheckInjectKeyEvent(const std::shared_ptr keyEvent) { CHKPR(keyEvent, ERROR_NULL_POINTER); return sMsgHandler_.OnInjectKeyEvent(keyEvent); } #endif // OHOS_BUILD_ENABLE_KEYBOARD #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH) int32_t MMIService::CheckInjectPointerEvent(const std::shared_ptr pointerEvent) { CHKPR(pointerEvent, ERROR_NULL_POINTER); return sMsgHandler_.OnInjectPointerEvent(pointerEvent); } #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH int32_t MMIService::InjectPointerEvent(const std::shared_ptr pointerEvent) { CALL_DEBUG_ENTER; #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH) int32_t ret = delegateTasks_.PostSyncTask(std::bind(&MMIService::CheckInjectPointerEvent, this, pointerEvent)); if (ret != RET_OK) { MMI_HILOGE("Inject pointer event failed, ret:%{public}d", ret); return RET_ERR; } #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH return RET_OK; } #ifdef OHOS_RSS_CLIENT void MMIService::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) { CALL_INFO_TRACE; if (systemAbilityId == RES_SCHED_SYS_ABILITY_ID) { int sleepSeconds = 1; sleep(sleepSeconds); uint64_t tid = tid_.load(); std::unordered_map payload; payload["uid"] = std::to_string(getuid()); payload["pid"] = std::to_string(getpid()); ResourceSchedule::ResSchedClient::GetInstance().ReportData( ResourceSchedule::ResType::RES_TYPE_REPORT_MMI_PROCESS, tid, payload); } } #endif int32_t MMIService::SubscribeKeyEvent(int32_t subscribeId, const std::shared_ptr option) { CALL_DEBUG_ENTER; #ifdef OHOS_BUILD_ENABLE_KEYBOARD int32_t pid = GetCallingPid(); int32_t ret = delegateTasks_.PostSyncTask( std::bind(&ServerMsgHandler::OnSubscribeKeyEvent, &sMsgHandler_, this, pid, subscribeId, option)); if (ret != RET_OK) { MMI_HILOGE("The subscribe key event processed failed, ret:%{public}d", ret); return RET_ERR; } #endif // OHOS_BUILD_ENABLE_KEYBOARD return RET_OK; } int32_t MMIService::UnsubscribeKeyEvent(int32_t subscribeId) { CALL_INFO_TRACE; #ifdef OHOS_BUILD_ENABLE_KEYBOARD int32_t pid = GetCallingPid(); int32_t ret = delegateTasks_.PostSyncTask( std::bind(&ServerMsgHandler::OnUnsubscribeKeyEvent, &sMsgHandler_, this, pid, subscribeId)); if (ret != RET_OK) { MMI_HILOGE("The unsubscribe key event processed failed, ret:%{public}d", ret); return RET_ERR; } #endif // OHOS_BUILD_ENABLE_KEYBOARD return RET_OK; } int32_t MMIService::SubscribeSwitchEvent(int32_t subscribeId) { CALL_INFO_TRACE; #ifdef OHOS_BUILD_ENABLE_SWITCH int32_t pid = GetCallingPid(); int32_t ret = delegateTasks_.PostSyncTask( std::bind(&ServerMsgHandler::OnSubscribeSwitchEvent, &sMsgHandler_, this, pid, subscribeId)); if (ret != RET_OK) { MMI_HILOGE("The subscribe switch event processed failed, ret:%{public}d", ret); return RET_ERR; } #endif // OHOS_BUILD_ENABLE_SWITCH return RET_OK; } int32_t MMIService::UnsubscribeSwitchEvent(int32_t subscribeId) { CALL_INFO_TRACE; #ifdef OHOS_BUILD_ENABLE_SWITCH int32_t pid = GetCallingPid(); int32_t ret = delegateTasks_.PostSyncTask( std::bind(&ServerMsgHandler::OnUnsubscribeSwitchEvent, &sMsgHandler_, this, pid, subscribeId)); if (ret != RET_OK) { MMI_HILOGE("The unsubscribe switch event processed failed, ret:%{public}d", ret); return RET_ERR; } #endif // OHOS_BUILD_ENABLE_SWITCH return RET_OK; } int32_t MMIService::SetAnrObserver() { CALL_DEBUG_ENTER; int32_t pid = GetCallingPid(); int32_t ret = delegateTasks_.PostSyncTask(std::bind(&ANRManager::SetANRNoticedPid, ANRMgr, pid)); if (ret != RET_OK) { MMI_HILOGE("Set ANRNoticed pid failed, ret:%{public}d", ret); return RET_ERR; } return RET_OK; } int32_t MMIService::GetDisplayBindInfo(DisplayBindInfos &infos) { CALL_DEBUG_ENTER; int32_t ret = delegateTasks_.PostSyncTask(std::bind(&InputWindowsManager::GetDisplayBindInfo, WinMgr, std::ref(infos))); if (ret != RET_OK) { MMI_HILOGE("GetDisplayBindInfo pid failed, ret:%{public}d", ret); return RET_ERR; } return RET_OK; } int32_t MMIService::SetDisplayBind(int32_t deviceId, int32_t displayId, std::string &msg) { CALL_DEBUG_ENTER; int32_t ret = delegateTasks_.PostSyncTask( std::bind(&InputWindowsManager::SetDisplayBind, WinMgr, deviceId, displayId, std::ref(msg))); if (ret != RET_OK) { MMI_HILOGE("SetDisplayBind pid failed, ret:%{public}d", ret); return RET_ERR; } return RET_OK; } int32_t MMIService::GetFunctionKeyState(int32_t funcKey, bool &state) { CALL_INFO_TRACE; #ifdef OHOS_BUILD_ENABLE_KEYBOARD int32_t ret = delegateTasks_.PostSyncTask( std::bind(&ServerMsgHandler::OnGetFunctionKeyState, &sMsgHandler_, funcKey, std::ref(state))); if (ret != RET_OK) { MMI_HILOGE("Failed to get the keyboard status, ret:%{public}d", ret); return RET_ERR; } #else MMI_HILOGD("Function not supported"); #endif // OHOS_BUILD_ENABLE_KEYBOARD return RET_OK; } int32_t MMIService::SetFunctionKeyState(int32_t funcKey, bool enable) { CALL_INFO_TRACE; #ifdef OHOS_BUILD_ENABLE_KEYBOARD int32_t ret = delegateTasks_.PostSyncTask( std::bind(&ServerMsgHandler::OnSetFunctionKeyState, &sMsgHandler_, funcKey, enable)); if (ret != RET_OK) { MMI_HILOGE("Failed to update the keyboard status, ret:%{public}d", ret); return RET_ERR; } #else MMI_HILOGD("Function not supported"); #endif // OHOS_BUILD_ENABLE_KEYBOARD return RET_OK; } int32_t MMIService::SetPointerLocation(int32_t x, int32_t y) { CALL_DEBUG_ENTER; #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING) int32_t ret = delegateTasks_.PostSyncTask(std::bind(&MouseEventNormalize::SetPointerLocation, MouseEventHdr, x, y)); if (ret != RET_OK) { MMI_HILOGE("Set pointer location failed,ret %{public}d", ret); return RET_ERR; } #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING return RET_OK; } void MMIService::OnDelegateTask(epoll_event &ev) { if ((ev.events & EPOLLIN) == 0) { MMI_HILOGW("Not epollin"); return; } DelegateTasks::TaskData data = {}; auto res = read(delegateTasks_.GetReadFd(), &data, sizeof(data)); if (res == -1) { MMI_HILOGW("Read failed erron:%{public}d", errno); } MMI_HILOGD("RemoteRequest notify td:%{public}" PRId64 ",std:%{public}" PRId64 "" ",taskId:%{public}d", GetThisThreadId(), data.tid, data.taskId); delegateTasks_.ProcessTasks(); } void MMIService::OnThread() { SetThreadName(std::string("mmi_service")); uint64_t tid = GetThisThreadId(); delegateTasks_.SetWorkerThreadId(tid); MMI_HILOGI("Main worker thread start. tid:%{public}" PRId64 "", tid); #ifdef OHOS_RSS_CLIENT tid_.store(tid); #endif libinputAdapter_.ProcessPendingEvents(); while (state_ == ServiceRunningState::STATE_RUNNING) { epoll_event ev[MAX_EVENT_SIZE] = {}; int32_t timeout = TimerMgr->CalcNextDelay(); MMI_HILOGD("timeout:%{public}d", timeout); int32_t count = EpollWait(ev[0], MAX_EVENT_SIZE, timeout, mmiFd_); for (int32_t i = 0; i < count && state_ == ServiceRunningState::STATE_RUNNING; i++) { auto mmiEd = reinterpret_cast(ev[i].data.ptr); CHKPC(mmiEd); if (mmiEd->event_type == EPOLL_EVENT_INPUT) { libinputAdapter_.EventDispatch(mmiEd->fd); } else if (mmiEd->event_type == EPOLL_EVENT_SOCKET) { OnEpollEvent(ev[i]); } else if (mmiEd->event_type == EPOLL_EVENT_SIGNAL) { OnSignalEvent(mmiEd->fd); } else if (mmiEd->event_type == EPOLL_EVENT_ETASK) { OnDelegateTask(ev[i]); } else { MMI_HILOGW("Unknown epoll event type:%{public}d", mmiEd->event_type); } } TimerMgr->ProcessTimers(); if (state_ != ServiceRunningState::STATE_RUNNING) { break; } } MMI_HILOGI("Main worker thread stop. tid:%{public}" PRId64 "", tid); } bool MMIService::InitSignalHandler() { CALL_DEBUG_ENTER; sigset_t mask = { 0 }; int32_t retCode = sigfillset(&mask); if (retCode < 0) { MMI_HILOGE("Fill signal set failed:%{public}d", errno); return false; } retCode = sigprocmask(SIG_SETMASK, &mask, nullptr); if (retCode < 0) { MMI_HILOGE("Sigprocmask failed:%{public}d", errno); return false; } int32_t fdSignal = signalfd(-1, &mask, SFD_NONBLOCK | SFD_CLOEXEC); if (fdSignal < 0) { MMI_HILOGE("Signal fd failed:%{public}d", errno); return false; } retCode = AddEpoll(EPOLL_EVENT_SIGNAL, fdSignal); if (retCode < 0) { MMI_HILOGE("AddEpoll signalFd failed:%{public}d", retCode); close(fdSignal); return false; } return true; } void MMIService::OnSignalEvent(int32_t signalFd) { CALL_DEBUG_ENTER; signalfd_siginfo sigInfo; int32_t size = ::read(signalFd, &sigInfo, sizeof(signalfd_siginfo)); if (size != static_cast(sizeof(signalfd_siginfo))) { MMI_HILOGE("Read signal info failed, invalid size:%{public}d,errno:%{public}d", size, errno); return; } int32_t signo = static_cast(sigInfo.ssi_signo); MMI_HILOGD("Receive signal:%{public}d", signo); switch (signo) { case SIGINT: case SIGQUIT: case SIGILL: case SIGABRT: case SIGBUS: case SIGFPE: case SIGKILL: case SIGSEGV: case SIGTERM: { state_ = ServiceRunningState::STATE_EXIT; break; } default: { break; } } } void MMIService::AddReloadDeviceTimer() { CALL_DEBUG_ENTER; TimerMgr->AddTimer(2000, 2, [this]() { auto deviceIds = InputDevMgr->GetInputDeviceIds(); if (deviceIds.empty()) { libinputAdapter_.ReloadDevice(); } }); } int32_t MMIService::Dump(int32_t fd, const std::vector &args) { CALL_DEBUG_ENTER; if (fd < 0) { MMI_HILOGE("The fd is invalid"); return DUMP_PARAM_ERR; } if (args.empty()) { MMI_HILOGE("The args cannot be empty"); mprintf(fd, "args cannot be empty\n"); MMIEventDump->DumpHelp(fd); return DUMP_PARAM_ERR; } std::vector argList = { "" }; std::transform(args.begin(), args.end(), std::back_inserter(argList), [](const std::u16string &arg) { return Str16ToStr8(arg); }); MMIEventDump->ParseCommand(fd, argList); return RET_OK; } int32_t MMIService::SetMouseCaptureMode(int32_t windowId, bool isCaptureMode) { CALL_DEBUG_ENTER; int32_t ret = delegateTasks_.PostSyncTask( std::bind(&InputWindowsManager::SetMouseCaptureMode, WinMgr, windowId, isCaptureMode)); if (ret != RET_OK) { MMI_HILOGE("Set capture failed,return %{public}d", ret); return RET_ERR; } return RET_OK; } int32_t MMIService::OnGetWindowPid(int32_t windowId, int32_t &windowPid) { CALL_DEBUG_ENTER; windowPid = WinMgr->GetWindowPid(windowId); if (windowPid == RET_ERR) { MMI_HILOGE("Get window pid failed"); } MMI_HILOGD("windowpid is %{public}d", windowPid); return RET_OK; } int32_t MMIService::GetWindowPid(int32_t windowId) { CALL_DEBUG_ENTER; int32_t windowPid = -1; int32_t ret = delegateTasks_.PostSyncTask(std::bind(&MMIService::OnGetWindowPid, this, windowId, std::ref(windowPid))); if (ret != RET_OK) { MMI_HILOGE("OnGetWindowPid failed, ret:%{public}d", ret); return RET_ERR; } MMI_HILOGD("windowpid is %{public}d", windowPid); return windowPid; } int32_t MMIService::AppendExtraData(const ExtraData &extraData) { CALL_DEBUG_ENTER; int32_t ret = delegateTasks_.PostSyncTask(std::bind(&InputWindowsManager::AppendExtraData, WinMgr, extraData)); if (ret != RET_OK) { MMI_HILOGE("Append extra data failed:%{public}d", ret); } return ret; } int32_t MMIService::EnableInputDevice(bool enable) { CALL_DEBUG_ENTER; int32_t ret = delegateTasks_.PostSyncTask(std::bind(&InputDeviceManager::OnEnableInputDevice, InputDevMgr, enable)); if (ret != RET_OK) { MMI_HILOGE("OnEnableInputDevice failed:%{public}d", ret); } return ret; } int32_t MMIService::UpdateSettingsXml(const std::string &businessId, int32_t delay) { std::shared_ptr eventKeyCommandHandler = InputHandler->GetKeyCommandHandler(); CHKPR(eventKeyCommandHandler, RET_ERR); return eventKeyCommandHandler->UpdateSettingsXml(businessId, delay); } int32_t MMIService::SetKeyDownDuration(const std::string &businessId, int32_t delay) { CALL_DEBUG_ENTER; int32_t ret = delegateTasks_.PostSyncTask(std::bind(&MMIService::UpdateSettingsXml, this, businessId, delay)); if (ret != RET_OK) { MMI_HILOGE("Set key down duration failed: %{public}d", ret); return ret; } return RET_OK; } #ifdef OHOS_BUILD_ENABLE_POINTER int32_t MMIService::ReadTouchpadScrollSwich(bool &switchFlag) { MouseEventHdr->GetTouchpadScrollSwitch(switchFlag); return RET_OK; } int32_t MMIService::ReadTouchpadScrollDirection(bool &switchFlag) { MouseEventHdr->GetTouchpadScrollDirection(switchFlag); return RET_OK; } int32_t MMIService::ReadTouchpadTapSwitch(bool &switchFlag) { MouseEventHdr->GetTouchpadTapSwitch(switchFlag); return RET_OK; } int32_t MMIService::ReadTouchpadPointerSpeed(int32_t &speed) { MouseEventHdr->GetTouchpadPointerSpeed(speed); return RET_OK; } int32_t MMIService::ReadTouchpadPinchSwitch(bool &switchFlag) { TouchEventHdr->GetTouchpadPinchSwitch(switchFlag); return RET_OK; } int32_t MMIService::ReadTouchpadSwipeSwitch(bool &switchFlag) { TouchEventHdr->GetTouchpadSwipeSwitch(switchFlag); return RET_OK; } int32_t MMIService::ReadTouchpadRightMenuType(int32_t &type) { MouseEventHdr->GetTouchpadRightClickType(type); return RET_OK; } #endif // OHOS_BUILD_ENABLE_POINTER int32_t MMIService::SetTouchpadScrollSwitch(bool switchFlag) { CALL_DEBUG_ENTER; #if defined OHOS_BUILD_ENABLE_POINTER int32_t ret = delegateTasks_.PostSyncTask(std::bind(&MouseEventNormalize::SetTouchpadScrollSwitch, MouseEventHdr, switchFlag)); if (ret != RET_OK) { MMI_HILOGE("Set touchpad scroll switch failed, return %{public}d", ret); return ret; } #endif // OHOS_BUILD_ENABLE_POINTER return RET_OK; } int32_t MMIService::GetTouchpadScrollSwitch(bool &switchFlag) { CALL_DEBUG_ENTER; #ifdef OHOS_BUILD_ENABLE_POINTER int32_t ret = delegateTasks_.PostSyncTask(std::bind(&MMIService::ReadTouchpadScrollSwich, this, std::ref(switchFlag))); if (ret != RET_OK) { MMI_HILOGE("Get touchpad scroll switch failed, return %{public}d", ret); return RET_ERR; } #endif // OHOS_BUILD_ENABLE_POINTER return RET_OK; } int32_t MMIService::SetTouchpadScrollDirection(bool state) { CALL_DEBUG_ENTER; #if defined OHOS_BUILD_ENABLE_POINTER int32_t ret = delegateTasks_.PostSyncTask(std::bind(&MouseEventNormalize::SetTouchpadScrollDirection, MouseEventHdr, state)); if (ret != RET_OK) { MMI_HILOGE("Set touchpad scroll direction switch failed, return %{public}d", ret); return ret; } #endif // OHOS_BUILD_ENABLE_POINTER return RET_OK; } int32_t MMIService::GetTouchpadScrollDirection(bool &state) { CALL_DEBUG_ENTER; #ifdef OHOS_BUILD_ENABLE_POINTER int32_t ret = delegateTasks_.PostSyncTask(std::bind(&MMIService::ReadTouchpadScrollDirection, this, std::ref(state))); if (ret != RET_OK) { MMI_HILOGE("Get touchpad scroll direction switch failed, return %{public}d", ret); return RET_ERR; } #endif // OHOS_BUILD_ENABLE_POINTER return RET_OK; } int32_t MMIService::SetTouchpadTapSwitch(bool switchFlag) { CALL_DEBUG_ENTER; #if defined OHOS_BUILD_ENABLE_POINTER int32_t ret = delegateTasks_.PostSyncTask(std::bind(&MouseEventNormalize::SetTouchpadTapSwitch, MouseEventHdr, switchFlag)); if (ret != RET_OK) { MMI_HILOGE("Set touchpad tap switch failed, return %{public}d", ret); return ret; } #endif // OHOS_BUILD_ENABLE_POINTER return RET_OK; } int32_t MMIService::GetTouchpadTapSwitch(bool &switchFlag) { CALL_DEBUG_ENTER; #ifdef OHOS_BUILD_ENABLE_POINTER int32_t ret = delegateTasks_.PostSyncTask(std::bind(&MMIService::ReadTouchpadTapSwitch, this, std::ref(switchFlag))); if (ret != RET_OK) { MMI_HILOGE("Get touchpad tap switch failed, return %{public}d", ret); return RET_ERR; } #endif // OHOS_BUILD_ENABLE_POINTER return RET_OK; } int32_t MMIService::SetTouchpadPointerSpeed(int32_t speed) { CALL_DEBUG_ENTER; #if defined OHOS_BUILD_ENABLE_POINTER int32_t ret = delegateTasks_.PostSyncTask(std::bind(&MouseEventNormalize::SetTouchpadPointerSpeed, MouseEventHdr, speed)); if (ret != RET_OK) { MMI_HILOGE("Set touchpad speed failed, return %{public}d", ret); return ret; } #endif // OHOS_BUILD_ENABLE_POINTER return RET_OK; } int32_t MMIService::GetTouchpadPointerSpeed(int32_t &speed) { CALL_DEBUG_ENTER; #ifdef OHOS_BUILD_ENABLE_POINTER int32_t ret = delegateTasks_.PostSyncTask(std::bind(&MMIService::ReadTouchpadPointerSpeed, this, std::ref(speed))); if (ret != RET_OK) { MMI_HILOGE("Get touchpad speed failed, return %{public}d", ret); return RET_ERR; } #endif // OHOS_BUILD_ENABLE_POINTER return RET_OK; } int32_t MMIService::SetTouchpadPinchSwitch(bool switchFlag) { CALL_DEBUG_ENTER; #if defined OHOS_BUILD_ENABLE_POINTER int32_t ret = delegateTasks_.PostSyncTask(std::bind(&TouchEventNormalize::SetTouchpadPinchSwitch, TouchEventHdr, switchFlag)); if (ret != RET_OK) { MMI_HILOGE("Set touch pad pinch switch failed, return %{public}d", ret); return ret; } #endif // OHOS_BUILD_ENABLE_POINTER return RET_OK; } int32_t MMIService::GetTouchpadPinchSwitch(bool &switchFlag) { CALL_DEBUG_ENTER; #ifdef OHOS_BUILD_ENABLE_POINTER int32_t ret = delegateTasks_.PostSyncTask(std::bind(&MMIService::ReadTouchpadPinchSwitch, this, std::ref(switchFlag))); if (ret != RET_OK) { MMI_HILOGE("Get touch pad pinch switch failed, return %{public}d", ret); return RET_ERR; } #endif // OHOS_BUILD_ENABLE_POINTER return RET_OK; } int32_t MMIService::SetTouchpadSwipeSwitch(bool switchFlag) { CALL_DEBUG_ENTER; #if defined OHOS_BUILD_ENABLE_POINTER int32_t ret = delegateTasks_.PostSyncTask(std::bind(&TouchEventNormalize::SetTouchpadSwipeSwitch, TouchEventHdr, switchFlag)); if (ret != RET_OK) { MMI_HILOGE("Set touchpad swipe switch failed, return %{public}d", ret); return ret; } #endif // OHOS_BUILD_ENABLE_POINTER return RET_OK; } int32_t MMIService::GetTouchpadSwipeSwitch(bool &switchFlag) { CALL_DEBUG_ENTER; #ifdef OHOS_BUILD_ENABLE_POINTER int32_t ret = delegateTasks_.PostSyncTask(std::bind(&MMIService::ReadTouchpadSwipeSwitch, this, std::ref(switchFlag))); if (ret != RET_OK) { MMI_HILOGE("Get touchpad swipe switch failed, return %{public}d", ret); return RET_ERR; } #endif // OHOS_BUILD_ENABLE_POINTER return RET_OK; } int32_t MMIService::SetTouchpadRightClickType(int32_t type) { CALL_DEBUG_ENTER; #if defined OHOS_BUILD_ENABLE_POINTER int32_t ret = delegateTasks_.PostSyncTask(std::bind(&MouseEventNormalize::SetTouchpadRightClickType, MouseEventHdr, type)); if (ret != RET_OK) { MMI_HILOGE("Set touchpad right button menu type failed, return %{public}d", ret); return ret; } #endif // OHOS_BUILD_ENABLE_POINTER return RET_OK; } int32_t MMIService::GetTouchpadRightClickType(int32_t &type) { CALL_DEBUG_ENTER; #ifdef OHOS_BUILD_ENABLE_POINTER int32_t ret = delegateTasks_.PostSyncTask(std::bind(&MMIService::ReadTouchpadRightMenuType, this, std::ref(type))); if (ret != RET_OK) { MMI_HILOGE("Get touchpad right button menu type failed, return %{public}d", ret); return RET_ERR; } #endif // OHOS_BUILD_ENABLE_POINTER return RET_OK; } } // namespace MMI } // namespace OHOS