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