blob: 194db1c6b518606ed25811960f6521a87dd59876 [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
Jeff Brown5912f952013-07-01 19:10:31 -070090enum {
91 /* Used when a motion event is not associated with any display.
92 * Typically used for non-pointer events. */
93 ADISPLAY_ID_NONE = -1,
94
95 /* The default display id. */
96 ADISPLAY_ID_DEFAULT = 0,
97};
98
99enum {
100 /*
101 * Indicates that an input device has switches.
102 * This input source flag is hidden from the API because switches are only used by the system
103 * and applications have no way to interact with them.
104 */
105 AINPUT_SOURCE_SWITCH = 0x80000000,
106};
107
Michael Wright962a1082013-10-17 17:35:53 -0700108enum {
109 /**
110 * Constants for LEDs. Hidden from the API since we don't actually expose a way to interact
111 * with LEDs to developers
112 *
Michael Wright872db4f2014-04-22 15:03:51 -0700113 * NOTE: If you add LEDs here, you must also add them to InputEventLabels.h
Michael Wright962a1082013-10-17 17:35:53 -0700114 */
115
116 ALED_NUM_LOCK = 0x00,
117 ALED_CAPS_LOCK = 0x01,
118 ALED_SCROLL_LOCK = 0x02,
119 ALED_COMPOSE = 0x03,
120 ALED_KANA = 0x04,
121 ALED_SLEEP = 0x05,
122 ALED_SUSPEND = 0x06,
123 ALED_MUTE = 0x07,
124 ALED_MISC = 0x08,
125 ALED_MAIL = 0x09,
126 ALED_CHARGING = 0x0a,
127 ALED_CONTROLLER_1 = 0x10,
128 ALED_CONTROLLER_2 = 0x11,
129 ALED_CONTROLLER_3 = 0x12,
130 ALED_CONTROLLER_4 = 0x13,
131};
132
Michael Wright9b04f862013-10-18 17:53:50 -0700133/* Maximum number of controller LEDs we support */
134#define MAX_CONTROLLER_LEDS 4
135
Jeff Brown5912f952013-07-01 19:10:31 -0700136/*
137 * SystemUiVisibility constants from View.
138 */
139enum {
140 ASYSTEM_UI_VISIBILITY_STATUS_BAR_VISIBLE = 0,
141 ASYSTEM_UI_VISIBILITY_STATUS_BAR_HIDDEN = 0x00000001,
142};
143
144/*
145 * Maximum number of pointers supported per motion event.
146 * Smallest number of pointers is 1.
147 * (We want at least 10 but some touch controllers obstensibly configured for 10 pointers
148 * will occasionally emit 11. There is not much harm making this constant bigger.)
149 */
150#define MAX_POINTERS 16
151
152/*
Flanker552a8a52015-09-07 15:28:58 +0800153 * Maximum number of samples supported per motion event.
154 */
155#define MAX_SAMPLES UINT16_MAX
156
157/*
Jeff Brown5912f952013-07-01 19:10:31 -0700158 * Maximum pointer id value supported in a motion event.
159 * Smallest pointer id is 0.
160 * (This is limited by our use of BitSet32 to track pointer assignments.)
161 */
162#define MAX_POINTER_ID 31
163
164/*
165 * Declare a concrete type for the NDK's input event forward declaration.
166 */
167struct AInputEvent {
168 virtual ~AInputEvent() { }
169};
170
171/*
172 * Declare a concrete type for the NDK's input device forward declaration.
173 */
174struct AInputDevice {
175 virtual ~AInputDevice() { }
176};
177
178
179namespace android {
180
Elliott Hughes6071da72015-08-12 15:27:47 -0700181#ifdef __ANDROID__
Jeff Brown5912f952013-07-01 19:10:31 -0700182class Parcel;
183#endif
184
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800185const char* inputEventTypeToString(int32_t type);
186
Jeff Brown5912f952013-07-01 19:10:31 -0700187/*
188 * Flags that flow alongside events in the input dispatch system to help with certain
189 * policy decisions such as waking from device sleep.
190 *
191 * These flags are also defined in frameworks/base/core/java/android/view/WindowManagerPolicy.java.
192 */
193enum {
194 /* These flags originate in RawEvents and are generally set in the key map.
Michael Wright872db4f2014-04-22 15:03:51 -0700195 * NOTE: If you want a flag to be able to set in a keylayout file, then you must add it to
196 * InputEventLabels.h as well. */
Jeff Brown5912f952013-07-01 19:10:31 -0700197
Jeff Brownc9aa6282015-02-11 19:03:28 -0800198 // Indicates that the event should wake the device.
Jeff Brown5912f952013-07-01 19:10:31 -0700199 POLICY_FLAG_WAKE = 0x00000001,
Jeff Brownc9aa6282015-02-11 19:03:28 -0800200
201 // Indicates that the key is virtual, such as a capacitive button, and should
202 // generate haptic feedback. Virtual keys may be suppressed for some time
203 // after a recent touch to prevent accidental activation of virtual keys adjacent
204 // to the touch screen during an edge swipe.
Michael Wright872db4f2014-04-22 15:03:51 -0700205 POLICY_FLAG_VIRTUAL = 0x00000002,
Jeff Brownc9aa6282015-02-11 19:03:28 -0800206
207 // Indicates that the key is the special function modifier.
Michael Wright872db4f2014-04-22 15:03:51 -0700208 POLICY_FLAG_FUNCTION = 0x00000004,
Jeff Brown5912f952013-07-01 19:10:31 -0700209
Jeff Brownc9aa6282015-02-11 19:03:28 -0800210 // Indicates that the key represents a special gesture that has been detected by
211 // the touch firmware or driver. Causes touch events from the same device to be canceled.
212 POLICY_FLAG_GESTURE = 0x00000008,
213
Jeff Brown5912f952013-07-01 19:10:31 -0700214 POLICY_FLAG_RAW_MASK = 0x0000ffff,
215
216 /* These flags are set by the input dispatcher. */
217
218 // Indicates that the input event was injected.
219 POLICY_FLAG_INJECTED = 0x01000000,
220
221 // Indicates that the input event is from a trusted source such as a directly attached
222 // input device or an application with system-wide event injection permission.
223 POLICY_FLAG_TRUSTED = 0x02000000,
224
225 // Indicates that the input event has passed through an input filter.
226 POLICY_FLAG_FILTERED = 0x04000000,
227
228 // Disables automatic key repeating behavior.
229 POLICY_FLAG_DISABLE_KEY_REPEAT = 0x08000000,
230
231 /* These flags are set by the input reader policy as it intercepts each event. */
232
Jeff Browndb19e462014-04-08 19:55:38 -0700233 // Indicates that the device was in an interactive state when the
234 // event was intercepted.
235 POLICY_FLAG_INTERACTIVE = 0x20000000,
Jeff Brown5912f952013-07-01 19:10:31 -0700236
237 // Indicates that the event should be dispatched to applications.
238 // The input event should still be sent to the InputDispatcher so that it can see all
239 // input events received include those that it will not deliver.
240 POLICY_FLAG_PASS_TO_USER = 0x40000000,
241};
242
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800243/**
244 * Classifications of the current gesture, if available.
245 *
246 * The following values must be kept in sync with MotionEvent.java
247 */
248enum class MotionClassification : uint8_t {
249 /**
250 * No classification is available.
251 */
252 NONE = 0,
253 /**
254 * Too early to classify the current gesture. Need more events. Look for changes in the
255 * upcoming motion events.
256 */
257 AMBIGUOUS_GESTURE = 1,
258 /**
259 * The current gesture likely represents a user intentionally exerting force on the touchscreen.
260 */
261 DEEP_PRESS = 2,
262};
263
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800264/**
265 * String representation of MotionClassification
266 */
267const char* motionClassificationToString(MotionClassification classification);
268
Garfield Tan00f511d2019-06-12 16:55:40 -0700269/**
Garfield Tan84b087e2020-01-23 10:49:05 -0800270 * Generator of unique numbers used to identify input events.
271 *
272 * Layout of ID:
273 * |--------------------------|---------------------------|
274 * | 2 bits for source | 30 bits for random number |
275 * |--------------------------|---------------------------|
276 */
277class IdGenerator {
278private:
279 static constexpr uint32_t SOURCE_SHIFT = 30;
280
281public:
282 // Used to divide integer space to ensure no conflict among these sources./
283 enum class Source : int32_t {
Garfield Tan750c7f42020-05-18 17:41:46 -0700284 INPUT_READER = static_cast<int32_t>(0x0u << SOURCE_SHIFT),
285 INPUT_DISPATCHER = static_cast<int32_t>(0x1u << SOURCE_SHIFT),
286 OTHER = static_cast<int32_t>(0x3u << SOURCE_SHIFT), // E.g. app injected events
Garfield Tan84b087e2020-01-23 10:49:05 -0800287 };
288 IdGenerator(Source source);
289
290 int32_t nextId() const;
291
292 // Extract source from given id.
293 static inline Source getSource(int32_t id) { return static_cast<Source>(SOURCE_MASK & id); }
294
295private:
296 const Source mSource;
297
Garfield Tan750c7f42020-05-18 17:41:46 -0700298 static constexpr int32_t SOURCE_MASK = static_cast<int32_t>(0x3u << SOURCE_SHIFT);
Garfield Tan84b087e2020-01-23 10:49:05 -0800299};
300
301/**
Garfield Tan00f511d2019-06-12 16:55:40 -0700302 * Invalid value for cursor position. Used for non-mouse events, tests and injected events. Don't
303 * use it for direct comparison with any other value, because NaN isn't equal to itself according to
304 * IEEE 754. Use isnan() instead to check if a cursor position is valid.
305 */
306constexpr float AMOTION_EVENT_INVALID_CURSOR_POSITION = std::numeric_limits<float>::quiet_NaN();
307
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600308/**
309 * Invalid value of HMAC - SHA256. Any events with this HMAC value will be marked as not verified.
310 */
311constexpr std::array<uint8_t, 32> INVALID_HMAC = {0};
312
Jeff Brown5912f952013-07-01 19:10:31 -0700313/*
314 * Pointer coordinate data.
315 */
316struct PointerCoords {
Michael Wright8f6710f2014-06-09 18:56:43 -0700317 enum { MAX_AXES = 30 }; // 30 so that sizeof(PointerCoords) == 128
Jeff Brown5912f952013-07-01 19:10:31 -0700318
319 // Bitfield of axes that are present in this structure.
Fengwei Yin83e0e422014-05-24 05:32:09 +0800320 uint64_t bits __attribute__((aligned(8)));
Jeff Brown5912f952013-07-01 19:10:31 -0700321
322 // Values of axes that are stored in this structure packed in order by axis id
323 // for each axis that is present in the structure according to 'bits'.
324 float values[MAX_AXES];
325
326 inline void clear() {
Michael Wrightd0bd3912014-03-19 12:06:10 -0700327 BitSet64::clear(bits);
328 }
329
330 bool isEmpty() const {
331 return BitSet64::isEmpty(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700332 }
333
334 float getAxisValue(int32_t axis) const;
335 status_t setAxisValue(int32_t axis, float value);
336
Robert Carre07e1032018-11-26 12:55:53 -0800337 void scale(float globalScale);
338
339 // Scale the pointer coordinates according to a global scale and a
340 // window scale. The global scale will be applied to TOUCH/TOOL_MAJOR/MINOR
341 // axes, however the window scaling will not.
342 void scale(float globalScale, float windowXScale, float windowYScale);
Jeff Browned4d28d2014-02-11 14:28:48 -0800343 void applyOffset(float xOffset, float yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700344
345 inline float getX() const {
346 return getAxisValue(AMOTION_EVENT_AXIS_X);
347 }
348
349 inline float getY() const {
350 return getAxisValue(AMOTION_EVENT_AXIS_Y);
351 }
352
Elliott Hughes6071da72015-08-12 15:27:47 -0700353#ifdef __ANDROID__
Jeff Brown5912f952013-07-01 19:10:31 -0700354 status_t readFromParcel(Parcel* parcel);
355 status_t writeToParcel(Parcel* parcel) const;
356#endif
357
358 bool operator==(const PointerCoords& other) const;
359 inline bool operator!=(const PointerCoords& other) const {
360 return !(*this == other);
361 }
362
363 void copyFrom(const PointerCoords& other);
364
365private:
366 void tooManyAxes(int axis);
367};
368
369/*
370 * Pointer property data.
371 */
372struct PointerProperties {
373 // The id of the pointer.
374 int32_t id;
375
376 // The pointer tool type.
377 int32_t toolType;
378
379 inline void clear() {
380 id = -1;
381 toolType = 0;
382 }
383
384 bool operator==(const PointerProperties& other) const;
385 inline bool operator!=(const PointerProperties& other) const {
386 return !(*this == other);
387 }
388
389 void copyFrom(const PointerProperties& other);
390};
391
392/*
393 * Input events.
394 */
395class InputEvent : public AInputEvent {
396public:
397 virtual ~InputEvent() { }
398
399 virtual int32_t getType() const = 0;
400
Garfield Tan4cc839f2020-01-24 11:26:14 -0800401 inline int32_t getId() const { return mId; }
402
Jeff Brown5912f952013-07-01 19:10:31 -0700403 inline int32_t getDeviceId() const { return mDeviceId; }
404
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600405 inline uint32_t getSource() const { return mSource; }
Jeff Brown5912f952013-07-01 19:10:31 -0700406
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600407 inline void setSource(uint32_t source) { mSource = source; }
Jeff Brown5912f952013-07-01 19:10:31 -0700408
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100409 inline int32_t getDisplayId() const { return mDisplayId; }
410
411 inline void setDisplayId(int32_t displayId) { mDisplayId = displayId; }
412
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600413 inline std::array<uint8_t, 32> getHmac() const { return mHmac; }
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100414
Garfield Tan4cc839f2020-01-24 11:26:14 -0800415 static int32_t nextId();
416
Jeff Brown5912f952013-07-01 19:10:31 -0700417protected:
Garfield Tan4cc839f2020-01-24 11:26:14 -0800418 void initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600419 std::array<uint8_t, 32> hmac);
Garfield Tan4cc839f2020-01-24 11:26:14 -0800420
Jeff Brown5912f952013-07-01 19:10:31 -0700421 void initialize(const InputEvent& from);
422
Garfield Tan4cc839f2020-01-24 11:26:14 -0800423 int32_t mId;
Jeff Brown5912f952013-07-01 19:10:31 -0700424 int32_t mDeviceId;
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600425 uint32_t mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100426 int32_t mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600427 std::array<uint8_t, 32> mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700428};
429
430/*
431 * Key events.
432 */
433class KeyEvent : public InputEvent {
434public:
435 virtual ~KeyEvent() { }
436
437 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_KEY; }
438
439 inline int32_t getAction() const { return mAction; }
440
441 inline int32_t getFlags() const { return mFlags; }
442
443 inline void setFlags(int32_t flags) { mFlags = flags; }
444
445 inline int32_t getKeyCode() const { return mKeyCode; }
446
447 inline int32_t getScanCode() const { return mScanCode; }
448
449 inline int32_t getMetaState() const { return mMetaState; }
450
451 inline int32_t getRepeatCount() const { return mRepeatCount; }
452
453 inline nsecs_t getDownTime() const { return mDownTime; }
454
455 inline nsecs_t getEventTime() const { return mEventTime; }
456
Michael Wright872db4f2014-04-22 15:03:51 -0700457 static const char* getLabel(int32_t keyCode);
458 static int32_t getKeyCodeFromLabel(const char* label);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800459
Garfield Tan4cc839f2020-01-24 11:26:14 -0800460 void initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600461 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags, int32_t keyCode,
462 int32_t scanCode, int32_t metaState, int32_t repeatCount, nsecs_t downTime,
463 nsecs_t eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -0700464 void initialize(const KeyEvent& from);
465
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700466 static const char* actionToString(int32_t action);
467
Jeff Brown5912f952013-07-01 19:10:31 -0700468protected:
469 int32_t mAction;
470 int32_t mFlags;
471 int32_t mKeyCode;
472 int32_t mScanCode;
473 int32_t mMetaState;
474 int32_t mRepeatCount;
475 nsecs_t mDownTime;
476 nsecs_t mEventTime;
477};
478
479/*
480 * Motion events.
481 */
482class MotionEvent : public InputEvent {
483public:
484 virtual ~MotionEvent() { }
485
486 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_MOTION; }
487
488 inline int32_t getAction() const { return mAction; }
489
490 inline int32_t getActionMasked() const { return mAction & AMOTION_EVENT_ACTION_MASK; }
491
492 inline int32_t getActionIndex() const {
493 return (mAction & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK)
494 >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
495 }
496
497 inline void setAction(int32_t action) { mAction = action; }
498
499 inline int32_t getFlags() const { return mFlags; }
500
501 inline void setFlags(int32_t flags) { mFlags = flags; }
502
503 inline int32_t getEdgeFlags() const { return mEdgeFlags; }
504
505 inline void setEdgeFlags(int32_t edgeFlags) { mEdgeFlags = edgeFlags; }
506
507 inline int32_t getMetaState() const { return mMetaState; }
508
509 inline void setMetaState(int32_t metaState) { mMetaState = metaState; }
510
511 inline int32_t getButtonState() const { return mButtonState; }
512
Michael Wright6db58792016-09-14 19:53:37 +0100513 inline void setButtonState(int32_t buttonState) { mButtonState = buttonState; }
Michael Wright7b159c92015-05-14 14:48:03 +0100514
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800515 inline MotionClassification getClassification() const { return mClassification; }
516
Michael Wright7b159c92015-05-14 14:48:03 +0100517 inline int32_t getActionButton() const { return mActionButton; }
518
Michael Wright21957b92015-06-17 21:06:54 +0100519 inline void setActionButton(int32_t button) { mActionButton = button; }
520
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600521 inline float getXScale() const { return mXScale; }
522
523 inline float getYScale() const { return mYScale; }
524
Jeff Brown5912f952013-07-01 19:10:31 -0700525 inline float getXOffset() const { return mXOffset; }
526
527 inline float getYOffset() const { return mYOffset; }
528
529 inline float getXPrecision() const { return mXPrecision; }
530
531 inline float getYPrecision() const { return mYPrecision; }
532
Garfield Tan937bb832019-07-25 17:48:31 -0700533 inline float getRawXCursorPosition() const { return mRawXCursorPosition; }
Garfield Tan00f511d2019-06-12 16:55:40 -0700534
535 float getXCursorPosition() const;
536
Garfield Tan937bb832019-07-25 17:48:31 -0700537 inline float getRawYCursorPosition() const { return mRawYCursorPosition; }
Garfield Tan00f511d2019-06-12 16:55:40 -0700538
539 float getYCursorPosition() const;
540
Garfield Tan937bb832019-07-25 17:48:31 -0700541 void setCursorPosition(float x, float y);
542
Garfield Tanab0ab9c2019-07-10 18:58:28 -0700543 static inline bool isValidCursorPosition(float x, float y) { return !isnan(x) && !isnan(y); }
544
Jeff Brown5912f952013-07-01 19:10:31 -0700545 inline nsecs_t getDownTime() const { return mDownTime; }
546
547 inline void setDownTime(nsecs_t downTime) { mDownTime = downTime; }
548
549 inline size_t getPointerCount() const { return mPointerProperties.size(); }
550
551 inline const PointerProperties* getPointerProperties(size_t pointerIndex) const {
552 return &mPointerProperties[pointerIndex];
553 }
554
555 inline int32_t getPointerId(size_t pointerIndex) const {
556 return mPointerProperties[pointerIndex].id;
557 }
558
559 inline int32_t getToolType(size_t pointerIndex) const {
560 return mPointerProperties[pointerIndex].toolType;
561 }
562
563 inline nsecs_t getEventTime() const { return mSampleEventTimes[getHistorySize()]; }
564
565 const PointerCoords* getRawPointerCoords(size_t pointerIndex) const;
566
567 float getRawAxisValue(int32_t axis, size_t pointerIndex) const;
568
569 inline float getRawX(size_t pointerIndex) const {
570 return getRawAxisValue(AMOTION_EVENT_AXIS_X, pointerIndex);
571 }
572
573 inline float getRawY(size_t pointerIndex) const {
574 return getRawAxisValue(AMOTION_EVENT_AXIS_Y, pointerIndex);
575 }
576
577 float getAxisValue(int32_t axis, size_t pointerIndex) const;
578
579 inline float getX(size_t pointerIndex) const {
580 return getAxisValue(AMOTION_EVENT_AXIS_X, pointerIndex);
581 }
582
583 inline float getY(size_t pointerIndex) const {
584 return getAxisValue(AMOTION_EVENT_AXIS_Y, pointerIndex);
585 }
586
587 inline float getPressure(size_t pointerIndex) const {
588 return getAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pointerIndex);
589 }
590
591 inline float getSize(size_t pointerIndex) const {
592 return getAxisValue(AMOTION_EVENT_AXIS_SIZE, pointerIndex);
593 }
594
595 inline float getTouchMajor(size_t pointerIndex) const {
596 return getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, pointerIndex);
597 }
598
599 inline float getTouchMinor(size_t pointerIndex) const {
600 return getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, pointerIndex);
601 }
602
603 inline float getToolMajor(size_t pointerIndex) const {
604 return getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, pointerIndex);
605 }
606
607 inline float getToolMinor(size_t pointerIndex) const {
608 return getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, pointerIndex);
609 }
610
611 inline float getOrientation(size_t pointerIndex) const {
612 return getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, pointerIndex);
613 }
614
615 inline size_t getHistorySize() const { return mSampleEventTimes.size() - 1; }
616
617 inline nsecs_t getHistoricalEventTime(size_t historicalIndex) const {
618 return mSampleEventTimes[historicalIndex];
619 }
620
621 const PointerCoords* getHistoricalRawPointerCoords(
622 size_t pointerIndex, size_t historicalIndex) const;
623
624 float getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
625 size_t historicalIndex) const;
626
627 inline float getHistoricalRawX(size_t pointerIndex, size_t historicalIndex) const {
628 return getHistoricalRawAxisValue(
629 AMOTION_EVENT_AXIS_X, pointerIndex, historicalIndex);
630 }
631
632 inline float getHistoricalRawY(size_t pointerIndex, size_t historicalIndex) const {
633 return getHistoricalRawAxisValue(
634 AMOTION_EVENT_AXIS_Y, pointerIndex, historicalIndex);
635 }
636
637 float getHistoricalAxisValue(int32_t axis, size_t pointerIndex, size_t historicalIndex) const;
638
639 inline float getHistoricalX(size_t pointerIndex, size_t historicalIndex) const {
640 return getHistoricalAxisValue(
641 AMOTION_EVENT_AXIS_X, pointerIndex, historicalIndex);
642 }
643
644 inline float getHistoricalY(size_t pointerIndex, size_t historicalIndex) const {
645 return getHistoricalAxisValue(
646 AMOTION_EVENT_AXIS_Y, pointerIndex, historicalIndex);
647 }
648
649 inline float getHistoricalPressure(size_t pointerIndex, size_t historicalIndex) const {
650 return getHistoricalAxisValue(
651 AMOTION_EVENT_AXIS_PRESSURE, pointerIndex, historicalIndex);
652 }
653
654 inline float getHistoricalSize(size_t pointerIndex, size_t historicalIndex) const {
655 return getHistoricalAxisValue(
656 AMOTION_EVENT_AXIS_SIZE, pointerIndex, historicalIndex);
657 }
658
659 inline float getHistoricalTouchMajor(size_t pointerIndex, size_t historicalIndex) const {
660 return getHistoricalAxisValue(
661 AMOTION_EVENT_AXIS_TOUCH_MAJOR, pointerIndex, historicalIndex);
662 }
663
664 inline float getHistoricalTouchMinor(size_t pointerIndex, size_t historicalIndex) const {
665 return getHistoricalAxisValue(
666 AMOTION_EVENT_AXIS_TOUCH_MINOR, pointerIndex, historicalIndex);
667 }
668
669 inline float getHistoricalToolMajor(size_t pointerIndex, size_t historicalIndex) const {
670 return getHistoricalAxisValue(
671 AMOTION_EVENT_AXIS_TOOL_MAJOR, pointerIndex, historicalIndex);
672 }
673
674 inline float getHistoricalToolMinor(size_t pointerIndex, size_t historicalIndex) const {
675 return getHistoricalAxisValue(
676 AMOTION_EVENT_AXIS_TOOL_MINOR, pointerIndex, historicalIndex);
677 }
678
679 inline float getHistoricalOrientation(size_t pointerIndex, size_t historicalIndex) const {
680 return getHistoricalAxisValue(
681 AMOTION_EVENT_AXIS_ORIENTATION, pointerIndex, historicalIndex);
682 }
683
684 ssize_t findPointerIndex(int32_t pointerId) const;
685
Garfield Tan4cc839f2020-01-24 11:26:14 -0800686 void initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600687 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
688 int32_t flags, int32_t edgeFlags, int32_t metaState, int32_t buttonState,
689 MotionClassification classification, float xScale, float yScale, float xOffset,
Garfield Tan937bb832019-07-25 17:48:31 -0700690 float yOffset, float xPrecision, float yPrecision, float rawXCursorPosition,
691 float rawYCursorPosition, nsecs_t downTime, nsecs_t eventTime,
Garfield Tan00f511d2019-06-12 16:55:40 -0700692 size_t pointerCount, const PointerProperties* pointerProperties,
693 const PointerCoords* pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -0700694
695 void copyFrom(const MotionEvent* other, bool keepHistory);
696
697 void addSample(
698 nsecs_t eventTime,
699 const PointerCoords* pointerCoords);
700
701 void offsetLocation(float xOffset, float yOffset);
702
Robert Carre07e1032018-11-26 12:55:53 -0800703 void scale(float globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700704
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700705 // Apply 3x3 perspective matrix transformation.
706 // Matrix is in row-major form and compatible with SkMatrix.
707 void transform(const float matrix[9]);
Jeff Brown5912f952013-07-01 19:10:31 -0700708
Elliott Hughes6071da72015-08-12 15:27:47 -0700709#ifdef __ANDROID__
Jeff Brown5912f952013-07-01 19:10:31 -0700710 status_t readFromParcel(Parcel* parcel);
711 status_t writeToParcel(Parcel* parcel) const;
712#endif
713
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600714 static bool isTouchEvent(uint32_t source, int32_t action);
Jeff Brown5912f952013-07-01 19:10:31 -0700715 inline bool isTouchEvent() const {
716 return isTouchEvent(mSource, mAction);
717 }
718
719 // Low-level accessors.
720 inline const PointerProperties* getPointerProperties() const {
721 return mPointerProperties.array();
722 }
723 inline const nsecs_t* getSampleEventTimes() const { return mSampleEventTimes.array(); }
724 inline const PointerCoords* getSamplePointerCoords() const {
725 return mSamplePointerCoords.array();
726 }
727
Michael Wright872db4f2014-04-22 15:03:51 -0700728 static const char* getLabel(int32_t axis);
729 static int32_t getAxisFromLabel(const char* label);
730
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700731 static const char* actionToString(int32_t action);
732
Jeff Brown5912f952013-07-01 19:10:31 -0700733protected:
734 int32_t mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100735 int32_t mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700736 int32_t mFlags;
737 int32_t mEdgeFlags;
738 int32_t mMetaState;
739 int32_t mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800740 MotionClassification mClassification;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600741 float mXScale;
742 float mYScale;
Jeff Brown5912f952013-07-01 19:10:31 -0700743 float mXOffset;
744 float mYOffset;
chaviwfd9c1ed2020-07-01 10:57:59 -0700745 ui::Transform mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700746 float mXPrecision;
747 float mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700748 float mRawXCursorPosition;
749 float mRawYCursorPosition;
Jeff Brown5912f952013-07-01 19:10:31 -0700750 nsecs_t mDownTime;
751 Vector<PointerProperties> mPointerProperties;
752 Vector<nsecs_t> mSampleEventTimes;
753 Vector<PointerCoords> mSamplePointerCoords;
754};
755
756/*
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800757 * Focus events.
758 */
759class FocusEvent : public InputEvent {
760public:
761 virtual ~FocusEvent() {}
762
763 virtual int32_t getType() const override { return AINPUT_EVENT_TYPE_FOCUS; }
764
765 inline bool getHasFocus() const { return mHasFocus; }
766
767 inline bool getInTouchMode() const { return mInTouchMode; }
768
Garfield Tan4cc839f2020-01-24 11:26:14 -0800769 void initialize(int32_t id, bool hasFocus, bool inTouchMode);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800770
771 void initialize(const FocusEvent& from);
772
773protected:
774 bool mHasFocus;
775 bool mInTouchMode;
776};
777
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800778/**
779 * Base class for verified events.
780 * Do not create a VerifiedInputEvent explicitly.
781 * Use helper functions to create them from InputEvents.
782 */
783struct __attribute__((__packed__)) VerifiedInputEvent {
784 enum class Type : int32_t {
785 KEY = AINPUT_EVENT_TYPE_KEY,
786 MOTION = AINPUT_EVENT_TYPE_MOTION,
787 };
788
789 Type type;
790 int32_t deviceId;
791 nsecs_t eventTimeNanos;
792 uint32_t source;
793 int32_t displayId;
794};
795
796/**
797 * Same as KeyEvent, but only contains the data that can be verified.
798 * If you update this class, you must also update VerifiedKeyEvent.java
799 */
800struct __attribute__((__packed__)) VerifiedKeyEvent : public VerifiedInputEvent {
801 int32_t action;
802 nsecs_t downTimeNanos;
803 int32_t flags;
804 int32_t keyCode;
805 int32_t scanCode;
806 int32_t metaState;
807 int32_t repeatCount;
808};
809
810/**
811 * Same as MotionEvent, but only contains the data that can be verified.
812 * If you update this class, you must also update VerifiedMotionEvent.java
813 */
814struct __attribute__((__packed__)) VerifiedMotionEvent : public VerifiedInputEvent {
815 float rawX;
816 float rawY;
817 int32_t actionMasked;
818 nsecs_t downTimeNanos;
819 int32_t flags;
820 int32_t metaState;
821 int32_t buttonState;
822};
823
824VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event);
825VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event);
826
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800827/*
Jeff Brown5912f952013-07-01 19:10:31 -0700828 * Input event factory.
829 */
830class InputEventFactoryInterface {
831protected:
832 virtual ~InputEventFactoryInterface() { }
833
834public:
835 InputEventFactoryInterface() { }
836
837 virtual KeyEvent* createKeyEvent() = 0;
838 virtual MotionEvent* createMotionEvent() = 0;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800839 virtual FocusEvent* createFocusEvent() = 0;
Jeff Brown5912f952013-07-01 19:10:31 -0700840};
841
842/*
843 * A simple input event factory implementation that uses a single preallocated instance
844 * of each type of input event that are reused for each request.
845 */
846class PreallocatedInputEventFactory : public InputEventFactoryInterface {
847public:
848 PreallocatedInputEventFactory() { }
849 virtual ~PreallocatedInputEventFactory() { }
850
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800851 virtual KeyEvent* createKeyEvent() override { return &mKeyEvent; }
852 virtual MotionEvent* createMotionEvent() override { return &mMotionEvent; }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800853 virtual FocusEvent* createFocusEvent() override { return &mFocusEvent; }
Jeff Brown5912f952013-07-01 19:10:31 -0700854
855private:
856 KeyEvent mKeyEvent;
857 MotionEvent mMotionEvent;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800858 FocusEvent mFocusEvent;
Jeff Brown5912f952013-07-01 19:10:31 -0700859};
860
861/*
862 * An input event factory implementation that maintains a pool of input events.
863 */
864class PooledInputEventFactory : public InputEventFactoryInterface {
865public:
Chih-Hung Hsiehf43b02c2018-12-20 15:45:56 -0800866 explicit PooledInputEventFactory(size_t maxPoolSize = 20);
Jeff Brown5912f952013-07-01 19:10:31 -0700867 virtual ~PooledInputEventFactory();
868
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800869 virtual KeyEvent* createKeyEvent() override;
870 virtual MotionEvent* createMotionEvent() override;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800871 virtual FocusEvent* createFocusEvent() override;
Jeff Brown5912f952013-07-01 19:10:31 -0700872
873 void recycle(InputEvent* event);
874
875private:
876 const size_t mMaxPoolSize;
877
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800878 std::queue<std::unique_ptr<KeyEvent>> mKeyEventPool;
879 std::queue<std::unique_ptr<MotionEvent>> mMotionEventPool;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800880 std::queue<std::unique_ptr<FocusEvent>> mFocusEventPool;
Jeff Brown5912f952013-07-01 19:10:31 -0700881};
882
883} // namespace android
884
885#endif // _LIBINPUT_INPUT_H