Frameworks/base/libs/UI/eventhub. cpp
After pressing the power key, the system writes the scancode to the corresponding device contact, frameworks/base/libs/UI/eventhub. CPP reads the device node and passes the scancode. the KL file is sent as a keycode to the upper layer.
Framework/base/services/Java/COM/Android/Server/keyinputqueue. Java calls framework/base/services/JNI/com_android_server_keyinputqueue.cpp through JNI to call the files in eventhub. cpp.
Keyinputqueue. Java enters the endless loop and executes readevent first. Call getevent in eventhub. cpp to wait for the input.
Eventhub. getevent in CPP enters the endless loop and executes pollres = poll (MFDs, mfdcount,-1) to wait for data to be written to the device node (that is, a key is pressed ), when data is written, execute the following for loop to find out which Fd has content written and read the written data res = read (MFDs [I]. FD, & IEV, sizeof (IEV )). Only type and scancode () are read here, without keycode. The hardware layer can only write scancode to the device file, and the keycode is used by the upper layer, which is obtained by map. Next, use err = mdevices [I]-> layoutmap-> map (IEV. Code, outkeycode, outflags) to map the keycode and flags corresponding to scancode. MPA is implemented in, and the two values are found in m_keys according to scancode. M_keys is in keylayoutmap. when the CPP file is loaded, read the KL (usually in the/system/usr/keylayout folder of the mobile phone) file, parse the keycode and flag corresponding to all scancodes and add them to m_keys. Macro-like characters in the KL file, such as power, are matched in frameworks/base/include/UI/keycodelabels. h.
In the windowmanagerservice. Java file, we can find the constructor of windowmanagerservice. (As for the role of windowmanagerservice in the entire android system, I need to study the following content. I will not study it here)
Private windowmanagerservice (context, powermanagerservice pm,
Boolean haveinputmethods ){
...........................
..........................
Mqueue = new keyq ();
Minputthread = new inputdispatcherthread ();
........................................
.......................................
Minputthread. Start ();
// Add ourself to the watchdog monitors.
Watchdog. getinstance (). addmonitor (this );
}
We found that a new thread is created in this constructor. The retrieved name should be used to obtain user input. Keyq we can see that it is derived from the keyinputqueue class, continue to open keyinputqueue to view, we can see that there is also a thread in the constructor, and this thread is an endless loop, it will call the readevent () function continuously in such a loop.