blob: cfb3e35bec54ecbc15b47a6e0693e9251e98c2d9 [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>();
57 final ArrayList<VirtualKey> mVirtualKeys = new ArrayList<VirtualKey>();
Dianne Hackbornddca3ee2009-07-23 19:01:31 -070058 final HapticFeedbackCallback mHapticFeedbackCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059
60 int mGlobalMetaState = 0;
61 boolean mHaveGlobalMetaState = false;
62
63 final QueuedEvent mFirst;
64 final QueuedEvent mLast;
65 QueuedEvent mCache;
66 int mCacheCount;
67
68 Display mDisplay = null;
Dianne Hackborne3dd8842009-07-14 12:06:54 -070069 int mDisplayWidth;
70 int mDisplayHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071
72 int mOrientation = Surface.ROTATION_0;
73 int[] mKeyRotationMap = null;
74
Dianne Hackborne3dd8842009-07-14 12:06:54 -070075 VirtualKey mPressedVirtualKey = null;
76
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 PowerManager.WakeLock mWakeLock;
78
79 static final int[] KEY_90_MAP = new int[] {
80 KeyEvent.KEYCODE_DPAD_DOWN, KeyEvent.KEYCODE_DPAD_RIGHT,
81 KeyEvent.KEYCODE_DPAD_RIGHT, KeyEvent.KEYCODE_DPAD_UP,
82 KeyEvent.KEYCODE_DPAD_UP, KeyEvent.KEYCODE_DPAD_LEFT,
83 KeyEvent.KEYCODE_DPAD_LEFT, KeyEvent.KEYCODE_DPAD_DOWN,
84 };
85
86 static final int[] KEY_180_MAP = new int[] {
87 KeyEvent.KEYCODE_DPAD_DOWN, KeyEvent.KEYCODE_DPAD_UP,
88 KeyEvent.KEYCODE_DPAD_RIGHT, KeyEvent.KEYCODE_DPAD_LEFT,
89 KeyEvent.KEYCODE_DPAD_UP, KeyEvent.KEYCODE_DPAD_DOWN,
90 KeyEvent.KEYCODE_DPAD_LEFT, KeyEvent.KEYCODE_DPAD_RIGHT,
91 };
92
93 static final int[] KEY_270_MAP = new int[] {
94 KeyEvent.KEYCODE_DPAD_DOWN, KeyEvent.KEYCODE_DPAD_LEFT,
95 KeyEvent.KEYCODE_DPAD_LEFT, KeyEvent.KEYCODE_DPAD_UP,
96 KeyEvent.KEYCODE_DPAD_UP, KeyEvent.KEYCODE_DPAD_RIGHT,
97 KeyEvent.KEYCODE_DPAD_RIGHT, KeyEvent.KEYCODE_DPAD_DOWN,
98 };
99
100 public static final int FILTER_REMOVE = 0;
101 public static final int FILTER_KEEP = 1;
102 public static final int FILTER_ABORT = -1;
Michael Chan53071d62009-05-13 17:29:48 -0700103
104 private static final boolean MEASURE_LATENCY = false;
105 private LatencyTimer lt;
106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 public interface FilterCallback {
108 int filterEvent(QueuedEvent ev);
109 }
110
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700111 public interface HapticFeedbackCallback {
112 void virtualKeyFeedback(KeyEvent event);
113 }
114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 static class QueuedEvent {
116 InputDevice inputDevice;
Michael Chan53071d62009-05-13 17:29:48 -0700117 long whenNano;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 int flags; // From the raw event
119 int classType; // One of the class constants in InputEvent
120 Object event;
121 boolean inQueue;
122
123 void copyFrom(QueuedEvent that) {
124 this.inputDevice = that.inputDevice;
Michael Chan53071d62009-05-13 17:29:48 -0700125 this.whenNano = that.whenNano;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 this.flags = that.flags;
127 this.classType = that.classType;
128 this.event = that.event;
129 }
130
131 @Override
132 public String toString() {
133 return "QueuedEvent{"
134 + Integer.toHexString(System.identityHashCode(this))
135 + " " + event + "}";
136 }
137
138 // not copied
139 QueuedEvent prev;
140 QueuedEvent next;
141 }
142
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700143 /**
144 * A key that exists as a part of the touch-screen, outside of the normal
145 * display area of the screen.
146 */
147 static class VirtualKey {
148 int scancode;
149 int centerx;
150 int centery;
151 int width;
152 int height;
153
154 int hitLeft;
155 int hitTop;
156 int hitRight;
157 int hitBottom;
158
159 InputDevice lastDevice;
160 int lastKeycode;
161
162 boolean checkHit(int x, int y) {
163 return (x >= hitLeft && x <= hitRight
164 && y >= hitTop && y <= hitBottom);
165 }
166
167 void computeHitRect(InputDevice dev, int dw, int dh) {
168 if (dev == lastDevice) {
169 return;
170 }
171
172 if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "computeHitRect for " + scancode
173 + ": dev=" + dev + " absX=" + dev.absX + " absY=" + dev.absY);
174
175 lastDevice = dev;
176
177 int minx = dev.absX.minValue;
178 int maxx = dev.absX.maxValue;
179
180 int halfw = width/2;
181 int left = centerx - halfw;
182 int right = centerx + halfw;
183 hitLeft = minx + ((left*maxx-minx)/dw);
184 hitRight = minx + ((right*maxx-minx)/dw);
185
186 int miny = dev.absY.minValue;
187 int maxy = dev.absY.maxValue;
188
189 int halfh = height/2;
190 int top = centery - halfh;
191 int bottom = centery + halfh;
192 hitTop = miny + ((top*maxy-miny)/dh);
193 hitBottom = miny + ((bottom*maxy-miny)/dh);
194 }
195 }
Michael Chan53071d62009-05-13 17:29:48 -0700196
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400197 private void readVirtualKeys() {
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700198 try {
199 FileInputStream fis = new FileInputStream(
200 "/sys/board_properties/virtualkeys.synaptics-rmi-touchscreen");
201 InputStreamReader isr = new InputStreamReader(fis);
202 BufferedReader br = new BufferedReader(isr);
203 String str = br.readLine();
204 if (str != null) {
205 String[] it = str.split(":");
206 if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "***** VIRTUAL KEYS: " + it);
207 final int N = it.length-6;
208 for (int i=0; i<=N; i+=6) {
209 if (!"0x01".equals(it[i])) {
210 Log.w(TAG, "Unknown virtual key type at elem #" + i
211 + ": " + it[i]);
212 continue;
213 }
214 try {
215 VirtualKey sb = new VirtualKey();
216 sb.scancode = Integer.parseInt(it[i+1]);
217 sb.centerx = Integer.parseInt(it[i+2]);
218 sb.centery = Integer.parseInt(it[i+3]);
219 sb.width = Integer.parseInt(it[i+4]);
220 sb.height = Integer.parseInt(it[i+5]);
221 if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "Virtual key "
222 + sb.scancode + ": center=" + sb.centerx + ","
223 + sb.centery + " size=" + sb.width + "x"
224 + sb.height);
225 mVirtualKeys.add(sb);
226 } catch (NumberFormatException e) {
227 Log.w(TAG, "Bad number at region " + i + " in: "
228 + str, e);
229 }
230 }
231 }
232 br.close();
233 } catch (FileNotFoundException e) {
234 Log.i(TAG, "No virtual keys found");
235 } catch (IOException e) {
236 Log.w(TAG, "Error reading virtual keys", e);
237 }
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400238 }
239
240 private void readExcludedDevices() {
241 // Read partner-provided list of excluded input devices
242 XmlPullParser parser = null;
243 // Environment.getRootDirectory() is a fancy way of saying ANDROID_ROOT or "/system".
244 File confFile = new File(Environment.getRootDirectory(), EXCLUDED_DEVICES_PATH);
245 FileReader confreader = null;
246 try {
247 confreader = new FileReader(confFile);
248 parser = Xml.newPullParser();
249 parser.setInput(confreader);
250 XmlUtils.beginDocument(parser, "devices");
251
252 while (true) {
253 XmlUtils.nextElement(parser);
254 if (!"device".equals(parser.getName())) {
255 break;
256 }
257 String name = parser.getAttributeValue(null, "name");
258 if (name != null) {
259 Log.d(TAG, "addExcludedDevice " + name);
260 addExcludedDevice(name);
261 }
262 }
263 } catch (FileNotFoundException e) {
264 // It's ok if the file does not exist.
265 } catch (Exception e) {
266 Log.e(TAG, "Exception while parsing '" + confFile.getAbsolutePath() + "'", e);
267 } finally {
268 try { if (confreader != null) confreader.close(); } catch (IOException e) { }
269 }
270 }
271
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700272 KeyInputQueue(Context context, HapticFeedbackCallback hapticFeedbackCallback) {
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400273 if (MEASURE_LATENCY) {
274 lt = new LatencyTimer(100, 1000);
275 }
276
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700277 mHapticFeedbackCallback = hapticFeedbackCallback;
278
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400279 readVirtualKeys();
280 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);
395 mDevices.put(ev.deviceId, di);
396 configChanged = true;
397 }
398 } else if (ev.type == RawInputEvent.EV_DEVICE_REMOVED) {
399 synchronized (mFirst) {
400 Log.i(TAG, "Device removed: id=0x"
401 + Integer.toHexString(ev.deviceId));
402 di = mDevices.get(ev.deviceId);
403 if (di != null) {
404 mDevices.delete(ev.deviceId);
405 configChanged = true;
406 } else {
407 Log.w(TAG, "Bad device id: " + ev.deviceId);
408 }
409 }
410 } else {
411 di = getInputDevice(ev.deviceId);
412
413 // first crack at it
414 send = preprocessEvent(di, ev);
415
416 if (ev.type == RawInputEvent.EV_KEY) {
417 di.mMetaKeysState = makeMetaState(ev.keycode,
418 ev.value != 0, di.mMetaKeysState);
419 mHaveGlobalMetaState = false;
420 }
421 }
422
423 if (di == null) {
424 continue;
425 }
426
427 if (configChanged) {
428 synchronized (mFirst) {
Michael Chan53071d62009-05-13 17:29:48 -0700429 addLocked(di, System.nanoTime(), 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 RawInputEvent.CLASS_CONFIGURATION_CHANGED,
431 null);
432 }
433 }
434
435 if (!send) {
436 continue;
437 }
438
439 synchronized (mFirst) {
440 // NOTE: The event timebase absolutely must be the same
441 // timebase as SystemClock.uptimeMillis().
442 //curTime = gotOne ? ev.when : SystemClock.uptimeMillis();
443 final long curTime = SystemClock.uptimeMillis();
Michael Chan53071d62009-05-13 17:29:48 -0700444 final long curTimeNano = System.nanoTime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 //Log.i(TAG, "curTime=" + curTime + ", systemClock=" + SystemClock.uptimeMillis());
446
447 final int classes = di.classes;
448 final int type = ev.type;
449 final int scancode = ev.scancode;
450 send = false;
451
452 // Is it a key event?
453 if (type == RawInputEvent.EV_KEY &&
454 (classes&RawInputEvent.CLASS_KEYBOARD) != 0 &&
455 (scancode < RawInputEvent.BTN_FIRST ||
456 scancode > RawInputEvent.BTN_LAST)) {
457 boolean down;
458 if (ev.value != 0) {
459 down = true;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700460 di.mKeyDownTime = curTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 } else {
462 down = false;
463 }
464 int keycode = rotateKeyCodeLocked(ev.keycode);
Michael Chan53071d62009-05-13 17:29:48 -0700465 addLocked(di, curTimeNano, ev.flags,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 RawInputEvent.CLASS_KEYBOARD,
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700467 newKeyEvent(di, di.mKeyDownTime, curTime, down,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 keycode, 0, scancode,
469 ((ev.flags & WindowManagerPolicy.FLAG_WOKE_HERE) != 0)
470 ? KeyEvent.FLAG_WOKE_HERE : 0));
471 } else if (ev.type == RawInputEvent.EV_KEY) {
472 if (ev.scancode == RawInputEvent.BTN_TOUCH &&
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700473 (classes&(RawInputEvent.CLASS_TOUCHSCREEN
474 |RawInputEvent.CLASS_TOUCHSCREEN_MT))
475 == RawInputEvent.CLASS_TOUCHSCREEN) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 di.mAbs.changed = true;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700477 di.mAbs.mDown[0] = ev.value != 0;
478 } else if (ev.scancode == RawInputEvent.BTN_2 &&
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700479 (classes&(RawInputEvent.CLASS_TOUCHSCREEN
480 |RawInputEvent.CLASS_TOUCHSCREEN_MT))
481 == RawInputEvent.CLASS_TOUCHSCREEN) {
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700482 di.mAbs.changed = true;
483 di.mAbs.mDown[1] = ev.value != 0;
484 } else if (ev.scancode == RawInputEvent.BTN_MOUSE &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 (classes&RawInputEvent.CLASS_TRACKBALL) != 0) {
486 di.mRel.changed = true;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700487 di.mRel.mNextNumPointers = ev.value != 0 ? 1 : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 send = true;
489 }
490
491 } else if (ev.type == RawInputEvent.EV_ABS &&
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700492 (classes&RawInputEvent.CLASS_TOUCHSCREEN_MT) != 0) {
493 if (ev.scancode == RawInputEvent.ABS_MT_TOUCH_MAJOR) {
494 di.mAbs.changed = true;
495 di.mAbs.mNextData[di.mAbs.mAddingPointerOffset
496 + MotionEvent.SAMPLE_PRESSURE] = ev.value;
497 } else if (ev.scancode == RawInputEvent.ABS_MT_POSITION_X) {
498 di.mAbs.changed = true;
499 di.mAbs.mNextData[di.mAbs.mAddingPointerOffset
500 + MotionEvent.SAMPLE_X] = ev.value;
501 if (DEBUG_POINTERS) Log.v(TAG, "MT @"
502 + di.mAbs.mAddingPointerOffset
503 + " X:" + ev.value);
504 } else if (ev.scancode == RawInputEvent.ABS_MT_POSITION_Y) {
505 di.mAbs.changed = true;
506 di.mAbs.mNextData[di.mAbs.mAddingPointerOffset
507 + MotionEvent.SAMPLE_Y] = ev.value;
508 if (DEBUG_POINTERS) Log.v(TAG, "MT @"
509 + di.mAbs.mAddingPointerOffset
510 + " Y:" + ev.value);
511 } else if (ev.scancode == RawInputEvent.ABS_MT_WIDTH_MAJOR) {
512 di.mAbs.changed = true;
513 di.mAbs.mNextData[di.mAbs.mAddingPointerOffset
514 + MotionEvent.SAMPLE_SIZE] = ev.value;
515 }
516
517 } else if (ev.type == RawInputEvent.EV_ABS &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 (classes&RawInputEvent.CLASS_TOUCHSCREEN) != 0) {
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700519 // Finger 1
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 if (ev.scancode == RawInputEvent.ABS_X) {
521 di.mAbs.changed = true;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700522 di.mAbs.mNextData[MotionEvent.SAMPLE_X] = ev.value;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 } else if (ev.scancode == RawInputEvent.ABS_Y) {
524 di.mAbs.changed = true;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700525 di.mAbs.mNextData[MotionEvent.SAMPLE_Y] = ev.value;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 } else if (ev.scancode == RawInputEvent.ABS_PRESSURE) {
527 di.mAbs.changed = true;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700528 di.mAbs.mNextData[MotionEvent.SAMPLE_PRESSURE] = ev.value;
529 di.mAbs.mNextData[MotionEvent.NUM_SAMPLE_DATA
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700530 + MotionEvent.SAMPLE_PRESSURE] = ev.value;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 } else if (ev.scancode == RawInputEvent.ABS_TOOL_WIDTH) {
532 di.mAbs.changed = true;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700533 di.mAbs.mNextData[MotionEvent.SAMPLE_SIZE] = ev.value;
534 di.mAbs.mNextData[MotionEvent.NUM_SAMPLE_DATA
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700535 + MotionEvent.SAMPLE_SIZE] = ev.value;
536
537 // Finger 2
538 } else if (ev.scancode == RawInputEvent.ABS_HAT0X) {
539 di.mAbs.changed = true;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700540 di.mAbs.mNextData[(di.mAbs.mDown[0] ?
541 MotionEvent.NUM_SAMPLE_DATA : 0)
542 + MotionEvent.SAMPLE_X] = ev.value;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700543 } else if (ev.scancode == RawInputEvent.ABS_HAT0Y) {
544 di.mAbs.changed = true;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700545 di.mAbs.mNextData[(di.mAbs.mDown[0] ?
546 MotionEvent.NUM_SAMPLE_DATA : 0)
547 + MotionEvent.SAMPLE_Y] = ev.value;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 }
549
550 } else if (ev.type == RawInputEvent.EV_REL &&
551 (classes&RawInputEvent.CLASS_TRACKBALL) != 0) {
552 // Add this relative movement into our totals.
553 if (ev.scancode == RawInputEvent.REL_X) {
554 di.mRel.changed = true;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700555 di.mRel.mNextData[MotionEvent.SAMPLE_X] += ev.value;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 } else if (ev.scancode == RawInputEvent.REL_Y) {
557 di.mRel.changed = true;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700558 di.mRel.mNextData[MotionEvent.SAMPLE_Y] += ev.value;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 }
560 }
561
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700562 if (ev.type == RawInputEvent.EV_SYN
563 && ev.scancode == RawInputEvent.SYN_MT_REPORT
564 && di.mAbs != null) {
565 di.mAbs.changed = true;
566 if (di.mAbs.mNextData[MotionEvent.SAMPLE_PRESSURE] > 0) {
567 // If the value is <= 0, the pointer is not
568 // down, so keep it in the count.
569
570 if (di.mAbs.mNextData[di.mAbs.mAddingPointerOffset
571 + MotionEvent.SAMPLE_PRESSURE] != 0) {
572 final int num = di.mAbs.mNextNumPointers+1;
573 di.mAbs.mNextNumPointers = num;
574 if (DEBUG_POINTERS) Log.v(TAG,
575 "MT_REPORT: now have " + num + " pointers");
576 final int newOffset = (num <= InputDevice.MAX_POINTERS)
577 ? (num * MotionEvent.NUM_SAMPLE_DATA)
578 : (InputDevice.MAX_POINTERS *
579 MotionEvent.NUM_SAMPLE_DATA);
580 di.mAbs.mAddingPointerOffset = newOffset;
581 di.mAbs.mNextData[newOffset
582 + MotionEvent.SAMPLE_PRESSURE] = 0;
583 } else {
584 if (DEBUG_POINTERS) Log.v(TAG, "MT_REPORT: no pointer");
585 }
586 }
587 } else if (send || (ev.type == RawInputEvent.EV_SYN
588 && ev.scancode == RawInputEvent.SYN_REPORT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589 if (mDisplay != null) {
590 if (!mHaveGlobalMetaState) {
591 computeGlobalMetaStateLocked();
592 }
593
594 MotionEvent me;
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700595
596 InputDevice.MotionState ms = di.mAbs;
597 if (ms.changed) {
598 ms.changed = false;
599
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700600 if ((classes&(RawInputEvent.CLASS_TOUCHSCREEN
601 |RawInputEvent.CLASS_TOUCHSCREEN_MT))
602 == RawInputEvent.CLASS_TOUCHSCREEN) {
603 ms.mNextNumPointers = 0;
604 if (ms.mDown[0]) ms.mNextNumPointers++;
605 if (ms.mDown[1]) ms.mNextNumPointers++;
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700606 }
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700607
608 boolean doMotion = !monitorVirtualKey(di,
609 ev, curTime, curTimeNano);
610
611 if (doMotion && ms.mNextNumPointers > 0
612 && ms.mLastNumPointers == 0) {
613 doMotion = !generateVirtualKeyDown(di,
614 ev, curTime, curTimeNano);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 }
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700616
617 if (doMotion) {
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700618 // XXX Need to be able to generate
619 // multiple events here, for example
620 // if two fingers change up/down state
621 // at the same time.
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700622 do {
623 me = ms.generateAbsMotion(di, curTime,
624 curTimeNano, mDisplay,
625 mOrientation, mGlobalMetaState);
626 if (false) Log.v(TAG, "Absolute: x="
627 + di.mAbs.mNextData[MotionEvent.SAMPLE_X]
628 + " y="
629 + di.mAbs.mNextData[MotionEvent.SAMPLE_Y]
630 + " ev=" + me);
631 if (me != null) {
632 if (WindowManagerPolicy.WATCH_POINTER) {
633 Log.i(TAG, "Enqueueing: " + me);
634 }
635 addLocked(di, curTimeNano, ev.flags,
636 RawInputEvent.CLASS_TOUCHSCREEN, me);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700637 }
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700638 } while (ms.hasMore());
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700639 }
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700640
641 ms.finish();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 }
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700643
644 ms = di.mRel;
645 if (ms.changed) {
646 ms.changed = false;
647
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700648 me = ms.generateRelMotion(di, curTime,
649 curTimeNano,
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700650 mOrientation, mGlobalMetaState);
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700651 if (false) Log.v(TAG, "Relative: x="
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700652 + di.mRel.mNextData[MotionEvent.SAMPLE_X]
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700653 + " y="
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700654 + di.mRel.mNextData[MotionEvent.SAMPLE_Y]
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700655 + " ev=" + me);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700656 if (me != null) {
657 addLocked(di, curTimeNano, ev.flags,
658 RawInputEvent.CLASS_TRACKBALL, me);
659 }
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700660
661 ms.finish();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662 }
663 }
664 }
665 }
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700666
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700667 } catch (RuntimeException exc) {
668 Log.e(TAG, "InputReaderThread uncaught exception", exc);
669 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 }
671 }
672 };
673
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700674 private boolean isInsideDisplay(InputDevice dev) {
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700675 final InputDevice.AbsoluteInfo absx = dev.absX;
676 final InputDevice.AbsoluteInfo absy = dev.absY;
677 final InputDevice.MotionState absm = dev.mAbs;
678 if (absx == null || absy == null || absm == null) {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700679 return true;
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700680 }
681
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700682 if (absm.mNextData[MotionEvent.SAMPLE_X] >= absx.minValue
683 && absm.mNextData[MotionEvent.SAMPLE_X] <= absx.maxValue
684 && absm.mNextData[MotionEvent.SAMPLE_Y] >= absy.minValue
685 && absm.mNextData[MotionEvent.SAMPLE_Y] <= absy.maxValue) {
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700686 if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "Input ("
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700687 + absm.mNextData[MotionEvent.SAMPLE_X]
688 + "," + absm.mNextData[MotionEvent.SAMPLE_Y]
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700689 + ") inside of display");
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700690 return true;
691 }
692
693 return false;
694 }
695
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700696 private VirtualKey findVirtualKey(InputDevice dev) {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700697 final int N = mVirtualKeys.size();
698 if (N <= 0) {
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700699 return null;
700 }
701
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700702 final InputDevice.MotionState absm = dev.mAbs;
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700703 for (int i=0; i<N; i++) {
704 VirtualKey sb = mVirtualKeys.get(i);
705 sb.computeHitRect(dev, mDisplayWidth, mDisplayHeight);
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700706 if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "Hit test ("
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700707 + absm.mNextData[MotionEvent.SAMPLE_X] + ","
708 + absm.mNextData[MotionEvent.SAMPLE_Y] + ") in code "
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700709 + sb.scancode + " - (" + sb.hitLeft
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700710 + "," + sb.hitTop + ")-(" + sb.hitRight + ","
711 + sb.hitBottom + ")");
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700712 if (sb.checkHit(absm.mNextData[MotionEvent.SAMPLE_X],
713 absm.mNextData[MotionEvent.SAMPLE_Y])) {
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700714 if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "Hit!");
715 return sb;
716 }
717 }
718
719 return null;
720 }
721
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700722 private boolean generateVirtualKeyDown(InputDevice di, RawInputEvent ev,
723 long curTime, long curTimeNano) {
724 if (isInsideDisplay(di)) {
725 // Didn't consume event.
726 return false;
727 }
728
729
730 VirtualKey vk = findVirtualKey(di);
731 if (vk != null) {
732 final InputDevice.MotionState ms = di.mAbs;
733 mPressedVirtualKey = vk;
734 vk.lastKeycode = scancodeToKeycode(di.id, vk.scancode);
735 ms.mLastNumPointers = ms.mNextNumPointers;
736 di.mKeyDownTime = curTime;
737 if (DEBUG_VIRTUAL_KEYS) Log.v(TAG,
738 "Generate key down for: " + vk.scancode
739 + " (keycode=" + vk.lastKeycode + ")");
740 KeyEvent event = newKeyEvent(di, di.mKeyDownTime, curTime, true,
741 vk.lastKeycode, 0, vk.scancode,
742 KeyEvent.FLAG_VIRTUAL_HARD_KEY);
743 mHapticFeedbackCallback.virtualKeyFeedback(event);
744 addLocked(di, curTimeNano, ev.flags, RawInputEvent.CLASS_KEYBOARD,
745 event);
746 }
747
748 // We always consume the event, even if we didn't
749 // generate a key event. There are two reasons for
750 // this: to avoid spurious touches when holding
751 // the edges of the device near the touchscreen,
752 // and to avoid reporting events if there are virtual
753 // keys on the touchscreen outside of the display
754 // area.
755 // Note that for all of this we are only looking at the
756 // first pointer, since what we are handling here is the
757 // first pointer going down, and this is the coordinate
758 // that will be used to dispatch the event.
759 if (false) {
760 final InputDevice.AbsoluteInfo absx = di.absX;
761 final InputDevice.AbsoluteInfo absy = di.absY;
762 final InputDevice.MotionState absm = di.mAbs;
763 Log.v(TAG, "Rejecting ("
764 + absm.mNextData[MotionEvent.SAMPLE_X] + ","
765 + absm.mNextData[MotionEvent.SAMPLE_Y] + "): outside of ("
766 + absx.minValue + "," + absy.minValue
767 + ")-(" + absx.maxValue + ","
768 + absx.maxValue + ")");
769 }
770 return true;
771 }
772
773 private boolean monitorVirtualKey(InputDevice di, RawInputEvent ev,
774 long curTime, long curTimeNano) {
775 VirtualKey vk = mPressedVirtualKey;
776 if (vk == null) {
777 return false;
778 }
779
780 final InputDevice.MotionState ms = di.mAbs;
781 if (ms.mNextNumPointers <= 0) {
782 mPressedVirtualKey = null;
783 ms.mLastNumPointers = 0;
784 if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "Generate key up for: " + vk.scancode);
785 KeyEvent event = newKeyEvent(di, di.mKeyDownTime, curTime, false,
786 vk.lastKeycode, 0, vk.scancode,
787 KeyEvent.FLAG_VIRTUAL_HARD_KEY);
788 mHapticFeedbackCallback.virtualKeyFeedback(event);
789 addLocked(di, curTimeNano, ev.flags, RawInputEvent.CLASS_KEYBOARD,
790 event);
791 return true;
792
793 } else if (isInsideDisplay(di)) {
794 // Whoops the pointer has moved into
795 // the display area! Cancel the
796 // virtual key and start a pointer
797 // motion.
798 mPressedVirtualKey = null;
799 if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "Cancel key up for: " + vk.scancode);
800 KeyEvent event = newKeyEvent(di, di.mKeyDownTime, curTime, false,
801 vk.lastKeycode, 0, vk.scancode,
802 KeyEvent.FLAG_CANCELED | KeyEvent.FLAG_VIRTUAL_HARD_KEY);
803 mHapticFeedbackCallback.virtualKeyFeedback(event);
804 addLocked(di, curTimeNano, ev.flags, RawInputEvent.CLASS_KEYBOARD,
805 event);
806 ms.mLastNumPointers = 0;
807 return false;
808 }
809
810 return true;
811 }
812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800813 /**
814 * Returns a new meta state for the given keys and old state.
815 */
816 private static final int makeMetaState(int keycode, boolean down, int old) {
817 int mask;
818 switch (keycode) {
819 case KeyEvent.KEYCODE_ALT_LEFT:
820 mask = KeyEvent.META_ALT_LEFT_ON;
821 break;
822 case KeyEvent.KEYCODE_ALT_RIGHT:
823 mask = KeyEvent.META_ALT_RIGHT_ON;
824 break;
825 case KeyEvent.KEYCODE_SHIFT_LEFT:
826 mask = KeyEvent.META_SHIFT_LEFT_ON;
827 break;
828 case KeyEvent.KEYCODE_SHIFT_RIGHT:
829 mask = KeyEvent.META_SHIFT_RIGHT_ON;
830 break;
831 case KeyEvent.KEYCODE_SYM:
832 mask = KeyEvent.META_SYM_ON;
833 break;
834 default:
835 return old;
836 }
837 int result = ~(KeyEvent.META_ALT_ON | KeyEvent.META_SHIFT_ON)
838 & (down ? (old | mask) : (old & ~mask));
839 if (0 != (result & (KeyEvent.META_ALT_LEFT_ON | KeyEvent.META_ALT_RIGHT_ON))) {
840 result |= KeyEvent.META_ALT_ON;
841 }
842 if (0 != (result & (KeyEvent.META_SHIFT_LEFT_ON | KeyEvent.META_SHIFT_RIGHT_ON))) {
843 result |= KeyEvent.META_SHIFT_ON;
844 }
845 return result;
846 }
847
848 private void computeGlobalMetaStateLocked() {
849 int i = mDevices.size();
850 mGlobalMetaState = 0;
851 while ((--i) >= 0) {
852 mGlobalMetaState |= mDevices.valueAt(i).mMetaKeysState;
853 }
854 mHaveGlobalMetaState = true;
855 }
856
857 /*
858 * Return true if you want the event to get passed on to the
859 * rest of the system, and false if you've handled it and want
860 * it dropped.
861 */
862 abstract boolean preprocessEvent(InputDevice device, RawInputEvent event);
863
864 InputDevice getInputDevice(int deviceId) {
865 synchronized (mFirst) {
866 return getInputDeviceLocked(deviceId);
867 }
868 }
869
870 private InputDevice getInputDeviceLocked(int deviceId) {
871 return mDevices.get(deviceId);
872 }
873
874 public void setOrientation(int orientation) {
875 synchronized(mFirst) {
876 mOrientation = orientation;
877 switch (orientation) {
878 case Surface.ROTATION_90:
879 mKeyRotationMap = KEY_90_MAP;
880 break;
881 case Surface.ROTATION_180:
882 mKeyRotationMap = KEY_180_MAP;
883 break;
884 case Surface.ROTATION_270:
885 mKeyRotationMap = KEY_270_MAP;
886 break;
887 default:
888 mKeyRotationMap = null;
889 break;
890 }
891 }
892 }
893
894 public int rotateKeyCode(int keyCode) {
895 synchronized(mFirst) {
896 return rotateKeyCodeLocked(keyCode);
897 }
898 }
899
900 private int rotateKeyCodeLocked(int keyCode) {
901 int[] map = mKeyRotationMap;
902 if (map != null) {
903 final int N = map.length;
904 for (int i=0; i<N; i+=2) {
905 if (map[i] == keyCode) {
906 return map[i+1];
907 }
908 }
909 }
910 return keyCode;
911 }
912
913 boolean hasEvents() {
914 synchronized (mFirst) {
915 return mFirst.next != mLast;
916 }
917 }
918
919 /*
920 * returns true if we returned an event, and false if we timed out
921 */
922 QueuedEvent getEvent(long timeoutMS) {
923 long begin = SystemClock.uptimeMillis();
924 final long end = begin+timeoutMS;
925 long now = begin;
926 synchronized (mFirst) {
927 while (mFirst.next == mLast && end > now) {
928 try {
929 mWakeLock.release();
930 mFirst.wait(end-now);
931 }
932 catch (InterruptedException e) {
933 }
934 now = SystemClock.uptimeMillis();
935 if (begin > now) {
936 begin = now;
937 }
938 }
939 if (mFirst.next == mLast) {
940 return null;
941 }
942 QueuedEvent p = mFirst.next;
943 mFirst.next = p.next;
944 mFirst.next.prev = mFirst;
945 p.inQueue = false;
946 return p;
947 }
948 }
949
950 void recycleEvent(QueuedEvent ev) {
951 synchronized (mFirst) {
952 //Log.i(TAG, "Recycle event: " + ev);
953 if (ev.event == ev.inputDevice.mAbs.currentMove) {
954 ev.inputDevice.mAbs.currentMove = null;
955 }
956 if (ev.event == ev.inputDevice.mRel.currentMove) {
957 if (false) Log.i(TAG, "Detach rel " + ev.event);
958 ev.inputDevice.mRel.currentMove = null;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700959 ev.inputDevice.mRel.mNextData[MotionEvent.SAMPLE_X] = 0;
960 ev.inputDevice.mRel.mNextData[MotionEvent.SAMPLE_Y] = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961 }
962 recycleLocked(ev);
963 }
964 }
965
966 void filterQueue(FilterCallback cb) {
967 synchronized (mFirst) {
968 QueuedEvent cur = mLast.prev;
969 while (cur.prev != null) {
970 switch (cb.filterEvent(cur)) {
971 case FILTER_REMOVE:
972 cur.prev.next = cur.next;
973 cur.next.prev = cur.prev;
974 break;
975 case FILTER_ABORT:
976 return;
977 }
978 cur = cur.prev;
979 }
980 }
981 }
982
Michael Chan53071d62009-05-13 17:29:48 -0700983 private QueuedEvent obtainLocked(InputDevice device, long whenNano,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984 int flags, int classType, Object event) {
985 QueuedEvent ev;
986 if (mCacheCount == 0) {
987 ev = new QueuedEvent();
988 } else {
989 ev = mCache;
990 ev.inQueue = false;
991 mCache = ev.next;
992 mCacheCount--;
993 }
994 ev.inputDevice = device;
Michael Chan53071d62009-05-13 17:29:48 -0700995 ev.whenNano = whenNano;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 ev.flags = flags;
997 ev.classType = classType;
998 ev.event = event;
999 return ev;
1000 }
1001
1002 private void recycleLocked(QueuedEvent ev) {
1003 if (ev.inQueue) {
1004 throw new RuntimeException("Event already in queue!");
1005 }
1006 if (mCacheCount < 10) {
1007 mCacheCount++;
1008 ev.next = mCache;
1009 mCache = ev;
1010 ev.inQueue = true;
1011 }
1012 }
1013
Michael Chan53071d62009-05-13 17:29:48 -07001014 private void addLocked(InputDevice device, long whenNano, int flags,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 int classType, Object event) {
1016 boolean poke = mFirst.next == mLast;
1017
Michael Chan53071d62009-05-13 17:29:48 -07001018 QueuedEvent ev = obtainLocked(device, whenNano, flags, classType, event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 QueuedEvent p = mLast.prev;
Michael Chan53071d62009-05-13 17:29:48 -07001020 while (p != mFirst && ev.whenNano < p.whenNano) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 p = p.prev;
1022 }
1023
1024 ev.next = p.next;
1025 ev.prev = p;
1026 p.next = ev;
1027 ev.next.prev = ev;
1028 ev.inQueue = true;
1029
1030 if (poke) {
Michael Chan53071d62009-05-13 17:29:48 -07001031 long time;
1032 if (MEASURE_LATENCY) {
1033 time = System.nanoTime();
1034 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 mFirst.notify();
1036 mWakeLock.acquire();
Michael Chan53071d62009-05-13 17:29:48 -07001037 if (MEASURE_LATENCY) {
1038 lt.sample("1 addLocked-queued event ", System.nanoTime() - time);
1039 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 }
1041 }
1042
1043 private InputDevice newInputDevice(int deviceId) {
1044 int classes = getDeviceClasses(deviceId);
1045 String name = getDeviceName(deviceId);
1046 Log.i(TAG, "Device added: id=0x" + Integer.toHexString(deviceId)
1047 + ", name=" + name
1048 + ", classes=" + Integer.toHexString(classes));
1049 InputDevice.AbsoluteInfo absX;
1050 InputDevice.AbsoluteInfo absY;
1051 InputDevice.AbsoluteInfo absPressure;
1052 InputDevice.AbsoluteInfo absSize;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001053 if ((classes&RawInputEvent.CLASS_TOUCHSCREEN_MT) != 0) {
1054 absX = loadAbsoluteInfo(deviceId, RawInputEvent.ABS_MT_POSITION_X, "X");
1055 absY = loadAbsoluteInfo(deviceId, RawInputEvent.ABS_MT_POSITION_Y, "Y");
1056 absPressure = loadAbsoluteInfo(deviceId, RawInputEvent.ABS_MT_TOUCH_MAJOR, "Pressure");
1057 absSize = loadAbsoluteInfo(deviceId, RawInputEvent.ABS_MT_WIDTH_MAJOR, "Size");
1058 } else if ((classes&RawInputEvent.CLASS_TOUCHSCREEN) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 absX = loadAbsoluteInfo(deviceId, RawInputEvent.ABS_X, "X");
1060 absY = loadAbsoluteInfo(deviceId, RawInputEvent.ABS_Y, "Y");
1061 absPressure = loadAbsoluteInfo(deviceId, RawInputEvent.ABS_PRESSURE, "Pressure");
1062 absSize = loadAbsoluteInfo(deviceId, RawInputEvent.ABS_TOOL_WIDTH, "Size");
1063 } else {
1064 absX = null;
1065 absY = null;
1066 absPressure = null;
1067 absSize = null;
1068 }
1069
1070 return new InputDevice(deviceId, classes, name, absX, absY, absPressure, absSize);
1071 }
1072
1073 private InputDevice.AbsoluteInfo loadAbsoluteInfo(int id, int channel,
1074 String name) {
1075 InputDevice.AbsoluteInfo info = new InputDevice.AbsoluteInfo();
1076 if (getAbsoluteInfo(id, channel, info)
1077 && info.minValue != info.maxValue) {
1078 Log.i(TAG, " " + name + ": min=" + info.minValue
1079 + " max=" + info.maxValue
1080 + " flat=" + info.flat
1081 + " fuzz=" + info.fuzz);
1082 info.range = info.maxValue-info.minValue;
1083 return info;
1084 }
1085 Log.i(TAG, " " + name + ": unknown values");
1086 return null;
1087 }
1088 private static native boolean readEvent(RawInputEvent outEvent);
1089}