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