1 /* 2 * Copyright (C) 2010 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 #ifndef _LIBINPUT_INPUT_H 18 #define _LIBINPUT_INPUT_H 19 20 /** 21 * Native input event structures. 22 */ 23 24 #include <android/input.h> 25 #include <utils/BitSet.h> 26 #include <utils/KeyedVector.h> 27 #include <utils/RefBase.h> 28 #include <utils/String8.h> 29 #include <utils/Timers.h> 30 #include <utils/Vector.h> 31 32 /* 33 * Additional private constants not defined in ndk/ui/input.h. 34 */ 35 enum { 36 /* Signifies that the key is being predispatched */ 37 AKEY_EVENT_FLAG_PREDISPATCH = 0x20000000, 38 39 /* Private control to determine when an app is tracking a key sequence. */ 40 AKEY_EVENT_FLAG_START_TRACKING = 0x40000000, 41 42 /* Key event is inconsistent with previously sent key events. */ 43 AKEY_EVENT_FLAG_TAINTED = 0x80000000, 44 }; 45 46 enum { 47 /* Motion event is inconsistent with previously sent motion events. */ 48 AMOTION_EVENT_FLAG_TAINTED = 0x80000000, 49 }; 50 51 enum { 52 /* Used when a motion event is not associated with any display. 53 * Typically used for non-pointer events. */ 54 ADISPLAY_ID_NONE = -1, 55 56 /* The default display id. */ 57 ADISPLAY_ID_DEFAULT = 0, 58 }; 59 60 enum { 61 /* 62 * Indicates that an input device has switches. 63 * This input source flag is hidden from the API because switches are only used by the system 64 * and applications have no way to interact with them. 65 */ 66 AINPUT_SOURCE_SWITCH = 0x80000000, 67 }; 68 69 enum { 70 /** 71 * Constants for LEDs. Hidden from the API since we don't actually expose a way to interact 72 * with LEDs to developers 73 * 74 * NOTE: If you add LEDs here, you must also add them to InputEventLabels.h 75 */ 76 77 ALED_NUM_LOCK = 0x00, 78 ALED_CAPS_LOCK = 0x01, 79 ALED_SCROLL_LOCK = 0x02, 80 ALED_COMPOSE = 0x03, 81 ALED_KANA = 0x04, 82 ALED_SLEEP = 0x05, 83 ALED_SUSPEND = 0x06, 84 ALED_MUTE = 0x07, 85 ALED_MISC = 0x08, 86 ALED_MAIL = 0x09, 87 ALED_CHARGING = 0x0a, 88 ALED_CONTROLLER_1 = 0x10, 89 ALED_CONTROLLER_2 = 0x11, 90 ALED_CONTROLLER_3 = 0x12, 91 ALED_CONTROLLER_4 = 0x13, 92 }; 93 94 /* Maximum number of controller LEDs we support */ 95 #define MAX_CONTROLLER_LEDS 4 96 97 /* 98 * SystemUiVisibility constants from View. 99 */ 100 enum { 101 ASYSTEM_UI_VISIBILITY_STATUS_BAR_VISIBLE = 0, 102 ASYSTEM_UI_VISIBILITY_STATUS_BAR_HIDDEN = 0x00000001, 103 }; 104 105 /* 106 * Maximum number of pointers supported per motion event. 107 * Smallest number of pointers is 1. 108 * (We want at least 10 but some touch controllers obstensibly configured for 10 pointers 109 * will occasionally emit 11. There is not much harm making this constant bigger.) 110 */ 111 #define MAX_POINTERS 16 112 113 /* 114 * Maximum pointer id value supported in a motion event. 115 * Smallest pointer id is 0. 116 * (This is limited by our use of BitSet32 to track pointer assignments.) 117 */ 118 #define MAX_POINTER_ID 31 119 120 /* 121 * Declare a concrete type for the NDK's input event forward declaration. 122 */ 123 struct AInputEvent { 124 virtual ~AInputEvent() { } 125 }; 126 127 /* 128 * Declare a concrete type for the NDK's input device forward declaration. 129 */ 130 struct AInputDevice { 131 virtual ~AInputDevice() { } 132 }; 133 134 135 namespace android { 136 137 #ifdef HAVE_ANDROID_OS 138 class Parcel; 139 #endif 140 141 /* 142 * Flags that flow alongside events in the input dispatch system to help with certain 143 * policy decisions such as waking from device sleep. 144 * 145 * These flags are also defined in frameworks/base/core/java/android/view/WindowManagerPolicy.java. 146 */ 147 enum { 148 /* These flags originate in RawEvents and are generally set in the key map. 149 * NOTE: If you want a flag to be able to set in a keylayout file, then you must add it to 150 * InputEventLabels.h as well. */ 151 152 // Indicates that the event should wake the device. 153 POLICY_FLAG_WAKE = 0x00000001, 154 155 // Indicates that the key is virtual, such as a capacitive button, and should 156 // generate haptic feedback. Virtual keys may be suppressed for some time 157 // after a recent touch to prevent accidental activation of virtual keys adjacent 158 // to the touch screen during an edge swipe. 159 POLICY_FLAG_VIRTUAL = 0x00000002, 160 161 // Indicates that the key is the special function modifier. 162 POLICY_FLAG_FUNCTION = 0x00000004, 163 164 // Indicates that the key represents a special gesture that has been detected by 165 // the touch firmware or driver. Causes touch events from the same device to be canceled. 166 POLICY_FLAG_GESTURE = 0x00000008, 167 168 POLICY_FLAG_RAW_MASK = 0x0000ffff, 169 170 /* These flags are set by the input dispatcher. */ 171 172 // Indicates that the input event was injected. 173 POLICY_FLAG_INJECTED = 0x01000000, 174 175 // Indicates that the input event is from a trusted source such as a directly attached 176 // input device or an application with system-wide event injection permission. 177 POLICY_FLAG_TRUSTED = 0x02000000, 178 179 // Indicates that the input event has passed through an input filter. 180 POLICY_FLAG_FILTERED = 0x04000000, 181 182 // Disables automatic key repeating behavior. 183 POLICY_FLAG_DISABLE_KEY_REPEAT = 0x08000000, 184 185 /* These flags are set by the input reader policy as it intercepts each event. */ 186 187 // Indicates that the device was in an interactive state when the 188 // event was intercepted. 189 POLICY_FLAG_INTERACTIVE = 0x20000000, 190 191 // Indicates that the event should be dispatched to applications. 192 // The input event should still be sent to the InputDispatcher so that it can see all 193 // input events received include those that it will not deliver. 194 POLICY_FLAG_PASS_TO_USER = 0x40000000, 195 }; 196 197 /* 198 * Pointer coordinate data. 199 */ 200 struct PointerCoords { 201 enum { MAX_AXES = 30 }; // 30 so that sizeof(PointerCoords) == 128 202 203 // Bitfield of axes that are present in this structure. 204 uint64_t bits __attribute__((aligned(8))); 205 206 // Values of axes that are stored in this structure packed in order by axis id 207 // for each axis that is present in the structure according to 'bits'. 208 float values[MAX_AXES]; 209 210 inline void clear() { 211 BitSet64::clear(bits); 212 } 213 214 bool isEmpty() const { 215 return BitSet64::isEmpty(bits); 216 } 217 218 float getAxisValue(int32_t axis) const; 219 status_t setAxisValue(int32_t axis, float value); 220 221 void scale(float scale); 222 void applyOffset(float xOffset, float yOffset); 223 224 inline float getX() const { 225 return getAxisValue(AMOTION_EVENT_AXIS_X); 226 } 227 228 inline float getY() const { 229 return getAxisValue(AMOTION_EVENT_AXIS_Y); 230 } 231 232 #ifdef HAVE_ANDROID_OS 233 status_t readFromParcel(Parcel* parcel); 234 status_t writeToParcel(Parcel* parcel) const; 235 #endif 236 237 bool operator==(const PointerCoords& other) const; 238 inline bool operator!=(const PointerCoords& other) const { 239 return !(*this == other); 240 } 241 242 void copyFrom(const PointerCoords& other); 243 244 private: 245 void tooManyAxes(int axis); 246 }; 247 248 /* 249 * Pointer property data. 250 */ 251 struct PointerProperties { 252 // The id of the pointer. 253 int32_t id; 254 255 // The pointer tool type. 256 int32_t toolType; 257 258 inline void clear() { 259 id = -1; 260 toolType = 0; 261 } 262 263 bool operator==(const PointerProperties& other) const; 264 inline bool operator!=(const PointerProperties& other) const { 265 return !(*this == other); 266 } 267 268 void copyFrom(const PointerProperties& other); 269 }; 270 271 /* 272 * Input events. 273 */ 274 class InputEvent : public AInputEvent { 275 public: 276 virtual ~InputEvent() { } 277 278 virtual int32_t getType() const = 0; 279 280 inline int32_t getDeviceId() const { return mDeviceId; } 281 282 inline int32_t getSource() const { return mSource; } 283 284 inline void setSource(int32_t source) { mSource = source; } 285 286 protected: 287 void initialize(int32_t deviceId, int32_t source); 288 void initialize(const InputEvent& from); 289 290 int32_t mDeviceId; 291 int32_t mSource; 292 }; 293 294 /* 295 * Key events. 296 */ 297 class KeyEvent : public InputEvent { 298 public: 299 virtual ~KeyEvent() { } 300 301 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_KEY; } 302 303 inline int32_t getAction() const { return mAction; } 304 305 inline int32_t getFlags() const { return mFlags; } 306 307 inline void setFlags(int32_t flags) { mFlags = flags; } 308 309 inline int32_t getKeyCode() const { return mKeyCode; } 310 311 inline int32_t getScanCode() const { return mScanCode; } 312 313 inline int32_t getMetaState() const { return mMetaState; } 314 315 inline int32_t getRepeatCount() const { return mRepeatCount; } 316 317 inline nsecs_t getDownTime() const { return mDownTime; } 318 319 inline nsecs_t getEventTime() const { return mEventTime; } 320 321 static const char* getLabel(int32_t keyCode); 322 static int32_t getKeyCodeFromLabel(const char* label); 323 324 void initialize( 325 int32_t deviceId, 326 int32_t source, 327 int32_t action, 328 int32_t flags, 329 int32_t keyCode, 330 int32_t scanCode, 331 int32_t metaState, 332 int32_t repeatCount, 333 nsecs_t downTime, 334 nsecs_t eventTime); 335 void initialize(const KeyEvent& from); 336 337 protected: 338 int32_t mAction; 339 int32_t mFlags; 340 int32_t mKeyCode; 341 int32_t mScanCode; 342 int32_t mMetaState; 343 int32_t mRepeatCount; 344 nsecs_t mDownTime; 345 nsecs_t mEventTime; 346 }; 347 348 /* 349 * Motion events. 350 */ 351 class MotionEvent : public InputEvent { 352 public: 353 virtual ~MotionEvent() { } 354 355 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_MOTION; } 356 357 inline int32_t getAction() const { return mAction; } 358 359 inline int32_t getActionMasked() const { return mAction & AMOTION_EVENT_ACTION_MASK; } 360 361 inline int32_t getActionIndex() const { 362 return (mAction & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) 363 >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; 364 } 365 366 inline void setAction(int32_t action) { mAction = action; } 367 368 inline int32_t getFlags() const { return mFlags; } 369 370 inline void setFlags(int32_t flags) { mFlags = flags; } 371 372 inline int32_t getEdgeFlags() const { return mEdgeFlags; } 373 374 inline void setEdgeFlags(int32_t edgeFlags) { mEdgeFlags = edgeFlags; } 375 376 inline int32_t getMetaState() const { return mMetaState; } 377 378 inline void setMetaState(int32_t metaState) { mMetaState = metaState; } 379 380 inline int32_t getButtonState() const { return mButtonState; } 381 382 inline int32_t setButtonState(int32_t buttonState) { mButtonState = buttonState; } 383 384 inline int32_t getActionButton() const { return mActionButton; } 385 386 inline void setActionButton(int32_t button) { mActionButton = button; } 387 388 inline float getXOffset() const { return mXOffset; } 389 390 inline float getYOffset() const { return mYOffset; } 391 392 inline float getXPrecision() const { return mXPrecision; } 393 394 inline float getYPrecision() const { return mYPrecision; } 395 396 inline nsecs_t getDownTime() const { return mDownTime; } 397 398 inline void setDownTime(nsecs_t downTime) { mDownTime = downTime; } 399 400 inline size_t getPointerCount() const { return mPointerProperties.size(); } 401 402 inline const PointerProperties* getPointerProperties(size_t pointerIndex) const { 403 return &mPointerProperties[pointerIndex]; 404 } 405 406 inline int32_t getPointerId(size_t pointerIndex) const { 407 return mPointerProperties[pointerIndex].id; 408 } 409 410 inline int32_t getToolType(size_t pointerIndex) const { 411 return mPointerProperties[pointerIndex].toolType; 412 } 413 414 inline nsecs_t getEventTime() const { return mSampleEventTimes[getHistorySize()]; } 415 416 const PointerCoords* getRawPointerCoords(size_t pointerIndex) const; 417 418 float getRawAxisValue(int32_t axis, size_t pointerIndex) const; 419 420 inline float getRawX(size_t pointerIndex) const { 421 return getRawAxisValue(AMOTION_EVENT_AXIS_X, pointerIndex); 422 } 423 424 inline float getRawY(size_t pointerIndex) const { 425 return getRawAxisValue(AMOTION_EVENT_AXIS_Y, pointerIndex); 426 } 427 428 float getAxisValue(int32_t axis, size_t pointerIndex) const; 429 430 inline float getX(size_t pointerIndex) const { 431 return getAxisValue(AMOTION_EVENT_AXIS_X, pointerIndex); 432 } 433 434 inline float getY(size_t pointerIndex) const { 435 return getAxisValue(AMOTION_EVENT_AXIS_Y, pointerIndex); 436 } 437 438 inline float getPressure(size_t pointerIndex) const { 439 return getAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pointerIndex); 440 } 441 442 inline float getSize(size_t pointerIndex) const { 443 return getAxisValue(AMOTION_EVENT_AXIS_SIZE, pointerIndex); 444 } 445 446 inline float getTouchMajor(size_t pointerIndex) const { 447 return getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, pointerIndex); 448 } 449 450 inline float getTouchMinor(size_t pointerIndex) const { 451 return getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, pointerIndex); 452 } 453 454 inline float getToolMajor(size_t pointerIndex) const { 455 return getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, pointerIndex); 456 } 457 458 inline float getToolMinor(size_t pointerIndex) const { 459 return getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, pointerIndex); 460 } 461 462 inline float getOrientation(size_t pointerIndex) const { 463 return getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, pointerIndex); 464 } 465 466 inline size_t getHistorySize() const { return mSampleEventTimes.size() - 1; } 467 468 inline nsecs_t getHistoricalEventTime(size_t historicalIndex) const { 469 return mSampleEventTimes[historicalIndex]; 470 } 471 472 const PointerCoords* getHistoricalRawPointerCoords( 473 size_t pointerIndex, size_t historicalIndex) const; 474 475 float getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex, 476 size_t historicalIndex) const; 477 478 inline float getHistoricalRawX(size_t pointerIndex, size_t historicalIndex) const { 479 return getHistoricalRawAxisValue( 480 AMOTION_EVENT_AXIS_X, pointerIndex, historicalIndex); 481 } 482 483 inline float getHistoricalRawY(size_t pointerIndex, size_t historicalIndex) const { 484 return getHistoricalRawAxisValue( 485 AMOTION_EVENT_AXIS_Y, pointerIndex, historicalIndex); 486 } 487 488 float getHistoricalAxisValue(int32_t axis, size_t pointerIndex, size_t historicalIndex) const; 489 490 inline float getHistoricalX(size_t pointerIndex, size_t historicalIndex) const { 491 return getHistoricalAxisValue( 492 AMOTION_EVENT_AXIS_X, pointerIndex, historicalIndex); 493 } 494 495 inline float getHistoricalY(size_t pointerIndex, size_t historicalIndex) const { 496 return getHistoricalAxisValue( 497 AMOTION_EVENT_AXIS_Y, pointerIndex, historicalIndex); 498 } 499 500 inline float getHistoricalPressure(size_t pointerIndex, size_t historicalIndex) const { 501 return getHistoricalAxisValue( 502 AMOTION_EVENT_AXIS_PRESSURE, pointerIndex, historicalIndex); 503 } 504 505 inline float getHistoricalSize(size_t pointerIndex, size_t historicalIndex) const { 506 return getHistoricalAxisValue( 507 AMOTION_EVENT_AXIS_SIZE, pointerIndex, historicalIndex); 508 } 509 510 inline float getHistoricalTouchMajor(size_t pointerIndex, size_t historicalIndex) const { 511 return getHistoricalAxisValue( 512 AMOTION_EVENT_AXIS_TOUCH_MAJOR, pointerIndex, historicalIndex); 513 } 514 515 inline float getHistoricalTouchMinor(size_t pointerIndex, size_t historicalIndex) const { 516 return getHistoricalAxisValue( 517 AMOTION_EVENT_AXIS_TOUCH_MINOR, pointerIndex, historicalIndex); 518 } 519 520 inline float getHistoricalToolMajor(size_t pointerIndex, size_t historicalIndex) const { 521 return getHistoricalAxisValue( 522 AMOTION_EVENT_AXIS_TOOL_MAJOR, pointerIndex, historicalIndex); 523 } 524 525 inline float getHistoricalToolMinor(size_t pointerIndex, size_t historicalIndex) const { 526 return getHistoricalAxisValue( 527 AMOTION_EVENT_AXIS_TOOL_MINOR, pointerIndex, historicalIndex); 528 } 529 530 inline float getHistoricalOrientation(size_t pointerIndex, size_t historicalIndex) const { 531 return getHistoricalAxisValue( 532 AMOTION_EVENT_AXIS_ORIENTATION, pointerIndex, historicalIndex); 533 } 534 535 ssize_t findPointerIndex(int32_t pointerId) const; 536 537 void initialize( 538 int32_t deviceId, 539 int32_t source, 540 int32_t action, 541 int32_t actionButton, 542 int32_t flags, 543 int32_t edgeFlags, 544 int32_t metaState, 545 int32_t buttonState, 546 float xOffset, 547 float yOffset, 548 float xPrecision, 549 float yPrecision, 550 nsecs_t downTime, 551 nsecs_t eventTime, 552 size_t pointerCount, 553 const PointerProperties* pointerProperties, 554 const PointerCoords* pointerCoords); 555 556 void copyFrom(const MotionEvent* other, bool keepHistory); 557 558 void addSample( 559 nsecs_t eventTime, 560 const PointerCoords* pointerCoords); 561 562 void offsetLocation(float xOffset, float yOffset); 563 564 void scale(float scaleFactor); 565 566 // Apply 3x3 perspective matrix transformation. 567 // Matrix is in row-major form and compatible with SkMatrix. 568 void transform(const float matrix[9]); 569 570 #ifdef HAVE_ANDROID_OS 571 status_t readFromParcel(Parcel* parcel); 572 status_t writeToParcel(Parcel* parcel) const; 573 #endif 574 575 static bool isTouchEvent(int32_t source, int32_t action); 576 inline bool isTouchEvent() const { 577 return isTouchEvent(mSource, mAction); 578 } 579 580 // Low-level accessors. 581 inline const PointerProperties* getPointerProperties() const { 582 return mPointerProperties.array(); 583 } 584 inline const nsecs_t* getSampleEventTimes() const { return mSampleEventTimes.array(); } 585 inline const PointerCoords* getSamplePointerCoords() const { 586 return mSamplePointerCoords.array(); 587 } 588 589 static const char* getLabel(int32_t axis); 590 static int32_t getAxisFromLabel(const char* label); 591 592 protected: 593 int32_t mAction; 594 int32_t mActionButton; 595 int32_t mFlags; 596 int32_t mEdgeFlags; 597 int32_t mMetaState; 598 int32_t mButtonState; 599 float mXOffset; 600 float mYOffset; 601 float mXPrecision; 602 float mYPrecision; 603 nsecs_t mDownTime; 604 Vector<PointerProperties> mPointerProperties; 605 Vector<nsecs_t> mSampleEventTimes; 606 Vector<PointerCoords> mSamplePointerCoords; 607 }; 608 609 /* 610 * Input event factory. 611 */ 612 class InputEventFactoryInterface { 613 protected: 614 virtual ~InputEventFactoryInterface() { } 615 616 public: 617 InputEventFactoryInterface() { } 618 619 virtual KeyEvent* createKeyEvent() = 0; 620 virtual MotionEvent* createMotionEvent() = 0; 621 }; 622 623 /* 624 * A simple input event factory implementation that uses a single preallocated instance 625 * of each type of input event that are reused for each request. 626 */ 627 class PreallocatedInputEventFactory : public InputEventFactoryInterface { 628 public: 629 PreallocatedInputEventFactory() { } 630 virtual ~PreallocatedInputEventFactory() { } 631 632 virtual KeyEvent* createKeyEvent() { return & mKeyEvent; } 633 virtual MotionEvent* createMotionEvent() { return & mMotionEvent; } 634 635 private: 636 KeyEvent mKeyEvent; 637 MotionEvent mMotionEvent; 638 }; 639 640 /* 641 * An input event factory implementation that maintains a pool of input events. 642 */ 643 class PooledInputEventFactory : public InputEventFactoryInterface { 644 public: 645 PooledInputEventFactory(size_t maxPoolSize = 20); 646 virtual ~PooledInputEventFactory(); 647 648 virtual KeyEvent* createKeyEvent(); 649 virtual MotionEvent* createMotionEvent(); 650 651 void recycle(InputEvent* event); 652 653 private: 654 const size_t mMaxPoolSize; 655 656 Vector<KeyEvent*> mKeyEventPool; 657 Vector<MotionEvent*> mMotionEventPool; 658 }; 659 660 } // namespace android 661 662 #endif // _LIBINPUT_INPUT_H 663