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 #pragma GCC system_header 21 22 /** 23 * Native input event structures. 24 */ 25 26 #include <android/input.h> 27 #include <math.h> 28 #include <stdint.h> 29 #include <utils/BitSet.h> 30 #include <utils/KeyedVector.h> 31 #include <utils/RefBase.h> 32 #include <utils/Timers.h> 33 #include <utils/Vector.h> 34 #include <array> 35 #include <limits> 36 #include <queue> 37 38 /* 39 * Additional private constants not defined in ndk/ui/input.h. 40 */ 41 enum { 42 /* Signifies that the key is being predispatched */ 43 AKEY_EVENT_FLAG_PREDISPATCH = 0x20000000, 44 45 /* Private control to determine when an app is tracking a key sequence. */ 46 AKEY_EVENT_FLAG_START_TRACKING = 0x40000000, 47 48 /* Key event is inconsistent with previously sent key events. */ 49 AKEY_EVENT_FLAG_TAINTED = 0x80000000, 50 }; 51 52 enum { 53 54 /** 55 * This flag indicates that the window that received this motion event is partly 56 * or wholly obscured by another visible window above it. This flag is set to true 57 * even if the event did not directly pass through the obscured area. 58 * A security sensitive application can check this flag to identify situations in which 59 * a malicious application may have covered up part of its content for the purpose 60 * of misleading the user or hijacking touches. An appropriate response might be 61 * to drop the suspect touches or to take additional precautions to confirm the user's 62 * actual intent. 63 */ 64 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED = 0x2, 65 66 /** 67 * This flag indicates that the event has been generated by a gesture generator. It 68 * provides a hint to the GestureDetector to not apply any touch slop. 69 */ 70 AMOTION_EVENT_FLAG_IS_GENERATED_GESTURE = 0x8, 71 72 /* Motion event is inconsistent with previously sent motion events. */ 73 AMOTION_EVENT_FLAG_TAINTED = 0x80000000, 74 }; 75 76 /** 77 * Allowed VerifiedKeyEvent flags. All other flags from KeyEvent do not get verified. 78 * These values must be kept in sync with VerifiedKeyEvent.java 79 */ 80 constexpr int32_t VERIFIED_KEY_EVENT_FLAGS = AKEY_EVENT_FLAG_CANCELED; 81 82 /** 83 * Allowed VerifiedMotionEventFlags. All other flags from MotionEvent do not get verified. 84 * These values must be kept in sync with VerifiedMotionEvent.java 85 */ 86 constexpr int32_t VERIFIED_MOTION_EVENT_FLAGS = 87 AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; 88 89 enum { 90 /* Used when a motion event is not associated with any display. 91 * Typically used for non-pointer events. */ 92 ADISPLAY_ID_NONE = -1, 93 94 /* The default display id. */ 95 ADISPLAY_ID_DEFAULT = 0, 96 }; 97 98 enum { 99 /* 100 * Indicates that an input device has switches. 101 * This input source flag is hidden from the API because switches are only used by the system 102 * and applications have no way to interact with them. 103 */ 104 AINPUT_SOURCE_SWITCH = 0x80000000, 105 }; 106 107 enum { 108 /** 109 * Constants for LEDs. Hidden from the API since we don't actually expose a way to interact 110 * with LEDs to developers 111 * 112 * NOTE: If you add LEDs here, you must also add them to InputEventLabels.h 113 */ 114 115 ALED_NUM_LOCK = 0x00, 116 ALED_CAPS_LOCK = 0x01, 117 ALED_SCROLL_LOCK = 0x02, 118 ALED_COMPOSE = 0x03, 119 ALED_KANA = 0x04, 120 ALED_SLEEP = 0x05, 121 ALED_SUSPEND = 0x06, 122 ALED_MUTE = 0x07, 123 ALED_MISC = 0x08, 124 ALED_MAIL = 0x09, 125 ALED_CHARGING = 0x0a, 126 ALED_CONTROLLER_1 = 0x10, 127 ALED_CONTROLLER_2 = 0x11, 128 ALED_CONTROLLER_3 = 0x12, 129 ALED_CONTROLLER_4 = 0x13, 130 }; 131 132 /* Maximum number of controller LEDs we support */ 133 #define MAX_CONTROLLER_LEDS 4 134 135 /* 136 * SystemUiVisibility constants from View. 137 */ 138 enum { 139 ASYSTEM_UI_VISIBILITY_STATUS_BAR_VISIBLE = 0, 140 ASYSTEM_UI_VISIBILITY_STATUS_BAR_HIDDEN = 0x00000001, 141 }; 142 143 /* 144 * Maximum number of pointers supported per motion event. 145 * Smallest number of pointers is 1. 146 * (We want at least 10 but some touch controllers obstensibly configured for 10 pointers 147 * will occasionally emit 11. There is not much harm making this constant bigger.) 148 */ 149 #define MAX_POINTERS 16 150 151 /* 152 * Maximum number of samples supported per motion event. 153 */ 154 #define MAX_SAMPLES UINT16_MAX 155 156 /* 157 * Maximum pointer id value supported in a motion event. 158 * Smallest pointer id is 0. 159 * (This is limited by our use of BitSet32 to track pointer assignments.) 160 */ 161 #define MAX_POINTER_ID 31 162 163 /* 164 * Declare a concrete type for the NDK's input event forward declaration. 165 */ 166 struct AInputEvent { ~AInputEventAInputEvent167 virtual ~AInputEvent() { } 168 }; 169 170 /* 171 * Declare a concrete type for the NDK's input device forward declaration. 172 */ 173 struct AInputDevice { ~AInputDeviceAInputDevice174 virtual ~AInputDevice() { } 175 }; 176 177 178 namespace android { 179 180 #ifdef __ANDROID__ 181 class Parcel; 182 #endif 183 184 const char* inputEventTypeToString(int32_t type); 185 186 /* 187 * Flags that flow alongside events in the input dispatch system to help with certain 188 * policy decisions such as waking from device sleep. 189 * 190 * These flags are also defined in frameworks/base/core/java/android/view/WindowManagerPolicy.java. 191 */ 192 enum { 193 /* These flags originate in RawEvents and are generally set in the key map. 194 * NOTE: If you want a flag to be able to set in a keylayout file, then you must add it to 195 * InputEventLabels.h as well. */ 196 197 // Indicates that the event should wake the device. 198 POLICY_FLAG_WAKE = 0x00000001, 199 200 // Indicates that the key is virtual, such as a capacitive button, and should 201 // generate haptic feedback. Virtual keys may be suppressed for some time 202 // after a recent touch to prevent accidental activation of virtual keys adjacent 203 // to the touch screen during an edge swipe. 204 POLICY_FLAG_VIRTUAL = 0x00000002, 205 206 // Indicates that the key is the special function modifier. 207 POLICY_FLAG_FUNCTION = 0x00000004, 208 209 // Indicates that the key represents a special gesture that has been detected by 210 // the touch firmware or driver. Causes touch events from the same device to be canceled. 211 POLICY_FLAG_GESTURE = 0x00000008, 212 213 POLICY_FLAG_RAW_MASK = 0x0000ffff, 214 215 /* These flags are set by the input dispatcher. */ 216 217 // Indicates that the input event was injected. 218 POLICY_FLAG_INJECTED = 0x01000000, 219 220 // Indicates that the input event is from a trusted source such as a directly attached 221 // input device or an application with system-wide event injection permission. 222 POLICY_FLAG_TRUSTED = 0x02000000, 223 224 // Indicates that the input event has passed through an input filter. 225 POLICY_FLAG_FILTERED = 0x04000000, 226 227 // Disables automatic key repeating behavior. 228 POLICY_FLAG_DISABLE_KEY_REPEAT = 0x08000000, 229 230 /* These flags are set by the input reader policy as it intercepts each event. */ 231 232 // Indicates that the device was in an interactive state when the 233 // event was intercepted. 234 POLICY_FLAG_INTERACTIVE = 0x20000000, 235 236 // Indicates that the event should be dispatched to applications. 237 // The input event should still be sent to the InputDispatcher so that it can see all 238 // input events received include those that it will not deliver. 239 POLICY_FLAG_PASS_TO_USER = 0x40000000, 240 }; 241 242 /** 243 * Classifications of the current gesture, if available. 244 * 245 * The following values must be kept in sync with MotionEvent.java 246 */ 247 enum class MotionClassification : uint8_t { 248 /** 249 * No classification is available. 250 */ 251 NONE = 0, 252 /** 253 * Too early to classify the current gesture. Need more events. Look for changes in the 254 * upcoming motion events. 255 */ 256 AMBIGUOUS_GESTURE = 1, 257 /** 258 * The current gesture likely represents a user intentionally exerting force on the touchscreen. 259 */ 260 DEEP_PRESS = 2, 261 }; 262 263 /** 264 * String representation of MotionClassification 265 */ 266 const char* motionClassificationToString(MotionClassification classification); 267 268 /** 269 * Generator of unique numbers used to identify input events. 270 * 271 * Layout of ID: 272 * |--------------------------|---------------------------| 273 * | 2 bits for source | 30 bits for random number | 274 * |--------------------------|---------------------------| 275 */ 276 class IdGenerator { 277 private: 278 static constexpr uint32_t SOURCE_SHIFT = 30; 279 280 public: 281 // Used to divide integer space to ensure no conflict among these sources./ 282 enum class Source : int32_t { 283 INPUT_READER = 0x0 << SOURCE_SHIFT, 284 INPUT_DISPATCHER = 0x1 << SOURCE_SHIFT, 285 OTHER = 0x3 << SOURCE_SHIFT, // E.g. app injected events 286 }; 287 IdGenerator(Source source); 288 289 int32_t nextId() const; 290 291 // Extract source from given id. getSource(int32_t id)292 static inline Source getSource(int32_t id) { return static_cast<Source>(SOURCE_MASK & id); } 293 294 private: 295 const Source mSource; 296 297 static constexpr int32_t SOURCE_MASK = 0x3 << SOURCE_SHIFT; 298 }; 299 300 /** 301 * Invalid value for cursor position. Used for non-mouse events, tests and injected events. Don't 302 * use it for direct comparison with any other value, because NaN isn't equal to itself according to 303 * IEEE 754. Use isnan() instead to check if a cursor position is valid. 304 */ 305 constexpr float AMOTION_EVENT_INVALID_CURSOR_POSITION = std::numeric_limits<float>::quiet_NaN(); 306 307 /** 308 * Invalid value of HMAC - SHA256. Any events with this HMAC value will be marked as not verified. 309 */ 310 constexpr std::array<uint8_t, 32> INVALID_HMAC = {0}; 311 312 /* 313 * Pointer coordinate data. 314 */ 315 struct PointerCoords { 316 enum { MAX_AXES = 30 }; // 30 so that sizeof(PointerCoords) == 128 317 318 // Bitfield of axes that are present in this structure. 319 uint64_t bits __attribute__((aligned(8))); 320 321 // Values of axes that are stored in this structure packed in order by axis id 322 // for each axis that is present in the structure according to 'bits'. 323 float values[MAX_AXES]; 324 clearPointerCoords325 inline void clear() { 326 BitSet64::clear(bits); 327 } 328 isEmptyPointerCoords329 bool isEmpty() const { 330 return BitSet64::isEmpty(bits); 331 } 332 333 float getAxisValue(int32_t axis) const; 334 status_t setAxisValue(int32_t axis, float value); 335 336 void scale(float globalScale); 337 338 // Scale the pointer coordinates according to a global scale and a 339 // window scale. The global scale will be applied to TOUCH/TOOL_MAJOR/MINOR 340 // axes, however the window scaling will not. 341 void scale(float globalScale, float windowXScale, float windowYScale); 342 void applyOffset(float xOffset, float yOffset); 343 getXPointerCoords344 inline float getX() const { 345 return getAxisValue(AMOTION_EVENT_AXIS_X); 346 } 347 getYPointerCoords348 inline float getY() const { 349 return getAxisValue(AMOTION_EVENT_AXIS_Y); 350 } 351 352 #ifdef __ANDROID__ 353 status_t readFromParcel(Parcel* parcel); 354 status_t writeToParcel(Parcel* parcel) const; 355 #endif 356 357 bool operator==(const PointerCoords& other) const; 358 inline bool operator!=(const PointerCoords& other) const { 359 return !(*this == other); 360 } 361 362 void copyFrom(const PointerCoords& other); 363 364 private: 365 void tooManyAxes(int axis); 366 }; 367 368 /* 369 * Pointer property data. 370 */ 371 struct PointerProperties { 372 // The id of the pointer. 373 int32_t id; 374 375 // The pointer tool type. 376 int32_t toolType; 377 clearPointerProperties378 inline void clear() { 379 id = -1; 380 toolType = 0; 381 } 382 383 bool operator==(const PointerProperties& other) const; 384 inline bool operator!=(const PointerProperties& other) const { 385 return !(*this == other); 386 } 387 388 void copyFrom(const PointerProperties& other); 389 }; 390 391 /* 392 * Input events. 393 */ 394 class InputEvent : public AInputEvent { 395 public: ~InputEvent()396 virtual ~InputEvent() { } 397 398 virtual int32_t getType() const = 0; 399 getId()400 inline int32_t getId() const { return mId; } 401 getDeviceId()402 inline int32_t getDeviceId() const { return mDeviceId; } 403 getSource()404 inline uint32_t getSource() const { return mSource; } 405 setSource(uint32_t source)406 inline void setSource(uint32_t source) { mSource = source; } 407 getDisplayId()408 inline int32_t getDisplayId() const { return mDisplayId; } 409 setDisplayId(int32_t displayId)410 inline void setDisplayId(int32_t displayId) { mDisplayId = displayId; } 411 getHmac()412 inline std::array<uint8_t, 32> getHmac() const { return mHmac; } 413 414 static int32_t nextId(); 415 416 protected: 417 void initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId, 418 std::array<uint8_t, 32> hmac); 419 420 void initialize(const InputEvent& from); 421 422 int32_t mId; 423 int32_t mDeviceId; 424 uint32_t mSource; 425 int32_t mDisplayId; 426 std::array<uint8_t, 32> mHmac; 427 }; 428 429 /* 430 * Key events. 431 */ 432 class KeyEvent : public InputEvent { 433 public: ~KeyEvent()434 virtual ~KeyEvent() { } 435 getType()436 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_KEY; } 437 getAction()438 inline int32_t getAction() const { return mAction; } 439 getFlags()440 inline int32_t getFlags() const { return mFlags; } 441 setFlags(int32_t flags)442 inline void setFlags(int32_t flags) { mFlags = flags; } 443 getKeyCode()444 inline int32_t getKeyCode() const { return mKeyCode; } 445 getScanCode()446 inline int32_t getScanCode() const { return mScanCode; } 447 getMetaState()448 inline int32_t getMetaState() const { return mMetaState; } 449 getRepeatCount()450 inline int32_t getRepeatCount() const { return mRepeatCount; } 451 getDownTime()452 inline nsecs_t getDownTime() const { return mDownTime; } 453 getEventTime()454 inline nsecs_t getEventTime() const { return mEventTime; } 455 456 static const char* getLabel(int32_t keyCode); 457 static int32_t getKeyCodeFromLabel(const char* label); 458 459 void initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId, 460 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags, int32_t keyCode, 461 int32_t scanCode, int32_t metaState, int32_t repeatCount, nsecs_t downTime, 462 nsecs_t eventTime); 463 void initialize(const KeyEvent& from); 464 465 static const char* actionToString(int32_t action); 466 467 protected: 468 int32_t mAction; 469 int32_t mFlags; 470 int32_t mKeyCode; 471 int32_t mScanCode; 472 int32_t mMetaState; 473 int32_t mRepeatCount; 474 nsecs_t mDownTime; 475 nsecs_t mEventTime; 476 }; 477 478 /* 479 * Motion events. 480 */ 481 class MotionEvent : public InputEvent { 482 public: ~MotionEvent()483 virtual ~MotionEvent() { } 484 getType()485 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_MOTION; } 486 getAction()487 inline int32_t getAction() const { return mAction; } 488 getActionMasked()489 inline int32_t getActionMasked() const { return mAction & AMOTION_EVENT_ACTION_MASK; } 490 getActionIndex()491 inline int32_t getActionIndex() const { 492 return (mAction & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) 493 >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; 494 } 495 setAction(int32_t action)496 inline void setAction(int32_t action) { mAction = action; } 497 getFlags()498 inline int32_t getFlags() const { return mFlags; } 499 setFlags(int32_t flags)500 inline void setFlags(int32_t flags) { mFlags = flags; } 501 getEdgeFlags()502 inline int32_t getEdgeFlags() const { return mEdgeFlags; } 503 setEdgeFlags(int32_t edgeFlags)504 inline void setEdgeFlags(int32_t edgeFlags) { mEdgeFlags = edgeFlags; } 505 getMetaState()506 inline int32_t getMetaState() const { return mMetaState; } 507 setMetaState(int32_t metaState)508 inline void setMetaState(int32_t metaState) { mMetaState = metaState; } 509 getButtonState()510 inline int32_t getButtonState() const { return mButtonState; } 511 setButtonState(int32_t buttonState)512 inline void setButtonState(int32_t buttonState) { mButtonState = buttonState; } 513 getClassification()514 inline MotionClassification getClassification() const { return mClassification; } 515 getActionButton()516 inline int32_t getActionButton() const { return mActionButton; } 517 setActionButton(int32_t button)518 inline void setActionButton(int32_t button) { mActionButton = button; } 519 getXScale()520 inline float getXScale() const { return mXScale; } 521 getYScale()522 inline float getYScale() const { return mYScale; } 523 getXOffset()524 inline float getXOffset() const { return mXOffset; } 525 getYOffset()526 inline float getYOffset() const { return mYOffset; } 527 getXPrecision()528 inline float getXPrecision() const { return mXPrecision; } 529 getYPrecision()530 inline float getYPrecision() const { return mYPrecision; } 531 getRawXCursorPosition()532 inline float getRawXCursorPosition() const { return mRawXCursorPosition; } 533 534 float getXCursorPosition() const; 535 getRawYCursorPosition()536 inline float getRawYCursorPosition() const { return mRawYCursorPosition; } 537 538 float getYCursorPosition() const; 539 540 void setCursorPosition(float x, float y); 541 isValidCursorPosition(float x,float y)542 static inline bool isValidCursorPosition(float x, float y) { return !isnan(x) && !isnan(y); } 543 getDownTime()544 inline nsecs_t getDownTime() const { return mDownTime; } 545 setDownTime(nsecs_t downTime)546 inline void setDownTime(nsecs_t downTime) { mDownTime = downTime; } 547 getPointerCount()548 inline size_t getPointerCount() const { return mPointerProperties.size(); } 549 getPointerProperties(size_t pointerIndex)550 inline const PointerProperties* getPointerProperties(size_t pointerIndex) const { 551 return &mPointerProperties[pointerIndex]; 552 } 553 getPointerId(size_t pointerIndex)554 inline int32_t getPointerId(size_t pointerIndex) const { 555 return mPointerProperties[pointerIndex].id; 556 } 557 getToolType(size_t pointerIndex)558 inline int32_t getToolType(size_t pointerIndex) const { 559 return mPointerProperties[pointerIndex].toolType; 560 } 561 getEventTime()562 inline nsecs_t getEventTime() const { return mSampleEventTimes[getHistorySize()]; } 563 564 const PointerCoords* getRawPointerCoords(size_t pointerIndex) const; 565 566 float getRawAxisValue(int32_t axis, size_t pointerIndex) const; 567 getRawX(size_t pointerIndex)568 inline float getRawX(size_t pointerIndex) const { 569 return getRawAxisValue(AMOTION_EVENT_AXIS_X, pointerIndex); 570 } 571 getRawY(size_t pointerIndex)572 inline float getRawY(size_t pointerIndex) const { 573 return getRawAxisValue(AMOTION_EVENT_AXIS_Y, pointerIndex); 574 } 575 576 float getAxisValue(int32_t axis, size_t pointerIndex) const; 577 getX(size_t pointerIndex)578 inline float getX(size_t pointerIndex) const { 579 return getAxisValue(AMOTION_EVENT_AXIS_X, pointerIndex); 580 } 581 getY(size_t pointerIndex)582 inline float getY(size_t pointerIndex) const { 583 return getAxisValue(AMOTION_EVENT_AXIS_Y, pointerIndex); 584 } 585 getPressure(size_t pointerIndex)586 inline float getPressure(size_t pointerIndex) const { 587 return getAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pointerIndex); 588 } 589 getSize(size_t pointerIndex)590 inline float getSize(size_t pointerIndex) const { 591 return getAxisValue(AMOTION_EVENT_AXIS_SIZE, pointerIndex); 592 } 593 getTouchMajor(size_t pointerIndex)594 inline float getTouchMajor(size_t pointerIndex) const { 595 return getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, pointerIndex); 596 } 597 getTouchMinor(size_t pointerIndex)598 inline float getTouchMinor(size_t pointerIndex) const { 599 return getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, pointerIndex); 600 } 601 getToolMajor(size_t pointerIndex)602 inline float getToolMajor(size_t pointerIndex) const { 603 return getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, pointerIndex); 604 } 605 getToolMinor(size_t pointerIndex)606 inline float getToolMinor(size_t pointerIndex) const { 607 return getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, pointerIndex); 608 } 609 getOrientation(size_t pointerIndex)610 inline float getOrientation(size_t pointerIndex) const { 611 return getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, pointerIndex); 612 } 613 getHistorySize()614 inline size_t getHistorySize() const { return mSampleEventTimes.size() - 1; } 615 getHistoricalEventTime(size_t historicalIndex)616 inline nsecs_t getHistoricalEventTime(size_t historicalIndex) const { 617 return mSampleEventTimes[historicalIndex]; 618 } 619 620 const PointerCoords* getHistoricalRawPointerCoords( 621 size_t pointerIndex, size_t historicalIndex) const; 622 623 float getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex, 624 size_t historicalIndex) const; 625 getHistoricalRawX(size_t pointerIndex,size_t historicalIndex)626 inline float getHistoricalRawX(size_t pointerIndex, size_t historicalIndex) const { 627 return getHistoricalRawAxisValue( 628 AMOTION_EVENT_AXIS_X, pointerIndex, historicalIndex); 629 } 630 getHistoricalRawY(size_t pointerIndex,size_t historicalIndex)631 inline float getHistoricalRawY(size_t pointerIndex, size_t historicalIndex) const { 632 return getHistoricalRawAxisValue( 633 AMOTION_EVENT_AXIS_Y, pointerIndex, historicalIndex); 634 } 635 636 float getHistoricalAxisValue(int32_t axis, size_t pointerIndex, size_t historicalIndex) const; 637 getHistoricalX(size_t pointerIndex,size_t historicalIndex)638 inline float getHistoricalX(size_t pointerIndex, size_t historicalIndex) const { 639 return getHistoricalAxisValue( 640 AMOTION_EVENT_AXIS_X, pointerIndex, historicalIndex); 641 } 642 getHistoricalY(size_t pointerIndex,size_t historicalIndex)643 inline float getHistoricalY(size_t pointerIndex, size_t historicalIndex) const { 644 return getHistoricalAxisValue( 645 AMOTION_EVENT_AXIS_Y, pointerIndex, historicalIndex); 646 } 647 getHistoricalPressure(size_t pointerIndex,size_t historicalIndex)648 inline float getHistoricalPressure(size_t pointerIndex, size_t historicalIndex) const { 649 return getHistoricalAxisValue( 650 AMOTION_EVENT_AXIS_PRESSURE, pointerIndex, historicalIndex); 651 } 652 getHistoricalSize(size_t pointerIndex,size_t historicalIndex)653 inline float getHistoricalSize(size_t pointerIndex, size_t historicalIndex) const { 654 return getHistoricalAxisValue( 655 AMOTION_EVENT_AXIS_SIZE, pointerIndex, historicalIndex); 656 } 657 getHistoricalTouchMajor(size_t pointerIndex,size_t historicalIndex)658 inline float getHistoricalTouchMajor(size_t pointerIndex, size_t historicalIndex) const { 659 return getHistoricalAxisValue( 660 AMOTION_EVENT_AXIS_TOUCH_MAJOR, pointerIndex, historicalIndex); 661 } 662 getHistoricalTouchMinor(size_t pointerIndex,size_t historicalIndex)663 inline float getHistoricalTouchMinor(size_t pointerIndex, size_t historicalIndex) const { 664 return getHistoricalAxisValue( 665 AMOTION_EVENT_AXIS_TOUCH_MINOR, pointerIndex, historicalIndex); 666 } 667 getHistoricalToolMajor(size_t pointerIndex,size_t historicalIndex)668 inline float getHistoricalToolMajor(size_t pointerIndex, size_t historicalIndex) const { 669 return getHistoricalAxisValue( 670 AMOTION_EVENT_AXIS_TOOL_MAJOR, pointerIndex, historicalIndex); 671 } 672 getHistoricalToolMinor(size_t pointerIndex,size_t historicalIndex)673 inline float getHistoricalToolMinor(size_t pointerIndex, size_t historicalIndex) const { 674 return getHistoricalAxisValue( 675 AMOTION_EVENT_AXIS_TOOL_MINOR, pointerIndex, historicalIndex); 676 } 677 getHistoricalOrientation(size_t pointerIndex,size_t historicalIndex)678 inline float getHistoricalOrientation(size_t pointerIndex, size_t historicalIndex) const { 679 return getHistoricalAxisValue( 680 AMOTION_EVENT_AXIS_ORIENTATION, pointerIndex, historicalIndex); 681 } 682 683 ssize_t findPointerIndex(int32_t pointerId) const; 684 685 void initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId, 686 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton, 687 int32_t flags, int32_t edgeFlags, int32_t metaState, int32_t buttonState, 688 MotionClassification classification, float xScale, float yScale, float xOffset, 689 float yOffset, float xPrecision, float yPrecision, float rawXCursorPosition, 690 float rawYCursorPosition, nsecs_t downTime, nsecs_t eventTime, 691 size_t pointerCount, const PointerProperties* pointerProperties, 692 const PointerCoords* pointerCoords); 693 694 void copyFrom(const MotionEvent* other, bool keepHistory); 695 696 void addSample( 697 nsecs_t eventTime, 698 const PointerCoords* pointerCoords); 699 700 void offsetLocation(float xOffset, float yOffset); 701 702 void scale(float globalScaleFactor); 703 704 // Apply 3x3 perspective matrix transformation. 705 // Matrix is in row-major form and compatible with SkMatrix. 706 void transform(const float matrix[9]); 707 708 #ifdef __ANDROID__ 709 status_t readFromParcel(Parcel* parcel); 710 status_t writeToParcel(Parcel* parcel) const; 711 #endif 712 713 static bool isTouchEvent(uint32_t source, int32_t action); isTouchEvent()714 inline bool isTouchEvent() const { 715 return isTouchEvent(mSource, mAction); 716 } 717 718 // Low-level accessors. getPointerProperties()719 inline const PointerProperties* getPointerProperties() const { 720 return mPointerProperties.array(); 721 } getSampleEventTimes()722 inline const nsecs_t* getSampleEventTimes() const { return mSampleEventTimes.array(); } getSamplePointerCoords()723 inline const PointerCoords* getSamplePointerCoords() const { 724 return mSamplePointerCoords.array(); 725 } 726 727 static const char* getLabel(int32_t axis); 728 static int32_t getAxisFromLabel(const char* label); 729 730 static const char* actionToString(int32_t action); 731 732 protected: 733 int32_t mAction; 734 int32_t mActionButton; 735 int32_t mFlags; 736 int32_t mEdgeFlags; 737 int32_t mMetaState; 738 int32_t mButtonState; 739 MotionClassification mClassification; 740 float mXScale; 741 float mYScale; 742 float mXOffset; 743 float mYOffset; 744 float mXPrecision; 745 float mYPrecision; 746 float mRawXCursorPosition; 747 float mRawYCursorPosition; 748 nsecs_t mDownTime; 749 Vector<PointerProperties> mPointerProperties; 750 Vector<nsecs_t> mSampleEventTimes; 751 Vector<PointerCoords> mSamplePointerCoords; 752 }; 753 754 /* 755 * Focus events. 756 */ 757 class FocusEvent : public InputEvent { 758 public: ~FocusEvent()759 virtual ~FocusEvent() {} 760 getType()761 virtual int32_t getType() const override { return AINPUT_EVENT_TYPE_FOCUS; } 762 getHasFocus()763 inline bool getHasFocus() const { return mHasFocus; } 764 getInTouchMode()765 inline bool getInTouchMode() const { return mInTouchMode; } 766 767 void initialize(int32_t id, bool hasFocus, bool inTouchMode); 768 769 void initialize(const FocusEvent& from); 770 771 protected: 772 bool mHasFocus; 773 bool mInTouchMode; 774 }; 775 776 /** 777 * Base class for verified events. 778 * Do not create a VerifiedInputEvent explicitly. 779 * Use helper functions to create them from InputEvents. 780 */ 781 struct __attribute__((__packed__)) VerifiedInputEvent { 782 enum class Type : int32_t { 783 KEY = AINPUT_EVENT_TYPE_KEY, 784 MOTION = AINPUT_EVENT_TYPE_MOTION, 785 }; 786 787 Type type; 788 int32_t deviceId; 789 nsecs_t eventTimeNanos; 790 uint32_t source; 791 int32_t displayId; 792 }; 793 794 /** 795 * Same as KeyEvent, but only contains the data that can be verified. 796 * If you update this class, you must also update VerifiedKeyEvent.java 797 */ 798 struct __attribute__((__packed__)) VerifiedKeyEvent : public VerifiedInputEvent { 799 int32_t action; 800 nsecs_t downTimeNanos; 801 int32_t flags; 802 int32_t keyCode; 803 int32_t scanCode; 804 int32_t metaState; 805 int32_t repeatCount; 806 }; 807 808 /** 809 * Same as MotionEvent, but only contains the data that can be verified. 810 * If you update this class, you must also update VerifiedMotionEvent.java 811 */ 812 struct __attribute__((__packed__)) VerifiedMotionEvent : public VerifiedInputEvent { 813 float rawX; 814 float rawY; 815 int32_t actionMasked; 816 nsecs_t downTimeNanos; 817 int32_t flags; 818 int32_t metaState; 819 int32_t buttonState; 820 }; 821 822 VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event); 823 VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event); 824 825 /* 826 * Input event factory. 827 */ 828 class InputEventFactoryInterface { 829 protected: ~InputEventFactoryInterface()830 virtual ~InputEventFactoryInterface() { } 831 832 public: InputEventFactoryInterface()833 InputEventFactoryInterface() { } 834 835 virtual KeyEvent* createKeyEvent() = 0; 836 virtual MotionEvent* createMotionEvent() = 0; 837 virtual FocusEvent* createFocusEvent() = 0; 838 }; 839 840 /* 841 * A simple input event factory implementation that uses a single preallocated instance 842 * of each type of input event that are reused for each request. 843 */ 844 class PreallocatedInputEventFactory : public InputEventFactoryInterface { 845 public: PreallocatedInputEventFactory()846 PreallocatedInputEventFactory() { } ~PreallocatedInputEventFactory()847 virtual ~PreallocatedInputEventFactory() { } 848 createKeyEvent()849 virtual KeyEvent* createKeyEvent() override { return &mKeyEvent; } createMotionEvent()850 virtual MotionEvent* createMotionEvent() override { return &mMotionEvent; } createFocusEvent()851 virtual FocusEvent* createFocusEvent() override { return &mFocusEvent; } 852 853 private: 854 KeyEvent mKeyEvent; 855 MotionEvent mMotionEvent; 856 FocusEvent mFocusEvent; 857 }; 858 859 /* 860 * An input event factory implementation that maintains a pool of input events. 861 */ 862 class PooledInputEventFactory : public InputEventFactoryInterface { 863 public: 864 explicit PooledInputEventFactory(size_t maxPoolSize = 20); 865 virtual ~PooledInputEventFactory(); 866 867 virtual KeyEvent* createKeyEvent() override; 868 virtual MotionEvent* createMotionEvent() override; 869 virtual FocusEvent* createFocusEvent() override; 870 871 void recycle(InputEvent* event); 872 873 private: 874 const size_t mMaxPoolSize; 875 876 std::queue<std::unique_ptr<KeyEvent>> mKeyEventPool; 877 std::queue<std::unique_ptr<MotionEvent>> mMotionEventPool; 878 std::queue<std::unique_ptr<FocusEvent>> mFocusEventPool; 879 }; 880 881 } // namespace android 882 883 #endif // _LIBINPUT_INPUT_H 884