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

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

說(shuō)明:
- Ability生命周期函數(shù)OnStart()中會(huì)去創(chuàng)建WindowImpl實(shí)例,WindowImpl::Create()中調(diào)用InputTransferStation::AddInputWindow()創(chuàng)建InputEventListener并注冊(cè)到InputManagerImpl中。后續(xù)收到多模服務(wù)端發(fā)送來(lái)的輸入事件之后會(huì)通過(guò)回調(diào)InputEventListener的接口函數(shù),把事件上報(bào)到窗口管理,窗口管理再把事件進(jìn)一步上報(bào)給ArkUI。
- InputManagerImpl::SetWindowInputEventConsumer()方法中會(huì)去初始化多模Socket客戶端,用于接收多模服務(wù)端發(fā)來(lái)的輸入事件。
(2)ArkUI何時(shí)注冊(cè)的窗口管理輸入事件回調(diào)?
AceAbility::OnStart()方法中先調(diào)用基類Ability::OnStart()方法走完上述時(shí)序圖的流程,然后調(diào)用如下代碼段,創(chuàng)建AceWindowListener,并調(diào)用WindowImpl::SetInputEventConsumer()注冊(cè)輸入事件回調(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ù)初始化流程

說(shuō)明:
- MMIService::OnThread()中會(huì)起循環(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()會(huì)創(chuàng)建IInputEventHandler對(duì)象鏈,用于處理libinput上報(bào)的input事件。類圖如下:
- InputEventHandler::OnEvent(void event)調(diào)用。
EventNormalizeHandler::HandleEvent(libinput_event event)開始按順序處理輸入事件。 - EventNormalizeHandler把libinput_event標(biāo)準(zhǔn)化成各種InputEvent(KeyEvent,PointerEvent,AxisEvent),并傳遞給下一級(jí) EventFilterHandler處理。
- EventFilterHandler會(huì)過(guò)濾一些事件,否則繼續(xù)往下傳遞。
- EventInterceptorHandler事件攔截器,攔截成功不會(huì)繼續(xù)往下傳。
- KeyCommandHandler根據(jù)配置文件,對(duì)一些特殊按鍵,拉起特定應(yīng)用界面,或者對(duì)電源鍵,音量鍵做特殊處理,否則繼續(xù)往下傳遞。
- KeySubscriberHandler應(yīng)用訂閱的組合按鍵(應(yīng)用通過(guò)inputConsumer.on接口訂閱)處理,否則繼續(xù)往下傳遞。
- EventMonitorHandler事件跟蹤器,把事件分發(fā)給跟蹤者并繼續(xù)往下傳。
- EventDispatchHandler通過(guò)socket把事件派發(fā)給應(yīng)用。
4、多模輸入touch事件派發(fā)流程

說(shuō)明:
MMIService收到libinput上報(bào)的input事件后,會(huì)調(diào)用InputEventHandler::OnEvent來(lái)處理輸入事件。最終EventDispatchHandler通過(guò)socket把事件派發(fā)給目標(biāo)應(yīng)用進(jìn)程。
5、如何確定輸入事件派發(fā)的目標(biāo)進(jìn)程?
多模服務(wù)端InputWindowsManager類中有如下成員變量。
DisplayGroupInfo displayGroupInfo_;
std::map<int32_t, WindowInfo> touchItemDownInfos_;
DisplayGroupInfo中包含了當(dāng)前獲焦的窗口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上報(bào)的輸入事件之后,最終走到EventDispatchHandler::DispatchKeyEventPid(UDSServer& udsServer, std::shared_ptr<KeyEvent> key)函數(shù)。
簡(jiǎn)化的調(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ù)中把當(dāng)前獲焦windowId信息設(shè)置到InputEvent中,并且返回目標(biāo)窗口所在進(jìn)程pid,有了目標(biāo)進(jìn)程pid,就可以獲取到目標(biāo)進(jìn)程對(duì)應(yīng)的socket會(huì)話的服務(wù)端fd,把事件派發(fā)給目標(biāo)進(jìn)程。
touch事件目標(biāo)窗口信息的獲取和按鍵事件不同,感興趣的可以自己查看代碼。
6、總結(jié)
本篇文章基于社區(qū)weekly_20230207的代碼,對(duì)多模輸入客戶端注冊(cè)監(jiān)聽流程和多模服務(wù)端事件派發(fā)流程作了簡(jiǎn)單介紹。相信大家通過(guò)本文,對(duì)多模輸入子系統(tǒng)能有一個(gè)大致了解。
??想了解更多關(guān)于開源的內(nèi)容,請(qǐng)?jiān)L問(wèn):??
??51CTO 開源基礎(chǔ)軟件社區(qū)??
??https://ost.51cto.com??