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; |
Mike Lockwood | 1d9dfc5 | 2009-07-16 11:11:18 -0400 | [diff] [blame] | 21 | import android.os.Environment; |
Michael Chan | 53071d6 | 2009-05-13 17:29:48 -0700 | [diff] [blame] | 22 | import android.os.LatencyTimer; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import android.os.PowerManager; |
Michael Chan | 53071d6 | 2009-05-13 17:29:48 -0700 | [diff] [blame] | 24 | import android.os.SystemClock; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import android.util.Log; |
| 26 | import android.util.SparseArray; |
Mike Lockwood | 1d9dfc5 | 2009-07-16 11:11:18 -0400 | [diff] [blame] | 27 | import android.util.Xml; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | import android.view.Display; |
| 29 | import android.view.KeyEvent; |
| 30 | import android.view.MotionEvent; |
| 31 | import android.view.RawInputEvent; |
| 32 | import android.view.Surface; |
| 33 | import android.view.WindowManagerPolicy; |
| 34 | |
Mike Lockwood | 1d9dfc5 | 2009-07-16 11:11:18 -0400 | [diff] [blame] | 35 | import com.android.internal.util.XmlUtils; |
| 36 | |
| 37 | import org.xmlpull.v1.XmlPullParser; |
Mike Lockwood | 1d9dfc5 | 2009-07-16 11:11:18 -0400 | [diff] [blame] | 38 | |
Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 39 | import java.io.BufferedReader; |
Mike Lockwood | 1d9dfc5 | 2009-07-16 11:11:18 -0400 | [diff] [blame] | 40 | import java.io.File; |
Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 41 | import java.io.FileInputStream; |
| 42 | import java.io.FileNotFoundException; |
Mike Lockwood | 1d9dfc5 | 2009-07-16 11:11:18 -0400 | [diff] [blame] | 43 | import java.io.FileReader; |
Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 44 | import java.io.IOException; |
| 45 | import java.io.InputStreamReader; |
| 46 | import java.util.ArrayList; |
| 47 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | public abstract class KeyInputQueue { |
| 49 | static final String TAG = "KeyInputQueue"; |
| 50 | |
Dianne Hackborn | bd0a81f | 2009-10-04 13:30:50 -0700 | [diff] [blame] | 51 | static final boolean DEBUG = false; |
Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 52 | static final boolean DEBUG_VIRTUAL_KEYS = false; |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 53 | static final boolean DEBUG_POINTERS = false; |
Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 54 | |
Dianne Hackborn | 1411d1c | 2009-10-12 23:21:18 -0700 | [diff] [blame] | 55 | /** |
| 56 | * Turn on some hacks we have to improve the touch interaction with a |
| 57 | * certain device whose screen currently is not all that good. |
| 58 | */ |
| 59 | static final boolean BAD_TOUCH_HACK = true; |
| 60 | |
Mike Lockwood | 1d9dfc5 | 2009-07-16 11:11:18 -0400 | [diff] [blame] | 61 | private static final String EXCLUDED_DEVICES_PATH = "etc/excluded-input-devices.xml"; |
| 62 | |
Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 63 | final SparseArray<InputDevice> mDevices = new SparseArray<InputDevice>(); |
Dianne Hackborn | a8f6018 | 2009-09-01 19:01:50 -0700 | [diff] [blame] | 64 | final SparseArray<InputDevice> mIgnoredDevices = new SparseArray<InputDevice>(); |
Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 65 | final ArrayList<VirtualKey> mVirtualKeys = new ArrayList<VirtualKey>(); |
Dianne Hackborn | ddca3ee | 2009-07-23 19:01:31 -0700 | [diff] [blame] | 66 | final HapticFeedbackCallback mHapticFeedbackCallback; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | |
| 68 | int mGlobalMetaState = 0; |
| 69 | boolean mHaveGlobalMetaState = false; |
| 70 | |
| 71 | final QueuedEvent mFirst; |
| 72 | final QueuedEvent mLast; |
| 73 | QueuedEvent mCache; |
| 74 | int mCacheCount; |
| 75 | |
| 76 | Display mDisplay = null; |
Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 77 | int mDisplayWidth; |
| 78 | int mDisplayHeight; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | |
| 80 | int mOrientation = Surface.ROTATION_0; |
| 81 | int[] mKeyRotationMap = null; |
| 82 | |
Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 83 | VirtualKey mPressedVirtualKey = null; |
| 84 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | PowerManager.WakeLock mWakeLock; |
| 86 | |
| 87 | static final int[] KEY_90_MAP = new int[] { |
| 88 | KeyEvent.KEYCODE_DPAD_DOWN, KeyEvent.KEYCODE_DPAD_RIGHT, |
| 89 | KeyEvent.KEYCODE_DPAD_RIGHT, KeyEvent.KEYCODE_DPAD_UP, |
| 90 | KeyEvent.KEYCODE_DPAD_UP, KeyEvent.KEYCODE_DPAD_LEFT, |
| 91 | KeyEvent.KEYCODE_DPAD_LEFT, KeyEvent.KEYCODE_DPAD_DOWN, |
| 92 | }; |
| 93 | |
| 94 | static final int[] KEY_180_MAP = new int[] { |
| 95 | KeyEvent.KEYCODE_DPAD_DOWN, KeyEvent.KEYCODE_DPAD_UP, |
| 96 | KeyEvent.KEYCODE_DPAD_RIGHT, KeyEvent.KEYCODE_DPAD_LEFT, |
| 97 | KeyEvent.KEYCODE_DPAD_UP, KeyEvent.KEYCODE_DPAD_DOWN, |
| 98 | KeyEvent.KEYCODE_DPAD_LEFT, KeyEvent.KEYCODE_DPAD_RIGHT, |
| 99 | }; |
| 100 | |
| 101 | static final int[] KEY_270_MAP = new int[] { |
| 102 | KeyEvent.KEYCODE_DPAD_DOWN, KeyEvent.KEYCODE_DPAD_LEFT, |
| 103 | KeyEvent.KEYCODE_DPAD_LEFT, KeyEvent.KEYCODE_DPAD_UP, |
| 104 | KeyEvent.KEYCODE_DPAD_UP, KeyEvent.KEYCODE_DPAD_RIGHT, |
| 105 | KeyEvent.KEYCODE_DPAD_RIGHT, KeyEvent.KEYCODE_DPAD_DOWN, |
| 106 | }; |
| 107 | |
| 108 | public static final int FILTER_REMOVE = 0; |
| 109 | public static final int FILTER_KEEP = 1; |
| 110 | public static final int FILTER_ABORT = -1; |
Michael Chan | 53071d6 | 2009-05-13 17:29:48 -0700 | [diff] [blame] | 111 | |
| 112 | private static final boolean MEASURE_LATENCY = false; |
| 113 | private LatencyTimer lt; |
| 114 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | public interface FilterCallback { |
| 116 | int filterEvent(QueuedEvent ev); |
| 117 | } |
| 118 | |
Dianne Hackborn | ddca3ee | 2009-07-23 19:01:31 -0700 | [diff] [blame] | 119 | public interface HapticFeedbackCallback { |
| 120 | void virtualKeyFeedback(KeyEvent event); |
| 121 | } |
| 122 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | static class QueuedEvent { |
| 124 | InputDevice inputDevice; |
Michael Chan | 53071d6 | 2009-05-13 17:29:48 -0700 | [diff] [blame] | 125 | long whenNano; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | int flags; // From the raw event |
| 127 | int classType; // One of the class constants in InputEvent |
| 128 | Object event; |
| 129 | boolean inQueue; |
| 130 | |
| 131 | void copyFrom(QueuedEvent that) { |
| 132 | this.inputDevice = that.inputDevice; |
Michael Chan | 53071d6 | 2009-05-13 17:29:48 -0700 | [diff] [blame] | 133 | this.whenNano = that.whenNano; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 134 | this.flags = that.flags; |
| 135 | this.classType = that.classType; |
| 136 | this.event = that.event; |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public String toString() { |
| 141 | return "QueuedEvent{" |
| 142 | + Integer.toHexString(System.identityHashCode(this)) |
| 143 | + " " + event + "}"; |
| 144 | } |
| 145 | |
| 146 | // not copied |
| 147 | QueuedEvent prev; |
| 148 | QueuedEvent next; |
| 149 | } |
| 150 | |
Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 151 | /** |
| 152 | * A key that exists as a part of the touch-screen, outside of the normal |
| 153 | * display area of the screen. |
| 154 | */ |
| 155 | static class VirtualKey { |
| 156 | int scancode; |
| 157 | int centerx; |
| 158 | int centery; |
| 159 | int width; |
| 160 | int height; |
| 161 | |
| 162 | int hitLeft; |
| 163 | int hitTop; |
| 164 | int hitRight; |
| 165 | int hitBottom; |
| 166 | |
| 167 | InputDevice lastDevice; |
| 168 | int lastKeycode; |
| 169 | |
| 170 | boolean checkHit(int x, int y) { |
| 171 | return (x >= hitLeft && x <= hitRight |
| 172 | && y >= hitTop && y <= hitBottom); |
| 173 | } |
| 174 | |
| 175 | void computeHitRect(InputDevice dev, int dw, int dh) { |
| 176 | if (dev == lastDevice) { |
| 177 | return; |
| 178 | } |
| 179 | |
| 180 | if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "computeHitRect for " + scancode |
| 181 | + ": dev=" + dev + " absX=" + dev.absX + " absY=" + dev.absY); |
| 182 | |
| 183 | lastDevice = dev; |
| 184 | |
| 185 | int minx = dev.absX.minValue; |
| 186 | int maxx = dev.absX.maxValue; |
| 187 | |
| 188 | int halfw = width/2; |
| 189 | int left = centerx - halfw; |
| 190 | int right = centerx + halfw; |
| 191 | hitLeft = minx + ((left*maxx-minx)/dw); |
| 192 | hitRight = minx + ((right*maxx-minx)/dw); |
| 193 | |
| 194 | int miny = dev.absY.minValue; |
| 195 | int maxy = dev.absY.maxValue; |
| 196 | |
| 197 | int halfh = height/2; |
| 198 | int top = centery - halfh; |
| 199 | int bottom = centery + halfh; |
| 200 | hitTop = miny + ((top*maxy-miny)/dh); |
| 201 | hitBottom = miny + ((bottom*maxy-miny)/dh); |
| 202 | } |
| 203 | } |
Michael Chan | 53071d6 | 2009-05-13 17:29:48 -0700 | [diff] [blame] | 204 | |
Iliyan Malchev | 75b2aed | 2009-08-06 14:50:57 -0700 | [diff] [blame] | 205 | private void readVirtualKeys(String deviceName) { |
Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 206 | try { |
| 207 | FileInputStream fis = new FileInputStream( |
Iliyan Malchev | 75b2aed | 2009-08-06 14:50:57 -0700 | [diff] [blame] | 208 | "/sys/board_properties/virtualkeys." + deviceName); |
Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 209 | InputStreamReader isr = new InputStreamReader(fis); |
Dianne Hackborn | bd0a81f | 2009-10-04 13:30:50 -0700 | [diff] [blame] | 210 | BufferedReader br = new BufferedReader(isr, 2048); |
Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 211 | String str = br.readLine(); |
| 212 | if (str != null) { |
| 213 | String[] it = str.split(":"); |
| 214 | if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "***** VIRTUAL KEYS: " + it); |
| 215 | final int N = it.length-6; |
| 216 | for (int i=0; i<=N; i+=6) { |
| 217 | if (!"0x01".equals(it[i])) { |
| 218 | Log.w(TAG, "Unknown virtual key type at elem #" + i |
| 219 | + ": " + it[i]); |
| 220 | continue; |
| 221 | } |
| 222 | try { |
| 223 | VirtualKey sb = new VirtualKey(); |
| 224 | sb.scancode = Integer.parseInt(it[i+1]); |
| 225 | sb.centerx = Integer.parseInt(it[i+2]); |
| 226 | sb.centery = Integer.parseInt(it[i+3]); |
| 227 | sb.width = Integer.parseInt(it[i+4]); |
| 228 | sb.height = Integer.parseInt(it[i+5]); |
| 229 | if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "Virtual key " |
| 230 | + sb.scancode + ": center=" + sb.centerx + "," |
| 231 | + sb.centery + " size=" + sb.width + "x" |
| 232 | + sb.height); |
| 233 | mVirtualKeys.add(sb); |
| 234 | } catch (NumberFormatException e) { |
| 235 | Log.w(TAG, "Bad number at region " + i + " in: " |
| 236 | + str, e); |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | br.close(); |
| 241 | } catch (FileNotFoundException e) { |
| 242 | Log.i(TAG, "No virtual keys found"); |
| 243 | } catch (IOException e) { |
| 244 | Log.w(TAG, "Error reading virtual keys", e); |
| 245 | } |
Mike Lockwood | 1d9dfc5 | 2009-07-16 11:11:18 -0400 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | private void readExcludedDevices() { |
| 249 | // Read partner-provided list of excluded input devices |
| 250 | XmlPullParser parser = null; |
| 251 | // Environment.getRootDirectory() is a fancy way of saying ANDROID_ROOT or "/system". |
| 252 | File confFile = new File(Environment.getRootDirectory(), EXCLUDED_DEVICES_PATH); |
| 253 | FileReader confreader = null; |
| 254 | try { |
| 255 | confreader = new FileReader(confFile); |
| 256 | parser = Xml.newPullParser(); |
| 257 | parser.setInput(confreader); |
| 258 | XmlUtils.beginDocument(parser, "devices"); |
| 259 | |
| 260 | while (true) { |
| 261 | XmlUtils.nextElement(parser); |
| 262 | if (!"device".equals(parser.getName())) { |
| 263 | break; |
| 264 | } |
| 265 | String name = parser.getAttributeValue(null, "name"); |
| 266 | if (name != null) { |
Dianne Hackborn | bd0a81f | 2009-10-04 13:30:50 -0700 | [diff] [blame] | 267 | if (DEBUG) Log.v(TAG, "addExcludedDevice " + name); |
Mike Lockwood | 1d9dfc5 | 2009-07-16 11:11:18 -0400 | [diff] [blame] | 268 | addExcludedDevice(name); |
| 269 | } |
| 270 | } |
| 271 | } catch (FileNotFoundException e) { |
| 272 | // It's ok if the file does not exist. |
| 273 | } catch (Exception e) { |
| 274 | Log.e(TAG, "Exception while parsing '" + confFile.getAbsolutePath() + "'", e); |
| 275 | } finally { |
| 276 | try { if (confreader != null) confreader.close(); } catch (IOException e) { } |
| 277 | } |
| 278 | } |
| 279 | |
Dianne Hackborn | ddca3ee | 2009-07-23 19:01:31 -0700 | [diff] [blame] | 280 | KeyInputQueue(Context context, HapticFeedbackCallback hapticFeedbackCallback) { |
Mike Lockwood | 1d9dfc5 | 2009-07-16 11:11:18 -0400 | [diff] [blame] | 281 | if (MEASURE_LATENCY) { |
| 282 | lt = new LatencyTimer(100, 1000); |
| 283 | } |
| 284 | |
Dianne Hackborn | ddca3ee | 2009-07-23 19:01:31 -0700 | [diff] [blame] | 285 | mHapticFeedbackCallback = hapticFeedbackCallback; |
| 286 | |
Mike Lockwood | 1d9dfc5 | 2009-07-16 11:11:18 -0400 | [diff] [blame] | 287 | readExcludedDevices(); |
Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 288 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 289 | PowerManager pm = (PowerManager)context.getSystemService( |
| 290 | Context.POWER_SERVICE); |
| 291 | mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, |
| 292 | "KeyInputQueue"); |
| 293 | mWakeLock.setReferenceCounted(false); |
| 294 | |
| 295 | mFirst = new QueuedEvent(); |
| 296 | mLast = new QueuedEvent(); |
| 297 | mFirst.next = mLast; |
| 298 | mLast.prev = mFirst; |
| 299 | |
| 300 | mThread.start(); |
| 301 | } |
| 302 | |
| 303 | public void setDisplay(Display display) { |
| 304 | mDisplay = display; |
Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 305 | |
| 306 | // We assume at this point that the display dimensions reflect the |
| 307 | // natural, unrotated display. We will perform hit tests for soft |
| 308 | // buttons based on that display. |
| 309 | mDisplayWidth = display.getWidth(); |
| 310 | mDisplayHeight = display.getHeight(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | public void getInputConfiguration(Configuration config) { |
| 314 | synchronized (mFirst) { |
| 315 | config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH; |
| 316 | config.keyboard = Configuration.KEYBOARD_NOKEYS; |
| 317 | config.navigation = Configuration.NAVIGATION_NONAV; |
| 318 | |
| 319 | final int N = mDevices.size(); |
| 320 | for (int i=0; i<N; i++) { |
| 321 | InputDevice d = mDevices.valueAt(i); |
| 322 | if (d != null) { |
| 323 | if ((d.classes&RawInputEvent.CLASS_TOUCHSCREEN) != 0) { |
| 324 | config.touchscreen |
| 325 | = Configuration.TOUCHSCREEN_FINGER; |
| 326 | //Log.i("foo", "***** HAVE TOUCHSCREEN!"); |
| 327 | } |
| 328 | if ((d.classes&RawInputEvent.CLASS_ALPHAKEY) != 0) { |
| 329 | config.keyboard |
| 330 | = Configuration.KEYBOARD_QWERTY; |
| 331 | //Log.i("foo", "***** HAVE QWERTY!"); |
| 332 | } |
| 333 | if ((d.classes&RawInputEvent.CLASS_TRACKBALL) != 0) { |
| 334 | config.navigation |
| 335 | = Configuration.NAVIGATION_TRACKBALL; |
| 336 | //Log.i("foo", "***** HAVE TRACKBALL!"); |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 337 | } else if ((d.classes&RawInputEvent.CLASS_DPAD) != 0) { |
| 338 | config.navigation |
| 339 | = Configuration.NAVIGATION_DPAD; |
| 340 | //Log.i("foo", "***** HAVE DPAD!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 341 | } |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | |
Dianne Hackborn | 6af0d50 | 2009-09-28 13:25:46 -0700 | [diff] [blame] | 347 | public int getScancodeState(int code) { |
| 348 | synchronized (mFirst) { |
| 349 | VirtualKey vk = mPressedVirtualKey; |
| 350 | if (vk != null) { |
| 351 | if (vk.scancode == code) { |
| 352 | return 2; |
| 353 | } |
| 354 | } |
| 355 | return nativeGetScancodeState(code); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | public int getScancodeState(int deviceId, int code) { |
| 360 | synchronized (mFirst) { |
| 361 | VirtualKey vk = mPressedVirtualKey; |
| 362 | if (vk != null) { |
| 363 | if (vk.scancode == code) { |
| 364 | return 2; |
| 365 | } |
| 366 | } |
| 367 | return nativeGetScancodeState(deviceId, code); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | public int getKeycodeState(int code) { |
| 372 | synchronized (mFirst) { |
| 373 | VirtualKey vk = mPressedVirtualKey; |
| 374 | if (vk != null) { |
| 375 | if (vk.lastKeycode == code) { |
| 376 | return 2; |
| 377 | } |
| 378 | } |
| 379 | return nativeGetKeycodeState(code); |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | public int getKeycodeState(int deviceId, int code) { |
| 384 | synchronized (mFirst) { |
| 385 | VirtualKey vk = mPressedVirtualKey; |
| 386 | if (vk != null) { |
| 387 | if (vk.lastKeycode == code) { |
| 388 | return 2; |
| 389 | } |
| 390 | } |
| 391 | return nativeGetKeycodeState(deviceId, code); |
| 392 | } |
| 393 | } |
| 394 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 395 | public static native String getDeviceName(int deviceId); |
| 396 | public static native int getDeviceClasses(int deviceId); |
Mike Lockwood | 1d9dfc5 | 2009-07-16 11:11:18 -0400 | [diff] [blame] | 397 | public static native void addExcludedDevice(String deviceName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 398 | public static native boolean getAbsoluteInfo(int deviceId, int axis, |
| 399 | InputDevice.AbsoluteInfo outInfo); |
| 400 | public static native int getSwitchState(int sw); |
| 401 | public static native int getSwitchState(int deviceId, int sw); |
Dianne Hackborn | 6af0d50 | 2009-09-28 13:25:46 -0700 | [diff] [blame] | 402 | public static native int nativeGetScancodeState(int code); |
| 403 | public static native int nativeGetScancodeState(int deviceId, int code); |
| 404 | public static native int nativeGetKeycodeState(int code); |
| 405 | public static native int nativeGetKeycodeState(int deviceId, int code); |
Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 406 | public static native int scancodeToKeycode(int deviceId, int scancode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 407 | public static native boolean hasKeys(int[] keycodes, boolean[] keyExists); |
| 408 | |
| 409 | public static KeyEvent newKeyEvent(InputDevice device, long downTime, |
| 410 | long eventTime, boolean down, int keycode, int repeatCount, |
| 411 | int scancode, int flags) { |
| 412 | return new KeyEvent( |
| 413 | downTime, eventTime, |
| 414 | down ? KeyEvent.ACTION_DOWN : KeyEvent.ACTION_UP, |
| 415 | keycode, repeatCount, |
| 416 | device != null ? device.mMetaKeysState : 0, |
| 417 | device != null ? device.id : -1, scancode, |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 418 | flags | KeyEvent.FLAG_FROM_SYSTEM); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | Thread mThread = new Thread("InputDeviceReader") { |
| 422 | public void run() { |
Dianne Hackborn | bd0a81f | 2009-10-04 13:30:50 -0700 | [diff] [blame] | 423 | if (DEBUG) Log.v(TAG, "InputDeviceReader.run()"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 424 | android.os.Process.setThreadPriority( |
| 425 | android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY); |
| 426 | |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 427 | RawInputEvent ev = new RawInputEvent(); |
| 428 | while (true) { |
| 429 | try { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 430 | InputDevice di; |
| 431 | |
| 432 | // block, doesn't release the monitor |
| 433 | readEvent(ev); |
| 434 | |
| 435 | boolean send = false; |
| 436 | boolean configChanged = false; |
| 437 | |
| 438 | if (false) { |
| 439 | Log.i(TAG, "Input event: dev=0x" |
| 440 | + Integer.toHexString(ev.deviceId) |
| 441 | + " type=0x" + Integer.toHexString(ev.type) |
| 442 | + " scancode=" + ev.scancode |
| 443 | + " keycode=" + ev.keycode |
| 444 | + " value=" + ev.value); |
| 445 | } |
| 446 | |
| 447 | if (ev.type == RawInputEvent.EV_DEVICE_ADDED) { |
| 448 | synchronized (mFirst) { |
| 449 | di = newInputDevice(ev.deviceId); |
Dianne Hackborn | a8f6018 | 2009-09-01 19:01:50 -0700 | [diff] [blame] | 450 | if (di.classes != 0) { |
| 451 | // If this device is some kind of input class, |
| 452 | // we care about it. |
| 453 | mDevices.put(ev.deviceId, di); |
| 454 | if ((di.classes & RawInputEvent.CLASS_TOUCHSCREEN) != 0) { |
| 455 | readVirtualKeys(di.name); |
| 456 | } |
| 457 | // The configuration may have changed because |
| 458 | // of this device. |
| 459 | configChanged = true; |
| 460 | } else { |
| 461 | // We won't do anything with this device. |
| 462 | mIgnoredDevices.put(ev.deviceId, di); |
| 463 | Log.i(TAG, "Ignoring non-input device: id=0x" |
| 464 | + Integer.toHexString(di.id) |
| 465 | + ", name=" + di.name); |
Iliyan Malchev | 75b2aed | 2009-08-06 14:50:57 -0700 | [diff] [blame] | 466 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 467 | } |
| 468 | } else if (ev.type == RawInputEvent.EV_DEVICE_REMOVED) { |
| 469 | synchronized (mFirst) { |
Dianne Hackborn | a8f6018 | 2009-09-01 19:01:50 -0700 | [diff] [blame] | 470 | if (false) { |
| 471 | Log.i(TAG, "Device removed: id=0x" |
| 472 | + Integer.toHexString(ev.deviceId)); |
| 473 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 474 | di = mDevices.get(ev.deviceId); |
| 475 | if (di != null) { |
| 476 | mDevices.delete(ev.deviceId); |
Dianne Hackborn | a8f6018 | 2009-09-01 19:01:50 -0700 | [diff] [blame] | 477 | // The configuration may have changed because |
| 478 | // of this device. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 479 | configChanged = true; |
Dianne Hackborn | a8f6018 | 2009-09-01 19:01:50 -0700 | [diff] [blame] | 480 | } else if ((di=mIgnoredDevices.get(ev.deviceId)) != null) { |
| 481 | mIgnoredDevices.remove(ev.deviceId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 482 | } else { |
Dianne Hackborn | a8f6018 | 2009-09-01 19:01:50 -0700 | [diff] [blame] | 483 | Log.w(TAG, "Removing bad device id: " |
| 484 | + Integer.toHexString(ev.deviceId)); |
| 485 | continue; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 486 | } |
| 487 | } |
| 488 | } else { |
| 489 | di = getInputDevice(ev.deviceId); |
Dianne Hackborn | a8f6018 | 2009-09-01 19:01:50 -0700 | [diff] [blame] | 490 | if (di == null) { |
| 491 | // This may be some junk from an ignored device. |
| 492 | continue; |
| 493 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 494 | |
| 495 | // first crack at it |
| 496 | send = preprocessEvent(di, ev); |
| 497 | |
| 498 | if (ev.type == RawInputEvent.EV_KEY) { |
| 499 | di.mMetaKeysState = makeMetaState(ev.keycode, |
| 500 | ev.value != 0, di.mMetaKeysState); |
| 501 | mHaveGlobalMetaState = false; |
| 502 | } |
| 503 | } |
| 504 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 505 | if (configChanged) { |
| 506 | synchronized (mFirst) { |
Michael Chan | 53071d6 | 2009-05-13 17:29:48 -0700 | [diff] [blame] | 507 | addLocked(di, System.nanoTime(), 0, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 508 | RawInputEvent.CLASS_CONFIGURATION_CHANGED, |
| 509 | null); |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | if (!send) { |
| 514 | continue; |
| 515 | } |
| 516 | |
| 517 | synchronized (mFirst) { |
| 518 | // NOTE: The event timebase absolutely must be the same |
| 519 | // timebase as SystemClock.uptimeMillis(). |
| 520 | //curTime = gotOne ? ev.when : SystemClock.uptimeMillis(); |
| 521 | final long curTime = SystemClock.uptimeMillis(); |
Michael Chan | 53071d6 | 2009-05-13 17:29:48 -0700 | [diff] [blame] | 522 | final long curTimeNano = System.nanoTime(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 523 | //Log.i(TAG, "curTime=" + curTime + ", systemClock=" + SystemClock.uptimeMillis()); |
| 524 | |
| 525 | final int classes = di.classes; |
| 526 | final int type = ev.type; |
| 527 | final int scancode = ev.scancode; |
| 528 | send = false; |
| 529 | |
| 530 | // Is it a key event? |
| 531 | if (type == RawInputEvent.EV_KEY && |
| 532 | (classes&RawInputEvent.CLASS_KEYBOARD) != 0 && |
| 533 | (scancode < RawInputEvent.BTN_FIRST || |
| 534 | scancode > RawInputEvent.BTN_LAST)) { |
| 535 | boolean down; |
| 536 | if (ev.value != 0) { |
| 537 | down = true; |
Dianne Hackborn | 9822d2b | 2009-07-20 17:33:15 -0700 | [diff] [blame] | 538 | di.mKeyDownTime = curTime; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 539 | } else { |
| 540 | down = false; |
| 541 | } |
| 542 | int keycode = rotateKeyCodeLocked(ev.keycode); |
Michael Chan | 53071d6 | 2009-05-13 17:29:48 -0700 | [diff] [blame] | 543 | addLocked(di, curTimeNano, ev.flags, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 544 | RawInputEvent.CLASS_KEYBOARD, |
Dianne Hackborn | 9822d2b | 2009-07-20 17:33:15 -0700 | [diff] [blame] | 545 | newKeyEvent(di, di.mKeyDownTime, curTime, down, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 546 | keycode, 0, scancode, |
| 547 | ((ev.flags & WindowManagerPolicy.FLAG_WOKE_HERE) != 0) |
| 548 | ? KeyEvent.FLAG_WOKE_HERE : 0)); |
Dianne Hackborn | 1411d1c | 2009-10-12 23:21:18 -0700 | [diff] [blame] | 549 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 550 | } else if (ev.type == RawInputEvent.EV_KEY) { |
Dianne Hackborn | 1411d1c | 2009-10-12 23:21:18 -0700 | [diff] [blame] | 551 | // Single touch protocol: touch going down or up. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 552 | if (ev.scancode == RawInputEvent.BTN_TOUCH && |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 553 | (classes&(RawInputEvent.CLASS_TOUCHSCREEN |
| 554 | |RawInputEvent.CLASS_TOUCHSCREEN_MT)) |
| 555 | == RawInputEvent.CLASS_TOUCHSCREEN) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 556 | di.mAbs.changed = true; |
Dianne Hackborn | 9822d2b | 2009-07-20 17:33:15 -0700 | [diff] [blame] | 557 | di.mAbs.mDown[0] = ev.value != 0; |
Dianne Hackborn | 1411d1c | 2009-10-12 23:21:18 -0700 | [diff] [blame] | 558 | |
| 559 | // Trackball (mouse) protocol: press down or up. |
Dianne Hackborn | 9822d2b | 2009-07-20 17:33:15 -0700 | [diff] [blame] | 560 | } else if (ev.scancode == RawInputEvent.BTN_MOUSE && |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 561 | (classes&RawInputEvent.CLASS_TRACKBALL) != 0) { |
| 562 | di.mRel.changed = true; |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 563 | di.mRel.mNextNumPointers = ev.value != 0 ? 1 : 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 564 | send = true; |
| 565 | } |
| 566 | |
Dianne Hackborn | 1411d1c | 2009-10-12 23:21:18 -0700 | [diff] [blame] | 567 | // Process position events from multitouch protocol. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 568 | } else if (ev.type == RawInputEvent.EV_ABS && |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 569 | (classes&RawInputEvent.CLASS_TOUCHSCREEN_MT) != 0) { |
| 570 | if (ev.scancode == RawInputEvent.ABS_MT_TOUCH_MAJOR) { |
| 571 | di.mAbs.changed = true; |
| 572 | di.mAbs.mNextData[di.mAbs.mAddingPointerOffset |
| 573 | + MotionEvent.SAMPLE_PRESSURE] = ev.value; |
| 574 | } else if (ev.scancode == RawInputEvent.ABS_MT_POSITION_X) { |
| 575 | di.mAbs.changed = true; |
| 576 | di.mAbs.mNextData[di.mAbs.mAddingPointerOffset |
| 577 | + MotionEvent.SAMPLE_X] = ev.value; |
| 578 | if (DEBUG_POINTERS) Log.v(TAG, "MT @" |
| 579 | + di.mAbs.mAddingPointerOffset |
| 580 | + " X:" + ev.value); |
| 581 | } else if (ev.scancode == RawInputEvent.ABS_MT_POSITION_Y) { |
| 582 | di.mAbs.changed = true; |
| 583 | di.mAbs.mNextData[di.mAbs.mAddingPointerOffset |
| 584 | + MotionEvent.SAMPLE_Y] = ev.value; |
| 585 | if (DEBUG_POINTERS) Log.v(TAG, "MT @" |
| 586 | + di.mAbs.mAddingPointerOffset |
| 587 | + " Y:" + ev.value); |
| 588 | } else if (ev.scancode == RawInputEvent.ABS_MT_WIDTH_MAJOR) { |
| 589 | di.mAbs.changed = true; |
| 590 | di.mAbs.mNextData[di.mAbs.mAddingPointerOffset |
| 591 | + MotionEvent.SAMPLE_SIZE] = ev.value; |
| 592 | } |
Dianne Hackborn | 1411d1c | 2009-10-12 23:21:18 -0700 | [diff] [blame] | 593 | |
| 594 | // Process position events from single touch protocol. |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 595 | } else if (ev.type == RawInputEvent.EV_ABS && |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 596 | (classes&RawInputEvent.CLASS_TOUCHSCREEN) != 0) { |
| 597 | if (ev.scancode == RawInputEvent.ABS_X) { |
| 598 | di.mAbs.changed = true; |
Dianne Hackborn | 2a2b3443 | 2009-08-12 17:13:55 -0700 | [diff] [blame] | 599 | di.curTouchVals[MotionEvent.SAMPLE_X] = ev.value; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 600 | } else if (ev.scancode == RawInputEvent.ABS_Y) { |
| 601 | di.mAbs.changed = true; |
Dianne Hackborn | 2a2b3443 | 2009-08-12 17:13:55 -0700 | [diff] [blame] | 602 | di.curTouchVals[MotionEvent.SAMPLE_Y] = ev.value; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 603 | } else if (ev.scancode == RawInputEvent.ABS_PRESSURE) { |
| 604 | di.mAbs.changed = true; |
Dianne Hackborn | 2a2b3443 | 2009-08-12 17:13:55 -0700 | [diff] [blame] | 605 | di.curTouchVals[MotionEvent.SAMPLE_PRESSURE] = ev.value; |
| 606 | di.curTouchVals[MotionEvent.NUM_SAMPLE_DATA |
Dianne Hackborn | 9822d2b | 2009-07-20 17:33:15 -0700 | [diff] [blame] | 607 | + MotionEvent.SAMPLE_PRESSURE] = ev.value; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 608 | } else if (ev.scancode == RawInputEvent.ABS_TOOL_WIDTH) { |
| 609 | di.mAbs.changed = true; |
Dianne Hackborn | 2a2b3443 | 2009-08-12 17:13:55 -0700 | [diff] [blame] | 610 | di.curTouchVals[MotionEvent.SAMPLE_SIZE] = ev.value; |
| 611 | di.curTouchVals[MotionEvent.NUM_SAMPLE_DATA |
Dianne Hackborn | 9822d2b | 2009-07-20 17:33:15 -0700 | [diff] [blame] | 612 | + MotionEvent.SAMPLE_SIZE] = ev.value; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 613 | } |
| 614 | |
Dianne Hackborn | 1411d1c | 2009-10-12 23:21:18 -0700 | [diff] [blame] | 615 | // Process movement events from trackball (mouse) protocol. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 616 | } else if (ev.type == RawInputEvent.EV_REL && |
| 617 | (classes&RawInputEvent.CLASS_TRACKBALL) != 0) { |
| 618 | // Add this relative movement into our totals. |
| 619 | if (ev.scancode == RawInputEvent.REL_X) { |
| 620 | di.mRel.changed = true; |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 621 | di.mRel.mNextData[MotionEvent.SAMPLE_X] += ev.value; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 622 | } else if (ev.scancode == RawInputEvent.REL_Y) { |
| 623 | di.mRel.changed = true; |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 624 | di.mRel.mNextData[MotionEvent.SAMPLE_Y] += ev.value; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 625 | } |
| 626 | } |
| 627 | |
Dianne Hackborn | 1411d1c | 2009-10-12 23:21:18 -0700 | [diff] [blame] | 628 | // Handle multitouch protocol sync: tells us that the |
| 629 | // driver has returned all data for -one- of the pointers |
| 630 | // that is currently down. |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 631 | if (ev.type == RawInputEvent.EV_SYN |
| 632 | && ev.scancode == RawInputEvent.SYN_MT_REPORT |
| 633 | && di.mAbs != null) { |
| 634 | di.mAbs.changed = true; |
| 635 | if (di.mAbs.mNextData[MotionEvent.SAMPLE_PRESSURE] > 0) { |
| 636 | // If the value is <= 0, the pointer is not |
| 637 | // down, so keep it in the count. |
| 638 | |
| 639 | if (di.mAbs.mNextData[di.mAbs.mAddingPointerOffset |
| 640 | + MotionEvent.SAMPLE_PRESSURE] != 0) { |
| 641 | final int num = di.mAbs.mNextNumPointers+1; |
| 642 | di.mAbs.mNextNumPointers = num; |
| 643 | if (DEBUG_POINTERS) Log.v(TAG, |
| 644 | "MT_REPORT: now have " + num + " pointers"); |
| 645 | final int newOffset = (num <= InputDevice.MAX_POINTERS) |
| 646 | ? (num * MotionEvent.NUM_SAMPLE_DATA) |
| 647 | : (InputDevice.MAX_POINTERS * |
| 648 | MotionEvent.NUM_SAMPLE_DATA); |
| 649 | di.mAbs.mAddingPointerOffset = newOffset; |
| 650 | di.mAbs.mNextData[newOffset |
| 651 | + MotionEvent.SAMPLE_PRESSURE] = 0; |
| 652 | } else { |
| 653 | if (DEBUG_POINTERS) Log.v(TAG, "MT_REPORT: no pointer"); |
| 654 | } |
| 655 | } |
Dianne Hackborn | 1411d1c | 2009-10-12 23:21:18 -0700 | [diff] [blame] | 656 | |
| 657 | // Handle general event sync: all data for the current |
| 658 | // event update has been delivered. |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 659 | } else if (send || (ev.type == RawInputEvent.EV_SYN |
| 660 | && ev.scancode == RawInputEvent.SYN_REPORT)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 661 | if (mDisplay != null) { |
| 662 | if (!mHaveGlobalMetaState) { |
| 663 | computeGlobalMetaStateLocked(); |
| 664 | } |
| 665 | |
| 666 | MotionEvent me; |
Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 667 | |
| 668 | InputDevice.MotionState ms = di.mAbs; |
| 669 | if (ms.changed) { |
| 670 | ms.changed = false; |
| 671 | |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 672 | if ((classes&(RawInputEvent.CLASS_TOUCHSCREEN |
| 673 | |RawInputEvent.CLASS_TOUCHSCREEN_MT)) |
| 674 | == RawInputEvent.CLASS_TOUCHSCREEN) { |
| 675 | ms.mNextNumPointers = 0; |
Dianne Hackborn | 2a2b3443 | 2009-08-12 17:13:55 -0700 | [diff] [blame] | 676 | if (ms.mDown[0]) { |
| 677 | System.arraycopy(di.curTouchVals, 0, |
| 678 | ms.mNextData, 0, |
| 679 | MotionEvent.NUM_SAMPLE_DATA); |
| 680 | ms.mNextNumPointers++; |
| 681 | } |
Dianne Hackborn | 1411d1c | 2009-10-12 23:21:18 -0700 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | if (BAD_TOUCH_HACK) { |
| 685 | ms.dropBadPoint(di); |
Dianne Hackborn | ddca3ee | 2009-07-23 19:01:31 -0700 | [diff] [blame] | 686 | } |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 687 | |
| 688 | boolean doMotion = !monitorVirtualKey(di, |
| 689 | ev, curTime, curTimeNano); |
| 690 | |
| 691 | if (doMotion && ms.mNextNumPointers > 0 |
| 692 | && ms.mLastNumPointers == 0) { |
| 693 | doMotion = !generateVirtualKeyDown(di, |
| 694 | ev, curTime, curTimeNano); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 695 | } |
Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 696 | |
| 697 | if (doMotion) { |
Dianne Hackborn | 9822d2b | 2009-07-20 17:33:15 -0700 | [diff] [blame] | 698 | // XXX Need to be able to generate |
| 699 | // multiple events here, for example |
| 700 | // if two fingers change up/down state |
| 701 | // at the same time. |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 702 | do { |
| 703 | me = ms.generateAbsMotion(di, curTime, |
| 704 | curTimeNano, mDisplay, |
| 705 | mOrientation, mGlobalMetaState); |
| 706 | if (false) Log.v(TAG, "Absolute: x=" |
| 707 | + di.mAbs.mNextData[MotionEvent.SAMPLE_X] |
| 708 | + " y=" |
| 709 | + di.mAbs.mNextData[MotionEvent.SAMPLE_Y] |
| 710 | + " ev=" + me); |
| 711 | if (me != null) { |
| 712 | if (WindowManagerPolicy.WATCH_POINTER) { |
| 713 | Log.i(TAG, "Enqueueing: " + me); |
| 714 | } |
| 715 | addLocked(di, curTimeNano, ev.flags, |
| 716 | RawInputEvent.CLASS_TOUCHSCREEN, me); |
Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 717 | } |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 718 | } while (ms.hasMore()); |
Dianne Hackborn | 1411d1c | 2009-10-12 23:21:18 -0700 | [diff] [blame] | 719 | } else { |
| 720 | // We are consuming movement in the |
| 721 | // virtual key area... but still |
| 722 | // propagate this to the previous |
| 723 | // data for comparisons. |
Dianne Hackborn | f2ddfb1 | 2009-10-13 22:43:33 -0700 | [diff] [blame^] | 724 | int num = ms.mNextNumPointers; |
| 725 | if (num > InputDevice.MAX_POINTERS) { |
| 726 | num = InputDevice.MAX_POINTERS; |
| 727 | } |
Dianne Hackborn | 1411d1c | 2009-10-12 23:21:18 -0700 | [diff] [blame] | 728 | System.arraycopy(ms.mNextData, 0, |
| 729 | ms.mLastData, 0, |
Dianne Hackborn | f2ddfb1 | 2009-10-13 22:43:33 -0700 | [diff] [blame^] | 730 | num * MotionEvent.NUM_SAMPLE_DATA); |
| 731 | ms.mLastNumPointers = num; |
Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 732 | } |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 733 | |
| 734 | ms.finish(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 735 | } |
Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 736 | |
| 737 | ms = di.mRel; |
| 738 | if (ms.changed) { |
| 739 | ms.changed = false; |
| 740 | |
Dianne Hackborn | 9822d2b | 2009-07-20 17:33:15 -0700 | [diff] [blame] | 741 | me = ms.generateRelMotion(di, curTime, |
| 742 | curTimeNano, |
Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 743 | mOrientation, mGlobalMetaState); |
Dianne Hackborn | 9822d2b | 2009-07-20 17:33:15 -0700 | [diff] [blame] | 744 | if (false) Log.v(TAG, "Relative: x=" |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 745 | + di.mRel.mNextData[MotionEvent.SAMPLE_X] |
Dianne Hackborn | 9822d2b | 2009-07-20 17:33:15 -0700 | [diff] [blame] | 746 | + " y=" |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 747 | + di.mRel.mNextData[MotionEvent.SAMPLE_Y] |
Dianne Hackborn | 9822d2b | 2009-07-20 17:33:15 -0700 | [diff] [blame] | 748 | + " ev=" + me); |
Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 749 | if (me != null) { |
| 750 | addLocked(di, curTimeNano, ev.flags, |
| 751 | RawInputEvent.CLASS_TRACKBALL, me); |
| 752 | } |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 753 | |
| 754 | ms.finish(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 755 | } |
| 756 | } |
| 757 | } |
| 758 | } |
Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 759 | |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 760 | } catch (RuntimeException exc) { |
| 761 | Log.e(TAG, "InputReaderThread uncaught exception", exc); |
| 762 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 763 | } |
| 764 | } |
| 765 | }; |
| 766 | |
Dianne Hackborn | ddca3ee | 2009-07-23 19:01:31 -0700 | [diff] [blame] | 767 | private boolean isInsideDisplay(InputDevice dev) { |
Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 768 | final InputDevice.AbsoluteInfo absx = dev.absX; |
| 769 | final InputDevice.AbsoluteInfo absy = dev.absY; |
| 770 | final InputDevice.MotionState absm = dev.mAbs; |
| 771 | if (absx == null || absy == null || absm == null) { |
Dianne Hackborn | ddca3ee | 2009-07-23 19:01:31 -0700 | [diff] [blame] | 772 | return true; |
Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 773 | } |
| 774 | |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 775 | if (absm.mNextData[MotionEvent.SAMPLE_X] >= absx.minValue |
| 776 | && absm.mNextData[MotionEvent.SAMPLE_X] <= absx.maxValue |
| 777 | && absm.mNextData[MotionEvent.SAMPLE_Y] >= absy.minValue |
| 778 | && absm.mNextData[MotionEvent.SAMPLE_Y] <= absy.maxValue) { |
Dianne Hackborn | 9822d2b | 2009-07-20 17:33:15 -0700 | [diff] [blame] | 779 | if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "Input (" |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 780 | + absm.mNextData[MotionEvent.SAMPLE_X] |
| 781 | + "," + absm.mNextData[MotionEvent.SAMPLE_Y] |
Dianne Hackborn | 9822d2b | 2009-07-20 17:33:15 -0700 | [diff] [blame] | 782 | + ") inside of display"); |
Dianne Hackborn | ddca3ee | 2009-07-23 19:01:31 -0700 | [diff] [blame] | 783 | return true; |
| 784 | } |
| 785 | |
| 786 | return false; |
| 787 | } |
| 788 | |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 789 | private VirtualKey findVirtualKey(InputDevice dev) { |
Dianne Hackborn | ddca3ee | 2009-07-23 19:01:31 -0700 | [diff] [blame] | 790 | final int N = mVirtualKeys.size(); |
| 791 | if (N <= 0) { |
Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 792 | return null; |
| 793 | } |
| 794 | |
Dianne Hackborn | ddca3ee | 2009-07-23 19:01:31 -0700 | [diff] [blame] | 795 | final InputDevice.MotionState absm = dev.mAbs; |
Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 796 | for (int i=0; i<N; i++) { |
| 797 | VirtualKey sb = mVirtualKeys.get(i); |
| 798 | sb.computeHitRect(dev, mDisplayWidth, mDisplayHeight); |
Dianne Hackborn | 9822d2b | 2009-07-20 17:33:15 -0700 | [diff] [blame] | 799 | if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "Hit test (" |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 800 | + absm.mNextData[MotionEvent.SAMPLE_X] + "," |
| 801 | + absm.mNextData[MotionEvent.SAMPLE_Y] + ") in code " |
Dianne Hackborn | 9822d2b | 2009-07-20 17:33:15 -0700 | [diff] [blame] | 802 | + sb.scancode + " - (" + sb.hitLeft |
Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 803 | + "," + sb.hitTop + ")-(" + sb.hitRight + "," |
| 804 | + sb.hitBottom + ")"); |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 805 | if (sb.checkHit(absm.mNextData[MotionEvent.SAMPLE_X], |
| 806 | absm.mNextData[MotionEvent.SAMPLE_Y])) { |
Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 807 | if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "Hit!"); |
| 808 | return sb; |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | return null; |
| 813 | } |
| 814 | |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 815 | private boolean generateVirtualKeyDown(InputDevice di, RawInputEvent ev, |
| 816 | long curTime, long curTimeNano) { |
| 817 | if (isInsideDisplay(di)) { |
| 818 | // Didn't consume event. |
| 819 | return false; |
| 820 | } |
| 821 | |
| 822 | |
| 823 | VirtualKey vk = findVirtualKey(di); |
| 824 | if (vk != null) { |
| 825 | final InputDevice.MotionState ms = di.mAbs; |
| 826 | mPressedVirtualKey = vk; |
| 827 | vk.lastKeycode = scancodeToKeycode(di.id, vk.scancode); |
| 828 | ms.mLastNumPointers = ms.mNextNumPointers; |
| 829 | di.mKeyDownTime = curTime; |
| 830 | if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, |
| 831 | "Generate key down for: " + vk.scancode |
| 832 | + " (keycode=" + vk.lastKeycode + ")"); |
| 833 | KeyEvent event = newKeyEvent(di, di.mKeyDownTime, curTime, true, |
| 834 | vk.lastKeycode, 0, vk.scancode, |
| 835 | KeyEvent.FLAG_VIRTUAL_HARD_KEY); |
| 836 | mHapticFeedbackCallback.virtualKeyFeedback(event); |
| 837 | addLocked(di, curTimeNano, ev.flags, RawInputEvent.CLASS_KEYBOARD, |
| 838 | event); |
| 839 | } |
| 840 | |
| 841 | // We always consume the event, even if we didn't |
| 842 | // generate a key event. There are two reasons for |
| 843 | // this: to avoid spurious touches when holding |
| 844 | // the edges of the device near the touchscreen, |
| 845 | // and to avoid reporting events if there are virtual |
| 846 | // keys on the touchscreen outside of the display |
| 847 | // area. |
| 848 | // Note that for all of this we are only looking at the |
| 849 | // first pointer, since what we are handling here is the |
| 850 | // first pointer going down, and this is the coordinate |
| 851 | // that will be used to dispatch the event. |
| 852 | if (false) { |
| 853 | final InputDevice.AbsoluteInfo absx = di.absX; |
| 854 | final InputDevice.AbsoluteInfo absy = di.absY; |
| 855 | final InputDevice.MotionState absm = di.mAbs; |
| 856 | Log.v(TAG, "Rejecting (" |
| 857 | + absm.mNextData[MotionEvent.SAMPLE_X] + "," |
| 858 | + absm.mNextData[MotionEvent.SAMPLE_Y] + "): outside of (" |
| 859 | + absx.minValue + "," + absy.minValue |
| 860 | + ")-(" + absx.maxValue + "," |
| 861 | + absx.maxValue + ")"); |
| 862 | } |
| 863 | return true; |
| 864 | } |
| 865 | |
| 866 | private boolean monitorVirtualKey(InputDevice di, RawInputEvent ev, |
| 867 | long curTime, long curTimeNano) { |
| 868 | VirtualKey vk = mPressedVirtualKey; |
| 869 | if (vk == null) { |
| 870 | return false; |
| 871 | } |
| 872 | |
| 873 | final InputDevice.MotionState ms = di.mAbs; |
| 874 | if (ms.mNextNumPointers <= 0) { |
| 875 | mPressedVirtualKey = null; |
| 876 | ms.mLastNumPointers = 0; |
| 877 | if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "Generate key up for: " + vk.scancode); |
| 878 | KeyEvent event = newKeyEvent(di, di.mKeyDownTime, curTime, false, |
| 879 | vk.lastKeycode, 0, vk.scancode, |
| 880 | KeyEvent.FLAG_VIRTUAL_HARD_KEY); |
| 881 | mHapticFeedbackCallback.virtualKeyFeedback(event); |
| 882 | addLocked(di, curTimeNano, ev.flags, RawInputEvent.CLASS_KEYBOARD, |
| 883 | event); |
| 884 | return true; |
| 885 | |
| 886 | } else if (isInsideDisplay(di)) { |
| 887 | // Whoops the pointer has moved into |
| 888 | // the display area! Cancel the |
| 889 | // virtual key and start a pointer |
| 890 | // motion. |
| 891 | mPressedVirtualKey = null; |
| 892 | if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "Cancel key up for: " + vk.scancode); |
| 893 | KeyEvent event = newKeyEvent(di, di.mKeyDownTime, curTime, false, |
| 894 | vk.lastKeycode, 0, vk.scancode, |
| 895 | KeyEvent.FLAG_CANCELED | KeyEvent.FLAG_VIRTUAL_HARD_KEY); |
| 896 | mHapticFeedbackCallback.virtualKeyFeedback(event); |
| 897 | addLocked(di, curTimeNano, ev.flags, RawInputEvent.CLASS_KEYBOARD, |
| 898 | event); |
| 899 | ms.mLastNumPointers = 0; |
| 900 | return false; |
| 901 | } |
| 902 | |
| 903 | return true; |
| 904 | } |
| 905 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 906 | /** |
| 907 | * Returns a new meta state for the given keys and old state. |
| 908 | */ |
| 909 | private static final int makeMetaState(int keycode, boolean down, int old) { |
| 910 | int mask; |
| 911 | switch (keycode) { |
| 912 | case KeyEvent.KEYCODE_ALT_LEFT: |
| 913 | mask = KeyEvent.META_ALT_LEFT_ON; |
| 914 | break; |
| 915 | case KeyEvent.KEYCODE_ALT_RIGHT: |
| 916 | mask = KeyEvent.META_ALT_RIGHT_ON; |
| 917 | break; |
| 918 | case KeyEvent.KEYCODE_SHIFT_LEFT: |
| 919 | mask = KeyEvent.META_SHIFT_LEFT_ON; |
| 920 | break; |
| 921 | case KeyEvent.KEYCODE_SHIFT_RIGHT: |
| 922 | mask = KeyEvent.META_SHIFT_RIGHT_ON; |
| 923 | break; |
| 924 | case KeyEvent.KEYCODE_SYM: |
| 925 | mask = KeyEvent.META_SYM_ON; |
| 926 | break; |
| 927 | default: |
| 928 | return old; |
| 929 | } |
| 930 | int result = ~(KeyEvent.META_ALT_ON | KeyEvent.META_SHIFT_ON) |
| 931 | & (down ? (old | mask) : (old & ~mask)); |
| 932 | if (0 != (result & (KeyEvent.META_ALT_LEFT_ON | KeyEvent.META_ALT_RIGHT_ON))) { |
| 933 | result |= KeyEvent.META_ALT_ON; |
| 934 | } |
| 935 | if (0 != (result & (KeyEvent.META_SHIFT_LEFT_ON | KeyEvent.META_SHIFT_RIGHT_ON))) { |
| 936 | result |= KeyEvent.META_SHIFT_ON; |
| 937 | } |
| 938 | return result; |
| 939 | } |
| 940 | |
| 941 | private void computeGlobalMetaStateLocked() { |
| 942 | int i = mDevices.size(); |
| 943 | mGlobalMetaState = 0; |
| 944 | while ((--i) >= 0) { |
| 945 | mGlobalMetaState |= mDevices.valueAt(i).mMetaKeysState; |
| 946 | } |
| 947 | mHaveGlobalMetaState = true; |
| 948 | } |
| 949 | |
| 950 | /* |
| 951 | * Return true if you want the event to get passed on to the |
| 952 | * rest of the system, and false if you've handled it and want |
| 953 | * it dropped. |
| 954 | */ |
| 955 | abstract boolean preprocessEvent(InputDevice device, RawInputEvent event); |
| 956 | |
| 957 | InputDevice getInputDevice(int deviceId) { |
| 958 | synchronized (mFirst) { |
| 959 | return getInputDeviceLocked(deviceId); |
| 960 | } |
| 961 | } |
| 962 | |
| 963 | private InputDevice getInputDeviceLocked(int deviceId) { |
| 964 | return mDevices.get(deviceId); |
| 965 | } |
| 966 | |
| 967 | public void setOrientation(int orientation) { |
| 968 | synchronized(mFirst) { |
| 969 | mOrientation = orientation; |
| 970 | switch (orientation) { |
| 971 | case Surface.ROTATION_90: |
| 972 | mKeyRotationMap = KEY_90_MAP; |
| 973 | break; |
| 974 | case Surface.ROTATION_180: |
| 975 | mKeyRotationMap = KEY_180_MAP; |
| 976 | break; |
| 977 | case Surface.ROTATION_270: |
| 978 | mKeyRotationMap = KEY_270_MAP; |
| 979 | break; |
| 980 | default: |
| 981 | mKeyRotationMap = null; |
| 982 | break; |
| 983 | } |
| 984 | } |
| 985 | } |
| 986 | |
| 987 | public int rotateKeyCode(int keyCode) { |
| 988 | synchronized(mFirst) { |
| 989 | return rotateKeyCodeLocked(keyCode); |
| 990 | } |
| 991 | } |
| 992 | |
| 993 | private int rotateKeyCodeLocked(int keyCode) { |
| 994 | int[] map = mKeyRotationMap; |
| 995 | if (map != null) { |
| 996 | final int N = map.length; |
| 997 | for (int i=0; i<N; i+=2) { |
| 998 | if (map[i] == keyCode) { |
| 999 | return map[i+1]; |
| 1000 | } |
| 1001 | } |
| 1002 | } |
| 1003 | return keyCode; |
| 1004 | } |
| 1005 | |
| 1006 | boolean hasEvents() { |
| 1007 | synchronized (mFirst) { |
| 1008 | return mFirst.next != mLast; |
| 1009 | } |
| 1010 | } |
| 1011 | |
| 1012 | /* |
| 1013 | * returns true if we returned an event, and false if we timed out |
| 1014 | */ |
| 1015 | QueuedEvent getEvent(long timeoutMS) { |
| 1016 | long begin = SystemClock.uptimeMillis(); |
| 1017 | final long end = begin+timeoutMS; |
| 1018 | long now = begin; |
| 1019 | synchronized (mFirst) { |
| 1020 | while (mFirst.next == mLast && end > now) { |
| 1021 | try { |
| 1022 | mWakeLock.release(); |
| 1023 | mFirst.wait(end-now); |
| 1024 | } |
| 1025 | catch (InterruptedException e) { |
| 1026 | } |
| 1027 | now = SystemClock.uptimeMillis(); |
| 1028 | if (begin > now) { |
| 1029 | begin = now; |
| 1030 | } |
| 1031 | } |
| 1032 | if (mFirst.next == mLast) { |
| 1033 | return null; |
| 1034 | } |
| 1035 | QueuedEvent p = mFirst.next; |
| 1036 | mFirst.next = p.next; |
| 1037 | mFirst.next.prev = mFirst; |
| 1038 | p.inQueue = false; |
| 1039 | return p; |
| 1040 | } |
| 1041 | } |
| 1042 | |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 1043 | /** |
| 1044 | * Return true if the queue has an up event pending that corresponds |
| 1045 | * to the same key as the given key event. |
| 1046 | */ |
| 1047 | boolean hasKeyUpEvent(KeyEvent origEvent) { |
| 1048 | synchronized (mFirst) { |
| 1049 | final int keyCode = origEvent.getKeyCode(); |
| 1050 | QueuedEvent cur = mLast.prev; |
| 1051 | while (cur.prev != null) { |
| 1052 | if (cur.classType == RawInputEvent.CLASS_KEYBOARD) { |
| 1053 | KeyEvent ke = (KeyEvent)cur.event; |
| 1054 | if (ke.getAction() == KeyEvent.ACTION_UP |
| 1055 | && ke.getKeyCode() == keyCode) { |
| 1056 | return true; |
| 1057 | } |
| 1058 | } |
| 1059 | cur = cur.prev; |
| 1060 | } |
| 1061 | } |
| 1062 | |
| 1063 | return false; |
| 1064 | } |
| 1065 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1066 | void recycleEvent(QueuedEvent ev) { |
| 1067 | synchronized (mFirst) { |
| 1068 | //Log.i(TAG, "Recycle event: " + ev); |
| 1069 | if (ev.event == ev.inputDevice.mAbs.currentMove) { |
| 1070 | ev.inputDevice.mAbs.currentMove = null; |
| 1071 | } |
| 1072 | if (ev.event == ev.inputDevice.mRel.currentMove) { |
| 1073 | if (false) Log.i(TAG, "Detach rel " + ev.event); |
| 1074 | ev.inputDevice.mRel.currentMove = null; |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 1075 | ev.inputDevice.mRel.mNextData[MotionEvent.SAMPLE_X] = 0; |
| 1076 | ev.inputDevice.mRel.mNextData[MotionEvent.SAMPLE_Y] = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1077 | } |
| 1078 | recycleLocked(ev); |
| 1079 | } |
| 1080 | } |
| 1081 | |
| 1082 | void filterQueue(FilterCallback cb) { |
| 1083 | synchronized (mFirst) { |
| 1084 | QueuedEvent cur = mLast.prev; |
| 1085 | while (cur.prev != null) { |
| 1086 | switch (cb.filterEvent(cur)) { |
| 1087 | case FILTER_REMOVE: |
| 1088 | cur.prev.next = cur.next; |
| 1089 | cur.next.prev = cur.prev; |
| 1090 | break; |
| 1091 | case FILTER_ABORT: |
| 1092 | return; |
| 1093 | } |
| 1094 | cur = cur.prev; |
| 1095 | } |
| 1096 | } |
| 1097 | } |
| 1098 | |
Michael Chan | 53071d6 | 2009-05-13 17:29:48 -0700 | [diff] [blame] | 1099 | private QueuedEvent obtainLocked(InputDevice device, long whenNano, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1100 | int flags, int classType, Object event) { |
| 1101 | QueuedEvent ev; |
| 1102 | if (mCacheCount == 0) { |
| 1103 | ev = new QueuedEvent(); |
| 1104 | } else { |
| 1105 | ev = mCache; |
| 1106 | ev.inQueue = false; |
| 1107 | mCache = ev.next; |
| 1108 | mCacheCount--; |
| 1109 | } |
| 1110 | ev.inputDevice = device; |
Michael Chan | 53071d6 | 2009-05-13 17:29:48 -0700 | [diff] [blame] | 1111 | ev.whenNano = whenNano; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1112 | ev.flags = flags; |
| 1113 | ev.classType = classType; |
| 1114 | ev.event = event; |
| 1115 | return ev; |
| 1116 | } |
| 1117 | |
| 1118 | private void recycleLocked(QueuedEvent ev) { |
| 1119 | if (ev.inQueue) { |
| 1120 | throw new RuntimeException("Event already in queue!"); |
| 1121 | } |
| 1122 | if (mCacheCount < 10) { |
| 1123 | mCacheCount++; |
| 1124 | ev.next = mCache; |
| 1125 | mCache = ev; |
| 1126 | ev.inQueue = true; |
| 1127 | } |
| 1128 | } |
| 1129 | |
Michael Chan | 53071d6 | 2009-05-13 17:29:48 -0700 | [diff] [blame] | 1130 | private void addLocked(InputDevice device, long whenNano, int flags, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1131 | int classType, Object event) { |
| 1132 | boolean poke = mFirst.next == mLast; |
| 1133 | |
Michael Chan | 53071d6 | 2009-05-13 17:29:48 -0700 | [diff] [blame] | 1134 | QueuedEvent ev = obtainLocked(device, whenNano, flags, classType, event); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1135 | QueuedEvent p = mLast.prev; |
Michael Chan | 53071d6 | 2009-05-13 17:29:48 -0700 | [diff] [blame] | 1136 | while (p != mFirst && ev.whenNano < p.whenNano) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1137 | p = p.prev; |
| 1138 | } |
| 1139 | |
| 1140 | ev.next = p.next; |
| 1141 | ev.prev = p; |
| 1142 | p.next = ev; |
| 1143 | ev.next.prev = ev; |
| 1144 | ev.inQueue = true; |
| 1145 | |
| 1146 | if (poke) { |
Michael Chan | 53071d6 | 2009-05-13 17:29:48 -0700 | [diff] [blame] | 1147 | long time; |
| 1148 | if (MEASURE_LATENCY) { |
| 1149 | time = System.nanoTime(); |
| 1150 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1151 | mFirst.notify(); |
| 1152 | mWakeLock.acquire(); |
Michael Chan | 53071d6 | 2009-05-13 17:29:48 -0700 | [diff] [blame] | 1153 | if (MEASURE_LATENCY) { |
| 1154 | lt.sample("1 addLocked-queued event ", System.nanoTime() - time); |
| 1155 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1156 | } |
| 1157 | } |
| 1158 | |
| 1159 | private InputDevice newInputDevice(int deviceId) { |
| 1160 | int classes = getDeviceClasses(deviceId); |
| 1161 | String name = getDeviceName(deviceId); |
Dianne Hackborn | a8f6018 | 2009-09-01 19:01:50 -0700 | [diff] [blame] | 1162 | InputDevice.AbsoluteInfo absX = null; |
| 1163 | InputDevice.AbsoluteInfo absY = null; |
| 1164 | InputDevice.AbsoluteInfo absPressure = null; |
| 1165 | InputDevice.AbsoluteInfo absSize = null; |
| 1166 | if (classes != 0) { |
| 1167 | Log.i(TAG, "Device added: id=0x" + Integer.toHexString(deviceId) |
| 1168 | + ", name=" + name |
| 1169 | + ", classes=" + Integer.toHexString(classes)); |
| 1170 | if ((classes&RawInputEvent.CLASS_TOUCHSCREEN_MT) != 0) { |
| 1171 | absX = loadAbsoluteInfo(deviceId, |
| 1172 | RawInputEvent.ABS_MT_POSITION_X, "X"); |
| 1173 | absY = loadAbsoluteInfo(deviceId, |
| 1174 | RawInputEvent.ABS_MT_POSITION_Y, "Y"); |
| 1175 | absPressure = loadAbsoluteInfo(deviceId, |
| 1176 | RawInputEvent.ABS_MT_TOUCH_MAJOR, "Pressure"); |
| 1177 | absSize = loadAbsoluteInfo(deviceId, |
| 1178 | RawInputEvent.ABS_MT_WIDTH_MAJOR, "Size"); |
| 1179 | } else if ((classes&RawInputEvent.CLASS_TOUCHSCREEN) != 0) { |
| 1180 | absX = loadAbsoluteInfo(deviceId, |
| 1181 | RawInputEvent.ABS_X, "X"); |
| 1182 | absY = loadAbsoluteInfo(deviceId, |
| 1183 | RawInputEvent.ABS_Y, "Y"); |
| 1184 | absPressure = loadAbsoluteInfo(deviceId, |
| 1185 | RawInputEvent.ABS_PRESSURE, "Pressure"); |
| 1186 | absSize = loadAbsoluteInfo(deviceId, |
| 1187 | RawInputEvent.ABS_TOOL_WIDTH, "Size"); |
| 1188 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1189 | } |
| 1190 | |
| 1191 | return new InputDevice(deviceId, classes, name, absX, absY, absPressure, absSize); |
| 1192 | } |
| 1193 | |
| 1194 | private InputDevice.AbsoluteInfo loadAbsoluteInfo(int id, int channel, |
| 1195 | String name) { |
| 1196 | InputDevice.AbsoluteInfo info = new InputDevice.AbsoluteInfo(); |
| 1197 | if (getAbsoluteInfo(id, channel, info) |
| 1198 | && info.minValue != info.maxValue) { |
| 1199 | Log.i(TAG, " " + name + ": min=" + info.minValue |
| 1200 | + " max=" + info.maxValue |
| 1201 | + " flat=" + info.flat |
| 1202 | + " fuzz=" + info.fuzz); |
| 1203 | info.range = info.maxValue-info.minValue; |
| 1204 | return info; |
| 1205 | } |
| 1206 | Log.i(TAG, " " + name + ": unknown values"); |
| 1207 | return null; |
| 1208 | } |
| 1209 | private static native boolean readEvent(RawInputEvent outEvent); |
| 1210 | } |