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