原文网址:http://www.cnblogs.com/cnhome/archive/2009/12/22/1629435.html
?
按键事件 对于按键事件,调用mDevices->layoutMap->map进行映射。映射实际是由 KeyLayoutMap::map完成的,KeyLayoutMap类里读取配置文件qwerty.kl,由配置 文件 qwerty.kl 决定键值的映射关系。你可以通过修 改./development/emulator/keymaps/qwerty.kl来改变键值的映射关系。? ?readEvent调用hub->getEvent读了取事件,然后转换成JAVA的结构。? Thread mThread = new Thread("InputDeviceReader") { ??????????????????? // block, doesn't release the monitor ??????????????????? boolean send = false; ? 按键、触摸屏流、轨迹球程分析 输入事件分发线程? ??? WindowManagerService() ??? mQueue = new KeyQ();? private class KeyQ extends KeyInputQueue implements KeyInputQueue.FilterCallback KeyQ 是抽象类 KeyInputQueue 的实现,所以 new KeyQ类的时候实际上在 KeyInputQueue 类中创建了一个线程 InputDeviceReader 专门用来从设备读取按键事件, 代码:? ? readEvent() 实际上调用的是 com_android_server_KeyInputQueue.cpp (frameworks\base\services\jni)中的 static jboolean android_server_KeyInputQueue_readEvent(JNIEnv* env, jobject clazz,jobject event) 来读取事件, ? bool res = hub->getEvent(&deviceId, &type, &scancode, &keycode,&flags, &value, &when)调用的是EventHub.cpp (frameworks\base\libs\ui)中的: ??? mInputThread.start();? ? case RawInputEvent.CLASS_TRACKBALL: ? =============================================================== 补充一些内容: 在写程序时,需要捕获KEYCODE_HOME、KEYCODE_ENDCALL、KEYCODE_POWER这几个按键,但是这几个按键系统做了特殊处理, 在进行dispatch之前做了一些操作,HOME除了Keygaurd之外,不分发给任何其他APP,ENDCALL和POWER也类似,所以需要我们系统 处理之前进行处理。 我的做法是自己定义一个FLAG,在自己的程序中添加此FLAG,然后在WindowManagerServices.java中获取当前窗口的FLAG属性,如果是我 们自己设置的那个FLAG,则不进行特殊处理,直接分发按键消息到我们的APP当中,由APP自己处理。 这部分代码最好添加在 @Override 方法中,这个方法是KeyInputQueue中的一个虚函数,在处理按键事件之前的一个“预处理”。 PS:对HOME键的处理好像必需要修改PhoneWindowManager.java中的interceptKeyTi方法,具体可以参考对KeyGuard程序的处理。 ? =============================================================== ? 系统底层事件处理过程 ??? bool EventHub::penPlatformInput(void) ??? res = scan_dir(device_path);? ? 打开设备的时候,如果 device->classes&CLASS_KEYBOARD 不等于 0 表明是键盘。? ????????CLASS_ALPHAKEY? ?? ?= 0x00000002, //? ????????CLASS_TOUCHSCREEN? ?= 0x00000004, //触摸屏? ????????CLASS_TRACKBALL? ???= 0x00000008 //轨迹球? ? 如果没有定义键盘映射文件,那么默认使用系统的 /system/usr/keylayout/qwerty.kl 可以修改 /system/usr/keylayout/qwerty.kl 文件改变Android公司的按键映射。? ??? status_t KeyLayoutMap::load(const char* filename)通过解析 pxa27x-keypad.kl?
JNI 函数?
在frameworks/base/services/jni/com_android_server_KeyInputQueue.cpp文 件中,向 JAVA提供了函数android_server_KeyInputQueue_readEvent,用于读 取输入设备事件。?
C代码:?
???????????????????????????????????????????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(when)));?
????return?res;?
}
事件中转线程?
在frameworks/base/services/java/com/android/server/KeyInputQueue.java 里创建了一个线程,它循环的读取事件,然后把事件放入事件队列里。?
Java代码:
??????? 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);
??????????????????? boolean configChanged = false;
????????????????????
??????????????????? if (false) {
??????????????????????? Log.i(TAG, "Input event: dev=0x"
??????????????????????????????? + Integer.toHexString(ev.deviceId)
??????????????????????????????? + " type=0x" + Integer.toHexString(ev.type)
??????????????????????????????? + " scancode=" + ev.scancode
??????????????????????????????? + " keycode=" + ev.keycode
??????????????????????????????? + " value=" + ev.value);
??????????????????? }
????????????????????
??????????????????? if (ev.type == RawInputEvent.EV_DEVICE_ADDED) {
??????????????????????? synchronized (mFirst) {
??????????????????????????? di = newInputDevice(ev.deviceId);
??????????????????????????? mDevices.put(ev.deviceId, di);
??????????????????????????? configChanged = true;
??????????????????????? }
??????????????????? }
在frameworks/base/services/java/com/android/server/WindowManagerService.java里创建了一个输入事件分发线程,它负责把事件分发到相应的窗口上去。
按键触摸屏流程分析:?
??? WindowManagerService类的构造函数
因为 WindowManagerService.java (frameworks\base\services\java\com\android\server)中有:
????public?void?run()?{?
????????// 在循环中调用:
????????...?
????????send?=?preprocessEvent(di,?ev);?
?????????...?
????????int?keycode?=?rotateKeyCodeLocked(ev.keycode);?
????????for?(int?i=0;?i<N;?i+=2)?{?
????????????if?(map?==?keyCode)??
????????????return?map[i+1];?
????????}?//?
????????addLocked(di,?curTime,?ev.flags,RawInputEvent.CLASS_KEYBOARD,
??????????????????newKeyEvent(di,?di.mDownTime,?curTime,?down,keycode,?0,?scancode,...));?
????????QueuedEvent?ev?=?obtainLocked(device,?when,?flags,?classType,?event);?
????}?
};?
??? bool EventHub::getEvent (int32_t* outDeviceId, int32_t* outType,?
? ?? ?????? int32_t* outScancode, int32_t* outKeycode, uint32_t *outFlags,?
? ?? ?????? int32_t* outValue, nsecs_t* outWhen)?
在函数中调用了读设备操作:res = read(mFDs.fd, &iev, sizeof(iev));?
在构造函数 WindowManagerService()调用 new KeyQ() 以后接着调用了:?
??? mInputThread = new InputDispatcherThread();???????
??? ...?????
来启动一个线程 InputDispatcherThread?
??? run()?
??? process();?
??? QueuedEvent ev = mQueue.getEvent(...)?
因为WindowManagerService类中: final KeyQ mQueue;?
所以实际上 InputDispatcherThread 线程实际上从 KeyQ 的事件队列中读取按键事件,在process() 方法中进行处理事件。
??? switch (ev.classType)?
??? case RawInputEvent.CLASS_KEYBOARD:?
??? ??? ...?
??? ??? dispatchKey((KeyEvent)ev.event, 0, 0);?
??? ??? mQueue.recycleEvent(ev);?
??? ??? break;?
??? case RawInputEvent.CLASS_TOUCHSCREEN:?
??? ??? //Log.i(TAG, "Read next event " + ev);?
??? ??? dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);?
??? ??? break;
??????? dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
??????? break;
boolean preprocessEvent(InputDevice device, RawInputEvent event)
在系统启动后,android 会通过?
??? static const char *device_path = "/dev/input";
通过下面的函数打开设备。?
{?
????...?
????fd?=?open(deviceName,?O_RDWR);?
????...??
????mFDs[mFDCount].fd?=?fd;?
????mFDs[mFDCount].events?=?POLLIN;?
????...?
????ioctl(mFDs[mFDCount].fd,?EVIOCGNAME(sizeof(devname)-1),?devname);?
????...?
????const?char*?root?=?getenv("ANDROID_ROOT");?
????snprintf(keylayoutFilename,?sizeof(keylayoutFilename),?
?????????????????"%s/usr/keylayout/%s.kl",?root,?tmpfn);?
????...?
????device->layoutMap->load(keylayoutFilename);?
????...?
}?
常用输入设备的定义有:?
enum {?
????????CLASS_KEYBOARD? ?? ?= 0x00000001, //键盘?
};?
打开键盘设备的时候通过上面的 ioctl 获得设备名称,命令字 EVIOCGNAME 的定义在文件:?
kernel/include/linux/input.h 中。?
#define EVIOCGNAME(len)? ?_IOC(_IOC_READ, 'E', 0x06, len) /* get device name */?
在内核键盘驱动文件 drivers/input/keyboard/pxa27x_keypad.c 中定义了设备名称:pxa27x-keypad?
static struct platform_driver pxa27x_keypad_driver = {?
? ? .probe? ?? ???= pxa27x_keypad_probe,?
? ? .remove? ?? ???= __devexit_p(pxa27x_keypad_remove),?
? ? .suspend? ? = pxa27x_keypad_suspend,?
? ? .resume? ?? ???= pxa27x_keypad_resume,?
? ? .driver? ?? ???= {?
? ?? ???.name? ? = "pxa27x-keypad",?
? ?? ???.owner? ? = THIS_MODULE,?
? ? },?
};?
ANDROID_ROOT 为环境变量,在android的命令模式下通过 printenv 可以知道它为: system?
所以 keylayoutFilename 为:/system/usr/keylayout/pxa27x-keypad.kl?
pxa27x-keypad.kl 定义了按键映射,具体内容如下:
key?2???1?
key?3???2?
key?4???3?
key?5???4?
key?6???5?
key?7???6?
key?8???7?
key?9???8?
key?10?9?
key?11?0?
key?83?POUND?
key?55?STAR?
#?FUNCTIONAL?KEYS?
key?231?MENU????????WAKE_DROPPED?
key?192?BACK???????????WAKE_DROPPED?
key?193?HOME???????WAKE?
key?107?DEL????????WAKE?
key?102?CALL????????WAKE_DROPPED?
key?158?ENDCALL?????WAKE_DROPPED?
key?28???DPAD_CENTER?????WAKE?
key?115?VOLUME_UP?
key?114?VOLUME_DOWN?
device->layoutMap->load(keylayoutFilename) 调用的是文件 KeyLayoutMap.cpp (frameworks\base\libs\ui)中的函数:
把按键的映射关系保存在 :KeyedVector<int32_t,Key> m_keys; 中。?
当获得按键事件以后调用:?
status_t KeyLayoutMap::map(int32_t scancode, int32_t *keycode, uint32_t *flags)?
由映射关系 KeyedVector<int32_t,Key> m_keys 把扫描码转换成andorid上层可以识别的按键。