本篇文章基于社區(qū)weekly_20230207的代碼,對多模輸入客戶端注冊監(jiān)聽流程和多模服務(wù)端事件派發(fā)流程作了簡單介紹。相信大家通過本文,對多模輸入子系統(tǒng)能有一個大致了解。

??想了解更多關(guān)于開源的內(nèi)容,請訪問:??
??51CTO 開源基礎(chǔ)軟件社區(qū)??
??https://ost.51cto.com??
1、簡介
多模輸入子系統(tǒng)是 OpenHarmony 輸入事件管理框架。多模輸入服務(wù)接收多種類型輸入設(shè)備(觸摸屏、鼠標、鍵盤、觸摸板等)的輸入事件,通過歸一/標準化處理后,分發(fā)給多??蛻舳耍☉?yīng)用,系統(tǒng)服務(wù))。多模輸入還提供事件注入接口,該接口目前僅對系統(tǒng)應(yīng)用開放。
多模輸入子系統(tǒng)分為框架部分和服務(wù)部分:框架部分封裝了各種接口給其他子系統(tǒng)和應(yīng)用來調(diào)用;服務(wù)部分實現(xiàn)了這些接口,并且實現(xiàn)了事件派發(fā)處理的核心邏輯。這兩個部分運行在不同進程中,根據(jù)具體接口,通過socket或者binder ipc機制進行通信。
(1)主要模塊交互圖

(2)代碼目錄
/foundation/multimodalinput/input
├── frameworks # napi接口代碼,客戶端實現(xiàn)代碼
├── interfaces # 對外接口存放目錄
│ └── native # 對外native層接口存放目錄
│ └── innerkits # 對系統(tǒng)內(nèi)部子系統(tǒng)提供native層接口存放目錄
├── service # 服務(wù)端代碼
├── sa_profile # 服務(wù)啟動配置文件
├── tools # 輸入事件注入工具
├── uinput # 輸入事件注入模塊
├── util # socket相關(guān)工具類
2、多??蛻舳藛恿鞒?/h2>(1)時序圖

說明:
- Ability生命周期函數(shù)OnStart()中會去創(chuàng)建WindowImpl實例,WindowImpl::Create()中調(diào)用InputTransferStation::AddInputWindow()創(chuàng)建InputEventListener并注冊到InputManagerImpl中。后續(xù)收到多模服務(wù)端發(fā)送來的輸入事件之后會通過回調(diào)InputEventListener的接口函數(shù),把事件上報到窗口管理,窗口管理再把事件進一步上報給ArkUI。
- InputManagerImpl::SetWindowInputEventConsumer()方法中會去初始化多模Socket客戶端,用于接收多模服務(wù)端發(fā)來的輸入事件。
(2)ArkUI何時注冊的窗口管理輸入事件回調(diào)?
AceAbility::OnStart()方法中先調(diào)用基類Ability::OnStart()方法走完上述時序圖的流程,然后調(diào)用如下代碼段,創(chuàng)建AceWindowListener,并調(diào)用WindowImpl::SetInputEventConsumer()注冊輸入事件回調(diào)。
OHOS::sptr<OHOS::Rosen::Window> window = Ability::GetWindow();
std::shared_ptr<AceAbility> self = std::static_pointer_cast<AceAbility>(shared_from_this());
OHOS::sptr<AceWindowListener> aceWindowListener = new AceWindowListener(self);
// register surface change callback and window mode change callback
window->RegisterWindowChangeListener(aceWindowListener);
// register drag event callback
window->RegisterDragListener(aceWindowListener);
// register Occupied Area callback
window->RegisterOccupiedAreaChangeListener(aceWindowListener);
// register ace ability handler callback
window->SetAceAbilityHandler(aceWindowListener);
// register input consumer callback
std::shared_ptr<AceWindowListener> aceInputConsumer = std::make_shared<AceWindowListener>(self);
window->SetInputEventConsumer(aceInputConsumer);
3、多模輸入服務(wù)
(1)多模服務(wù)初始化流程

說明:
- MMIService::OnThread()中會起循環(huán),等待并處理epoll事件。接收到libinput相關(guān)的epoll事件后,調(diào)用LibinputAdapter::EventDispatch()處理input事件。
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_.RetriggerHotplugEvents();
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<mmi_epoll_event*>(ev[i].data.ptr);
CHKPC(mmiEd);
if (mmiEd->event_type == EPOLL_EVENT_INPUT) {
libinputAdapter_.EventDispatch(ev[i]);//處理input事件
} 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);
}
- InputEventHandler::BuildInputHandlerChain()會創(chuàng)建IInputEventHandler對象鏈,用于處理libinput上報的input事件。類圖如下:
- InputEventHandler::OnEvent(void event)調(diào)用。
EventNormalizeHandler::HandleEvent(libinput_event event)開始按順序處理輸入事件。 - EventNormalizeHandler把libinput_event標準化成各種InputEvent(KeyEvent,PointerEvent,AxisEvent),并傳遞給下一級 EventFilterHandler處理。
- EventFilterHandler會過濾一些事件,否則繼續(xù)往下傳遞。
- EventInterceptorHandler事件攔截器,攔截成功不會繼續(xù)往下傳。
- KeyCommandHandler根據(jù)配置文件,對一些特殊按鍵,拉起特定應(yīng)用界面,或者對電源鍵,音量鍵做特殊處理,否則繼續(xù)往下傳遞。
- KeySubscriberHandler應(yīng)用訂閱的組合按鍵(應(yīng)用通過inputConsumer.on接口訂閱)處理,否則繼續(xù)往下傳遞。
- EventMonitorHandler事件跟蹤器,把事件分發(fā)給跟蹤者并繼續(xù)往下傳。
- EventDispatchHandler通過socket把事件派發(fā)給應(yīng)用。
4、多模輸入touch事件派發(fā)流程

說明:
MMIService收到libinput上報的input事件后,會調(diào)用InputEventHandler::OnEvent來處理輸入事件。最終EventDispatchHandler通過socket把事件派發(fā)給目標應(yīng)用進程。
5、如何確定輸入事件派發(fā)的目標進程?
多模服務(wù)端InputWindowsManager類中有如下成員變量。
DisplayGroupInfo displayGroupInfo_;
std::map<int32_t, WindowInfo> touchItemDownInfos_;
DisplayGroupInfo中包含了當前獲焦的窗口id,以z軸排序的窗口信息列表,物理屏幕信息列表等。displayGroupInfo_信息由窗口管理服務(wù)調(diào)用。
MMI::InputManager::GetInstance()->UpdateDisplayInfo(displayGroupInfo_)接口設(shè)置。
struct DisplayGroupInfo {
int32_t width; //Width of the logical display
int32_t height; //Height of the logical display
int32_t focusWindowId; //ID of the focus window
//List of window information of the logical display arranged in Z order, with the top window at the top
std::vector<WindowInfo> windowsInfo;
std::vector<DisplayInfo> displaysInfo; //Physical screen information list
};
以鍵盤按鍵事件為例。
收到libinput上報的輸入事件之后,最終走到EventDispatchHandler::DispatchKeyEventPid(UDSServer& udsServer, std::shared_ptr<KeyEvent> key)函數(shù)。
簡化的調(diào)用流程如下:
EventDispatchHandler::DispatchKeyEventPid() =>
InputWindowsManager::UpdateTarget() =>
InputWindowsManager::GetPidAndUpdateTarget()
int32_t InputWindowsManager::GetPidAndUpdateTarget(std::shared_ptr<InputEvent> inputEvent)
{
CALL_DEBUG_ENTER;
CHKPR(inputEvent, INVALID_PID);
const int32_t focusWindowId = displayGroupInfo_.focusWindowId;
WindowInfo* windowInfo = nullptr;
for (auto &item : displayGroupInfo_.windowsInfo) {
if (item.id == focusWindowId) {
windowInfo = &item;
break;
}
}
CHKPR(windowInfo, INVALID_PID);
inputEvent->SetTargetWindowId(windowInfo->id);
inputEvent->SetAgentWindowId(windowInfo->agentWindowId);
MMI_HILOGD("focusWindowId:%{public}d, pid:%{public}d", focusWindowId, windowInfo->pid);
return windowInfo->pid;
}
InputWindowsManager::GetPidAndUpdateTarget()函數(shù)中把當前獲焦windowId信息設(shè)置到InputEvent中,并且返回目標窗口所在進程pid,有了目標進程pid,就可以獲取到目標進程對應(yīng)的socket會話的服務(wù)端fd,把事件派發(fā)給目標進程。
touch事件目標窗口信息的獲取和按鍵事件不同,感興趣的可以自己查看代碼。
6、總結(jié)
本篇文章基于社區(qū)weekly_20230207的代碼,對多模輸入客戶端注冊監(jiān)聽流程和多模服務(wù)端事件派發(fā)流程作了簡單介紹。相信大家通過本文,對多模輸入子系統(tǒng)能有一個大致了解。
??想了解更多關(guān)于開源的內(nèi)容,請訪問:??
??51CTO 開源基礎(chǔ)軟件社區(qū)??
??https://ost.51cto.com??