blob: d9f4c9c7654b24af7e8e5b5692ef446ffb5b1678 [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 Hackborne3dd8842009-07-14 12:06:54 -070051 static final boolean DEBUG_VIRTUAL_KEYS = false;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -070052 static final boolean DEBUG_POINTERS = false;
Dianne Hackborne3dd8842009-07-14 12:06:54 -070053
Mike Lockwood1d9dfc52009-07-16 11:11:18 -040054 private static final String EXCLUDED_DEVICES_PATH = "etc/excluded-input-devices.xml";
55
Dianne Hackborne3dd8842009-07-14 12:06:54 -070056 final SparseArray<InputDevice> mDevices = new SparseArray<InputDevice>();
Dianne Hackborna8f60182009-09-01 19:01:50 -070057 final SparseArray<InputDevice> mIgnoredDevices = new SparseArray<InputDevice>();
Dianne Hackborne3dd8842009-07-14 12:06:54 -070058 final ArrayList<VirtualKey> mVirtualKeys = new ArrayList<VirtualKey>();
Dianne Hackbornddca3ee2009-07-23 19:01:31 -070059 final HapticFeedbackCallback mHapticFeedbackCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060
61 int mGlobalMetaState = 0;
62 boolean mHaveGlobalMetaState = false;
63
64 final QueuedEvent mFirst;
65 final QueuedEvent mLast;
66 QueuedEvent mCache;
67 int mCacheCount;
68
69 Display mDisplay = null;
Dianne Hackborne3dd8842009-07-14 12:06:54 -070070 int mDisplayWidth;
71 int mDisplayHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072
73 int mOrientation = Surface.ROTATION_0;
74 int[] mKeyRotationMap = null;
75
Dianne Hackborne3dd8842009-07-14 12:06:54 -070076 VirtualKey mPressedVirtualKey = null;
77
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 PowerManager.WakeLock mWakeLock;
79
80 static final int[] KEY_90_MAP = new int[] {
81 KeyEvent.KEYCODE_DPAD_DOWN, KeyEvent.KEYCODE_DPAD_RIGHT,
82 KeyEvent.KEYCODE_DPAD_RIGHT, KeyEvent.KEYCODE_DPAD_UP,
83 KeyEvent.KEYCODE_DPAD_UP, KeyEvent.KEYCODE_DPAD_LEFT,
84 KeyEvent.KEYCODE_DPAD_LEFT, KeyEvent.KEYCODE_DPAD_DOWN,
85 };
86
87 static final int[] KEY_180_MAP = new int[] {
88 KeyEvent.KEYCODE_DPAD_DOWN, KeyEvent.KEYCODE_DPAD_UP,
89 KeyEvent.KEYCODE_DPAD_RIGHT, KeyEvent.KEYCODE_DPAD_LEFT,
90 KeyEvent.KEYCODE_DPAD_UP, KeyEvent.KEYCODE_DPAD_DOWN,
91 KeyEvent.KEYCODE_DPAD_LEFT, KeyEvent.KEYCODE_DPAD_RIGHT,
92 };
93
94 static final int[] KEY_270_MAP = new int[] {
95 KeyEvent.KEYCODE_DPAD_DOWN, KeyEvent.KEYCODE_DPAD_LEFT,
96 KeyEvent.KEYCODE_DPAD_LEFT, KeyEvent.KEYCODE_DPAD_UP,
97 KeyEvent.KEYCODE_DPAD_UP, KeyEvent.KEYCODE_DPAD_RIGHT,
98 KeyEvent.KEYCODE_DPAD_RIGHT, KeyEvent.KEYCODE_DPAD_DOWN,
99 };
100
101 public static final int FILTER_REMOVE = 0;
102 public static final int FILTER_KEEP = 1;
103 public static final int FILTER_ABORT = -1;
Michael Chan53071d62009-05-13 17:29:48 -0700104
105 private static final boolean MEASURE_LATENCY = false;
106 private LatencyTimer lt;
107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 public interface FilterCallback {
109 int filterEvent(QueuedEvent ev);
110 }
111
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700112 public interface HapticFeedbackCallback {
113 void virtualKeyFeedback(KeyEvent event);
114 }
115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 static class QueuedEvent {
117 InputDevice inputDevice;
Michael Chan53071d62009-05-13 17:29:48 -0700118 long whenNano;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 int flags; // From the raw event
120 int classType; // One of the class constants in InputEvent
121 Object event;
122 boolean inQueue;
123
124 void copyFrom(QueuedEvent that) {
125 this.inputDevice = that.inputDevice;
Michael Chan53071d62009-05-13 17:29:48 -0700126 this.whenNano = that.whenNano;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 this.flags = that.flags;
128 this.classType = that.classType;
129 this.event = that.event;
130 }
131
132 @Override
133 public String toString() {
134 return "QueuedEvent{"
135 + Integer.toHexString(System.identityHashCode(this))
136 + " " + event + "}";
137 }
138
139 // not copied
140 QueuedEvent prev;
141 QueuedEvent next;
142 }
143
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700144 /**
145 * A key that exists as a part of the touch-screen, outside of the normal
146 * display area of the screen.
147 */
148 static class VirtualKey {
149 int scancode;
150 int centerx;
151 int centery;
152 int width;
153 int height;
154
155 int hitLeft;
156 int hitTop;
157 int hitRight;
158 int hitBottom;
159
160 InputDevice lastDevice;
161 int lastKeycode;
162
163 boolean checkHit(int x, int y) {
164 return (x >= hitLeft && x <= hitRight
165 && y >= hitTop && y <= hitBottom);
166 }
167
168 void computeHitRect(InputDevice dev, int dw, int dh) {
169 if (dev == lastDevice) {
170 return;
171 }
172
173 if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "computeHitRect for " + scancode
174 + ": dev=" + dev + " absX=" + dev.absX + " absY=" + dev.absY);
175
176 lastDevice = dev;
177
178 int minx = dev.absX.minValue;
179 int maxx = dev.absX.maxValue;
180
181 int halfw = width/2;
182 int left = centerx - halfw;
183 int right = centerx + halfw;
184 hitLeft = minx + ((left*maxx-minx)/dw);
185 hitRight = minx + ((right*maxx-minx)/dw);
186
187 int miny = dev.absY.minValue;
188 int maxy = dev.absY.maxValue;
189
190 int halfh = height/2;
191 int top = centery - halfh;
192 int bottom = centery + halfh;
193 hitTop = miny + ((top*maxy-miny)/dh);
194 hitBottom = miny + ((bottom*maxy-miny)/dh);
195 }
196 }
Michael Chan53071d62009-05-13 17:29:48 -0700197
Iliyan Malchev75b2aed2009-08-06 14:50:57 -0700198 private void readVirtualKeys(String deviceName) {
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700199 try {
200 FileInputStream fis = new FileInputStream(
Iliyan Malchev75b2aed2009-08-06 14:50:57 -0700201 "/sys/board_properties/virtualkeys." + deviceName);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700202 InputStreamReader isr = new InputStreamReader(fis);
203 BufferedReader br = new BufferedReader(isr);
204 String str = br.readLine();
205 if (str != null) {
206 String[] it = str.split(":");
207 if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "***** VIRTUAL KEYS: " + it);
208 final int N = it.length-6;
209 for (int i=0; i<=N; i+=6) {
210 if (!"0x01".equals(it[i])) {
211 Log.w(TAG, "Unknown virtual key type at elem #" + i
212 + ": " + it[i]);
213 continue;
214 }
215 try {
216 VirtualKey sb = new VirtualKey();
217 sb.scancode = Integer.parseInt(it[i+1]);
218 sb.centerx = Integer.parseInt(it[i+2]);
219 sb.centery = Integer.parseInt(it[i+3]);
220 sb.width = Integer.parseInt(it[i+4]);
221 sb.height = Integer.parseInt(it[i+5]);
222 if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "Virtual key "
223 + sb.scancode + ": center=" + sb.centerx + ","
224 + sb.centery + " size=" + sb.width + "x"
225 + sb.height);
226 mVirtualKeys.add(sb);
227 } catch (NumberFormatException e) {
228 Log.w(TAG, "Bad number at region " + i + " in: "
229 + str, e);
230 }
231 }
232 }
233 br.close();
234 } catch (FileNotFoundException e) {
235 Log.i(TAG, "No virtual keys found");
236 } catch (IOException e) {
237 Log.w(TAG, "Error reading virtual keys", e);
238 }
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400239 }
240
241 private void readExcludedDevices() {
242 // Read partner-provided list of excluded input devices
243 XmlPullParser parser = null;
244 // Environment.getRootDirectory() is a fancy way of saying ANDROID_ROOT or "/system".
245 File confFile = new File(Environment.getRootDirectory(), EXCLUDED_DEVICES_PATH);
246 FileReader confreader = null;
247 try {
248 confreader = new FileReader(confFile);
249 parser = Xml.newPullParser();
250 parser.setInput(confreader);
251 XmlUtils.beginDocument(parser, "devices");
252
253 while (true) {
254 XmlUtils.nextElement(parser);
255 if (!"device".equals(parser.getName())) {
256 break;
257 }
258 String name = parser.getAttributeValue(null, "name");
259 if (name != null) {
260 Log.d(TAG, "addExcludedDevice " + name);
261 addExcludedDevice(name);
262 }
263 }
264 } catch (FileNotFoundException e) {
265 // It's ok if the file does not exist.
266 } catch (Exception e) {
267 Log.e(TAG, "Exception while parsing '" + confFile.getAbsolutePath() + "'", e);
268 } finally {
269 try { if (confreader != null) confreader.close(); } catch (IOException e) { }
270 }
271 }
272
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700273 KeyInputQueue(Context context, HapticFeedbackCallback hapticFeedbackCallback) {
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400274 if (MEASURE_LATENCY) {
275 lt = new LatencyTimer(100, 1000);
276 }
277
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700278 mHapticFeedbackCallback = hapticFeedbackCallback;
279
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400280 readExcludedDevices();
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 PowerManager pm = (PowerManager)context.getSystemService(
283 Context.POWER_SERVICE);
284 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
285 "KeyInputQueue");
286 mWakeLock.setReferenceCounted(false);
287
288 mFirst = new QueuedEvent();
289 mLast = new QueuedEvent();
290 mFirst.next = mLast;
291 mLast.prev = mFirst;
292
293 mThread.start();
294 }
295
296 public void setDisplay(Display display) {
297 mDisplay = display;
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700298
299 // We assume at this point that the display dimensions reflect the
300 // natural, unrotated display. We will perform hit tests for soft
301 // buttons based on that display.
302 mDisplayWidth = display.getWidth();
303 mDisplayHeight = display.getHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 }
305
306 public void getInputConfiguration(Configuration config) {
307 synchronized (mFirst) {
308 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
309 config.keyboard = Configuration.KEYBOARD_NOKEYS;
310 config.navigation = Configuration.NAVIGATION_NONAV;
311
312 final int N = mDevices.size();
313 for (int i=0; i<N; i++) {
314 InputDevice d = mDevices.valueAt(i);
315 if (d != null) {
316 if ((d.classes&RawInputEvent.CLASS_TOUCHSCREEN) != 0) {
317 config.touchscreen
318 = Configuration.TOUCHSCREEN_FINGER;
319 //Log.i("foo", "***** HAVE TOUCHSCREEN!");
320 }
321 if ((d.classes&RawInputEvent.CLASS_ALPHAKEY) != 0) {
322 config.keyboard
323 = Configuration.KEYBOARD_QWERTY;
324 //Log.i("foo", "***** HAVE QWERTY!");
325 }
326 if ((d.classes&RawInputEvent.CLASS_TRACKBALL) != 0) {
327 config.navigation
328 = Configuration.NAVIGATION_TRACKBALL;
329 //Log.i("foo", "***** HAVE TRACKBALL!");
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700330 } else if ((d.classes&RawInputEvent.CLASS_DPAD) != 0) {
331 config.navigation
332 = Configuration.NAVIGATION_DPAD;
333 //Log.i("foo", "***** HAVE DPAD!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 }
335 }
336 }
337 }
338 }
339
340 public static native String getDeviceName(int deviceId);
341 public static native int getDeviceClasses(int deviceId);
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400342 public static native void addExcludedDevice(String deviceName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 public static native boolean getAbsoluteInfo(int deviceId, int axis,
344 InputDevice.AbsoluteInfo outInfo);
345 public static native int getSwitchState(int sw);
346 public static native int getSwitchState(int deviceId, int sw);
347 public static native int getScancodeState(int sw);
348 public static native int getScancodeState(int deviceId, int sw);
349 public static native int getKeycodeState(int sw);
350 public static native int getKeycodeState(int deviceId, int sw);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700351 public static native int scancodeToKeycode(int deviceId, int scancode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 public static native boolean hasKeys(int[] keycodes, boolean[] keyExists);
353
354 public static KeyEvent newKeyEvent(InputDevice device, long downTime,
355 long eventTime, boolean down, int keycode, int repeatCount,
356 int scancode, int flags) {
357 return new KeyEvent(
358 downTime, eventTime,
359 down ? KeyEvent.ACTION_DOWN : KeyEvent.ACTION_UP,
360 keycode, repeatCount,
361 device != null ? device.mMetaKeysState : 0,
362 device != null ? device.id : -1, scancode,
The Android Open Source Project10592532009-03-18 17:39:46 -0700363 flags | KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 }
365
366 Thread mThread = new Thread("InputDeviceReader") {
367 public void run() {
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400368 Log.d(TAG, "InputDeviceReader.run()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 android.os.Process.setThreadPriority(
370 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
371
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700372 RawInputEvent ev = new RawInputEvent();
373 while (true) {
374 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 InputDevice di;
376
377 // block, doesn't release the monitor
378 readEvent(ev);
379
380 boolean send = false;
381 boolean configChanged = false;
382
383 if (false) {
384 Log.i(TAG, "Input event: dev=0x"
385 + Integer.toHexString(ev.deviceId)
386 + " type=0x" + Integer.toHexString(ev.type)
387 + " scancode=" + ev.scancode
388 + " keycode=" + ev.keycode
389 + " value=" + ev.value);
390 }
391
392 if (ev.type == RawInputEvent.EV_DEVICE_ADDED) {
393 synchronized (mFirst) {
394 di = newInputDevice(ev.deviceId);
Dianne Hackborna8f60182009-09-01 19:01:50 -0700395 if (di.classes != 0) {
396 // If this device is some kind of input class,
397 // we care about it.
398 mDevices.put(ev.deviceId, di);
399 if ((di.classes & RawInputEvent.CLASS_TOUCHSCREEN) != 0) {
400 readVirtualKeys(di.name);
401 }
402 // The configuration may have changed because
403 // of this device.
404 configChanged = true;
405 } else {
406 // We won't do anything with this device.
407 mIgnoredDevices.put(ev.deviceId, di);
408 Log.i(TAG, "Ignoring non-input device: id=0x"
409 + Integer.toHexString(di.id)
410 + ", name=" + di.name);
Iliyan Malchev75b2aed2009-08-06 14:50:57 -0700411 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 }
413 } else if (ev.type == RawInputEvent.EV_DEVICE_REMOVED) {
414 synchronized (mFirst) {
Dianne Hackborna8f60182009-09-01 19:01:50 -0700415 if (false) {
416 Log.i(TAG, "Device removed: id=0x"
417 + Integer.toHexString(ev.deviceId));
418 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 di = mDevices.get(ev.deviceId);
420 if (di != null) {
421 mDevices.delete(ev.deviceId);
Dianne Hackborna8f60182009-09-01 19:01:50 -0700422 // The configuration may have changed because
423 // of this device.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 configChanged = true;
Dianne Hackborna8f60182009-09-01 19:01:50 -0700425 } else if ((di=mIgnoredDevices.get(ev.deviceId)) != null) {
426 mIgnoredDevices.remove(ev.deviceId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 } else {
Dianne Hackborna8f60182009-09-01 19:01:50 -0700428 Log.w(TAG, "Removing bad device id: "
429 + Integer.toHexString(ev.deviceId));
430 continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 }
432 }
433 } else {
434 di = getInputDevice(ev.deviceId);
Dianne Hackborna8f60182009-09-01 19:01:50 -0700435 if (di == null) {
436 // This may be some junk from an ignored device.
437 continue;
438 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439
440 // first crack at it
441 send = preprocessEvent(di, ev);
442
443 if (ev.type == RawInputEvent.EV_KEY) {
444 di.mMetaKeysState = makeMetaState(ev.keycode,
445 ev.value != 0, di.mMetaKeysState);
446 mHaveGlobalMetaState = false;
447 }
448 }
449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 if (configChanged) {
451 synchronized (mFirst) {
Michael Chan53071d62009-05-13 17:29:48 -0700452 addLocked(di, System.nanoTime(), 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 RawInputEvent.CLASS_CONFIGURATION_CHANGED,
454 null);
455 }
456 }
457
458 if (!send) {
459 continue;
460 }
461
462 synchronized (mFirst) {
463 // NOTE: The event timebase absolutely must be the same
464 // timebase as SystemClock.uptimeMillis().
465 //curTime = gotOne ? ev.when : SystemClock.uptimeMillis();
466 final long curTime = SystemClock.uptimeMillis();
Michael Chan53071d62009-05-13 17:29:48 -0700467 final long curTimeNano = System.nanoTime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 //Log.i(TAG, "curTime=" + curTime + ", systemClock=" + SystemClock.uptimeMillis());
469
470 final int classes = di.classes;
471 final int type = ev.type;
472 final int scancode = ev.scancode;
473 send = false;
474
475 // Is it a key event?
476 if (type == RawInputEvent.EV_KEY &&
477 (classes&RawInputEvent.CLASS_KEYBOARD) != 0 &&
478 (scancode < RawInputEvent.BTN_FIRST ||
479 scancode > RawInputEvent.BTN_LAST)) {
480 boolean down;
481 if (ev.value != 0) {
482 down = true;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700483 di.mKeyDownTime = curTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 } else {
485 down = false;
486 }
487 int keycode = rotateKeyCodeLocked(ev.keycode);
Michael Chan53071d62009-05-13 17:29:48 -0700488 addLocked(di, curTimeNano, ev.flags,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489 RawInputEvent.CLASS_KEYBOARD,
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700490 newKeyEvent(di, di.mKeyDownTime, curTime, down,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 keycode, 0, scancode,
492 ((ev.flags & WindowManagerPolicy.FLAG_WOKE_HERE) != 0)
493 ? KeyEvent.FLAG_WOKE_HERE : 0));
494 } else if (ev.type == RawInputEvent.EV_KEY) {
495 if (ev.scancode == RawInputEvent.BTN_TOUCH &&
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700496 (classes&(RawInputEvent.CLASS_TOUCHSCREEN
497 |RawInputEvent.CLASS_TOUCHSCREEN_MT))
498 == RawInputEvent.CLASS_TOUCHSCREEN) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 di.mAbs.changed = true;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700500 di.mAbs.mDown[0] = ev.value != 0;
501 } else if (ev.scancode == RawInputEvent.BTN_2 &&
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700502 (classes&(RawInputEvent.CLASS_TOUCHSCREEN
503 |RawInputEvent.CLASS_TOUCHSCREEN_MT))
504 == RawInputEvent.CLASS_TOUCHSCREEN) {
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700505 di.mAbs.changed = true;
506 di.mAbs.mDown[1] = ev.value != 0;
507 } else if (ev.scancode == RawInputEvent.BTN_MOUSE &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 (classes&RawInputEvent.CLASS_TRACKBALL) != 0) {
509 di.mRel.changed = true;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700510 di.mRel.mNextNumPointers = ev.value != 0 ? 1 : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 send = true;
512 }
513
514 } else if (ev.type == RawInputEvent.EV_ABS &&
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700515 (classes&RawInputEvent.CLASS_TOUCHSCREEN_MT) != 0) {
516 if (ev.scancode == RawInputEvent.ABS_MT_TOUCH_MAJOR) {
517 di.mAbs.changed = true;
518 di.mAbs.mNextData[di.mAbs.mAddingPointerOffset
519 + MotionEvent.SAMPLE_PRESSURE] = ev.value;
520 } else if (ev.scancode == RawInputEvent.ABS_MT_POSITION_X) {
521 di.mAbs.changed = true;
522 di.mAbs.mNextData[di.mAbs.mAddingPointerOffset
523 + MotionEvent.SAMPLE_X] = ev.value;
524 if (DEBUG_POINTERS) Log.v(TAG, "MT @"
525 + di.mAbs.mAddingPointerOffset
526 + " X:" + ev.value);
527 } else if (ev.scancode == RawInputEvent.ABS_MT_POSITION_Y) {
528 di.mAbs.changed = true;
529 di.mAbs.mNextData[di.mAbs.mAddingPointerOffset
530 + MotionEvent.SAMPLE_Y] = ev.value;
531 if (DEBUG_POINTERS) Log.v(TAG, "MT @"
532 + di.mAbs.mAddingPointerOffset
533 + " Y:" + ev.value);
534 } else if (ev.scancode == RawInputEvent.ABS_MT_WIDTH_MAJOR) {
535 di.mAbs.changed = true;
536 di.mAbs.mNextData[di.mAbs.mAddingPointerOffset
537 + MotionEvent.SAMPLE_SIZE] = ev.value;
538 }
539
540 } else if (ev.type == RawInputEvent.EV_ABS &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541 (classes&RawInputEvent.CLASS_TOUCHSCREEN) != 0) {
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700542 // Finger 1
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 if (ev.scancode == RawInputEvent.ABS_X) {
544 di.mAbs.changed = true;
Dianne Hackborn2a2b34432009-08-12 17:13:55 -0700545 di.curTouchVals[MotionEvent.SAMPLE_X] = ev.value;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 } else if (ev.scancode == RawInputEvent.ABS_Y) {
547 di.mAbs.changed = true;
Dianne Hackborn2a2b34432009-08-12 17:13:55 -0700548 di.curTouchVals[MotionEvent.SAMPLE_Y] = ev.value;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 } else if (ev.scancode == RawInputEvent.ABS_PRESSURE) {
550 di.mAbs.changed = true;
Dianne Hackborn2a2b34432009-08-12 17:13:55 -0700551 di.curTouchVals[MotionEvent.SAMPLE_PRESSURE] = ev.value;
552 di.curTouchVals[MotionEvent.NUM_SAMPLE_DATA
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700553 + MotionEvent.SAMPLE_PRESSURE] = ev.value;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 } else if (ev.scancode == RawInputEvent.ABS_TOOL_WIDTH) {
555 di.mAbs.changed = true;
Dianne Hackborn2a2b34432009-08-12 17:13:55 -0700556 di.curTouchVals[MotionEvent.SAMPLE_SIZE] = ev.value;
557 di.curTouchVals[MotionEvent.NUM_SAMPLE_DATA
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700558 + MotionEvent.SAMPLE_SIZE] = ev.value;
559
560 // Finger 2
561 } else if (ev.scancode == RawInputEvent.ABS_HAT0X) {
562 di.mAbs.changed = true;
Dianne Hackborn2a2b34432009-08-12 17:13:55 -0700563 di.curTouchVals[MotionEvent.NUM_SAMPLE_DATA
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700564 + MotionEvent.SAMPLE_X] = ev.value;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700565 } else if (ev.scancode == RawInputEvent.ABS_HAT0Y) {
566 di.mAbs.changed = true;
Dianne Hackborn2a2b34432009-08-12 17:13:55 -0700567 di.curTouchVals[MotionEvent.NUM_SAMPLE_DATA
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700568 + MotionEvent.SAMPLE_Y] = ev.value;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 }
570
571 } else if (ev.type == RawInputEvent.EV_REL &&
572 (classes&RawInputEvent.CLASS_TRACKBALL) != 0) {
573 // Add this relative movement into our totals.
574 if (ev.scancode == RawInputEvent.REL_X) {
575 di.mRel.changed = true;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700576 di.mRel.mNextData[MotionEvent.SAMPLE_X] += ev.value;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 } else if (ev.scancode == RawInputEvent.REL_Y) {
578 di.mRel.changed = true;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700579 di.mRel.mNextData[MotionEvent.SAMPLE_Y] += ev.value;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 }
581 }
582
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700583 if (ev.type == RawInputEvent.EV_SYN
584 && ev.scancode == RawInputEvent.SYN_MT_REPORT
585 && di.mAbs != null) {
586 di.mAbs.changed = true;
587 if (di.mAbs.mNextData[MotionEvent.SAMPLE_PRESSURE] > 0) {
588 // If the value is <= 0, the pointer is not
589 // down, so keep it in the count.
590
591 if (di.mAbs.mNextData[di.mAbs.mAddingPointerOffset
592 + MotionEvent.SAMPLE_PRESSURE] != 0) {
593 final int num = di.mAbs.mNextNumPointers+1;
594 di.mAbs.mNextNumPointers = num;
595 if (DEBUG_POINTERS) Log.v(TAG,
596 "MT_REPORT: now have " + num + " pointers");
597 final int newOffset = (num <= InputDevice.MAX_POINTERS)
598 ? (num * MotionEvent.NUM_SAMPLE_DATA)
599 : (InputDevice.MAX_POINTERS *
600 MotionEvent.NUM_SAMPLE_DATA);
601 di.mAbs.mAddingPointerOffset = newOffset;
602 di.mAbs.mNextData[newOffset
603 + MotionEvent.SAMPLE_PRESSURE] = 0;
604 } else {
605 if (DEBUG_POINTERS) Log.v(TAG, "MT_REPORT: no pointer");
606 }
607 }
608 } else if (send || (ev.type == RawInputEvent.EV_SYN
609 && ev.scancode == RawInputEvent.SYN_REPORT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 if (mDisplay != null) {
611 if (!mHaveGlobalMetaState) {
612 computeGlobalMetaStateLocked();
613 }
614
615 MotionEvent me;
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700616
617 InputDevice.MotionState ms = di.mAbs;
618 if (ms.changed) {
619 ms.changed = false;
620
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700621 if ((classes&(RawInputEvent.CLASS_TOUCHSCREEN
622 |RawInputEvent.CLASS_TOUCHSCREEN_MT))
623 == RawInputEvent.CLASS_TOUCHSCREEN) {
624 ms.mNextNumPointers = 0;
Dianne Hackborn2a2b34432009-08-12 17:13:55 -0700625 if (ms.mDown[0]) {
626 System.arraycopy(di.curTouchVals, 0,
627 ms.mNextData, 0,
628 MotionEvent.NUM_SAMPLE_DATA);
629 ms.mNextNumPointers++;
630 }
631 if (ms.mDown[1]) {
632 System.arraycopy(di.curTouchVals,
633 MotionEvent.NUM_SAMPLE_DATA,
634 ms.mNextData,
635 ms.mNextNumPointers
636 * MotionEvent.NUM_SAMPLE_DATA,
637 MotionEvent.NUM_SAMPLE_DATA);
638 ms.mNextNumPointers++;
639 }
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700640 }
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700641
642 boolean doMotion = !monitorVirtualKey(di,
643 ev, curTime, curTimeNano);
644
645 if (doMotion && ms.mNextNumPointers > 0
646 && ms.mLastNumPointers == 0) {
647 doMotion = !generateVirtualKeyDown(di,
648 ev, curTime, curTimeNano);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649 }
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700650
651 if (doMotion) {
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700652 // XXX Need to be able to generate
653 // multiple events here, for example
654 // if two fingers change up/down state
655 // at the same time.
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700656 do {
657 me = ms.generateAbsMotion(di, curTime,
658 curTimeNano, mDisplay,
659 mOrientation, mGlobalMetaState);
660 if (false) Log.v(TAG, "Absolute: x="
661 + di.mAbs.mNextData[MotionEvent.SAMPLE_X]
662 + " y="
663 + di.mAbs.mNextData[MotionEvent.SAMPLE_Y]
664 + " ev=" + me);
665 if (me != null) {
666 if (WindowManagerPolicy.WATCH_POINTER) {
667 Log.i(TAG, "Enqueueing: " + me);
668 }
669 addLocked(di, curTimeNano, ev.flags,
670 RawInputEvent.CLASS_TOUCHSCREEN, me);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700671 }
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700672 } while (ms.hasMore());
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700673 }
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700674
675 ms.finish();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 }
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700677
678 ms = di.mRel;
679 if (ms.changed) {
680 ms.changed = false;
681
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700682 me = ms.generateRelMotion(di, curTime,
683 curTimeNano,
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700684 mOrientation, mGlobalMetaState);
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700685 if (false) Log.v(TAG, "Relative: x="
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700686 + di.mRel.mNextData[MotionEvent.SAMPLE_X]
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700687 + " y="
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700688 + di.mRel.mNextData[MotionEvent.SAMPLE_Y]
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700689 + " ev=" + me);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700690 if (me != null) {
691 addLocked(di, curTimeNano, ev.flags,
692 RawInputEvent.CLASS_TRACKBALL, me);
693 }
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700694
695 ms.finish();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696 }
697 }
698 }
699 }
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700700
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700701 } catch (RuntimeException exc) {
702 Log.e(TAG, "InputReaderThread uncaught exception", exc);
703 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 }
705 }
706 };
707
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700708 private boolean isInsideDisplay(InputDevice dev) {
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700709 final InputDevice.AbsoluteInfo absx = dev.absX;
710 final InputDevice.AbsoluteInfo absy = dev.absY;
711 final InputDevice.MotionState absm = dev.mAbs;
712 if (absx == null || absy == null || absm == null) {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700713 return true;
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700714 }
715
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700716 if (absm.mNextData[MotionEvent.SAMPLE_X] >= absx.minValue
717 && absm.mNextData[MotionEvent.SAMPLE_X] <= absx.maxValue
718 && absm.mNextData[MotionEvent.SAMPLE_Y] >= absy.minValue
719 && absm.mNextData[MotionEvent.SAMPLE_Y] <= absy.maxValue) {
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700720 if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "Input ("
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700721 + absm.mNextData[MotionEvent.SAMPLE_X]
722 + "," + absm.mNextData[MotionEvent.SAMPLE_Y]
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700723 + ") inside of display");
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700724 return true;
725 }
726
727 return false;
728 }
729
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700730 private VirtualKey findVirtualKey(InputDevice dev) {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700731 final int N = mVirtualKeys.size();
732 if (N <= 0) {
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700733 return null;
734 }
735
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700736 final InputDevice.MotionState absm = dev.mAbs;
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700737 for (int i=0; i<N; i++) {
738 VirtualKey sb = mVirtualKeys.get(i);
739 sb.computeHitRect(dev, mDisplayWidth, mDisplayHeight);
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700740 if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "Hit test ("
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700741 + absm.mNextData[MotionEvent.SAMPLE_X] + ","
742 + absm.mNextData[MotionEvent.SAMPLE_Y] + ") in code "
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700743 + sb.scancode + " - (" + sb.hitLeft
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700744 + "," + sb.hitTop + ")-(" + sb.hitRight + ","
745 + sb.hitBottom + ")");
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700746 if (sb.checkHit(absm.mNextData[MotionEvent.SAMPLE_X],
747 absm.mNextData[MotionEvent.SAMPLE_Y])) {
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700748 if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "Hit!");
749 return sb;
750 }
751 }
752
753 return null;
754 }
755
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700756 private boolean generateVirtualKeyDown(InputDevice di, RawInputEvent ev,
757 long curTime, long curTimeNano) {
758 if (isInsideDisplay(di)) {
759 // Didn't consume event.
760 return false;
761 }
762
763
764 VirtualKey vk = findVirtualKey(di);
765 if (vk != null) {
766 final InputDevice.MotionState ms = di.mAbs;
767 mPressedVirtualKey = vk;
768 vk.lastKeycode = scancodeToKeycode(di.id, vk.scancode);
769 ms.mLastNumPointers = ms.mNextNumPointers;
770 di.mKeyDownTime = curTime;
771 if (DEBUG_VIRTUAL_KEYS) Log.v(TAG,
772 "Generate key down for: " + vk.scancode
773 + " (keycode=" + vk.lastKeycode + ")");
774 KeyEvent event = newKeyEvent(di, di.mKeyDownTime, curTime, true,
775 vk.lastKeycode, 0, vk.scancode,
776 KeyEvent.FLAG_VIRTUAL_HARD_KEY);
777 mHapticFeedbackCallback.virtualKeyFeedback(event);
778 addLocked(di, curTimeNano, ev.flags, RawInputEvent.CLASS_KEYBOARD,
779 event);
780 }
781
782 // We always consume the event, even if we didn't
783 // generate a key event. There are two reasons for
784 // this: to avoid spurious touches when holding
785 // the edges of the device near the touchscreen,
786 // and to avoid reporting events if there are virtual
787 // keys on the touchscreen outside of the display
788 // area.
789 // Note that for all of this we are only looking at the
790 // first pointer, since what we are handling here is the
791 // first pointer going down, and this is the coordinate
792 // that will be used to dispatch the event.
793 if (false) {
794 final InputDevice.AbsoluteInfo absx = di.absX;
795 final InputDevice.AbsoluteInfo absy = di.absY;
796 final InputDevice.MotionState absm = di.mAbs;
797 Log.v(TAG, "Rejecting ("
798 + absm.mNextData[MotionEvent.SAMPLE_X] + ","
799 + absm.mNextData[MotionEvent.SAMPLE_Y] + "): outside of ("
800 + absx.minValue + "," + absy.minValue
801 + ")-(" + absx.maxValue + ","
802 + absx.maxValue + ")");
803 }
804 return true;
805 }
806
807 private boolean monitorVirtualKey(InputDevice di, RawInputEvent ev,
808 long curTime, long curTimeNano) {
809 VirtualKey vk = mPressedVirtualKey;
810 if (vk == null) {
811 return false;
812 }
813
814 final InputDevice.MotionState ms = di.mAbs;
815 if (ms.mNextNumPointers <= 0) {
816 mPressedVirtualKey = null;
817 ms.mLastNumPointers = 0;
818 if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "Generate key up for: " + vk.scancode);
819 KeyEvent event = newKeyEvent(di, di.mKeyDownTime, curTime, false,
820 vk.lastKeycode, 0, vk.scancode,
821 KeyEvent.FLAG_VIRTUAL_HARD_KEY);
822 mHapticFeedbackCallback.virtualKeyFeedback(event);
823 addLocked(di, curTimeNano, ev.flags, RawInputEvent.CLASS_KEYBOARD,
824 event);
825 return true;
826
827 } else if (isInsideDisplay(di)) {
828 // Whoops the pointer has moved into
829 // the display area! Cancel the
830 // virtual key and start a pointer
831 // motion.
832 mPressedVirtualKey = null;
833 if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "Cancel key up for: " + vk.scancode);
834 KeyEvent event = newKeyEvent(di, di.mKeyDownTime, curTime, false,
835 vk.lastKeycode, 0, vk.scancode,
836 KeyEvent.FLAG_CANCELED | KeyEvent.FLAG_VIRTUAL_HARD_KEY);
837 mHapticFeedbackCallback.virtualKeyFeedback(event);
838 addLocked(di, curTimeNano, ev.flags, RawInputEvent.CLASS_KEYBOARD,
839 event);
840 ms.mLastNumPointers = 0;
841 return false;
842 }
843
844 return true;
845 }
846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 /**
848 * Returns a new meta state for the given keys and old state.
849 */
850 private static final int makeMetaState(int keycode, boolean down, int old) {
851 int mask;
852 switch (keycode) {
853 case KeyEvent.KEYCODE_ALT_LEFT:
854 mask = KeyEvent.META_ALT_LEFT_ON;
855 break;
856 case KeyEvent.KEYCODE_ALT_RIGHT:
857 mask = KeyEvent.META_ALT_RIGHT_ON;
858 break;
859 case KeyEvent.KEYCODE_SHIFT_LEFT:
860 mask = KeyEvent.META_SHIFT_LEFT_ON;
861 break;
862 case KeyEvent.KEYCODE_SHIFT_RIGHT:
863 mask = KeyEvent.META_SHIFT_RIGHT_ON;
864 break;
865 case KeyEvent.KEYCODE_SYM:
866 mask = KeyEvent.META_SYM_ON;
867 break;
868 default:
869 return old;
870 }
871 int result = ~(KeyEvent.META_ALT_ON | KeyEvent.META_SHIFT_ON)
872 & (down ? (old | mask) : (old & ~mask));
873 if (0 != (result & (KeyEvent.META_ALT_LEFT_ON | KeyEvent.META_ALT_RIGHT_ON))) {
874 result |= KeyEvent.META_ALT_ON;
875 }
876 if (0 != (result & (KeyEvent.META_SHIFT_LEFT_ON | KeyEvent.META_SHIFT_RIGHT_ON))) {
877 result |= KeyEvent.META_SHIFT_ON;
878 }
879 return result;
880 }
881
882 private void computeGlobalMetaStateLocked() {
883 int i = mDevices.size();
884 mGlobalMetaState = 0;
885 while ((--i) >= 0) {
886 mGlobalMetaState |= mDevices.valueAt(i).mMetaKeysState;
887 }
888 mHaveGlobalMetaState = true;
889 }
890
891 /*
892 * Return true if you want the event to get passed on to the
893 * rest of the system, and false if you've handled it and want
894 * it dropped.
895 */
896 abstract boolean preprocessEvent(InputDevice device, RawInputEvent event);
897
898 InputDevice getInputDevice(int deviceId) {
899 synchronized (mFirst) {
900 return getInputDeviceLocked(deviceId);
901 }
902 }
903
904 private InputDevice getInputDeviceLocked(int deviceId) {
905 return mDevices.get(deviceId);
906 }
907
908 public void setOrientation(int orientation) {
909 synchronized(mFirst) {
910 mOrientation = orientation;
911 switch (orientation) {
912 case Surface.ROTATION_90:
913 mKeyRotationMap = KEY_90_MAP;
914 break;
915 case Surface.ROTATION_180:
916 mKeyRotationMap = KEY_180_MAP;
917 break;
918 case Surface.ROTATION_270:
919 mKeyRotationMap = KEY_270_MAP;
920 break;
921 default:
922 mKeyRotationMap = null;
923 break;
924 }
925 }
926 }
927
928 public int rotateKeyCode(int keyCode) {
929 synchronized(mFirst) {
930 return rotateKeyCodeLocked(keyCode);
931 }
932 }
933
934 private int rotateKeyCodeLocked(int keyCode) {
935 int[] map = mKeyRotationMap;
936 if (map != null) {
937 final int N = map.length;
938 for (int i=0; i<N; i+=2) {
939 if (map[i] == keyCode) {
940 return map[i+1];
941 }
942 }
943 }
944 return keyCode;
945 }
946
947 boolean hasEvents() {
948 synchronized (mFirst) {
949 return mFirst.next != mLast;
950 }
951 }
952
953 /*
954 * returns true if we returned an event, and false if we timed out
955 */
956 QueuedEvent getEvent(long timeoutMS) {
957 long begin = SystemClock.uptimeMillis();
958 final long end = begin+timeoutMS;
959 long now = begin;
960 synchronized (mFirst) {
961 while (mFirst.next == mLast && end > now) {
962 try {
963 mWakeLock.release();
964 mFirst.wait(end-now);
965 }
966 catch (InterruptedException e) {
967 }
968 now = SystemClock.uptimeMillis();
969 if (begin > now) {
970 begin = now;
971 }
972 }
973 if (mFirst.next == mLast) {
974 return null;
975 }
976 QueuedEvent p = mFirst.next;
977 mFirst.next = p.next;
978 mFirst.next.prev = mFirst;
979 p.inQueue = false;
980 return p;
981 }
982 }
983
Dianne Hackborn83fe3f52009-09-12 23:38:30 -0700984 /**
985 * Return true if the queue has an up event pending that corresponds
986 * to the same key as the given key event.
987 */
988 boolean hasKeyUpEvent(KeyEvent origEvent) {
989 synchronized (mFirst) {
990 final int keyCode = origEvent.getKeyCode();
991 QueuedEvent cur = mLast.prev;
992 while (cur.prev != null) {
993 if (cur.classType == RawInputEvent.CLASS_KEYBOARD) {
994 KeyEvent ke = (KeyEvent)cur.event;
995 if (ke.getAction() == KeyEvent.ACTION_UP
996 && ke.getKeyCode() == keyCode) {
997 return true;
998 }
999 }
1000 cur = cur.prev;
1001 }
1002 }
1003
1004 return false;
1005 }
1006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 void recycleEvent(QueuedEvent ev) {
1008 synchronized (mFirst) {
1009 //Log.i(TAG, "Recycle event: " + ev);
1010 if (ev.event == ev.inputDevice.mAbs.currentMove) {
1011 ev.inputDevice.mAbs.currentMove = null;
1012 }
1013 if (ev.event == ev.inputDevice.mRel.currentMove) {
1014 if (false) Log.i(TAG, "Detach rel " + ev.event);
1015 ev.inputDevice.mRel.currentMove = null;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001016 ev.inputDevice.mRel.mNextData[MotionEvent.SAMPLE_X] = 0;
1017 ev.inputDevice.mRel.mNextData[MotionEvent.SAMPLE_Y] = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 }
1019 recycleLocked(ev);
1020 }
1021 }
1022
1023 void filterQueue(FilterCallback cb) {
1024 synchronized (mFirst) {
1025 QueuedEvent cur = mLast.prev;
1026 while (cur.prev != null) {
1027 switch (cb.filterEvent(cur)) {
1028 case FILTER_REMOVE:
1029 cur.prev.next = cur.next;
1030 cur.next.prev = cur.prev;
1031 break;
1032 case FILTER_ABORT:
1033 return;
1034 }
1035 cur = cur.prev;
1036 }
1037 }
1038 }
1039
Michael Chan53071d62009-05-13 17:29:48 -07001040 private QueuedEvent obtainLocked(InputDevice device, long whenNano,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 int flags, int classType, Object event) {
1042 QueuedEvent ev;
1043 if (mCacheCount == 0) {
1044 ev = new QueuedEvent();
1045 } else {
1046 ev = mCache;
1047 ev.inQueue = false;
1048 mCache = ev.next;
1049 mCacheCount--;
1050 }
1051 ev.inputDevice = device;
Michael Chan53071d62009-05-13 17:29:48 -07001052 ev.whenNano = whenNano;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001053 ev.flags = flags;
1054 ev.classType = classType;
1055 ev.event = event;
1056 return ev;
1057 }
1058
1059 private void recycleLocked(QueuedEvent ev) {
1060 if (ev.inQueue) {
1061 throw new RuntimeException("Event already in queue!");
1062 }
1063 if (mCacheCount < 10) {
1064 mCacheCount++;
1065 ev.next = mCache;
1066 mCache = ev;
1067 ev.inQueue = true;
1068 }
1069 }
1070
Michael Chan53071d62009-05-13 17:29:48 -07001071 private void addLocked(InputDevice device, long whenNano, int flags,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 int classType, Object event) {
1073 boolean poke = mFirst.next == mLast;
1074
Michael Chan53071d62009-05-13 17:29:48 -07001075 QueuedEvent ev = obtainLocked(device, whenNano, flags, classType, event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 QueuedEvent p = mLast.prev;
Michael Chan53071d62009-05-13 17:29:48 -07001077 while (p != mFirst && ev.whenNano < p.whenNano) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 p = p.prev;
1079 }
1080
1081 ev.next = p.next;
1082 ev.prev = p;
1083 p.next = ev;
1084 ev.next.prev = ev;
1085 ev.inQueue = true;
1086
1087 if (poke) {
Michael Chan53071d62009-05-13 17:29:48 -07001088 long time;
1089 if (MEASURE_LATENCY) {
1090 time = System.nanoTime();
1091 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 mFirst.notify();
1093 mWakeLock.acquire();
Michael Chan53071d62009-05-13 17:29:48 -07001094 if (MEASURE_LATENCY) {
1095 lt.sample("1 addLocked-queued event ", System.nanoTime() - time);
1096 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 }
1098 }
1099
1100 private InputDevice newInputDevice(int deviceId) {
1101 int classes = getDeviceClasses(deviceId);
1102 String name = getDeviceName(deviceId);
Dianne Hackborna8f60182009-09-01 19:01:50 -07001103 InputDevice.AbsoluteInfo absX = null;
1104 InputDevice.AbsoluteInfo absY = null;
1105 InputDevice.AbsoluteInfo absPressure = null;
1106 InputDevice.AbsoluteInfo absSize = null;
1107 if (classes != 0) {
1108 Log.i(TAG, "Device added: id=0x" + Integer.toHexString(deviceId)
1109 + ", name=" + name
1110 + ", classes=" + Integer.toHexString(classes));
1111 if ((classes&RawInputEvent.CLASS_TOUCHSCREEN_MT) != 0) {
1112 absX = loadAbsoluteInfo(deviceId,
1113 RawInputEvent.ABS_MT_POSITION_X, "X");
1114 absY = loadAbsoluteInfo(deviceId,
1115 RawInputEvent.ABS_MT_POSITION_Y, "Y");
1116 absPressure = loadAbsoluteInfo(deviceId,
1117 RawInputEvent.ABS_MT_TOUCH_MAJOR, "Pressure");
1118 absSize = loadAbsoluteInfo(deviceId,
1119 RawInputEvent.ABS_MT_WIDTH_MAJOR, "Size");
1120 } else if ((classes&RawInputEvent.CLASS_TOUCHSCREEN) != 0) {
1121 absX = loadAbsoluteInfo(deviceId,
1122 RawInputEvent.ABS_X, "X");
1123 absY = loadAbsoluteInfo(deviceId,
1124 RawInputEvent.ABS_Y, "Y");
1125 absPressure = loadAbsoluteInfo(deviceId,
1126 RawInputEvent.ABS_PRESSURE, "Pressure");
1127 absSize = loadAbsoluteInfo(deviceId,
1128 RawInputEvent.ABS_TOOL_WIDTH, "Size");
1129 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130 }
1131
1132 return new InputDevice(deviceId, classes, name, absX, absY, absPressure, absSize);
1133 }
1134
1135 private InputDevice.AbsoluteInfo loadAbsoluteInfo(int id, int channel,
1136 String name) {
1137 InputDevice.AbsoluteInfo info = new InputDevice.AbsoluteInfo();
1138 if (getAbsoluteInfo(id, channel, info)
1139 && info.minValue != info.maxValue) {
1140 Log.i(TAG, " " + name + ": min=" + info.minValue
1141 + " max=" + info.maxValue
1142 + " flat=" + info.flat
1143 + " fuzz=" + info.fuzz);
1144 info.range = info.maxValue-info.minValue;
1145 return info;
1146 }
1147 Log.i(TAG, " " + name + ": unknown values");
1148 return null;
1149 }
1150 private static native boolean readEvent(RawInputEvent outEvent);
1151}