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 #ifdef __linux__ 28 #include <android/os/IInputConstants.h> 29 #endif 30 #include <math.h> 31 #include <stdint.h> 32 #include <ui/Transform.h> 33 #include <utils/BitSet.h> 34 #include <utils/RefBase.h> 35 #include <utils/Timers.h> 36 #include <array> 37 #include <limits> 38 #include <queue> 39 40 /* 41 * Additional private constants not defined in ndk/ui/input.h. 42 */ 43 enum { 44 #ifdef __linux__ 45 /* This event was generated or modified by accessibility service. */ 46 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT = 47 android::os::IInputConstants::INPUT_EVENT_FLAG_IS_ACCESSIBILITY_EVENT, // 0x800, 48 #else 49 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT = 0x800, 50 #endif 51 /* Signifies that the key is being predispatched */ 52 AKEY_EVENT_FLAG_PREDISPATCH = 0x20000000, 53 54 /* Private control to determine when an app is tracking a key sequence. */ 55 AKEY_EVENT_FLAG_START_TRACKING = 0x40000000, 56 57 /* Key event is inconsistent with previously sent key events. */ 58 AKEY_EVENT_FLAG_TAINTED = 0x80000000, 59 }; 60 61 enum { 62 63 /** 64 * This flag indicates that the window that received this motion event is partly 65 * or wholly obscured by another visible window above it. This flag is set to true 66 * even if the event did not directly pass through the obscured area. 67 * A security sensitive application can check this flag to identify situations in which 68 * a malicious application may have covered up part of its content for the purpose 69 * of misleading the user or hijacking touches. An appropriate response might be 70 * to drop the suspect touches or to take additional precautions to confirm the user's 71 * actual intent. 72 */ 73 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED = 0x2, 74 75 /** 76 * This flag indicates that the event has been generated by a gesture generator. It 77 * provides a hint to the GestureDetector to not apply any touch slop. 78 */ 79 AMOTION_EVENT_FLAG_IS_GENERATED_GESTURE = 0x8, 80 81 /** 82 * This flag indicates that the event will not cause a focus change if it is directed to an 83 * unfocused window, even if it an ACTION_DOWN. This is typically used with pointer 84 * gestures to allow the user to direct gestures to an unfocused window without bringing it 85 * into focus. 86 */ 87 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE = 0x40, 88 89 #if defined(__linux__) 90 /** 91 * This event was generated or modified by accessibility service. 92 */ 93 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT = 94 android::os::IInputConstants::INPUT_EVENT_FLAG_IS_ACCESSIBILITY_EVENT, // 0x800, 95 #else 96 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT = 0x800, 97 #endif 98 99 /* Motion event is inconsistent with previously sent motion events. */ 100 AMOTION_EVENT_FLAG_TAINTED = 0x80000000, 101 }; 102 103 /** 104 * Allowed VerifiedKeyEvent flags. All other flags from KeyEvent do not get verified. 105 * These values must be kept in sync with VerifiedKeyEvent.java 106 */ 107 constexpr int32_t VERIFIED_KEY_EVENT_FLAGS = 108 AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; 109 110 /** 111 * Allowed VerifiedMotionEventFlags. All other flags from MotionEvent do not get verified. 112 * These values must be kept in sync with VerifiedMotionEvent.java 113 */ 114 constexpr int32_t VERIFIED_MOTION_EVENT_FLAGS = AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | 115 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED | AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; 116 117 /** 118 * This flag indicates that the point up event has been canceled. 119 * Typically this is used for palm event when the user has accidental touches. 120 * TODO: Adjust flag to public api 121 */ 122 constexpr int32_t AMOTION_EVENT_FLAG_CANCELED = 0x20; 123 124 enum { 125 /* 126 * Indicates that an input device has switches. 127 * This input source flag is hidden from the API because switches are only used by the system 128 * and applications have no way to interact with them. 129 */ 130 AINPUT_SOURCE_SWITCH = 0x80000000, 131 }; 132 133 enum { 134 /** 135 * Constants for LEDs. Hidden from the API since we don't actually expose a way to interact 136 * with LEDs to developers 137 * 138 * NOTE: If you add LEDs here, you must also add them to InputEventLabels.h 139 */ 140 141 ALED_NUM_LOCK = 0x00, 142 ALED_CAPS_LOCK = 0x01, 143 ALED_SCROLL_LOCK = 0x02, 144 ALED_COMPOSE = 0x03, 145 ALED_KANA = 0x04, 146 ALED_SLEEP = 0x05, 147 ALED_SUSPEND = 0x06, 148 ALED_MUTE = 0x07, 149 ALED_MISC = 0x08, 150 ALED_MAIL = 0x09, 151 ALED_CHARGING = 0x0a, 152 ALED_CONTROLLER_1 = 0x10, 153 ALED_CONTROLLER_2 = 0x11, 154 ALED_CONTROLLER_3 = 0x12, 155 ALED_CONTROLLER_4 = 0x13, 156 }; 157 158 /* Maximum number of controller LEDs we support */ 159 #define MAX_CONTROLLER_LEDS 4 160 161 /* 162 * Maximum number of pointers supported per motion event. 163 * Smallest number of pointers is 1. 164 * (We want at least 10 but some touch controllers obstensibly configured for 10 pointers 165 * will occasionally emit 11. There is not much harm making this constant bigger.) 166 */ 167 static constexpr size_t MAX_POINTERS = 16; 168 169 /* 170 * Maximum number of samples supported per motion event. 171 */ 172 #define MAX_SAMPLES UINT16_MAX 173 174 /* 175 * Maximum pointer id value supported in a motion event. 176 * Smallest pointer id is 0. 177 * (This is limited by our use of BitSet32 to track pointer assignments.) 178 */ 179 #define MAX_POINTER_ID 31 180 181 /* 182 * Declare a concrete type for the NDK's input event forward declaration. 183 */ 184 struct AInputEvent { ~AInputEventAInputEvent185 virtual ~AInputEvent() { } 186 }; 187 188 /* 189 * Declare a concrete type for the NDK's input device forward declaration. 190 */ 191 struct AInputDevice { ~AInputDeviceAInputDevice192 virtual ~AInputDevice() { } 193 }; 194 195 196 namespace android { 197 198 #ifdef __linux__ 199 class Parcel; 200 #endif 201 202 /* 203 * Apply the given transform to the point without applying any translation/offset. 204 */ 205 vec2 transformWithoutTranslation(const ui::Transform& transform, const vec2& xy); 206 207 const char* inputEventTypeToString(int32_t type); 208 209 std::string inputEventSourceToString(int32_t source); 210 211 bool isFromSource(uint32_t source, uint32_t test); 212 213 /* 214 * Flags that flow alongside events in the input dispatch system to help with certain 215 * policy decisions such as waking from device sleep. 216 * 217 * These flags are also defined in frameworks/base/core/java/android/view/WindowManagerPolicy.java. 218 */ 219 enum { 220 /* These flags originate in RawEvents and are generally set in the key map. 221 * NOTE: If you want a flag to be able to set in a keylayout file, then you must add it to 222 * InputEventLabels.h as well. */ 223 224 // Indicates that the event should wake the device. 225 POLICY_FLAG_WAKE = 0x00000001, 226 227 // Indicates that the key is virtual, such as a capacitive button, and should 228 // generate haptic feedback. Virtual keys may be suppressed for some time 229 // after a recent touch to prevent accidental activation of virtual keys adjacent 230 // to the touch screen during an edge swipe. 231 POLICY_FLAG_VIRTUAL = 0x00000002, 232 233 // Indicates that the key is the special function modifier. 234 POLICY_FLAG_FUNCTION = 0x00000004, 235 236 // Indicates that the key represents a special gesture that has been detected by 237 // the touch firmware or driver. Causes touch events from the same device to be canceled. 238 POLICY_FLAG_GESTURE = 0x00000008, 239 240 POLICY_FLAG_RAW_MASK = 0x0000ffff, 241 242 #ifdef __linux__ 243 POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY = 244 android::os::IInputConstants::POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, 245 #else 246 POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY = 0x20000, 247 #endif 248 249 /* These flags are set by the input dispatcher. */ 250 251 // Indicates that the input event was injected. 252 POLICY_FLAG_INJECTED = 0x01000000, 253 254 // Indicates that the input event is from a trusted source such as a directly attached 255 // input device or an application with system-wide event injection permission. 256 POLICY_FLAG_TRUSTED = 0x02000000, 257 258 // Indicates that the input event has passed through an input filter. 259 POLICY_FLAG_FILTERED = 0x04000000, 260 261 // Disables automatic key repeating behavior. 262 POLICY_FLAG_DISABLE_KEY_REPEAT = 0x08000000, 263 264 /* These flags are set by the input reader policy as it intercepts each event. */ 265 266 // Indicates that the device was in an interactive state when the 267 // event was intercepted. 268 POLICY_FLAG_INTERACTIVE = 0x20000000, 269 270 // Indicates that the event should be dispatched to applications. 271 // The input event should still be sent to the InputDispatcher so that it can see all 272 // input events received include those that it will not deliver. 273 POLICY_FLAG_PASS_TO_USER = 0x40000000, 274 }; 275 276 /** 277 * Classifications of the current gesture, if available. 278 */ 279 enum class MotionClassification : uint8_t { 280 /** 281 * No classification is available. 282 */ 283 NONE = AMOTION_EVENT_CLASSIFICATION_NONE, 284 /** 285 * Too early to classify the current gesture. Need more events. Look for changes in the 286 * upcoming motion events. 287 */ 288 AMBIGUOUS_GESTURE = AMOTION_EVENT_CLASSIFICATION_AMBIGUOUS_GESTURE, 289 /** 290 * The current gesture likely represents a user intentionally exerting force on the touchscreen. 291 */ 292 DEEP_PRESS = AMOTION_EVENT_CLASSIFICATION_DEEP_PRESS, 293 }; 294 295 /** 296 * String representation of MotionClassification 297 */ 298 const char* motionClassificationToString(MotionClassification classification); 299 300 const char* motionToolTypeToString(int32_t toolType); 301 302 /** 303 * Portion of FrameMetrics timeline of interest to input code. 304 */ 305 enum GraphicsTimeline : size_t { 306 /** Time when the app sent the buffer to SurfaceFlinger. */ 307 GPU_COMPLETED_TIME = 0, 308 309 /** Time when the frame was presented on the display */ 310 PRESENT_TIME = 1, 311 312 /** Total size of the 'GraphicsTimeline' array. Must always be last. */ 313 SIZE = 2 314 }; 315 316 /** 317 * Generator of unique numbers used to identify input events. 318 * 319 * Layout of ID: 320 * |--------------------------|---------------------------| 321 * | 2 bits for source | 30 bits for random number | 322 * |--------------------------|---------------------------| 323 */ 324 class IdGenerator { 325 private: 326 static constexpr uint32_t SOURCE_SHIFT = 30; 327 328 public: 329 // Used to divide integer space to ensure no conflict among these sources./ 330 enum class Source : int32_t { 331 INPUT_READER = static_cast<int32_t>(0x0u << SOURCE_SHIFT), 332 INPUT_DISPATCHER = static_cast<int32_t>(0x1u << SOURCE_SHIFT), 333 OTHER = static_cast<int32_t>(0x3u << SOURCE_SHIFT), // E.g. app injected events 334 }; 335 IdGenerator(Source source); 336 337 int32_t nextId() const; 338 339 // Extract source from given id. getSource(int32_t id)340 static inline Source getSource(int32_t id) { return static_cast<Source>(SOURCE_MASK & id); } 341 342 private: 343 const Source mSource; 344 345 static constexpr int32_t SOURCE_MASK = static_cast<int32_t>(0x3u << SOURCE_SHIFT); 346 }; 347 348 /** 349 * Invalid value for cursor position. Used for non-mouse events, tests and injected events. Don't 350 * use it for direct comparison with any other value, because NaN isn't equal to itself according to 351 * IEEE 754. Use isnan() instead to check if a cursor position is valid. 352 */ 353 constexpr float AMOTION_EVENT_INVALID_CURSOR_POSITION = std::numeric_limits<float>::quiet_NaN(); 354 355 /* 356 * Pointer coordinate data. 357 */ 358 struct PointerCoords { 359 enum { MAX_AXES = 30 }; // 30 so that sizeof(PointerCoords) == 128 360 361 // Bitfield of axes that are present in this structure. 362 uint64_t bits __attribute__((aligned(8))); 363 364 // Values of axes that are stored in this structure packed in order by axis id 365 // for each axis that is present in the structure according to 'bits'. 366 float values[MAX_AXES]; 367 clearPointerCoords368 inline void clear() { 369 BitSet64::clear(bits); 370 } 371 isEmptyPointerCoords372 bool isEmpty() const { 373 return BitSet64::isEmpty(bits); 374 } 375 376 float getAxisValue(int32_t axis) const; 377 status_t setAxisValue(int32_t axis, float value); 378 379 // Scale the pointer coordinates according to a global scale and a 380 // window scale. The global scale will be applied to TOUCH/TOOL_MAJOR/MINOR 381 // axes, however the window scaling will not. 382 void scale(float globalScale, float windowXScale, float windowYScale); 383 384 void transform(const ui::Transform& transform); 385 getXPointerCoords386 inline float getX() const { 387 return getAxisValue(AMOTION_EVENT_AXIS_X); 388 } 389 getYPointerCoords390 inline float getY() const { 391 return getAxisValue(AMOTION_EVENT_AXIS_Y); 392 } 393 getXYValuePointerCoords394 vec2 getXYValue() const { return vec2(getX(), getY()); } 395 396 #ifdef __linux__ 397 status_t readFromParcel(Parcel* parcel); 398 status_t writeToParcel(Parcel* parcel) const; 399 #endif 400 401 bool operator==(const PointerCoords& other) const; 402 inline bool operator!=(const PointerCoords& other) const { 403 return !(*this == other); 404 } 405 406 void copyFrom(const PointerCoords& other); 407 408 private: 409 void tooManyAxes(int axis); 410 }; 411 412 /* 413 * Pointer property data. 414 */ 415 struct PointerProperties { 416 // The id of the pointer. 417 int32_t id; 418 419 // The pointer tool type. 420 int32_t toolType; 421 clearPointerProperties422 inline void clear() { 423 id = -1; 424 toolType = 0; 425 } 426 427 bool operator==(const PointerProperties& other) const; 428 inline bool operator!=(const PointerProperties& other) const { 429 return !(*this == other); 430 } 431 432 void copyFrom(const PointerProperties& other); 433 }; 434 435 /* 436 * Input events. 437 */ 438 class InputEvent : public AInputEvent { 439 public: ~InputEvent()440 virtual ~InputEvent() { } 441 442 virtual int32_t getType() const = 0; 443 getId()444 inline int32_t getId() const { return mId; } 445 getDeviceId()446 inline int32_t getDeviceId() const { return mDeviceId; } 447 getSource()448 inline uint32_t getSource() const { return mSource; } 449 setSource(uint32_t source)450 inline void setSource(uint32_t source) { mSource = source; } 451 getDisplayId()452 inline int32_t getDisplayId() const { return mDisplayId; } 453 setDisplayId(int32_t displayId)454 inline void setDisplayId(int32_t displayId) { mDisplayId = displayId; } 455 getHmac()456 inline std::array<uint8_t, 32> getHmac() const { return mHmac; } 457 458 static int32_t nextId(); 459 460 protected: 461 void initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId, 462 std::array<uint8_t, 32> hmac); 463 464 void initialize(const InputEvent& from); 465 466 int32_t mId; 467 int32_t mDeviceId; 468 uint32_t mSource; 469 int32_t mDisplayId; 470 std::array<uint8_t, 32> mHmac; 471 }; 472 473 /* 474 * Key events. 475 */ 476 class KeyEvent : public InputEvent { 477 public: ~KeyEvent()478 virtual ~KeyEvent() { } 479 getType()480 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_KEY; } 481 getAction()482 inline int32_t getAction() const { return mAction; } 483 getFlags()484 inline int32_t getFlags() const { return mFlags; } 485 setFlags(int32_t flags)486 inline void setFlags(int32_t flags) { mFlags = flags; } 487 getKeyCode()488 inline int32_t getKeyCode() const { return mKeyCode; } 489 getScanCode()490 inline int32_t getScanCode() const { return mScanCode; } 491 getMetaState()492 inline int32_t getMetaState() const { return mMetaState; } 493 getRepeatCount()494 inline int32_t getRepeatCount() const { return mRepeatCount; } 495 getDownTime()496 inline nsecs_t getDownTime() const { return mDownTime; } 497 getEventTime()498 inline nsecs_t getEventTime() const { return mEventTime; } 499 500 static const char* getLabel(int32_t keyCode); 501 static int32_t getKeyCodeFromLabel(const char* label); 502 503 void initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId, 504 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags, int32_t keyCode, 505 int32_t scanCode, int32_t metaState, int32_t repeatCount, nsecs_t downTime, 506 nsecs_t eventTime); 507 void initialize(const KeyEvent& from); 508 509 static const char* actionToString(int32_t action); 510 511 protected: 512 int32_t mAction; 513 int32_t mFlags; 514 int32_t mKeyCode; 515 int32_t mScanCode; 516 int32_t mMetaState; 517 int32_t mRepeatCount; 518 nsecs_t mDownTime; 519 nsecs_t mEventTime; 520 }; 521 522 /* 523 * Motion events. 524 */ 525 class MotionEvent : public InputEvent { 526 public: ~MotionEvent()527 virtual ~MotionEvent() { } 528 getType()529 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_MOTION; } 530 getAction()531 inline int32_t getAction() const { return mAction; } 532 getActionMasked(int32_t action)533 static int32_t getActionMasked(int32_t action) { return action & AMOTION_EVENT_ACTION_MASK; } 534 getActionMasked()535 inline int32_t getActionMasked() const { return getActionMasked(mAction); } 536 getActionIndex(int32_t action)537 static uint8_t getActionIndex(int32_t action) { 538 return (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> 539 AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; 540 } 541 getActionIndex()542 inline int32_t getActionIndex() const { return getActionIndex(mAction); } 543 setAction(int32_t action)544 inline void setAction(int32_t action) { mAction = action; } 545 getFlags()546 inline int32_t getFlags() const { return mFlags; } 547 setFlags(int32_t flags)548 inline void setFlags(int32_t flags) { mFlags = flags; } 549 getEdgeFlags()550 inline int32_t getEdgeFlags() const { return mEdgeFlags; } 551 setEdgeFlags(int32_t edgeFlags)552 inline void setEdgeFlags(int32_t edgeFlags) { mEdgeFlags = edgeFlags; } 553 getMetaState()554 inline int32_t getMetaState() const { return mMetaState; } 555 setMetaState(int32_t metaState)556 inline void setMetaState(int32_t metaState) { mMetaState = metaState; } 557 getButtonState()558 inline int32_t getButtonState() const { return mButtonState; } 559 setButtonState(int32_t buttonState)560 inline void setButtonState(int32_t buttonState) { mButtonState = buttonState; } 561 getClassification()562 inline MotionClassification getClassification() const { return mClassification; } 563 getActionButton()564 inline int32_t getActionButton() const { return mActionButton; } 565 setActionButton(int32_t button)566 inline void setActionButton(int32_t button) { mActionButton = button; } 567 getXOffset()568 inline float getXOffset() const { return mTransform.tx(); } 569 getYOffset()570 inline float getYOffset() const { return mTransform.ty(); } 571 getTransform()572 inline const ui::Transform& getTransform() const { return mTransform; } 573 574 int getSurfaceRotation() const; 575 getXPrecision()576 inline float getXPrecision() const { return mXPrecision; } 577 getYPrecision()578 inline float getYPrecision() const { return mYPrecision; } 579 getRawXCursorPosition()580 inline float getRawXCursorPosition() const { return mRawXCursorPosition; } 581 582 float getXCursorPosition() const; 583 getRawYCursorPosition()584 inline float getRawYCursorPosition() const { return mRawYCursorPosition; } 585 586 float getYCursorPosition() const; 587 588 void setCursorPosition(float x, float y); 589 getRawTransform()590 inline const ui::Transform& getRawTransform() const { return mRawTransform; } 591 isValidCursorPosition(float x,float y)592 static inline bool isValidCursorPosition(float x, float y) { return !isnan(x) && !isnan(y); } 593 getDownTime()594 inline nsecs_t getDownTime() const { return mDownTime; } 595 setDownTime(nsecs_t downTime)596 inline void setDownTime(nsecs_t downTime) { mDownTime = downTime; } 597 getPointerCount()598 inline size_t getPointerCount() const { return mPointerProperties.size(); } 599 getPointerProperties(size_t pointerIndex)600 inline const PointerProperties* getPointerProperties(size_t pointerIndex) const { 601 return &mPointerProperties[pointerIndex]; 602 } 603 getPointerId(size_t pointerIndex)604 inline int32_t getPointerId(size_t pointerIndex) const { 605 return mPointerProperties[pointerIndex].id; 606 } 607 getToolType(size_t pointerIndex)608 inline int32_t getToolType(size_t pointerIndex) const { 609 return mPointerProperties[pointerIndex].toolType; 610 } 611 getEventTime()612 inline nsecs_t getEventTime() const { return mSampleEventTimes[getHistorySize()]; } 613 614 /** 615 * The actual raw pointer coords: whatever comes from the input device without any external 616 * transforms applied. 617 */ 618 const PointerCoords* getRawPointerCoords(size_t pointerIndex) const; 619 620 /** 621 * This is the raw axis value. However, for X/Y axes, this currently applies a "compat-raw" 622 * transform because many apps (incorrectly) assumed that raw == oriented-screen-space. 623 * "compat raw" is raw coordinates with screen rotation applied. 624 */ 625 float getRawAxisValue(int32_t axis, size_t pointerIndex) const; 626 getRawX(size_t pointerIndex)627 inline float getRawX(size_t pointerIndex) const { 628 return getRawAxisValue(AMOTION_EVENT_AXIS_X, pointerIndex); 629 } 630 getRawY(size_t pointerIndex)631 inline float getRawY(size_t pointerIndex) const { 632 return getRawAxisValue(AMOTION_EVENT_AXIS_Y, pointerIndex); 633 } 634 635 float getAxisValue(int32_t axis, size_t pointerIndex) const; 636 637 /** 638 * Get the X coordinate of the latest sample in this MotionEvent for pointer 'pointerIndex'. 639 * Identical to calling getHistoricalX(pointerIndex, getHistorySize()). 640 */ getX(size_t pointerIndex)641 inline float getX(size_t pointerIndex) const { 642 return getAxisValue(AMOTION_EVENT_AXIS_X, pointerIndex); 643 } 644 645 /** 646 * Get the Y coordinate of the latest sample in this MotionEvent for pointer 'pointerIndex'. 647 * Identical to calling getHistoricalX(pointerIndex, getHistorySize()). 648 */ getY(size_t pointerIndex)649 inline float getY(size_t pointerIndex) const { 650 return getAxisValue(AMOTION_EVENT_AXIS_Y, pointerIndex); 651 } 652 getPressure(size_t pointerIndex)653 inline float getPressure(size_t pointerIndex) const { 654 return getAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pointerIndex); 655 } 656 getSize(size_t pointerIndex)657 inline float getSize(size_t pointerIndex) const { 658 return getAxisValue(AMOTION_EVENT_AXIS_SIZE, pointerIndex); 659 } 660 getTouchMajor(size_t pointerIndex)661 inline float getTouchMajor(size_t pointerIndex) const { 662 return getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, pointerIndex); 663 } 664 getTouchMinor(size_t pointerIndex)665 inline float getTouchMinor(size_t pointerIndex) const { 666 return getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, pointerIndex); 667 } 668 getToolMajor(size_t pointerIndex)669 inline float getToolMajor(size_t pointerIndex) const { 670 return getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, pointerIndex); 671 } 672 getToolMinor(size_t pointerIndex)673 inline float getToolMinor(size_t pointerIndex) const { 674 return getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, pointerIndex); 675 } 676 getOrientation(size_t pointerIndex)677 inline float getOrientation(size_t pointerIndex) const { 678 return getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, pointerIndex); 679 } 680 getHistorySize()681 inline size_t getHistorySize() const { return mSampleEventTimes.size() - 1; } 682 getHistoricalEventTime(size_t historicalIndex)683 inline nsecs_t getHistoricalEventTime(size_t historicalIndex) const { 684 return mSampleEventTimes[historicalIndex]; 685 } 686 687 /** 688 * The actual raw pointer coords: whatever comes from the input device without any external 689 * transforms applied. 690 */ 691 const PointerCoords* getHistoricalRawPointerCoords( 692 size_t pointerIndex, size_t historicalIndex) const; 693 694 /** 695 * This is the raw axis value. However, for X/Y axes, this currently applies a "compat-raw" 696 * transform because many apps (incorrectly) assumed that raw == oriented-screen-space. 697 * "compat raw" is raw coordinates with screen rotation applied. 698 */ 699 float getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex, 700 size_t historicalIndex) const; 701 getHistoricalRawX(size_t pointerIndex,size_t historicalIndex)702 inline float getHistoricalRawX(size_t pointerIndex, size_t historicalIndex) const { 703 return getHistoricalRawAxisValue( 704 AMOTION_EVENT_AXIS_X, pointerIndex, historicalIndex); 705 } 706 getHistoricalRawY(size_t pointerIndex,size_t historicalIndex)707 inline float getHistoricalRawY(size_t pointerIndex, size_t historicalIndex) const { 708 return getHistoricalRawAxisValue( 709 AMOTION_EVENT_AXIS_Y, pointerIndex, historicalIndex); 710 } 711 712 float getHistoricalAxisValue(int32_t axis, size_t pointerIndex, size_t historicalIndex) const; 713 getHistoricalX(size_t pointerIndex,size_t historicalIndex)714 inline float getHistoricalX(size_t pointerIndex, size_t historicalIndex) const { 715 return getHistoricalAxisValue( 716 AMOTION_EVENT_AXIS_X, pointerIndex, historicalIndex); 717 } 718 getHistoricalY(size_t pointerIndex,size_t historicalIndex)719 inline float getHistoricalY(size_t pointerIndex, size_t historicalIndex) const { 720 return getHistoricalAxisValue( 721 AMOTION_EVENT_AXIS_Y, pointerIndex, historicalIndex); 722 } 723 getHistoricalPressure(size_t pointerIndex,size_t historicalIndex)724 inline float getHistoricalPressure(size_t pointerIndex, size_t historicalIndex) const { 725 return getHistoricalAxisValue( 726 AMOTION_EVENT_AXIS_PRESSURE, pointerIndex, historicalIndex); 727 } 728 getHistoricalSize(size_t pointerIndex,size_t historicalIndex)729 inline float getHistoricalSize(size_t pointerIndex, size_t historicalIndex) const { 730 return getHistoricalAxisValue( 731 AMOTION_EVENT_AXIS_SIZE, pointerIndex, historicalIndex); 732 } 733 getHistoricalTouchMajor(size_t pointerIndex,size_t historicalIndex)734 inline float getHistoricalTouchMajor(size_t pointerIndex, size_t historicalIndex) const { 735 return getHistoricalAxisValue( 736 AMOTION_EVENT_AXIS_TOUCH_MAJOR, pointerIndex, historicalIndex); 737 } 738 getHistoricalTouchMinor(size_t pointerIndex,size_t historicalIndex)739 inline float getHistoricalTouchMinor(size_t pointerIndex, size_t historicalIndex) const { 740 return getHistoricalAxisValue( 741 AMOTION_EVENT_AXIS_TOUCH_MINOR, pointerIndex, historicalIndex); 742 } 743 getHistoricalToolMajor(size_t pointerIndex,size_t historicalIndex)744 inline float getHistoricalToolMajor(size_t pointerIndex, size_t historicalIndex) const { 745 return getHistoricalAxisValue( 746 AMOTION_EVENT_AXIS_TOOL_MAJOR, pointerIndex, historicalIndex); 747 } 748 getHistoricalToolMinor(size_t pointerIndex,size_t historicalIndex)749 inline float getHistoricalToolMinor(size_t pointerIndex, size_t historicalIndex) const { 750 return getHistoricalAxisValue( 751 AMOTION_EVENT_AXIS_TOOL_MINOR, pointerIndex, historicalIndex); 752 } 753 getHistoricalOrientation(size_t pointerIndex,size_t historicalIndex)754 inline float getHistoricalOrientation(size_t pointerIndex, size_t historicalIndex) const { 755 return getHistoricalAxisValue( 756 AMOTION_EVENT_AXIS_ORIENTATION, pointerIndex, historicalIndex); 757 } 758 759 ssize_t findPointerIndex(int32_t pointerId) const; 760 761 void initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId, 762 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton, 763 int32_t flags, int32_t edgeFlags, int32_t metaState, int32_t buttonState, 764 MotionClassification classification, const ui::Transform& transform, 765 float xPrecision, float yPrecision, float rawXCursorPosition, 766 float rawYCursorPosition, const ui::Transform& rawTransform, nsecs_t downTime, 767 nsecs_t eventTime, size_t pointerCount, 768 const PointerProperties* pointerProperties, const PointerCoords* pointerCoords); 769 770 void copyFrom(const MotionEvent* other, bool keepHistory); 771 772 void addSample( 773 nsecs_t eventTime, 774 const PointerCoords* pointerCoords); 775 776 void offsetLocation(float xOffset, float yOffset); 777 778 void scale(float globalScaleFactor); 779 780 // Set 3x3 perspective matrix transformation. 781 // Matrix is in row-major form and compatible with SkMatrix. 782 void transform(const std::array<float, 9>& matrix); 783 784 // Apply 3x3 perspective matrix transformation only to content (do not modify mTransform). 785 // Matrix is in row-major form and compatible with SkMatrix. 786 void applyTransform(const std::array<float, 9>& matrix); 787 788 #ifdef __linux__ 789 status_t readFromParcel(Parcel* parcel); 790 status_t writeToParcel(Parcel* parcel) const; 791 #endif 792 793 static bool isTouchEvent(uint32_t source, int32_t action); isTouchEvent()794 inline bool isTouchEvent() const { 795 return isTouchEvent(mSource, mAction); 796 } 797 798 // Low-level accessors. getPointerProperties()799 inline const PointerProperties* getPointerProperties() const { 800 return mPointerProperties.data(); 801 } getSampleEventTimes()802 inline const nsecs_t* getSampleEventTimes() const { return mSampleEventTimes.data(); } getSamplePointerCoords()803 inline const PointerCoords* getSamplePointerCoords() const { 804 return mSamplePointerCoords.data(); 805 } 806 807 static const char* getLabel(int32_t axis); 808 static int32_t getAxisFromLabel(const char* label); 809 810 static std::string actionToString(int32_t action); 811 812 // MotionEvent will transform various axes in different ways, based on the source. For 813 // example, the x and y axes will not have any offsets/translations applied if it comes from a 814 // relative mouse device (since SOURCE_RELATIVE_MOUSE is a non-pointer source). These methods 815 // are used to apply these transformations for different axes. 816 static vec2 calculateTransformedXY(uint32_t source, const ui::Transform&, const vec2& xy); 817 static float calculateTransformedAxisValue(int32_t axis, uint32_t source, const ui::Transform&, 818 const PointerCoords&); 819 static PointerCoords calculateTransformedCoords(uint32_t source, const ui::Transform&, 820 const PointerCoords&); 821 822 protected: 823 int32_t mAction; 824 int32_t mActionButton; 825 int32_t mFlags; 826 int32_t mEdgeFlags; 827 int32_t mMetaState; 828 int32_t mButtonState; 829 MotionClassification mClassification; 830 ui::Transform mTransform; 831 float mXPrecision; 832 float mYPrecision; 833 float mRawXCursorPosition; 834 float mRawYCursorPosition; 835 ui::Transform mRawTransform; 836 nsecs_t mDownTime; 837 std::vector<PointerProperties> mPointerProperties; 838 std::vector<nsecs_t> mSampleEventTimes; 839 std::vector<PointerCoords> mSamplePointerCoords; 840 }; 841 842 std::ostream& operator<<(std::ostream& out, const MotionEvent& event); 843 844 /* 845 * Focus events. 846 */ 847 class FocusEvent : public InputEvent { 848 public: ~FocusEvent()849 virtual ~FocusEvent() {} 850 getType()851 virtual int32_t getType() const override { return AINPUT_EVENT_TYPE_FOCUS; } 852 getHasFocus()853 inline bool getHasFocus() const { return mHasFocus; } 854 855 void initialize(int32_t id, bool hasFocus); 856 857 void initialize(const FocusEvent& from); 858 859 protected: 860 bool mHasFocus; 861 }; 862 863 /* 864 * Capture events. 865 */ 866 class CaptureEvent : public InputEvent { 867 public: ~CaptureEvent()868 virtual ~CaptureEvent() {} 869 getType()870 virtual int32_t getType() const override { return AINPUT_EVENT_TYPE_CAPTURE; } 871 getPointerCaptureEnabled()872 inline bool getPointerCaptureEnabled() const { return mPointerCaptureEnabled; } 873 874 void initialize(int32_t id, bool pointerCaptureEnabled); 875 876 void initialize(const CaptureEvent& from); 877 878 protected: 879 bool mPointerCaptureEnabled; 880 }; 881 882 /* 883 * Drag events. 884 */ 885 class DragEvent : public InputEvent { 886 public: ~DragEvent()887 virtual ~DragEvent() {} 888 getType()889 virtual int32_t getType() const override { return AINPUT_EVENT_TYPE_DRAG; } 890 isExiting()891 inline bool isExiting() const { return mIsExiting; } 892 getX()893 inline float getX() const { return mX; } 894 getY()895 inline float getY() const { return mY; } 896 897 void initialize(int32_t id, float x, float y, bool isExiting); 898 899 void initialize(const DragEvent& from); 900 901 protected: 902 bool mIsExiting; 903 float mX, mY; 904 }; 905 906 /* 907 * Touch mode events. 908 */ 909 class TouchModeEvent : public InputEvent { 910 public: ~TouchModeEvent()911 virtual ~TouchModeEvent() {} 912 getType()913 virtual int32_t getType() const override { return AINPUT_EVENT_TYPE_TOUCH_MODE; } 914 isInTouchMode()915 inline bool isInTouchMode() const { return mIsInTouchMode; } 916 917 void initialize(int32_t id, bool isInTouchMode); 918 919 void initialize(const TouchModeEvent& from); 920 921 protected: 922 bool mIsInTouchMode; 923 }; 924 925 /** 926 * Base class for verified events. 927 * Do not create a VerifiedInputEvent explicitly. 928 * Use helper functions to create them from InputEvents. 929 */ 930 struct __attribute__((__packed__)) VerifiedInputEvent { 931 enum class Type : int32_t { 932 KEY = AINPUT_EVENT_TYPE_KEY, 933 MOTION = AINPUT_EVENT_TYPE_MOTION, 934 }; 935 936 Type type; 937 int32_t deviceId; 938 nsecs_t eventTimeNanos; 939 uint32_t source; 940 int32_t displayId; 941 }; 942 943 /** 944 * Same as KeyEvent, but only contains the data that can be verified. 945 * If you update this class, you must also update VerifiedKeyEvent.java 946 */ 947 struct __attribute__((__packed__)) VerifiedKeyEvent : public VerifiedInputEvent { 948 int32_t action; 949 int32_t flags; 950 nsecs_t downTimeNanos; 951 int32_t keyCode; 952 int32_t scanCode; 953 int32_t metaState; 954 int32_t repeatCount; 955 }; 956 957 /** 958 * Same as MotionEvent, but only contains the data that can be verified. 959 * If you update this class, you must also update VerifiedMotionEvent.java 960 */ 961 struct __attribute__((__packed__)) VerifiedMotionEvent : public VerifiedInputEvent { 962 float rawX; 963 float rawY; 964 int32_t actionMasked; 965 int32_t flags; 966 nsecs_t downTimeNanos; 967 int32_t metaState; 968 int32_t buttonState; 969 }; 970 971 VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event); 972 VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event); 973 974 /* 975 * Input event factory. 976 */ 977 class InputEventFactoryInterface { 978 protected: ~InputEventFactoryInterface()979 virtual ~InputEventFactoryInterface() { } 980 981 public: InputEventFactoryInterface()982 InputEventFactoryInterface() { } 983 984 virtual KeyEvent* createKeyEvent() = 0; 985 virtual MotionEvent* createMotionEvent() = 0; 986 virtual FocusEvent* createFocusEvent() = 0; 987 virtual CaptureEvent* createCaptureEvent() = 0; 988 virtual DragEvent* createDragEvent() = 0; 989 virtual TouchModeEvent* createTouchModeEvent() = 0; 990 }; 991 992 /* 993 * A simple input event factory implementation that uses a single preallocated instance 994 * of each type of input event that are reused for each request. 995 */ 996 class PreallocatedInputEventFactory : public InputEventFactoryInterface { 997 public: PreallocatedInputEventFactory()998 PreallocatedInputEventFactory() { } ~PreallocatedInputEventFactory()999 virtual ~PreallocatedInputEventFactory() { } 1000 createKeyEvent()1001 virtual KeyEvent* createKeyEvent() override { return &mKeyEvent; } createMotionEvent()1002 virtual MotionEvent* createMotionEvent() override { return &mMotionEvent; } createFocusEvent()1003 virtual FocusEvent* createFocusEvent() override { return &mFocusEvent; } createCaptureEvent()1004 virtual CaptureEvent* createCaptureEvent() override { return &mCaptureEvent; } createDragEvent()1005 virtual DragEvent* createDragEvent() override { return &mDragEvent; } createTouchModeEvent()1006 virtual TouchModeEvent* createTouchModeEvent() override { return &mTouchModeEvent; } 1007 1008 private: 1009 KeyEvent mKeyEvent; 1010 MotionEvent mMotionEvent; 1011 FocusEvent mFocusEvent; 1012 CaptureEvent mCaptureEvent; 1013 DragEvent mDragEvent; 1014 TouchModeEvent mTouchModeEvent; 1015 }; 1016 1017 /* 1018 * An input event factory implementation that maintains a pool of input events. 1019 */ 1020 class PooledInputEventFactory : public InputEventFactoryInterface { 1021 public: 1022 explicit PooledInputEventFactory(size_t maxPoolSize = 20); 1023 virtual ~PooledInputEventFactory(); 1024 1025 virtual KeyEvent* createKeyEvent() override; 1026 virtual MotionEvent* createMotionEvent() override; 1027 virtual FocusEvent* createFocusEvent() override; 1028 virtual CaptureEvent* createCaptureEvent() override; 1029 virtual DragEvent* createDragEvent() override; 1030 virtual TouchModeEvent* createTouchModeEvent() override; 1031 1032 void recycle(InputEvent* event); 1033 1034 private: 1035 const size_t mMaxPoolSize; 1036 1037 std::queue<std::unique_ptr<KeyEvent>> mKeyEventPool; 1038 std::queue<std::unique_ptr<MotionEvent>> mMotionEventPool; 1039 std::queue<std::unique_ptr<FocusEvent>> mFocusEventPool; 1040 std::queue<std::unique_ptr<CaptureEvent>> mCaptureEventPool; 1041 std::queue<std::unique_ptr<DragEvent>> mDragEventPool; 1042 std::queue<std::unique_ptr<TouchModeEvent>> mTouchModeEventPool; 1043 }; 1044 1045 /* 1046 * Describes a unique request to enable or disable Pointer Capture. 1047 */ 1048 struct PointerCaptureRequest { 1049 public: PointerCaptureRequestPointerCaptureRequest1050 inline PointerCaptureRequest() : enable(false), seq(0) {} PointerCaptureRequestPointerCaptureRequest1051 inline PointerCaptureRequest(bool enable, uint32_t seq) : enable(enable), seq(seq) {} 1052 inline bool operator==(const PointerCaptureRequest& other) const { 1053 return enable == other.enable && seq == other.seq; 1054 } 1055 explicit inline operator bool() const { return enable; } 1056 1057 // True iff this is a request to enable Pointer Capture. 1058 bool enable; 1059 1060 // The sequence number for the request. 1061 uint32_t seq; 1062 }; 1063 1064 } // namespace android 1065 1066 #endif // _LIBINPUT_INPUT_H 1067