blob: 7b522bbc1ebdd764c7a8122b1cd8ec9cd4d9af1c [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/*
Jeff Brown5912f952013-07-01 19:10:31 -0700144 * 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
Brett Chabotfaa986c2020-11-04 17:39:36 -0800180#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -0700181class Parcel;
Brett Chabotfaa986c2020-11-04 17:39:36 -0800182#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700183
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/**
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000269 * Portion of FrameMetrics timeline of interest to input code.
270 */
271enum GraphicsTimeline : size_t {
272 /** Time when the app sent the buffer to SurfaceFlinger. */
273 GPU_COMPLETED_TIME = 0,
274
275 /** Time when the frame was presented on the display */
276 PRESENT_TIME = 1,
277
278 /** Total size of the 'GraphicsTimeline' array. Must always be last. */
279 SIZE = 2
280};
281
282/**
Garfield Tan84b087e2020-01-23 10:49:05 -0800283 * Generator of unique numbers used to identify input events.
284 *
285 * Layout of ID:
286 * |--------------------------|---------------------------|
287 * | 2 bits for source | 30 bits for random number |
288 * |--------------------------|---------------------------|
289 */
290class IdGenerator {
291private:
292 static constexpr uint32_t SOURCE_SHIFT = 30;
293
294public:
295 // Used to divide integer space to ensure no conflict among these sources./
296 enum class Source : int32_t {
Garfield Tan750c7f42020-05-18 17:41:46 -0700297 INPUT_READER = static_cast<int32_t>(0x0u << SOURCE_SHIFT),
298 INPUT_DISPATCHER = static_cast<int32_t>(0x1u << SOURCE_SHIFT),
299 OTHER = static_cast<int32_t>(0x3u << SOURCE_SHIFT), // E.g. app injected events
Garfield Tan84b087e2020-01-23 10:49:05 -0800300 };
301 IdGenerator(Source source);
302
303 int32_t nextId() const;
304
305 // Extract source from given id.
306 static inline Source getSource(int32_t id) { return static_cast<Source>(SOURCE_MASK & id); }
307
308private:
309 const Source mSource;
310
Garfield Tan750c7f42020-05-18 17:41:46 -0700311 static constexpr int32_t SOURCE_MASK = static_cast<int32_t>(0x3u << SOURCE_SHIFT);
Garfield Tan84b087e2020-01-23 10:49:05 -0800312};
313
314/**
Garfield Tan00f511d2019-06-12 16:55:40 -0700315 * Invalid value for cursor position. Used for non-mouse events, tests and injected events. Don't
316 * use it for direct comparison with any other value, because NaN isn't equal to itself according to
317 * IEEE 754. Use isnan() instead to check if a cursor position is valid.
318 */
319constexpr float AMOTION_EVENT_INVALID_CURSOR_POSITION = std::numeric_limits<float>::quiet_NaN();
320
Evan Rosky84f07f02021-04-16 10:42:42 -0700321/**
322 * Invalid value for display size. Used when display size isn't available for an event or doesn't
323 * matter. This is just a constant 0 so that it has no effect if unused.
324 */
325constexpr int32_t AMOTION_EVENT_INVALID_DISPLAY_SIZE = 0;
326
Jeff Brown5912f952013-07-01 19:10:31 -0700327/*
328 * Pointer coordinate data.
329 */
330struct PointerCoords {
Michael Wright8f6710f2014-06-09 18:56:43 -0700331 enum { MAX_AXES = 30 }; // 30 so that sizeof(PointerCoords) == 128
Jeff Brown5912f952013-07-01 19:10:31 -0700332
333 // Bitfield of axes that are present in this structure.
Fengwei Yin83e0e422014-05-24 05:32:09 +0800334 uint64_t bits __attribute__((aligned(8)));
Jeff Brown5912f952013-07-01 19:10:31 -0700335
336 // Values of axes that are stored in this structure packed in order by axis id
337 // for each axis that is present in the structure according to 'bits'.
338 float values[MAX_AXES];
339
340 inline void clear() {
Michael Wrightd0bd3912014-03-19 12:06:10 -0700341 BitSet64::clear(bits);
342 }
343
344 bool isEmpty() const {
345 return BitSet64::isEmpty(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700346 }
347
348 float getAxisValue(int32_t axis) const;
349 status_t setAxisValue(int32_t axis, float value);
350
Robert Carre07e1032018-11-26 12:55:53 -0800351 void scale(float globalScale);
352
353 // Scale the pointer coordinates according to a global scale and a
354 // window scale. The global scale will be applied to TOUCH/TOOL_MAJOR/MINOR
355 // axes, however the window scaling will not.
356 void scale(float globalScale, float windowXScale, float windowYScale);
Jeff Browned4d28d2014-02-11 14:28:48 -0800357 void applyOffset(float xOffset, float yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700358
chaviwc01e1372020-07-01 12:37:31 -0700359 void transform(const ui::Transform& transform);
360
Jeff Brown5912f952013-07-01 19:10:31 -0700361 inline float getX() const {
362 return getAxisValue(AMOTION_EVENT_AXIS_X);
363 }
364
365 inline float getY() const {
366 return getAxisValue(AMOTION_EVENT_AXIS_Y);
367 }
368
Evan Rosky84f07f02021-04-16 10:42:42 -0700369 vec2 getXYValue() const { return vec2(getX(), getY()); }
370
Brett Chabotfaa986c2020-11-04 17:39:36 -0800371#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -0700372 status_t readFromParcel(Parcel* parcel);
373 status_t writeToParcel(Parcel* parcel) const;
Brett Chabotfaa986c2020-11-04 17:39:36 -0800374#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700375
376 bool operator==(const PointerCoords& other) const;
377 inline bool operator!=(const PointerCoords& other) const {
378 return !(*this == other);
379 }
380
381 void copyFrom(const PointerCoords& other);
382
383private:
384 void tooManyAxes(int axis);
385};
386
387/*
388 * Pointer property data.
389 */
390struct PointerProperties {
391 // The id of the pointer.
392 int32_t id;
393
394 // The pointer tool type.
395 int32_t toolType;
396
397 inline void clear() {
398 id = -1;
399 toolType = 0;
400 }
401
402 bool operator==(const PointerProperties& other) const;
403 inline bool operator!=(const PointerProperties& other) const {
404 return !(*this == other);
405 }
406
407 void copyFrom(const PointerProperties& other);
408};
409
410/*
411 * Input events.
412 */
413class InputEvent : public AInputEvent {
414public:
415 virtual ~InputEvent() { }
416
417 virtual int32_t getType() const = 0;
418
Garfield Tan4cc839f2020-01-24 11:26:14 -0800419 inline int32_t getId() const { return mId; }
420
Jeff Brown5912f952013-07-01 19:10:31 -0700421 inline int32_t getDeviceId() const { return mDeviceId; }
422
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600423 inline uint32_t getSource() const { return mSource; }
Jeff Brown5912f952013-07-01 19:10:31 -0700424
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600425 inline void setSource(uint32_t source) { mSource = source; }
Jeff Brown5912f952013-07-01 19:10:31 -0700426
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100427 inline int32_t getDisplayId() const { return mDisplayId; }
428
429 inline void setDisplayId(int32_t displayId) { mDisplayId = displayId; }
430
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600431 inline std::array<uint8_t, 32> getHmac() const { return mHmac; }
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100432
Garfield Tan4cc839f2020-01-24 11:26:14 -0800433 static int32_t nextId();
434
Jeff Brown5912f952013-07-01 19:10:31 -0700435protected:
Garfield Tan4cc839f2020-01-24 11:26:14 -0800436 void initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600437 std::array<uint8_t, 32> hmac);
Garfield Tan4cc839f2020-01-24 11:26:14 -0800438
Jeff Brown5912f952013-07-01 19:10:31 -0700439 void initialize(const InputEvent& from);
440
Garfield Tan4cc839f2020-01-24 11:26:14 -0800441 int32_t mId;
Jeff Brown5912f952013-07-01 19:10:31 -0700442 int32_t mDeviceId;
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600443 uint32_t mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100444 int32_t mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600445 std::array<uint8_t, 32> mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700446};
447
448/*
449 * Key events.
450 */
451class KeyEvent : public InputEvent {
452public:
453 virtual ~KeyEvent() { }
454
455 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_KEY; }
456
457 inline int32_t getAction() const { return mAction; }
458
459 inline int32_t getFlags() const { return mFlags; }
460
461 inline void setFlags(int32_t flags) { mFlags = flags; }
462
463 inline int32_t getKeyCode() const { return mKeyCode; }
464
465 inline int32_t getScanCode() const { return mScanCode; }
466
467 inline int32_t getMetaState() const { return mMetaState; }
468
469 inline int32_t getRepeatCount() const { return mRepeatCount; }
470
471 inline nsecs_t getDownTime() const { return mDownTime; }
472
473 inline nsecs_t getEventTime() const { return mEventTime; }
474
Michael Wright872db4f2014-04-22 15:03:51 -0700475 static const char* getLabel(int32_t keyCode);
476 static int32_t getKeyCodeFromLabel(const char* label);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800477
Garfield Tan4cc839f2020-01-24 11:26:14 -0800478 void initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600479 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags, int32_t keyCode,
480 int32_t scanCode, int32_t metaState, int32_t repeatCount, nsecs_t downTime,
481 nsecs_t eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -0700482 void initialize(const KeyEvent& from);
483
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700484 static const char* actionToString(int32_t action);
485
Jeff Brown5912f952013-07-01 19:10:31 -0700486protected:
487 int32_t mAction;
488 int32_t mFlags;
489 int32_t mKeyCode;
490 int32_t mScanCode;
491 int32_t mMetaState;
492 int32_t mRepeatCount;
493 nsecs_t mDownTime;
494 nsecs_t mEventTime;
495};
496
497/*
498 * Motion events.
499 */
500class MotionEvent : public InputEvent {
501public:
502 virtual ~MotionEvent() { }
503
504 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_MOTION; }
505
506 inline int32_t getAction() const { return mAction; }
507
508 inline int32_t getActionMasked() const { return mAction & AMOTION_EVENT_ACTION_MASK; }
509
510 inline int32_t getActionIndex() const {
511 return (mAction & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK)
512 >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
513 }
514
515 inline void setAction(int32_t action) { mAction = action; }
516
517 inline int32_t getFlags() const { return mFlags; }
518
519 inline void setFlags(int32_t flags) { mFlags = flags; }
520
521 inline int32_t getEdgeFlags() const { return mEdgeFlags; }
522
523 inline void setEdgeFlags(int32_t edgeFlags) { mEdgeFlags = edgeFlags; }
524
525 inline int32_t getMetaState() const { return mMetaState; }
526
527 inline void setMetaState(int32_t metaState) { mMetaState = metaState; }
528
529 inline int32_t getButtonState() const { return mButtonState; }
530
Michael Wright6db58792016-09-14 19:53:37 +0100531 inline void setButtonState(int32_t buttonState) { mButtonState = buttonState; }
Michael Wright7b159c92015-05-14 14:48:03 +0100532
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800533 inline MotionClassification getClassification() const { return mClassification; }
534
Michael Wright7b159c92015-05-14 14:48:03 +0100535 inline int32_t getActionButton() const { return mActionButton; }
536
Michael Wright21957b92015-06-17 21:06:54 +0100537 inline void setActionButton(int32_t button) { mActionButton = button; }
538
chaviw9eaa22c2020-07-01 16:21:27 -0700539 inline float getXOffset() const { return mTransform.tx(); }
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600540
chaviw9eaa22c2020-07-01 16:21:27 -0700541 inline float getYOffset() const { return mTransform.ty(); }
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600542
chaviw9eaa22c2020-07-01 16:21:27 -0700543 inline ui::Transform getTransform() const { return mTransform; }
Jeff Brown5912f952013-07-01 19:10:31 -0700544
545 inline float getXPrecision() const { return mXPrecision; }
546
547 inline float getYPrecision() const { return mYPrecision; }
548
Garfield Tan937bb832019-07-25 17:48:31 -0700549 inline float getRawXCursorPosition() const { return mRawXCursorPosition; }
Garfield Tan00f511d2019-06-12 16:55:40 -0700550
551 float getXCursorPosition() const;
552
Garfield Tan937bb832019-07-25 17:48:31 -0700553 inline float getRawYCursorPosition() const { return mRawYCursorPosition; }
Garfield Tan00f511d2019-06-12 16:55:40 -0700554
555 float getYCursorPosition() const;
556
Garfield Tan937bb832019-07-25 17:48:31 -0700557 void setCursorPosition(float x, float y);
558
Evan Rosky84f07f02021-04-16 10:42:42 -0700559 int2 getDisplaySize() const { return {mDisplayWidth, mDisplayHeight}; }
560
Garfield Tanab0ab9c2019-07-10 18:58:28 -0700561 static inline bool isValidCursorPosition(float x, float y) { return !isnan(x) && !isnan(y); }
562
Jeff Brown5912f952013-07-01 19:10:31 -0700563 inline nsecs_t getDownTime() const { return mDownTime; }
564
565 inline void setDownTime(nsecs_t downTime) { mDownTime = downTime; }
566
567 inline size_t getPointerCount() const { return mPointerProperties.size(); }
568
569 inline const PointerProperties* getPointerProperties(size_t pointerIndex) const {
570 return &mPointerProperties[pointerIndex];
571 }
572
573 inline int32_t getPointerId(size_t pointerIndex) const {
574 return mPointerProperties[pointerIndex].id;
575 }
576
577 inline int32_t getToolType(size_t pointerIndex) const {
578 return mPointerProperties[pointerIndex].toolType;
579 }
580
581 inline nsecs_t getEventTime() const { return mSampleEventTimes[getHistorySize()]; }
582
Evan Rosky84f07f02021-04-16 10:42:42 -0700583 /**
584 * The actual raw pointer coords: whatever comes from the input device without any external
585 * transforms applied.
586 */
Jeff Brown5912f952013-07-01 19:10:31 -0700587 const PointerCoords* getRawPointerCoords(size_t pointerIndex) const;
588
Evan Rosky84f07f02021-04-16 10:42:42 -0700589 /**
590 * This is the raw axis value. However, for X/Y axes, this currently applies a "compat-raw"
591 * transform because many apps (incorrectly) assumed that raw == oriented-screen-space.
592 * "compat raw" is raw coordinates with screen rotation applied.
593 */
Jeff Brown5912f952013-07-01 19:10:31 -0700594 float getRawAxisValue(int32_t axis, size_t pointerIndex) const;
595
596 inline float getRawX(size_t pointerIndex) const {
597 return getRawAxisValue(AMOTION_EVENT_AXIS_X, pointerIndex);
598 }
599
600 inline float getRawY(size_t pointerIndex) const {
601 return getRawAxisValue(AMOTION_EVENT_AXIS_Y, pointerIndex);
602 }
603
604 float getAxisValue(int32_t axis, size_t pointerIndex) const;
605
Siarhei Vishniakou69e4d0f2020-09-14 19:53:29 -0500606 /**
607 * Get the X coordinate of the latest sample in this MotionEvent for pointer 'pointerIndex'.
608 * Identical to calling getHistoricalX(pointerIndex, getHistorySize()).
609 */
Jeff Brown5912f952013-07-01 19:10:31 -0700610 inline float getX(size_t pointerIndex) const {
611 return getAxisValue(AMOTION_EVENT_AXIS_X, pointerIndex);
612 }
613
Siarhei Vishniakou69e4d0f2020-09-14 19:53:29 -0500614 /**
615 * Get the Y coordinate of the latest sample in this MotionEvent for pointer 'pointerIndex'.
616 * Identical to calling getHistoricalX(pointerIndex, getHistorySize()).
617 */
Jeff Brown5912f952013-07-01 19:10:31 -0700618 inline float getY(size_t pointerIndex) const {
619 return getAxisValue(AMOTION_EVENT_AXIS_Y, pointerIndex);
620 }
621
622 inline float getPressure(size_t pointerIndex) const {
623 return getAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pointerIndex);
624 }
625
626 inline float getSize(size_t pointerIndex) const {
627 return getAxisValue(AMOTION_EVENT_AXIS_SIZE, pointerIndex);
628 }
629
630 inline float getTouchMajor(size_t pointerIndex) const {
631 return getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, pointerIndex);
632 }
633
634 inline float getTouchMinor(size_t pointerIndex) const {
635 return getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, pointerIndex);
636 }
637
638 inline float getToolMajor(size_t pointerIndex) const {
639 return getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, pointerIndex);
640 }
641
642 inline float getToolMinor(size_t pointerIndex) const {
643 return getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, pointerIndex);
644 }
645
646 inline float getOrientation(size_t pointerIndex) const {
647 return getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, pointerIndex);
648 }
649
650 inline size_t getHistorySize() const { return mSampleEventTimes.size() - 1; }
651
652 inline nsecs_t getHistoricalEventTime(size_t historicalIndex) const {
653 return mSampleEventTimes[historicalIndex];
654 }
655
Evan Rosky84f07f02021-04-16 10:42:42 -0700656 /**
657 * The actual raw pointer coords: whatever comes from the input device without any external
658 * transforms applied.
659 */
Jeff Brown5912f952013-07-01 19:10:31 -0700660 const PointerCoords* getHistoricalRawPointerCoords(
661 size_t pointerIndex, size_t historicalIndex) const;
662
Evan Rosky84f07f02021-04-16 10:42:42 -0700663 /**
664 * This is the raw axis value. However, for X/Y axes, this currently applies a "compat-raw"
665 * transform because many apps (incorrectly) assumed that raw == oriented-screen-space.
666 * "compat raw" is raw coordinates with screen rotation applied.
667 */
Jeff Brown5912f952013-07-01 19:10:31 -0700668 float getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
669 size_t historicalIndex) const;
670
671 inline float getHistoricalRawX(size_t pointerIndex, size_t historicalIndex) const {
672 return getHistoricalRawAxisValue(
673 AMOTION_EVENT_AXIS_X, pointerIndex, historicalIndex);
674 }
675
676 inline float getHistoricalRawY(size_t pointerIndex, size_t historicalIndex) const {
677 return getHistoricalRawAxisValue(
678 AMOTION_EVENT_AXIS_Y, pointerIndex, historicalIndex);
679 }
680
681 float getHistoricalAxisValue(int32_t axis, size_t pointerIndex, size_t historicalIndex) const;
682
683 inline float getHistoricalX(size_t pointerIndex, size_t historicalIndex) const {
684 return getHistoricalAxisValue(
685 AMOTION_EVENT_AXIS_X, pointerIndex, historicalIndex);
686 }
687
688 inline float getHistoricalY(size_t pointerIndex, size_t historicalIndex) const {
689 return getHistoricalAxisValue(
690 AMOTION_EVENT_AXIS_Y, pointerIndex, historicalIndex);
691 }
692
693 inline float getHistoricalPressure(size_t pointerIndex, size_t historicalIndex) const {
694 return getHistoricalAxisValue(
695 AMOTION_EVENT_AXIS_PRESSURE, pointerIndex, historicalIndex);
696 }
697
698 inline float getHistoricalSize(size_t pointerIndex, size_t historicalIndex) const {
699 return getHistoricalAxisValue(
700 AMOTION_EVENT_AXIS_SIZE, pointerIndex, historicalIndex);
701 }
702
703 inline float getHistoricalTouchMajor(size_t pointerIndex, size_t historicalIndex) const {
704 return getHistoricalAxisValue(
705 AMOTION_EVENT_AXIS_TOUCH_MAJOR, pointerIndex, historicalIndex);
706 }
707
708 inline float getHistoricalTouchMinor(size_t pointerIndex, size_t historicalIndex) const {
709 return getHistoricalAxisValue(
710 AMOTION_EVENT_AXIS_TOUCH_MINOR, pointerIndex, historicalIndex);
711 }
712
713 inline float getHistoricalToolMajor(size_t pointerIndex, size_t historicalIndex) const {
714 return getHistoricalAxisValue(
715 AMOTION_EVENT_AXIS_TOOL_MAJOR, pointerIndex, historicalIndex);
716 }
717
718 inline float getHistoricalToolMinor(size_t pointerIndex, size_t historicalIndex) const {
719 return getHistoricalAxisValue(
720 AMOTION_EVENT_AXIS_TOOL_MINOR, pointerIndex, historicalIndex);
721 }
722
723 inline float getHistoricalOrientation(size_t pointerIndex, size_t historicalIndex) const {
724 return getHistoricalAxisValue(
725 AMOTION_EVENT_AXIS_ORIENTATION, pointerIndex, historicalIndex);
726 }
727
728 ssize_t findPointerIndex(int32_t pointerId) const;
729
Garfield Tan4cc839f2020-01-24 11:26:14 -0800730 void initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600731 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
732 int32_t flags, int32_t edgeFlags, int32_t metaState, int32_t buttonState,
chaviw9eaa22c2020-07-01 16:21:27 -0700733 MotionClassification classification, const ui::Transform& transform,
734 float xPrecision, float yPrecision, float rawXCursorPosition,
Evan Rosky84f07f02021-04-16 10:42:42 -0700735 float rawYCursorPosition, int32_t displayWidth, int32_t displayHeight,
736 nsecs_t downTime, nsecs_t eventTime, size_t pointerCount,
737 const PointerProperties* pointerProperties, const PointerCoords* pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -0700738
739 void copyFrom(const MotionEvent* other, bool keepHistory);
740
741 void addSample(
742 nsecs_t eventTime,
743 const PointerCoords* pointerCoords);
744
745 void offsetLocation(float xOffset, float yOffset);
746
Robert Carre07e1032018-11-26 12:55:53 -0800747 void scale(float globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700748
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700749 // Apply 3x3 perspective matrix transformation.
750 // Matrix is in row-major form and compatible with SkMatrix.
chaviw9eaa22c2020-07-01 16:21:27 -0700751 void transform(const std::array<float, 9>& matrix);
Jeff Brown5912f952013-07-01 19:10:31 -0700752
Brett Chabotfaa986c2020-11-04 17:39:36 -0800753#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -0700754 status_t readFromParcel(Parcel* parcel);
755 status_t writeToParcel(Parcel* parcel) const;
Brett Chabotfaa986c2020-11-04 17:39:36 -0800756#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700757
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600758 static bool isTouchEvent(uint32_t source, int32_t action);
Jeff Brown5912f952013-07-01 19:10:31 -0700759 inline bool isTouchEvent() const {
760 return isTouchEvent(mSource, mAction);
761 }
762
763 // Low-level accessors.
764 inline const PointerProperties* getPointerProperties() const {
765 return mPointerProperties.array();
766 }
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500767 inline const nsecs_t* getSampleEventTimes() const { return mSampleEventTimes.data(); }
Jeff Brown5912f952013-07-01 19:10:31 -0700768 inline const PointerCoords* getSamplePointerCoords() const {
769 return mSamplePointerCoords.array();
770 }
771
Michael Wright872db4f2014-04-22 15:03:51 -0700772 static const char* getLabel(int32_t axis);
773 static int32_t getAxisFromLabel(const char* label);
774
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500775 static std::string actionToString(int32_t action);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700776
Jeff Brown5912f952013-07-01 19:10:31 -0700777protected:
778 int32_t mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100779 int32_t mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700780 int32_t mFlags;
781 int32_t mEdgeFlags;
782 int32_t mMetaState;
783 int32_t mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800784 MotionClassification mClassification;
chaviwfd9c1ed2020-07-01 10:57:59 -0700785 ui::Transform mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700786 float mXPrecision;
787 float mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700788 float mRawXCursorPosition;
789 float mRawYCursorPosition;
Evan Rosky84f07f02021-04-16 10:42:42 -0700790 int32_t mDisplayWidth;
791 int32_t mDisplayHeight;
Jeff Brown5912f952013-07-01 19:10:31 -0700792 nsecs_t mDownTime;
793 Vector<PointerProperties> mPointerProperties;
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500794 std::vector<nsecs_t> mSampleEventTimes;
Jeff Brown5912f952013-07-01 19:10:31 -0700795 Vector<PointerCoords> mSamplePointerCoords;
796};
797
798/*
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800799 * Focus events.
800 */
801class FocusEvent : public InputEvent {
802public:
803 virtual ~FocusEvent() {}
804
805 virtual int32_t getType() const override { return AINPUT_EVENT_TYPE_FOCUS; }
806
807 inline bool getHasFocus() const { return mHasFocus; }
808
809 inline bool getInTouchMode() const { return mInTouchMode; }
810
Garfield Tan4cc839f2020-01-24 11:26:14 -0800811 void initialize(int32_t id, bool hasFocus, bool inTouchMode);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800812
813 void initialize(const FocusEvent& from);
814
815protected:
816 bool mHasFocus;
817 bool mInTouchMode;
818};
819
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800820/*
821 * Capture events.
822 */
823class CaptureEvent : public InputEvent {
824public:
825 virtual ~CaptureEvent() {}
826
827 virtual int32_t getType() const override { return AINPUT_EVENT_TYPE_CAPTURE; }
828
829 inline bool getPointerCaptureEnabled() const { return mPointerCaptureEnabled; }
830
831 void initialize(int32_t id, bool pointerCaptureEnabled);
832
833 void initialize(const CaptureEvent& from);
834
835protected:
836 bool mPointerCaptureEnabled;
837};
838
arthurhung7632c332020-12-30 16:58:01 +0800839/*
840 * Drag events.
841 */
842class DragEvent : public InputEvent {
843public:
844 virtual ~DragEvent() {}
845
846 virtual int32_t getType() const override { return AINPUT_EVENT_TYPE_DRAG; }
847
848 inline bool isExiting() const { return mIsExiting; }
849
850 inline float getX() const { return mX; }
851
852 inline float getY() const { return mY; }
853
854 void initialize(int32_t id, float x, float y, bool isExiting);
855
856 void initialize(const DragEvent& from);
857
858protected:
859 bool mIsExiting;
860 float mX, mY;
861};
862
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800863/**
864 * Base class for verified events.
865 * Do not create a VerifiedInputEvent explicitly.
866 * Use helper functions to create them from InputEvents.
867 */
868struct __attribute__((__packed__)) VerifiedInputEvent {
869 enum class Type : int32_t {
870 KEY = AINPUT_EVENT_TYPE_KEY,
871 MOTION = AINPUT_EVENT_TYPE_MOTION,
872 };
873
874 Type type;
875 int32_t deviceId;
876 nsecs_t eventTimeNanos;
877 uint32_t source;
878 int32_t displayId;
879};
880
881/**
882 * Same as KeyEvent, but only contains the data that can be verified.
883 * If you update this class, you must also update VerifiedKeyEvent.java
884 */
885struct __attribute__((__packed__)) VerifiedKeyEvent : public VerifiedInputEvent {
886 int32_t action;
887 nsecs_t downTimeNanos;
888 int32_t flags;
889 int32_t keyCode;
890 int32_t scanCode;
891 int32_t metaState;
892 int32_t repeatCount;
893};
894
895/**
896 * Same as MotionEvent, but only contains the data that can be verified.
897 * If you update this class, you must also update VerifiedMotionEvent.java
898 */
899struct __attribute__((__packed__)) VerifiedMotionEvent : public VerifiedInputEvent {
900 float rawX;
901 float rawY;
902 int32_t actionMasked;
903 nsecs_t downTimeNanos;
904 int32_t flags;
905 int32_t metaState;
906 int32_t buttonState;
907};
908
909VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event);
910VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event);
911
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800912/*
Jeff Brown5912f952013-07-01 19:10:31 -0700913 * Input event factory.
914 */
915class InputEventFactoryInterface {
916protected:
917 virtual ~InputEventFactoryInterface() { }
918
919public:
920 InputEventFactoryInterface() { }
921
922 virtual KeyEvent* createKeyEvent() = 0;
923 virtual MotionEvent* createMotionEvent() = 0;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800924 virtual FocusEvent* createFocusEvent() = 0;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800925 virtual CaptureEvent* createCaptureEvent() = 0;
arthurhung7632c332020-12-30 16:58:01 +0800926 virtual DragEvent* createDragEvent() = 0;
Jeff Brown5912f952013-07-01 19:10:31 -0700927};
928
929/*
930 * A simple input event factory implementation that uses a single preallocated instance
931 * of each type of input event that are reused for each request.
932 */
933class PreallocatedInputEventFactory : public InputEventFactoryInterface {
934public:
935 PreallocatedInputEventFactory() { }
936 virtual ~PreallocatedInputEventFactory() { }
937
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800938 virtual KeyEvent* createKeyEvent() override { return &mKeyEvent; }
939 virtual MotionEvent* createMotionEvent() override { return &mMotionEvent; }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800940 virtual FocusEvent* createFocusEvent() override { return &mFocusEvent; }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800941 virtual CaptureEvent* createCaptureEvent() override { return &mCaptureEvent; }
arthurhung7632c332020-12-30 16:58:01 +0800942 virtual DragEvent* createDragEvent() override { return &mDragEvent; }
Jeff Brown5912f952013-07-01 19:10:31 -0700943
944private:
945 KeyEvent mKeyEvent;
946 MotionEvent mMotionEvent;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800947 FocusEvent mFocusEvent;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800948 CaptureEvent mCaptureEvent;
arthurhung7632c332020-12-30 16:58:01 +0800949 DragEvent mDragEvent;
Jeff Brown5912f952013-07-01 19:10:31 -0700950};
951
952/*
953 * An input event factory implementation that maintains a pool of input events.
954 */
955class PooledInputEventFactory : public InputEventFactoryInterface {
956public:
Chih-Hung Hsiehf43b02c2018-12-20 15:45:56 -0800957 explicit PooledInputEventFactory(size_t maxPoolSize = 20);
Jeff Brown5912f952013-07-01 19:10:31 -0700958 virtual ~PooledInputEventFactory();
959
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800960 virtual KeyEvent* createKeyEvent() override;
961 virtual MotionEvent* createMotionEvent() override;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800962 virtual FocusEvent* createFocusEvent() override;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800963 virtual CaptureEvent* createCaptureEvent() override;
arthurhung7632c332020-12-30 16:58:01 +0800964 virtual DragEvent* createDragEvent() override;
Jeff Brown5912f952013-07-01 19:10:31 -0700965
966 void recycle(InputEvent* event);
967
968private:
969 const size_t mMaxPoolSize;
970
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800971 std::queue<std::unique_ptr<KeyEvent>> mKeyEventPool;
972 std::queue<std::unique_ptr<MotionEvent>> mMotionEventPool;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800973 std::queue<std::unique_ptr<FocusEvent>> mFocusEventPool;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800974 std::queue<std::unique_ptr<CaptureEvent>> mCaptureEventPool;
arthurhung7632c332020-12-30 16:58:01 +0800975 std::queue<std::unique_ptr<DragEvent>> mDragEventPool;
Jeff Brown5912f952013-07-01 19:10:31 -0700976};
977
978} // namespace android
979
980#endif // _LIBINPUT_INPUT_H