The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame^] | 1 | /* |
| 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 | |
| 17 | package com.android.server; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.content.res.Configuration; |
| 21 | import android.os.SystemClock; |
| 22 | import android.os.PowerManager; |
| 23 | import android.util.Log; |
| 24 | import android.util.SparseArray; |
| 25 | import android.view.Display; |
| 26 | import android.view.KeyEvent; |
| 27 | import android.view.MotionEvent; |
| 28 | import android.view.RawInputEvent; |
| 29 | import android.view.Surface; |
| 30 | import android.view.WindowManagerPolicy; |
| 31 | |
| 32 | public abstract class KeyInputQueue { |
| 33 | static final String TAG = "KeyInputQueue"; |
| 34 | |
| 35 | SparseArray<InputDevice> mDevices = new SparseArray<InputDevice>(); |
| 36 | |
| 37 | int mGlobalMetaState = 0; |
| 38 | boolean mHaveGlobalMetaState = false; |
| 39 | |
| 40 | final QueuedEvent mFirst; |
| 41 | final QueuedEvent mLast; |
| 42 | QueuedEvent mCache; |
| 43 | int mCacheCount; |
| 44 | |
| 45 | Display mDisplay = null; |
| 46 | |
| 47 | int mOrientation = Surface.ROTATION_0; |
| 48 | int[] mKeyRotationMap = null; |
| 49 | |
| 50 | PowerManager.WakeLock mWakeLock; |
| 51 | |
| 52 | static final int[] KEY_90_MAP = new int[] { |
| 53 | KeyEvent.KEYCODE_DPAD_DOWN, KeyEvent.KEYCODE_DPAD_RIGHT, |
| 54 | KeyEvent.KEYCODE_DPAD_RIGHT, KeyEvent.KEYCODE_DPAD_UP, |
| 55 | KeyEvent.KEYCODE_DPAD_UP, KeyEvent.KEYCODE_DPAD_LEFT, |
| 56 | KeyEvent.KEYCODE_DPAD_LEFT, KeyEvent.KEYCODE_DPAD_DOWN, |
| 57 | }; |
| 58 | |
| 59 | static final int[] KEY_180_MAP = new int[] { |
| 60 | KeyEvent.KEYCODE_DPAD_DOWN, KeyEvent.KEYCODE_DPAD_UP, |
| 61 | KeyEvent.KEYCODE_DPAD_RIGHT, KeyEvent.KEYCODE_DPAD_LEFT, |
| 62 | KeyEvent.KEYCODE_DPAD_UP, KeyEvent.KEYCODE_DPAD_DOWN, |
| 63 | KeyEvent.KEYCODE_DPAD_LEFT, KeyEvent.KEYCODE_DPAD_RIGHT, |
| 64 | }; |
| 65 | |
| 66 | static final int[] KEY_270_MAP = new int[] { |
| 67 | KeyEvent.KEYCODE_DPAD_DOWN, KeyEvent.KEYCODE_DPAD_LEFT, |
| 68 | KeyEvent.KEYCODE_DPAD_LEFT, KeyEvent.KEYCODE_DPAD_UP, |
| 69 | KeyEvent.KEYCODE_DPAD_UP, KeyEvent.KEYCODE_DPAD_RIGHT, |
| 70 | KeyEvent.KEYCODE_DPAD_RIGHT, KeyEvent.KEYCODE_DPAD_DOWN, |
| 71 | }; |
| 72 | |
| 73 | public static final int FILTER_REMOVE = 0; |
| 74 | public static final int FILTER_KEEP = 1; |
| 75 | public static final int FILTER_ABORT = -1; |
| 76 | |
| 77 | public interface FilterCallback { |
| 78 | int filterEvent(QueuedEvent ev); |
| 79 | } |
| 80 | |
| 81 | static class QueuedEvent { |
| 82 | InputDevice inputDevice; |
| 83 | long when; |
| 84 | int flags; // From the raw event |
| 85 | int classType; // One of the class constants in InputEvent |
| 86 | Object event; |
| 87 | boolean inQueue; |
| 88 | |
| 89 | void copyFrom(QueuedEvent that) { |
| 90 | this.inputDevice = that.inputDevice; |
| 91 | this.when = that.when; |
| 92 | this.flags = that.flags; |
| 93 | this.classType = that.classType; |
| 94 | this.event = that.event; |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public String toString() { |
| 99 | return "QueuedEvent{" |
| 100 | + Integer.toHexString(System.identityHashCode(this)) |
| 101 | + " " + event + "}"; |
| 102 | } |
| 103 | |
| 104 | // not copied |
| 105 | QueuedEvent prev; |
| 106 | QueuedEvent next; |
| 107 | } |
| 108 | |
| 109 | KeyInputQueue(Context context) { |
| 110 | PowerManager pm = (PowerManager)context.getSystemService( |
| 111 | Context.POWER_SERVICE); |
| 112 | mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, |
| 113 | "KeyInputQueue"); |
| 114 | mWakeLock.setReferenceCounted(false); |
| 115 | |
| 116 | mFirst = new QueuedEvent(); |
| 117 | mLast = new QueuedEvent(); |
| 118 | mFirst.next = mLast; |
| 119 | mLast.prev = mFirst; |
| 120 | |
| 121 | mThread.start(); |
| 122 | } |
| 123 | |
| 124 | public void setDisplay(Display display) { |
| 125 | mDisplay = display; |
| 126 | } |
| 127 | |
| 128 | public void getInputConfiguration(Configuration config) { |
| 129 | synchronized (mFirst) { |
| 130 | config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH; |
| 131 | config.keyboard = Configuration.KEYBOARD_NOKEYS; |
| 132 | config.navigation = Configuration.NAVIGATION_NONAV; |
| 133 | |
| 134 | final int N = mDevices.size(); |
| 135 | for (int i=0; i<N; i++) { |
| 136 | InputDevice d = mDevices.valueAt(i); |
| 137 | if (d != null) { |
| 138 | if ((d.classes&RawInputEvent.CLASS_TOUCHSCREEN) != 0) { |
| 139 | config.touchscreen |
| 140 | = Configuration.TOUCHSCREEN_FINGER; |
| 141 | //Log.i("foo", "***** HAVE TOUCHSCREEN!"); |
| 142 | } |
| 143 | if ((d.classes&RawInputEvent.CLASS_ALPHAKEY) != 0) { |
| 144 | config.keyboard |
| 145 | = Configuration.KEYBOARD_QWERTY; |
| 146 | //Log.i("foo", "***** HAVE QWERTY!"); |
| 147 | } |
| 148 | if ((d.classes&RawInputEvent.CLASS_TRACKBALL) != 0) { |
| 149 | config.navigation |
| 150 | = Configuration.NAVIGATION_TRACKBALL; |
| 151 | //Log.i("foo", "***** HAVE TRACKBALL!"); |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | public static native String getDeviceName(int deviceId); |
| 159 | public static native int getDeviceClasses(int deviceId); |
| 160 | public static native boolean getAbsoluteInfo(int deviceId, int axis, |
| 161 | InputDevice.AbsoluteInfo outInfo); |
| 162 | public static native int getSwitchState(int sw); |
| 163 | public static native int getSwitchState(int deviceId, int sw); |
| 164 | public static native int getScancodeState(int sw); |
| 165 | public static native int getScancodeState(int deviceId, int sw); |
| 166 | public static native int getKeycodeState(int sw); |
| 167 | public static native int getKeycodeState(int deviceId, int sw); |
| 168 | public static native boolean hasKeys(int[] keycodes, boolean[] keyExists); |
| 169 | |
| 170 | public static KeyEvent newKeyEvent(InputDevice device, long downTime, |
| 171 | long eventTime, boolean down, int keycode, int repeatCount, |
| 172 | int scancode, int flags) { |
| 173 | return new KeyEvent( |
| 174 | downTime, eventTime, |
| 175 | down ? KeyEvent.ACTION_DOWN : KeyEvent.ACTION_UP, |
| 176 | keycode, repeatCount, |
| 177 | device != null ? device.mMetaKeysState : 0, |
| 178 | device != null ? device.id : -1, scancode, |
| 179 | flags); |
| 180 | } |
| 181 | |
| 182 | Thread mThread = new Thread("InputDeviceReader") { |
| 183 | public void run() { |
| 184 | android.os.Process.setThreadPriority( |
| 185 | android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY); |
| 186 | |
| 187 | try { |
| 188 | RawInputEvent ev = new RawInputEvent(); |
| 189 | while (true) { |
| 190 | InputDevice di; |
| 191 | |
| 192 | // block, doesn't release the monitor |
| 193 | readEvent(ev); |
| 194 | |
| 195 | boolean send = false; |
| 196 | boolean configChanged = false; |
| 197 | |
| 198 | if (false) { |
| 199 | Log.i(TAG, "Input event: dev=0x" |
| 200 | + Integer.toHexString(ev.deviceId) |
| 201 | + " type=0x" + Integer.toHexString(ev.type) |
| 202 | + " scancode=" + ev.scancode |
| 203 | + " keycode=" + ev.keycode |
| 204 | + " value=" + ev.value); |
| 205 | } |
| 206 | |
| 207 | if (ev.type == RawInputEvent.EV_DEVICE_ADDED) { |
| 208 | synchronized (mFirst) { |
| 209 | di = newInputDevice(ev.deviceId); |
| 210 | mDevices.put(ev.deviceId, di); |
| 211 | configChanged = true; |
| 212 | } |
| 213 | } else if (ev.type == RawInputEvent.EV_DEVICE_REMOVED) { |
| 214 | synchronized (mFirst) { |
| 215 | Log.i(TAG, "Device removed: id=0x" |
| 216 | + Integer.toHexString(ev.deviceId)); |
| 217 | di = mDevices.get(ev.deviceId); |
| 218 | if (di != null) { |
| 219 | mDevices.delete(ev.deviceId); |
| 220 | configChanged = true; |
| 221 | } else { |
| 222 | Log.w(TAG, "Bad device id: " + ev.deviceId); |
| 223 | } |
| 224 | } |
| 225 | } else { |
| 226 | di = getInputDevice(ev.deviceId); |
| 227 | |
| 228 | // first crack at it |
| 229 | send = preprocessEvent(di, ev); |
| 230 | |
| 231 | if (ev.type == RawInputEvent.EV_KEY) { |
| 232 | di.mMetaKeysState = makeMetaState(ev.keycode, |
| 233 | ev.value != 0, di.mMetaKeysState); |
| 234 | mHaveGlobalMetaState = false; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | if (di == null) { |
| 239 | continue; |
| 240 | } |
| 241 | |
| 242 | if (configChanged) { |
| 243 | synchronized (mFirst) { |
| 244 | addLocked(di, SystemClock.uptimeMillis(), 0, |
| 245 | RawInputEvent.CLASS_CONFIGURATION_CHANGED, |
| 246 | null); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | if (!send) { |
| 251 | continue; |
| 252 | } |
| 253 | |
| 254 | synchronized (mFirst) { |
| 255 | // NOTE: The event timebase absolutely must be the same |
| 256 | // timebase as SystemClock.uptimeMillis(). |
| 257 | //curTime = gotOne ? ev.when : SystemClock.uptimeMillis(); |
| 258 | final long curTime = SystemClock.uptimeMillis(); |
| 259 | //Log.i(TAG, "curTime=" + curTime + ", systemClock=" + SystemClock.uptimeMillis()); |
| 260 | |
| 261 | final int classes = di.classes; |
| 262 | final int type = ev.type; |
| 263 | final int scancode = ev.scancode; |
| 264 | send = false; |
| 265 | |
| 266 | // Is it a key event? |
| 267 | if (type == RawInputEvent.EV_KEY && |
| 268 | (classes&RawInputEvent.CLASS_KEYBOARD) != 0 && |
| 269 | (scancode < RawInputEvent.BTN_FIRST || |
| 270 | scancode > RawInputEvent.BTN_LAST)) { |
| 271 | boolean down; |
| 272 | if (ev.value != 0) { |
| 273 | down = true; |
| 274 | di.mDownTime = curTime; |
| 275 | } else { |
| 276 | down = false; |
| 277 | } |
| 278 | int keycode = rotateKeyCodeLocked(ev.keycode); |
| 279 | addLocked(di, curTime, ev.flags, |
| 280 | RawInputEvent.CLASS_KEYBOARD, |
| 281 | newKeyEvent(di, di.mDownTime, curTime, down, |
| 282 | keycode, 0, scancode, |
| 283 | ((ev.flags & WindowManagerPolicy.FLAG_WOKE_HERE) != 0) |
| 284 | ? KeyEvent.FLAG_WOKE_HERE : 0)); |
| 285 | } else if (ev.type == RawInputEvent.EV_KEY) { |
| 286 | if (ev.scancode == RawInputEvent.BTN_TOUCH && |
| 287 | (classes&RawInputEvent.CLASS_TOUCHSCREEN) != 0) { |
| 288 | di.mAbs.changed = true; |
| 289 | di.mAbs.down = ev.value != 0; |
| 290 | } |
| 291 | if (ev.scancode == RawInputEvent.BTN_MOUSE && |
| 292 | (classes&RawInputEvent.CLASS_TRACKBALL) != 0) { |
| 293 | di.mRel.changed = true; |
| 294 | di.mRel.down = ev.value != 0; |
| 295 | send = true; |
| 296 | } |
| 297 | |
| 298 | } else if (ev.type == RawInputEvent.EV_ABS && |
| 299 | (classes&RawInputEvent.CLASS_TOUCHSCREEN) != 0) { |
| 300 | if (ev.scancode == RawInputEvent.ABS_X) { |
| 301 | di.mAbs.changed = true; |
| 302 | di.mAbs.x = ev.value; |
| 303 | } else if (ev.scancode == RawInputEvent.ABS_Y) { |
| 304 | di.mAbs.changed = true; |
| 305 | di.mAbs.y = ev.value; |
| 306 | } else if (ev.scancode == RawInputEvent.ABS_PRESSURE) { |
| 307 | di.mAbs.changed = true; |
| 308 | di.mAbs.pressure = ev.value; |
| 309 | } else if (ev.scancode == RawInputEvent.ABS_TOOL_WIDTH) { |
| 310 | di.mAbs.changed = true; |
| 311 | di.mAbs.size = ev.value; |
| 312 | } |
| 313 | |
| 314 | } else if (ev.type == RawInputEvent.EV_REL && |
| 315 | (classes&RawInputEvent.CLASS_TRACKBALL) != 0) { |
| 316 | // Add this relative movement into our totals. |
| 317 | if (ev.scancode == RawInputEvent.REL_X) { |
| 318 | di.mRel.changed = true; |
| 319 | di.mRel.x += ev.value; |
| 320 | } else if (ev.scancode == RawInputEvent.REL_Y) { |
| 321 | di.mRel.changed = true; |
| 322 | di.mRel.y += ev.value; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | if (send || ev.type == RawInputEvent.EV_SYN) { |
| 327 | if (mDisplay != null) { |
| 328 | if (!mHaveGlobalMetaState) { |
| 329 | computeGlobalMetaStateLocked(); |
| 330 | } |
| 331 | |
| 332 | MotionEvent me; |
| 333 | me = di.mAbs.generateMotion(di, curTime, true, |
| 334 | mDisplay, mOrientation, mGlobalMetaState); |
| 335 | if (false) Log.v(TAG, "Absolute: x=" + di.mAbs.x |
| 336 | + " y=" + di.mAbs.y + " ev=" + me); |
| 337 | if (me != null) { |
| 338 | if (WindowManagerPolicy.WATCH_POINTER) { |
| 339 | Log.i(TAG, "Enqueueing: " + me); |
| 340 | } |
| 341 | addLocked(di, curTime, ev.flags, |
| 342 | RawInputEvent.CLASS_TOUCHSCREEN, me); |
| 343 | } |
| 344 | me = di.mRel.generateMotion(di, curTime, false, |
| 345 | mDisplay, mOrientation, mGlobalMetaState); |
| 346 | if (false) Log.v(TAG, "Relative: x=" + di.mRel.x |
| 347 | + " y=" + di.mRel.y + " ev=" + me); |
| 348 | if (me != null) { |
| 349 | addLocked(di, curTime, ev.flags, |
| 350 | RawInputEvent.CLASS_TRACKBALL, me); |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | catch (RuntimeException exc) { |
| 358 | Log.e(TAG, "InputReaderThread uncaught exception", exc); |
| 359 | } |
| 360 | } |
| 361 | }; |
| 362 | |
| 363 | /** |
| 364 | * Returns a new meta state for the given keys and old state. |
| 365 | */ |
| 366 | private static final int makeMetaState(int keycode, boolean down, int old) { |
| 367 | int mask; |
| 368 | switch (keycode) { |
| 369 | case KeyEvent.KEYCODE_ALT_LEFT: |
| 370 | mask = KeyEvent.META_ALT_LEFT_ON; |
| 371 | break; |
| 372 | case KeyEvent.KEYCODE_ALT_RIGHT: |
| 373 | mask = KeyEvent.META_ALT_RIGHT_ON; |
| 374 | break; |
| 375 | case KeyEvent.KEYCODE_SHIFT_LEFT: |
| 376 | mask = KeyEvent.META_SHIFT_LEFT_ON; |
| 377 | break; |
| 378 | case KeyEvent.KEYCODE_SHIFT_RIGHT: |
| 379 | mask = KeyEvent.META_SHIFT_RIGHT_ON; |
| 380 | break; |
| 381 | case KeyEvent.KEYCODE_SYM: |
| 382 | mask = KeyEvent.META_SYM_ON; |
| 383 | break; |
| 384 | default: |
| 385 | return old; |
| 386 | } |
| 387 | int result = ~(KeyEvent.META_ALT_ON | KeyEvent.META_SHIFT_ON) |
| 388 | & (down ? (old | mask) : (old & ~mask)); |
| 389 | if (0 != (result & (KeyEvent.META_ALT_LEFT_ON | KeyEvent.META_ALT_RIGHT_ON))) { |
| 390 | result |= KeyEvent.META_ALT_ON; |
| 391 | } |
| 392 | if (0 != (result & (KeyEvent.META_SHIFT_LEFT_ON | KeyEvent.META_SHIFT_RIGHT_ON))) { |
| 393 | result |= KeyEvent.META_SHIFT_ON; |
| 394 | } |
| 395 | return result; |
| 396 | } |
| 397 | |
| 398 | private void computeGlobalMetaStateLocked() { |
| 399 | int i = mDevices.size(); |
| 400 | mGlobalMetaState = 0; |
| 401 | while ((--i) >= 0) { |
| 402 | mGlobalMetaState |= mDevices.valueAt(i).mMetaKeysState; |
| 403 | } |
| 404 | mHaveGlobalMetaState = true; |
| 405 | } |
| 406 | |
| 407 | /* |
| 408 | * Return true if you want the event to get passed on to the |
| 409 | * rest of the system, and false if you've handled it and want |
| 410 | * it dropped. |
| 411 | */ |
| 412 | abstract boolean preprocessEvent(InputDevice device, RawInputEvent event); |
| 413 | |
| 414 | InputDevice getInputDevice(int deviceId) { |
| 415 | synchronized (mFirst) { |
| 416 | return getInputDeviceLocked(deviceId); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | private InputDevice getInputDeviceLocked(int deviceId) { |
| 421 | return mDevices.get(deviceId); |
| 422 | } |
| 423 | |
| 424 | public void setOrientation(int orientation) { |
| 425 | synchronized(mFirst) { |
| 426 | mOrientation = orientation; |
| 427 | switch (orientation) { |
| 428 | case Surface.ROTATION_90: |
| 429 | mKeyRotationMap = KEY_90_MAP; |
| 430 | break; |
| 431 | case Surface.ROTATION_180: |
| 432 | mKeyRotationMap = KEY_180_MAP; |
| 433 | break; |
| 434 | case Surface.ROTATION_270: |
| 435 | mKeyRotationMap = KEY_270_MAP; |
| 436 | break; |
| 437 | default: |
| 438 | mKeyRotationMap = null; |
| 439 | break; |
| 440 | } |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | public int rotateKeyCode(int keyCode) { |
| 445 | synchronized(mFirst) { |
| 446 | return rotateKeyCodeLocked(keyCode); |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | private int rotateKeyCodeLocked(int keyCode) { |
| 451 | int[] map = mKeyRotationMap; |
| 452 | if (map != null) { |
| 453 | final int N = map.length; |
| 454 | for (int i=0; i<N; i+=2) { |
| 455 | if (map[i] == keyCode) { |
| 456 | return map[i+1]; |
| 457 | } |
| 458 | } |
| 459 | } |
| 460 | return keyCode; |
| 461 | } |
| 462 | |
| 463 | boolean hasEvents() { |
| 464 | synchronized (mFirst) { |
| 465 | return mFirst.next != mLast; |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | /* |
| 470 | * returns true if we returned an event, and false if we timed out |
| 471 | */ |
| 472 | QueuedEvent getEvent(long timeoutMS) { |
| 473 | long begin = SystemClock.uptimeMillis(); |
| 474 | final long end = begin+timeoutMS; |
| 475 | long now = begin; |
| 476 | synchronized (mFirst) { |
| 477 | while (mFirst.next == mLast && end > now) { |
| 478 | try { |
| 479 | mWakeLock.release(); |
| 480 | mFirst.wait(end-now); |
| 481 | } |
| 482 | catch (InterruptedException e) { |
| 483 | } |
| 484 | now = SystemClock.uptimeMillis(); |
| 485 | if (begin > now) { |
| 486 | begin = now; |
| 487 | } |
| 488 | } |
| 489 | if (mFirst.next == mLast) { |
| 490 | return null; |
| 491 | } |
| 492 | QueuedEvent p = mFirst.next; |
| 493 | mFirst.next = p.next; |
| 494 | mFirst.next.prev = mFirst; |
| 495 | p.inQueue = false; |
| 496 | return p; |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | void recycleEvent(QueuedEvent ev) { |
| 501 | synchronized (mFirst) { |
| 502 | //Log.i(TAG, "Recycle event: " + ev); |
| 503 | if (ev.event == ev.inputDevice.mAbs.currentMove) { |
| 504 | ev.inputDevice.mAbs.currentMove = null; |
| 505 | } |
| 506 | if (ev.event == ev.inputDevice.mRel.currentMove) { |
| 507 | if (false) Log.i(TAG, "Detach rel " + ev.event); |
| 508 | ev.inputDevice.mRel.currentMove = null; |
| 509 | ev.inputDevice.mRel.x = 0; |
| 510 | ev.inputDevice.mRel.y = 0; |
| 511 | } |
| 512 | recycleLocked(ev); |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | void filterQueue(FilterCallback cb) { |
| 517 | synchronized (mFirst) { |
| 518 | QueuedEvent cur = mLast.prev; |
| 519 | while (cur.prev != null) { |
| 520 | switch (cb.filterEvent(cur)) { |
| 521 | case FILTER_REMOVE: |
| 522 | cur.prev.next = cur.next; |
| 523 | cur.next.prev = cur.prev; |
| 524 | break; |
| 525 | case FILTER_ABORT: |
| 526 | return; |
| 527 | } |
| 528 | cur = cur.prev; |
| 529 | } |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | private QueuedEvent obtainLocked(InputDevice device, long when, |
| 534 | int flags, int classType, Object event) { |
| 535 | QueuedEvent ev; |
| 536 | if (mCacheCount == 0) { |
| 537 | ev = new QueuedEvent(); |
| 538 | } else { |
| 539 | ev = mCache; |
| 540 | ev.inQueue = false; |
| 541 | mCache = ev.next; |
| 542 | mCacheCount--; |
| 543 | } |
| 544 | ev.inputDevice = device; |
| 545 | ev.when = when; |
| 546 | ev.flags = flags; |
| 547 | ev.classType = classType; |
| 548 | ev.event = event; |
| 549 | return ev; |
| 550 | } |
| 551 | |
| 552 | private void recycleLocked(QueuedEvent ev) { |
| 553 | if (ev.inQueue) { |
| 554 | throw new RuntimeException("Event already in queue!"); |
| 555 | } |
| 556 | if (mCacheCount < 10) { |
| 557 | mCacheCount++; |
| 558 | ev.next = mCache; |
| 559 | mCache = ev; |
| 560 | ev.inQueue = true; |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | private void addLocked(InputDevice device, long when, int flags, |
| 565 | int classType, Object event) { |
| 566 | boolean poke = mFirst.next == mLast; |
| 567 | |
| 568 | QueuedEvent ev = obtainLocked(device, when, flags, classType, event); |
| 569 | QueuedEvent p = mLast.prev; |
| 570 | while (p != mFirst && ev.when < p.when) { |
| 571 | p = p.prev; |
| 572 | } |
| 573 | |
| 574 | ev.next = p.next; |
| 575 | ev.prev = p; |
| 576 | p.next = ev; |
| 577 | ev.next.prev = ev; |
| 578 | ev.inQueue = true; |
| 579 | |
| 580 | if (poke) { |
| 581 | mFirst.notify(); |
| 582 | mWakeLock.acquire(); |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | private InputDevice newInputDevice(int deviceId) { |
| 587 | int classes = getDeviceClasses(deviceId); |
| 588 | String name = getDeviceName(deviceId); |
| 589 | Log.i(TAG, "Device added: id=0x" + Integer.toHexString(deviceId) |
| 590 | + ", name=" + name |
| 591 | + ", classes=" + Integer.toHexString(classes)); |
| 592 | InputDevice.AbsoluteInfo absX; |
| 593 | InputDevice.AbsoluteInfo absY; |
| 594 | InputDevice.AbsoluteInfo absPressure; |
| 595 | InputDevice.AbsoluteInfo absSize; |
| 596 | if ((classes&RawInputEvent.CLASS_TOUCHSCREEN) != 0) { |
| 597 | absX = loadAbsoluteInfo(deviceId, RawInputEvent.ABS_X, "X"); |
| 598 | absY = loadAbsoluteInfo(deviceId, RawInputEvent.ABS_Y, "Y"); |
| 599 | absPressure = loadAbsoluteInfo(deviceId, RawInputEvent.ABS_PRESSURE, "Pressure"); |
| 600 | absSize = loadAbsoluteInfo(deviceId, RawInputEvent.ABS_TOOL_WIDTH, "Size"); |
| 601 | } else { |
| 602 | absX = null; |
| 603 | absY = null; |
| 604 | absPressure = null; |
| 605 | absSize = null; |
| 606 | } |
| 607 | |
| 608 | return new InputDevice(deviceId, classes, name, absX, absY, absPressure, absSize); |
| 609 | } |
| 610 | |
| 611 | private InputDevice.AbsoluteInfo loadAbsoluteInfo(int id, int channel, |
| 612 | String name) { |
| 613 | InputDevice.AbsoluteInfo info = new InputDevice.AbsoluteInfo(); |
| 614 | if (getAbsoluteInfo(id, channel, info) |
| 615 | && info.minValue != info.maxValue) { |
| 616 | Log.i(TAG, " " + name + ": min=" + info.minValue |
| 617 | + " max=" + info.maxValue |
| 618 | + " flat=" + info.flat |
| 619 | + " fuzz=" + info.fuzz); |
| 620 | info.range = info.maxValue-info.minValue; |
| 621 | return info; |
| 622 | } |
| 623 | Log.i(TAG, " " + name + ": unknown values"); |
| 624 | return null; |
| 625 | } |
| 626 | private static native boolean readEvent(RawInputEvent outEvent); |
| 627 | } |