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