blob: 77051bd0023c8d5afedf0f51b654bf399d61ac1f [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
19import android.content.Context;
20import android.content.res.Configuration;
Mike Lockwood1d9dfc52009-07-16 11:11:18 -040021import android.os.Environment;
Michael Chan53071d62009-05-13 17:29:48 -070022import android.os.LatencyTimer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import android.os.PowerManager;
Michael Chan53071d62009-05-13 17:29:48 -070024import android.os.SystemClock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.util.Log;
26import android.util.SparseArray;
Mike Lockwood1d9dfc52009-07-16 11:11:18 -040027import android.util.Xml;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.view.Display;
29import android.view.KeyEvent;
30import android.view.MotionEvent;
31import android.view.RawInputEvent;
32import android.view.Surface;
33import android.view.WindowManagerPolicy;
34
Mike Lockwood1d9dfc52009-07-16 11:11:18 -040035import com.android.internal.util.XmlUtils;
36
37import org.xmlpull.v1.XmlPullParser;
38import org.xmlpull.v1.XmlPullParserException;
39
Dianne Hackborne3dd8842009-07-14 12:06:54 -070040import java.io.BufferedReader;
Mike Lockwood1d9dfc52009-07-16 11:11:18 -040041import java.io.File;
Dianne Hackborne3dd8842009-07-14 12:06:54 -070042import java.io.FileInputStream;
43import java.io.FileNotFoundException;
Mike Lockwood1d9dfc52009-07-16 11:11:18 -040044import java.io.FileReader;
Dianne Hackborne3dd8842009-07-14 12:06:54 -070045import java.io.IOException;
46import java.io.InputStreamReader;
47import java.util.ArrayList;
48
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049public abstract class KeyInputQueue {
50 static final String TAG = "KeyInputQueue";
51
Dianne Hackborne3dd8842009-07-14 12:06:54 -070052 static final boolean DEBUG_VIRTUAL_KEYS = false;
53
Mike Lockwood1d9dfc52009-07-16 11:11:18 -040054 private static final String EXCLUDED_DEVICES_PATH = "etc/excluded-input-devices.xml";
55
Dianne Hackborne3dd8842009-07-14 12:06:54 -070056 final SparseArray<InputDevice> mDevices = new SparseArray<InputDevice>();
57 final ArrayList<VirtualKey> mVirtualKeys = new ArrayList<VirtualKey>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058
59 int mGlobalMetaState = 0;
60 boolean mHaveGlobalMetaState = false;
61
62 final QueuedEvent mFirst;
63 final QueuedEvent mLast;
64 QueuedEvent mCache;
65 int mCacheCount;
66
67 Display mDisplay = null;
Dianne Hackborne3dd8842009-07-14 12:06:54 -070068 int mDisplayWidth;
69 int mDisplayHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070
71 int mOrientation = Surface.ROTATION_0;
72 int[] mKeyRotationMap = null;
73
Dianne Hackborne3dd8842009-07-14 12:06:54 -070074 VirtualKey mPressedVirtualKey = null;
75
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 PowerManager.WakeLock mWakeLock;
77
78 static final int[] KEY_90_MAP = new int[] {
79 KeyEvent.KEYCODE_DPAD_DOWN, KeyEvent.KEYCODE_DPAD_RIGHT,
80 KeyEvent.KEYCODE_DPAD_RIGHT, KeyEvent.KEYCODE_DPAD_UP,
81 KeyEvent.KEYCODE_DPAD_UP, KeyEvent.KEYCODE_DPAD_LEFT,
82 KeyEvent.KEYCODE_DPAD_LEFT, KeyEvent.KEYCODE_DPAD_DOWN,
83 };
84
85 static final int[] KEY_180_MAP = new int[] {
86 KeyEvent.KEYCODE_DPAD_DOWN, KeyEvent.KEYCODE_DPAD_UP,
87 KeyEvent.KEYCODE_DPAD_RIGHT, KeyEvent.KEYCODE_DPAD_LEFT,
88 KeyEvent.KEYCODE_DPAD_UP, KeyEvent.KEYCODE_DPAD_DOWN,
89 KeyEvent.KEYCODE_DPAD_LEFT, KeyEvent.KEYCODE_DPAD_RIGHT,
90 };
91
92 static final int[] KEY_270_MAP = new int[] {
93 KeyEvent.KEYCODE_DPAD_DOWN, KeyEvent.KEYCODE_DPAD_LEFT,
94 KeyEvent.KEYCODE_DPAD_LEFT, KeyEvent.KEYCODE_DPAD_UP,
95 KeyEvent.KEYCODE_DPAD_UP, KeyEvent.KEYCODE_DPAD_RIGHT,
96 KeyEvent.KEYCODE_DPAD_RIGHT, KeyEvent.KEYCODE_DPAD_DOWN,
97 };
98
99 public static final int FILTER_REMOVE = 0;
100 public static final int FILTER_KEEP = 1;
101 public static final int FILTER_ABORT = -1;
Michael Chan53071d62009-05-13 17:29:48 -0700102
103 private static final boolean MEASURE_LATENCY = false;
104 private LatencyTimer lt;
105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 public interface FilterCallback {
107 int filterEvent(QueuedEvent ev);
108 }
109
110 static class QueuedEvent {
111 InputDevice inputDevice;
Michael Chan53071d62009-05-13 17:29:48 -0700112 long whenNano;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 int flags; // From the raw event
114 int classType; // One of the class constants in InputEvent
115 Object event;
116 boolean inQueue;
117
118 void copyFrom(QueuedEvent that) {
119 this.inputDevice = that.inputDevice;
Michael Chan53071d62009-05-13 17:29:48 -0700120 this.whenNano = that.whenNano;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 this.flags = that.flags;
122 this.classType = that.classType;
123 this.event = that.event;
124 }
125
126 @Override
127 public String toString() {
128 return "QueuedEvent{"
129 + Integer.toHexString(System.identityHashCode(this))
130 + " " + event + "}";
131 }
132
133 // not copied
134 QueuedEvent prev;
135 QueuedEvent next;
136 }
137
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700138 /**
139 * A key that exists as a part of the touch-screen, outside of the normal
140 * display area of the screen.
141 */
142 static class VirtualKey {
143 int scancode;
144 int centerx;
145 int centery;
146 int width;
147 int height;
148
149 int hitLeft;
150 int hitTop;
151 int hitRight;
152 int hitBottom;
153
154 InputDevice lastDevice;
155 int lastKeycode;
156
157 boolean checkHit(int x, int y) {
158 return (x >= hitLeft && x <= hitRight
159 && y >= hitTop && y <= hitBottom);
160 }
161
162 void computeHitRect(InputDevice dev, int dw, int dh) {
163 if (dev == lastDevice) {
164 return;
165 }
166
167 if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "computeHitRect for " + scancode
168 + ": dev=" + dev + " absX=" + dev.absX + " absY=" + dev.absY);
169
170 lastDevice = dev;
171
172 int minx = dev.absX.minValue;
173 int maxx = dev.absX.maxValue;
174
175 int halfw = width/2;
176 int left = centerx - halfw;
177 int right = centerx + halfw;
178 hitLeft = minx + ((left*maxx-minx)/dw);
179 hitRight = minx + ((right*maxx-minx)/dw);
180
181 int miny = dev.absY.minValue;
182 int maxy = dev.absY.maxValue;
183
184 int halfh = height/2;
185 int top = centery - halfh;
186 int bottom = centery + halfh;
187 hitTop = miny + ((top*maxy-miny)/dh);
188 hitBottom = miny + ((bottom*maxy-miny)/dh);
189 }
190 }
Michael Chan53071d62009-05-13 17:29:48 -0700191
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400192 private void readVirtualKeys() {
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700193 try {
194 FileInputStream fis = new FileInputStream(
195 "/sys/board_properties/virtualkeys.synaptics-rmi-touchscreen");
196 InputStreamReader isr = new InputStreamReader(fis);
197 BufferedReader br = new BufferedReader(isr);
198 String str = br.readLine();
199 if (str != null) {
200 String[] it = str.split(":");
201 if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "***** VIRTUAL KEYS: " + it);
202 final int N = it.length-6;
203 for (int i=0; i<=N; i+=6) {
204 if (!"0x01".equals(it[i])) {
205 Log.w(TAG, "Unknown virtual key type at elem #" + i
206 + ": " + it[i]);
207 continue;
208 }
209 try {
210 VirtualKey sb = new VirtualKey();
211 sb.scancode = Integer.parseInt(it[i+1]);
212 sb.centerx = Integer.parseInt(it[i+2]);
213 sb.centery = Integer.parseInt(it[i+3]);
214 sb.width = Integer.parseInt(it[i+4]);
215 sb.height = Integer.parseInt(it[i+5]);
216 if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "Virtual key "
217 + sb.scancode + ": center=" + sb.centerx + ","
218 + sb.centery + " size=" + sb.width + "x"
219 + sb.height);
220 mVirtualKeys.add(sb);
221 } catch (NumberFormatException e) {
222 Log.w(TAG, "Bad number at region " + i + " in: "
223 + str, e);
224 }
225 }
226 }
227 br.close();
228 } catch (FileNotFoundException e) {
229 Log.i(TAG, "No virtual keys found");
230 } catch (IOException e) {
231 Log.w(TAG, "Error reading virtual keys", e);
232 }
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400233 }
234
235 private void readExcludedDevices() {
236 // Read partner-provided list of excluded input devices
237 XmlPullParser parser = null;
238 // Environment.getRootDirectory() is a fancy way of saying ANDROID_ROOT or "/system".
239 File confFile = new File(Environment.getRootDirectory(), EXCLUDED_DEVICES_PATH);
240 FileReader confreader = null;
241 try {
242 confreader = new FileReader(confFile);
243 parser = Xml.newPullParser();
244 parser.setInput(confreader);
245 XmlUtils.beginDocument(parser, "devices");
246
247 while (true) {
248 XmlUtils.nextElement(parser);
249 if (!"device".equals(parser.getName())) {
250 break;
251 }
252 String name = parser.getAttributeValue(null, "name");
253 if (name != null) {
254 Log.d(TAG, "addExcludedDevice " + name);
255 addExcludedDevice(name);
256 }
257 }
258 } catch (FileNotFoundException e) {
259 // It's ok if the file does not exist.
260 } catch (Exception e) {
261 Log.e(TAG, "Exception while parsing '" + confFile.getAbsolutePath() + "'", e);
262 } finally {
263 try { if (confreader != null) confreader.close(); } catch (IOException e) { }
264 }
265 }
266
267 KeyInputQueue(Context context) {
268 if (MEASURE_LATENCY) {
269 lt = new LatencyTimer(100, 1000);
270 }
271
272 readVirtualKeys();
273 readExcludedDevices();
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 PowerManager pm = (PowerManager)context.getSystemService(
276 Context.POWER_SERVICE);
277 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
278 "KeyInputQueue");
279 mWakeLock.setReferenceCounted(false);
280
281 mFirst = new QueuedEvent();
282 mLast = new QueuedEvent();
283 mFirst.next = mLast;
284 mLast.prev = mFirst;
285
286 mThread.start();
287 }
288
289 public void setDisplay(Display display) {
290 mDisplay = display;
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700291
292 // We assume at this point that the display dimensions reflect the
293 // natural, unrotated display. We will perform hit tests for soft
294 // buttons based on that display.
295 mDisplayWidth = display.getWidth();
296 mDisplayHeight = display.getHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 }
298
299 public void getInputConfiguration(Configuration config) {
300 synchronized (mFirst) {
301 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
302 config.keyboard = Configuration.KEYBOARD_NOKEYS;
303 config.navigation = Configuration.NAVIGATION_NONAV;
304
305 final int N = mDevices.size();
306 for (int i=0; i<N; i++) {
307 InputDevice d = mDevices.valueAt(i);
308 if (d != null) {
309 if ((d.classes&RawInputEvent.CLASS_TOUCHSCREEN) != 0) {
310 config.touchscreen
311 = Configuration.TOUCHSCREEN_FINGER;
312 //Log.i("foo", "***** HAVE TOUCHSCREEN!");
313 }
314 if ((d.classes&RawInputEvent.CLASS_ALPHAKEY) != 0) {
315 config.keyboard
316 = Configuration.KEYBOARD_QWERTY;
317 //Log.i("foo", "***** HAVE QWERTY!");
318 }
319 if ((d.classes&RawInputEvent.CLASS_TRACKBALL) != 0) {
320 config.navigation
321 = Configuration.NAVIGATION_TRACKBALL;
322 //Log.i("foo", "***** HAVE TRACKBALL!");
323 }
324 }
325 }
326 }
327 }
328
329 public static native String getDeviceName(int deviceId);
330 public static native int getDeviceClasses(int deviceId);
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400331 public static native void addExcludedDevice(String deviceName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 public static native boolean getAbsoluteInfo(int deviceId, int axis,
333 InputDevice.AbsoluteInfo outInfo);
334 public static native int getSwitchState(int sw);
335 public static native int getSwitchState(int deviceId, int sw);
336 public static native int getScancodeState(int sw);
337 public static native int getScancodeState(int deviceId, int sw);
338 public static native int getKeycodeState(int sw);
339 public static native int getKeycodeState(int deviceId, int sw);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700340 public static native int scancodeToKeycode(int deviceId, int scancode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 public static native boolean hasKeys(int[] keycodes, boolean[] keyExists);
342
343 public static KeyEvent newKeyEvent(InputDevice device, long downTime,
344 long eventTime, boolean down, int keycode, int repeatCount,
345 int scancode, int flags) {
346 return new KeyEvent(
347 downTime, eventTime,
348 down ? KeyEvent.ACTION_DOWN : KeyEvent.ACTION_UP,
349 keycode, repeatCount,
350 device != null ? device.mMetaKeysState : 0,
351 device != null ? device.id : -1, scancode,
The Android Open Source Project10592532009-03-18 17:39:46 -0700352 flags | KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 }
354
355 Thread mThread = new Thread("InputDeviceReader") {
356 public void run() {
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400357 Log.d(TAG, "InputDeviceReader.run()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 android.os.Process.setThreadPriority(
359 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
360
361 try {
362 RawInputEvent ev = new RawInputEvent();
363 while (true) {
364 InputDevice di;
365
366 // block, doesn't release the monitor
367 readEvent(ev);
368
369 boolean send = false;
370 boolean configChanged = false;
371
372 if (false) {
373 Log.i(TAG, "Input event: dev=0x"
374 + Integer.toHexString(ev.deviceId)
375 + " type=0x" + Integer.toHexString(ev.type)
376 + " scancode=" + ev.scancode
377 + " keycode=" + ev.keycode
378 + " value=" + ev.value);
379 }
380
381 if (ev.type == RawInputEvent.EV_DEVICE_ADDED) {
382 synchronized (mFirst) {
383 di = newInputDevice(ev.deviceId);
384 mDevices.put(ev.deviceId, di);
385 configChanged = true;
386 }
387 } else if (ev.type == RawInputEvent.EV_DEVICE_REMOVED) {
388 synchronized (mFirst) {
389 Log.i(TAG, "Device removed: id=0x"
390 + Integer.toHexString(ev.deviceId));
391 di = mDevices.get(ev.deviceId);
392 if (di != null) {
393 mDevices.delete(ev.deviceId);
394 configChanged = true;
395 } else {
396 Log.w(TAG, "Bad device id: " + ev.deviceId);
397 }
398 }
399 } else {
400 di = getInputDevice(ev.deviceId);
401
402 // first crack at it
403 send = preprocessEvent(di, ev);
404
405 if (ev.type == RawInputEvent.EV_KEY) {
406 di.mMetaKeysState = makeMetaState(ev.keycode,
407 ev.value != 0, di.mMetaKeysState);
408 mHaveGlobalMetaState = false;
409 }
410 }
411
412 if (di == null) {
413 continue;
414 }
415
416 if (configChanged) {
417 synchronized (mFirst) {
Michael Chan53071d62009-05-13 17:29:48 -0700418 addLocked(di, System.nanoTime(), 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 RawInputEvent.CLASS_CONFIGURATION_CHANGED,
420 null);
421 }
422 }
423
424 if (!send) {
425 continue;
426 }
427
428 synchronized (mFirst) {
429 // NOTE: The event timebase absolutely must be the same
430 // timebase as SystemClock.uptimeMillis().
431 //curTime = gotOne ? ev.when : SystemClock.uptimeMillis();
432 final long curTime = SystemClock.uptimeMillis();
Michael Chan53071d62009-05-13 17:29:48 -0700433 final long curTimeNano = System.nanoTime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 //Log.i(TAG, "curTime=" + curTime + ", systemClock=" + SystemClock.uptimeMillis());
435
436 final int classes = di.classes;
437 final int type = ev.type;
438 final int scancode = ev.scancode;
439 send = false;
440
441 // Is it a key event?
442 if (type == RawInputEvent.EV_KEY &&
443 (classes&RawInputEvent.CLASS_KEYBOARD) != 0 &&
444 (scancode < RawInputEvent.BTN_FIRST ||
445 scancode > RawInputEvent.BTN_LAST)) {
446 boolean down;
447 if (ev.value != 0) {
448 down = true;
449 di.mDownTime = curTime;
450 } else {
451 down = false;
452 }
453 int keycode = rotateKeyCodeLocked(ev.keycode);
Michael Chan53071d62009-05-13 17:29:48 -0700454 addLocked(di, curTimeNano, ev.flags,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 RawInputEvent.CLASS_KEYBOARD,
456 newKeyEvent(di, di.mDownTime, curTime, down,
457 keycode, 0, scancode,
458 ((ev.flags & WindowManagerPolicy.FLAG_WOKE_HERE) != 0)
459 ? KeyEvent.FLAG_WOKE_HERE : 0));
460 } else if (ev.type == RawInputEvent.EV_KEY) {
461 if (ev.scancode == RawInputEvent.BTN_TOUCH &&
462 (classes&RawInputEvent.CLASS_TOUCHSCREEN) != 0) {
463 di.mAbs.changed = true;
464 di.mAbs.down = ev.value != 0;
465 }
466 if (ev.scancode == RawInputEvent.BTN_MOUSE &&
467 (classes&RawInputEvent.CLASS_TRACKBALL) != 0) {
468 di.mRel.changed = true;
469 di.mRel.down = ev.value != 0;
470 send = true;
471 }
472
473 } else if (ev.type == RawInputEvent.EV_ABS &&
474 (classes&RawInputEvent.CLASS_TOUCHSCREEN) != 0) {
475 if (ev.scancode == RawInputEvent.ABS_X) {
476 di.mAbs.changed = true;
477 di.mAbs.x = ev.value;
478 } else if (ev.scancode == RawInputEvent.ABS_Y) {
479 di.mAbs.changed = true;
480 di.mAbs.y = ev.value;
481 } else if (ev.scancode == RawInputEvent.ABS_PRESSURE) {
482 di.mAbs.changed = true;
483 di.mAbs.pressure = ev.value;
484 } else if (ev.scancode == RawInputEvent.ABS_TOOL_WIDTH) {
485 di.mAbs.changed = true;
486 di.mAbs.size = ev.value;
487 }
488
489 } else if (ev.type == RawInputEvent.EV_REL &&
490 (classes&RawInputEvent.CLASS_TRACKBALL) != 0) {
491 // Add this relative movement into our totals.
492 if (ev.scancode == RawInputEvent.REL_X) {
493 di.mRel.changed = true;
494 di.mRel.x += ev.value;
495 } else if (ev.scancode == RawInputEvent.REL_Y) {
496 di.mRel.changed = true;
497 di.mRel.y += ev.value;
498 }
499 }
500
501 if (send || ev.type == RawInputEvent.EV_SYN) {
502 if (mDisplay != null) {
503 if (!mHaveGlobalMetaState) {
504 computeGlobalMetaStateLocked();
505 }
506
507 MotionEvent me;
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700508
509 InputDevice.MotionState ms = di.mAbs;
510 if (ms.changed) {
511 ms.changed = false;
512
513 boolean doMotion = true;
514
515 // Look for virtual buttons.
516 VirtualKey vk = mPressedVirtualKey;
517 if (vk != null) {
518 doMotion = false;
519 if (!ms.down) {
520 mPressedVirtualKey = null;
521 ms.lastDown = ms.down;
522 if (DEBUG_VIRTUAL_KEYS) Log.v(TAG,
523 "Generate key up for: " + vk.scancode);
524 addLocked(di, curTimeNano, ev.flags,
525 RawInputEvent.CLASS_KEYBOARD,
526 newKeyEvent(di, di.mDownTime,
527 curTime, false,
528 vk.lastKeycode,
529 0, vk.scancode, 0));
530 }
531 } else if (ms.down && !ms.lastDown) {
532 vk = findSoftButton(di);
533 if (vk != null) {
534 doMotion = false;
535 mPressedVirtualKey = vk;
536 vk.lastKeycode = scancodeToKeycode(
537 di.id, vk.scancode);
538 ms.lastDown = ms.down;
539 di.mDownTime = curTime;
540 if (DEBUG_VIRTUAL_KEYS) Log.v(TAG,
541 "Generate key down for: " + vk.scancode
542 + " (keycode=" + vk.lastKeycode + ")");
543 addLocked(di, curTimeNano, ev.flags,
544 RawInputEvent.CLASS_KEYBOARD,
545 newKeyEvent(di, di.mDownTime,
546 curTime, true,
547 vk.lastKeycode, 0,
548 vk.scancode, 0));
549 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 }
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700551
552 if (doMotion) {
553 me = ms.generateMotion(di, curTime,
554 curTimeNano, true, mDisplay,
555 mOrientation, mGlobalMetaState);
556 if (false) Log.v(TAG, "Absolute: x=" + di.mAbs.x
557 + " y=" + di.mAbs.y + " ev=" + me);
558 if (me != null) {
559 if (WindowManagerPolicy.WATCH_POINTER) {
560 Log.i(TAG, "Enqueueing: " + me);
561 }
562 addLocked(di, curTimeNano, ev.flags,
563 RawInputEvent.CLASS_TOUCHSCREEN, me);
564 }
565 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 }
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700567
568 ms = di.mRel;
569 if (ms.changed) {
570 ms.changed = false;
571
572 me = ms.generateMotion(di, curTime,
573 curTimeNano, false, mDisplay,
574 mOrientation, mGlobalMetaState);
575 if (false) Log.v(TAG, "Relative: x=" + di.mRel.x
576 + " y=" + di.mRel.y + " ev=" + me);
577 if (me != null) {
578 addLocked(di, curTimeNano, ev.flags,
579 RawInputEvent.CLASS_TRACKBALL, me);
580 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 }
582 }
583 }
584 }
585 }
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700586
587 } catch (RuntimeException exc) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588 Log.e(TAG, "InputReaderThread uncaught exception", exc);
589 }
590 }
591 };
592
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700593 private VirtualKey findSoftButton(InputDevice dev) {
594 final int N = mVirtualKeys.size();
595 if (N <= 0) {
596 return null;
597 }
598
599 final InputDevice.AbsoluteInfo absx = dev.absX;
600 final InputDevice.AbsoluteInfo absy = dev.absY;
601 final InputDevice.MotionState absm = dev.mAbs;
602 if (absx == null || absy == null || absm == null) {
603 return null;
604 }
605
606 if (absm.x >= absx.minValue && absm.x <= absx.maxValue
607 && absm.y >= absy.minValue && absm.y <= absy.maxValue) {
608 if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "Input (" + absm.x
609 + "," + absm.y + ") inside of display");
610 return null;
611 }
612
613 for (int i=0; i<N; i++) {
614 VirtualKey sb = mVirtualKeys.get(i);
615 sb.computeHitRect(dev, mDisplayWidth, mDisplayHeight);
616 if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "Hit test (" + absm.x + ","
617 + absm.y + ") in code " + sb.scancode + " - (" + sb.hitLeft
618 + "," + sb.hitTop + ")-(" + sb.hitRight + ","
619 + sb.hitBottom + ")");
620 if (sb.checkHit(absm.x, absm.y)) {
621 if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "Hit!");
622 return sb;
623 }
624 }
625
626 return null;
627 }
628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 /**
630 * Returns a new meta state for the given keys and old state.
631 */
632 private static final int makeMetaState(int keycode, boolean down, int old) {
633 int mask;
634 switch (keycode) {
635 case KeyEvent.KEYCODE_ALT_LEFT:
636 mask = KeyEvent.META_ALT_LEFT_ON;
637 break;
638 case KeyEvent.KEYCODE_ALT_RIGHT:
639 mask = KeyEvent.META_ALT_RIGHT_ON;
640 break;
641 case KeyEvent.KEYCODE_SHIFT_LEFT:
642 mask = KeyEvent.META_SHIFT_LEFT_ON;
643 break;
644 case KeyEvent.KEYCODE_SHIFT_RIGHT:
645 mask = KeyEvent.META_SHIFT_RIGHT_ON;
646 break;
647 case KeyEvent.KEYCODE_SYM:
648 mask = KeyEvent.META_SYM_ON;
649 break;
650 default:
651 return old;
652 }
653 int result = ~(KeyEvent.META_ALT_ON | KeyEvent.META_SHIFT_ON)
654 & (down ? (old | mask) : (old & ~mask));
655 if (0 != (result & (KeyEvent.META_ALT_LEFT_ON | KeyEvent.META_ALT_RIGHT_ON))) {
656 result |= KeyEvent.META_ALT_ON;
657 }
658 if (0 != (result & (KeyEvent.META_SHIFT_LEFT_ON | KeyEvent.META_SHIFT_RIGHT_ON))) {
659 result |= KeyEvent.META_SHIFT_ON;
660 }
661 return result;
662 }
663
664 private void computeGlobalMetaStateLocked() {
665 int i = mDevices.size();
666 mGlobalMetaState = 0;
667 while ((--i) >= 0) {
668 mGlobalMetaState |= mDevices.valueAt(i).mMetaKeysState;
669 }
670 mHaveGlobalMetaState = true;
671 }
672
673 /*
674 * Return true if you want the event to get passed on to the
675 * rest of the system, and false if you've handled it and want
676 * it dropped.
677 */
678 abstract boolean preprocessEvent(InputDevice device, RawInputEvent event);
679
680 InputDevice getInputDevice(int deviceId) {
681 synchronized (mFirst) {
682 return getInputDeviceLocked(deviceId);
683 }
684 }
685
686 private InputDevice getInputDeviceLocked(int deviceId) {
687 return mDevices.get(deviceId);
688 }
689
690 public void setOrientation(int orientation) {
691 synchronized(mFirst) {
692 mOrientation = orientation;
693 switch (orientation) {
694 case Surface.ROTATION_90:
695 mKeyRotationMap = KEY_90_MAP;
696 break;
697 case Surface.ROTATION_180:
698 mKeyRotationMap = KEY_180_MAP;
699 break;
700 case Surface.ROTATION_270:
701 mKeyRotationMap = KEY_270_MAP;
702 break;
703 default:
704 mKeyRotationMap = null;
705 break;
706 }
707 }
708 }
709
710 public int rotateKeyCode(int keyCode) {
711 synchronized(mFirst) {
712 return rotateKeyCodeLocked(keyCode);
713 }
714 }
715
716 private int rotateKeyCodeLocked(int keyCode) {
717 int[] map = mKeyRotationMap;
718 if (map != null) {
719 final int N = map.length;
720 for (int i=0; i<N; i+=2) {
721 if (map[i] == keyCode) {
722 return map[i+1];
723 }
724 }
725 }
726 return keyCode;
727 }
728
729 boolean hasEvents() {
730 synchronized (mFirst) {
731 return mFirst.next != mLast;
732 }
733 }
734
735 /*
736 * returns true if we returned an event, and false if we timed out
737 */
738 QueuedEvent getEvent(long timeoutMS) {
739 long begin = SystemClock.uptimeMillis();
740 final long end = begin+timeoutMS;
741 long now = begin;
742 synchronized (mFirst) {
743 while (mFirst.next == mLast && end > now) {
744 try {
745 mWakeLock.release();
746 mFirst.wait(end-now);
747 }
748 catch (InterruptedException e) {
749 }
750 now = SystemClock.uptimeMillis();
751 if (begin > now) {
752 begin = now;
753 }
754 }
755 if (mFirst.next == mLast) {
756 return null;
757 }
758 QueuedEvent p = mFirst.next;
759 mFirst.next = p.next;
760 mFirst.next.prev = mFirst;
761 p.inQueue = false;
762 return p;
763 }
764 }
765
766 void recycleEvent(QueuedEvent ev) {
767 synchronized (mFirst) {
768 //Log.i(TAG, "Recycle event: " + ev);
769 if (ev.event == ev.inputDevice.mAbs.currentMove) {
770 ev.inputDevice.mAbs.currentMove = null;
771 }
772 if (ev.event == ev.inputDevice.mRel.currentMove) {
773 if (false) Log.i(TAG, "Detach rel " + ev.event);
774 ev.inputDevice.mRel.currentMove = null;
775 ev.inputDevice.mRel.x = 0;
776 ev.inputDevice.mRel.y = 0;
777 }
778 recycleLocked(ev);
779 }
780 }
781
782 void filterQueue(FilterCallback cb) {
783 synchronized (mFirst) {
784 QueuedEvent cur = mLast.prev;
785 while (cur.prev != null) {
786 switch (cb.filterEvent(cur)) {
787 case FILTER_REMOVE:
788 cur.prev.next = cur.next;
789 cur.next.prev = cur.prev;
790 break;
791 case FILTER_ABORT:
792 return;
793 }
794 cur = cur.prev;
795 }
796 }
797 }
798
Michael Chan53071d62009-05-13 17:29:48 -0700799 private QueuedEvent obtainLocked(InputDevice device, long whenNano,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800 int flags, int classType, Object event) {
801 QueuedEvent ev;
802 if (mCacheCount == 0) {
803 ev = new QueuedEvent();
804 } else {
805 ev = mCache;
806 ev.inQueue = false;
807 mCache = ev.next;
808 mCacheCount--;
809 }
810 ev.inputDevice = device;
Michael Chan53071d62009-05-13 17:29:48 -0700811 ev.whenNano = whenNano;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800812 ev.flags = flags;
813 ev.classType = classType;
814 ev.event = event;
815 return ev;
816 }
817
818 private void recycleLocked(QueuedEvent ev) {
819 if (ev.inQueue) {
820 throw new RuntimeException("Event already in queue!");
821 }
822 if (mCacheCount < 10) {
823 mCacheCount++;
824 ev.next = mCache;
825 mCache = ev;
826 ev.inQueue = true;
827 }
828 }
829
Michael Chan53071d62009-05-13 17:29:48 -0700830 private void addLocked(InputDevice device, long whenNano, int flags,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831 int classType, Object event) {
832 boolean poke = mFirst.next == mLast;
833
Michael Chan53071d62009-05-13 17:29:48 -0700834 QueuedEvent ev = obtainLocked(device, whenNano, flags, classType, event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800835 QueuedEvent p = mLast.prev;
Michael Chan53071d62009-05-13 17:29:48 -0700836 while (p != mFirst && ev.whenNano < p.whenNano) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 p = p.prev;
838 }
839
840 ev.next = p.next;
841 ev.prev = p;
842 p.next = ev;
843 ev.next.prev = ev;
844 ev.inQueue = true;
845
846 if (poke) {
Michael Chan53071d62009-05-13 17:29:48 -0700847 long time;
848 if (MEASURE_LATENCY) {
849 time = System.nanoTime();
850 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800851 mFirst.notify();
852 mWakeLock.acquire();
Michael Chan53071d62009-05-13 17:29:48 -0700853 if (MEASURE_LATENCY) {
854 lt.sample("1 addLocked-queued event ", System.nanoTime() - time);
855 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800856 }
857 }
858
859 private InputDevice newInputDevice(int deviceId) {
860 int classes = getDeviceClasses(deviceId);
861 String name = getDeviceName(deviceId);
862 Log.i(TAG, "Device added: id=0x" + Integer.toHexString(deviceId)
863 + ", name=" + name
864 + ", classes=" + Integer.toHexString(classes));
865 InputDevice.AbsoluteInfo absX;
866 InputDevice.AbsoluteInfo absY;
867 InputDevice.AbsoluteInfo absPressure;
868 InputDevice.AbsoluteInfo absSize;
869 if ((classes&RawInputEvent.CLASS_TOUCHSCREEN) != 0) {
870 absX = loadAbsoluteInfo(deviceId, RawInputEvent.ABS_X, "X");
871 absY = loadAbsoluteInfo(deviceId, RawInputEvent.ABS_Y, "Y");
872 absPressure = loadAbsoluteInfo(deviceId, RawInputEvent.ABS_PRESSURE, "Pressure");
873 absSize = loadAbsoluteInfo(deviceId, RawInputEvent.ABS_TOOL_WIDTH, "Size");
874 } else {
875 absX = null;
876 absY = null;
877 absPressure = null;
878 absSize = null;
879 }
880
881 return new InputDevice(deviceId, classes, name, absX, absY, absPressure, absSize);
882 }
883
884 private InputDevice.AbsoluteInfo loadAbsoluteInfo(int id, int channel,
885 String name) {
886 InputDevice.AbsoluteInfo info = new InputDevice.AbsoluteInfo();
887 if (getAbsoluteInfo(id, channel, info)
888 && info.minValue != info.maxValue) {
889 Log.i(TAG, " " + name + ": min=" + info.minValue
890 + " max=" + info.maxValue
891 + " flat=" + info.flat
892 + " fuzz=" + info.fuzz);
893 info.range = info.maxValue-info.minValue;
894 return info;
895 }
896 Log.i(TAG, " " + name + ": unknown values");
897 return null;
898 }
899 private static native boolean readEvent(RawInputEvent outEvent);
900}