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 #pragma once 18 19 #pragma GCC system_header 20 21 /** 22 * Native input event structures. 23 */ 24 25 #include <android/input.h> 26 #ifdef __linux__ 27 #include <android/os/IInputConstants.h> 28 #include <android/os/MotionEventFlag.h> 29 #endif 30 #include <android/os/PointerIconType.h> 31 #include <math.h> 32 #include <stdint.h> 33 #include <ui/LogicalDisplayId.h> 34 #include <ui/Transform.h> 35 #include <utils/BitSet.h> 36 #include <utils/Timers.h> 37 #include <array> 38 #include <limits> 39 #include <queue> 40 41 /* 42 * Additional private constants not defined in ndk/ui/input.h. 43 */ 44 enum { 45 46 /* This event was generated or modified by accessibility service. */ 47 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT = 48 android::os::IInputConstants::INPUT_EVENT_FLAG_IS_ACCESSIBILITY_EVENT, 49 50 /* Signifies that the key is being predispatched */ 51 AKEY_EVENT_FLAG_PREDISPATCH = 0x20000000, 52 53 /* Private control to determine when an app is tracking a key sequence. */ 54 AKEY_EVENT_FLAG_START_TRACKING = 0x40000000, 55 56 /* Key event is inconsistent with previously sent key events. */ 57 AKEY_EVENT_FLAG_TAINTED = android::os::IInputConstants::INPUT_EVENT_FLAG_TAINTED, 58 }; 59 60 enum { 61 // AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED is defined in include/android/input.h 62 /** 63 * This flag indicates that the window that received this motion event is partly 64 * or wholly obscured by another visible window above it. This flag is set to true 65 * even if the event did not directly pass through the obscured area. 66 * A security sensitive application can check this flag to identify situations in which 67 * a malicious application may have covered up part of its content for the purpose 68 * of misleading the user or hijacking touches. An appropriate response might be 69 * to drop the suspect touches or to take additional precautions to confirm the user's 70 * actual intent. 71 */ 72 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED = 73 static_cast<int32_t>(android::os::MotionEventFlag::WINDOW_IS_PARTIALLY_OBSCURED), 74 75 AMOTION_EVENT_FLAG_HOVER_EXIT_PENDING = 76 static_cast<int32_t>(android::os::MotionEventFlag::HOVER_EXIT_PENDING), 77 78 /** 79 * This flag indicates that the event has been generated by a gesture generator. It 80 * provides a hint to the GestureDetector to not apply any touch slop. 81 */ 82 AMOTION_EVENT_FLAG_IS_GENERATED_GESTURE = 83 static_cast<int32_t>(android::os::MotionEventFlag::IS_GENERATED_GESTURE), 84 85 /** 86 * This flag indicates that the event will not cause a focus change if it is directed to an 87 * unfocused window, even if it an ACTION_DOWN. This is typically used with pointer 88 * gestures to allow the user to direct gestures to an unfocused window without bringing it 89 * into focus. 90 */ 91 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE = 92 static_cast<int32_t>(android::os::MotionEventFlag::NO_FOCUS_CHANGE), 93 94 /** 95 * This event was injected from some AccessibilityService, which may be either an 96 * Accessibility Tool OR a service using that API for purposes other than assisting users 97 * with disabilities. 98 */ 99 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT = 100 static_cast<int32_t>(android::os::MotionEventFlag::IS_ACCESSIBILITY_EVENT), 101 102 /** 103 * This event was injected from an AccessibilityService with the 104 * AccessibilityServiceInfo#isAccessibilityTool property set to true. These services (known as 105 * "Accessibility Tools") are used to assist users with disabilities, so events from these 106 * services should be able to reach all Views including Views which set 107 * View#isAccessibilityDataSensitive to true. 108 */ 109 AMOTION_EVENT_FLAG_INJECTED_FROM_ACCESSIBILITY_TOOL = 110 static_cast<int32_t>(android::os::MotionEventFlag::INJECTED_FROM_ACCESSIBILITY_TOOL), 111 112 AMOTION_EVENT_FLAG_TARGET_ACCESSIBILITY_FOCUS = 113 static_cast<int32_t>(android::os::MotionEventFlag::TARGET_ACCESSIBILITY_FOCUS), 114 115 /* Motion event is inconsistent with previously sent motion events. */ 116 AMOTION_EVENT_FLAG_TAINTED = static_cast<int32_t>(android::os::MotionEventFlag::TAINTED), 117 118 /** Private flag, not used in Java. */ 119 AMOTION_EVENT_PRIVATE_FLAG_SUPPORTS_ORIENTATION = 120 static_cast<int32_t>(android::os::MotionEventFlag::PRIVATE_FLAG_SUPPORTS_ORIENTATION), 121 122 /** Private flag, not used in Java. */ 123 AMOTION_EVENT_PRIVATE_FLAG_SUPPORTS_DIRECTIONAL_ORIENTATION = static_cast<int32_t>( 124 android::os::MotionEventFlag::PRIVATE_FLAG_SUPPORTS_DIRECTIONAL_ORIENTATION), 125 126 /** Mask for all private flags that are not used in Java. */ 127 AMOTION_EVENT_PRIVATE_FLAG_MASK = AMOTION_EVENT_PRIVATE_FLAG_SUPPORTS_ORIENTATION | 128 AMOTION_EVENT_PRIVATE_FLAG_SUPPORTS_DIRECTIONAL_ORIENTATION, 129 }; 130 131 /** 132 * Allowed VerifiedKeyEvent flags. All other flags from KeyEvent do not get verified. 133 * These values must be kept in sync with VerifiedKeyEvent.java 134 */ 135 constexpr int32_t VERIFIED_KEY_EVENT_FLAGS = 136 AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; 137 138 /** 139 * Allowed VerifiedMotionEventFlags. All other flags from MotionEvent do not get verified. 140 * These values must be kept in sync with VerifiedMotionEvent.java 141 */ 142 constexpr int32_t VERIFIED_MOTION_EVENT_FLAGS = AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | 143 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED | AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; 144 145 /** 146 * This flag indicates that the point up event has been canceled. 147 * Typically this is used for palm event when the user has accidental touches. 148 * TODO(b/338143308): Add this to NDK 149 */ 150 constexpr int32_t AMOTION_EVENT_FLAG_CANCELED = 151 android::os::IInputConstants::INPUT_EVENT_FLAG_CANCELED; 152 153 enum { 154 /* 155 * Indicates that an input device has switches. 156 * This input source flag is hidden from the API because switches are only used by the system 157 * and applications have no way to interact with them. 158 */ 159 AINPUT_SOURCE_SWITCH = 0x80000000, 160 }; 161 162 enum { 163 /** 164 * Constants for LEDs. Hidden from the API since we don't actually expose a way to interact 165 * with LEDs to developers 166 * 167 * NOTE: If you add LEDs here, you must also add them to InputEventLabels.h 168 */ 169 170 ALED_NUM_LOCK = 0x00, 171 ALED_CAPS_LOCK = 0x01, 172 ALED_SCROLL_LOCK = 0x02, 173 ALED_COMPOSE = 0x03, 174 ALED_KANA = 0x04, 175 ALED_SLEEP = 0x05, 176 ALED_SUSPEND = 0x06, 177 ALED_MUTE = 0x07, 178 ALED_MISC = 0x08, 179 ALED_MAIL = 0x09, 180 ALED_CHARGING = 0x0a, 181 ALED_CONTROLLER_1 = 0x10, 182 ALED_CONTROLLER_2 = 0x11, 183 ALED_CONTROLLER_3 = 0x12, 184 ALED_CONTROLLER_4 = 0x13, 185 }; 186 187 /* Maximum number of controller LEDs we support */ 188 #define MAX_CONTROLLER_LEDS 4 189 190 /* 191 * Maximum number of pointers supported per motion event. 192 * Smallest number of pointers is 1. 193 * (We want at least 10 but some touch controllers obstensibly configured for 10 pointers 194 * will occasionally emit 11. There is not much harm making this constant bigger.) 195 */ 196 static constexpr size_t MAX_POINTERS = 16; 197 198 /* 199 * Maximum number of samples supported per motion event. 200 */ 201 #define MAX_SAMPLES UINT16_MAX 202 203 /* 204 * Maximum pointer id value supported in a motion event. 205 * Smallest pointer id is 0. 206 * (This is limited by our use of BitSet32 to track pointer assignments.) 207 */ 208 #define MAX_POINTER_ID 31 209 210 /* 211 * Number of high resolution scroll units for one detent (scroll wheel click), as defined in 212 * evdev. This is relevant when an input device is emitting REL_WHEEL_HI_RES or REL_HWHEEL_HI_RES 213 * events. 214 */ 215 constexpr int32_t kEvdevHighResScrollUnitsPerDetent = 120; 216 217 /* 218 * Declare a concrete type for the NDK's input event forward declaration. 219 */ 220 struct AInputEvent { ~AInputEventAInputEvent221 virtual ~AInputEvent() {} 222 bool operator==(const AInputEvent&) const = default; 223 }; 224 225 /* 226 * Declare a concrete type for the NDK's input device forward declaration. 227 */ 228 struct AInputDevice { ~AInputDeviceAInputDevice229 virtual ~AInputDevice() { } 230 }; 231 232 233 namespace android { 234 235 class Parcel; 236 237 /* 238 * Apply the given transform to the point without applying any translation/offset. 239 */ 240 vec2 transformWithoutTranslation(const ui::Transform& transform, const vec2& xy); 241 242 /* 243 * Transform an angle on the x-y plane. An angle of 0 radians corresponds to "north" or 244 * pointing upwards in the negative Y direction, a positive angle points towards the right, and a 245 * negative angle points towards the left. 246 * 247 * If the angle represents a direction that needs to be preserved, set isDirectional to true to get 248 * an output range of [-pi, pi]. If the angle's direction does not need to be preserved, set 249 * isDirectional to false to get an output range of [-pi/2, pi/2]. 250 */ 251 float transformAngle(const ui::Transform& transform, float angleRadians, bool isDirectional); 252 253 /** 254 * The type of the InputEvent. 255 * This should have 1:1 correspondence with the values of anonymous enum defined in input.h. 256 */ 257 enum class InputEventType { 258 KEY = AINPUT_EVENT_TYPE_KEY, 259 MOTION = AINPUT_EVENT_TYPE_MOTION, 260 FOCUS = AINPUT_EVENT_TYPE_FOCUS, 261 CAPTURE = AINPUT_EVENT_TYPE_CAPTURE, 262 DRAG = AINPUT_EVENT_TYPE_DRAG, 263 TOUCH_MODE = AINPUT_EVENT_TYPE_TOUCH_MODE, 264 ftl_first = KEY, 265 ftl_last = TOUCH_MODE, 266 // Used by LatencyTracker fuzzer 267 kMaxValue = ftl_last 268 }; 269 270 std::string inputEventSourceToString(int32_t source); 271 272 bool isFromSource(uint32_t source, uint32_t test); 273 274 /** 275 * The pointer tool type. 276 */ 277 enum class ToolType { 278 UNKNOWN = AMOTION_EVENT_TOOL_TYPE_UNKNOWN, 279 FINGER = AMOTION_EVENT_TOOL_TYPE_FINGER, 280 STYLUS = AMOTION_EVENT_TOOL_TYPE_STYLUS, 281 MOUSE = AMOTION_EVENT_TOOL_TYPE_MOUSE, 282 ERASER = AMOTION_EVENT_TOOL_TYPE_ERASER, 283 PALM = AMOTION_EVENT_TOOL_TYPE_PALM, 284 ftl_first = UNKNOWN, 285 ftl_last = PALM, 286 }; 287 288 /** 289 * The state of the key. This should have 1:1 correspondence with the values of anonymous enum 290 * defined in input.h 291 */ 292 enum class KeyState { 293 UNKNOWN = AKEY_STATE_UNKNOWN, 294 UP = AKEY_STATE_UP, 295 DOWN = AKEY_STATE_DOWN, 296 VIRTUAL = AKEY_STATE_VIRTUAL, 297 ftl_first = UNKNOWN, 298 ftl_last = VIRTUAL, 299 }; 300 301 /** 302 * The keyboard type. This should have 1:1 correspondence with the values of anonymous enum 303 * defined in input.h 304 */ 305 enum class KeyboardType { 306 NONE = AINPUT_KEYBOARD_TYPE_NONE, 307 NON_ALPHABETIC = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, 308 ALPHABETIC = AINPUT_KEYBOARD_TYPE_ALPHABETIC, 309 ftl_first = NONE, 310 ftl_last = ALPHABETIC, 311 }; 312 313 bool isStylusToolType(ToolType toolType); 314 315 struct PointerProperties; 316 317 bool isStylusEvent(uint32_t source, const std::vector<PointerProperties>& properties); 318 319 bool isStylusHoverEvent(uint32_t source, const std::vector<PointerProperties>& properties, 320 int32_t action); 321 322 bool isFromMouse(uint32_t source, ToolType tooltype); 323 324 bool isFromTouchpad(uint32_t source, ToolType tooltype); 325 326 bool isFromDrawingTablet(uint32_t source, ToolType tooltype); 327 328 bool isHoverAction(int32_t action); 329 330 bool isMouseOrTouchpad(uint32_t sources); 331 332 /* 333 * Flags that flow alongside events in the input dispatch system to help with certain 334 * policy decisions such as waking from device sleep. 335 * 336 * These flags are also defined in frameworks/base/core/java/android/view/WindowManagerPolicy.java. 337 */ 338 enum { 339 /* These flags originate in RawEvents and are generally set in the key map. 340 * NOTE: If you want a flag to be able to set in a keylayout file, then you must add it to 341 * InputEventLabels.h as well. */ 342 343 // Indicates that the event should wake the device. 344 POLICY_FLAG_WAKE = 0x00000001, 345 346 // Indicates that the key is virtual, such as a capacitive button, and should 347 // generate haptic feedback. Virtual keys may be suppressed for some time 348 // after a recent touch to prevent accidental activation of virtual keys adjacent 349 // to the touch screen during an edge swipe. 350 POLICY_FLAG_VIRTUAL = 0x00000002, 351 352 // Indicates that the key is the special function modifier. 353 POLICY_FLAG_FUNCTION = 0x00000004, 354 355 // Indicates that the key represents a special gesture that has been detected by 356 // the touch firmware or driver. Causes touch events from the same device to be canceled. 357 // This policy flag prevents key events from changing touch mode state. 358 POLICY_FLAG_GESTURE = 0x00000008, 359 360 // Indicates that key usage mapping represents a fallback mapping. 361 // Fallback mappings cannot be used to definitively determine whether a device 362 // supports a key code. For example, a HID device can report a key press 363 // as a HID usage code if it is not mapped to any linux key code in the kernel. 364 // However, we cannot know which HID usage codes that device supports from 365 // userspace through the evdev. We can use fallback mappings to convert HID 366 // usage codes to Android key codes without needing to know if a device can 367 // actually report the usage code. 368 POLICY_FLAG_FALLBACK_USAGE_MAPPING = 0x00000010, 369 370 POLICY_FLAG_RAW_MASK = 0x0000ffff, 371 372 POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY = 373 android::os::IInputConstants::POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, 374 375 POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY_TOOL = 376 android::os::IInputConstants::POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY_TOOL, 377 378 /* These flags are set by the input dispatcher. */ 379 380 // Indicates that the input event was injected. 381 POLICY_FLAG_INJECTED = 0x01000000, 382 383 // Indicates that the input event is from a trusted source such as a directly attached 384 // input device or an application with system-wide event injection permission. 385 POLICY_FLAG_TRUSTED = 0x02000000, 386 387 // Indicates that the input event has passed through an input filter. 388 POLICY_FLAG_FILTERED = 0x04000000, 389 390 // Disables automatic key repeating behavior. 391 POLICY_FLAG_DISABLE_KEY_REPEAT = 0x08000000, 392 393 /* These flags are set by the input reader policy as it intercepts each event. */ 394 395 // Indicates that the device was in an interactive state when the 396 // event was intercepted. 397 POLICY_FLAG_INTERACTIVE = 0x20000000, 398 399 // Indicates that the event should be dispatched to applications. 400 // The input event should still be sent to the InputDispatcher so that it can see all 401 // input events received include those that it will not deliver. 402 POLICY_FLAG_PASS_TO_USER = 0x40000000, 403 }; 404 405 /** 406 * Classifications of the current gesture, if available. 407 */ 408 enum class MotionClassification : uint8_t { 409 /** 410 * No classification is available. 411 */ 412 NONE = AMOTION_EVENT_CLASSIFICATION_NONE, 413 /** 414 * Too early to classify the current gesture. Need more events. Look for changes in the 415 * upcoming motion events. 416 */ 417 AMBIGUOUS_GESTURE = AMOTION_EVENT_CLASSIFICATION_AMBIGUOUS_GESTURE, 418 /** 419 * The current gesture likely represents a user intentionally exerting force on the touchscreen. 420 */ 421 DEEP_PRESS = AMOTION_EVENT_CLASSIFICATION_DEEP_PRESS, 422 /** 423 * The current gesture represents the user swiping with two fingers on a touchpad. 424 */ 425 TWO_FINGER_SWIPE = AMOTION_EVENT_CLASSIFICATION_TWO_FINGER_SWIPE, 426 /** 427 * The current gesture represents the user swiping with three or more fingers on a touchpad. 428 * Unlike two-finger swipes, these are only to be handled by the system UI, which is why they 429 * have a separate constant from two-finger swipes. 430 */ 431 MULTI_FINGER_SWIPE = AMOTION_EVENT_CLASSIFICATION_MULTI_FINGER_SWIPE, 432 /** 433 * The current gesture represents the user pinching with two fingers on a touchpad. The gesture 434 * is centered around the current cursor position. 435 */ 436 PINCH = AMOTION_EVENT_CLASSIFICATION_PINCH, 437 }; 438 439 /** 440 * String representation of MotionClassification 441 */ 442 const char* motionClassificationToString(MotionClassification classification); 443 444 /** 445 * Portion of FrameMetrics timeline of interest to input code. 446 */ 447 enum GraphicsTimeline : size_t { 448 /** Time when the app sent the buffer to SurfaceFlinger. */ 449 GPU_COMPLETED_TIME = 0, 450 451 /** Time when the frame was presented on the display */ 452 PRESENT_TIME = 1, 453 454 /** Total size of the 'GraphicsTimeline' array. Must always be last. */ 455 SIZE = 2 456 }; 457 458 /** 459 * Generator of unique numbers used to identify input events. 460 * 461 * Layout of ID: 462 * |--------------------------|---------------------------| 463 * | 2 bits for source | 30 bits for random number | 464 * |--------------------------|---------------------------| 465 */ 466 class IdGenerator { 467 private: 468 static constexpr uint32_t SOURCE_SHIFT = 30; 469 470 public: 471 // Used to divide integer space to ensure no conflict among these sources./ 472 enum class Source : int32_t { 473 INPUT_READER = static_cast<int32_t>(0x0u << SOURCE_SHIFT), 474 INPUT_DISPATCHER = static_cast<int32_t>(0x1u << SOURCE_SHIFT), 475 OTHER = static_cast<int32_t>(0x3u << SOURCE_SHIFT), // E.g. app injected events 476 }; 477 IdGenerator(Source source); 478 479 int32_t nextId() const; 480 481 // Extract source from given id. getSource(int32_t id)482 static inline Source getSource(int32_t id) { return static_cast<Source>(SOURCE_MASK & id); } 483 484 private: 485 const Source mSource; 486 487 static constexpr int32_t SOURCE_MASK = static_cast<int32_t>(0x3u << SOURCE_SHIFT); 488 }; 489 490 /** 491 * Invalid value for cursor position. Used for non-mouse events, tests and injected events. Don't 492 * use it for direct comparison with any other value, because NaN isn't equal to itself according to 493 * IEEE 754. Use isnan() instead to check if a cursor position is valid. 494 */ 495 constexpr float AMOTION_EVENT_INVALID_CURSOR_POSITION = std::numeric_limits<float>::quiet_NaN(); 496 497 /* 498 * Pointer coordinate data. 499 */ 500 struct PointerCoords { 501 enum { MAX_AXES = 30 }; // 30 so that sizeof(PointerCoords) == 136 502 503 // Bitfield of axes that are present in this structure. 504 uint64_t bits __attribute__((aligned(8))); 505 506 // Values of axes that are stored in this structure packed in order by axis id 507 // for each axis that is present in the structure according to 'bits'. 508 std::array<float, MAX_AXES> values; 509 510 // Whether these coordinate data were generated by resampling. 511 bool isResampled; 512 513 static_assert(sizeof(bool) == 1); // Ensure padding is correctly sized. 514 uint8_t empty[7]; 515 clearPointerCoords516 inline void clear() { 517 BitSet64::clear(bits); 518 isResampled = false; 519 } 520 isEmptyPointerCoords521 bool isEmpty() const { 522 return BitSet64::isEmpty(bits); 523 } 524 525 float getAxisValue(int32_t axis) const; 526 status_t setAxisValue(int32_t axis, float value); 527 528 // Scale the pointer coordinates according to a global scale and a 529 // window scale. The global scale will be applied to TOUCH/TOOL_MAJOR/MINOR 530 // axes, however the window scaling will not. 531 void scale(float globalScale, float windowXScale, float windowYScale); 532 getXPointerCoords533 inline float getX() const { 534 return getAxisValue(AMOTION_EVENT_AXIS_X); 535 } 536 getYPointerCoords537 inline float getY() const { 538 return getAxisValue(AMOTION_EVENT_AXIS_Y); 539 } 540 getXYValuePointerCoords541 vec2 getXYValue() const { return vec2(getX(), getY()); } 542 543 status_t readFromParcel(Parcel* parcel); 544 status_t writeToParcel(Parcel* parcel) const; 545 546 bool operator==(const PointerCoords& other) const; 547 inline bool operator!=(const PointerCoords& other) const { 548 return !(*this == other); 549 } 550 copyFromPointerCoords551 inline void copyFrom(const PointerCoords& other) { *this = other; } 552 PointerCoords& operator=(const PointerCoords&) = default; 553 554 private: 555 void tooManyAxes(int axis); 556 }; 557 558 /* 559 * Pointer property data. 560 */ 561 struct PointerProperties { 562 // The id of the pointer. 563 int32_t id; 564 565 // The pointer tool type. 566 ToolType toolType; 567 clearPointerProperties568 inline void clear() { 569 id = -1; 570 toolType = ToolType::UNKNOWN; 571 } 572 573 bool operator==(const PointerProperties& other) const = default; 574 inline bool operator!=(const PointerProperties& other) const { 575 return !(*this == other); 576 } 577 578 PointerProperties& operator=(const PointerProperties&) = default; 579 }; 580 581 std::ostream& operator<<(std::ostream& out, const PointerProperties& properties); 582 583 // TODO(b/211379801) : Use a strong type from ftl/mixins.h instead 584 using DeviceId = int32_t; 585 586 /* 587 * Input events. 588 */ 589 class InputEvent : public AInputEvent { 590 public: ~InputEvent()591 virtual ~InputEvent() { } 592 593 virtual InputEventType getType() const = 0; 594 getId()595 inline int32_t getId() const { return mId; } 596 getDeviceId()597 inline DeviceId getDeviceId() const { return mDeviceId; } 598 getSource()599 inline uint32_t getSource() const { return mSource; } 600 setSource(uint32_t source)601 inline void setSource(uint32_t source) { mSource = source; } 602 getDisplayId()603 inline ui::LogicalDisplayId getDisplayId() const { return mDisplayId; } 604 setDisplayId(ui::LogicalDisplayId displayId)605 inline void setDisplayId(ui::LogicalDisplayId displayId) { mDisplayId = displayId; } 606 getHmac()607 inline std::array<uint8_t, 32> getHmac() const { return mHmac; } 608 609 static int32_t nextId(); 610 611 bool operator==(const InputEvent&) const = default; 612 613 protected: 614 void initialize(int32_t id, DeviceId deviceId, uint32_t source, ui::LogicalDisplayId displayId, 615 std::array<uint8_t, 32> hmac); 616 617 void initialize(const InputEvent& from); 618 619 int32_t mId; 620 DeviceId mDeviceId; 621 uint32_t mSource; 622 ui::LogicalDisplayId mDisplayId{ui::LogicalDisplayId::INVALID}; 623 std::array<uint8_t, 32> mHmac; 624 }; 625 626 std::ostream& operator<<(std::ostream& out, const InputEvent& event); 627 628 /* 629 * Key events. 630 */ 631 class KeyEvent : public InputEvent { 632 public: ~KeyEvent()633 virtual ~KeyEvent() { } 634 getType()635 InputEventType getType() const override { return InputEventType::KEY; } 636 getAction()637 inline int32_t getAction() const { return mAction; } 638 getFlags()639 inline int32_t getFlags() const { return mFlags; } 640 setFlags(int32_t flags)641 inline void setFlags(int32_t flags) { mFlags = flags; } 642 getKeyCode()643 inline int32_t getKeyCode() const { return mKeyCode; } 644 getScanCode()645 inline int32_t getScanCode() const { return mScanCode; } 646 getMetaState()647 inline int32_t getMetaState() const { return mMetaState; } 648 getRepeatCount()649 inline int32_t getRepeatCount() const { return mRepeatCount; } 650 getDownTime()651 inline nsecs_t getDownTime() const { return mDownTime; } 652 getEventTime()653 inline nsecs_t getEventTime() const { return mEventTime; } 654 655 static const char* getLabel(int32_t keyCode); 656 static std::optional<int> getKeyCodeFromLabel(const char* label); 657 658 void initialize(int32_t id, DeviceId deviceId, uint32_t source, ui::LogicalDisplayId displayId, 659 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags, int32_t keyCode, 660 int32_t scanCode, int32_t metaState, int32_t repeatCount, nsecs_t downTime, 661 nsecs_t eventTime); 662 void initialize(const KeyEvent& from); 663 664 static const char* actionToString(int32_t action); 665 666 bool operator==(const KeyEvent&) const = default; 667 668 protected: 669 int32_t mAction; 670 int32_t mFlags; 671 int32_t mKeyCode; 672 int32_t mScanCode; 673 int32_t mMetaState; 674 int32_t mRepeatCount; 675 nsecs_t mDownTime; 676 nsecs_t mEventTime; 677 }; 678 679 std::ostream& operator<<(std::ostream& out, const KeyEvent& event); 680 681 /* 682 * Motion events. 683 */ 684 class MotionEvent : public InputEvent { 685 public: ~MotionEvent()686 virtual ~MotionEvent() { } 687 getType()688 InputEventType getType() const override { return InputEventType::MOTION; } 689 getAction()690 inline int32_t getAction() const { return mAction; } 691 getActionMasked(int32_t action)692 static int32_t getActionMasked(int32_t action) { return action & AMOTION_EVENT_ACTION_MASK; } 693 getActionMasked()694 inline int32_t getActionMasked() const { return getActionMasked(mAction); } 695 getActionIndex(int32_t action)696 static uint8_t getActionIndex(int32_t action) { 697 return (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> 698 AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; 699 } 700 getActionIndex()701 inline int32_t getActionIndex() const { return getActionIndex(mAction); } 702 setAction(int32_t action)703 inline void setAction(int32_t action) { mAction = action; } 704 getFlags()705 inline int32_t getFlags() const { return mFlags; } 706 setFlags(int32_t flags)707 inline void setFlags(int32_t flags) { mFlags = flags; } 708 getEdgeFlags()709 inline int32_t getEdgeFlags() const { return mEdgeFlags; } 710 setEdgeFlags(int32_t edgeFlags)711 inline void setEdgeFlags(int32_t edgeFlags) { mEdgeFlags = edgeFlags; } 712 getMetaState()713 inline int32_t getMetaState() const { return mMetaState; } 714 setMetaState(int32_t metaState)715 inline void setMetaState(int32_t metaState) { mMetaState = metaState; } 716 getButtonState()717 inline int32_t getButtonState() const { return mButtonState; } 718 setButtonState(int32_t buttonState)719 inline void setButtonState(int32_t buttonState) { mButtonState = buttonState; } 720 getClassification()721 inline MotionClassification getClassification() const { return mClassification; } 722 getActionButton()723 inline int32_t getActionButton() const { return mActionButton; } 724 setActionButton(int32_t button)725 inline void setActionButton(int32_t button) { mActionButton = button; } 726 getTransform()727 inline const ui::Transform& getTransform() const { return mTransform; } 728 729 std::optional<ui::Rotation> getSurfaceRotation() const; 730 getXPrecision()731 inline float getXPrecision() const { return mXPrecision; } 732 getYPrecision()733 inline float getYPrecision() const { return mYPrecision; } 734 getRawXCursorPosition()735 inline float getRawXCursorPosition() const { return mRawXCursorPosition; } 736 737 float getXCursorPosition() const; 738 getRawYCursorPosition()739 inline float getRawYCursorPosition() const { return mRawYCursorPosition; } 740 741 float getYCursorPosition() const; 742 743 void setCursorPosition(float x, float y); 744 getRawTransform()745 inline const ui::Transform& getRawTransform() const { return mRawTransform; } 746 isValidCursorPosition(float x,float y)747 static inline bool isValidCursorPosition(float x, float y) { return !isnan(x) && !isnan(y); } 748 getDownTime()749 inline nsecs_t getDownTime() const { return mDownTime; } 750 setDownTime(nsecs_t downTime)751 inline void setDownTime(nsecs_t downTime) { mDownTime = downTime; } 752 getPointerCount()753 inline size_t getPointerCount() const { return mPointerProperties.size(); } 754 getPointerProperties(size_t pointerIndex)755 inline const PointerProperties* getPointerProperties(size_t pointerIndex) const { 756 return &mPointerProperties[pointerIndex]; 757 } 758 getPointerId(size_t pointerIndex)759 inline int32_t getPointerId(size_t pointerIndex) const { 760 return mPointerProperties[pointerIndex].id; 761 } 762 getToolType(size_t pointerIndex)763 inline ToolType getToolType(size_t pointerIndex) const { 764 return mPointerProperties[pointerIndex].toolType; 765 } 766 getEventTime()767 inline nsecs_t getEventTime() const { return mSampleEventTimes[getHistorySize()]; } 768 769 /** 770 * The actual raw pointer coords: whatever comes from the input device without any external 771 * transforms applied. 772 */ 773 const PointerCoords* getRawPointerCoords(size_t pointerIndex) const; 774 775 /** 776 * This is the raw axis value. However, for X/Y axes, this currently applies a "compat-raw" 777 * transform because many apps (incorrectly) assumed that raw == oriented-screen-space. 778 * "compat raw" is raw coordinates with screen rotation applied. 779 */ 780 float getRawAxisValue(int32_t axis, size_t pointerIndex) const; 781 getRawX(size_t pointerIndex)782 inline float getRawX(size_t pointerIndex) const { 783 return getRawAxisValue(AMOTION_EVENT_AXIS_X, pointerIndex); 784 } 785 getRawY(size_t pointerIndex)786 inline float getRawY(size_t pointerIndex) const { 787 return getRawAxisValue(AMOTION_EVENT_AXIS_Y, pointerIndex); 788 } 789 790 float getAxisValue(int32_t axis, size_t pointerIndex) const; 791 792 /** 793 * Get the X coordinate of the latest sample in this MotionEvent for pointer 'pointerIndex'. 794 * Identical to calling getHistoricalX(pointerIndex, getHistorySize()). 795 */ getX(size_t pointerIndex)796 inline float getX(size_t pointerIndex) const { 797 return getAxisValue(AMOTION_EVENT_AXIS_X, pointerIndex); 798 } 799 800 /** 801 * Get the Y coordinate of the latest sample in this MotionEvent for pointer 'pointerIndex'. 802 * Identical to calling getHistoricalX(pointerIndex, getHistorySize()). 803 */ getY(size_t pointerIndex)804 inline float getY(size_t pointerIndex) const { 805 return getAxisValue(AMOTION_EVENT_AXIS_Y, pointerIndex); 806 } 807 getPressure(size_t pointerIndex)808 inline float getPressure(size_t pointerIndex) const { 809 return getAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pointerIndex); 810 } 811 getSize(size_t pointerIndex)812 inline float getSize(size_t pointerIndex) const { 813 return getAxisValue(AMOTION_EVENT_AXIS_SIZE, pointerIndex); 814 } 815 getTouchMajor(size_t pointerIndex)816 inline float getTouchMajor(size_t pointerIndex) const { 817 return getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, pointerIndex); 818 } 819 getTouchMinor(size_t pointerIndex)820 inline float getTouchMinor(size_t pointerIndex) const { 821 return getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, pointerIndex); 822 } 823 getToolMajor(size_t pointerIndex)824 inline float getToolMajor(size_t pointerIndex) const { 825 return getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, pointerIndex); 826 } 827 getToolMinor(size_t pointerIndex)828 inline float getToolMinor(size_t pointerIndex) const { 829 return getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, pointerIndex); 830 } 831 getOrientation(size_t pointerIndex)832 inline float getOrientation(size_t pointerIndex) const { 833 return getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, pointerIndex); 834 } 835 getHistorySize()836 inline size_t getHistorySize() const { return mSampleEventTimes.size() - 1; } 837 getHistoricalEventTime(size_t historicalIndex)838 inline nsecs_t getHistoricalEventTime(size_t historicalIndex) const { 839 return mSampleEventTimes[historicalIndex]; 840 } 841 842 /** 843 * The actual raw pointer coords: whatever comes from the input device without any external 844 * transforms applied. 845 */ 846 const PointerCoords* getHistoricalRawPointerCoords( 847 size_t pointerIndex, size_t historicalIndex) const; 848 849 /** 850 * This is the raw axis value. However, for X/Y axes, this currently applies a "compat-raw" 851 * transform because many apps (incorrectly) assumed that raw == oriented-screen-space. 852 * "compat raw" is raw coordinates with screen rotation applied. 853 */ 854 float getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex, 855 size_t historicalIndex) const; 856 getHistoricalRawX(size_t pointerIndex,size_t historicalIndex)857 inline float getHistoricalRawX(size_t pointerIndex, size_t historicalIndex) const { 858 return getHistoricalRawAxisValue( 859 AMOTION_EVENT_AXIS_X, pointerIndex, historicalIndex); 860 } 861 getHistoricalRawY(size_t pointerIndex,size_t historicalIndex)862 inline float getHistoricalRawY(size_t pointerIndex, size_t historicalIndex) const { 863 return getHistoricalRawAxisValue( 864 AMOTION_EVENT_AXIS_Y, pointerIndex, historicalIndex); 865 } 866 867 float getHistoricalAxisValue(int32_t axis, size_t pointerIndex, size_t historicalIndex) const; 868 getHistoricalX(size_t pointerIndex,size_t historicalIndex)869 inline float getHistoricalX(size_t pointerIndex, size_t historicalIndex) const { 870 return getHistoricalAxisValue( 871 AMOTION_EVENT_AXIS_X, pointerIndex, historicalIndex); 872 } 873 getHistoricalY(size_t pointerIndex,size_t historicalIndex)874 inline float getHistoricalY(size_t pointerIndex, size_t historicalIndex) const { 875 return getHistoricalAxisValue( 876 AMOTION_EVENT_AXIS_Y, pointerIndex, historicalIndex); 877 } 878 getHistoricalPressure(size_t pointerIndex,size_t historicalIndex)879 inline float getHistoricalPressure(size_t pointerIndex, size_t historicalIndex) const { 880 return getHistoricalAxisValue( 881 AMOTION_EVENT_AXIS_PRESSURE, pointerIndex, historicalIndex); 882 } 883 getHistoricalSize(size_t pointerIndex,size_t historicalIndex)884 inline float getHistoricalSize(size_t pointerIndex, size_t historicalIndex) const { 885 return getHistoricalAxisValue( 886 AMOTION_EVENT_AXIS_SIZE, pointerIndex, historicalIndex); 887 } 888 getHistoricalTouchMajor(size_t pointerIndex,size_t historicalIndex)889 inline float getHistoricalTouchMajor(size_t pointerIndex, size_t historicalIndex) const { 890 return getHistoricalAxisValue( 891 AMOTION_EVENT_AXIS_TOUCH_MAJOR, pointerIndex, historicalIndex); 892 } 893 getHistoricalTouchMinor(size_t pointerIndex,size_t historicalIndex)894 inline float getHistoricalTouchMinor(size_t pointerIndex, size_t historicalIndex) const { 895 return getHistoricalAxisValue( 896 AMOTION_EVENT_AXIS_TOUCH_MINOR, pointerIndex, historicalIndex); 897 } 898 getHistoricalToolMajor(size_t pointerIndex,size_t historicalIndex)899 inline float getHistoricalToolMajor(size_t pointerIndex, size_t historicalIndex) const { 900 return getHistoricalAxisValue( 901 AMOTION_EVENT_AXIS_TOOL_MAJOR, pointerIndex, historicalIndex); 902 } 903 getHistoricalToolMinor(size_t pointerIndex,size_t historicalIndex)904 inline float getHistoricalToolMinor(size_t pointerIndex, size_t historicalIndex) const { 905 return getHistoricalAxisValue( 906 AMOTION_EVENT_AXIS_TOOL_MINOR, pointerIndex, historicalIndex); 907 } 908 getHistoricalOrientation(size_t pointerIndex,size_t historicalIndex)909 inline float getHistoricalOrientation(size_t pointerIndex, size_t historicalIndex) const { 910 return getHistoricalAxisValue( 911 AMOTION_EVENT_AXIS_ORIENTATION, pointerIndex, historicalIndex); 912 } 913 isResampled(size_t pointerIndex,size_t historicalIndex)914 inline bool isResampled(size_t pointerIndex, size_t historicalIndex) const { 915 return getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->isResampled; 916 } 917 918 ssize_t findPointerIndex(int32_t pointerId) const; 919 920 void initialize(int32_t id, DeviceId deviceId, uint32_t source, ui::LogicalDisplayId displayId, 921 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton, 922 int32_t flags, int32_t edgeFlags, int32_t metaState, int32_t buttonState, 923 MotionClassification classification, const ui::Transform& transform, 924 float xPrecision, float yPrecision, float rawXCursorPosition, 925 float rawYCursorPosition, const ui::Transform& rawTransform, nsecs_t downTime, 926 nsecs_t eventTime, size_t pointerCount, 927 const PointerProperties* pointerProperties, const PointerCoords* pointerCoords); 928 929 void copyFrom(const MotionEvent* other, bool keepHistory); 930 931 // Initialize this event by keeping only the pointers from "other" that are in splitPointerIds. 932 void splitFrom(const MotionEvent& other, std::bitset<MAX_POINTER_ID + 1> splitPointerIds, 933 int32_t newEventId); 934 935 void addSample(nsecs_t eventTime, const PointerCoords* pointerCoords, int32_t eventId); 936 937 void offsetLocation(float xOffset, float yOffset); 938 939 /** 940 * Get the X offset of this motion event relative to the origin of the raw coordinate space. 941 * 942 * In practice, this is the delta that was added to the raw screen coordinates (i.e. in logical 943 * display space) to adjust for the absolute position of the containing windows and views. 944 */ 945 float getRawXOffset() const; 946 947 /** 948 * Get the Y offset of this motion event relative to the origin of the raw coordinate space. 949 * 950 * In practice, this is the delta that was added to the raw screen coordinates (i.e. in logical 951 * display space) to adjust for the absolute position of the containing windows and views. 952 */ 953 float getRawYOffset() const; 954 955 void scale(float globalScaleFactor); 956 957 // Set 3x3 perspective matrix transformation. 958 // Matrix is in row-major form and compatible with SkMatrix. 959 void transform(const std::array<float, 9>& matrix); 960 961 // Apply 3x3 perspective matrix transformation only to content (do not modify mTransform). 962 // Matrix is in row-major form and compatible with SkMatrix. 963 void applyTransform(const std::array<float, 9>& matrix); 964 965 status_t readFromParcel(Parcel* parcel); 966 status_t writeToParcel(Parcel* parcel) const; 967 968 static bool isTouchEvent(uint32_t source, int32_t action); isTouchEvent()969 inline bool isTouchEvent() const { 970 return isTouchEvent(mSource, mAction); 971 } 972 973 // Low-level accessors. getPointerProperties()974 inline const PointerProperties* getPointerProperties() const { 975 return mPointerProperties.data(); 976 } getSampleEventTimes()977 inline const nsecs_t* getSampleEventTimes() const { return mSampleEventTimes.data(); } getSamplePointerCoords()978 inline const PointerCoords* getSamplePointerCoords() const { 979 return mSamplePointerCoords.data(); 980 } 981 982 static const char* getLabel(int32_t axis); 983 static std::optional<int> getAxisFromLabel(const char* label); 984 985 static std::string actionToString(int32_t action); 986 987 static std::tuple<int32_t /*action*/, std::vector<PointerProperties>, 988 std::vector<PointerCoords>> 989 split(int32_t action, int32_t flags, int32_t historySize, const std::vector<PointerProperties>&, 990 const std::vector<PointerCoords>&, std::bitset<MAX_POINTER_ID + 1> splitPointerIds); 991 992 // MotionEvent will transform various axes in different ways, based on the source. For 993 // example, the x and y axes will not have any offsets/translations applied if it comes from a 994 // relative mouse device (since SOURCE_RELATIVE_MOUSE is a non-pointer source). These methods 995 // are used to apply these transformations for different axes. 996 static vec2 calculateTransformedXY(uint32_t source, const ui::Transform&, const vec2& xy); 997 static float calculateTransformedAxisValue(int32_t axis, uint32_t source, int32_t flags, 998 const ui::Transform&, const PointerCoords&); 999 static void calculateTransformedCoordsInPlace(PointerCoords& coords, uint32_t source, 1000 int32_t flags, const ui::Transform&); 1001 static PointerCoords calculateTransformedCoords(uint32_t source, int32_t flags, 1002 const ui::Transform&, const PointerCoords&); 1003 // The rounding precision for transformed motion events. 1004 static constexpr float ROUNDING_PRECISION = 0.001f; 1005 1006 bool operator==(const MotionEvent&) const; 1007 inline bool operator!=(const MotionEvent& o) const { return !(*this == o); }; 1008 1009 protected: 1010 int32_t mAction; 1011 int32_t mActionButton; 1012 int32_t mFlags; 1013 int32_t mEdgeFlags; 1014 int32_t mMetaState; 1015 int32_t mButtonState; 1016 MotionClassification mClassification; 1017 ui::Transform mTransform; 1018 float mXPrecision; 1019 float mYPrecision; 1020 float mRawXCursorPosition; 1021 float mRawYCursorPosition; 1022 ui::Transform mRawTransform; 1023 nsecs_t mDownTime; 1024 std::vector<PointerProperties> mPointerProperties; 1025 std::vector<nsecs_t> mSampleEventTimes; 1026 std::vector<PointerCoords> mSamplePointerCoords; 1027 1028 private: 1029 /** 1030 * Create a human-readable string representation of the event's data for debugging purposes. 1031 * 1032 * Unlike operator<<, this method does not assume that the event data is valid or consistent, or 1033 * call any accessor methods that might themselves call safeDump in the case of invalid data. 1034 */ 1035 std::string safeDump() const; 1036 }; 1037 1038 std::ostream& operator<<(std::ostream& out, const MotionEvent& event); 1039 1040 /* 1041 * Focus events. 1042 */ 1043 class FocusEvent : public InputEvent { 1044 public: ~FocusEvent()1045 virtual ~FocusEvent() {} 1046 getType()1047 InputEventType getType() const override { return InputEventType::FOCUS; } 1048 getHasFocus()1049 inline bool getHasFocus() const { return mHasFocus; } 1050 1051 void initialize(int32_t id, bool hasFocus); 1052 1053 void initialize(const FocusEvent& from); 1054 1055 protected: 1056 bool mHasFocus; 1057 }; 1058 1059 /* 1060 * Capture events. 1061 */ 1062 class CaptureEvent : public InputEvent { 1063 public: ~CaptureEvent()1064 virtual ~CaptureEvent() {} 1065 getType()1066 InputEventType getType() const override { return InputEventType::CAPTURE; } 1067 getPointerCaptureEnabled()1068 inline bool getPointerCaptureEnabled() const { return mPointerCaptureEnabled; } 1069 1070 void initialize(int32_t id, bool pointerCaptureEnabled); 1071 1072 void initialize(const CaptureEvent& from); 1073 1074 protected: 1075 bool mPointerCaptureEnabled; 1076 }; 1077 1078 /* 1079 * Drag events. 1080 */ 1081 class DragEvent : public InputEvent { 1082 public: ~DragEvent()1083 virtual ~DragEvent() {} 1084 getType()1085 InputEventType getType() const override { return InputEventType::DRAG; } 1086 isExiting()1087 inline bool isExiting() const { return mIsExiting; } 1088 getX()1089 inline float getX() const { return mX; } 1090 getY()1091 inline float getY() const { return mY; } 1092 1093 void initialize(int32_t id, float x, float y, bool isExiting); 1094 1095 void initialize(const DragEvent& from); 1096 1097 protected: 1098 bool mIsExiting; 1099 float mX, mY; 1100 }; 1101 1102 /* 1103 * Touch mode events. 1104 */ 1105 class TouchModeEvent : public InputEvent { 1106 public: ~TouchModeEvent()1107 virtual ~TouchModeEvent() {} 1108 getType()1109 InputEventType getType() const override { return InputEventType::TOUCH_MODE; } 1110 isInTouchMode()1111 inline bool isInTouchMode() const { return mIsInTouchMode; } 1112 1113 void initialize(int32_t id, bool isInTouchMode); 1114 1115 void initialize(const TouchModeEvent& from); 1116 1117 protected: 1118 bool mIsInTouchMode; 1119 }; 1120 1121 /** 1122 * Base class for verified events. 1123 * Do not create a VerifiedInputEvent explicitly. 1124 * Use helper functions to create them from InputEvents. 1125 */ 1126 struct __attribute__((__packed__)) VerifiedInputEvent { 1127 enum class Type : int32_t { 1128 KEY = AINPUT_EVENT_TYPE_KEY, 1129 MOTION = AINPUT_EVENT_TYPE_MOTION, 1130 }; 1131 1132 Type type; 1133 DeviceId deviceId; 1134 nsecs_t eventTimeNanos; 1135 uint32_t source; 1136 ui::LogicalDisplayId displayId; 1137 }; 1138 1139 /** 1140 * Same as KeyEvent, but only contains the data that can be verified. 1141 * If you update this class, you must also update VerifiedKeyEvent.java 1142 */ 1143 struct __attribute__((__packed__)) VerifiedKeyEvent : public VerifiedInputEvent { 1144 int32_t action; 1145 int32_t flags; 1146 nsecs_t downTimeNanos; 1147 int32_t keyCode; 1148 int32_t scanCode; 1149 int32_t metaState; 1150 int32_t repeatCount; 1151 }; 1152 1153 /** 1154 * Same as MotionEvent, but only contains the data that can be verified. 1155 * If you update this class, you must also update VerifiedMotionEvent.java 1156 */ 1157 struct __attribute__((__packed__)) VerifiedMotionEvent : public VerifiedInputEvent { 1158 float rawX; 1159 float rawY; 1160 int32_t actionMasked; 1161 int32_t flags; 1162 nsecs_t downTimeNanos; 1163 int32_t metaState; 1164 int32_t buttonState; 1165 }; 1166 1167 VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event); 1168 VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event); 1169 1170 /* 1171 * Input event factory. 1172 */ 1173 class InputEventFactoryInterface { 1174 protected: ~InputEventFactoryInterface()1175 virtual ~InputEventFactoryInterface() { } 1176 1177 public: InputEventFactoryInterface()1178 InputEventFactoryInterface() { } 1179 1180 virtual KeyEvent* createKeyEvent() = 0; 1181 virtual MotionEvent* createMotionEvent() = 0; 1182 virtual FocusEvent* createFocusEvent() = 0; 1183 virtual CaptureEvent* createCaptureEvent() = 0; 1184 virtual DragEvent* createDragEvent() = 0; 1185 virtual TouchModeEvent* createTouchModeEvent() = 0; 1186 }; 1187 1188 /* 1189 * A simple input event factory implementation that uses a single preallocated instance 1190 * of each type of input event that are reused for each request. 1191 */ 1192 class PreallocatedInputEventFactory : public InputEventFactoryInterface { 1193 public: PreallocatedInputEventFactory()1194 PreallocatedInputEventFactory() { } ~PreallocatedInputEventFactory()1195 virtual ~PreallocatedInputEventFactory() { } 1196 createKeyEvent()1197 virtual KeyEvent* createKeyEvent() override { return &mKeyEvent; } createMotionEvent()1198 virtual MotionEvent* createMotionEvent() override { return &mMotionEvent; } createFocusEvent()1199 virtual FocusEvent* createFocusEvent() override { return &mFocusEvent; } createCaptureEvent()1200 virtual CaptureEvent* createCaptureEvent() override { return &mCaptureEvent; } createDragEvent()1201 virtual DragEvent* createDragEvent() override { return &mDragEvent; } createTouchModeEvent()1202 virtual TouchModeEvent* createTouchModeEvent() override { return &mTouchModeEvent; } 1203 1204 private: 1205 KeyEvent mKeyEvent; 1206 MotionEvent mMotionEvent; 1207 FocusEvent mFocusEvent; 1208 CaptureEvent mCaptureEvent; 1209 DragEvent mDragEvent; 1210 TouchModeEvent mTouchModeEvent; 1211 }; 1212 1213 /* 1214 * An input event factory implementation that maintains a pool of input events. 1215 */ 1216 class PooledInputEventFactory : public InputEventFactoryInterface { 1217 public: 1218 explicit PooledInputEventFactory(size_t maxPoolSize = 20); 1219 virtual ~PooledInputEventFactory(); 1220 1221 virtual KeyEvent* createKeyEvent() override; 1222 virtual MotionEvent* createMotionEvent() override; 1223 virtual FocusEvent* createFocusEvent() override; 1224 virtual CaptureEvent* createCaptureEvent() override; 1225 virtual DragEvent* createDragEvent() override; 1226 virtual TouchModeEvent* createTouchModeEvent() override; 1227 1228 void recycle(InputEvent* event); 1229 1230 private: 1231 const size_t mMaxPoolSize; 1232 1233 std::queue<std::unique_ptr<KeyEvent>> mKeyEventPool; 1234 std::queue<std::unique_ptr<MotionEvent>> mMotionEventPool; 1235 std::queue<std::unique_ptr<FocusEvent>> mFocusEventPool; 1236 std::queue<std::unique_ptr<CaptureEvent>> mCaptureEventPool; 1237 std::queue<std::unique_ptr<DragEvent>> mDragEventPool; 1238 std::queue<std::unique_ptr<TouchModeEvent>> mTouchModeEventPool; 1239 }; 1240 1241 /** 1242 * An input event factory implementation that simply creates the input events on the heap, when 1243 * needed. The caller is responsible for destroying the returned references. 1244 * It is recommended that the caller wrap these return values into std::unique_ptr. 1245 */ 1246 class DynamicInputEventFactory : public InputEventFactoryInterface { 1247 public: DynamicInputEventFactory()1248 explicit DynamicInputEventFactory(){}; ~DynamicInputEventFactory()1249 ~DynamicInputEventFactory(){}; 1250 createKeyEvent()1251 KeyEvent* createKeyEvent() override { return new KeyEvent(); }; createMotionEvent()1252 MotionEvent* createMotionEvent() override { return new MotionEvent(); }; createFocusEvent()1253 FocusEvent* createFocusEvent() override { return new FocusEvent(); }; createCaptureEvent()1254 CaptureEvent* createCaptureEvent() override { return new CaptureEvent(); }; createDragEvent()1255 DragEvent* createDragEvent() override { return new DragEvent(); }; createTouchModeEvent()1256 TouchModeEvent* createTouchModeEvent() override { return new TouchModeEvent(); }; 1257 }; 1258 1259 /* 1260 * Describes a unique request to enable or disable Pointer Capture. 1261 */ 1262 struct PointerCaptureRequest { 1263 public: PointerCaptureRequestPointerCaptureRequest1264 inline PointerCaptureRequest() : window(), seq(0) {} PointerCaptureRequestPointerCaptureRequest1265 inline PointerCaptureRequest(sp<IBinder> window, uint32_t seq) : window(window), seq(seq) {} 1266 inline bool operator==(const PointerCaptureRequest& other) const { 1267 return window == other.window && seq == other.seq; 1268 } isEnablePointerCaptureRequest1269 inline bool isEnable() const { return window != nullptr; } 1270 1271 // The requesting window. 1272 // If the request is to enable the capture, this is the input token of the window that requested 1273 // pointer capture. Otherwise, this is nullptr. 1274 sp<IBinder> window; 1275 1276 // The sequence number for the request. 1277 uint32_t seq; 1278 }; 1279 1280 /* Pointer icon styles. 1281 * Must match the definition in android.view.PointerIcon. 1282 * 1283 * Due to backwards compatibility and public api constraints, this is a duplicate (but type safe) 1284 * definition of PointerIcon.java. 1285 */ 1286 enum class PointerIconStyle : int32_t { 1287 TYPE_CUSTOM = static_cast<int32_t>(::android::os::PointerIconType::CUSTOM), 1288 TYPE_NULL = static_cast<int32_t>(::android::os::PointerIconType::TYPE_NULL), 1289 TYPE_NOT_SPECIFIED = static_cast<int32_t>(::android::os::PointerIconType::NOT_SPECIFIED), 1290 TYPE_ARROW = static_cast<int32_t>(::android::os::PointerIconType::ARROW), 1291 TYPE_CONTEXT_MENU = static_cast<int32_t>(::android::os::PointerIconType::CONTEXT_MENU), 1292 TYPE_HAND = static_cast<int32_t>(::android::os::PointerIconType::HAND), 1293 TYPE_HELP = static_cast<int32_t>(::android::os::PointerIconType::HELP), 1294 TYPE_WAIT = static_cast<int32_t>(::android::os::PointerIconType::WAIT), 1295 TYPE_CELL = static_cast<int32_t>(::android::os::PointerIconType::CELL), 1296 TYPE_CROSSHAIR = static_cast<int32_t>(::android::os::PointerIconType::CROSSHAIR), 1297 TYPE_TEXT = static_cast<int32_t>(::android::os::PointerIconType::TEXT), 1298 TYPE_VERTICAL_TEXT = static_cast<int32_t>(::android::os::PointerIconType::VERTICAL_TEXT), 1299 TYPE_ALIAS = static_cast<int32_t>(::android::os::PointerIconType::ALIAS), 1300 TYPE_COPY = static_cast<int32_t>(::android::os::PointerIconType::COPY), 1301 TYPE_NO_DROP = static_cast<int32_t>(::android::os::PointerIconType::NO_DROP), 1302 TYPE_ALL_SCROLL = static_cast<int32_t>(::android::os::PointerIconType::ALL_SCROLL), 1303 TYPE_HORIZONTAL_DOUBLE_ARROW = 1304 static_cast<int32_t>(::android::os::PointerIconType::HORIZONTAL_DOUBLE_ARROW), 1305 TYPE_VERTICAL_DOUBLE_ARROW = 1306 static_cast<int32_t>(::android::os::PointerIconType::VERTICAL_DOUBLE_ARROW), 1307 TYPE_TOP_RIGHT_DOUBLE_ARROW = 1308 static_cast<int32_t>(::android::os::PointerIconType::TOP_RIGHT_DOUBLE_ARROW), 1309 TYPE_TOP_LEFT_DOUBLE_ARROW = 1310 static_cast<int32_t>(::android::os::PointerIconType::TOP_LEFT_DOUBLE_ARROW), 1311 TYPE_ZOOM_IN = static_cast<int32_t>(::android::os::PointerIconType::ZOOM_IN), 1312 TYPE_ZOOM_OUT = static_cast<int32_t>(::android::os::PointerIconType::ZOOM_OUT), 1313 TYPE_GRAB = static_cast<int32_t>(::android::os::PointerIconType::GRAB), 1314 TYPE_GRABBING = static_cast<int32_t>(::android::os::PointerIconType::GRABBING), 1315 TYPE_HANDWRITING = static_cast<int32_t>(::android::os::PointerIconType::HANDWRITING), 1316 1317 TYPE_SPOT_HOVER = static_cast<int32_t>(::android::os::PointerIconType::SPOT_HOVER), 1318 TYPE_SPOT_TOUCH = static_cast<int32_t>(::android::os::PointerIconType::SPOT_TOUCH), 1319 TYPE_SPOT_ANCHOR = static_cast<int32_t>(::android::os::PointerIconType::SPOT_ANCHOR), 1320 }; 1321 1322 } // namespace android 1323