This example describes the Android programming input event process. Share to everyone for your reference, specific as follows:
The input device is encapsulated by the Eventhub. The input device driver provides some device files for the user space application, which is placed inside the/dev/input.
Eventhub scans all device files under/dev/input and opens them.
BOOL Eventhub::openplatforminput (void)
{
...
Mfdcount = 1;
MfDs = (POLLFD *) calloc (1, sizeof (mfds[0));
Mdevices = (device_t * *) calloc (1, sizeof (mdevices[0));
mfds[0].events = Pollin;
Mdevices[0] = NULL;
res = Scan_dir (device_path);
...
return true;
}
Eventhub provides a function to read data from an input device file.
BOOL Eventhub::getevent (int32_t* Outdeviceid, int32_t* outtype, int32_t* outscancode, int32_t* outKeycode, uint32_t * Outflags, int32_t* Outvalue, nsecs_t* outwhen) {... while (1) {//I, the, the
Ad last been added/removed.
if (mclosingdevices!= NULL) {device_t* device = mclosingdevices;
LOGV ("Reporting device closed:id=0x%x, name=%s\n", Device->id, Device->path.string ());
Mclosingdevices = device->next;
*outdeviceid = device->id;
if (*outdeviceid = = mfirstkeyboardid) *outdeviceid = 0;
*outtype = device_removed;
Delete device;
return true;
} if (mopeningdevices!= NULL) {device_t* device = mopeningdevices;
LOGV ("Reporting device opened:id=0x%x, name=%s\n", Device->id, Device->path.string ());
Mopeningdevices = device->next;
*outdeviceid = device->id; if (*outdeviceid = = mfirstkeyboardid) *outdeviceid = 0;
*outtype = device_added;
return true;
} release_wake_lock (wake_lock_id);
Pollres = Poll (MfDs, Mfdcount,-1);
Acquire_wake_lock (Partial_wake_lock, wake_lock_id);
if (pollres <= 0) {if (errno!= eintr) {LOGW ("Select failed (errno=%d) \ n", errno);
Usleep (100000);
} continue; for (i = 1; i < Mfdcount. i++) {if (mfds[i].revents) {LOGV ("revents for%d = 0x%08x", I, mfds[i].re
vents);
if (Mfds[i].revents & Pollin) {res = read (MFDS[I].FD, &iev, sizeof (Iev)); if (res = = sizeof (Iev)) {LOGV ("%s got:t0=%d, t1=%d, type=%d, code=%d, v=%d", mdevices[i]-> Path.string (), (int) iev.time.tv_sec, (int) iev.time.tv_usec, Iev.type, Iev.code, Iev.value)
;
*outdeviceid = mdevices[i]->id;
if (*outdeviceid = = mfirstkeyboardid) *outdeviceid = 0;
*outtype = Iev.type; *outscAncode = Iev.code;
if (Iev.type = = Ev_key) {err = Mdevices[i]->layoutmap->map (Iev.code, Outkeycode, outflags);
LOGV ("iev.code=%d outkeycode=%d outflags=0x%08x err=%d\n", Iev.code, *outkeycode, *outflags, err);
if (Err!= 0) {*outkeycode = 0;
*outflags = 0;
} else {*outkeycode = Iev.code;
} *outvalue = Iev.value;
*outwhen = S2ns (iev.time.tv_sec) + us2ns (iev.time.tv_usec);
return true;
else {if (res<0) {LOGW ("Could not get event (errno=%d)", errno);
else {LOGE ("Could not get event (wrong size:%d)", res);
} continue;
}
}
}
}
...
}
For key events, call Mdevices[i]->layoutmap->map for mapping. The mapping is actually done by Keylayoutmap::map, and the Keylayoutmap class reads the configuration file Qwerty.kl, which determines the mapping relationship between the key values by the profile qwerty.kl. You can change the mapping of the key values by modifying the./development/emulator/keymaps/qwerty.kl.
JNI functions
In the Frameworks/base/services/jni/com_android_server_keyinputqueue.cpp file, a function is provided to Java Android_server_keyinputqueue_ Readevent, which is used to read input device events.
static Jboolean Android_server_keyinputqueue_readevent (jnienv* env, Jobject Clazz, Jobject
Event) {Glock.lock ();
SP hub = Ghub;
if (hub = NULL) {hub = new Eventhub;
Ghub = hub;
} glock.unlock ();
int32_t deviceId;
int32_t type;
int32_t Scancode, KeyCode;
uint32_t flags;
int32_t value;
nsecs_t when;
BOOL res = hub->getevent (&deviceid, &type, &scancode, &keycode, &flags, &value, &when);
Env->setintfield (Event, Ginputoffsets.mdeviceid, (Jint) deviceId);
Env->setintfield (Event, Ginputoffsets.mtype, (jint) type);
Env->setintfield (Event, Ginputoffsets.mscancode, (Jint) scancode);
Env->setintfield (Event, Ginputoffsets.mkeycode, (Jint) keycode);
Env->setintfield (Event, Ginputoffsets.mflags, (jint) flags);
Env->setintfield (event, ginputoffsets.mvalue, value);
Env->setlongfield (Event, Ginputoffsets.mwhen, (Jlong) (nanoseconds_to_milliseconds));
return res; }
Readevent calls Hub->getevent read the event and then converts it into a Java structure.
Event Relay Threads
A thread is created in the Frameworks/base/services/java/com/android/server/keyinputqueue.java, which loops through the read events, and then puts the events into the event queue.
Thread mthread = new Thread ("Inputdevicereader") {public
void run () {
android.os.Process.setThreadPriority
Android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
try {
rawinputevent ev = new rawinputevent ();
while (true) {
inputdevice di;
Readevent (EV);
send = Preprocessevent (di, Ev);
Addlocked (Di, curtime, Ev.flags, ..., me);}}
}
;
Input Event Distribution Thread
An input event distribution thread was created in Frameworks/base/services/java/com/android/server/windowmanagerservice.java, which is responsible for distributing events to the appropriate windows.
Mqueue.getevent
Dispatchkey/dispatchpointer/dispatchtrackball
For more information on Android-related content readers can view the site topics: "Android Development Introduction and Advanced Course", "Android debugging techniques and common problems solution summary", "Android Multimedia operating skills Summary (audio, video, recording, etc.)", " Android Basic Components Usage Summary, Android View tips Summary, Android layout layout tips and Android Control usage summary
I hope this article will help you with the Android program.