blob: e05c800b3e0c5f3516918c568707641a0cf10379 [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;
Mike Lockwood1d9dfc52009-07-16 11:11:18 -040038
Dianne Hackborne3dd8842009-07-14 12:06:54 -070039import java.io.BufferedReader;
Mike Lockwood1d9dfc52009-07-16 11:11:18 -040040import java.io.File;
Dianne Hackborne3dd8842009-07-14 12:06:54 -070041import java.io.FileInputStream;
42import java.io.FileNotFoundException;
Mike Lockwood1d9dfc52009-07-16 11:11:18 -040043import java.io.FileReader;
Dianne Hackborne3dd8842009-07-14 12:06:54 -070044import java.io.IOException;
45import java.io.InputStreamReader;
46import java.util.ArrayList;
47
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048public abstract class KeyInputQueue {
49 static final String TAG = "KeyInputQueue";
50
Dianne Hackbornbd0a81f2009-10-04 13:30:50 -070051 static final boolean DEBUG = false;
Dianne Hackborne3dd8842009-07-14 12:06:54 -070052 static final boolean DEBUG_VIRTUAL_KEYS = false;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -070053 static final boolean DEBUG_POINTERS = false;
Dianne Hackborne3dd8842009-07-14 12:06:54 -070054
Dianne Hackborn1411d1c2009-10-12 23:21:18 -070055 /**
56 * Turn on some hacks we have to improve the touch interaction with a
57 * certain device whose screen currently is not all that good.
58 */
Dianne Hackborn65cb605e2009-11-10 17:06:22 -080059 static boolean BAD_TOUCH_HACK = false;
Dianne Hackborn1411d1c2009-10-12 23:21:18 -070060
Mike Lockwood1d9dfc52009-07-16 11:11:18 -040061 private static final String EXCLUDED_DEVICES_PATH = "etc/excluded-input-devices.xml";
62
Dianne Hackborne3dd8842009-07-14 12:06:54 -070063 final SparseArray<InputDevice> mDevices = new SparseArray<InputDevice>();
Dianne Hackborna8f60182009-09-01 19:01:50 -070064 final SparseArray<InputDevice> mIgnoredDevices = new SparseArray<InputDevice>();
Dianne Hackborne3dd8842009-07-14 12:06:54 -070065 final ArrayList<VirtualKey> mVirtualKeys = new ArrayList<VirtualKey>();
Dianne Hackbornddca3ee2009-07-23 19:01:31 -070066 final HapticFeedbackCallback mHapticFeedbackCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067
68 int mGlobalMetaState = 0;
69 boolean mHaveGlobalMetaState = false;
70
71 final QueuedEvent mFirst;
72 final QueuedEvent mLast;
73 QueuedEvent mCache;
74 int mCacheCount;
75
76 Display mDisplay = null;
Dianne Hackborne3dd8842009-07-14 12:06:54 -070077 int mDisplayWidth;
78 int mDisplayHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079
80 int mOrientation = Surface.ROTATION_0;
81 int[] mKeyRotationMap = null;
82
Dianne Hackborne3dd8842009-07-14 12:06:54 -070083 VirtualKey mPressedVirtualKey = null;
84
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 PowerManager.WakeLock mWakeLock;
86
87 static final int[] KEY_90_MAP = new int[] {
88 KeyEvent.KEYCODE_DPAD_DOWN, KeyEvent.KEYCODE_DPAD_RIGHT,
89 KeyEvent.KEYCODE_DPAD_RIGHT, KeyEvent.KEYCODE_DPAD_UP,
90 KeyEvent.KEYCODE_DPAD_UP, KeyEvent.KEYCODE_DPAD_LEFT,
91 KeyEvent.KEYCODE_DPAD_LEFT, KeyEvent.KEYCODE_DPAD_DOWN,
92 };
93
94 static final int[] KEY_180_MAP = new int[] {
95 KeyEvent.KEYCODE_DPAD_DOWN, KeyEvent.KEYCODE_DPAD_UP,
96 KeyEvent.KEYCODE_DPAD_RIGHT, KeyEvent.KEYCODE_DPAD_LEFT,
97 KeyEvent.KEYCODE_DPAD_UP, KeyEvent.KEYCODE_DPAD_DOWN,
98 KeyEvent.KEYCODE_DPAD_LEFT, KeyEvent.KEYCODE_DPAD_RIGHT,
99 };
100
101 static final int[] KEY_270_MAP = new int[] {
102 KeyEvent.KEYCODE_DPAD_DOWN, KeyEvent.KEYCODE_DPAD_LEFT,
103 KeyEvent.KEYCODE_DPAD_LEFT, KeyEvent.KEYCODE_DPAD_UP,
104 KeyEvent.KEYCODE_DPAD_UP, KeyEvent.KEYCODE_DPAD_RIGHT,
105 KeyEvent.KEYCODE_DPAD_RIGHT, KeyEvent.KEYCODE_DPAD_DOWN,
106 };
107
108 public static final int FILTER_REMOVE = 0;
109 public static final int FILTER_KEEP = 1;
110 public static final int FILTER_ABORT = -1;
Michael Chan53071d62009-05-13 17:29:48 -0700111
112 private static final boolean MEASURE_LATENCY = false;
113 private LatencyTimer lt;
114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 public interface FilterCallback {
116 int filterEvent(QueuedEvent ev);
117 }
118
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700119 public interface HapticFeedbackCallback {
120 void virtualKeyFeedback(KeyEvent event);
121 }
122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 static class QueuedEvent {
124 InputDevice inputDevice;
Michael Chan53071d62009-05-13 17:29:48 -0700125 long whenNano;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 int flags; // From the raw event
127 int classType; // One of the class constants in InputEvent
128 Object event;
129 boolean inQueue;
130
131 void copyFrom(QueuedEvent that) {
132 this.inputDevice = that.inputDevice;
Michael Chan53071d62009-05-13 17:29:48 -0700133 this.whenNano = that.whenNano;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 this.flags = that.flags;
135 this.classType = that.classType;
136 this.event = that.event;
137 }
138
139 @Override
140 public String toString() {
141 return "QueuedEvent{"
142 + Integer.toHexString(System.identityHashCode(this))
143 + " " + event + "}";
144 }
145
146 // not copied
147 QueuedEvent prev;
148 QueuedEvent next;
149 }
150
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700151 /**
152 * A key that exists as a part of the touch-screen, outside of the normal
153 * display area of the screen.
154 */
155 static class VirtualKey {
156 int scancode;
157 int centerx;
158 int centery;
159 int width;
160 int height;
161
162 int hitLeft;
163 int hitTop;
164 int hitRight;
165 int hitBottom;
166
167 InputDevice lastDevice;
168 int lastKeycode;
169
170 boolean checkHit(int x, int y) {
171 return (x >= hitLeft && x <= hitRight
172 && y >= hitTop && y <= hitBottom);
173 }
174
175 void computeHitRect(InputDevice dev, int dw, int dh) {
176 if (dev == lastDevice) {
177 return;
178 }
179
180 if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "computeHitRect for " + scancode
181 + ": dev=" + dev + " absX=" + dev.absX + " absY=" + dev.absY);
182
183 lastDevice = dev;
184
185 int minx = dev.absX.minValue;
186 int maxx = dev.absX.maxValue;
187
188 int halfw = width/2;
189 int left = centerx - halfw;
190 int right = centerx + halfw;
191 hitLeft = minx + ((left*maxx-minx)/dw);
192 hitRight = minx + ((right*maxx-minx)/dw);
193
194 int miny = dev.absY.minValue;
195 int maxy = dev.absY.maxValue;
196
197 int halfh = height/2;
198 int top = centery - halfh;
199 int bottom = centery + halfh;
200 hitTop = miny + ((top*maxy-miny)/dh);
201 hitBottom = miny + ((bottom*maxy-miny)/dh);
202 }
203 }
Michael Chan53071d62009-05-13 17:29:48 -0700204
Iliyan Malchev75b2aed2009-08-06 14:50:57 -0700205 private void readVirtualKeys(String deviceName) {
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700206 try {
207 FileInputStream fis = new FileInputStream(
Iliyan Malchev75b2aed2009-08-06 14:50:57 -0700208 "/sys/board_properties/virtualkeys." + deviceName);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700209 InputStreamReader isr = new InputStreamReader(fis);
Dianne Hackbornbd0a81f2009-10-04 13:30:50 -0700210 BufferedReader br = new BufferedReader(isr, 2048);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700211 String str = br.readLine();
212 if (str != null) {
213 String[] it = str.split(":");
214 if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "***** VIRTUAL KEYS: " + it);
215 final int N = it.length-6;
216 for (int i=0; i<=N; i+=6) {
217 if (!"0x01".equals(it[i])) {
218 Log.w(TAG, "Unknown virtual key type at elem #" + i
219 + ": " + it[i]);
220 continue;
221 }
222 try {
223 VirtualKey sb = new VirtualKey();
224 sb.scancode = Integer.parseInt(it[i+1]);
225 sb.centerx = Integer.parseInt(it[i+2]);
226 sb.centery = Integer.parseInt(it[i+3]);
227 sb.width = Integer.parseInt(it[i+4]);
228 sb.height = Integer.parseInt(it[i+5]);
229 if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "Virtual key "
230 + sb.scancode + ": center=" + sb.centerx + ","
231 + sb.centery + " size=" + sb.width + "x"
232 + sb.height);
233 mVirtualKeys.add(sb);
234 } catch (NumberFormatException e) {
235 Log.w(TAG, "Bad number at region " + i + " in: "
236 + str, e);
237 }
238 }
239 }
240 br.close();
241 } catch (FileNotFoundException e) {
242 Log.i(TAG, "No virtual keys found");
243 } catch (IOException e) {
244 Log.w(TAG, "Error reading virtual keys", e);
245 }
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400246 }
247
248 private void readExcludedDevices() {
249 // Read partner-provided list of excluded input devices
250 XmlPullParser parser = null;
251 // Environment.getRootDirectory() is a fancy way of saying ANDROID_ROOT or "/system".
252 File confFile = new File(Environment.getRootDirectory(), EXCLUDED_DEVICES_PATH);
253 FileReader confreader = null;
254 try {
255 confreader = new FileReader(confFile);
256 parser = Xml.newPullParser();
257 parser.setInput(confreader);
258 XmlUtils.beginDocument(parser, "devices");
259
260 while (true) {
261 XmlUtils.nextElement(parser);
262 if (!"device".equals(parser.getName())) {
263 break;
264 }
265 String name = parser.getAttributeValue(null, "name");
266 if (name != null) {
Dianne Hackbornbd0a81f2009-10-04 13:30:50 -0700267 if (DEBUG) Log.v(TAG, "addExcludedDevice " + name);
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400268 addExcludedDevice(name);
269 }
270 }
271 } catch (FileNotFoundException e) {
272 // It's ok if the file does not exist.
273 } catch (Exception e) {
274 Log.e(TAG, "Exception while parsing '" + confFile.getAbsolutePath() + "'", e);
275 } finally {
276 try { if (confreader != null) confreader.close(); } catch (IOException e) { }
277 }
278 }
279
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700280 KeyInputQueue(Context context, HapticFeedbackCallback hapticFeedbackCallback) {
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400281 if (MEASURE_LATENCY) {
282 lt = new LatencyTimer(100, 1000);
283 }
284
Dianne Hackborn65cb605e2009-11-10 17:06:22 -0800285 BAD_TOUCH_HACK = context.getResources().getBoolean(
286 com.android.internal.R.bool.config_filterTouchEvents);
287
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700288 mHapticFeedbackCallback = hapticFeedbackCallback;
289
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400290 readExcludedDevices();
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 PowerManager pm = (PowerManager)context.getSystemService(
293 Context.POWER_SERVICE);
294 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
295 "KeyInputQueue");
296 mWakeLock.setReferenceCounted(false);
297
298 mFirst = new QueuedEvent();
299 mLast = new QueuedEvent();
300 mFirst.next = mLast;
301 mLast.prev = mFirst;
Christian Petersson15f66ce2010-06-03 11:16:39 +0200302 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303
Christian Petersson15f66ce2010-06-03 11:16:39 +0200304 void start() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 mThread.start();
306 }
307
308 public void setDisplay(Display display) {
309 mDisplay = display;
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700310
311 // We assume at this point that the display dimensions reflect the
312 // natural, unrotated display. We will perform hit tests for soft
313 // buttons based on that display.
314 mDisplayWidth = display.getWidth();
315 mDisplayHeight = display.getHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 }
317
318 public void getInputConfiguration(Configuration config) {
319 synchronized (mFirst) {
320 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
321 config.keyboard = Configuration.KEYBOARD_NOKEYS;
322 config.navigation = Configuration.NAVIGATION_NONAV;
323
324 final int N = mDevices.size();
325 for (int i=0; i<N; i++) {
326 InputDevice d = mDevices.valueAt(i);
327 if (d != null) {
328 if ((d.classes&RawInputEvent.CLASS_TOUCHSCREEN) != 0) {
329 config.touchscreen
330 = Configuration.TOUCHSCREEN_FINGER;
331 //Log.i("foo", "***** HAVE TOUCHSCREEN!");
332 }
333 if ((d.classes&RawInputEvent.CLASS_ALPHAKEY) != 0) {
334 config.keyboard
335 = Configuration.KEYBOARD_QWERTY;
336 //Log.i("foo", "***** HAVE QWERTY!");
337 }
338 if ((d.classes&RawInputEvent.CLASS_TRACKBALL) != 0) {
339 config.navigation
340 = Configuration.NAVIGATION_TRACKBALL;
341 //Log.i("foo", "***** HAVE TRACKBALL!");
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700342 } else if ((d.classes&RawInputEvent.CLASS_DPAD) != 0) {
343 config.navigation
344 = Configuration.NAVIGATION_DPAD;
345 //Log.i("foo", "***** HAVE DPAD!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 }
347 }
348 }
349 }
350 }
351
Dianne Hackborn6af0d502009-09-28 13:25:46 -0700352 public int getScancodeState(int code) {
353 synchronized (mFirst) {
354 VirtualKey vk = mPressedVirtualKey;
355 if (vk != null) {
356 if (vk.scancode == code) {
357 return 2;
358 }
359 }
360 return nativeGetScancodeState(code);
361 }
362 }
363
364 public int getScancodeState(int deviceId, int code) {
365 synchronized (mFirst) {
366 VirtualKey vk = mPressedVirtualKey;
367 if (vk != null) {
368 if (vk.scancode == code) {
369 return 2;
370 }
371 }
372 return nativeGetScancodeState(deviceId, code);
373 }
374 }
375
Dianne Hackborn1d62ea92009-11-17 12:49:50 -0800376 public int getTrackballScancodeState(int code) {
377 synchronized (mFirst) {
378 final int N = mDevices.size();
379 for (int i=0; i<N; i++) {
380 InputDevice dev = mDevices.valueAt(i);
381 if ((dev.classes&RawInputEvent.CLASS_TRACKBALL) != 0) {
382 int res = nativeGetScancodeState(dev.id, code);
383 if (res > 0) {
384 return res;
385 }
386 }
387 }
388 }
389
390 return 0;
391 }
392
393 public int getDPadScancodeState(int code) {
394 synchronized (mFirst) {
395 final int N = mDevices.size();
396 for (int i=0; i<N; i++) {
397 InputDevice dev = mDevices.valueAt(i);
398 if ((dev.classes&RawInputEvent.CLASS_DPAD) != 0) {
399 int res = nativeGetScancodeState(dev.id, code);
400 if (res > 0) {
401 return res;
402 }
403 }
404 }
405 }
406
407 return 0;
408 }
409
Dianne Hackborn6af0d502009-09-28 13:25:46 -0700410 public int getKeycodeState(int code) {
411 synchronized (mFirst) {
412 VirtualKey vk = mPressedVirtualKey;
413 if (vk != null) {
414 if (vk.lastKeycode == code) {
415 return 2;
416 }
417 }
418 return nativeGetKeycodeState(code);
419 }
420 }
421
422 public int getKeycodeState(int deviceId, int code) {
423 synchronized (mFirst) {
424 VirtualKey vk = mPressedVirtualKey;
425 if (vk != null) {
426 if (vk.lastKeycode == code) {
427 return 2;
428 }
429 }
430 return nativeGetKeycodeState(deviceId, code);
431 }
432 }
433
Dianne Hackborn1d62ea92009-11-17 12:49:50 -0800434 public int getTrackballKeycodeState(int code) {
435 synchronized (mFirst) {
436 final int N = mDevices.size();
437 for (int i=0; i<N; i++) {
438 InputDevice dev = mDevices.valueAt(i);
439 if ((dev.classes&RawInputEvent.CLASS_TRACKBALL) != 0) {
440 int res = nativeGetKeycodeState(dev.id, code);
441 if (res > 0) {
442 return res;
443 }
444 }
445 }
446 }
447
448 return 0;
449 }
450
451 public int getDPadKeycodeState(int code) {
452 synchronized (mFirst) {
453 final int N = mDevices.size();
454 for (int i=0; i<N; i++) {
455 InputDevice dev = mDevices.valueAt(i);
456 if ((dev.classes&RawInputEvent.CLASS_DPAD) != 0) {
457 int res = nativeGetKeycodeState(dev.id, code);
458 if (res > 0) {
459 return res;
460 }
461 }
462 }
463 }
464
465 return 0;
466 }
467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 public static native String getDeviceName(int deviceId);
469 public static native int getDeviceClasses(int deviceId);
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400470 public static native void addExcludedDevice(String deviceName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 public static native boolean getAbsoluteInfo(int deviceId, int axis,
472 InputDevice.AbsoluteInfo outInfo);
473 public static native int getSwitchState(int sw);
474 public static native int getSwitchState(int deviceId, int sw);
Dianne Hackborn6af0d502009-09-28 13:25:46 -0700475 public static native int nativeGetScancodeState(int code);
476 public static native int nativeGetScancodeState(int deviceId, int code);
477 public static native int nativeGetKeycodeState(int code);
478 public static native int nativeGetKeycodeState(int deviceId, int code);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700479 public static native int scancodeToKeycode(int deviceId, int scancode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 public static native boolean hasKeys(int[] keycodes, boolean[] keyExists);
481
482 public static KeyEvent newKeyEvent(InputDevice device, long downTime,
483 long eventTime, boolean down, int keycode, int repeatCount,
484 int scancode, int flags) {
485 return new KeyEvent(
486 downTime, eventTime,
487 down ? KeyEvent.ACTION_DOWN : KeyEvent.ACTION_UP,
488 keycode, repeatCount,
489 device != null ? device.mMetaKeysState : 0,
490 device != null ? device.id : -1, scancode,
The Android Open Source Project10592532009-03-18 17:39:46 -0700491 flags | KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 }
493
494 Thread mThread = new Thread("InputDeviceReader") {
495 public void run() {
Dianne Hackbornbd0a81f2009-10-04 13:30:50 -0700496 if (DEBUG) Log.v(TAG, "InputDeviceReader.run()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 android.os.Process.setThreadPriority(
498 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
499
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700500 RawInputEvent ev = new RawInputEvent();
501 while (true) {
502 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 InputDevice di;
504
505 // block, doesn't release the monitor
506 readEvent(ev);
507
508 boolean send = false;
509 boolean configChanged = false;
510
511 if (false) {
512 Log.i(TAG, "Input event: dev=0x"
513 + Integer.toHexString(ev.deviceId)
514 + " type=0x" + Integer.toHexString(ev.type)
515 + " scancode=" + ev.scancode
516 + " keycode=" + ev.keycode
517 + " value=" + ev.value);
518 }
519
520 if (ev.type == RawInputEvent.EV_DEVICE_ADDED) {
521 synchronized (mFirst) {
522 di = newInputDevice(ev.deviceId);
Dianne Hackborna8f60182009-09-01 19:01:50 -0700523 if (di.classes != 0) {
524 // If this device is some kind of input class,
525 // we care about it.
526 mDevices.put(ev.deviceId, di);
527 if ((di.classes & RawInputEvent.CLASS_TOUCHSCREEN) != 0) {
528 readVirtualKeys(di.name);
529 }
530 // The configuration may have changed because
531 // of this device.
532 configChanged = true;
533 } else {
534 // We won't do anything with this device.
535 mIgnoredDevices.put(ev.deviceId, di);
536 Log.i(TAG, "Ignoring non-input device: id=0x"
537 + Integer.toHexString(di.id)
538 + ", name=" + di.name);
Iliyan Malchev75b2aed2009-08-06 14:50:57 -0700539 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 }
541 } else if (ev.type == RawInputEvent.EV_DEVICE_REMOVED) {
542 synchronized (mFirst) {
Dianne Hackborna8f60182009-09-01 19:01:50 -0700543 if (false) {
544 Log.i(TAG, "Device removed: id=0x"
545 + Integer.toHexString(ev.deviceId));
546 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 di = mDevices.get(ev.deviceId);
548 if (di != null) {
549 mDevices.delete(ev.deviceId);
Dianne Hackborna8f60182009-09-01 19:01:50 -0700550 // The configuration may have changed because
551 // of this device.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 configChanged = true;
Dianne Hackborna8f60182009-09-01 19:01:50 -0700553 } else if ((di=mIgnoredDevices.get(ev.deviceId)) != null) {
554 mIgnoredDevices.remove(ev.deviceId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 } else {
Dianne Hackborna8f60182009-09-01 19:01:50 -0700556 Log.w(TAG, "Removing bad device id: "
557 + Integer.toHexString(ev.deviceId));
558 continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 }
560 }
561 } else {
562 di = getInputDevice(ev.deviceId);
Dianne Hackborna8f60182009-09-01 19:01:50 -0700563 if (di == null) {
564 // This may be some junk from an ignored device.
565 continue;
566 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567
568 // first crack at it
569 send = preprocessEvent(di, ev);
570
571 if (ev.type == RawInputEvent.EV_KEY) {
572 di.mMetaKeysState = makeMetaState(ev.keycode,
573 ev.value != 0, di.mMetaKeysState);
574 mHaveGlobalMetaState = false;
575 }
576 }
577
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 if (configChanged) {
579 synchronized (mFirst) {
Michael Chan53071d62009-05-13 17:29:48 -0700580 addLocked(di, System.nanoTime(), 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 RawInputEvent.CLASS_CONFIGURATION_CHANGED,
582 null);
583 }
584 }
585
586 if (!send) {
587 continue;
588 }
589
590 synchronized (mFirst) {
591 // NOTE: The event timebase absolutely must be the same
592 // timebase as SystemClock.uptimeMillis().
593 //curTime = gotOne ? ev.when : SystemClock.uptimeMillis();
594 final long curTime = SystemClock.uptimeMillis();
Michael Chan53071d62009-05-13 17:29:48 -0700595 final long curTimeNano = System.nanoTime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 //Log.i(TAG, "curTime=" + curTime + ", systemClock=" + SystemClock.uptimeMillis());
597
598 final int classes = di.classes;
599 final int type = ev.type;
600 final int scancode = ev.scancode;
601 send = false;
602
603 // Is it a key event?
604 if (type == RawInputEvent.EV_KEY &&
605 (classes&RawInputEvent.CLASS_KEYBOARD) != 0 &&
606 (scancode < RawInputEvent.BTN_FIRST ||
607 scancode > RawInputEvent.BTN_LAST)) {
608 boolean down;
609 if (ev.value != 0) {
610 down = true;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700611 di.mKeyDownTime = curTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 } else {
613 down = false;
614 }
615 int keycode = rotateKeyCodeLocked(ev.keycode);
Michael Chan53071d62009-05-13 17:29:48 -0700616 addLocked(di, curTimeNano, ev.flags,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 RawInputEvent.CLASS_KEYBOARD,
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700618 newKeyEvent(di, di.mKeyDownTime, curTime, down,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 keycode, 0, scancode,
620 ((ev.flags & WindowManagerPolicy.FLAG_WOKE_HERE) != 0)
621 ? KeyEvent.FLAG_WOKE_HERE : 0));
Dianne Hackborn1411d1c2009-10-12 23:21:18 -0700622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 } else if (ev.type == RawInputEvent.EV_KEY) {
Dianne Hackborn1411d1c2009-10-12 23:21:18 -0700624 // Single touch protocol: touch going down or up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625 if (ev.scancode == RawInputEvent.BTN_TOUCH &&
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700626 (classes&(RawInputEvent.CLASS_TOUCHSCREEN
627 |RawInputEvent.CLASS_TOUCHSCREEN_MT))
628 == RawInputEvent.CLASS_TOUCHSCREEN) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 di.mAbs.changed = true;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700630 di.mAbs.mDown[0] = ev.value != 0;
Dianne Hackborn1411d1c2009-10-12 23:21:18 -0700631
632 // Trackball (mouse) protocol: press down or up.
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700633 } else if (ev.scancode == RawInputEvent.BTN_MOUSE &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 (classes&RawInputEvent.CLASS_TRACKBALL) != 0) {
635 di.mRel.changed = true;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700636 di.mRel.mNextNumPointers = ev.value != 0 ? 1 : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 send = true;
638 }
639
Dianne Hackborn1411d1c2009-10-12 23:21:18 -0700640 // Process position events from multitouch protocol.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 } else if (ev.type == RawInputEvent.EV_ABS &&
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700642 (classes&RawInputEvent.CLASS_TOUCHSCREEN_MT) != 0) {
643 if (ev.scancode == RawInputEvent.ABS_MT_TOUCH_MAJOR) {
644 di.mAbs.changed = true;
645 di.mAbs.mNextData[di.mAbs.mAddingPointerOffset
646 + MotionEvent.SAMPLE_PRESSURE] = ev.value;
647 } else if (ev.scancode == RawInputEvent.ABS_MT_POSITION_X) {
648 di.mAbs.changed = true;
649 di.mAbs.mNextData[di.mAbs.mAddingPointerOffset
650 + MotionEvent.SAMPLE_X] = ev.value;
651 if (DEBUG_POINTERS) Log.v(TAG, "MT @"
652 + di.mAbs.mAddingPointerOffset
653 + " X:" + ev.value);
654 } else if (ev.scancode == RawInputEvent.ABS_MT_POSITION_Y) {
655 di.mAbs.changed = true;
656 di.mAbs.mNextData[di.mAbs.mAddingPointerOffset
657 + MotionEvent.SAMPLE_Y] = ev.value;
658 if (DEBUG_POINTERS) Log.v(TAG, "MT @"
659 + di.mAbs.mAddingPointerOffset
660 + " Y:" + ev.value);
661 } else if (ev.scancode == RawInputEvent.ABS_MT_WIDTH_MAJOR) {
662 di.mAbs.changed = true;
663 di.mAbs.mNextData[di.mAbs.mAddingPointerOffset
664 + MotionEvent.SAMPLE_SIZE] = ev.value;
665 }
Dianne Hackborn1411d1c2009-10-12 23:21:18 -0700666
667 // Process position events from single touch protocol.
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700668 } else if (ev.type == RawInputEvent.EV_ABS &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 (classes&RawInputEvent.CLASS_TOUCHSCREEN) != 0) {
670 if (ev.scancode == RawInputEvent.ABS_X) {
671 di.mAbs.changed = true;
Dianne Hackborn2a2b34432009-08-12 17:13:55 -0700672 di.curTouchVals[MotionEvent.SAMPLE_X] = ev.value;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 } else if (ev.scancode == RawInputEvent.ABS_Y) {
674 di.mAbs.changed = true;
Dianne Hackborn2a2b34432009-08-12 17:13:55 -0700675 di.curTouchVals[MotionEvent.SAMPLE_Y] = ev.value;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 } else if (ev.scancode == RawInputEvent.ABS_PRESSURE) {
677 di.mAbs.changed = true;
Dianne Hackborn2a2b34432009-08-12 17:13:55 -0700678 di.curTouchVals[MotionEvent.SAMPLE_PRESSURE] = ev.value;
679 di.curTouchVals[MotionEvent.NUM_SAMPLE_DATA
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700680 + MotionEvent.SAMPLE_PRESSURE] = ev.value;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 } else if (ev.scancode == RawInputEvent.ABS_TOOL_WIDTH) {
682 di.mAbs.changed = true;
Dianne Hackborn2a2b34432009-08-12 17:13:55 -0700683 di.curTouchVals[MotionEvent.SAMPLE_SIZE] = ev.value;
684 di.curTouchVals[MotionEvent.NUM_SAMPLE_DATA
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700685 + MotionEvent.SAMPLE_SIZE] = ev.value;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 }
687
Dianne Hackborn1411d1c2009-10-12 23:21:18 -0700688 // Process movement events from trackball (mouse) protocol.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 } else if (ev.type == RawInputEvent.EV_REL &&
690 (classes&RawInputEvent.CLASS_TRACKBALL) != 0) {
691 // Add this relative movement into our totals.
692 if (ev.scancode == RawInputEvent.REL_X) {
693 di.mRel.changed = true;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700694 di.mRel.mNextData[MotionEvent.SAMPLE_X] += ev.value;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 } else if (ev.scancode == RawInputEvent.REL_Y) {
696 di.mRel.changed = true;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700697 di.mRel.mNextData[MotionEvent.SAMPLE_Y] += ev.value;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 }
699 }
700
Dianne Hackborn1411d1c2009-10-12 23:21:18 -0700701 // Handle multitouch protocol sync: tells us that the
702 // driver has returned all data for -one- of the pointers
703 // that is currently down.
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700704 if (ev.type == RawInputEvent.EV_SYN
705 && ev.scancode == RawInputEvent.SYN_MT_REPORT
706 && di.mAbs != null) {
707 di.mAbs.changed = true;
708 if (di.mAbs.mNextData[MotionEvent.SAMPLE_PRESSURE] > 0) {
709 // If the value is <= 0, the pointer is not
710 // down, so keep it in the count.
711
712 if (di.mAbs.mNextData[di.mAbs.mAddingPointerOffset
713 + MotionEvent.SAMPLE_PRESSURE] != 0) {
714 final int num = di.mAbs.mNextNumPointers+1;
715 di.mAbs.mNextNumPointers = num;
716 if (DEBUG_POINTERS) Log.v(TAG,
717 "MT_REPORT: now have " + num + " pointers");
718 final int newOffset = (num <= InputDevice.MAX_POINTERS)
719 ? (num * MotionEvent.NUM_SAMPLE_DATA)
720 : (InputDevice.MAX_POINTERS *
721 MotionEvent.NUM_SAMPLE_DATA);
722 di.mAbs.mAddingPointerOffset = newOffset;
723 di.mAbs.mNextData[newOffset
724 + MotionEvent.SAMPLE_PRESSURE] = 0;
725 } else {
726 if (DEBUG_POINTERS) Log.v(TAG, "MT_REPORT: no pointer");
727 }
728 }
Dianne Hackborn1411d1c2009-10-12 23:21:18 -0700729
730 // Handle general event sync: all data for the current
731 // event update has been delivered.
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700732 } else if (send || (ev.type == RawInputEvent.EV_SYN
733 && ev.scancode == RawInputEvent.SYN_REPORT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734 if (mDisplay != null) {
735 if (!mHaveGlobalMetaState) {
736 computeGlobalMetaStateLocked();
737 }
738
739 MotionEvent me;
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700740
741 InputDevice.MotionState ms = di.mAbs;
742 if (ms.changed) {
743 ms.changed = false;
744
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700745 if ((classes&(RawInputEvent.CLASS_TOUCHSCREEN
746 |RawInputEvent.CLASS_TOUCHSCREEN_MT))
747 == RawInputEvent.CLASS_TOUCHSCREEN) {
748 ms.mNextNumPointers = 0;
Dianne Hackborn2a2b34432009-08-12 17:13:55 -0700749 if (ms.mDown[0]) {
750 System.arraycopy(di.curTouchVals, 0,
751 ms.mNextData, 0,
752 MotionEvent.NUM_SAMPLE_DATA);
753 ms.mNextNumPointers++;
754 }
Dianne Hackborn1411d1c2009-10-12 23:21:18 -0700755 }
756
757 if (BAD_TOUCH_HACK) {
758 ms.dropBadPoint(di);
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700759 }
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700760
761 boolean doMotion = !monitorVirtualKey(di,
762 ev, curTime, curTimeNano);
763
764 if (doMotion && ms.mNextNumPointers > 0
Dianne Hackborndc953722009-10-19 11:24:39 -0700765 && (ms.mLastNumPointers == 0
766 || ms.mSkipLastPointers)) {
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700767 doMotion = !generateVirtualKeyDown(di,
768 ev, curTime, curTimeNano);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769 }
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700770
771 if (doMotion) {
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700772 // XXX Need to be able to generate
773 // multiple events here, for example
774 // if two fingers change up/down state
775 // at the same time.
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700776 do {
777 me = ms.generateAbsMotion(di, curTime,
778 curTimeNano, mDisplay,
779 mOrientation, mGlobalMetaState);
Dianne Hackborndc953722009-10-19 11:24:39 -0700780 if (DEBUG_POINTERS) Log.v(TAG, "Absolute: x="
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700781 + di.mAbs.mNextData[MotionEvent.SAMPLE_X]
782 + " y="
783 + di.mAbs.mNextData[MotionEvent.SAMPLE_Y]
784 + " ev=" + me);
785 if (me != null) {
786 if (WindowManagerPolicy.WATCH_POINTER) {
787 Log.i(TAG, "Enqueueing: " + me);
788 }
789 addLocked(di, curTimeNano, ev.flags,
790 RawInputEvent.CLASS_TOUCHSCREEN, me);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700791 }
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700792 } while (ms.hasMore());
Dianne Hackborn1411d1c2009-10-12 23:21:18 -0700793 } else {
794 // We are consuming movement in the
795 // virtual key area... but still
796 // propagate this to the previous
797 // data for comparisons.
Dianne Hackbornf2ddfb12009-10-13 22:43:33 -0700798 int num = ms.mNextNumPointers;
799 if (num > InputDevice.MAX_POINTERS) {
800 num = InputDevice.MAX_POINTERS;
801 }
Dianne Hackborn1411d1c2009-10-12 23:21:18 -0700802 System.arraycopy(ms.mNextData, 0,
803 ms.mLastData, 0,
Dianne Hackbornf2ddfb12009-10-13 22:43:33 -0700804 num * MotionEvent.NUM_SAMPLE_DATA);
805 ms.mLastNumPointers = num;
Dianne Hackborndc953722009-10-19 11:24:39 -0700806 ms.mSkipLastPointers = true;
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700807 }
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700808
809 ms.finish();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 }
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700811
812 ms = di.mRel;
813 if (ms.changed) {
814 ms.changed = false;
815
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700816 me = ms.generateRelMotion(di, curTime,
817 curTimeNano,
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700818 mOrientation, mGlobalMetaState);
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700819 if (false) Log.v(TAG, "Relative: x="
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700820 + di.mRel.mNextData[MotionEvent.SAMPLE_X]
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700821 + " y="
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700822 + di.mRel.mNextData[MotionEvent.SAMPLE_Y]
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700823 + " ev=" + me);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700824 if (me != null) {
825 addLocked(di, curTimeNano, ev.flags,
826 RawInputEvent.CLASS_TRACKBALL, me);
827 }
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700828
829 ms.finish();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800830 }
831 }
832 }
833 }
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700834
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700835 } catch (RuntimeException exc) {
836 Log.e(TAG, "InputReaderThread uncaught exception", exc);
837 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838 }
839 }
840 };
841
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700842 private boolean isInsideDisplay(InputDevice dev) {
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700843 final InputDevice.AbsoluteInfo absx = dev.absX;
844 final InputDevice.AbsoluteInfo absy = dev.absY;
845 final InputDevice.MotionState absm = dev.mAbs;
846 if (absx == null || absy == null || absm == null) {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700847 return true;
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700848 }
849
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700850 if (absm.mNextData[MotionEvent.SAMPLE_X] >= absx.minValue
851 && absm.mNextData[MotionEvent.SAMPLE_X] <= absx.maxValue
852 && absm.mNextData[MotionEvent.SAMPLE_Y] >= absy.minValue
853 && absm.mNextData[MotionEvent.SAMPLE_Y] <= absy.maxValue) {
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700854 if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "Input ("
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700855 + absm.mNextData[MotionEvent.SAMPLE_X]
856 + "," + absm.mNextData[MotionEvent.SAMPLE_Y]
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700857 + ") inside of display");
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700858 return true;
859 }
860
861 return false;
862 }
863
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700864 private VirtualKey findVirtualKey(InputDevice dev) {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700865 final int N = mVirtualKeys.size();
866 if (N <= 0) {
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700867 return null;
868 }
869
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700870 final InputDevice.MotionState absm = dev.mAbs;
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700871 for (int i=0; i<N; i++) {
872 VirtualKey sb = mVirtualKeys.get(i);
873 sb.computeHitRect(dev, mDisplayWidth, mDisplayHeight);
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700874 if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "Hit test ("
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700875 + absm.mNextData[MotionEvent.SAMPLE_X] + ","
876 + absm.mNextData[MotionEvent.SAMPLE_Y] + ") in code "
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700877 + sb.scancode + " - (" + sb.hitLeft
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700878 + "," + sb.hitTop + ")-(" + sb.hitRight + ","
879 + sb.hitBottom + ")");
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700880 if (sb.checkHit(absm.mNextData[MotionEvent.SAMPLE_X],
881 absm.mNextData[MotionEvent.SAMPLE_Y])) {
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700882 if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "Hit!");
883 return sb;
884 }
885 }
886
887 return null;
888 }
889
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700890 private boolean generateVirtualKeyDown(InputDevice di, RawInputEvent ev,
891 long curTime, long curTimeNano) {
892 if (isInsideDisplay(di)) {
893 // Didn't consume event.
894 return false;
895 }
896
897
898 VirtualKey vk = findVirtualKey(di);
899 if (vk != null) {
900 final InputDevice.MotionState ms = di.mAbs;
901 mPressedVirtualKey = vk;
902 vk.lastKeycode = scancodeToKeycode(di.id, vk.scancode);
903 ms.mLastNumPointers = ms.mNextNumPointers;
904 di.mKeyDownTime = curTime;
905 if (DEBUG_VIRTUAL_KEYS) Log.v(TAG,
906 "Generate key down for: " + vk.scancode
907 + " (keycode=" + vk.lastKeycode + ")");
908 KeyEvent event = newKeyEvent(di, di.mKeyDownTime, curTime, true,
909 vk.lastKeycode, 0, vk.scancode,
910 KeyEvent.FLAG_VIRTUAL_HARD_KEY);
911 mHapticFeedbackCallback.virtualKeyFeedback(event);
912 addLocked(di, curTimeNano, ev.flags, RawInputEvent.CLASS_KEYBOARD,
913 event);
914 }
915
916 // We always consume the event, even if we didn't
917 // generate a key event. There are two reasons for
918 // this: to avoid spurious touches when holding
919 // the edges of the device near the touchscreen,
920 // and to avoid reporting events if there are virtual
921 // keys on the touchscreen outside of the display
922 // area.
923 // Note that for all of this we are only looking at the
924 // first pointer, since what we are handling here is the
925 // first pointer going down, and this is the coordinate
926 // that will be used to dispatch the event.
927 if (false) {
928 final InputDevice.AbsoluteInfo absx = di.absX;
929 final InputDevice.AbsoluteInfo absy = di.absY;
930 final InputDevice.MotionState absm = di.mAbs;
931 Log.v(TAG, "Rejecting ("
932 + absm.mNextData[MotionEvent.SAMPLE_X] + ","
933 + absm.mNextData[MotionEvent.SAMPLE_Y] + "): outside of ("
934 + absx.minValue + "," + absy.minValue
935 + ")-(" + absx.maxValue + ","
936 + absx.maxValue + ")");
937 }
938 return true;
939 }
940
941 private boolean monitorVirtualKey(InputDevice di, RawInputEvent ev,
942 long curTime, long curTimeNano) {
943 VirtualKey vk = mPressedVirtualKey;
944 if (vk == null) {
945 return false;
946 }
947
948 final InputDevice.MotionState ms = di.mAbs;
949 if (ms.mNextNumPointers <= 0) {
950 mPressedVirtualKey = null;
951 ms.mLastNumPointers = 0;
952 if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "Generate key up for: " + vk.scancode);
953 KeyEvent event = newKeyEvent(di, di.mKeyDownTime, curTime, false,
954 vk.lastKeycode, 0, vk.scancode,
955 KeyEvent.FLAG_VIRTUAL_HARD_KEY);
956 mHapticFeedbackCallback.virtualKeyFeedback(event);
957 addLocked(di, curTimeNano, ev.flags, RawInputEvent.CLASS_KEYBOARD,
958 event);
959 return true;
960
961 } else if (isInsideDisplay(di)) {
962 // Whoops the pointer has moved into
963 // the display area! Cancel the
964 // virtual key and start a pointer
965 // motion.
966 mPressedVirtualKey = null;
967 if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "Cancel key up for: " + vk.scancode);
968 KeyEvent event = newKeyEvent(di, di.mKeyDownTime, curTime, false,
969 vk.lastKeycode, 0, vk.scancode,
970 KeyEvent.FLAG_CANCELED | KeyEvent.FLAG_VIRTUAL_HARD_KEY);
971 mHapticFeedbackCallback.virtualKeyFeedback(event);
972 addLocked(di, curTimeNano, ev.flags, RawInputEvent.CLASS_KEYBOARD,
973 event);
974 ms.mLastNumPointers = 0;
975 return false;
976 }
977
978 return true;
979 }
980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 /**
982 * Returns a new meta state for the given keys and old state.
983 */
984 private static final int makeMetaState(int keycode, boolean down, int old) {
985 int mask;
986 switch (keycode) {
987 case KeyEvent.KEYCODE_ALT_LEFT:
988 mask = KeyEvent.META_ALT_LEFT_ON;
989 break;
990 case KeyEvent.KEYCODE_ALT_RIGHT:
991 mask = KeyEvent.META_ALT_RIGHT_ON;
992 break;
993 case KeyEvent.KEYCODE_SHIFT_LEFT:
994 mask = KeyEvent.META_SHIFT_LEFT_ON;
995 break;
996 case KeyEvent.KEYCODE_SHIFT_RIGHT:
997 mask = KeyEvent.META_SHIFT_RIGHT_ON;
998 break;
999 case KeyEvent.KEYCODE_SYM:
1000 mask = KeyEvent.META_SYM_ON;
1001 break;
1002 default:
1003 return old;
1004 }
1005 int result = ~(KeyEvent.META_ALT_ON | KeyEvent.META_SHIFT_ON)
1006 & (down ? (old | mask) : (old & ~mask));
1007 if (0 != (result & (KeyEvent.META_ALT_LEFT_ON | KeyEvent.META_ALT_RIGHT_ON))) {
1008 result |= KeyEvent.META_ALT_ON;
1009 }
1010 if (0 != (result & (KeyEvent.META_SHIFT_LEFT_ON | KeyEvent.META_SHIFT_RIGHT_ON))) {
1011 result |= KeyEvent.META_SHIFT_ON;
1012 }
1013 return result;
1014 }
1015
1016 private void computeGlobalMetaStateLocked() {
1017 int i = mDevices.size();
1018 mGlobalMetaState = 0;
1019 while ((--i) >= 0) {
1020 mGlobalMetaState |= mDevices.valueAt(i).mMetaKeysState;
1021 }
1022 mHaveGlobalMetaState = true;
1023 }
1024
1025 /*
1026 * Return true if you want the event to get passed on to the
1027 * rest of the system, and false if you've handled it and want
1028 * it dropped.
1029 */
1030 abstract boolean preprocessEvent(InputDevice device, RawInputEvent event);
1031
1032 InputDevice getInputDevice(int deviceId) {
1033 synchronized (mFirst) {
1034 return getInputDeviceLocked(deviceId);
1035 }
1036 }
1037
1038 private InputDevice getInputDeviceLocked(int deviceId) {
1039 return mDevices.get(deviceId);
1040 }
1041
1042 public void setOrientation(int orientation) {
1043 synchronized(mFirst) {
1044 mOrientation = orientation;
1045 switch (orientation) {
1046 case Surface.ROTATION_90:
1047 mKeyRotationMap = KEY_90_MAP;
1048 break;
1049 case Surface.ROTATION_180:
1050 mKeyRotationMap = KEY_180_MAP;
1051 break;
1052 case Surface.ROTATION_270:
1053 mKeyRotationMap = KEY_270_MAP;
1054 break;
1055 default:
1056 mKeyRotationMap = null;
1057 break;
1058 }
1059 }
1060 }
1061
1062 public int rotateKeyCode(int keyCode) {
1063 synchronized(mFirst) {
1064 return rotateKeyCodeLocked(keyCode);
1065 }
1066 }
1067
1068 private int rotateKeyCodeLocked(int keyCode) {
1069 int[] map = mKeyRotationMap;
1070 if (map != null) {
1071 final int N = map.length;
1072 for (int i=0; i<N; i+=2) {
1073 if (map[i] == keyCode) {
1074 return map[i+1];
1075 }
1076 }
1077 }
1078 return keyCode;
1079 }
1080
1081 boolean hasEvents() {
1082 synchronized (mFirst) {
1083 return mFirst.next != mLast;
1084 }
1085 }
1086
1087 /*
1088 * returns true if we returned an event, and false if we timed out
1089 */
1090 QueuedEvent getEvent(long timeoutMS) {
1091 long begin = SystemClock.uptimeMillis();
1092 final long end = begin+timeoutMS;
1093 long now = begin;
1094 synchronized (mFirst) {
1095 while (mFirst.next == mLast && end > now) {
1096 try {
1097 mWakeLock.release();
1098 mFirst.wait(end-now);
1099 }
1100 catch (InterruptedException e) {
1101 }
1102 now = SystemClock.uptimeMillis();
1103 if (begin > now) {
1104 begin = now;
1105 }
1106 }
1107 if (mFirst.next == mLast) {
1108 return null;
1109 }
1110 QueuedEvent p = mFirst.next;
1111 mFirst.next = p.next;
1112 mFirst.next.prev = mFirst;
1113 p.inQueue = false;
1114 return p;
1115 }
1116 }
1117
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001118 /**
1119 * Return true if the queue has an up event pending that corresponds
1120 * to the same key as the given key event.
1121 */
1122 boolean hasKeyUpEvent(KeyEvent origEvent) {
1123 synchronized (mFirst) {
1124 final int keyCode = origEvent.getKeyCode();
1125 QueuedEvent cur = mLast.prev;
1126 while (cur.prev != null) {
1127 if (cur.classType == RawInputEvent.CLASS_KEYBOARD) {
1128 KeyEvent ke = (KeyEvent)cur.event;
1129 if (ke.getAction() == KeyEvent.ACTION_UP
1130 && ke.getKeyCode() == keyCode) {
1131 return true;
1132 }
1133 }
1134 cur = cur.prev;
1135 }
1136 }
1137
1138 return false;
1139 }
1140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 void recycleEvent(QueuedEvent ev) {
1142 synchronized (mFirst) {
1143 //Log.i(TAG, "Recycle event: " + ev);
1144 if (ev.event == ev.inputDevice.mAbs.currentMove) {
1145 ev.inputDevice.mAbs.currentMove = null;
1146 }
1147 if (ev.event == ev.inputDevice.mRel.currentMove) {
1148 if (false) Log.i(TAG, "Detach rel " + ev.event);
1149 ev.inputDevice.mRel.currentMove = null;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001150 ev.inputDevice.mRel.mNextData[MotionEvent.SAMPLE_X] = 0;
1151 ev.inputDevice.mRel.mNextData[MotionEvent.SAMPLE_Y] = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 }
1153 recycleLocked(ev);
1154 }
1155 }
1156
1157 void filterQueue(FilterCallback cb) {
1158 synchronized (mFirst) {
1159 QueuedEvent cur = mLast.prev;
1160 while (cur.prev != null) {
1161 switch (cb.filterEvent(cur)) {
1162 case FILTER_REMOVE:
1163 cur.prev.next = cur.next;
1164 cur.next.prev = cur.prev;
1165 break;
1166 case FILTER_ABORT:
1167 return;
1168 }
1169 cur = cur.prev;
1170 }
1171 }
1172 }
1173
Michael Chan53071d62009-05-13 17:29:48 -07001174 private QueuedEvent obtainLocked(InputDevice device, long whenNano,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 int flags, int classType, Object event) {
1176 QueuedEvent ev;
1177 if (mCacheCount == 0) {
1178 ev = new QueuedEvent();
1179 } else {
1180 ev = mCache;
1181 ev.inQueue = false;
1182 mCache = ev.next;
1183 mCacheCount--;
1184 }
1185 ev.inputDevice = device;
Michael Chan53071d62009-05-13 17:29:48 -07001186 ev.whenNano = whenNano;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 ev.flags = flags;
1188 ev.classType = classType;
1189 ev.event = event;
1190 return ev;
1191 }
1192
1193 private void recycleLocked(QueuedEvent ev) {
1194 if (ev.inQueue) {
1195 throw new RuntimeException("Event already in queue!");
1196 }
1197 if (mCacheCount < 10) {
1198 mCacheCount++;
1199 ev.next = mCache;
1200 mCache = ev;
1201 ev.inQueue = true;
1202 }
1203 }
1204
Michael Chan53071d62009-05-13 17:29:48 -07001205 private void addLocked(InputDevice device, long whenNano, int flags,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 int classType, Object event) {
1207 boolean poke = mFirst.next == mLast;
1208
Michael Chan53071d62009-05-13 17:29:48 -07001209 QueuedEvent ev = obtainLocked(device, whenNano, flags, classType, event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 QueuedEvent p = mLast.prev;
Michael Chan53071d62009-05-13 17:29:48 -07001211 while (p != mFirst && ev.whenNano < p.whenNano) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 p = p.prev;
1213 }
1214
1215 ev.next = p.next;
1216 ev.prev = p;
1217 p.next = ev;
1218 ev.next.prev = ev;
1219 ev.inQueue = true;
1220
1221 if (poke) {
Michael Chan53071d62009-05-13 17:29:48 -07001222 long time;
1223 if (MEASURE_LATENCY) {
1224 time = System.nanoTime();
1225 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 mFirst.notify();
1227 mWakeLock.acquire();
Michael Chan53071d62009-05-13 17:29:48 -07001228 if (MEASURE_LATENCY) {
1229 lt.sample("1 addLocked-queued event ", System.nanoTime() - time);
1230 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 }
1232 }
1233
1234 private InputDevice newInputDevice(int deviceId) {
1235 int classes = getDeviceClasses(deviceId);
1236 String name = getDeviceName(deviceId);
Dianne Hackborna8f60182009-09-01 19:01:50 -07001237 InputDevice.AbsoluteInfo absX = null;
1238 InputDevice.AbsoluteInfo absY = null;
1239 InputDevice.AbsoluteInfo absPressure = null;
1240 InputDevice.AbsoluteInfo absSize = null;
1241 if (classes != 0) {
1242 Log.i(TAG, "Device added: id=0x" + Integer.toHexString(deviceId)
1243 + ", name=" + name
1244 + ", classes=" + Integer.toHexString(classes));
1245 if ((classes&RawInputEvent.CLASS_TOUCHSCREEN_MT) != 0) {
1246 absX = loadAbsoluteInfo(deviceId,
1247 RawInputEvent.ABS_MT_POSITION_X, "X");
1248 absY = loadAbsoluteInfo(deviceId,
1249 RawInputEvent.ABS_MT_POSITION_Y, "Y");
1250 absPressure = loadAbsoluteInfo(deviceId,
1251 RawInputEvent.ABS_MT_TOUCH_MAJOR, "Pressure");
1252 absSize = loadAbsoluteInfo(deviceId,
1253 RawInputEvent.ABS_MT_WIDTH_MAJOR, "Size");
1254 } else if ((classes&RawInputEvent.CLASS_TOUCHSCREEN) != 0) {
1255 absX = loadAbsoluteInfo(deviceId,
1256 RawInputEvent.ABS_X, "X");
1257 absY = loadAbsoluteInfo(deviceId,
1258 RawInputEvent.ABS_Y, "Y");
1259 absPressure = loadAbsoluteInfo(deviceId,
1260 RawInputEvent.ABS_PRESSURE, "Pressure");
1261 absSize = loadAbsoluteInfo(deviceId,
1262 RawInputEvent.ABS_TOOL_WIDTH, "Size");
1263 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 }
1265
1266 return new InputDevice(deviceId, classes, name, absX, absY, absPressure, absSize);
1267 }
1268
1269 private InputDevice.AbsoluteInfo loadAbsoluteInfo(int id, int channel,
1270 String name) {
1271 InputDevice.AbsoluteInfo info = new InputDevice.AbsoluteInfo();
1272 if (getAbsoluteInfo(id, channel, info)
1273 && info.minValue != info.maxValue) {
1274 Log.i(TAG, " " + name + ": min=" + info.minValue
1275 + " max=" + info.maxValue
1276 + " flat=" + info.flat
1277 + " fuzz=" + info.fuzz);
1278 info.range = info.maxValue-info.minValue;
1279 return info;
1280 }
1281 Log.i(TAG, " " + name + ": unknown values");
1282 return null;
1283 }
1284 private static native boolean readEvent(RawInputEvent outEvent);
1285}