blob: a885df8c632ec54b820b3a20bfef82950c7a8e53 [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 Hackborn65cb6052009-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 Hackborn65cb6052009-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;
302
303 mThread.start();
304 }
305
306 public void setDisplay(Display display) {
307 mDisplay = display;
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700308
309 // We assume at this point that the display dimensions reflect the
310 // natural, unrotated display. We will perform hit tests for soft
311 // buttons based on that display.
312 mDisplayWidth = display.getWidth();
313 mDisplayHeight = display.getHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 }
315
316 public void getInputConfiguration(Configuration config) {
317 synchronized (mFirst) {
318 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
319 config.keyboard = Configuration.KEYBOARD_NOKEYS;
320 config.navigation = Configuration.NAVIGATION_NONAV;
321
322 final int N = mDevices.size();
323 for (int i=0; i<N; i++) {
324 InputDevice d = mDevices.valueAt(i);
325 if (d != null) {
326 if ((d.classes&RawInputEvent.CLASS_TOUCHSCREEN) != 0) {
327 config.touchscreen
328 = Configuration.TOUCHSCREEN_FINGER;
329 //Log.i("foo", "***** HAVE TOUCHSCREEN!");
330 }
331 if ((d.classes&RawInputEvent.CLASS_ALPHAKEY) != 0) {
332 config.keyboard
333 = Configuration.KEYBOARD_QWERTY;
334 //Log.i("foo", "***** HAVE QWERTY!");
335 }
336 if ((d.classes&RawInputEvent.CLASS_TRACKBALL) != 0) {
337 config.navigation
338 = Configuration.NAVIGATION_TRACKBALL;
339 //Log.i("foo", "***** HAVE TRACKBALL!");
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700340 } else if ((d.classes&RawInputEvent.CLASS_DPAD) != 0) {
341 config.navigation
342 = Configuration.NAVIGATION_DPAD;
343 //Log.i("foo", "***** HAVE DPAD!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 }
345 }
346 }
347 }
348 }
349
Dianne Hackborn6af0d502009-09-28 13:25:46 -0700350 public int getScancodeState(int code) {
351 synchronized (mFirst) {
352 VirtualKey vk = mPressedVirtualKey;
353 if (vk != null) {
354 if (vk.scancode == code) {
355 return 2;
356 }
357 }
358 return nativeGetScancodeState(code);
359 }
360 }
361
362 public int getScancodeState(int deviceId, int code) {
363 synchronized (mFirst) {
364 VirtualKey vk = mPressedVirtualKey;
365 if (vk != null) {
366 if (vk.scancode == code) {
367 return 2;
368 }
369 }
370 return nativeGetScancodeState(deviceId, code);
371 }
372 }
373
374 public int getKeycodeState(int code) {
375 synchronized (mFirst) {
376 VirtualKey vk = mPressedVirtualKey;
377 if (vk != null) {
378 if (vk.lastKeycode == code) {
379 return 2;
380 }
381 }
382 return nativeGetKeycodeState(code);
383 }
384 }
385
386 public int getKeycodeState(int deviceId, int code) {
387 synchronized (mFirst) {
388 VirtualKey vk = mPressedVirtualKey;
389 if (vk != null) {
390 if (vk.lastKeycode == code) {
391 return 2;
392 }
393 }
394 return nativeGetKeycodeState(deviceId, code);
395 }
396 }
397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 public static native String getDeviceName(int deviceId);
399 public static native int getDeviceClasses(int deviceId);
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400400 public static native void addExcludedDevice(String deviceName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 public static native boolean getAbsoluteInfo(int deviceId, int axis,
402 InputDevice.AbsoluteInfo outInfo);
403 public static native int getSwitchState(int sw);
404 public static native int getSwitchState(int deviceId, int sw);
Dianne Hackborn6af0d502009-09-28 13:25:46 -0700405 public static native int nativeGetScancodeState(int code);
406 public static native int nativeGetScancodeState(int deviceId, int code);
407 public static native int nativeGetKeycodeState(int code);
408 public static native int nativeGetKeycodeState(int deviceId, int code);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700409 public static native int scancodeToKeycode(int deviceId, int scancode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 public static native boolean hasKeys(int[] keycodes, boolean[] keyExists);
411
412 public static KeyEvent newKeyEvent(InputDevice device, long downTime,
413 long eventTime, boolean down, int keycode, int repeatCount,
414 int scancode, int flags) {
415 return new KeyEvent(
416 downTime, eventTime,
417 down ? KeyEvent.ACTION_DOWN : KeyEvent.ACTION_UP,
418 keycode, repeatCount,
419 device != null ? device.mMetaKeysState : 0,
420 device != null ? device.id : -1, scancode,
The Android Open Source Project10592532009-03-18 17:39:46 -0700421 flags | KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 }
423
424 Thread mThread = new Thread("InputDeviceReader") {
425 public void run() {
Dianne Hackbornbd0a81f2009-10-04 13:30:50 -0700426 if (DEBUG) Log.v(TAG, "InputDeviceReader.run()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 android.os.Process.setThreadPriority(
428 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
429
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700430 RawInputEvent ev = new RawInputEvent();
431 while (true) {
432 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 InputDevice di;
434
435 // block, doesn't release the monitor
436 readEvent(ev);
437
438 boolean send = false;
439 boolean configChanged = false;
440
441 if (false) {
442 Log.i(TAG, "Input event: dev=0x"
443 + Integer.toHexString(ev.deviceId)
444 + " type=0x" + Integer.toHexString(ev.type)
445 + " scancode=" + ev.scancode
446 + " keycode=" + ev.keycode
447 + " value=" + ev.value);
448 }
449
450 if (ev.type == RawInputEvent.EV_DEVICE_ADDED) {
451 synchronized (mFirst) {
452 di = newInputDevice(ev.deviceId);
Dianne Hackborna8f60182009-09-01 19:01:50 -0700453 if (di.classes != 0) {
454 // If this device is some kind of input class,
455 // we care about it.
456 mDevices.put(ev.deviceId, di);
457 if ((di.classes & RawInputEvent.CLASS_TOUCHSCREEN) != 0) {
458 readVirtualKeys(di.name);
459 }
460 // The configuration may have changed because
461 // of this device.
462 configChanged = true;
463 } else {
464 // We won't do anything with this device.
465 mIgnoredDevices.put(ev.deviceId, di);
466 Log.i(TAG, "Ignoring non-input device: id=0x"
467 + Integer.toHexString(di.id)
468 + ", name=" + di.name);
Iliyan Malchev75b2aed2009-08-06 14:50:57 -0700469 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 }
471 } else if (ev.type == RawInputEvent.EV_DEVICE_REMOVED) {
472 synchronized (mFirst) {
Dianne Hackborna8f60182009-09-01 19:01:50 -0700473 if (false) {
474 Log.i(TAG, "Device removed: id=0x"
475 + Integer.toHexString(ev.deviceId));
476 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 di = mDevices.get(ev.deviceId);
478 if (di != null) {
479 mDevices.delete(ev.deviceId);
Dianne Hackborna8f60182009-09-01 19:01:50 -0700480 // The configuration may have changed because
481 // of this device.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 configChanged = true;
Dianne Hackborna8f60182009-09-01 19:01:50 -0700483 } else if ((di=mIgnoredDevices.get(ev.deviceId)) != null) {
484 mIgnoredDevices.remove(ev.deviceId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 } else {
Dianne Hackborna8f60182009-09-01 19:01:50 -0700486 Log.w(TAG, "Removing bad device id: "
487 + Integer.toHexString(ev.deviceId));
488 continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489 }
490 }
491 } else {
492 di = getInputDevice(ev.deviceId);
Dianne Hackborna8f60182009-09-01 19:01:50 -0700493 if (di == null) {
494 // This may be some junk from an ignored device.
495 continue;
496 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497
498 // first crack at it
499 send = preprocessEvent(di, ev);
500
501 if (ev.type == RawInputEvent.EV_KEY) {
502 di.mMetaKeysState = makeMetaState(ev.keycode,
503 ev.value != 0, di.mMetaKeysState);
504 mHaveGlobalMetaState = false;
505 }
506 }
507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 if (configChanged) {
509 synchronized (mFirst) {
Michael Chan53071d62009-05-13 17:29:48 -0700510 addLocked(di, System.nanoTime(), 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 RawInputEvent.CLASS_CONFIGURATION_CHANGED,
512 null);
513 }
514 }
515
516 if (!send) {
517 continue;
518 }
519
520 synchronized (mFirst) {
521 // NOTE: The event timebase absolutely must be the same
522 // timebase as SystemClock.uptimeMillis().
523 //curTime = gotOne ? ev.when : SystemClock.uptimeMillis();
524 final long curTime = SystemClock.uptimeMillis();
Michael Chan53071d62009-05-13 17:29:48 -0700525 final long curTimeNano = System.nanoTime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 //Log.i(TAG, "curTime=" + curTime + ", systemClock=" + SystemClock.uptimeMillis());
527
528 final int classes = di.classes;
529 final int type = ev.type;
530 final int scancode = ev.scancode;
531 send = false;
532
533 // Is it a key event?
534 if (type == RawInputEvent.EV_KEY &&
535 (classes&RawInputEvent.CLASS_KEYBOARD) != 0 &&
536 (scancode < RawInputEvent.BTN_FIRST ||
537 scancode > RawInputEvent.BTN_LAST)) {
538 boolean down;
539 if (ev.value != 0) {
540 down = true;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700541 di.mKeyDownTime = curTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542 } else {
543 down = false;
544 }
545 int keycode = rotateKeyCodeLocked(ev.keycode);
Michael Chan53071d62009-05-13 17:29:48 -0700546 addLocked(di, curTimeNano, ev.flags,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 RawInputEvent.CLASS_KEYBOARD,
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700548 newKeyEvent(di, di.mKeyDownTime, curTime, down,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 keycode, 0, scancode,
550 ((ev.flags & WindowManagerPolicy.FLAG_WOKE_HERE) != 0)
551 ? KeyEvent.FLAG_WOKE_HERE : 0));
Dianne Hackborn1411d1c2009-10-12 23:21:18 -0700552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 } else if (ev.type == RawInputEvent.EV_KEY) {
Dianne Hackborn1411d1c2009-10-12 23:21:18 -0700554 // Single touch protocol: touch going down or up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 if (ev.scancode == RawInputEvent.BTN_TOUCH &&
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700556 (classes&(RawInputEvent.CLASS_TOUCHSCREEN
557 |RawInputEvent.CLASS_TOUCHSCREEN_MT))
558 == RawInputEvent.CLASS_TOUCHSCREEN) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 di.mAbs.changed = true;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700560 di.mAbs.mDown[0] = ev.value != 0;
Dianne Hackborn1411d1c2009-10-12 23:21:18 -0700561
562 // Trackball (mouse) protocol: press down or up.
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700563 } else if (ev.scancode == RawInputEvent.BTN_MOUSE &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 (classes&RawInputEvent.CLASS_TRACKBALL) != 0) {
565 di.mRel.changed = true;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700566 di.mRel.mNextNumPointers = ev.value != 0 ? 1 : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 send = true;
568 }
569
Dianne Hackborn1411d1c2009-10-12 23:21:18 -0700570 // Process position events from multitouch protocol.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 } else if (ev.type == RawInputEvent.EV_ABS &&
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700572 (classes&RawInputEvent.CLASS_TOUCHSCREEN_MT) != 0) {
573 if (ev.scancode == RawInputEvent.ABS_MT_TOUCH_MAJOR) {
574 di.mAbs.changed = true;
575 di.mAbs.mNextData[di.mAbs.mAddingPointerOffset
576 + MotionEvent.SAMPLE_PRESSURE] = ev.value;
577 } else if (ev.scancode == RawInputEvent.ABS_MT_POSITION_X) {
578 di.mAbs.changed = true;
579 di.mAbs.mNextData[di.mAbs.mAddingPointerOffset
580 + MotionEvent.SAMPLE_X] = ev.value;
581 if (DEBUG_POINTERS) Log.v(TAG, "MT @"
582 + di.mAbs.mAddingPointerOffset
583 + " X:" + ev.value);
584 } else if (ev.scancode == RawInputEvent.ABS_MT_POSITION_Y) {
585 di.mAbs.changed = true;
586 di.mAbs.mNextData[di.mAbs.mAddingPointerOffset
587 + MotionEvent.SAMPLE_Y] = ev.value;
588 if (DEBUG_POINTERS) Log.v(TAG, "MT @"
589 + di.mAbs.mAddingPointerOffset
590 + " Y:" + ev.value);
591 } else if (ev.scancode == RawInputEvent.ABS_MT_WIDTH_MAJOR) {
592 di.mAbs.changed = true;
593 di.mAbs.mNextData[di.mAbs.mAddingPointerOffset
594 + MotionEvent.SAMPLE_SIZE] = ev.value;
595 }
Dianne Hackborn1411d1c2009-10-12 23:21:18 -0700596
597 // Process position events from single touch protocol.
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700598 } else if (ev.type == RawInputEvent.EV_ABS &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 (classes&RawInputEvent.CLASS_TOUCHSCREEN) != 0) {
600 if (ev.scancode == RawInputEvent.ABS_X) {
601 di.mAbs.changed = true;
Dianne Hackborn2a2b34432009-08-12 17:13:55 -0700602 di.curTouchVals[MotionEvent.SAMPLE_X] = ev.value;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 } else if (ev.scancode == RawInputEvent.ABS_Y) {
604 di.mAbs.changed = true;
Dianne Hackborn2a2b34432009-08-12 17:13:55 -0700605 di.curTouchVals[MotionEvent.SAMPLE_Y] = ev.value;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 } else if (ev.scancode == RawInputEvent.ABS_PRESSURE) {
607 di.mAbs.changed = true;
Dianne Hackborn2a2b34432009-08-12 17:13:55 -0700608 di.curTouchVals[MotionEvent.SAMPLE_PRESSURE] = ev.value;
609 di.curTouchVals[MotionEvent.NUM_SAMPLE_DATA
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700610 + MotionEvent.SAMPLE_PRESSURE] = ev.value;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 } else if (ev.scancode == RawInputEvent.ABS_TOOL_WIDTH) {
612 di.mAbs.changed = true;
Dianne Hackborn2a2b34432009-08-12 17:13:55 -0700613 di.curTouchVals[MotionEvent.SAMPLE_SIZE] = ev.value;
614 di.curTouchVals[MotionEvent.NUM_SAMPLE_DATA
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700615 + MotionEvent.SAMPLE_SIZE] = ev.value;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 }
617
Dianne Hackborn1411d1c2009-10-12 23:21:18 -0700618 // Process movement events from trackball (mouse) protocol.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 } else if (ev.type == RawInputEvent.EV_REL &&
620 (classes&RawInputEvent.CLASS_TRACKBALL) != 0) {
621 // Add this relative movement into our totals.
622 if (ev.scancode == RawInputEvent.REL_X) {
623 di.mRel.changed = true;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700624 di.mRel.mNextData[MotionEvent.SAMPLE_X] += ev.value;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625 } else if (ev.scancode == RawInputEvent.REL_Y) {
626 di.mRel.changed = true;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700627 di.mRel.mNextData[MotionEvent.SAMPLE_Y] += ev.value;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 }
629 }
630
Dianne Hackborn1411d1c2009-10-12 23:21:18 -0700631 // Handle multitouch protocol sync: tells us that the
632 // driver has returned all data for -one- of the pointers
633 // that is currently down.
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700634 if (ev.type == RawInputEvent.EV_SYN
635 && ev.scancode == RawInputEvent.SYN_MT_REPORT
636 && di.mAbs != null) {
637 di.mAbs.changed = true;
638 if (di.mAbs.mNextData[MotionEvent.SAMPLE_PRESSURE] > 0) {
639 // If the value is <= 0, the pointer is not
640 // down, so keep it in the count.
641
642 if (di.mAbs.mNextData[di.mAbs.mAddingPointerOffset
643 + MotionEvent.SAMPLE_PRESSURE] != 0) {
644 final int num = di.mAbs.mNextNumPointers+1;
645 di.mAbs.mNextNumPointers = num;
646 if (DEBUG_POINTERS) Log.v(TAG,
647 "MT_REPORT: now have " + num + " pointers");
648 final int newOffset = (num <= InputDevice.MAX_POINTERS)
649 ? (num * MotionEvent.NUM_SAMPLE_DATA)
650 : (InputDevice.MAX_POINTERS *
651 MotionEvent.NUM_SAMPLE_DATA);
652 di.mAbs.mAddingPointerOffset = newOffset;
653 di.mAbs.mNextData[newOffset
654 + MotionEvent.SAMPLE_PRESSURE] = 0;
655 } else {
656 if (DEBUG_POINTERS) Log.v(TAG, "MT_REPORT: no pointer");
657 }
658 }
Dianne Hackborn1411d1c2009-10-12 23:21:18 -0700659
660 // Handle general event sync: all data for the current
661 // event update has been delivered.
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700662 } else if (send || (ev.type == RawInputEvent.EV_SYN
663 && ev.scancode == RawInputEvent.SYN_REPORT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 if (mDisplay != null) {
665 if (!mHaveGlobalMetaState) {
666 computeGlobalMetaStateLocked();
667 }
668
669 MotionEvent me;
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700670
671 InputDevice.MotionState ms = di.mAbs;
672 if (ms.changed) {
673 ms.changed = false;
674
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700675 if ((classes&(RawInputEvent.CLASS_TOUCHSCREEN
676 |RawInputEvent.CLASS_TOUCHSCREEN_MT))
677 == RawInputEvent.CLASS_TOUCHSCREEN) {
678 ms.mNextNumPointers = 0;
Dianne Hackborn2a2b34432009-08-12 17:13:55 -0700679 if (ms.mDown[0]) {
680 System.arraycopy(di.curTouchVals, 0,
681 ms.mNextData, 0,
682 MotionEvent.NUM_SAMPLE_DATA);
683 ms.mNextNumPointers++;
684 }
Dianne Hackborn1411d1c2009-10-12 23:21:18 -0700685 }
686
687 if (BAD_TOUCH_HACK) {
688 ms.dropBadPoint(di);
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700689 }
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700690
691 boolean doMotion = !monitorVirtualKey(di,
692 ev, curTime, curTimeNano);
693
694 if (doMotion && ms.mNextNumPointers > 0
Dianne Hackborndc953722009-10-19 11:24:39 -0700695 && (ms.mLastNumPointers == 0
696 || ms.mSkipLastPointers)) {
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700697 doMotion = !generateVirtualKeyDown(di,
698 ev, curTime, curTimeNano);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699 }
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700700
701 if (doMotion) {
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700702 // XXX Need to be able to generate
703 // multiple events here, for example
704 // if two fingers change up/down state
705 // at the same time.
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700706 do {
707 me = ms.generateAbsMotion(di, curTime,
708 curTimeNano, mDisplay,
709 mOrientation, mGlobalMetaState);
Dianne Hackborndc953722009-10-19 11:24:39 -0700710 if (DEBUG_POINTERS) Log.v(TAG, "Absolute: x="
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700711 + di.mAbs.mNextData[MotionEvent.SAMPLE_X]
712 + " y="
713 + di.mAbs.mNextData[MotionEvent.SAMPLE_Y]
714 + " ev=" + me);
715 if (me != null) {
716 if (WindowManagerPolicy.WATCH_POINTER) {
717 Log.i(TAG, "Enqueueing: " + me);
718 }
719 addLocked(di, curTimeNano, ev.flags,
720 RawInputEvent.CLASS_TOUCHSCREEN, me);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700721 }
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700722 } while (ms.hasMore());
Dianne Hackborn1411d1c2009-10-12 23:21:18 -0700723 } else {
724 // We are consuming movement in the
725 // virtual key area... but still
726 // propagate this to the previous
727 // data for comparisons.
Dianne Hackbornf2ddfb12009-10-13 22:43:33 -0700728 int num = ms.mNextNumPointers;
729 if (num > InputDevice.MAX_POINTERS) {
730 num = InputDevice.MAX_POINTERS;
731 }
Dianne Hackborn1411d1c2009-10-12 23:21:18 -0700732 System.arraycopy(ms.mNextData, 0,
733 ms.mLastData, 0,
Dianne Hackbornf2ddfb12009-10-13 22:43:33 -0700734 num * MotionEvent.NUM_SAMPLE_DATA);
735 ms.mLastNumPointers = num;
Dianne Hackborndc953722009-10-19 11:24:39 -0700736 ms.mSkipLastPointers = true;
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700737 }
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700738
739 ms.finish();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 }
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700741
742 ms = di.mRel;
743 if (ms.changed) {
744 ms.changed = false;
745
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700746 me = ms.generateRelMotion(di, curTime,
747 curTimeNano,
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700748 mOrientation, mGlobalMetaState);
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700749 if (false) Log.v(TAG, "Relative: x="
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700750 + di.mRel.mNextData[MotionEvent.SAMPLE_X]
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700751 + " y="
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700752 + di.mRel.mNextData[MotionEvent.SAMPLE_Y]
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700753 + " ev=" + me);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700754 if (me != null) {
755 addLocked(di, curTimeNano, ev.flags,
756 RawInputEvent.CLASS_TRACKBALL, me);
757 }
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700758
759 ms.finish();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 }
761 }
762 }
763 }
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700764
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700765 } catch (RuntimeException exc) {
766 Log.e(TAG, "InputReaderThread uncaught exception", exc);
767 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768 }
769 }
770 };
771
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700772 private boolean isInsideDisplay(InputDevice dev) {
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700773 final InputDevice.AbsoluteInfo absx = dev.absX;
774 final InputDevice.AbsoluteInfo absy = dev.absY;
775 final InputDevice.MotionState absm = dev.mAbs;
776 if (absx == null || absy == null || absm == null) {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700777 return true;
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700778 }
779
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700780 if (absm.mNextData[MotionEvent.SAMPLE_X] >= absx.minValue
781 && absm.mNextData[MotionEvent.SAMPLE_X] <= absx.maxValue
782 && absm.mNextData[MotionEvent.SAMPLE_Y] >= absy.minValue
783 && absm.mNextData[MotionEvent.SAMPLE_Y] <= absy.maxValue) {
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700784 if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "Input ("
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700785 + absm.mNextData[MotionEvent.SAMPLE_X]
786 + "," + absm.mNextData[MotionEvent.SAMPLE_Y]
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700787 + ") inside of display");
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700788 return true;
789 }
790
791 return false;
792 }
793
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700794 private VirtualKey findVirtualKey(InputDevice dev) {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700795 final int N = mVirtualKeys.size();
796 if (N <= 0) {
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700797 return null;
798 }
799
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700800 final InputDevice.MotionState absm = dev.mAbs;
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700801 for (int i=0; i<N; i++) {
802 VirtualKey sb = mVirtualKeys.get(i);
803 sb.computeHitRect(dev, mDisplayWidth, mDisplayHeight);
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700804 if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "Hit test ("
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700805 + absm.mNextData[MotionEvent.SAMPLE_X] + ","
806 + absm.mNextData[MotionEvent.SAMPLE_Y] + ") in code "
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700807 + sb.scancode + " - (" + sb.hitLeft
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700808 + "," + sb.hitTop + ")-(" + sb.hitRight + ","
809 + sb.hitBottom + ")");
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700810 if (sb.checkHit(absm.mNextData[MotionEvent.SAMPLE_X],
811 absm.mNextData[MotionEvent.SAMPLE_Y])) {
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700812 if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "Hit!");
813 return sb;
814 }
815 }
816
817 return null;
818 }
819
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700820 private boolean generateVirtualKeyDown(InputDevice di, RawInputEvent ev,
821 long curTime, long curTimeNano) {
822 if (isInsideDisplay(di)) {
823 // Didn't consume event.
824 return false;
825 }
826
827
828 VirtualKey vk = findVirtualKey(di);
829 if (vk != null) {
830 final InputDevice.MotionState ms = di.mAbs;
831 mPressedVirtualKey = vk;
832 vk.lastKeycode = scancodeToKeycode(di.id, vk.scancode);
833 ms.mLastNumPointers = ms.mNextNumPointers;
834 di.mKeyDownTime = curTime;
835 if (DEBUG_VIRTUAL_KEYS) Log.v(TAG,
836 "Generate key down for: " + vk.scancode
837 + " (keycode=" + vk.lastKeycode + ")");
838 KeyEvent event = newKeyEvent(di, di.mKeyDownTime, curTime, true,
839 vk.lastKeycode, 0, vk.scancode,
840 KeyEvent.FLAG_VIRTUAL_HARD_KEY);
841 mHapticFeedbackCallback.virtualKeyFeedback(event);
842 addLocked(di, curTimeNano, ev.flags, RawInputEvent.CLASS_KEYBOARD,
843 event);
844 }
845
846 // We always consume the event, even if we didn't
847 // generate a key event. There are two reasons for
848 // this: to avoid spurious touches when holding
849 // the edges of the device near the touchscreen,
850 // and to avoid reporting events if there are virtual
851 // keys on the touchscreen outside of the display
852 // area.
853 // Note that for all of this we are only looking at the
854 // first pointer, since what we are handling here is the
855 // first pointer going down, and this is the coordinate
856 // that will be used to dispatch the event.
857 if (false) {
858 final InputDevice.AbsoluteInfo absx = di.absX;
859 final InputDevice.AbsoluteInfo absy = di.absY;
860 final InputDevice.MotionState absm = di.mAbs;
861 Log.v(TAG, "Rejecting ("
862 + absm.mNextData[MotionEvent.SAMPLE_X] + ","
863 + absm.mNextData[MotionEvent.SAMPLE_Y] + "): outside of ("
864 + absx.minValue + "," + absy.minValue
865 + ")-(" + absx.maxValue + ","
866 + absx.maxValue + ")");
867 }
868 return true;
869 }
870
871 private boolean monitorVirtualKey(InputDevice di, RawInputEvent ev,
872 long curTime, long curTimeNano) {
873 VirtualKey vk = mPressedVirtualKey;
874 if (vk == null) {
875 return false;
876 }
877
878 final InputDevice.MotionState ms = di.mAbs;
879 if (ms.mNextNumPointers <= 0) {
880 mPressedVirtualKey = null;
881 ms.mLastNumPointers = 0;
882 if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "Generate key up for: " + vk.scancode);
883 KeyEvent event = newKeyEvent(di, di.mKeyDownTime, curTime, false,
884 vk.lastKeycode, 0, vk.scancode,
885 KeyEvent.FLAG_VIRTUAL_HARD_KEY);
886 mHapticFeedbackCallback.virtualKeyFeedback(event);
887 addLocked(di, curTimeNano, ev.flags, RawInputEvent.CLASS_KEYBOARD,
888 event);
889 return true;
890
891 } else if (isInsideDisplay(di)) {
892 // Whoops the pointer has moved into
893 // the display area! Cancel the
894 // virtual key and start a pointer
895 // motion.
896 mPressedVirtualKey = null;
897 if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "Cancel key up for: " + vk.scancode);
898 KeyEvent event = newKeyEvent(di, di.mKeyDownTime, curTime, false,
899 vk.lastKeycode, 0, vk.scancode,
900 KeyEvent.FLAG_CANCELED | KeyEvent.FLAG_VIRTUAL_HARD_KEY);
901 mHapticFeedbackCallback.virtualKeyFeedback(event);
902 addLocked(di, curTimeNano, ev.flags, RawInputEvent.CLASS_KEYBOARD,
903 event);
904 ms.mLastNumPointers = 0;
905 return false;
906 }
907
908 return true;
909 }
910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 /**
912 * Returns a new meta state for the given keys and old state.
913 */
914 private static final int makeMetaState(int keycode, boolean down, int old) {
915 int mask;
916 switch (keycode) {
917 case KeyEvent.KEYCODE_ALT_LEFT:
918 mask = KeyEvent.META_ALT_LEFT_ON;
919 break;
920 case KeyEvent.KEYCODE_ALT_RIGHT:
921 mask = KeyEvent.META_ALT_RIGHT_ON;
922 break;
923 case KeyEvent.KEYCODE_SHIFT_LEFT:
924 mask = KeyEvent.META_SHIFT_LEFT_ON;
925 break;
926 case KeyEvent.KEYCODE_SHIFT_RIGHT:
927 mask = KeyEvent.META_SHIFT_RIGHT_ON;
928 break;
929 case KeyEvent.KEYCODE_SYM:
930 mask = KeyEvent.META_SYM_ON;
931 break;
932 default:
933 return old;
934 }
935 int result = ~(KeyEvent.META_ALT_ON | KeyEvent.META_SHIFT_ON)
936 & (down ? (old | mask) : (old & ~mask));
937 if (0 != (result & (KeyEvent.META_ALT_LEFT_ON | KeyEvent.META_ALT_RIGHT_ON))) {
938 result |= KeyEvent.META_ALT_ON;
939 }
940 if (0 != (result & (KeyEvent.META_SHIFT_LEFT_ON | KeyEvent.META_SHIFT_RIGHT_ON))) {
941 result |= KeyEvent.META_SHIFT_ON;
942 }
943 return result;
944 }
945
946 private void computeGlobalMetaStateLocked() {
947 int i = mDevices.size();
948 mGlobalMetaState = 0;
949 while ((--i) >= 0) {
950 mGlobalMetaState |= mDevices.valueAt(i).mMetaKeysState;
951 }
952 mHaveGlobalMetaState = true;
953 }
954
955 /*
956 * Return true if you want the event to get passed on to the
957 * rest of the system, and false if you've handled it and want
958 * it dropped.
959 */
960 abstract boolean preprocessEvent(InputDevice device, RawInputEvent event);
961
962 InputDevice getInputDevice(int deviceId) {
963 synchronized (mFirst) {
964 return getInputDeviceLocked(deviceId);
965 }
966 }
967
968 private InputDevice getInputDeviceLocked(int deviceId) {
969 return mDevices.get(deviceId);
970 }
971
972 public void setOrientation(int orientation) {
973 synchronized(mFirst) {
974 mOrientation = orientation;
975 switch (orientation) {
976 case Surface.ROTATION_90:
977 mKeyRotationMap = KEY_90_MAP;
978 break;
979 case Surface.ROTATION_180:
980 mKeyRotationMap = KEY_180_MAP;
981 break;
982 case Surface.ROTATION_270:
983 mKeyRotationMap = KEY_270_MAP;
984 break;
985 default:
986 mKeyRotationMap = null;
987 break;
988 }
989 }
990 }
991
992 public int rotateKeyCode(int keyCode) {
993 synchronized(mFirst) {
994 return rotateKeyCodeLocked(keyCode);
995 }
996 }
997
998 private int rotateKeyCodeLocked(int keyCode) {
999 int[] map = mKeyRotationMap;
1000 if (map != null) {
1001 final int N = map.length;
1002 for (int i=0; i<N; i+=2) {
1003 if (map[i] == keyCode) {
1004 return map[i+1];
1005 }
1006 }
1007 }
1008 return keyCode;
1009 }
1010
1011 boolean hasEvents() {
1012 synchronized (mFirst) {
1013 return mFirst.next != mLast;
1014 }
1015 }
1016
1017 /*
1018 * returns true if we returned an event, and false if we timed out
1019 */
1020 QueuedEvent getEvent(long timeoutMS) {
1021 long begin = SystemClock.uptimeMillis();
1022 final long end = begin+timeoutMS;
1023 long now = begin;
1024 synchronized (mFirst) {
1025 while (mFirst.next == mLast && end > now) {
1026 try {
1027 mWakeLock.release();
1028 mFirst.wait(end-now);
1029 }
1030 catch (InterruptedException e) {
1031 }
1032 now = SystemClock.uptimeMillis();
1033 if (begin > now) {
1034 begin = now;
1035 }
1036 }
1037 if (mFirst.next == mLast) {
1038 return null;
1039 }
1040 QueuedEvent p = mFirst.next;
1041 mFirst.next = p.next;
1042 mFirst.next.prev = mFirst;
1043 p.inQueue = false;
1044 return p;
1045 }
1046 }
1047
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001048 /**
1049 * Return true if the queue has an up event pending that corresponds
1050 * to the same key as the given key event.
1051 */
1052 boolean hasKeyUpEvent(KeyEvent origEvent) {
1053 synchronized (mFirst) {
1054 final int keyCode = origEvent.getKeyCode();
1055 QueuedEvent cur = mLast.prev;
1056 while (cur.prev != null) {
1057 if (cur.classType == RawInputEvent.CLASS_KEYBOARD) {
1058 KeyEvent ke = (KeyEvent)cur.event;
1059 if (ke.getAction() == KeyEvent.ACTION_UP
1060 && ke.getKeyCode() == keyCode) {
1061 return true;
1062 }
1063 }
1064 cur = cur.prev;
1065 }
1066 }
1067
1068 return false;
1069 }
1070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001071 void recycleEvent(QueuedEvent ev) {
1072 synchronized (mFirst) {
1073 //Log.i(TAG, "Recycle event: " + ev);
1074 if (ev.event == ev.inputDevice.mAbs.currentMove) {
1075 ev.inputDevice.mAbs.currentMove = null;
1076 }
1077 if (ev.event == ev.inputDevice.mRel.currentMove) {
1078 if (false) Log.i(TAG, "Detach rel " + ev.event);
1079 ev.inputDevice.mRel.currentMove = null;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001080 ev.inputDevice.mRel.mNextData[MotionEvent.SAMPLE_X] = 0;
1081 ev.inputDevice.mRel.mNextData[MotionEvent.SAMPLE_Y] = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001082 }
1083 recycleLocked(ev);
1084 }
1085 }
1086
1087 void filterQueue(FilterCallback cb) {
1088 synchronized (mFirst) {
1089 QueuedEvent cur = mLast.prev;
1090 while (cur.prev != null) {
1091 switch (cb.filterEvent(cur)) {
1092 case FILTER_REMOVE:
1093 cur.prev.next = cur.next;
1094 cur.next.prev = cur.prev;
1095 break;
1096 case FILTER_ABORT:
1097 return;
1098 }
1099 cur = cur.prev;
1100 }
1101 }
1102 }
1103
Michael Chan53071d62009-05-13 17:29:48 -07001104 private QueuedEvent obtainLocked(InputDevice device, long whenNano,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 int flags, int classType, Object event) {
1106 QueuedEvent ev;
1107 if (mCacheCount == 0) {
1108 ev = new QueuedEvent();
1109 } else {
1110 ev = mCache;
1111 ev.inQueue = false;
1112 mCache = ev.next;
1113 mCacheCount--;
1114 }
1115 ev.inputDevice = device;
Michael Chan53071d62009-05-13 17:29:48 -07001116 ev.whenNano = whenNano;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 ev.flags = flags;
1118 ev.classType = classType;
1119 ev.event = event;
1120 return ev;
1121 }
1122
1123 private void recycleLocked(QueuedEvent ev) {
1124 if (ev.inQueue) {
1125 throw new RuntimeException("Event already in queue!");
1126 }
1127 if (mCacheCount < 10) {
1128 mCacheCount++;
1129 ev.next = mCache;
1130 mCache = ev;
1131 ev.inQueue = true;
1132 }
1133 }
1134
Michael Chan53071d62009-05-13 17:29:48 -07001135 private void addLocked(InputDevice device, long whenNano, int flags,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 int classType, Object event) {
1137 boolean poke = mFirst.next == mLast;
1138
Michael Chan53071d62009-05-13 17:29:48 -07001139 QueuedEvent ev = obtainLocked(device, whenNano, flags, classType, event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 QueuedEvent p = mLast.prev;
Michael Chan53071d62009-05-13 17:29:48 -07001141 while (p != mFirst && ev.whenNano < p.whenNano) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 p = p.prev;
1143 }
1144
1145 ev.next = p.next;
1146 ev.prev = p;
1147 p.next = ev;
1148 ev.next.prev = ev;
1149 ev.inQueue = true;
1150
1151 if (poke) {
Michael Chan53071d62009-05-13 17:29:48 -07001152 long time;
1153 if (MEASURE_LATENCY) {
1154 time = System.nanoTime();
1155 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 mFirst.notify();
1157 mWakeLock.acquire();
Michael Chan53071d62009-05-13 17:29:48 -07001158 if (MEASURE_LATENCY) {
1159 lt.sample("1 addLocked-queued event ", System.nanoTime() - time);
1160 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 }
1162 }
1163
1164 private InputDevice newInputDevice(int deviceId) {
1165 int classes = getDeviceClasses(deviceId);
1166 String name = getDeviceName(deviceId);
Dianne Hackborna8f60182009-09-01 19:01:50 -07001167 InputDevice.AbsoluteInfo absX = null;
1168 InputDevice.AbsoluteInfo absY = null;
1169 InputDevice.AbsoluteInfo absPressure = null;
1170 InputDevice.AbsoluteInfo absSize = null;
1171 if (classes != 0) {
1172 Log.i(TAG, "Device added: id=0x" + Integer.toHexString(deviceId)
1173 + ", name=" + name
1174 + ", classes=" + Integer.toHexString(classes));
1175 if ((classes&RawInputEvent.CLASS_TOUCHSCREEN_MT) != 0) {
1176 absX = loadAbsoluteInfo(deviceId,
1177 RawInputEvent.ABS_MT_POSITION_X, "X");
1178 absY = loadAbsoluteInfo(deviceId,
1179 RawInputEvent.ABS_MT_POSITION_Y, "Y");
1180 absPressure = loadAbsoluteInfo(deviceId,
1181 RawInputEvent.ABS_MT_TOUCH_MAJOR, "Pressure");
1182 absSize = loadAbsoluteInfo(deviceId,
1183 RawInputEvent.ABS_MT_WIDTH_MAJOR, "Size");
1184 } else if ((classes&RawInputEvent.CLASS_TOUCHSCREEN) != 0) {
1185 absX = loadAbsoluteInfo(deviceId,
1186 RawInputEvent.ABS_X, "X");
1187 absY = loadAbsoluteInfo(deviceId,
1188 RawInputEvent.ABS_Y, "Y");
1189 absPressure = loadAbsoluteInfo(deviceId,
1190 RawInputEvent.ABS_PRESSURE, "Pressure");
1191 absSize = loadAbsoluteInfo(deviceId,
1192 RawInputEvent.ABS_TOOL_WIDTH, "Size");
1193 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 }
1195
1196 return new InputDevice(deviceId, classes, name, absX, absY, absPressure, absSize);
1197 }
1198
1199 private InputDevice.AbsoluteInfo loadAbsoluteInfo(int id, int channel,
1200 String name) {
1201 InputDevice.AbsoluteInfo info = new InputDevice.AbsoluteInfo();
1202 if (getAbsoluteInfo(id, channel, info)
1203 && info.minValue != info.maxValue) {
1204 Log.i(TAG, " " + name + ": min=" + info.minValue
1205 + " max=" + info.maxValue
1206 + " flat=" + info.flat
1207 + " fuzz=" + info.fuzz);
1208 info.range = info.maxValue-info.minValue;
1209 return info;
1210 }
1211 Log.i(TAG, " " + name + ": unknown values");
1212 return null;
1213 }
1214 private static native boolean readEvent(RawInputEvent outEvent);
1215}