blob: 9525bcbef4c8ac65f21bdbfe7808c02e260e0598 [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>
chaviwfd9c1ed2020-07-01 10:57:59 -070029#include <ui/Transform.h>
Michael Wrightd0bd3912014-03-19 12:06:10 -070030#include <utils/BitSet.h>
Jeff Brown5912f952013-07-01 19:10:31 -070031#include <utils/KeyedVector.h>
Jeff Brown5912f952013-07-01 19:10:31 -070032#include <utils/RefBase.h>
Michael Wrightd0bd3912014-03-19 12:06:10 -070033#include <utils/Timers.h>
34#include <utils/Vector.h>
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -060035#include <array>
Garfield Tan00f511d2019-06-12 16:55:40 -070036#include <limits>
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -080037#include <queue>
Jeff Brown5912f952013-07-01 19:10:31 -070038
Jeff Brown5912f952013-07-01 19:10:31 -070039/*
40 * Additional private constants not defined in ndk/ui/input.h.
41 */
42enum {
43 /* Signifies that the key is being predispatched */
44 AKEY_EVENT_FLAG_PREDISPATCH = 0x20000000,
45
46 /* Private control to determine when an app is tracking a key sequence. */
47 AKEY_EVENT_FLAG_START_TRACKING = 0x40000000,
48
49 /* Key event is inconsistent with previously sent key events. */
50 AKEY_EVENT_FLAG_TAINTED = 0x80000000,
51};
52
53enum {
Michael Wrightcdcd8f22016-03-22 16:52:13 -070054
55 /**
56 * This flag indicates that the window that received this motion event is partly
57 * or wholly obscured by another visible window above it. This flag is set to true
58 * even if the event did not directly pass through the obscured area.
59 * A security sensitive application can check this flag to identify situations in which
60 * a malicious application may have covered up part of its content for the purpose
61 * of misleading the user or hijacking touches. An appropriate response might be
62 * to drop the suspect touches or to take additional precautions to confirm the user's
63 * actual intent.
64 */
65 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED = 0x2,
66
Prabir Pradhan141a9862018-11-19 14:35:56 -080067 /**
68 * This flag indicates that the event has been generated by a gesture generator. It
69 * provides a hint to the GestureDetector to not apply any touch slop.
70 */
71 AMOTION_EVENT_FLAG_IS_GENERATED_GESTURE = 0x8,
72
Jeff Brown5912f952013-07-01 19:10:31 -070073 /* Motion event is inconsistent with previously sent motion events. */
74 AMOTION_EVENT_FLAG_TAINTED = 0x80000000,
75};
76
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -080077/**
78 * Allowed VerifiedKeyEvent flags. All other flags from KeyEvent do not get verified.
79 * These values must be kept in sync with VerifiedKeyEvent.java
80 */
81constexpr int32_t VERIFIED_KEY_EVENT_FLAGS = AKEY_EVENT_FLAG_CANCELED;
82
83/**
84 * Allowed VerifiedMotionEventFlags. All other flags from MotionEvent do not get verified.
85 * These values must be kept in sync with VerifiedMotionEvent.java
86 */
87constexpr int32_t VERIFIED_MOTION_EVENT_FLAGS =
88 AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED;
89
arthurhungcc7f9802020-04-30 17:55:40 +080090/**
91 * This flag indicates that the point up event has been canceled.
92 * Typically this is used for palm event when the user has accidental touches.
93 * TODO: Adjust flag to public api
94 */
95constexpr int32_t AMOTION_EVENT_FLAG_CANCELED = 0x20;
96
Jeff Brown5912f952013-07-01 19:10:31 -070097enum {
98 /* Used when a motion event is not associated with any display.
99 * Typically used for non-pointer events. */
100 ADISPLAY_ID_NONE = -1,
101
102 /* The default display id. */
103 ADISPLAY_ID_DEFAULT = 0,
104};
105
106enum {
107 /*
108 * Indicates that an input device has switches.
109 * This input source flag is hidden from the API because switches are only used by the system
110 * and applications have no way to interact with them.
111 */
112 AINPUT_SOURCE_SWITCH = 0x80000000,
113};
114
Michael Wright962a1082013-10-17 17:35:53 -0700115enum {
116 /**
117 * Constants for LEDs. Hidden from the API since we don't actually expose a way to interact
118 * with LEDs to developers
119 *
Michael Wright872db4f2014-04-22 15:03:51 -0700120 * NOTE: If you add LEDs here, you must also add them to InputEventLabels.h
Michael Wright962a1082013-10-17 17:35:53 -0700121 */
122
123 ALED_NUM_LOCK = 0x00,
124 ALED_CAPS_LOCK = 0x01,
125 ALED_SCROLL_LOCK = 0x02,
126 ALED_COMPOSE = 0x03,
127 ALED_KANA = 0x04,
128 ALED_SLEEP = 0x05,
129 ALED_SUSPEND = 0x06,
130 ALED_MUTE = 0x07,
131 ALED_MISC = 0x08,
132 ALED_MAIL = 0x09,
133 ALED_CHARGING = 0x0a,
134 ALED_CONTROLLER_1 = 0x10,
135 ALED_CONTROLLER_2 = 0x11,
136 ALED_CONTROLLER_3 = 0x12,
137 ALED_CONTROLLER_4 = 0x13,
138};
139
Michael Wright9b04f862013-10-18 17:53:50 -0700140/* Maximum number of controller LEDs we support */
141#define MAX_CONTROLLER_LEDS 4
142
Jeff Brown5912f952013-07-01 19:10:31 -0700143/*
144 * SystemUiVisibility constants from View.
145 */
146enum {
147 ASYSTEM_UI_VISIBILITY_STATUS_BAR_VISIBLE = 0,
148 ASYSTEM_UI_VISIBILITY_STATUS_BAR_HIDDEN = 0x00000001,
149};
150
151/*
152 * Maximum number of pointers supported per motion event.
153 * Smallest number of pointers is 1.
154 * (We want at least 10 but some touch controllers obstensibly configured for 10 pointers
155 * will occasionally emit 11. There is not much harm making this constant bigger.)
156 */
157#define MAX_POINTERS 16
158
159/*
Flanker552a8a52015-09-07 15:28:58 +0800160 * Maximum number of samples supported per motion event.
161 */
162#define MAX_SAMPLES UINT16_MAX
163
164/*
Jeff Brown5912f952013-07-01 19:10:31 -0700165 * Maximum pointer id value supported in a motion event.
166 * Smallest pointer id is 0.
167 * (This is limited by our use of BitSet32 to track pointer assignments.)
168 */
169#define MAX_POINTER_ID 31
170
171/*
172 * Declare a concrete type for the NDK's input event forward declaration.
173 */
174struct AInputEvent {
175 virtual ~AInputEvent() { }
176};
177
178/*
179 * Declare a concrete type for the NDK's input device forward declaration.
180 */
181struct AInputDevice {
182 virtual ~AInputDevice() { }
183};
184
185
186namespace android {
187
Elliott Hughes6071da72015-08-12 15:27:47 -0700188#ifdef __ANDROID__
Jeff Brown5912f952013-07-01 19:10:31 -0700189class Parcel;
190#endif
191
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800192const char* inputEventTypeToString(int32_t type);
193
Jeff Brown5912f952013-07-01 19:10:31 -0700194/*
195 * Flags that flow alongside events in the input dispatch system to help with certain
196 * policy decisions such as waking from device sleep.
197 *
198 * These flags are also defined in frameworks/base/core/java/android/view/WindowManagerPolicy.java.
199 */
200enum {
201 /* These flags originate in RawEvents and are generally set in the key map.
Michael Wright872db4f2014-04-22 15:03:51 -0700202 * NOTE: If you want a flag to be able to set in a keylayout file, then you must add it to
203 * InputEventLabels.h as well. */
Jeff Brown5912f952013-07-01 19:10:31 -0700204
Jeff Brownc9aa6282015-02-11 19:03:28 -0800205 // Indicates that the event should wake the device.
Jeff Brown5912f952013-07-01 19:10:31 -0700206 POLICY_FLAG_WAKE = 0x00000001,
Jeff Brownc9aa6282015-02-11 19:03:28 -0800207
208 // Indicates that the key is virtual, such as a capacitive button, and should
209 // generate haptic feedback. Virtual keys may be suppressed for some time
210 // after a recent touch to prevent accidental activation of virtual keys adjacent
211 // to the touch screen during an edge swipe.
Michael Wright872db4f2014-04-22 15:03:51 -0700212 POLICY_FLAG_VIRTUAL = 0x00000002,
Jeff Brownc9aa6282015-02-11 19:03:28 -0800213
214 // Indicates that the key is the special function modifier.
Michael Wright872db4f2014-04-22 15:03:51 -0700215 POLICY_FLAG_FUNCTION = 0x00000004,
Jeff Brown5912f952013-07-01 19:10:31 -0700216
Jeff Brownc9aa6282015-02-11 19:03:28 -0800217 // Indicates that the key represents a special gesture that has been detected by
218 // the touch firmware or driver. Causes touch events from the same device to be canceled.
219 POLICY_FLAG_GESTURE = 0x00000008,
220
Jeff Brown5912f952013-07-01 19:10:31 -0700221 POLICY_FLAG_RAW_MASK = 0x0000ffff,
222
223 /* These flags are set by the input dispatcher. */
224
225 // Indicates that the input event was injected.
226 POLICY_FLAG_INJECTED = 0x01000000,
227
228 // Indicates that the input event is from a trusted source such as a directly attached
229 // input device or an application with system-wide event injection permission.
230 POLICY_FLAG_TRUSTED = 0x02000000,
231
232 // Indicates that the input event has passed through an input filter.
233 POLICY_FLAG_FILTERED = 0x04000000,
234
235 // Disables automatic key repeating behavior.
236 POLICY_FLAG_DISABLE_KEY_REPEAT = 0x08000000,
237
238 /* These flags are set by the input reader policy as it intercepts each event. */
239
Jeff Browndb19e462014-04-08 19:55:38 -0700240 // Indicates that the device was in an interactive state when the
241 // event was intercepted.
242 POLICY_FLAG_INTERACTIVE = 0x20000000,
Jeff Brown5912f952013-07-01 19:10:31 -0700243
244 // Indicates that the event should be dispatched to applications.
245 // The input event should still be sent to the InputDispatcher so that it can see all
246 // input events received include those that it will not deliver.
247 POLICY_FLAG_PASS_TO_USER = 0x40000000,
248};
249
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800250/**
251 * Classifications of the current gesture, if available.
252 *
253 * The following values must be kept in sync with MotionEvent.java
254 */
255enum class MotionClassification : uint8_t {
256 /**
257 * No classification is available.
258 */
259 NONE = 0,
260 /**
261 * Too early to classify the current gesture. Need more events. Look for changes in the
262 * upcoming motion events.
263 */
264 AMBIGUOUS_GESTURE = 1,
265 /**
266 * The current gesture likely represents a user intentionally exerting force on the touchscreen.
267 */
268 DEEP_PRESS = 2,
269};
270
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800271/**
272 * String representation of MotionClassification
273 */
274const char* motionClassificationToString(MotionClassification classification);
275
Garfield Tan00f511d2019-06-12 16:55:40 -0700276/**
Garfield Tan84b087e2020-01-23 10:49:05 -0800277 * Generator of unique numbers used to identify input events.
278 *
279 * Layout of ID:
280 * |--------------------------|---------------------------|
281 * | 2 bits for source | 30 bits for random number |
282 * |--------------------------|---------------------------|
283 */
284class IdGenerator {
285private:
286 static constexpr uint32_t SOURCE_SHIFT = 30;
287
288public:
289 // Used to divide integer space to ensure no conflict among these sources./
290 enum class Source : int32_t {
Garfield Tan750c7f42020-05-18 17:41:46 -0700291 INPUT_READER = static_cast<int32_t>(0x0u << SOURCE_SHIFT),
292 INPUT_DISPATCHER = static_cast<int32_t>(0x1u << SOURCE_SHIFT),
293 OTHER = static_cast<int32_t>(0x3u << SOURCE_SHIFT), // E.g. app injected events
Garfield Tan84b087e2020-01-23 10:49:05 -0800294 };
295 IdGenerator(Source source);
296
297 int32_t nextId() const;
298
299 // Extract source from given id.
300 static inline Source getSource(int32_t id) { return static_cast<Source>(SOURCE_MASK & id); }
301
302private:
303 const Source mSource;
304
Garfield Tan750c7f42020-05-18 17:41:46 -0700305 static constexpr int32_t SOURCE_MASK = static_cast<int32_t>(0x3u << SOURCE_SHIFT);
Garfield Tan84b087e2020-01-23 10:49:05 -0800306};
307
308/**
Garfield Tan00f511d2019-06-12 16:55:40 -0700309 * Invalid value for cursor position. Used for non-mouse events, tests and injected events. Don't
310 * use it for direct comparison with any other value, because NaN isn't equal to itself according to
311 * IEEE 754. Use isnan() instead to check if a cursor position is valid.
312 */
313constexpr float AMOTION_EVENT_INVALID_CURSOR_POSITION = std::numeric_limits<float>::quiet_NaN();
314
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600315/**
316 * Invalid value of HMAC - SHA256. Any events with this HMAC value will be marked as not verified.
317 */
318constexpr std::array<uint8_t, 32> INVALID_HMAC = {0};
319
Jeff Brown5912f952013-07-01 19:10:31 -0700320/*
321 * Pointer coordinate data.
322 */
323struct PointerCoords {
Michael Wright8f6710f2014-06-09 18:56:43 -0700324 enum { MAX_AXES = 30 }; // 30 so that sizeof(PointerCoords) == 128
Jeff Brown5912f952013-07-01 19:10:31 -0700325
326 // Bitfield of axes that are present in this structure.
Fengwei Yin83e0e422014-05-24 05:32:09 +0800327 uint64_t bits __attribute__((aligned(8)));
Jeff Brown5912f952013-07-01 19:10:31 -0700328
329 // Values of axes that are stored in this structure packed in order by axis id
330 // for each axis that is present in the structure according to 'bits'.
331 float values[MAX_AXES];
332
333 inline void clear() {
Michael Wrightd0bd3912014-03-19 12:06:10 -0700334 BitSet64::clear(bits);
335 }
336
337 bool isEmpty() const {
338 return BitSet64::isEmpty(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700339 }
340
341 float getAxisValue(int32_t axis) const;
342 status_t setAxisValue(int32_t axis, float value);
343
Robert Carre07e1032018-11-26 12:55:53 -0800344 void scale(float globalScale);
345
346 // Scale the pointer coordinates according to a global scale and a
347 // window scale. The global scale will be applied to TOUCH/TOOL_MAJOR/MINOR
348 // axes, however the window scaling will not.
349 void scale(float globalScale, float windowXScale, float windowYScale);
Jeff Browned4d28d2014-02-11 14:28:48 -0800350 void applyOffset(float xOffset, float yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700351
chaviwc01e1372020-07-01 12:37:31 -0700352 void transform(const ui::Transform& transform);
353
Jeff Brown5912f952013-07-01 19:10:31 -0700354 inline float getX() const {
355 return getAxisValue(AMOTION_EVENT_AXIS_X);
356 }
357
358 inline float getY() const {
359 return getAxisValue(AMOTION_EVENT_AXIS_Y);
360 }
361
Elliott Hughes6071da72015-08-12 15:27:47 -0700362#ifdef __ANDROID__
Jeff Brown5912f952013-07-01 19:10:31 -0700363 status_t readFromParcel(Parcel* parcel);
364 status_t writeToParcel(Parcel* parcel) const;
365#endif
366
367 bool operator==(const PointerCoords& other) const;
368 inline bool operator!=(const PointerCoords& other) const {
369 return !(*this == other);
370 }
371
372 void copyFrom(const PointerCoords& other);
373
374private:
375 void tooManyAxes(int axis);
376};
377
378/*
379 * Pointer property data.
380 */
381struct PointerProperties {
382 // The id of the pointer.
383 int32_t id;
384
385 // The pointer tool type.
386 int32_t toolType;
387
388 inline void clear() {
389 id = -1;
390 toolType = 0;
391 }
392
393 bool operator==(const PointerProperties& other) const;
394 inline bool operator!=(const PointerProperties& other) const {
395 return !(*this == other);
396 }
397
398 void copyFrom(const PointerProperties& other);
399};
400
401/*
402 * Input events.
403 */
404class InputEvent : public AInputEvent {
405public:
406 virtual ~InputEvent() { }
407
408 virtual int32_t getType() const = 0;
409
Garfield Tan4cc839f2020-01-24 11:26:14 -0800410 inline int32_t getId() const { return mId; }
411
Jeff Brown5912f952013-07-01 19:10:31 -0700412 inline int32_t getDeviceId() const { return mDeviceId; }
413
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600414 inline uint32_t getSource() const { return mSource; }
Jeff Brown5912f952013-07-01 19:10:31 -0700415
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600416 inline void setSource(uint32_t source) { mSource = source; }
Jeff Brown5912f952013-07-01 19:10:31 -0700417
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100418 inline int32_t getDisplayId() const { return mDisplayId; }
419
420 inline void setDisplayId(int32_t displayId) { mDisplayId = displayId; }
421
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600422 inline std::array<uint8_t, 32> getHmac() const { return mHmac; }
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100423
Garfield Tan4cc839f2020-01-24 11:26:14 -0800424 static int32_t nextId();
425
Jeff Brown5912f952013-07-01 19:10:31 -0700426protected:
Garfield Tan4cc839f2020-01-24 11:26:14 -0800427 void initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600428 std::array<uint8_t, 32> hmac);
Garfield Tan4cc839f2020-01-24 11:26:14 -0800429
Jeff Brown5912f952013-07-01 19:10:31 -0700430 void initialize(const InputEvent& from);
431
Garfield Tan4cc839f2020-01-24 11:26:14 -0800432 int32_t mId;
Jeff Brown5912f952013-07-01 19:10:31 -0700433 int32_t mDeviceId;
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600434 uint32_t mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100435 int32_t mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600436 std::array<uint8_t, 32> mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700437};
438
439/*
440 * Key events.
441 */
442class KeyEvent : public InputEvent {
443public:
444 virtual ~KeyEvent() { }
445
446 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_KEY; }
447
448 inline int32_t getAction() const { return mAction; }
449
450 inline int32_t getFlags() const { return mFlags; }
451
452 inline void setFlags(int32_t flags) { mFlags = flags; }
453
454 inline int32_t getKeyCode() const { return mKeyCode; }
455
456 inline int32_t getScanCode() const { return mScanCode; }
457
458 inline int32_t getMetaState() const { return mMetaState; }
459
460 inline int32_t getRepeatCount() const { return mRepeatCount; }
461
462 inline nsecs_t getDownTime() const { return mDownTime; }
463
464 inline nsecs_t getEventTime() const { return mEventTime; }
465
Michael Wright872db4f2014-04-22 15:03:51 -0700466 static const char* getLabel(int32_t keyCode);
467 static int32_t getKeyCodeFromLabel(const char* label);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800468
Garfield Tan4cc839f2020-01-24 11:26:14 -0800469 void initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600470 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags, int32_t keyCode,
471 int32_t scanCode, int32_t metaState, int32_t repeatCount, nsecs_t downTime,
472 nsecs_t eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -0700473 void initialize(const KeyEvent& from);
474
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700475 static const char* actionToString(int32_t action);
476
Jeff Brown5912f952013-07-01 19:10:31 -0700477protected:
478 int32_t mAction;
479 int32_t mFlags;
480 int32_t mKeyCode;
481 int32_t mScanCode;
482 int32_t mMetaState;
483 int32_t mRepeatCount;
484 nsecs_t mDownTime;
485 nsecs_t mEventTime;
486};
487
488/*
489 * Motion events.
490 */
491class MotionEvent : public InputEvent {
492public:
493 virtual ~MotionEvent() { }
494
495 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_MOTION; }
496
497 inline int32_t getAction() const { return mAction; }
498
499 inline int32_t getActionMasked() const { return mAction & AMOTION_EVENT_ACTION_MASK; }
500
501 inline int32_t getActionIndex() const {
502 return (mAction & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK)
503 >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
504 }
505
506 inline void setAction(int32_t action) { mAction = action; }
507
508 inline int32_t getFlags() const { return mFlags; }
509
510 inline void setFlags(int32_t flags) { mFlags = flags; }
511
512 inline int32_t getEdgeFlags() const { return mEdgeFlags; }
513
514 inline void setEdgeFlags(int32_t edgeFlags) { mEdgeFlags = edgeFlags; }
515
516 inline int32_t getMetaState() const { return mMetaState; }
517
518 inline void setMetaState(int32_t metaState) { mMetaState = metaState; }
519
520 inline int32_t getButtonState() const { return mButtonState; }
521
Michael Wright6db58792016-09-14 19:53:37 +0100522 inline void setButtonState(int32_t buttonState) { mButtonState = buttonState; }
Michael Wright7b159c92015-05-14 14:48:03 +0100523
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800524 inline MotionClassification getClassification() const { return mClassification; }
525
Michael Wright7b159c92015-05-14 14:48:03 +0100526 inline int32_t getActionButton() const { return mActionButton; }
527
Michael Wright21957b92015-06-17 21:06:54 +0100528 inline void setActionButton(int32_t button) { mActionButton = button; }
529
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600530 inline float getXScale() const { return mXScale; }
531
532 inline float getYScale() const { return mYScale; }
533
Jeff Brown5912f952013-07-01 19:10:31 -0700534 inline float getXOffset() const { return mXOffset; }
535
536 inline float getYOffset() const { return mYOffset; }
537
538 inline float getXPrecision() const { return mXPrecision; }
539
540 inline float getYPrecision() const { return mYPrecision; }
541
Garfield Tan937bb832019-07-25 17:48:31 -0700542 inline float getRawXCursorPosition() const { return mRawXCursorPosition; }
Garfield Tan00f511d2019-06-12 16:55:40 -0700543
544 float getXCursorPosition() const;
545
Garfield Tan937bb832019-07-25 17:48:31 -0700546 inline float getRawYCursorPosition() const { return mRawYCursorPosition; }
Garfield Tan00f511d2019-06-12 16:55:40 -0700547
548 float getYCursorPosition() const;
549
Garfield Tan937bb832019-07-25 17:48:31 -0700550 void setCursorPosition(float x, float y);
551
Garfield Tanab0ab9c2019-07-10 18:58:28 -0700552 static inline bool isValidCursorPosition(float x, float y) { return !isnan(x) && !isnan(y); }
553
Jeff Brown5912f952013-07-01 19:10:31 -0700554 inline nsecs_t getDownTime() const { return mDownTime; }
555
556 inline void setDownTime(nsecs_t downTime) { mDownTime = downTime; }
557
558 inline size_t getPointerCount() const { return mPointerProperties.size(); }
559
560 inline const PointerProperties* getPointerProperties(size_t pointerIndex) const {
561 return &mPointerProperties[pointerIndex];
562 }
563
564 inline int32_t getPointerId(size_t pointerIndex) const {
565 return mPointerProperties[pointerIndex].id;
566 }
567
568 inline int32_t getToolType(size_t pointerIndex) const {
569 return mPointerProperties[pointerIndex].toolType;
570 }
571
572 inline nsecs_t getEventTime() const { return mSampleEventTimes[getHistorySize()]; }
573
574 const PointerCoords* getRawPointerCoords(size_t pointerIndex) const;
575
576 float getRawAxisValue(int32_t axis, size_t pointerIndex) const;
577
578 inline float getRawX(size_t pointerIndex) const {
579 return getRawAxisValue(AMOTION_EVENT_AXIS_X, pointerIndex);
580 }
581
582 inline float getRawY(size_t pointerIndex) const {
583 return getRawAxisValue(AMOTION_EVENT_AXIS_Y, pointerIndex);
584 }
585
586 float getAxisValue(int32_t axis, size_t pointerIndex) const;
587
588 inline float getX(size_t pointerIndex) const {
589 return getAxisValue(AMOTION_EVENT_AXIS_X, pointerIndex);
590 }
591
592 inline float getY(size_t pointerIndex) const {
593 return getAxisValue(AMOTION_EVENT_AXIS_Y, pointerIndex);
594 }
595
596 inline float getPressure(size_t pointerIndex) const {
597 return getAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pointerIndex);
598 }
599
600 inline float getSize(size_t pointerIndex) const {
601 return getAxisValue(AMOTION_EVENT_AXIS_SIZE, pointerIndex);
602 }
603
604 inline float getTouchMajor(size_t pointerIndex) const {
605 return getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, pointerIndex);
606 }
607
608 inline float getTouchMinor(size_t pointerIndex) const {
609 return getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, pointerIndex);
610 }
611
612 inline float getToolMajor(size_t pointerIndex) const {
613 return getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, pointerIndex);
614 }
615
616 inline float getToolMinor(size_t pointerIndex) const {
617 return getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, pointerIndex);
618 }
619
620 inline float getOrientation(size_t pointerIndex) const {
621 return getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, pointerIndex);
622 }
623
624 inline size_t getHistorySize() const { return mSampleEventTimes.size() - 1; }
625
626 inline nsecs_t getHistoricalEventTime(size_t historicalIndex) const {
627 return mSampleEventTimes[historicalIndex];
628 }
629
630 const PointerCoords* getHistoricalRawPointerCoords(
631 size_t pointerIndex, size_t historicalIndex) const;
632
633 float getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
634 size_t historicalIndex) const;
635
636 inline float getHistoricalRawX(size_t pointerIndex, size_t historicalIndex) const {
637 return getHistoricalRawAxisValue(
638 AMOTION_EVENT_AXIS_X, pointerIndex, historicalIndex);
639 }
640
641 inline float getHistoricalRawY(size_t pointerIndex, size_t historicalIndex) const {
642 return getHistoricalRawAxisValue(
643 AMOTION_EVENT_AXIS_Y, pointerIndex, historicalIndex);
644 }
645
646 float getHistoricalAxisValue(int32_t axis, size_t pointerIndex, size_t historicalIndex) const;
647
648 inline float getHistoricalX(size_t pointerIndex, size_t historicalIndex) const {
649 return getHistoricalAxisValue(
650 AMOTION_EVENT_AXIS_X, pointerIndex, historicalIndex);
651 }
652
653 inline float getHistoricalY(size_t pointerIndex, size_t historicalIndex) const {
654 return getHistoricalAxisValue(
655 AMOTION_EVENT_AXIS_Y, pointerIndex, historicalIndex);
656 }
657
658 inline float getHistoricalPressure(size_t pointerIndex, size_t historicalIndex) const {
659 return getHistoricalAxisValue(
660 AMOTION_EVENT_AXIS_PRESSURE, pointerIndex, historicalIndex);
661 }
662
663 inline float getHistoricalSize(size_t pointerIndex, size_t historicalIndex) const {
664 return getHistoricalAxisValue(
665 AMOTION_EVENT_AXIS_SIZE, pointerIndex, historicalIndex);
666 }
667
668 inline float getHistoricalTouchMajor(size_t pointerIndex, size_t historicalIndex) const {
669 return getHistoricalAxisValue(
670 AMOTION_EVENT_AXIS_TOUCH_MAJOR, pointerIndex, historicalIndex);
671 }
672
673 inline float getHistoricalTouchMinor(size_t pointerIndex, size_t historicalIndex) const {
674 return getHistoricalAxisValue(
675 AMOTION_EVENT_AXIS_TOUCH_MINOR, pointerIndex, historicalIndex);
676 }
677
678 inline float getHistoricalToolMajor(size_t pointerIndex, size_t historicalIndex) const {
679 return getHistoricalAxisValue(
680 AMOTION_EVENT_AXIS_TOOL_MAJOR, pointerIndex, historicalIndex);
681 }
682
683 inline float getHistoricalToolMinor(size_t pointerIndex, size_t historicalIndex) const {
684 return getHistoricalAxisValue(
685 AMOTION_EVENT_AXIS_TOOL_MINOR, pointerIndex, historicalIndex);
686 }
687
688 inline float getHistoricalOrientation(size_t pointerIndex, size_t historicalIndex) const {
689 return getHistoricalAxisValue(
690 AMOTION_EVENT_AXIS_ORIENTATION, pointerIndex, historicalIndex);
691 }
692
693 ssize_t findPointerIndex(int32_t pointerId) const;
694
Garfield Tan4cc839f2020-01-24 11:26:14 -0800695 void initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600696 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
697 int32_t flags, int32_t edgeFlags, int32_t metaState, int32_t buttonState,
698 MotionClassification classification, float xScale, float yScale, float xOffset,
Garfield Tan937bb832019-07-25 17:48:31 -0700699 float yOffset, float xPrecision, float yPrecision, float rawXCursorPosition,
700 float rawYCursorPosition, nsecs_t downTime, nsecs_t eventTime,
Garfield Tan00f511d2019-06-12 16:55:40 -0700701 size_t pointerCount, const PointerProperties* pointerProperties,
702 const PointerCoords* pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -0700703
704 void copyFrom(const MotionEvent* other, bool keepHistory);
705
706 void addSample(
707 nsecs_t eventTime,
708 const PointerCoords* pointerCoords);
709
710 void offsetLocation(float xOffset, float yOffset);
711
Robert Carre07e1032018-11-26 12:55:53 -0800712 void scale(float globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700713
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700714 // Apply 3x3 perspective matrix transformation.
715 // Matrix is in row-major form and compatible with SkMatrix.
716 void transform(const float matrix[9]);
Jeff Brown5912f952013-07-01 19:10:31 -0700717
Elliott Hughes6071da72015-08-12 15:27:47 -0700718#ifdef __ANDROID__
Jeff Brown5912f952013-07-01 19:10:31 -0700719 status_t readFromParcel(Parcel* parcel);
720 status_t writeToParcel(Parcel* parcel) const;
721#endif
722
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600723 static bool isTouchEvent(uint32_t source, int32_t action);
Jeff Brown5912f952013-07-01 19:10:31 -0700724 inline bool isTouchEvent() const {
725 return isTouchEvent(mSource, mAction);
726 }
727
728 // Low-level accessors.
729 inline const PointerProperties* getPointerProperties() const {
730 return mPointerProperties.array();
731 }
732 inline const nsecs_t* getSampleEventTimes() const { return mSampleEventTimes.array(); }
733 inline const PointerCoords* getSamplePointerCoords() const {
734 return mSamplePointerCoords.array();
735 }
736
Michael Wright872db4f2014-04-22 15:03:51 -0700737 static const char* getLabel(int32_t axis);
738 static int32_t getAxisFromLabel(const char* label);
739
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700740 static const char* actionToString(int32_t action);
741
Jeff Brown5912f952013-07-01 19:10:31 -0700742protected:
743 int32_t mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100744 int32_t mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700745 int32_t mFlags;
746 int32_t mEdgeFlags;
747 int32_t mMetaState;
748 int32_t mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800749 MotionClassification mClassification;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600750 float mXScale;
751 float mYScale;
Jeff Brown5912f952013-07-01 19:10:31 -0700752 float mXOffset;
753 float mYOffset;
chaviwfd9c1ed2020-07-01 10:57:59 -0700754 ui::Transform mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700755 float mXPrecision;
756 float mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700757 float mRawXCursorPosition;
758 float mRawYCursorPosition;
Jeff Brown5912f952013-07-01 19:10:31 -0700759 nsecs_t mDownTime;
760 Vector<PointerProperties> mPointerProperties;
761 Vector<nsecs_t> mSampleEventTimes;
762 Vector<PointerCoords> mSamplePointerCoords;
763};
764
765/*
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800766 * Focus events.
767 */
768class FocusEvent : public InputEvent {
769public:
770 virtual ~FocusEvent() {}
771
772 virtual int32_t getType() const override { return AINPUT_EVENT_TYPE_FOCUS; }
773
774 inline bool getHasFocus() const { return mHasFocus; }
775
776 inline bool getInTouchMode() const { return mInTouchMode; }
777
Garfield Tan4cc839f2020-01-24 11:26:14 -0800778 void initialize(int32_t id, bool hasFocus, bool inTouchMode);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800779
780 void initialize(const FocusEvent& from);
781
782protected:
783 bool mHasFocus;
784 bool mInTouchMode;
785};
786
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800787/**
788 * Base class for verified events.
789 * Do not create a VerifiedInputEvent explicitly.
790 * Use helper functions to create them from InputEvents.
791 */
792struct __attribute__((__packed__)) VerifiedInputEvent {
793 enum class Type : int32_t {
794 KEY = AINPUT_EVENT_TYPE_KEY,
795 MOTION = AINPUT_EVENT_TYPE_MOTION,
796 };
797
798 Type type;
799 int32_t deviceId;
800 nsecs_t eventTimeNanos;
801 uint32_t source;
802 int32_t displayId;
803};
804
805/**
806 * Same as KeyEvent, but only contains the data that can be verified.
807 * If you update this class, you must also update VerifiedKeyEvent.java
808 */
809struct __attribute__((__packed__)) VerifiedKeyEvent : public VerifiedInputEvent {
810 int32_t action;
811 nsecs_t downTimeNanos;
812 int32_t flags;
813 int32_t keyCode;
814 int32_t scanCode;
815 int32_t metaState;
816 int32_t repeatCount;
817};
818
819/**
820 * Same as MotionEvent, but only contains the data that can be verified.
821 * If you update this class, you must also update VerifiedMotionEvent.java
822 */
823struct __attribute__((__packed__)) VerifiedMotionEvent : public VerifiedInputEvent {
824 float rawX;
825 float rawY;
826 int32_t actionMasked;
827 nsecs_t downTimeNanos;
828 int32_t flags;
829 int32_t metaState;
830 int32_t buttonState;
831};
832
833VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event);
834VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event);
835
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800836/*
Jeff Brown5912f952013-07-01 19:10:31 -0700837 * Input event factory.
838 */
839class InputEventFactoryInterface {
840protected:
841 virtual ~InputEventFactoryInterface() { }
842
843public:
844 InputEventFactoryInterface() { }
845
846 virtual KeyEvent* createKeyEvent() = 0;
847 virtual MotionEvent* createMotionEvent() = 0;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800848 virtual FocusEvent* createFocusEvent() = 0;
Jeff Brown5912f952013-07-01 19:10:31 -0700849};
850
851/*
852 * A simple input event factory implementation that uses a single preallocated instance
853 * of each type of input event that are reused for each request.
854 */
855class PreallocatedInputEventFactory : public InputEventFactoryInterface {
856public:
857 PreallocatedInputEventFactory() { }
858 virtual ~PreallocatedInputEventFactory() { }
859
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800860 virtual KeyEvent* createKeyEvent() override { return &mKeyEvent; }
861 virtual MotionEvent* createMotionEvent() override { return &mMotionEvent; }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800862 virtual FocusEvent* createFocusEvent() override { return &mFocusEvent; }
Jeff Brown5912f952013-07-01 19:10:31 -0700863
864private:
865 KeyEvent mKeyEvent;
866 MotionEvent mMotionEvent;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800867 FocusEvent mFocusEvent;
Jeff Brown5912f952013-07-01 19:10:31 -0700868};
869
870/*
871 * An input event factory implementation that maintains a pool of input events.
872 */
873class PooledInputEventFactory : public InputEventFactoryInterface {
874public:
Chih-Hung Hsiehf43b02c2018-12-20 15:45:56 -0800875 explicit PooledInputEventFactory(size_t maxPoolSize = 20);
Jeff Brown5912f952013-07-01 19:10:31 -0700876 virtual ~PooledInputEventFactory();
877
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800878 virtual KeyEvent* createKeyEvent() override;
879 virtual MotionEvent* createMotionEvent() override;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800880 virtual FocusEvent* createFocusEvent() override;
Jeff Brown5912f952013-07-01 19:10:31 -0700881
882 void recycle(InputEvent* event);
883
884private:
885 const size_t mMaxPoolSize;
886
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800887 std::queue<std::unique_ptr<KeyEvent>> mKeyEventPool;
888 std::queue<std::unique_ptr<MotionEvent>> mMotionEventPool;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800889 std::queue<std::unique_ptr<FocusEvent>> mFocusEventPool;
Jeff Brown5912f952013-07-01 19:10:31 -0700890};
891
892} // namespace android
893
894#endif // _LIBINPUT_INPUT_H