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