Android Input事件处理
Input UML 图表
系统启动SystemServer时创建其他服务,初始话Input管理服务,创建InputManager、InputReader对象
对于InputReader在初始化成功之后,start启动时,创建InputTread loop工作线程
framework/native/services/inputflinger/reader/InputReader.cpp
InputReader.h
private:
std::unique_ptr<InputThread> mThread;
InputReader.cpp
status_t InputReader::start() {
if (mThread) {
return ALREADY_EXISTS;
}
mThread = std::make_unique<InputThread>(
"InputReader", [this]() { loopOnce(); }, [this]() { mEventHub->wake(); });
return OK;
}
如下图是系统启动input服务时序图:
如图可知,inputThread这个线程的工作就是通过EventHub的getEvents监听读取Input事件。
获取到事件Input事件之后,通知InputDispatcher派发InputEvent事件
在InputReader中loopOnce方法最后一句是flush
// Flush queued events out to the listener.
// This must happen outside of the lock because the listener could potentially call
// back into the InputReader's methods, such as getScanCodeState, or become blocked
// on another thread similarly waiting to acquire the InputReader lock thereby
// resulting in a deadlock. This situation is actually quite plausible because the
// listener is actually the input dispatcher, which calls into the window manager,
// which occasionally calls into the input reader.
mQueuedListener->flush();
如下图是flush动作的时序图