blob: dc7d22bca31ef0d2deb1022050f8cc9a0895c8d6 [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
Iliyan Malchev75b2aed2009-08-06 14:50:57 -0700197 private void readVirtualKeys(String deviceName) {
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700198 try {
199 FileInputStream fis = new FileInputStream(
Iliyan Malchev75b2aed2009-08-06 14:50:57 -0700200 "/sys/board_properties/virtualkeys." + deviceName);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700201 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 readExcludedDevices();
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 PowerManager pm = (PowerManager)context.getSystemService(
282 Context.POWER_SERVICE);
283 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
284 "KeyInputQueue");
285 mWakeLock.setReferenceCounted(false);
286
287 mFirst = new QueuedEvent();
288 mLast = new QueuedEvent();
289 mFirst.next = mLast;
290 mLast.prev = mFirst;
291
292 mThread.start();
293 }
294
295 public void setDisplay(Display display) {
296 mDisplay = display;
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700297
298 // We assume at this point that the display dimensions reflect the
299 // natural, unrotated display. We will perform hit tests for soft
300 // buttons based on that display.
301 mDisplayWidth = display.getWidth();
302 mDisplayHeight = display.getHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 }
304
305 public void getInputConfiguration(Configuration config) {
306 synchronized (mFirst) {
307 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
308 config.keyboard = Configuration.KEYBOARD_NOKEYS;
309 config.navigation = Configuration.NAVIGATION_NONAV;
310
311 final int N = mDevices.size();
312 for (int i=0; i<N; i++) {
313 InputDevice d = mDevices.valueAt(i);
314 if (d != null) {
315 if ((d.classes&RawInputEvent.CLASS_TOUCHSCREEN) != 0) {
316 config.touchscreen
317 = Configuration.TOUCHSCREEN_FINGER;
318 //Log.i("foo", "***** HAVE TOUCHSCREEN!");
319 }
320 if ((d.classes&RawInputEvent.CLASS_ALPHAKEY) != 0) {
321 config.keyboard
322 = Configuration.KEYBOARD_QWERTY;
323 //Log.i("foo", "***** HAVE QWERTY!");
324 }
325 if ((d.classes&RawInputEvent.CLASS_TRACKBALL) != 0) {
326 config.navigation
327 = Configuration.NAVIGATION_TRACKBALL;
328 //Log.i("foo", "***** HAVE TRACKBALL!");
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700329 } else if ((d.classes&RawInputEvent.CLASS_DPAD) != 0) {
330 config.navigation
331 = Configuration.NAVIGATION_DPAD;
332 //Log.i("foo", "***** HAVE DPAD!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 }
334 }
335 }
336 }
337 }
338
339 public static native String getDeviceName(int deviceId);
340 public static native int getDeviceClasses(int deviceId);
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400341 public static native void addExcludedDevice(String deviceName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 public static native boolean getAbsoluteInfo(int deviceId, int axis,
343 InputDevice.AbsoluteInfo outInfo);
344 public static native int getSwitchState(int sw);
345 public static native int getSwitchState(int deviceId, int sw);
346 public static native int getScancodeState(int sw);
347 public static native int getScancodeState(int deviceId, int sw);
348 public static native int getKeycodeState(int sw);
349 public static native int getKeycodeState(int deviceId, int sw);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700350 public static native int scancodeToKeycode(int deviceId, int scancode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 public static native boolean hasKeys(int[] keycodes, boolean[] keyExists);
352
353 public static KeyEvent newKeyEvent(InputDevice device, long downTime,
354 long eventTime, boolean down, int keycode, int repeatCount,
355 int scancode, int flags) {
356 return new KeyEvent(
357 downTime, eventTime,
358 down ? KeyEvent.ACTION_DOWN : KeyEvent.ACTION_UP,
359 keycode, repeatCount,
360 device != null ? device.mMetaKeysState : 0,
361 device != null ? device.id : -1, scancode,
The Android Open Source Project10592532009-03-18 17:39:46 -0700362 flags | KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 }
364
365 Thread mThread = new Thread("InputDeviceReader") {
366 public void run() {
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400367 Log.d(TAG, "InputDeviceReader.run()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 android.os.Process.setThreadPriority(
369 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
370
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700371 RawInputEvent ev = new RawInputEvent();
372 while (true) {
373 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 InputDevice di;
375
376 // block, doesn't release the monitor
377 readEvent(ev);
378
379 boolean send = false;
380 boolean configChanged = false;
381
382 if (false) {
383 Log.i(TAG, "Input event: dev=0x"
384 + Integer.toHexString(ev.deviceId)
385 + " type=0x" + Integer.toHexString(ev.type)
386 + " scancode=" + ev.scancode
387 + " keycode=" + ev.keycode
388 + " value=" + ev.value);
389 }
390
391 if (ev.type == RawInputEvent.EV_DEVICE_ADDED) {
392 synchronized (mFirst) {
393 di = newInputDevice(ev.deviceId);
394 mDevices.put(ev.deviceId, di);
Iliyan Malchev75b2aed2009-08-06 14:50:57 -0700395 if ((di.classes & RawInputEvent.CLASS_TOUCHSCREEN) != 0) {
396 readVirtualKeys(di.name);
397 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 configChanged = true;
399 }
400 } else if (ev.type == RawInputEvent.EV_DEVICE_REMOVED) {
401 synchronized (mFirst) {
402 Log.i(TAG, "Device removed: id=0x"
403 + Integer.toHexString(ev.deviceId));
404 di = mDevices.get(ev.deviceId);
405 if (di != null) {
406 mDevices.delete(ev.deviceId);
407 configChanged = true;
408 } else {
409 Log.w(TAG, "Bad device id: " + ev.deviceId);
410 }
411 }
412 } else {
413 di = getInputDevice(ev.deviceId);
414
415 // first crack at it
416 send = preprocessEvent(di, ev);
417
418 if (ev.type == RawInputEvent.EV_KEY) {
419 di.mMetaKeysState = makeMetaState(ev.keycode,
420 ev.value != 0, di.mMetaKeysState);
421 mHaveGlobalMetaState = false;
422 }
423 }
424
425 if (di == null) {
426 continue;
427 }
428
429 if (configChanged) {
430 synchronized (mFirst) {
Michael Chan53071d62009-05-13 17:29:48 -0700431 addLocked(di, System.nanoTime(), 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 RawInputEvent.CLASS_CONFIGURATION_CHANGED,
433 null);
434 }
435 }
436
437 if (!send) {
438 continue;
439 }
440
441 synchronized (mFirst) {
442 // NOTE: The event timebase absolutely must be the same
443 // timebase as SystemClock.uptimeMillis().
444 //curTime = gotOne ? ev.when : SystemClock.uptimeMillis();
445 final long curTime = SystemClock.uptimeMillis();
Michael Chan53071d62009-05-13 17:29:48 -0700446 final long curTimeNano = System.nanoTime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 //Log.i(TAG, "curTime=" + curTime + ", systemClock=" + SystemClock.uptimeMillis());
448
449 final int classes = di.classes;
450 final int type = ev.type;
451 final int scancode = ev.scancode;
452 send = false;
453
454 // Is it a key event?
455 if (type == RawInputEvent.EV_KEY &&
456 (classes&RawInputEvent.CLASS_KEYBOARD) != 0 &&
457 (scancode < RawInputEvent.BTN_FIRST ||
458 scancode > RawInputEvent.BTN_LAST)) {
459 boolean down;
460 if (ev.value != 0) {
461 down = true;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700462 di.mKeyDownTime = curTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 } else {
464 down = false;
465 }
466 int keycode = rotateKeyCodeLocked(ev.keycode);
Michael Chan53071d62009-05-13 17:29:48 -0700467 addLocked(di, curTimeNano, ev.flags,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 RawInputEvent.CLASS_KEYBOARD,
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700469 newKeyEvent(di, di.mKeyDownTime, curTime, down,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 keycode, 0, scancode,
471 ((ev.flags & WindowManagerPolicy.FLAG_WOKE_HERE) != 0)
472 ? KeyEvent.FLAG_WOKE_HERE : 0));
473 } else if (ev.type == RawInputEvent.EV_KEY) {
474 if (ev.scancode == RawInputEvent.BTN_TOUCH &&
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700475 (classes&(RawInputEvent.CLASS_TOUCHSCREEN
476 |RawInputEvent.CLASS_TOUCHSCREEN_MT))
477 == RawInputEvent.CLASS_TOUCHSCREEN) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 di.mAbs.changed = true;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700479 di.mAbs.mDown[0] = ev.value != 0;
480 } else if (ev.scancode == RawInputEvent.BTN_2 &&
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700481 (classes&(RawInputEvent.CLASS_TOUCHSCREEN
482 |RawInputEvent.CLASS_TOUCHSCREEN_MT))
483 == RawInputEvent.CLASS_TOUCHSCREEN) {
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700484 di.mAbs.changed = true;
485 di.mAbs.mDown[1] = ev.value != 0;
486 } else if (ev.scancode == RawInputEvent.BTN_MOUSE &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 (classes&RawInputEvent.CLASS_TRACKBALL) != 0) {
488 di.mRel.changed = true;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700489 di.mRel.mNextNumPointers = ev.value != 0 ? 1 : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 send = true;
491 }
492
493 } else if (ev.type == RawInputEvent.EV_ABS &&
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700494 (classes&RawInputEvent.CLASS_TOUCHSCREEN_MT) != 0) {
495 if (ev.scancode == RawInputEvent.ABS_MT_TOUCH_MAJOR) {
496 di.mAbs.changed = true;
497 di.mAbs.mNextData[di.mAbs.mAddingPointerOffset
498 + MotionEvent.SAMPLE_PRESSURE] = ev.value;
499 } else if (ev.scancode == RawInputEvent.ABS_MT_POSITION_X) {
500 di.mAbs.changed = true;
501 di.mAbs.mNextData[di.mAbs.mAddingPointerOffset
502 + MotionEvent.SAMPLE_X] = ev.value;
503 if (DEBUG_POINTERS) Log.v(TAG, "MT @"
504 + di.mAbs.mAddingPointerOffset
505 + " X:" + ev.value);
506 } else if (ev.scancode == RawInputEvent.ABS_MT_POSITION_Y) {
507 di.mAbs.changed = true;
508 di.mAbs.mNextData[di.mAbs.mAddingPointerOffset
509 + MotionEvent.SAMPLE_Y] = ev.value;
510 if (DEBUG_POINTERS) Log.v(TAG, "MT @"
511 + di.mAbs.mAddingPointerOffset
512 + " Y:" + ev.value);
513 } else if (ev.scancode == RawInputEvent.ABS_MT_WIDTH_MAJOR) {
514 di.mAbs.changed = true;
515 di.mAbs.mNextData[di.mAbs.mAddingPointerOffset
516 + MotionEvent.SAMPLE_SIZE] = ev.value;
517 }
518
519 } else if (ev.type == RawInputEvent.EV_ABS &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 (classes&RawInputEvent.CLASS_TOUCHSCREEN) != 0) {
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700521 // Finger 1
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 if (ev.scancode == RawInputEvent.ABS_X) {
523 di.mAbs.changed = true;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700524 di.mAbs.mNextData[MotionEvent.SAMPLE_X] = ev.value;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 } else if (ev.scancode == RawInputEvent.ABS_Y) {
526 di.mAbs.changed = true;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700527 di.mAbs.mNextData[MotionEvent.SAMPLE_Y] = ev.value;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 } else if (ev.scancode == RawInputEvent.ABS_PRESSURE) {
529 di.mAbs.changed = true;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700530 di.mAbs.mNextData[MotionEvent.SAMPLE_PRESSURE] = ev.value;
531 di.mAbs.mNextData[MotionEvent.NUM_SAMPLE_DATA
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700532 + MotionEvent.SAMPLE_PRESSURE] = ev.value;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 } else if (ev.scancode == RawInputEvent.ABS_TOOL_WIDTH) {
534 di.mAbs.changed = true;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700535 di.mAbs.mNextData[MotionEvent.SAMPLE_SIZE] = ev.value;
536 di.mAbs.mNextData[MotionEvent.NUM_SAMPLE_DATA
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700537 + MotionEvent.SAMPLE_SIZE] = ev.value;
538
539 // Finger 2
540 } else if (ev.scancode == RawInputEvent.ABS_HAT0X) {
541 di.mAbs.changed = true;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700542 di.mAbs.mNextData[(di.mAbs.mDown[0] ?
543 MotionEvent.NUM_SAMPLE_DATA : 0)
544 + MotionEvent.SAMPLE_X] = ev.value;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700545 } else if (ev.scancode == RawInputEvent.ABS_HAT0Y) {
546 di.mAbs.changed = true;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700547 di.mAbs.mNextData[(di.mAbs.mDown[0] ?
548 MotionEvent.NUM_SAMPLE_DATA : 0)
549 + MotionEvent.SAMPLE_Y] = ev.value;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 }
551
552 } else if (ev.type == RawInputEvent.EV_REL &&
553 (classes&RawInputEvent.CLASS_TRACKBALL) != 0) {
554 // Add this relative movement into our totals.
555 if (ev.scancode == RawInputEvent.REL_X) {
556 di.mRel.changed = true;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700557 di.mRel.mNextData[MotionEvent.SAMPLE_X] += ev.value;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 } else if (ev.scancode == RawInputEvent.REL_Y) {
559 di.mRel.changed = true;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700560 di.mRel.mNextData[MotionEvent.SAMPLE_Y] += ev.value;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 }
562 }
563
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700564 if (ev.type == RawInputEvent.EV_SYN
565 && ev.scancode == RawInputEvent.SYN_MT_REPORT
566 && di.mAbs != null) {
567 di.mAbs.changed = true;
568 if (di.mAbs.mNextData[MotionEvent.SAMPLE_PRESSURE] > 0) {
569 // If the value is <= 0, the pointer is not
570 // down, so keep it in the count.
571
572 if (di.mAbs.mNextData[di.mAbs.mAddingPointerOffset
573 + MotionEvent.SAMPLE_PRESSURE] != 0) {
574 final int num = di.mAbs.mNextNumPointers+1;
575 di.mAbs.mNextNumPointers = num;
576 if (DEBUG_POINTERS) Log.v(TAG,
577 "MT_REPORT: now have " + num + " pointers");
578 final int newOffset = (num <= InputDevice.MAX_POINTERS)
579 ? (num * MotionEvent.NUM_SAMPLE_DATA)
580 : (InputDevice.MAX_POINTERS *
581 MotionEvent.NUM_SAMPLE_DATA);
582 di.mAbs.mAddingPointerOffset = newOffset;
583 di.mAbs.mNextData[newOffset
584 + MotionEvent.SAMPLE_PRESSURE] = 0;
585 } else {
586 if (DEBUG_POINTERS) Log.v(TAG, "MT_REPORT: no pointer");
587 }
588 }
589 } else if (send || (ev.type == RawInputEvent.EV_SYN
590 && ev.scancode == RawInputEvent.SYN_REPORT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 if (mDisplay != null) {
592 if (!mHaveGlobalMetaState) {
593 computeGlobalMetaStateLocked();
594 }
595
596 MotionEvent me;
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700597
598 InputDevice.MotionState ms = di.mAbs;
599 if (ms.changed) {
600 ms.changed = false;
601
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700602 if ((classes&(RawInputEvent.CLASS_TOUCHSCREEN
603 |RawInputEvent.CLASS_TOUCHSCREEN_MT))
604 == RawInputEvent.CLASS_TOUCHSCREEN) {
605 ms.mNextNumPointers = 0;
606 if (ms.mDown[0]) ms.mNextNumPointers++;
607 if (ms.mDown[1]) ms.mNextNumPointers++;
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700608 }
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700609
610 boolean doMotion = !monitorVirtualKey(di,
611 ev, curTime, curTimeNano);
612
613 if (doMotion && ms.mNextNumPointers > 0
614 && ms.mLastNumPointers == 0) {
615 doMotion = !generateVirtualKeyDown(di,
616 ev, curTime, curTimeNano);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 }
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700618
619 if (doMotion) {
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700620 // XXX Need to be able to generate
621 // multiple events here, for example
622 // if two fingers change up/down state
623 // at the same time.
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700624 do {
625 me = ms.generateAbsMotion(di, curTime,
626 curTimeNano, mDisplay,
627 mOrientation, mGlobalMetaState);
628 if (false) Log.v(TAG, "Absolute: x="
629 + di.mAbs.mNextData[MotionEvent.SAMPLE_X]
630 + " y="
631 + di.mAbs.mNextData[MotionEvent.SAMPLE_Y]
632 + " ev=" + me);
633 if (me != null) {
634 if (WindowManagerPolicy.WATCH_POINTER) {
635 Log.i(TAG, "Enqueueing: " + me);
636 }
637 addLocked(di, curTimeNano, ev.flags,
638 RawInputEvent.CLASS_TOUCHSCREEN, me);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700639 }
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700640 } while (ms.hasMore());
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700641 }
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700642
643 ms.finish();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 }
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700645
646 ms = di.mRel;
647 if (ms.changed) {
648 ms.changed = false;
649
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700650 me = ms.generateRelMotion(di, curTime,
651 curTimeNano,
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700652 mOrientation, mGlobalMetaState);
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700653 if (false) Log.v(TAG, "Relative: x="
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700654 + di.mRel.mNextData[MotionEvent.SAMPLE_X]
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700655 + " y="
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700656 + di.mRel.mNextData[MotionEvent.SAMPLE_Y]
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700657 + " ev=" + me);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700658 if (me != null) {
659 addLocked(di, curTimeNano, ev.flags,
660 RawInputEvent.CLASS_TRACKBALL, me);
661 }
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700662
663 ms.finish();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 }
665 }
666 }
667 }
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700668
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700669 } catch (RuntimeException exc) {
670 Log.e(TAG, "InputReaderThread uncaught exception", exc);
671 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 }
673 }
674 };
675
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700676 private boolean isInsideDisplay(InputDevice dev) {
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700677 final InputDevice.AbsoluteInfo absx = dev.absX;
678 final InputDevice.AbsoluteInfo absy = dev.absY;
679 final InputDevice.MotionState absm = dev.mAbs;
680 if (absx == null || absy == null || absm == null) {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700681 return true;
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700682 }
683
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700684 if (absm.mNextData[MotionEvent.SAMPLE_X] >= absx.minValue
685 && absm.mNextData[MotionEvent.SAMPLE_X] <= absx.maxValue
686 && absm.mNextData[MotionEvent.SAMPLE_Y] >= absy.minValue
687 && absm.mNextData[MotionEvent.SAMPLE_Y] <= absy.maxValue) {
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700688 if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "Input ("
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700689 + absm.mNextData[MotionEvent.SAMPLE_X]
690 + "," + absm.mNextData[MotionEvent.SAMPLE_Y]
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700691 + ") inside of display");
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700692 return true;
693 }
694
695 return false;
696 }
697
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700698 private VirtualKey findVirtualKey(InputDevice dev) {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700699 final int N = mVirtualKeys.size();
700 if (N <= 0) {
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700701 return null;
702 }
703
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700704 final InputDevice.MotionState absm = dev.mAbs;
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700705 for (int i=0; i<N; i++) {
706 VirtualKey sb = mVirtualKeys.get(i);
707 sb.computeHitRect(dev, mDisplayWidth, mDisplayHeight);
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700708 if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "Hit test ("
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700709 + absm.mNextData[MotionEvent.SAMPLE_X] + ","
710 + absm.mNextData[MotionEvent.SAMPLE_Y] + ") in code "
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700711 + sb.scancode + " - (" + sb.hitLeft
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700712 + "," + sb.hitTop + ")-(" + sb.hitRight + ","
713 + sb.hitBottom + ")");
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700714 if (sb.checkHit(absm.mNextData[MotionEvent.SAMPLE_X],
715 absm.mNextData[MotionEvent.SAMPLE_Y])) {
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700716 if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "Hit!");
717 return sb;
718 }
719 }
720
721 return null;
722 }
723
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700724 private boolean generateVirtualKeyDown(InputDevice di, RawInputEvent ev,
725 long curTime, long curTimeNano) {
726 if (isInsideDisplay(di)) {
727 // Didn't consume event.
728 return false;
729 }
730
731
732 VirtualKey vk = findVirtualKey(di);
733 if (vk != null) {
734 final InputDevice.MotionState ms = di.mAbs;
735 mPressedVirtualKey = vk;
736 vk.lastKeycode = scancodeToKeycode(di.id, vk.scancode);
737 ms.mLastNumPointers = ms.mNextNumPointers;
738 di.mKeyDownTime = curTime;
739 if (DEBUG_VIRTUAL_KEYS) Log.v(TAG,
740 "Generate key down for: " + vk.scancode
741 + " (keycode=" + vk.lastKeycode + ")");
742 KeyEvent event = newKeyEvent(di, di.mKeyDownTime, curTime, true,
743 vk.lastKeycode, 0, vk.scancode,
744 KeyEvent.FLAG_VIRTUAL_HARD_KEY);
745 mHapticFeedbackCallback.virtualKeyFeedback(event);
746 addLocked(di, curTimeNano, ev.flags, RawInputEvent.CLASS_KEYBOARD,
747 event);
748 }
749
750 // We always consume the event, even if we didn't
751 // generate a key event. There are two reasons for
752 // this: to avoid spurious touches when holding
753 // the edges of the device near the touchscreen,
754 // and to avoid reporting events if there are virtual
755 // keys on the touchscreen outside of the display
756 // area.
757 // Note that for all of this we are only looking at the
758 // first pointer, since what we are handling here is the
759 // first pointer going down, and this is the coordinate
760 // that will be used to dispatch the event.
761 if (false) {
762 final InputDevice.AbsoluteInfo absx = di.absX;
763 final InputDevice.AbsoluteInfo absy = di.absY;
764 final InputDevice.MotionState absm = di.mAbs;
765 Log.v(TAG, "Rejecting ("
766 + absm.mNextData[MotionEvent.SAMPLE_X] + ","
767 + absm.mNextData[MotionEvent.SAMPLE_Y] + "): outside of ("
768 + absx.minValue + "," + absy.minValue
769 + ")-(" + absx.maxValue + ","
770 + absx.maxValue + ")");
771 }
772 return true;
773 }
774
775 private boolean monitorVirtualKey(InputDevice di, RawInputEvent ev,
776 long curTime, long curTimeNano) {
777 VirtualKey vk = mPressedVirtualKey;
778 if (vk == null) {
779 return false;
780 }
781
782 final InputDevice.MotionState ms = di.mAbs;
783 if (ms.mNextNumPointers <= 0) {
784 mPressedVirtualKey = null;
785 ms.mLastNumPointers = 0;
786 if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "Generate key up for: " + vk.scancode);
787 KeyEvent event = newKeyEvent(di, di.mKeyDownTime, curTime, false,
788 vk.lastKeycode, 0, vk.scancode,
789 KeyEvent.FLAG_VIRTUAL_HARD_KEY);
790 mHapticFeedbackCallback.virtualKeyFeedback(event);
791 addLocked(di, curTimeNano, ev.flags, RawInputEvent.CLASS_KEYBOARD,
792 event);
793 return true;
794
795 } else if (isInsideDisplay(di)) {
796 // Whoops the pointer has moved into
797 // the display area! Cancel the
798 // virtual key and start a pointer
799 // motion.
800 mPressedVirtualKey = null;
801 if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "Cancel key up for: " + vk.scancode);
802 KeyEvent event = newKeyEvent(di, di.mKeyDownTime, curTime, false,
803 vk.lastKeycode, 0, vk.scancode,
804 KeyEvent.FLAG_CANCELED | KeyEvent.FLAG_VIRTUAL_HARD_KEY);
805 mHapticFeedbackCallback.virtualKeyFeedback(event);
806 addLocked(di, curTimeNano, ev.flags, RawInputEvent.CLASS_KEYBOARD,
807 event);
808 ms.mLastNumPointers = 0;
809 return false;
810 }
811
812 return true;
813 }
814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800815 /**
816 * Returns a new meta state for the given keys and old state.
817 */
818 private static final int makeMetaState(int keycode, boolean down, int old) {
819 int mask;
820 switch (keycode) {
821 case KeyEvent.KEYCODE_ALT_LEFT:
822 mask = KeyEvent.META_ALT_LEFT_ON;
823 break;
824 case KeyEvent.KEYCODE_ALT_RIGHT:
825 mask = KeyEvent.META_ALT_RIGHT_ON;
826 break;
827 case KeyEvent.KEYCODE_SHIFT_LEFT:
828 mask = KeyEvent.META_SHIFT_LEFT_ON;
829 break;
830 case KeyEvent.KEYCODE_SHIFT_RIGHT:
831 mask = KeyEvent.META_SHIFT_RIGHT_ON;
832 break;
833 case KeyEvent.KEYCODE_SYM:
834 mask = KeyEvent.META_SYM_ON;
835 break;
836 default:
837 return old;
838 }
839 int result = ~(KeyEvent.META_ALT_ON | KeyEvent.META_SHIFT_ON)
840 & (down ? (old | mask) : (old & ~mask));
841 if (0 != (result & (KeyEvent.META_ALT_LEFT_ON | KeyEvent.META_ALT_RIGHT_ON))) {
842 result |= KeyEvent.META_ALT_ON;
843 }
844 if (0 != (result & (KeyEvent.META_SHIFT_LEFT_ON | KeyEvent.META_SHIFT_RIGHT_ON))) {
845 result |= KeyEvent.META_SHIFT_ON;
846 }
847 return result;
848 }
849
850 private void computeGlobalMetaStateLocked() {
851 int i = mDevices.size();
852 mGlobalMetaState = 0;
853 while ((--i) >= 0) {
854 mGlobalMetaState |= mDevices.valueAt(i).mMetaKeysState;
855 }
856 mHaveGlobalMetaState = true;
857 }
858
859 /*
860 * Return true if you want the event to get passed on to the
861 * rest of the system, and false if you've handled it and want
862 * it dropped.
863 */
864 abstract boolean preprocessEvent(InputDevice device, RawInputEvent event);
865
866 InputDevice getInputDevice(int deviceId) {
867 synchronized (mFirst) {
868 return getInputDeviceLocked(deviceId);
869 }
870 }
871
872 private InputDevice getInputDeviceLocked(int deviceId) {
873 return mDevices.get(deviceId);
874 }
875
876 public void setOrientation(int orientation) {
877 synchronized(mFirst) {
878 mOrientation = orientation;
879 switch (orientation) {
880 case Surface.ROTATION_90:
881 mKeyRotationMap = KEY_90_MAP;
882 break;
883 case Surface.ROTATION_180:
884 mKeyRotationMap = KEY_180_MAP;
885 break;
886 case Surface.ROTATION_270:
887 mKeyRotationMap = KEY_270_MAP;
888 break;
889 default:
890 mKeyRotationMap = null;
891 break;
892 }
893 }
894 }
895
896 public int rotateKeyCode(int keyCode) {
897 synchronized(mFirst) {
898 return rotateKeyCodeLocked(keyCode);
899 }
900 }
901
902 private int rotateKeyCodeLocked(int keyCode) {
903 int[] map = mKeyRotationMap;
904 if (map != null) {
905 final int N = map.length;
906 for (int i=0; i<N; i+=2) {
907 if (map[i] == keyCode) {
908 return map[i+1];
909 }
910 }
911 }
912 return keyCode;
913 }
914
915 boolean hasEvents() {
916 synchronized (mFirst) {
917 return mFirst.next != mLast;
918 }
919 }
920
921 /*
922 * returns true if we returned an event, and false if we timed out
923 */
924 QueuedEvent getEvent(long timeoutMS) {
925 long begin = SystemClock.uptimeMillis();
926 final long end = begin+timeoutMS;
927 long now = begin;
928 synchronized (mFirst) {
929 while (mFirst.next == mLast && end > now) {
930 try {
931 mWakeLock.release();
932 mFirst.wait(end-now);
933 }
934 catch (InterruptedException e) {
935 }
936 now = SystemClock.uptimeMillis();
937 if (begin > now) {
938 begin = now;
939 }
940 }
941 if (mFirst.next == mLast) {
942 return null;
943 }
944 QueuedEvent p = mFirst.next;
945 mFirst.next = p.next;
946 mFirst.next.prev = mFirst;
947 p.inQueue = false;
948 return p;
949 }
950 }
951
952 void recycleEvent(QueuedEvent ev) {
953 synchronized (mFirst) {
954 //Log.i(TAG, "Recycle event: " + ev);
955 if (ev.event == ev.inputDevice.mAbs.currentMove) {
956 ev.inputDevice.mAbs.currentMove = null;
957 }
958 if (ev.event == ev.inputDevice.mRel.currentMove) {
959 if (false) Log.i(TAG, "Detach rel " + ev.event);
960 ev.inputDevice.mRel.currentMove = null;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700961 ev.inputDevice.mRel.mNextData[MotionEvent.SAMPLE_X] = 0;
962 ev.inputDevice.mRel.mNextData[MotionEvent.SAMPLE_Y] = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800963 }
964 recycleLocked(ev);
965 }
966 }
967
968 void filterQueue(FilterCallback cb) {
969 synchronized (mFirst) {
970 QueuedEvent cur = mLast.prev;
971 while (cur.prev != null) {
972 switch (cb.filterEvent(cur)) {
973 case FILTER_REMOVE:
974 cur.prev.next = cur.next;
975 cur.next.prev = cur.prev;
976 break;
977 case FILTER_ABORT:
978 return;
979 }
980 cur = cur.prev;
981 }
982 }
983 }
984
Michael Chan53071d62009-05-13 17:29:48 -0700985 private QueuedEvent obtainLocked(InputDevice device, long whenNano,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986 int flags, int classType, Object event) {
987 QueuedEvent ev;
988 if (mCacheCount == 0) {
989 ev = new QueuedEvent();
990 } else {
991 ev = mCache;
992 ev.inQueue = false;
993 mCache = ev.next;
994 mCacheCount--;
995 }
996 ev.inputDevice = device;
Michael Chan53071d62009-05-13 17:29:48 -0700997 ev.whenNano = whenNano;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998 ev.flags = flags;
999 ev.classType = classType;
1000 ev.event = event;
1001 return ev;
1002 }
1003
1004 private void recycleLocked(QueuedEvent ev) {
1005 if (ev.inQueue) {
1006 throw new RuntimeException("Event already in queue!");
1007 }
1008 if (mCacheCount < 10) {
1009 mCacheCount++;
1010 ev.next = mCache;
1011 mCache = ev;
1012 ev.inQueue = true;
1013 }
1014 }
1015
Michael Chan53071d62009-05-13 17:29:48 -07001016 private void addLocked(InputDevice device, long whenNano, int flags,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 int classType, Object event) {
1018 boolean poke = mFirst.next == mLast;
1019
Michael Chan53071d62009-05-13 17:29:48 -07001020 QueuedEvent ev = obtainLocked(device, whenNano, flags, classType, event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 QueuedEvent p = mLast.prev;
Michael Chan53071d62009-05-13 17:29:48 -07001022 while (p != mFirst && ev.whenNano < p.whenNano) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 p = p.prev;
1024 }
1025
1026 ev.next = p.next;
1027 ev.prev = p;
1028 p.next = ev;
1029 ev.next.prev = ev;
1030 ev.inQueue = true;
1031
1032 if (poke) {
Michael Chan53071d62009-05-13 17:29:48 -07001033 long time;
1034 if (MEASURE_LATENCY) {
1035 time = System.nanoTime();
1036 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 mFirst.notify();
1038 mWakeLock.acquire();
Michael Chan53071d62009-05-13 17:29:48 -07001039 if (MEASURE_LATENCY) {
1040 lt.sample("1 addLocked-queued event ", System.nanoTime() - time);
1041 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 }
1043 }
1044
1045 private InputDevice newInputDevice(int deviceId) {
1046 int classes = getDeviceClasses(deviceId);
1047 String name = getDeviceName(deviceId);
1048 Log.i(TAG, "Device added: id=0x" + Integer.toHexString(deviceId)
1049 + ", name=" + name
1050 + ", classes=" + Integer.toHexString(classes));
1051 InputDevice.AbsoluteInfo absX;
1052 InputDevice.AbsoluteInfo absY;
1053 InputDevice.AbsoluteInfo absPressure;
1054 InputDevice.AbsoluteInfo absSize;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001055 if ((classes&RawInputEvent.CLASS_TOUCHSCREEN_MT) != 0) {
1056 absX = loadAbsoluteInfo(deviceId, RawInputEvent.ABS_MT_POSITION_X, "X");
1057 absY = loadAbsoluteInfo(deviceId, RawInputEvent.ABS_MT_POSITION_Y, "Y");
1058 absPressure = loadAbsoluteInfo(deviceId, RawInputEvent.ABS_MT_TOUCH_MAJOR, "Pressure");
1059 absSize = loadAbsoluteInfo(deviceId, RawInputEvent.ABS_MT_WIDTH_MAJOR, "Size");
1060 } else if ((classes&RawInputEvent.CLASS_TOUCHSCREEN) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001061 absX = loadAbsoluteInfo(deviceId, RawInputEvent.ABS_X, "X");
1062 absY = loadAbsoluteInfo(deviceId, RawInputEvent.ABS_Y, "Y");
1063 absPressure = loadAbsoluteInfo(deviceId, RawInputEvent.ABS_PRESSURE, "Pressure");
1064 absSize = loadAbsoluteInfo(deviceId, RawInputEvent.ABS_TOOL_WIDTH, "Size");
1065 } else {
1066 absX = null;
1067 absY = null;
1068 absPressure = null;
1069 absSize = null;
1070 }
1071
1072 return new InputDevice(deviceId, classes, name, absX, absY, absPressure, absSize);
1073 }
1074
1075 private InputDevice.AbsoluteInfo loadAbsoluteInfo(int id, int channel,
1076 String name) {
1077 InputDevice.AbsoluteInfo info = new InputDevice.AbsoluteInfo();
1078 if (getAbsoluteInfo(id, channel, info)
1079 && info.minValue != info.maxValue) {
1080 Log.i(TAG, " " + name + ": min=" + info.minValue
1081 + " max=" + info.maxValue
1082 + " flat=" + info.flat
1083 + " fuzz=" + info.fuzz);
1084 info.range = info.maxValue-info.minValue;
1085 return info;
1086 }
1087 Log.i(TAG, " " + name + ": unknown values");
1088 return null;
1089 }
1090 private static native boolean readEvent(RawInputEvent outEvent);
1091}