How does the upper layer of Android receive button events?

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.