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