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