Author: Zhing, Tencent Mobile client development engineer
Commercial reprint please contact Tencent Wetest authorized, Non-commercial reprint please indicate the source.
original link: http://wetest.qq.com/lab/view/349.html wetest Guide
When we write a program with a UI, if you want to get the input event, just write a callback function, such as (Onkeyevent,ontouchevent ...), the input event may come from the button, from the touch, also from the keyboard, in fact, the soft keyboard is also a separate input event. So why do I get these input events through the callback function? How does the system accurately let the program get input events and respond to it? Why does the system have an interface to get touch events at the same time? Here's an analysis of the Android system input subsystem to answer these questions. First, the forwarding process of the input events
second, the physical device is how to send input data to the kernel
The physical device sends the data to the kernel for transmission through the device, and there are several device files in the/dev/input/directory under Linux, Event0,event1,event2 ... These device files are actually driven to create, they share a main device number, only the secondary device number is different, indicating that this is a class of devices. For example, touch screen corresponding to event0, touch screen driver is mounted, the driver will be initialized, mainly to initialize the CPU pin, set the interrupt handler.
Well understood, the touchscreen is a physical device, but our driver runs on the CPU, this is two different devices, their physical connection through the wire will be the corresponding pins connected, but the wire in the PCB board is very small, the driver is the initialization of the CPU with the touch screen connection pins, But each pin corresponds to a register, which is detailed in the CPU's chip manual (datasheet).
When the touch screen is pressed, the touch screen has a lower pin level, the connected CPU pins check the pin voltage of this connection to be lower, then will trigger the interrupt, this is initialized in the touch drive, the CPU has a interrupt vector table, here is to our driver to write good interrupt handler function, The interrupt handler reads the touch-screen data, which is the binary data of the connected pins, such as (01011010), when our kernel gets the touch-screen data.
Touch screen chip sequence diagram III, the kernel is how to send input data to the user space Android framework
After the kernel gets the touch screen data, smoothing, filtering, data or kernel space, how does Android get the touch data? Android is actually running a set of processes on the Linux kernel, a combination of processes that provide users with the UI, the installation of applications, and so on.
The phone boot process is the Linux kernel starts first, after the startup completes will launch the Android process group, the framework belongs to this process group. Framewok has a service inputmanagerservice, we look at the Android source where it is instantiated:
Systemserver.java----------->
startotherservices ()------>/
* construction inputmanagerservice*/
InputManager = new Inputmanagerservice (context);
/* will InputManager pass to Windowmanagerservice to
wm=windowmanagerservice.main (context, InputManager,
Mfactorytestmode!=factorytest.factory_test_low_leve!mfirstboot, monlycore);
/* To Inputmanagerservice Set callback
/Inputmanager.setwindowmanagercallbacks (Wm.getinputmonitor ());
/* Fully initialized, Systemserver calls the start () function to allow two threads in InputManager to start running. First look at the Inputreaderthread, it is the event in the user state process of the start
/Inputmanager.start ();
so you can see it instantiated and started in the systemserver process, so first we need to look at what the Inputmanagerservice constructor does.
The constructor invokes the C + + object to which JNI creates Nativeinputmanager, and the Nativeinputmanager constructor creates
Sp Eventhub = new Eventhub ()
Minputmanager = new InputManager (eventhub,this,this);
the Eventhub object constructor does the following things:
Create the Epoll object, and then you can add the fd of each input device to the multiple wait for input event
Using inotify mechanism to monitor the changes under the/dev/input directory, if there is a means of equipment transformation, need to deal with, input device to add or subtract delete operation of the monitor, will represent INotify FD added to Epoll
Creates a pipe that can only be used to communicate between two of the public ancestors. Read-side Add Epoll
the InputManager object constructor does the following things:
Create Inputdispatcher
Create Inputreader (Eventhub,inputdispatcher), inputdispatcher inherit Inputlistenerinterface
Create Inputreaderthread
Create Inputdispatcherthread
We remember the most systemserver.java of the last through Inputmanager.start (); To run our inputmanagerservice, so keep looking at the start method, in fact, in the InputManager object in the native layer, The two threads created above are Inputreaderthread and Inputdispatcherthread in the Start method.
for the Inputreaderthread start method: invokes the Eventhub getevents method stored in the constructor to obtain the input event, what is done in the GetEvent method
1 Determine if you need to open the input device driver, if you need to turn on device drivers, scan the/dev/input directory of equipment files and open these devices, but also to determine whether the device list has a virtual keyboard, no words to create a device add in
2 to the next step, there are at least two input devices in the system, one is a touch screen, the other is a virtual keyboard, because the above getevent call needs to open the device, all of these actions are encapsulated into rawevent events, here are two device_added events +finish_ Device_scan events, returning these events, will not go down
3 If the second entry into the GetEvents method will wait for the read input event to send the Read touch event back
Here we know how the touch input data of kernel space is passed into the Android framework of user space, actually through the/dev/input directory, to scan the directory, if there is device to open this device, And added to the Epoll object, multiple waits for input events, and gets the data in the loop. Iv. How the Android framework sends input data to the app process
The Android framework acquires data for touch input, but there are so many processes in the system that many processes are getting input, how it is further processed, and exactly how the events are distributed.
The second thing to do in the Inputreaderthread Start method:
Call the Processeventslocked method to handle the rawevent returned by the GetEvents method above
1 according to the type of rawevent different, call different methods of processing, there are ordinary touch events
. Adding devices to Events
. Deleting an event for a device
. Finished_device_scan
2 for Touch events: A process method that invokes the input device (the InputDevice created between the touch events), which internally invokes the internal Inputmapper process method, and an input device has many mapper, Iterate through all the mapper and call the process, assuming that we are a touchscreen screen that supports multi-touch, its mapper is Multitouchinputmapper, and its process method is called.
3 The Multitouchinputmapper Process method internally handles this:
First, each time a touchevent gets the slot, the corresponding slot is the same before the Ev_syn is received, and then the x,y,pressure,touch_major is processed, and the values initialize the slot variables;
When received ev.type== Ev_syn and Ev.code = Syn_mt_report then the current slot index plus 1, the next touch event to record, while the sync function handle this touch event;
Then Currentcookedpointerdata and Lastcookedpointerdata do some column operations, Up,down or move events, then corresponding different events, call Dispatchmotion, Internal call Inputdispatcher's notifymotion
4) for the Inputdispatcher notifymotion:
If Inputdispatcher set Inputfilter, then first invoke Inputfilter to consume these events
If there is no inputfiler, or inputfilter is not interested in these events, a motionentry is constructed, added to the Minboundqueue, and the Inputdispatcher threading is awakened
5 for Inputdispatcher thread-handling loops:
Optimize app switching latency, when switching timeout, preempt distribution, discard all other events that are about to be processed;
Distributing events:
First call findtouchedwindowtagetslocked to look for the window windows with focus, and save the creation in the inputtargets array;
The Inputchannel of the previously registered monitor will also be added to the inputtargets array;
The event is then distributed to inputtargets array one by one.
Here we know how to find the app process. v. How the app process sends input data to its corresponding activity
An activity is a basic component of a process that can be thought of as representing an interface, a collection of view, and what to do every time the activity starts.
1, in fact, depending on what the Viewrootimpl did behind it, in the Setview method in Viewrootimpl.java, instantiating Inputchannel, of course, determines whether the current window can accept input events, The Addtodisplay method that is called to Session.java is then passed to the Windowmanagerservice, which is actually invoking the AddWindow method of the Windowmanagerservice, A pair of inputchannel[will be created in the Windowmanagerservice and then inputchannel[1] transferred to this inputchannel, The Setview method then continues to create a Windowinputeventreceiver object and then creates a good inputchannel
2, Windowmanagerservice in the AddWindow method:
inputchannel[] Inputchannels = Inputchannel.openinputchannelpair (name)
/*channel[0] saved on server side * *
Win.setinputchannel[inputchannels[0]]
//* Channel[1] returns to the Viewrootimpl end *
/Inputchannels[1].transferto ( Outinputchannel)
* * Registered to Inputmanagerservice
Minputmanager.registerinputchannel (Win.minputchannel,win.minputwindowhandle)
Here we can see how to distribute time to the corresponding activity, in fact, to the viewrootimpl behind it. The activity is how to send the input data to the specific view
The final step is to distribute the event to specific view in the activity, and to distribute the event to a specific view from Viewrootimpl, because the scope of the touch is known here, and the position and status of each view is also known here, Because the view is properly rendered, the Android graphics framework will take care of it, measure the size of each view, determine the location of each view, and distribute the data to each view in one layer at a viewrootimpl level. But each view knows for itself whether the touch event is acting on itself, and if not, discard it and distribute it below. Summary
The distribution process for touch events may seem complicated, but Android is still very elegant, and we're going to analyze its process, which is helpful for us to achieve some of the cooler features. Of course for us to debug the code will also help, when the discovery of touch, the system does not respond to the above process decomposition, always can analyze the reasons.
Tencent Wetest provides thousands of real-world mobile phones , testing anytime, anywhere, to ensure the quality of applications/hand travel. Saves millions of hardware costs and accelerates the agile development process.
at the same time, Tencent Wetest compatibility testing team has accumulated more than 10 years of hand travel testing experience, aimed at through the development of targeted testing programs, accurate selection of target models, the implementation of professional, complete test cases, to find out the compatibility of the game version in advance, targeted to make corrections and optimization, To ensure the quality of hand travel products. At present, the team has supported all Tencent in the research and operation of the hand-travel program.
Welcome to enter: Http://wetest.qq.com/product/cloudphone experience An Zuojin machine
Welcome to: http://wetest.qq.com/product/expert-compatibility-testing Use the expert Compatibility test service. the Wetest Compatibility Test team is looking forward to communicating with you. You create,we Test.
If you have any questions about use, please contact Tencent Wetest Enterprise qq:800024531