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