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