blob: b86d761e488082ae14dc0ebdae4345d0843a33ba [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
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -080076/**
77 * Allowed VerifiedKeyEvent flags. All other flags from KeyEvent do not get verified.
78 * These values must be kept in sync with VerifiedKeyEvent.java
79 */
80constexpr int32_t VERIFIED_KEY_EVENT_FLAGS = AKEY_EVENT_FLAG_CANCELED;
81
82/**
83 * Allowed VerifiedMotionEventFlags. All other flags from MotionEvent do not get verified.
84 * These values must be kept in sync with VerifiedMotionEvent.java
85 */
86constexpr int32_t VERIFIED_MOTION_EVENT_FLAGS =
87 AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED;
88
Jeff Brown5912f952013-07-01 19:10:31 -070089enum {
90 /* Used when a motion event is not associated with any display.
91 * Typically used for non-pointer events. */
92 ADISPLAY_ID_NONE = -1,
93
94 /* The default display id. */
95 ADISPLAY_ID_DEFAULT = 0,
96};
97
98enum {
99 /*
100 * Indicates that an input device has switches.
101 * This input source flag is hidden from the API because switches are only used by the system
102 * and applications have no way to interact with them.
103 */
104 AINPUT_SOURCE_SWITCH = 0x80000000,
105};
106
Michael Wright962a1082013-10-17 17:35:53 -0700107enum {
108 /**
109 * Constants for LEDs. Hidden from the API since we don't actually expose a way to interact
110 * with LEDs to developers
111 *
Michael Wright872db4f2014-04-22 15:03:51 -0700112 * NOTE: If you add LEDs here, you must also add them to InputEventLabels.h
Michael Wright962a1082013-10-17 17:35:53 -0700113 */
114
115 ALED_NUM_LOCK = 0x00,
116 ALED_CAPS_LOCK = 0x01,
117 ALED_SCROLL_LOCK = 0x02,
118 ALED_COMPOSE = 0x03,
119 ALED_KANA = 0x04,
120 ALED_SLEEP = 0x05,
121 ALED_SUSPEND = 0x06,
122 ALED_MUTE = 0x07,
123 ALED_MISC = 0x08,
124 ALED_MAIL = 0x09,
125 ALED_CHARGING = 0x0a,
126 ALED_CONTROLLER_1 = 0x10,
127 ALED_CONTROLLER_2 = 0x11,
128 ALED_CONTROLLER_3 = 0x12,
129 ALED_CONTROLLER_4 = 0x13,
130};
131
Michael Wright9b04f862013-10-18 17:53:50 -0700132/* Maximum number of controller LEDs we support */
133#define MAX_CONTROLLER_LEDS 4
134
Jeff Brown5912f952013-07-01 19:10:31 -0700135/*
136 * SystemUiVisibility constants from View.
137 */
138enum {
139 ASYSTEM_UI_VISIBILITY_STATUS_BAR_VISIBLE = 0,
140 ASYSTEM_UI_VISIBILITY_STATUS_BAR_HIDDEN = 0x00000001,
141};
142
143/*
144 * Maximum number of pointers supported per motion event.
145 * Smallest number of pointers is 1.
146 * (We want at least 10 but some touch controllers obstensibly configured for 10 pointers
147 * will occasionally emit 11. There is not much harm making this constant bigger.)
148 */
149#define MAX_POINTERS 16
150
151/*
Flanker552a8a52015-09-07 15:28:58 +0800152 * Maximum number of samples supported per motion event.
153 */
154#define MAX_SAMPLES UINT16_MAX
155
156/*
Jeff Brown5912f952013-07-01 19:10:31 -0700157 * Maximum pointer id value supported in a motion event.
158 * Smallest pointer id is 0.
159 * (This is limited by our use of BitSet32 to track pointer assignments.)
160 */
161#define MAX_POINTER_ID 31
162
163/*
164 * Declare a concrete type for the NDK's input event forward declaration.
165 */
166struct AInputEvent {
167 virtual ~AInputEvent() { }
168};
169
170/*
171 * Declare a concrete type for the NDK's input device forward declaration.
172 */
173struct AInputDevice {
174 virtual ~AInputDevice() { }
175};
176
177
178namespace android {
179
Elliott Hughes6071da72015-08-12 15:27:47 -0700180#ifdef __ANDROID__
Jeff Brown5912f952013-07-01 19:10:31 -0700181class Parcel;
182#endif
183
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800184const char* inputEventTypeToString(int32_t type);
185
Jeff Brown5912f952013-07-01 19:10:31 -0700186/*
187 * Flags that flow alongside events in the input dispatch system to help with certain
188 * policy decisions such as waking from device sleep.
189 *
190 * These flags are also defined in frameworks/base/core/java/android/view/WindowManagerPolicy.java.
191 */
192enum {
193 /* These flags originate in RawEvents and are generally set in the key map.
Michael Wright872db4f2014-04-22 15:03:51 -0700194 * NOTE: If you want a flag to be able to set in a keylayout file, then you must add it to
195 * InputEventLabels.h as well. */
Jeff Brown5912f952013-07-01 19:10:31 -0700196
Jeff Brownc9aa6282015-02-11 19:03:28 -0800197 // Indicates that the event should wake the device.
Jeff Brown5912f952013-07-01 19:10:31 -0700198 POLICY_FLAG_WAKE = 0x00000001,
Jeff Brownc9aa6282015-02-11 19:03:28 -0800199
200 // Indicates that the key is virtual, such as a capacitive button, and should
201 // generate haptic feedback. Virtual keys may be suppressed for some time
202 // after a recent touch to prevent accidental activation of virtual keys adjacent
203 // to the touch screen during an edge swipe.
Michael Wright872db4f2014-04-22 15:03:51 -0700204 POLICY_FLAG_VIRTUAL = 0x00000002,
Jeff Brownc9aa6282015-02-11 19:03:28 -0800205
206 // Indicates that the key is the special function modifier.
Michael Wright872db4f2014-04-22 15:03:51 -0700207 POLICY_FLAG_FUNCTION = 0x00000004,
Jeff Brown5912f952013-07-01 19:10:31 -0700208
Jeff Brownc9aa6282015-02-11 19:03:28 -0800209 // Indicates that the key represents a special gesture that has been detected by
210 // the touch firmware or driver. Causes touch events from the same device to be canceled.
211 POLICY_FLAG_GESTURE = 0x00000008,
212
Jeff Brown5912f952013-07-01 19:10:31 -0700213 POLICY_FLAG_RAW_MASK = 0x0000ffff,
214
215 /* These flags are set by the input dispatcher. */
216
217 // Indicates that the input event was injected.
218 POLICY_FLAG_INJECTED = 0x01000000,
219
220 // Indicates that the input event is from a trusted source such as a directly attached
221 // input device or an application with system-wide event injection permission.
222 POLICY_FLAG_TRUSTED = 0x02000000,
223
224 // Indicates that the input event has passed through an input filter.
225 POLICY_FLAG_FILTERED = 0x04000000,
226
227 // Disables automatic key repeating behavior.
228 POLICY_FLAG_DISABLE_KEY_REPEAT = 0x08000000,
229
230 /* These flags are set by the input reader policy as it intercepts each event. */
231
Jeff Browndb19e462014-04-08 19:55:38 -0700232 // Indicates that the device was in an interactive state when the
233 // event was intercepted.
234 POLICY_FLAG_INTERACTIVE = 0x20000000,
Jeff Brown5912f952013-07-01 19:10:31 -0700235
236 // Indicates that the event should be dispatched to applications.
237 // The input event should still be sent to the InputDispatcher so that it can see all
238 // input events received include those that it will not deliver.
239 POLICY_FLAG_PASS_TO_USER = 0x40000000,
240};
241
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800242/**
243 * Classifications of the current gesture, if available.
244 *
245 * The following values must be kept in sync with MotionEvent.java
246 */
247enum class MotionClassification : uint8_t {
248 /**
249 * No classification is available.
250 */
251 NONE = 0,
252 /**
253 * Too early to classify the current gesture. Need more events. Look for changes in the
254 * upcoming motion events.
255 */
256 AMBIGUOUS_GESTURE = 1,
257 /**
258 * The current gesture likely represents a user intentionally exerting force on the touchscreen.
259 */
260 DEEP_PRESS = 2,
261};
262
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800263/**
264 * String representation of MotionClassification
265 */
266const char* motionClassificationToString(MotionClassification classification);
267
Garfield Tan00f511d2019-06-12 16:55:40 -0700268/**
Garfield Tan84b087e2020-01-23 10:49:05 -0800269 * Generator of unique numbers used to identify input events.
270 *
271 * Layout of ID:
272 * |--------------------------|---------------------------|
273 * | 2 bits for source | 30 bits for random number |
274 * |--------------------------|---------------------------|
275 */
276class IdGenerator {
277private:
278 static constexpr uint32_t SOURCE_SHIFT = 30;
279
280public:
281 // Used to divide integer space to ensure no conflict among these sources./
282 enum class Source : int32_t {
283 INPUT_READER = 0x0 << SOURCE_SHIFT,
284 INPUT_DISPATCHER = 0x1 << SOURCE_SHIFT,
285 OTHER = 0x3 << SOURCE_SHIFT, // E.g. app injected events
286 };
287 IdGenerator(Source source);
288
289 int32_t nextId() const;
290
291 // Extract source from given id.
292 static inline Source getSource(int32_t id) { return static_cast<Source>(SOURCE_MASK & id); }
293
294private:
295 const Source mSource;
296
297 static constexpr int32_t SOURCE_MASK = 0x3 << SOURCE_SHIFT;
298};
299
300/**
Garfield Tan00f511d2019-06-12 16:55:40 -0700301 * Invalid value for cursor position. Used for non-mouse events, tests and injected events. Don't
302 * use it for direct comparison with any other value, because NaN isn't equal to itself according to
303 * IEEE 754. Use isnan() instead to check if a cursor position is valid.
304 */
305constexpr float AMOTION_EVENT_INVALID_CURSOR_POSITION = std::numeric_limits<float>::quiet_NaN();
306
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600307/**
308 * Invalid value of HMAC - SHA256. Any events with this HMAC value will be marked as not verified.
309 */
310constexpr std::array<uint8_t, 32> INVALID_HMAC = {0};
311
Jeff Brown5912f952013-07-01 19:10:31 -0700312/*
313 * Pointer coordinate data.
314 */
315struct PointerCoords {
Michael Wright8f6710f2014-06-09 18:56:43 -0700316 enum { MAX_AXES = 30 }; // 30 so that sizeof(PointerCoords) == 128
Jeff Brown5912f952013-07-01 19:10:31 -0700317
318 // Bitfield of axes that are present in this structure.
Fengwei Yin83e0e422014-05-24 05:32:09 +0800319 uint64_t bits __attribute__((aligned(8)));
Jeff Brown5912f952013-07-01 19:10:31 -0700320
321 // Values of axes that are stored in this structure packed in order by axis id
322 // for each axis that is present in the structure according to 'bits'.
323 float values[MAX_AXES];
324
325 inline void clear() {
Michael Wrightd0bd3912014-03-19 12:06:10 -0700326 BitSet64::clear(bits);
327 }
328
329 bool isEmpty() const {
330 return BitSet64::isEmpty(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700331 }
332
333 float getAxisValue(int32_t axis) const;
334 status_t setAxisValue(int32_t axis, float value);
335
Robert Carre07e1032018-11-26 12:55:53 -0800336 void scale(float globalScale);
337
338 // Scale the pointer coordinates according to a global scale and a
339 // window scale. The global scale will be applied to TOUCH/TOOL_MAJOR/MINOR
340 // axes, however the window scaling will not.
341 void scale(float globalScale, float windowXScale, float windowYScale);
Jeff Browned4d28d2014-02-11 14:28:48 -0800342 void applyOffset(float xOffset, float yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700343
344 inline float getX() const {
345 return getAxisValue(AMOTION_EVENT_AXIS_X);
346 }
347
348 inline float getY() const {
349 return getAxisValue(AMOTION_EVENT_AXIS_Y);
350 }
351
Elliott Hughes6071da72015-08-12 15:27:47 -0700352#ifdef __ANDROID__
Jeff Brown5912f952013-07-01 19:10:31 -0700353 status_t readFromParcel(Parcel* parcel);
354 status_t writeToParcel(Parcel* parcel) const;
355#endif
356
357 bool operator==(const PointerCoords& other) const;
358 inline bool operator!=(const PointerCoords& other) const {
359 return !(*this == other);
360 }
361
362 void copyFrom(const PointerCoords& other);
363
364private:
365 void tooManyAxes(int axis);
366};
367
368/*
369 * Pointer property data.
370 */
371struct PointerProperties {
372 // The id of the pointer.
373 int32_t id;
374
375 // The pointer tool type.
376 int32_t toolType;
377
378 inline void clear() {
379 id = -1;
380 toolType = 0;
381 }
382
383 bool operator==(const PointerProperties& other) const;
384 inline bool operator!=(const PointerProperties& other) const {
385 return !(*this == other);
386 }
387
388 void copyFrom(const PointerProperties& other);
389};
390
391/*
392 * Input events.
393 */
394class InputEvent : public AInputEvent {
395public:
396 virtual ~InputEvent() { }
397
398 virtual int32_t getType() const = 0;
399
400 inline int32_t getDeviceId() const { return mDeviceId; }
401
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600402 inline uint32_t getSource() const { return mSource; }
Jeff Brown5912f952013-07-01 19:10:31 -0700403
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600404 inline void setSource(uint32_t source) { mSource = source; }
Jeff Brown5912f952013-07-01 19:10:31 -0700405
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100406 inline int32_t getDisplayId() const { return mDisplayId; }
407
408 inline void setDisplayId(int32_t displayId) { mDisplayId = displayId; }
409
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600410 inline std::array<uint8_t, 32> getHmac() const { return mHmac; }
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100411
Jeff Brown5912f952013-07-01 19:10:31 -0700412protected:
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600413 void initialize(int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600414 std::array<uint8_t, 32> hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700415 void initialize(const InputEvent& from);
416
417 int32_t mDeviceId;
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600418 uint32_t mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100419 int32_t mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600420 std::array<uint8_t, 32> mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700421};
422
423/*
424 * Key events.
425 */
426class KeyEvent : public InputEvent {
427public:
428 virtual ~KeyEvent() { }
429
430 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_KEY; }
431
432 inline int32_t getAction() const { return mAction; }
433
434 inline int32_t getFlags() const { return mFlags; }
435
436 inline void setFlags(int32_t flags) { mFlags = flags; }
437
438 inline int32_t getKeyCode() const { return mKeyCode; }
439
440 inline int32_t getScanCode() const { return mScanCode; }
441
442 inline int32_t getMetaState() const { return mMetaState; }
443
444 inline int32_t getRepeatCount() const { return mRepeatCount; }
445
446 inline nsecs_t getDownTime() const { return mDownTime; }
447
448 inline nsecs_t getEventTime() const { return mEventTime; }
449
Michael Wright872db4f2014-04-22 15:03:51 -0700450 static const char* getLabel(int32_t keyCode);
451 static int32_t getKeyCodeFromLabel(const char* label);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800452
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600453 void initialize(int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600454 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags, int32_t keyCode,
455 int32_t scanCode, int32_t metaState, int32_t repeatCount, nsecs_t downTime,
456 nsecs_t eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -0700457 void initialize(const KeyEvent& from);
458
459protected:
460 int32_t mAction;
461 int32_t mFlags;
462 int32_t mKeyCode;
463 int32_t mScanCode;
464 int32_t mMetaState;
465 int32_t mRepeatCount;
466 nsecs_t mDownTime;
467 nsecs_t mEventTime;
468};
469
470/*
471 * Motion events.
472 */
473class MotionEvent : public InputEvent {
474public:
475 virtual ~MotionEvent() { }
476
477 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_MOTION; }
478
479 inline int32_t getAction() const { return mAction; }
480
481 inline int32_t getActionMasked() const { return mAction & AMOTION_EVENT_ACTION_MASK; }
482
483 inline int32_t getActionIndex() const {
484 return (mAction & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK)
485 >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
486 }
487
488 inline void setAction(int32_t action) { mAction = action; }
489
490 inline int32_t getFlags() const { return mFlags; }
491
492 inline void setFlags(int32_t flags) { mFlags = flags; }
493
494 inline int32_t getEdgeFlags() const { return mEdgeFlags; }
495
496 inline void setEdgeFlags(int32_t edgeFlags) { mEdgeFlags = edgeFlags; }
497
498 inline int32_t getMetaState() const { return mMetaState; }
499
500 inline void setMetaState(int32_t metaState) { mMetaState = metaState; }
501
502 inline int32_t getButtonState() const { return mButtonState; }
503
Michael Wright6db58792016-09-14 19:53:37 +0100504 inline void setButtonState(int32_t buttonState) { mButtonState = buttonState; }
Michael Wright7b159c92015-05-14 14:48:03 +0100505
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800506 inline MotionClassification getClassification() const { return mClassification; }
507
Michael Wright7b159c92015-05-14 14:48:03 +0100508 inline int32_t getActionButton() const { return mActionButton; }
509
Michael Wright21957b92015-06-17 21:06:54 +0100510 inline void setActionButton(int32_t button) { mActionButton = button; }
511
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600512 inline float getXScale() const { return mXScale; }
513
514 inline float getYScale() const { return mYScale; }
515
Jeff Brown5912f952013-07-01 19:10:31 -0700516 inline float getXOffset() const { return mXOffset; }
517
518 inline float getYOffset() const { return mYOffset; }
519
520 inline float getXPrecision() const { return mXPrecision; }
521
522 inline float getYPrecision() const { return mYPrecision; }
523
Garfield Tan937bb832019-07-25 17:48:31 -0700524 inline float getRawXCursorPosition() const { return mRawXCursorPosition; }
Garfield Tan00f511d2019-06-12 16:55:40 -0700525
526 float getXCursorPosition() const;
527
Garfield Tan937bb832019-07-25 17:48:31 -0700528 inline float getRawYCursorPosition() const { return mRawYCursorPosition; }
Garfield Tan00f511d2019-06-12 16:55:40 -0700529
530 float getYCursorPosition() const;
531
Garfield Tan937bb832019-07-25 17:48:31 -0700532 void setCursorPosition(float x, float y);
533
Garfield Tanab0ab9c2019-07-10 18:58:28 -0700534 static inline bool isValidCursorPosition(float x, float y) { return !isnan(x) && !isnan(y); }
535
Jeff Brown5912f952013-07-01 19:10:31 -0700536 inline nsecs_t getDownTime() const { return mDownTime; }
537
538 inline void setDownTime(nsecs_t downTime) { mDownTime = downTime; }
539
540 inline size_t getPointerCount() const { return mPointerProperties.size(); }
541
542 inline const PointerProperties* getPointerProperties(size_t pointerIndex) const {
543 return &mPointerProperties[pointerIndex];
544 }
545
546 inline int32_t getPointerId(size_t pointerIndex) const {
547 return mPointerProperties[pointerIndex].id;
548 }
549
550 inline int32_t getToolType(size_t pointerIndex) const {
551 return mPointerProperties[pointerIndex].toolType;
552 }
553
554 inline nsecs_t getEventTime() const { return mSampleEventTimes[getHistorySize()]; }
555
556 const PointerCoords* getRawPointerCoords(size_t pointerIndex) const;
557
558 float getRawAxisValue(int32_t axis, size_t pointerIndex) const;
559
560 inline float getRawX(size_t pointerIndex) const {
561 return getRawAxisValue(AMOTION_EVENT_AXIS_X, pointerIndex);
562 }
563
564 inline float getRawY(size_t pointerIndex) const {
565 return getRawAxisValue(AMOTION_EVENT_AXIS_Y, pointerIndex);
566 }
567
568 float getAxisValue(int32_t axis, size_t pointerIndex) const;
569
570 inline float getX(size_t pointerIndex) const {
571 return getAxisValue(AMOTION_EVENT_AXIS_X, pointerIndex);
572 }
573
574 inline float getY(size_t pointerIndex) const {
575 return getAxisValue(AMOTION_EVENT_AXIS_Y, pointerIndex);
576 }
577
578 inline float getPressure(size_t pointerIndex) const {
579 return getAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pointerIndex);
580 }
581
582 inline float getSize(size_t pointerIndex) const {
583 return getAxisValue(AMOTION_EVENT_AXIS_SIZE, pointerIndex);
584 }
585
586 inline float getTouchMajor(size_t pointerIndex) const {
587 return getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, pointerIndex);
588 }
589
590 inline float getTouchMinor(size_t pointerIndex) const {
591 return getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, pointerIndex);
592 }
593
594 inline float getToolMajor(size_t pointerIndex) const {
595 return getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, pointerIndex);
596 }
597
598 inline float getToolMinor(size_t pointerIndex) const {
599 return getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, pointerIndex);
600 }
601
602 inline float getOrientation(size_t pointerIndex) const {
603 return getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, pointerIndex);
604 }
605
606 inline size_t getHistorySize() const { return mSampleEventTimes.size() - 1; }
607
608 inline nsecs_t getHistoricalEventTime(size_t historicalIndex) const {
609 return mSampleEventTimes[historicalIndex];
610 }
611
612 const PointerCoords* getHistoricalRawPointerCoords(
613 size_t pointerIndex, size_t historicalIndex) const;
614
615 float getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
616 size_t historicalIndex) const;
617
618 inline float getHistoricalRawX(size_t pointerIndex, size_t historicalIndex) const {
619 return getHistoricalRawAxisValue(
620 AMOTION_EVENT_AXIS_X, pointerIndex, historicalIndex);
621 }
622
623 inline float getHistoricalRawY(size_t pointerIndex, size_t historicalIndex) const {
624 return getHistoricalRawAxisValue(
625 AMOTION_EVENT_AXIS_Y, pointerIndex, historicalIndex);
626 }
627
628 float getHistoricalAxisValue(int32_t axis, size_t pointerIndex, size_t historicalIndex) const;
629
630 inline float getHistoricalX(size_t pointerIndex, size_t historicalIndex) const {
631 return getHistoricalAxisValue(
632 AMOTION_EVENT_AXIS_X, pointerIndex, historicalIndex);
633 }
634
635 inline float getHistoricalY(size_t pointerIndex, size_t historicalIndex) const {
636 return getHistoricalAxisValue(
637 AMOTION_EVENT_AXIS_Y, pointerIndex, historicalIndex);
638 }
639
640 inline float getHistoricalPressure(size_t pointerIndex, size_t historicalIndex) const {
641 return getHistoricalAxisValue(
642 AMOTION_EVENT_AXIS_PRESSURE, pointerIndex, historicalIndex);
643 }
644
645 inline float getHistoricalSize(size_t pointerIndex, size_t historicalIndex) const {
646 return getHistoricalAxisValue(
647 AMOTION_EVENT_AXIS_SIZE, pointerIndex, historicalIndex);
648 }
649
650 inline float getHistoricalTouchMajor(size_t pointerIndex, size_t historicalIndex) const {
651 return getHistoricalAxisValue(
652 AMOTION_EVENT_AXIS_TOUCH_MAJOR, pointerIndex, historicalIndex);
653 }
654
655 inline float getHistoricalTouchMinor(size_t pointerIndex, size_t historicalIndex) const {
656 return getHistoricalAxisValue(
657 AMOTION_EVENT_AXIS_TOUCH_MINOR, pointerIndex, historicalIndex);
658 }
659
660 inline float getHistoricalToolMajor(size_t pointerIndex, size_t historicalIndex) const {
661 return getHistoricalAxisValue(
662 AMOTION_EVENT_AXIS_TOOL_MAJOR, pointerIndex, historicalIndex);
663 }
664
665 inline float getHistoricalToolMinor(size_t pointerIndex, size_t historicalIndex) const {
666 return getHistoricalAxisValue(
667 AMOTION_EVENT_AXIS_TOOL_MINOR, pointerIndex, historicalIndex);
668 }
669
670 inline float getHistoricalOrientation(size_t pointerIndex, size_t historicalIndex) const {
671 return getHistoricalAxisValue(
672 AMOTION_EVENT_AXIS_ORIENTATION, pointerIndex, historicalIndex);
673 }
674
675 ssize_t findPointerIndex(int32_t pointerId) const;
676
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600677 void initialize(int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600678 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
679 int32_t flags, int32_t edgeFlags, int32_t metaState, int32_t buttonState,
680 MotionClassification classification, float xScale, float yScale, float xOffset,
Garfield Tan937bb832019-07-25 17:48:31 -0700681 float yOffset, float xPrecision, float yPrecision, float rawXCursorPosition,
682 float rawYCursorPosition, nsecs_t downTime, nsecs_t eventTime,
Garfield Tan00f511d2019-06-12 16:55:40 -0700683 size_t pointerCount, const PointerProperties* pointerProperties,
684 const PointerCoords* pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -0700685
686 void copyFrom(const MotionEvent* other, bool keepHistory);
687
688 void addSample(
689 nsecs_t eventTime,
690 const PointerCoords* pointerCoords);
691
692 void offsetLocation(float xOffset, float yOffset);
693
Robert Carre07e1032018-11-26 12:55:53 -0800694 void scale(float globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700695
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700696 // Apply 3x3 perspective matrix transformation.
697 // Matrix is in row-major form and compatible with SkMatrix.
698 void transform(const float matrix[9]);
Jeff Brown5912f952013-07-01 19:10:31 -0700699
Elliott Hughes6071da72015-08-12 15:27:47 -0700700#ifdef __ANDROID__
Jeff Brown5912f952013-07-01 19:10:31 -0700701 status_t readFromParcel(Parcel* parcel);
702 status_t writeToParcel(Parcel* parcel) const;
703#endif
704
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600705 static bool isTouchEvent(uint32_t source, int32_t action);
Jeff Brown5912f952013-07-01 19:10:31 -0700706 inline bool isTouchEvent() const {
707 return isTouchEvent(mSource, mAction);
708 }
709
710 // Low-level accessors.
711 inline const PointerProperties* getPointerProperties() const {
712 return mPointerProperties.array();
713 }
714 inline const nsecs_t* getSampleEventTimes() const { return mSampleEventTimes.array(); }
715 inline const PointerCoords* getSamplePointerCoords() const {
716 return mSamplePointerCoords.array();
717 }
718
Michael Wright872db4f2014-04-22 15:03:51 -0700719 static const char* getLabel(int32_t axis);
720 static int32_t getAxisFromLabel(const char* label);
721
Jeff Brown5912f952013-07-01 19:10:31 -0700722protected:
723 int32_t mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100724 int32_t mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700725 int32_t mFlags;
726 int32_t mEdgeFlags;
727 int32_t mMetaState;
728 int32_t mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800729 MotionClassification mClassification;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600730 float mXScale;
731 float mYScale;
Jeff Brown5912f952013-07-01 19:10:31 -0700732 float mXOffset;
733 float mYOffset;
734 float mXPrecision;
735 float mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700736 float mRawXCursorPosition;
737 float mRawYCursorPosition;
Jeff Brown5912f952013-07-01 19:10:31 -0700738 nsecs_t mDownTime;
739 Vector<PointerProperties> mPointerProperties;
740 Vector<nsecs_t> mSampleEventTimes;
741 Vector<PointerCoords> mSamplePointerCoords;
742};
743
744/*
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800745 * Focus events.
746 */
747class FocusEvent : public InputEvent {
748public:
749 virtual ~FocusEvent() {}
750
751 virtual int32_t getType() const override { return AINPUT_EVENT_TYPE_FOCUS; }
752
753 inline bool getHasFocus() const { return mHasFocus; }
754
755 inline bool getInTouchMode() const { return mInTouchMode; }
756
757 void initialize(bool hasFocus, bool inTouchMode);
758
759 void initialize(const FocusEvent& from);
760
761protected:
762 bool mHasFocus;
763 bool mInTouchMode;
764};
765
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800766/**
767 * Base class for verified events.
768 * Do not create a VerifiedInputEvent explicitly.
769 * Use helper functions to create them from InputEvents.
770 */
771struct __attribute__((__packed__)) VerifiedInputEvent {
772 enum class Type : int32_t {
773 KEY = AINPUT_EVENT_TYPE_KEY,
774 MOTION = AINPUT_EVENT_TYPE_MOTION,
775 };
776
777 Type type;
778 int32_t deviceId;
779 nsecs_t eventTimeNanos;
780 uint32_t source;
781 int32_t displayId;
782};
783
784/**
785 * Same as KeyEvent, but only contains the data that can be verified.
786 * If you update this class, you must also update VerifiedKeyEvent.java
787 */
788struct __attribute__((__packed__)) VerifiedKeyEvent : public VerifiedInputEvent {
789 int32_t action;
790 nsecs_t downTimeNanos;
791 int32_t flags;
792 int32_t keyCode;
793 int32_t scanCode;
794 int32_t metaState;
795 int32_t repeatCount;
796};
797
798/**
799 * Same as MotionEvent, but only contains the data that can be verified.
800 * If you update this class, you must also update VerifiedMotionEvent.java
801 */
802struct __attribute__((__packed__)) VerifiedMotionEvent : public VerifiedInputEvent {
803 float rawX;
804 float rawY;
805 int32_t actionMasked;
806 nsecs_t downTimeNanos;
807 int32_t flags;
808 int32_t metaState;
809 int32_t buttonState;
810};
811
812VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event);
813VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event);
814
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800815/*
Jeff Brown5912f952013-07-01 19:10:31 -0700816 * Input event factory.
817 */
818class InputEventFactoryInterface {
819protected:
820 virtual ~InputEventFactoryInterface() { }
821
822public:
823 InputEventFactoryInterface() { }
824
825 virtual KeyEvent* createKeyEvent() = 0;
826 virtual MotionEvent* createMotionEvent() = 0;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800827 virtual FocusEvent* createFocusEvent() = 0;
Jeff Brown5912f952013-07-01 19:10:31 -0700828};
829
830/*
831 * A simple input event factory implementation that uses a single preallocated instance
832 * of each type of input event that are reused for each request.
833 */
834class PreallocatedInputEventFactory : public InputEventFactoryInterface {
835public:
836 PreallocatedInputEventFactory() { }
837 virtual ~PreallocatedInputEventFactory() { }
838
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800839 virtual KeyEvent* createKeyEvent() override { return &mKeyEvent; }
840 virtual MotionEvent* createMotionEvent() override { return &mMotionEvent; }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800841 virtual FocusEvent* createFocusEvent() override { return &mFocusEvent; }
Jeff Brown5912f952013-07-01 19:10:31 -0700842
843private:
844 KeyEvent mKeyEvent;
845 MotionEvent mMotionEvent;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800846 FocusEvent mFocusEvent;
Jeff Brown5912f952013-07-01 19:10:31 -0700847};
848
849/*
850 * An input event factory implementation that maintains a pool of input events.
851 */
852class PooledInputEventFactory : public InputEventFactoryInterface {
853public:
Chih-Hung Hsiehf43b02c2018-12-20 15:45:56 -0800854 explicit PooledInputEventFactory(size_t maxPoolSize = 20);
Jeff Brown5912f952013-07-01 19:10:31 -0700855 virtual ~PooledInputEventFactory();
856
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800857 virtual KeyEvent* createKeyEvent() override;
858 virtual MotionEvent* createMotionEvent() override;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800859 virtual FocusEvent* createFocusEvent() override;
Jeff Brown5912f952013-07-01 19:10:31 -0700860
861 void recycle(InputEvent* event);
862
863private:
864 const size_t mMaxPoolSize;
865
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800866 std::queue<std::unique_ptr<KeyEvent>> mKeyEventPool;
867 std::queue<std::unique_ptr<MotionEvent>> mMotionEventPool;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800868 std::queue<std::unique_ptr<FocusEvent>> mFocusEventPool;
Jeff Brown5912f952013-07-01 19:10:31 -0700869};
870
871} // namespace android
872
873#endif // _LIBINPUT_INPUT_H