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 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 73 | /* Motion event is inconsistent with previously sent motion events. */ |
| 74 | AMOTION_EVENT_FLAG_TAINTED = 0x80000000, |
| 75 | }; |
| 76 | |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 77 | /** |
| 78 | * Allowed VerifiedKeyEvent flags. All other flags from KeyEvent do not get verified. |
| 79 | * These values must be kept in sync with VerifiedKeyEvent.java |
| 80 | */ |
| 81 | constexpr int32_t VERIFIED_KEY_EVENT_FLAGS = AKEY_EVENT_FLAG_CANCELED; |
| 82 | |
| 83 | /** |
| 84 | * Allowed VerifiedMotionEventFlags. All other flags from MotionEvent do not get verified. |
| 85 | * These values must be kept in sync with VerifiedMotionEvent.java |
| 86 | */ |
| 87 | constexpr int32_t VERIFIED_MOTION_EVENT_FLAGS = |
| 88 | AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 89 | |
arthurhung | cc7f980 | 2020-04-30 17:55:40 +0800 | [diff] [blame] | 90 | /** |
| 91 | * This flag indicates that the point up event has been canceled. |
| 92 | * Typically this is used for palm event when the user has accidental touches. |
| 93 | * TODO: Adjust flag to public api |
| 94 | */ |
| 95 | constexpr int32_t AMOTION_EVENT_FLAG_CANCELED = 0x20; |
| 96 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 97 | enum { |
| 98 | /* Used when a motion event is not associated with any display. |
| 99 | * Typically used for non-pointer events. */ |
| 100 | ADISPLAY_ID_NONE = -1, |
| 101 | |
| 102 | /* The default display id. */ |
| 103 | ADISPLAY_ID_DEFAULT = 0, |
| 104 | }; |
| 105 | |
| 106 | enum { |
| 107 | /* |
| 108 | * Indicates that an input device has switches. |
| 109 | * This input source flag is hidden from the API because switches are only used by the system |
| 110 | * and applications have no way to interact with them. |
| 111 | */ |
| 112 | AINPUT_SOURCE_SWITCH = 0x80000000, |
| 113 | }; |
| 114 | |
Michael Wright | 962a108 | 2013-10-17 17:35:53 -0700 | [diff] [blame] | 115 | enum { |
| 116 | /** |
| 117 | * Constants for LEDs. Hidden from the API since we don't actually expose a way to interact |
| 118 | * with LEDs to developers |
| 119 | * |
Michael Wright | 872db4f | 2014-04-22 15:03:51 -0700 | [diff] [blame] | 120 | * 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] | 121 | */ |
| 122 | |
| 123 | ALED_NUM_LOCK = 0x00, |
| 124 | ALED_CAPS_LOCK = 0x01, |
| 125 | ALED_SCROLL_LOCK = 0x02, |
| 126 | ALED_COMPOSE = 0x03, |
| 127 | ALED_KANA = 0x04, |
| 128 | ALED_SLEEP = 0x05, |
| 129 | ALED_SUSPEND = 0x06, |
| 130 | ALED_MUTE = 0x07, |
| 131 | ALED_MISC = 0x08, |
| 132 | ALED_MAIL = 0x09, |
| 133 | ALED_CHARGING = 0x0a, |
| 134 | ALED_CONTROLLER_1 = 0x10, |
| 135 | ALED_CONTROLLER_2 = 0x11, |
| 136 | ALED_CONTROLLER_3 = 0x12, |
| 137 | ALED_CONTROLLER_4 = 0x13, |
| 138 | }; |
| 139 | |
Michael Wright | 9b04f86 | 2013-10-18 17:53:50 -0700 | [diff] [blame] | 140 | /* Maximum number of controller LEDs we support */ |
| 141 | #define MAX_CONTROLLER_LEDS 4 |
| 142 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 143 | /* |
| 144 | * SystemUiVisibility constants from View. |
| 145 | */ |
| 146 | enum { |
| 147 | ASYSTEM_UI_VISIBILITY_STATUS_BAR_VISIBLE = 0, |
| 148 | ASYSTEM_UI_VISIBILITY_STATUS_BAR_HIDDEN = 0x00000001, |
| 149 | }; |
| 150 | |
| 151 | /* |
| 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 | |
Elliott Hughes | 6071da7 | 2015-08-12 15:27:47 -0700 | [diff] [blame] | 188 | #ifdef __ANDROID__ |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 189 | class Parcel; |
| 190 | #endif |
| 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 | /** |
Garfield Tan | 84b087e | 2020-01-23 10:49:05 -0800 | [diff] [blame] | 277 | * Generator of unique numbers used to identify input events. |
| 278 | * |
| 279 | * Layout of ID: |
| 280 | * |--------------------------|---------------------------| |
| 281 | * | 2 bits for source | 30 bits for random number | |
| 282 | * |--------------------------|---------------------------| |
| 283 | */ |
| 284 | class IdGenerator { |
| 285 | private: |
| 286 | static constexpr uint32_t SOURCE_SHIFT = 30; |
| 287 | |
| 288 | public: |
| 289 | // Used to divide integer space to ensure no conflict among these sources./ |
| 290 | enum class Source : int32_t { |
Garfield Tan | 750c7f4 | 2020-05-18 17:41:46 -0700 | [diff] [blame] | 291 | INPUT_READER = static_cast<int32_t>(0x0u << SOURCE_SHIFT), |
| 292 | INPUT_DISPATCHER = static_cast<int32_t>(0x1u << SOURCE_SHIFT), |
| 293 | 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] | 294 | }; |
| 295 | IdGenerator(Source source); |
| 296 | |
| 297 | int32_t nextId() const; |
| 298 | |
| 299 | // Extract source from given id. |
| 300 | static inline Source getSource(int32_t id) { return static_cast<Source>(SOURCE_MASK & id); } |
| 301 | |
| 302 | private: |
| 303 | const Source mSource; |
| 304 | |
Garfield Tan | 750c7f4 | 2020-05-18 17:41:46 -0700 | [diff] [blame] | 305 | 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] | 306 | }; |
| 307 | |
| 308 | /** |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 309 | * Invalid value for cursor position. Used for non-mouse events, tests and injected events. Don't |
| 310 | * use it for direct comparison with any other value, because NaN isn't equal to itself according to |
| 311 | * IEEE 754. Use isnan() instead to check if a cursor position is valid. |
| 312 | */ |
| 313 | constexpr float AMOTION_EVENT_INVALID_CURSOR_POSITION = std::numeric_limits<float>::quiet_NaN(); |
| 314 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 315 | /* |
| 316 | * Pointer coordinate data. |
| 317 | */ |
| 318 | struct PointerCoords { |
Michael Wright | 8f6710f | 2014-06-09 18:56:43 -0700 | [diff] [blame] | 319 | enum { MAX_AXES = 30 }; // 30 so that sizeof(PointerCoords) == 128 |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 320 | |
| 321 | // Bitfield of axes that are present in this structure. |
Fengwei Yin | 83e0e42 | 2014-05-24 05:32:09 +0800 | [diff] [blame] | 322 | uint64_t bits __attribute__((aligned(8))); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 323 | |
| 324 | // Values of axes that are stored in this structure packed in order by axis id |
| 325 | // for each axis that is present in the structure according to 'bits'. |
| 326 | float values[MAX_AXES]; |
| 327 | |
| 328 | inline void clear() { |
Michael Wright | d0bd391 | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 329 | BitSet64::clear(bits); |
| 330 | } |
| 331 | |
| 332 | bool isEmpty() const { |
| 333 | return BitSet64::isEmpty(bits); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | float getAxisValue(int32_t axis) const; |
| 337 | status_t setAxisValue(int32_t axis, float value); |
| 338 | |
Robert Carr | e07e103 | 2018-11-26 12:55:53 -0800 | [diff] [blame] | 339 | void scale(float globalScale); |
| 340 | |
| 341 | // Scale the pointer coordinates according to a global scale and a |
| 342 | // window scale. The global scale will be applied to TOUCH/TOOL_MAJOR/MINOR |
| 343 | // axes, however the window scaling will not. |
| 344 | void scale(float globalScale, float windowXScale, float windowYScale); |
Jeff Brown | ed4d28d | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 345 | void applyOffset(float xOffset, float yOffset); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 346 | |
chaviw | c01e137 | 2020-07-01 12:37:31 -0700 | [diff] [blame] | 347 | void transform(const ui::Transform& transform); |
| 348 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 349 | inline float getX() const { |
| 350 | return getAxisValue(AMOTION_EVENT_AXIS_X); |
| 351 | } |
| 352 | |
| 353 | inline float getY() const { |
| 354 | return getAxisValue(AMOTION_EVENT_AXIS_Y); |
| 355 | } |
| 356 | |
Elliott Hughes | 6071da7 | 2015-08-12 15:27:47 -0700 | [diff] [blame] | 357 | #ifdef __ANDROID__ |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 358 | status_t readFromParcel(Parcel* parcel); |
| 359 | status_t writeToParcel(Parcel* parcel) const; |
| 360 | #endif |
| 361 | |
| 362 | bool operator==(const PointerCoords& other) const; |
| 363 | inline bool operator!=(const PointerCoords& other) const { |
| 364 | return !(*this == other); |
| 365 | } |
| 366 | |
| 367 | void copyFrom(const PointerCoords& other); |
| 368 | |
| 369 | private: |
| 370 | void tooManyAxes(int axis); |
| 371 | }; |
| 372 | |
| 373 | /* |
| 374 | * Pointer property data. |
| 375 | */ |
| 376 | struct PointerProperties { |
| 377 | // The id of the pointer. |
| 378 | int32_t id; |
| 379 | |
| 380 | // The pointer tool type. |
| 381 | int32_t toolType; |
| 382 | |
| 383 | inline void clear() { |
| 384 | id = -1; |
| 385 | toolType = 0; |
| 386 | } |
| 387 | |
| 388 | bool operator==(const PointerProperties& other) const; |
| 389 | inline bool operator!=(const PointerProperties& other) const { |
| 390 | return !(*this == other); |
| 391 | } |
| 392 | |
| 393 | void copyFrom(const PointerProperties& other); |
| 394 | }; |
| 395 | |
| 396 | /* |
| 397 | * Input events. |
| 398 | */ |
| 399 | class InputEvent : public AInputEvent { |
| 400 | public: |
| 401 | virtual ~InputEvent() { } |
| 402 | |
| 403 | virtual int32_t getType() const = 0; |
| 404 | |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 405 | inline int32_t getId() const { return mId; } |
| 406 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 407 | inline int32_t getDeviceId() const { return mDeviceId; } |
| 408 | |
Siarhei Vishniakou | 3826d47 | 2020-01-27 10:44:40 -0600 | [diff] [blame] | 409 | inline uint32_t getSource() const { return mSource; } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 410 | |
Siarhei Vishniakou | 3826d47 | 2020-01-27 10:44:40 -0600 | [diff] [blame] | 411 | inline void setSource(uint32_t source) { mSource = source; } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 412 | |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 413 | inline int32_t getDisplayId() const { return mDisplayId; } |
| 414 | |
| 415 | inline void setDisplayId(int32_t displayId) { mDisplayId = displayId; } |
| 416 | |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 417 | inline std::array<uint8_t, 32> getHmac() const { return mHmac; } |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 418 | |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 419 | static int32_t nextId(); |
| 420 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 421 | protected: |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 422 | 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] | 423 | std::array<uint8_t, 32> hmac); |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 424 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 425 | void initialize(const InputEvent& from); |
| 426 | |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 427 | int32_t mId; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 428 | int32_t mDeviceId; |
Siarhei Vishniakou | 3826d47 | 2020-01-27 10:44:40 -0600 | [diff] [blame] | 429 | uint32_t mSource; |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 430 | int32_t mDisplayId; |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 431 | std::array<uint8_t, 32> mHmac; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 432 | }; |
| 433 | |
| 434 | /* |
| 435 | * Key events. |
| 436 | */ |
| 437 | class KeyEvent : public InputEvent { |
| 438 | public: |
| 439 | virtual ~KeyEvent() { } |
| 440 | |
| 441 | virtual int32_t getType() const { return AINPUT_EVENT_TYPE_KEY; } |
| 442 | |
| 443 | inline int32_t getAction() const { return mAction; } |
| 444 | |
| 445 | inline int32_t getFlags() const { return mFlags; } |
| 446 | |
| 447 | inline void setFlags(int32_t flags) { mFlags = flags; } |
| 448 | |
| 449 | inline int32_t getKeyCode() const { return mKeyCode; } |
| 450 | |
| 451 | inline int32_t getScanCode() const { return mScanCode; } |
| 452 | |
| 453 | inline int32_t getMetaState() const { return mMetaState; } |
| 454 | |
| 455 | inline int32_t getRepeatCount() const { return mRepeatCount; } |
| 456 | |
| 457 | inline nsecs_t getDownTime() const { return mDownTime; } |
| 458 | |
| 459 | inline nsecs_t getEventTime() const { return mEventTime; } |
| 460 | |
Michael Wright | 872db4f | 2014-04-22 15:03:51 -0700 | [diff] [blame] | 461 | static const char* getLabel(int32_t keyCode); |
| 462 | static int32_t getKeyCodeFromLabel(const char* label); |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 463 | |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 464 | 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] | 465 | std::array<uint8_t, 32> hmac, int32_t action, int32_t flags, int32_t keyCode, |
| 466 | int32_t scanCode, int32_t metaState, int32_t repeatCount, nsecs_t downTime, |
| 467 | nsecs_t eventTime); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 468 | void initialize(const KeyEvent& from); |
| 469 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 470 | static const char* actionToString(int32_t action); |
| 471 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 472 | protected: |
| 473 | int32_t mAction; |
| 474 | int32_t mFlags; |
| 475 | int32_t mKeyCode; |
| 476 | int32_t mScanCode; |
| 477 | int32_t mMetaState; |
| 478 | int32_t mRepeatCount; |
| 479 | nsecs_t mDownTime; |
| 480 | nsecs_t mEventTime; |
| 481 | }; |
| 482 | |
| 483 | /* |
| 484 | * Motion events. |
| 485 | */ |
| 486 | class MotionEvent : public InputEvent { |
| 487 | public: |
| 488 | virtual ~MotionEvent() { } |
| 489 | |
| 490 | virtual int32_t getType() const { return AINPUT_EVENT_TYPE_MOTION; } |
| 491 | |
| 492 | inline int32_t getAction() const { return mAction; } |
| 493 | |
| 494 | inline int32_t getActionMasked() const { return mAction & AMOTION_EVENT_ACTION_MASK; } |
| 495 | |
| 496 | inline int32_t getActionIndex() const { |
| 497 | return (mAction & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) |
| 498 | >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; |
| 499 | } |
| 500 | |
| 501 | inline void setAction(int32_t action) { mAction = action; } |
| 502 | |
| 503 | inline int32_t getFlags() const { return mFlags; } |
| 504 | |
| 505 | inline void setFlags(int32_t flags) { mFlags = flags; } |
| 506 | |
| 507 | inline int32_t getEdgeFlags() const { return mEdgeFlags; } |
| 508 | |
| 509 | inline void setEdgeFlags(int32_t edgeFlags) { mEdgeFlags = edgeFlags; } |
| 510 | |
| 511 | inline int32_t getMetaState() const { return mMetaState; } |
| 512 | |
| 513 | inline void setMetaState(int32_t metaState) { mMetaState = metaState; } |
| 514 | |
| 515 | inline int32_t getButtonState() const { return mButtonState; } |
| 516 | |
Michael Wright | 6db5879 | 2016-09-14 19:53:37 +0100 | [diff] [blame] | 517 | inline void setButtonState(int32_t buttonState) { mButtonState = buttonState; } |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 518 | |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 519 | inline MotionClassification getClassification() const { return mClassification; } |
| 520 | |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 521 | inline int32_t getActionButton() const { return mActionButton; } |
| 522 | |
Michael Wright | 21957b9 | 2015-06-17 21:06:54 +0100 | [diff] [blame] | 523 | inline void setActionButton(int32_t button) { mActionButton = button; } |
| 524 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 525 | inline float getXOffset() const { return mTransform.tx(); } |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 526 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 527 | inline float getYOffset() const { return mTransform.ty(); } |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 528 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 529 | inline ui::Transform getTransform() const { return mTransform; } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 530 | |
| 531 | inline float getXPrecision() const { return mXPrecision; } |
| 532 | |
| 533 | inline float getYPrecision() const { return mYPrecision; } |
| 534 | |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 535 | inline float getRawXCursorPosition() const { return mRawXCursorPosition; } |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 536 | |
| 537 | float getXCursorPosition() const; |
| 538 | |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 539 | inline float getRawYCursorPosition() const { return mRawYCursorPosition; } |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 540 | |
| 541 | float getYCursorPosition() const; |
| 542 | |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 543 | void setCursorPosition(float x, float y); |
| 544 | |
Garfield Tan | ab0ab9c | 2019-07-10 18:58:28 -0700 | [diff] [blame] | 545 | static inline bool isValidCursorPosition(float x, float y) { return !isnan(x) && !isnan(y); } |
| 546 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 547 | inline nsecs_t getDownTime() const { return mDownTime; } |
| 548 | |
| 549 | inline void setDownTime(nsecs_t downTime) { mDownTime = downTime; } |
| 550 | |
| 551 | inline size_t getPointerCount() const { return mPointerProperties.size(); } |
| 552 | |
| 553 | inline const PointerProperties* getPointerProperties(size_t pointerIndex) const { |
| 554 | return &mPointerProperties[pointerIndex]; |
| 555 | } |
| 556 | |
| 557 | inline int32_t getPointerId(size_t pointerIndex) const { |
| 558 | return mPointerProperties[pointerIndex].id; |
| 559 | } |
| 560 | |
| 561 | inline int32_t getToolType(size_t pointerIndex) const { |
| 562 | return mPointerProperties[pointerIndex].toolType; |
| 563 | } |
| 564 | |
| 565 | inline nsecs_t getEventTime() const { return mSampleEventTimes[getHistorySize()]; } |
| 566 | |
| 567 | const PointerCoords* getRawPointerCoords(size_t pointerIndex) const; |
| 568 | |
| 569 | float getRawAxisValue(int32_t axis, size_t pointerIndex) const; |
| 570 | |
| 571 | inline float getRawX(size_t pointerIndex) const { |
| 572 | return getRawAxisValue(AMOTION_EVENT_AXIS_X, pointerIndex); |
| 573 | } |
| 574 | |
| 575 | inline float getRawY(size_t pointerIndex) const { |
| 576 | return getRawAxisValue(AMOTION_EVENT_AXIS_Y, pointerIndex); |
| 577 | } |
| 578 | |
| 579 | float getAxisValue(int32_t axis, size_t pointerIndex) const; |
| 580 | |
Siarhei Vishniakou | 69e4d0f | 2020-09-14 19:53:29 -0500 | [diff] [blame^] | 581 | /** |
| 582 | * Get the X coordinate of the latest sample in this MotionEvent for pointer 'pointerIndex'. |
| 583 | * Identical to calling getHistoricalX(pointerIndex, getHistorySize()). |
| 584 | */ |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 585 | inline float getX(size_t pointerIndex) const { |
| 586 | return getAxisValue(AMOTION_EVENT_AXIS_X, pointerIndex); |
| 587 | } |
| 588 | |
Siarhei Vishniakou | 69e4d0f | 2020-09-14 19:53:29 -0500 | [diff] [blame^] | 589 | /** |
| 590 | * Get the Y coordinate of the latest sample in this MotionEvent for pointer 'pointerIndex'. |
| 591 | * Identical to calling getHistoricalX(pointerIndex, getHistorySize()). |
| 592 | */ |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 593 | inline float getY(size_t pointerIndex) const { |
| 594 | return getAxisValue(AMOTION_EVENT_AXIS_Y, pointerIndex); |
| 595 | } |
| 596 | |
| 597 | inline float getPressure(size_t pointerIndex) const { |
| 598 | return getAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pointerIndex); |
| 599 | } |
| 600 | |
| 601 | inline float getSize(size_t pointerIndex) const { |
| 602 | return getAxisValue(AMOTION_EVENT_AXIS_SIZE, pointerIndex); |
| 603 | } |
| 604 | |
| 605 | inline float getTouchMajor(size_t pointerIndex) const { |
| 606 | return getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, pointerIndex); |
| 607 | } |
| 608 | |
| 609 | inline float getTouchMinor(size_t pointerIndex) const { |
| 610 | return getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, pointerIndex); |
| 611 | } |
| 612 | |
| 613 | inline float getToolMajor(size_t pointerIndex) const { |
| 614 | return getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, pointerIndex); |
| 615 | } |
| 616 | |
| 617 | inline float getToolMinor(size_t pointerIndex) const { |
| 618 | return getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, pointerIndex); |
| 619 | } |
| 620 | |
| 621 | inline float getOrientation(size_t pointerIndex) const { |
| 622 | return getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, pointerIndex); |
| 623 | } |
| 624 | |
| 625 | inline size_t getHistorySize() const { return mSampleEventTimes.size() - 1; } |
| 626 | |
| 627 | inline nsecs_t getHistoricalEventTime(size_t historicalIndex) const { |
| 628 | return mSampleEventTimes[historicalIndex]; |
| 629 | } |
| 630 | |
| 631 | const PointerCoords* getHistoricalRawPointerCoords( |
| 632 | size_t pointerIndex, size_t historicalIndex) const; |
| 633 | |
| 634 | float getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex, |
| 635 | size_t historicalIndex) const; |
| 636 | |
| 637 | inline float getHistoricalRawX(size_t pointerIndex, size_t historicalIndex) const { |
| 638 | return getHistoricalRawAxisValue( |
| 639 | AMOTION_EVENT_AXIS_X, pointerIndex, historicalIndex); |
| 640 | } |
| 641 | |
| 642 | inline float getHistoricalRawY(size_t pointerIndex, size_t historicalIndex) const { |
| 643 | return getHistoricalRawAxisValue( |
| 644 | AMOTION_EVENT_AXIS_Y, pointerIndex, historicalIndex); |
| 645 | } |
| 646 | |
| 647 | float getHistoricalAxisValue(int32_t axis, size_t pointerIndex, size_t historicalIndex) const; |
| 648 | |
| 649 | inline float getHistoricalX(size_t pointerIndex, size_t historicalIndex) const { |
| 650 | return getHistoricalAxisValue( |
| 651 | AMOTION_EVENT_AXIS_X, pointerIndex, historicalIndex); |
| 652 | } |
| 653 | |
| 654 | inline float getHistoricalY(size_t pointerIndex, size_t historicalIndex) const { |
| 655 | return getHistoricalAxisValue( |
| 656 | AMOTION_EVENT_AXIS_Y, pointerIndex, historicalIndex); |
| 657 | } |
| 658 | |
| 659 | inline float getHistoricalPressure(size_t pointerIndex, size_t historicalIndex) const { |
| 660 | return getHistoricalAxisValue( |
| 661 | AMOTION_EVENT_AXIS_PRESSURE, pointerIndex, historicalIndex); |
| 662 | } |
| 663 | |
| 664 | inline float getHistoricalSize(size_t pointerIndex, size_t historicalIndex) const { |
| 665 | return getHistoricalAxisValue( |
| 666 | AMOTION_EVENT_AXIS_SIZE, pointerIndex, historicalIndex); |
| 667 | } |
| 668 | |
| 669 | inline float getHistoricalTouchMajor(size_t pointerIndex, size_t historicalIndex) const { |
| 670 | return getHistoricalAxisValue( |
| 671 | AMOTION_EVENT_AXIS_TOUCH_MAJOR, pointerIndex, historicalIndex); |
| 672 | } |
| 673 | |
| 674 | inline float getHistoricalTouchMinor(size_t pointerIndex, size_t historicalIndex) const { |
| 675 | return getHistoricalAxisValue( |
| 676 | AMOTION_EVENT_AXIS_TOUCH_MINOR, pointerIndex, historicalIndex); |
| 677 | } |
| 678 | |
| 679 | inline float getHistoricalToolMajor(size_t pointerIndex, size_t historicalIndex) const { |
| 680 | return getHistoricalAxisValue( |
| 681 | AMOTION_EVENT_AXIS_TOOL_MAJOR, pointerIndex, historicalIndex); |
| 682 | } |
| 683 | |
| 684 | inline float getHistoricalToolMinor(size_t pointerIndex, size_t historicalIndex) const { |
| 685 | return getHistoricalAxisValue( |
| 686 | AMOTION_EVENT_AXIS_TOOL_MINOR, pointerIndex, historicalIndex); |
| 687 | } |
| 688 | |
| 689 | inline float getHistoricalOrientation(size_t pointerIndex, size_t historicalIndex) const { |
| 690 | return getHistoricalAxisValue( |
| 691 | AMOTION_EVENT_AXIS_ORIENTATION, pointerIndex, historicalIndex); |
| 692 | } |
| 693 | |
| 694 | ssize_t findPointerIndex(int32_t pointerId) const; |
| 695 | |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 696 | 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] | 697 | std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton, |
| 698 | int32_t flags, int32_t edgeFlags, int32_t metaState, int32_t buttonState, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 699 | MotionClassification classification, const ui::Transform& transform, |
| 700 | float xPrecision, float yPrecision, float rawXCursorPosition, |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 701 | float rawYCursorPosition, nsecs_t downTime, nsecs_t eventTime, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 702 | size_t pointerCount, const PointerProperties* pointerProperties, |
| 703 | const PointerCoords* pointerCoords); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 704 | |
| 705 | void copyFrom(const MotionEvent* other, bool keepHistory); |
| 706 | |
| 707 | void addSample( |
| 708 | nsecs_t eventTime, |
| 709 | const PointerCoords* pointerCoords); |
| 710 | |
| 711 | void offsetLocation(float xOffset, float yOffset); |
| 712 | |
Robert Carr | e07e103 | 2018-11-26 12:55:53 -0800 | [diff] [blame] | 713 | void scale(float globalScaleFactor); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 714 | |
Jeff Brown | 5a2f68e | 2013-07-15 17:28:19 -0700 | [diff] [blame] | 715 | // Apply 3x3 perspective matrix transformation. |
| 716 | // Matrix is in row-major form and compatible with SkMatrix. |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 717 | void transform(const std::array<float, 9>& matrix); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 718 | |
Elliott Hughes | 6071da7 | 2015-08-12 15:27:47 -0700 | [diff] [blame] | 719 | #ifdef __ANDROID__ |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 720 | status_t readFromParcel(Parcel* parcel); |
| 721 | status_t writeToParcel(Parcel* parcel) const; |
| 722 | #endif |
| 723 | |
Siarhei Vishniakou | 3826d47 | 2020-01-27 10:44:40 -0600 | [diff] [blame] | 724 | static bool isTouchEvent(uint32_t source, int32_t action); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 725 | inline bool isTouchEvent() const { |
| 726 | return isTouchEvent(mSource, mAction); |
| 727 | } |
| 728 | |
| 729 | // Low-level accessors. |
| 730 | inline const PointerProperties* getPointerProperties() const { |
| 731 | return mPointerProperties.array(); |
| 732 | } |
Siarhei Vishniakou | 46a2774 | 2020-09-09 13:57:28 -0500 | [diff] [blame] | 733 | inline const nsecs_t* getSampleEventTimes() const { return mSampleEventTimes.data(); } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 734 | inline const PointerCoords* getSamplePointerCoords() const { |
| 735 | return mSamplePointerCoords.array(); |
| 736 | } |
| 737 | |
Michael Wright | 872db4f | 2014-04-22 15:03:51 -0700 | [diff] [blame] | 738 | static const char* getLabel(int32_t axis); |
| 739 | static int32_t getAxisFromLabel(const char* label); |
| 740 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 741 | static const char* actionToString(int32_t action); |
| 742 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 743 | protected: |
| 744 | int32_t mAction; |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 745 | int32_t mActionButton; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 746 | int32_t mFlags; |
| 747 | int32_t mEdgeFlags; |
| 748 | int32_t mMetaState; |
| 749 | int32_t mButtonState; |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 750 | MotionClassification mClassification; |
chaviw | fd9c1ed | 2020-07-01 10:57:59 -0700 | [diff] [blame] | 751 | ui::Transform mTransform; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 752 | float mXPrecision; |
| 753 | float mYPrecision; |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 754 | float mRawXCursorPosition; |
| 755 | float mRawYCursorPosition; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 756 | nsecs_t mDownTime; |
| 757 | Vector<PointerProperties> mPointerProperties; |
Siarhei Vishniakou | 46a2774 | 2020-09-09 13:57:28 -0500 | [diff] [blame] | 758 | std::vector<nsecs_t> mSampleEventTimes; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 759 | Vector<PointerCoords> mSamplePointerCoords; |
| 760 | }; |
| 761 | |
| 762 | /* |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 763 | * Focus events. |
| 764 | */ |
| 765 | class FocusEvent : public InputEvent { |
| 766 | public: |
| 767 | virtual ~FocusEvent() {} |
| 768 | |
| 769 | virtual int32_t getType() const override { return AINPUT_EVENT_TYPE_FOCUS; } |
| 770 | |
| 771 | inline bool getHasFocus() const { return mHasFocus; } |
| 772 | |
| 773 | inline bool getInTouchMode() const { return mInTouchMode; } |
| 774 | |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 775 | void initialize(int32_t id, bool hasFocus, bool inTouchMode); |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 776 | |
| 777 | void initialize(const FocusEvent& from); |
| 778 | |
| 779 | protected: |
| 780 | bool mHasFocus; |
| 781 | bool mInTouchMode; |
| 782 | }; |
| 783 | |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 784 | /** |
| 785 | * Base class for verified events. |
| 786 | * Do not create a VerifiedInputEvent explicitly. |
| 787 | * Use helper functions to create them from InputEvents. |
| 788 | */ |
| 789 | struct __attribute__((__packed__)) VerifiedInputEvent { |
| 790 | enum class Type : int32_t { |
| 791 | KEY = AINPUT_EVENT_TYPE_KEY, |
| 792 | MOTION = AINPUT_EVENT_TYPE_MOTION, |
| 793 | }; |
| 794 | |
| 795 | Type type; |
| 796 | int32_t deviceId; |
| 797 | nsecs_t eventTimeNanos; |
| 798 | uint32_t source; |
| 799 | int32_t displayId; |
| 800 | }; |
| 801 | |
| 802 | /** |
| 803 | * Same as KeyEvent, but only contains the data that can be verified. |
| 804 | * If you update this class, you must also update VerifiedKeyEvent.java |
| 805 | */ |
| 806 | struct __attribute__((__packed__)) VerifiedKeyEvent : public VerifiedInputEvent { |
| 807 | int32_t action; |
| 808 | nsecs_t downTimeNanos; |
| 809 | int32_t flags; |
| 810 | int32_t keyCode; |
| 811 | int32_t scanCode; |
| 812 | int32_t metaState; |
| 813 | int32_t repeatCount; |
| 814 | }; |
| 815 | |
| 816 | /** |
| 817 | * Same as MotionEvent, but only contains the data that can be verified. |
| 818 | * If you update this class, you must also update VerifiedMotionEvent.java |
| 819 | */ |
| 820 | struct __attribute__((__packed__)) VerifiedMotionEvent : public VerifiedInputEvent { |
| 821 | float rawX; |
| 822 | float rawY; |
| 823 | int32_t actionMasked; |
| 824 | nsecs_t downTimeNanos; |
| 825 | int32_t flags; |
| 826 | int32_t metaState; |
| 827 | int32_t buttonState; |
| 828 | }; |
| 829 | |
| 830 | VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event); |
| 831 | VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event); |
| 832 | |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 833 | /* |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 834 | * Input event factory. |
| 835 | */ |
| 836 | class InputEventFactoryInterface { |
| 837 | protected: |
| 838 | virtual ~InputEventFactoryInterface() { } |
| 839 | |
| 840 | public: |
| 841 | InputEventFactoryInterface() { } |
| 842 | |
| 843 | virtual KeyEvent* createKeyEvent() = 0; |
| 844 | virtual MotionEvent* createMotionEvent() = 0; |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 845 | virtual FocusEvent* createFocusEvent() = 0; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 846 | }; |
| 847 | |
| 848 | /* |
| 849 | * A simple input event factory implementation that uses a single preallocated instance |
| 850 | * of each type of input event that are reused for each request. |
| 851 | */ |
| 852 | class PreallocatedInputEventFactory : public InputEventFactoryInterface { |
| 853 | public: |
| 854 | PreallocatedInputEventFactory() { } |
| 855 | virtual ~PreallocatedInputEventFactory() { } |
| 856 | |
Siarhei Vishniakou | 727a44e | 2019-11-23 12:59:16 -0800 | [diff] [blame] | 857 | virtual KeyEvent* createKeyEvent() override { return &mKeyEvent; } |
| 858 | virtual MotionEvent* createMotionEvent() override { return &mMotionEvent; } |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 859 | virtual FocusEvent* createFocusEvent() override { return &mFocusEvent; } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 860 | |
| 861 | private: |
| 862 | KeyEvent mKeyEvent; |
| 863 | MotionEvent mMotionEvent; |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 864 | FocusEvent mFocusEvent; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 865 | }; |
| 866 | |
| 867 | /* |
| 868 | * An input event factory implementation that maintains a pool of input events. |
| 869 | */ |
| 870 | class PooledInputEventFactory : public InputEventFactoryInterface { |
| 871 | public: |
Chih-Hung Hsieh | f43b02c | 2018-12-20 15:45:56 -0800 | [diff] [blame] | 872 | explicit PooledInputEventFactory(size_t maxPoolSize = 20); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 873 | virtual ~PooledInputEventFactory(); |
| 874 | |
Siarhei Vishniakou | 727a44e | 2019-11-23 12:59:16 -0800 | [diff] [blame] | 875 | virtual KeyEvent* createKeyEvent() override; |
| 876 | virtual MotionEvent* createMotionEvent() override; |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 877 | virtual FocusEvent* createFocusEvent() override; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 878 | |
| 879 | void recycle(InputEvent* event); |
| 880 | |
| 881 | private: |
| 882 | const size_t mMaxPoolSize; |
| 883 | |
Siarhei Vishniakou | 727a44e | 2019-11-23 12:59:16 -0800 | [diff] [blame] | 884 | std::queue<std::unique_ptr<KeyEvent>> mKeyEventPool; |
| 885 | std::queue<std::unique_ptr<MotionEvent>> mMotionEventPool; |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 886 | std::queue<std::unique_ptr<FocusEvent>> mFocusEventPool; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 887 | }; |
| 888 | |
| 889 | } // namespace android |
| 890 | |
| 891 | #endif // _LIBINPUT_INPUT_H |