blob: a7e706ed183594ce9b2682c2ddb5ac92b8c7be7f [file] [log] [blame]
Jeff Brown5912f952013-07-01 19:10:31 -07001/*
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 Carr2c358bf2018-08-08 15:58:15 -070020#pragma GCC system_header
21
Jeff Brown5912f952013-07-01 19:10:31 -070022/**
23 * Native input event structures.
24 */
25
26#include <android/input.h>
Garfield Tanab0ab9c2019-07-10 18:58:28 -070027#include <math.h>
Garfield Tan00f511d2019-06-12 16:55:40 -070028#include <stdint.h>
Michael Wrightd0bd3912014-03-19 12:06:10 -070029#include <utils/BitSet.h>
Jeff Brown5912f952013-07-01 19:10:31 -070030#include <utils/KeyedVector.h>
Jeff Brown5912f952013-07-01 19:10:31 -070031#include <utils/RefBase.h>
Michael Wrightd0bd3912014-03-19 12:06:10 -070032#include <utils/Timers.h>
33#include <utils/Vector.h>
Garfield Tan00f511d2019-06-12 16:55:40 -070034#include <limits>
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -080035#include <queue>
Jeff Brown5912f952013-07-01 19:10:31 -070036
Jeff Brown5912f952013-07-01 19:10:31 -070037/*
38 * Additional private constants not defined in ndk/ui/input.h.
39 */
40enum {
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
51enum {
Michael Wrightcdcd8f22016-03-22 16:52:13 -070052
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 Pradhan141a9862018-11-19 14:35:56 -080065 /**
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 Brown5912f952013-07-01 19:10:31 -070071 /* Motion event is inconsistent with previously sent motion events. */
72 AMOTION_EVENT_FLAG_TAINTED = 0x80000000,
73};
74
75enum {
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
84enum {
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 Wright962a1082013-10-17 17:35:53 -070093enum {
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 Wright872db4f2014-04-22 15:03:51 -070098 * NOTE: If you add LEDs here, you must also add them to InputEventLabels.h
Michael Wright962a1082013-10-17 17:35:53 -070099 */
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 Wright9b04f862013-10-18 17:53:50 -0700118/* Maximum number of controller LEDs we support */
119#define MAX_CONTROLLER_LEDS 4
120
Jeff Brown5912f952013-07-01 19:10:31 -0700121/*
122 * SystemUiVisibility constants from View.
123 */
124enum {
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/*
Flanker552a8a52015-09-07 15:28:58 +0800138 * Maximum number of samples supported per motion event.
139 */
140#define MAX_SAMPLES UINT16_MAX
141
142/*
Jeff Brown5912f952013-07-01 19:10:31 -0700143 * 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 */
152struct AInputEvent {
153 virtual ~AInputEvent() { }
154};
155
156/*
157 * Declare a concrete type for the NDK's input device forward declaration.
158 */
159struct AInputDevice {
160 virtual ~AInputDevice() { }
161};
162
163
164namespace android {
165
Elliott Hughes6071da72015-08-12 15:27:47 -0700166#ifdef __ANDROID__
Jeff Brown5912f952013-07-01 19:10:31 -0700167class Parcel;
168#endif
169
170/*
171 * Flags that flow alongside events in the input dispatch system to help with certain
172 * policy decisions such as waking from device sleep.
173 *
174 * These flags are also defined in frameworks/base/core/java/android/view/WindowManagerPolicy.java.
175 */
176enum {
177 /* These flags originate in RawEvents and are generally set in the key map.
Michael Wright872db4f2014-04-22 15:03:51 -0700178 * NOTE: If you want a flag to be able to set in a keylayout file, then you must add it to
179 * InputEventLabels.h as well. */
Jeff Brown5912f952013-07-01 19:10:31 -0700180
Jeff Brownc9aa6282015-02-11 19:03:28 -0800181 // Indicates that the event should wake the device.
Jeff Brown5912f952013-07-01 19:10:31 -0700182 POLICY_FLAG_WAKE = 0x00000001,
Jeff Brownc9aa6282015-02-11 19:03:28 -0800183
184 // Indicates that the key is virtual, such as a capacitive button, and should
185 // generate haptic feedback. Virtual keys may be suppressed for some time
186 // after a recent touch to prevent accidental activation of virtual keys adjacent
187 // to the touch screen during an edge swipe.
Michael Wright872db4f2014-04-22 15:03:51 -0700188 POLICY_FLAG_VIRTUAL = 0x00000002,
Jeff Brownc9aa6282015-02-11 19:03:28 -0800189
190 // Indicates that the key is the special function modifier.
Michael Wright872db4f2014-04-22 15:03:51 -0700191 POLICY_FLAG_FUNCTION = 0x00000004,
Jeff Brown5912f952013-07-01 19:10:31 -0700192
Jeff Brownc9aa6282015-02-11 19:03:28 -0800193 // Indicates that the key represents a special gesture that has been detected by
194 // the touch firmware or driver. Causes touch events from the same device to be canceled.
195 POLICY_FLAG_GESTURE = 0x00000008,
196
Jeff Brown5912f952013-07-01 19:10:31 -0700197 POLICY_FLAG_RAW_MASK = 0x0000ffff,
198
199 /* These flags are set by the input dispatcher. */
200
201 // Indicates that the input event was injected.
202 POLICY_FLAG_INJECTED = 0x01000000,
203
204 // Indicates that the input event is from a trusted source such as a directly attached
205 // input device or an application with system-wide event injection permission.
206 POLICY_FLAG_TRUSTED = 0x02000000,
207
208 // Indicates that the input event has passed through an input filter.
209 POLICY_FLAG_FILTERED = 0x04000000,
210
211 // Disables automatic key repeating behavior.
212 POLICY_FLAG_DISABLE_KEY_REPEAT = 0x08000000,
213
214 /* These flags are set by the input reader policy as it intercepts each event. */
215
Jeff Browndb19e462014-04-08 19:55:38 -0700216 // Indicates that the device was in an interactive state when the
217 // event was intercepted.
218 POLICY_FLAG_INTERACTIVE = 0x20000000,
Jeff Brown5912f952013-07-01 19:10:31 -0700219
220 // Indicates that the event should be dispatched to applications.
221 // The input event should still be sent to the InputDispatcher so that it can see all
222 // input events received include those that it will not deliver.
223 POLICY_FLAG_PASS_TO_USER = 0x40000000,
224};
225
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800226/**
227 * Classifications of the current gesture, if available.
228 *
229 * The following values must be kept in sync with MotionEvent.java
230 */
231enum class MotionClassification : uint8_t {
232 /**
233 * No classification is available.
234 */
235 NONE = 0,
236 /**
237 * Too early to classify the current gesture. Need more events. Look for changes in the
238 * upcoming motion events.
239 */
240 AMBIGUOUS_GESTURE = 1,
241 /**
242 * The current gesture likely represents a user intentionally exerting force on the touchscreen.
243 */
244 DEEP_PRESS = 2,
245};
246
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800247/**
248 * String representation of MotionClassification
249 */
250const char* motionClassificationToString(MotionClassification classification);
251
Garfield Tan00f511d2019-06-12 16:55:40 -0700252/**
253 * Invalid value for cursor position. Used for non-mouse events, tests and injected events. Don't
254 * use it for direct comparison with any other value, because NaN isn't equal to itself according to
255 * IEEE 754. Use isnan() instead to check if a cursor position is valid.
256 */
257constexpr float AMOTION_EVENT_INVALID_CURSOR_POSITION = std::numeric_limits<float>::quiet_NaN();
258
Jeff Brown5912f952013-07-01 19:10:31 -0700259/*
260 * Pointer coordinate data.
261 */
262struct PointerCoords {
Michael Wright8f6710f2014-06-09 18:56:43 -0700263 enum { MAX_AXES = 30 }; // 30 so that sizeof(PointerCoords) == 128
Jeff Brown5912f952013-07-01 19:10:31 -0700264
265 // Bitfield of axes that are present in this structure.
Fengwei Yin83e0e422014-05-24 05:32:09 +0800266 uint64_t bits __attribute__((aligned(8)));
Jeff Brown5912f952013-07-01 19:10:31 -0700267
268 // Values of axes that are stored in this structure packed in order by axis id
269 // for each axis that is present in the structure according to 'bits'.
270 float values[MAX_AXES];
271
272 inline void clear() {
Michael Wrightd0bd3912014-03-19 12:06:10 -0700273 BitSet64::clear(bits);
274 }
275
276 bool isEmpty() const {
277 return BitSet64::isEmpty(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700278 }
279
280 float getAxisValue(int32_t axis) const;
281 status_t setAxisValue(int32_t axis, float value);
282
Robert Carre07e1032018-11-26 12:55:53 -0800283 void scale(float globalScale);
284
285 // Scale the pointer coordinates according to a global scale and a
286 // window scale. The global scale will be applied to TOUCH/TOOL_MAJOR/MINOR
287 // axes, however the window scaling will not.
288 void scale(float globalScale, float windowXScale, float windowYScale);
Jeff Browned4d28d2014-02-11 14:28:48 -0800289 void applyOffset(float xOffset, float yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700290
291 inline float getX() const {
292 return getAxisValue(AMOTION_EVENT_AXIS_X);
293 }
294
295 inline float getY() const {
296 return getAxisValue(AMOTION_EVENT_AXIS_Y);
297 }
298
Elliott Hughes6071da72015-08-12 15:27:47 -0700299#ifdef __ANDROID__
Jeff Brown5912f952013-07-01 19:10:31 -0700300 status_t readFromParcel(Parcel* parcel);
301 status_t writeToParcel(Parcel* parcel) const;
302#endif
303
304 bool operator==(const PointerCoords& other) const;
305 inline bool operator!=(const PointerCoords& other) const {
306 return !(*this == other);
307 }
308
309 void copyFrom(const PointerCoords& other);
310
311private:
312 void tooManyAxes(int axis);
313};
314
315/*
316 * Pointer property data.
317 */
318struct PointerProperties {
319 // The id of the pointer.
320 int32_t id;
321
322 // The pointer tool type.
323 int32_t toolType;
324
325 inline void clear() {
326 id = -1;
327 toolType = 0;
328 }
329
330 bool operator==(const PointerProperties& other) const;
331 inline bool operator!=(const PointerProperties& other) const {
332 return !(*this == other);
333 }
334
335 void copyFrom(const PointerProperties& other);
336};
337
338/*
339 * Input events.
340 */
341class InputEvent : public AInputEvent {
342public:
343 virtual ~InputEvent() { }
344
345 virtual int32_t getType() const = 0;
346
347 inline int32_t getDeviceId() const { return mDeviceId; }
348
349 inline int32_t getSource() const { return mSource; }
350
351 inline void setSource(int32_t source) { mSource = source; }
352
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100353 inline int32_t getDisplayId() const { return mDisplayId; }
354
355 inline void setDisplayId(int32_t displayId) { mDisplayId = displayId; }
356
357
Jeff Brown5912f952013-07-01 19:10:31 -0700358protected:
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100359 void initialize(int32_t deviceId, int32_t source, int32_t displayId);
Jeff Brown5912f952013-07-01 19:10:31 -0700360 void initialize(const InputEvent& from);
361
362 int32_t mDeviceId;
363 int32_t mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100364 int32_t mDisplayId;
Jeff Brown5912f952013-07-01 19:10:31 -0700365};
366
367/*
368 * Key events.
369 */
370class KeyEvent : public InputEvent {
371public:
372 virtual ~KeyEvent() { }
373
374 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_KEY; }
375
376 inline int32_t getAction() const { return mAction; }
377
378 inline int32_t getFlags() const { return mFlags; }
379
380 inline void setFlags(int32_t flags) { mFlags = flags; }
381
382 inline int32_t getKeyCode() const { return mKeyCode; }
383
384 inline int32_t getScanCode() const { return mScanCode; }
385
386 inline int32_t getMetaState() const { return mMetaState; }
387
388 inline int32_t getRepeatCount() const { return mRepeatCount; }
389
390 inline nsecs_t getDownTime() const { return mDownTime; }
391
392 inline nsecs_t getEventTime() const { return mEventTime; }
393
Michael Wright872db4f2014-04-22 15:03:51 -0700394 static const char* getLabel(int32_t keyCode);
395 static int32_t getKeyCodeFromLabel(const char* label);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800396
Jeff Brown5912f952013-07-01 19:10:31 -0700397 void initialize(
398 int32_t deviceId,
399 int32_t source,
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100400 int32_t displayId,
Jeff Brown5912f952013-07-01 19:10:31 -0700401 int32_t action,
402 int32_t flags,
403 int32_t keyCode,
404 int32_t scanCode,
405 int32_t metaState,
406 int32_t repeatCount,
407 nsecs_t downTime,
408 nsecs_t eventTime);
409 void initialize(const KeyEvent& from);
410
411protected:
412 int32_t mAction;
413 int32_t mFlags;
414 int32_t mKeyCode;
415 int32_t mScanCode;
416 int32_t mMetaState;
417 int32_t mRepeatCount;
418 nsecs_t mDownTime;
419 nsecs_t mEventTime;
420};
421
422/*
423 * Motion events.
424 */
425class MotionEvent : public InputEvent {
426public:
427 virtual ~MotionEvent() { }
428
429 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_MOTION; }
430
431 inline int32_t getAction() const { return mAction; }
432
433 inline int32_t getActionMasked() const { return mAction & AMOTION_EVENT_ACTION_MASK; }
434
435 inline int32_t getActionIndex() const {
436 return (mAction & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK)
437 >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
438 }
439
440 inline void setAction(int32_t action) { mAction = action; }
441
442 inline int32_t getFlags() const { return mFlags; }
443
444 inline void setFlags(int32_t flags) { mFlags = flags; }
445
446 inline int32_t getEdgeFlags() const { return mEdgeFlags; }
447
448 inline void setEdgeFlags(int32_t edgeFlags) { mEdgeFlags = edgeFlags; }
449
450 inline int32_t getMetaState() const { return mMetaState; }
451
452 inline void setMetaState(int32_t metaState) { mMetaState = metaState; }
453
454 inline int32_t getButtonState() const { return mButtonState; }
455
Michael Wright6db58792016-09-14 19:53:37 +0100456 inline void setButtonState(int32_t buttonState) { mButtonState = buttonState; }
Michael Wright7b159c92015-05-14 14:48:03 +0100457
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800458 inline MotionClassification getClassification() const { return mClassification; }
459
Michael Wright7b159c92015-05-14 14:48:03 +0100460 inline int32_t getActionButton() const { return mActionButton; }
461
Michael Wright21957b92015-06-17 21:06:54 +0100462 inline void setActionButton(int32_t button) { mActionButton = button; }
463
Jeff Brown5912f952013-07-01 19:10:31 -0700464 inline float getXOffset() const { return mXOffset; }
465
466 inline float getYOffset() const { return mYOffset; }
467
468 inline float getXPrecision() const { return mXPrecision; }
469
470 inline float getYPrecision() const { return mYPrecision; }
471
Garfield Tan937bb832019-07-25 17:48:31 -0700472 inline float getRawXCursorPosition() const { return mRawXCursorPosition; }
Garfield Tan00f511d2019-06-12 16:55:40 -0700473
474 float getXCursorPosition() const;
475
Garfield Tan937bb832019-07-25 17:48:31 -0700476 inline float getRawYCursorPosition() const { return mRawYCursorPosition; }
Garfield Tan00f511d2019-06-12 16:55:40 -0700477
478 float getYCursorPosition() const;
479
Garfield Tan937bb832019-07-25 17:48:31 -0700480 void setCursorPosition(float x, float y);
481
Garfield Tanab0ab9c2019-07-10 18:58:28 -0700482 static inline bool isValidCursorPosition(float x, float y) { return !isnan(x) && !isnan(y); }
483
Jeff Brown5912f952013-07-01 19:10:31 -0700484 inline nsecs_t getDownTime() const { return mDownTime; }
485
486 inline void setDownTime(nsecs_t downTime) { mDownTime = downTime; }
487
488 inline size_t getPointerCount() const { return mPointerProperties.size(); }
489
490 inline const PointerProperties* getPointerProperties(size_t pointerIndex) const {
491 return &mPointerProperties[pointerIndex];
492 }
493
494 inline int32_t getPointerId(size_t pointerIndex) const {
495 return mPointerProperties[pointerIndex].id;
496 }
497
498 inline int32_t getToolType(size_t pointerIndex) const {
499 return mPointerProperties[pointerIndex].toolType;
500 }
501
502 inline nsecs_t getEventTime() const { return mSampleEventTimes[getHistorySize()]; }
503
504 const PointerCoords* getRawPointerCoords(size_t pointerIndex) const;
505
506 float getRawAxisValue(int32_t axis, size_t pointerIndex) const;
507
508 inline float getRawX(size_t pointerIndex) const {
509 return getRawAxisValue(AMOTION_EVENT_AXIS_X, pointerIndex);
510 }
511
512 inline float getRawY(size_t pointerIndex) const {
513 return getRawAxisValue(AMOTION_EVENT_AXIS_Y, pointerIndex);
514 }
515
516 float getAxisValue(int32_t axis, size_t pointerIndex) const;
517
518 inline float getX(size_t pointerIndex) const {
519 return getAxisValue(AMOTION_EVENT_AXIS_X, pointerIndex);
520 }
521
522 inline float getY(size_t pointerIndex) const {
523 return getAxisValue(AMOTION_EVENT_AXIS_Y, pointerIndex);
524 }
525
526 inline float getPressure(size_t pointerIndex) const {
527 return getAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pointerIndex);
528 }
529
530 inline float getSize(size_t pointerIndex) const {
531 return getAxisValue(AMOTION_EVENT_AXIS_SIZE, pointerIndex);
532 }
533
534 inline float getTouchMajor(size_t pointerIndex) const {
535 return getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, pointerIndex);
536 }
537
538 inline float getTouchMinor(size_t pointerIndex) const {
539 return getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, pointerIndex);
540 }
541
542 inline float getToolMajor(size_t pointerIndex) const {
543 return getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, pointerIndex);
544 }
545
546 inline float getToolMinor(size_t pointerIndex) const {
547 return getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, pointerIndex);
548 }
549
550 inline float getOrientation(size_t pointerIndex) const {
551 return getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, pointerIndex);
552 }
553
554 inline size_t getHistorySize() const { return mSampleEventTimes.size() - 1; }
555
556 inline nsecs_t getHistoricalEventTime(size_t historicalIndex) const {
557 return mSampleEventTimes[historicalIndex];
558 }
559
560 const PointerCoords* getHistoricalRawPointerCoords(
561 size_t pointerIndex, size_t historicalIndex) const;
562
563 float getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
564 size_t historicalIndex) const;
565
566 inline float getHistoricalRawX(size_t pointerIndex, size_t historicalIndex) const {
567 return getHistoricalRawAxisValue(
568 AMOTION_EVENT_AXIS_X, pointerIndex, historicalIndex);
569 }
570
571 inline float getHistoricalRawY(size_t pointerIndex, size_t historicalIndex) const {
572 return getHistoricalRawAxisValue(
573 AMOTION_EVENT_AXIS_Y, pointerIndex, historicalIndex);
574 }
575
576 float getHistoricalAxisValue(int32_t axis, size_t pointerIndex, size_t historicalIndex) const;
577
578 inline float getHistoricalX(size_t pointerIndex, size_t historicalIndex) const {
579 return getHistoricalAxisValue(
580 AMOTION_EVENT_AXIS_X, pointerIndex, historicalIndex);
581 }
582
583 inline float getHistoricalY(size_t pointerIndex, size_t historicalIndex) const {
584 return getHistoricalAxisValue(
585 AMOTION_EVENT_AXIS_Y, pointerIndex, historicalIndex);
586 }
587
588 inline float getHistoricalPressure(size_t pointerIndex, size_t historicalIndex) const {
589 return getHistoricalAxisValue(
590 AMOTION_EVENT_AXIS_PRESSURE, pointerIndex, historicalIndex);
591 }
592
593 inline float getHistoricalSize(size_t pointerIndex, size_t historicalIndex) const {
594 return getHistoricalAxisValue(
595 AMOTION_EVENT_AXIS_SIZE, pointerIndex, historicalIndex);
596 }
597
598 inline float getHistoricalTouchMajor(size_t pointerIndex, size_t historicalIndex) const {
599 return getHistoricalAxisValue(
600 AMOTION_EVENT_AXIS_TOUCH_MAJOR, pointerIndex, historicalIndex);
601 }
602
603 inline float getHistoricalTouchMinor(size_t pointerIndex, size_t historicalIndex) const {
604 return getHistoricalAxisValue(
605 AMOTION_EVENT_AXIS_TOUCH_MINOR, pointerIndex, historicalIndex);
606 }
607
608 inline float getHistoricalToolMajor(size_t pointerIndex, size_t historicalIndex) const {
609 return getHistoricalAxisValue(
610 AMOTION_EVENT_AXIS_TOOL_MAJOR, pointerIndex, historicalIndex);
611 }
612
613 inline float getHistoricalToolMinor(size_t pointerIndex, size_t historicalIndex) const {
614 return getHistoricalAxisValue(
615 AMOTION_EVENT_AXIS_TOOL_MINOR, pointerIndex, historicalIndex);
616 }
617
618 inline float getHistoricalOrientation(size_t pointerIndex, size_t historicalIndex) const {
619 return getHistoricalAxisValue(
620 AMOTION_EVENT_AXIS_ORIENTATION, pointerIndex, historicalIndex);
621 }
622
623 ssize_t findPointerIndex(int32_t pointerId) const;
624
Garfield Tan00f511d2019-06-12 16:55:40 -0700625 void initialize(int32_t deviceId, int32_t source, int32_t displayId, int32_t action,
626 int32_t actionButton, int32_t flags, int32_t edgeFlags, int32_t metaState,
627 int32_t buttonState, MotionClassification classification, float xOffset,
Garfield Tan937bb832019-07-25 17:48:31 -0700628 float yOffset, float xPrecision, float yPrecision, float rawXCursorPosition,
629 float rawYCursorPosition, nsecs_t downTime, nsecs_t eventTime,
Garfield Tan00f511d2019-06-12 16:55:40 -0700630 size_t pointerCount, const PointerProperties* pointerProperties,
631 const PointerCoords* pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -0700632
633 void copyFrom(const MotionEvent* other, bool keepHistory);
634
635 void addSample(
636 nsecs_t eventTime,
637 const PointerCoords* pointerCoords);
638
639 void offsetLocation(float xOffset, float yOffset);
640
Robert Carre07e1032018-11-26 12:55:53 -0800641 void scale(float globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700642
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700643 // Apply 3x3 perspective matrix transformation.
644 // Matrix is in row-major form and compatible with SkMatrix.
645 void transform(const float matrix[9]);
Jeff Brown5912f952013-07-01 19:10:31 -0700646
Elliott Hughes6071da72015-08-12 15:27:47 -0700647#ifdef __ANDROID__
Jeff Brown5912f952013-07-01 19:10:31 -0700648 status_t readFromParcel(Parcel* parcel);
649 status_t writeToParcel(Parcel* parcel) const;
650#endif
651
652 static bool isTouchEvent(int32_t source, int32_t action);
653 inline bool isTouchEvent() const {
654 return isTouchEvent(mSource, mAction);
655 }
656
657 // Low-level accessors.
658 inline const PointerProperties* getPointerProperties() const {
659 return mPointerProperties.array();
660 }
661 inline const nsecs_t* getSampleEventTimes() const { return mSampleEventTimes.array(); }
662 inline const PointerCoords* getSamplePointerCoords() const {
663 return mSamplePointerCoords.array();
664 }
665
Michael Wright872db4f2014-04-22 15:03:51 -0700666 static const char* getLabel(int32_t axis);
667 static int32_t getAxisFromLabel(const char* label);
668
Jeff Brown5912f952013-07-01 19:10:31 -0700669protected:
670 int32_t mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100671 int32_t mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700672 int32_t mFlags;
673 int32_t mEdgeFlags;
674 int32_t mMetaState;
675 int32_t mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800676 MotionClassification mClassification;
Jeff Brown5912f952013-07-01 19:10:31 -0700677 float mXOffset;
678 float mYOffset;
679 float mXPrecision;
680 float mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700681 float mRawXCursorPosition;
682 float mRawYCursorPosition;
Jeff Brown5912f952013-07-01 19:10:31 -0700683 nsecs_t mDownTime;
684 Vector<PointerProperties> mPointerProperties;
685 Vector<nsecs_t> mSampleEventTimes;
686 Vector<PointerCoords> mSamplePointerCoords;
687};
688
689/*
690 * Input event factory.
691 */
692class InputEventFactoryInterface {
693protected:
694 virtual ~InputEventFactoryInterface() { }
695
696public:
697 InputEventFactoryInterface() { }
698
699 virtual KeyEvent* createKeyEvent() = 0;
700 virtual MotionEvent* createMotionEvent() = 0;
701};
702
703/*
704 * A simple input event factory implementation that uses a single preallocated instance
705 * of each type of input event that are reused for each request.
706 */
707class PreallocatedInputEventFactory : public InputEventFactoryInterface {
708public:
709 PreallocatedInputEventFactory() { }
710 virtual ~PreallocatedInputEventFactory() { }
711
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800712 virtual KeyEvent* createKeyEvent() override { return &mKeyEvent; }
713 virtual MotionEvent* createMotionEvent() override { return &mMotionEvent; }
Jeff Brown5912f952013-07-01 19:10:31 -0700714
715private:
716 KeyEvent mKeyEvent;
717 MotionEvent mMotionEvent;
718};
719
720/*
721 * An input event factory implementation that maintains a pool of input events.
722 */
723class PooledInputEventFactory : public InputEventFactoryInterface {
724public:
Chih-Hung Hsiehf43b02c2018-12-20 15:45:56 -0800725 explicit PooledInputEventFactory(size_t maxPoolSize = 20);
Jeff Brown5912f952013-07-01 19:10:31 -0700726 virtual ~PooledInputEventFactory();
727
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800728 virtual KeyEvent* createKeyEvent() override;
729 virtual MotionEvent* createMotionEvent() override;
Jeff Brown5912f952013-07-01 19:10:31 -0700730
731 void recycle(InputEvent* event);
732
733private:
734 const size_t mMaxPoolSize;
735
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800736 std::queue<std::unique_ptr<KeyEvent>> mKeyEventPool;
737 std::queue<std::unique_ptr<MotionEvent>> mMotionEventPool;
Jeff Brown5912f952013-07-01 19:10:31 -0700738};
739
740} // namespace android
741
742#endif // _LIBINPUT_INPUT_H