Previous Linux/android--input system kernel layer and frameworks layer interaction (v) is introduced kernel layer and the Android side frameworks layer between the connection, is to get through the Android application layer and The kernel drive layer is critical to the entire input system, where the frameworks layer simply records several access points and begins to analyze the details of the frameworks layer.
Writing is not easy, reprint need to indicate the source: http://blog.csdn.net/jscese/article/details/42392311
Start of the input service:
Android startup will start a lot of service, this can refer to Systemserver.java, will start inputmanagerservice this service:
SLOG.I (TAG, "Input Manager"); InputManager = new Inputmanagerservice (context, wmhandler); .... Servicemanager.addservice (Context.input_service, InputManager);
Look directly at the start function in Inputmanagerservice.java:
public void Start () { slog.i (TAG, "starting input manager"); Nativestart (mptr); The local method is called, and the JNI corresponding CPP is under the JNI directory under the server ...}
The JNI of the server involved in Android, the first is to load android_server this dynamic library in Systemserver,
As for the compilation of this dynamic library, refer to the content in/frameworks/base/services/jni/android.mk
So when this nativestart method is called, the associated dynamic library is already loaded into the systemserver process.
First look at the implementation of the start function in the Jni file, Frameworks/base/services/jni/com_android_server_input_inputmanagerservice.cpp:
static void Nativestart (jnienv* env, Jclass clazz, Jint ptr) { nativeinputmanager* im = REINTERPRET_CAST<NATIVEINP Utmanager*> (PTR); This nativeinputmanager is the status_t result = Im->getinputmanager when calling Nativeinit when Inputmangerservice is constructed above ()->start (); This is called the Start method in the InputManager in Nativeinputmanager, and the same inputmanager is the if of the new when the Nativeinputmanager is constructed (result { jnithrowruntimeexception (env, "Input manager could not being started.");} }
Actually familiar with the JNI, I analyzed here, it should be almost. For JNI, you can refer to my previous blog: Andorid--ubuntu under the Ndk/jni
Look at the following in the Nativeinputmanager constructor:
sp<eventhub> Eventhub = new Eventhub (); Minputmanager = new InputManager (Eventhub, this, this);
The JNI section here is not much to say, now look at this InputManager start method, mentioned above to Android.mk, you can see include a libinput dynamic library,
And the path to this dynamic library is under/frameworks/base/services/input, which is clear. There are InputManager.cpp in this directory. EventHub.cpp, etc.
See Start directly:
status_t Inputmanager::start () { status_t result = Mdispatcherthread->run ("Inputdispatcher", PRIORITY_URGENT_ DISPLAY); This mdispatcherthread is called initialize initialization in the InputManager constructor, and it is very obvious that the thread named Inputdispatcher was launched if (result) { Aloge ("Could Not start Inputdispatcher thread due to error%d.", result); return result; } result = Mreaderthread->run ("Inputreader", Priority_urgent_display); As above, turn on thread if (result) { aloge ("Could Not start Inputreader thread due to error%d.", result); Mdispatcherthread->requestexit (); return result; } return OK;}
here is the feeling of seeing the true face, look at these two threads, literally, one is to distribute the input event to the current activity, and one is to read the input from the lower
Inputdispatcher Distribution:
Start profiling from the above thread:
BOOL Inputdispatcherthread::threadloop () { mdispatcher->dispatchonce (); return true;}
Call a function that is distributed once:
void Inputdispatcher::d ispatchonce () {nsecs_t nextwakeuptime = Long_long_max; {//Acquire lock Automutex _l (MLock); Mdispatcherisalivecondition.broadcast (); Run a dispatch loop if there is no pending commands. The dispatch loop might enqueue commands to run afterwards. if (!havecommandslocked ()) {///If there is a cached command to invoke the following RunCommand to execute, there is no word here to check whether there is a new input event, which defines a wake-up time control DISPATC Honceinnerlocked (&nextwakeuptime); }//Run All pending commands if there is any. If any commands were run then force the next poll to wake up immediately. if (runcommandslockedinterruptible ()) {nextwakeuptime = Long_long_min; }}//Release lock//Wait for callback or timeout or wake. (Make sure we round up, not down) nsecs_t currenttime = Now (); int timeoutmillis = Tomillisecondtimeoutdelay (currenttime, nextwakeuptime); Mlooper->pollonce (Timeoutmillis); After performing the previous distribution, it enters loop,This loop will continue to detect if the corresponding pipeline has content readable, and another thread will write to the Inputreader after reading the input event}
This is the processing of input events, subsequent analysis, the first to see how to read the event.
Inputreader read:
Source code is located in Frameworks/base/libs/ui/inputreader.cpp, open thread as follows:
BOOL Inputreaderthread::threadloop () { mreader->looponce (); return true;}
Here's a look at this looponce:
void Inputreader::looponce () {... size_t count = meventhub->getevents (Timeoutmillis, Meventbuffer, event_buffer_size); Here to get event events ... if (count) { processeventslocked (Meventbuffer, count);//If Get to event, Process } ...}
You can see that this is where the event was obtained. Mentioned in the previous article!
GetEvent will always read until the get to event, through precessevent processing, will eventually wake up the Inputdispatcherthread described above, notify it that there are new events
Frameworks layer of Linux/android--input system Inputmanagerservice (vi)