blob: af9c41d7c3cb9f5c059aa5c20fedf6b5f4438a97 [file] [log] [blame]
Jeff Brown5912f952013-07-01 19:10:31 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _LIBINPUT_INPUT_H
18#define _LIBINPUT_INPUT_H
19
Robert Carr2c358bf2018-08-08 15:58:15 -070020#pragma GCC system_header
21
Jeff Brown5912f952013-07-01 19:10:31 -070022/**
23 * Native input event structures.
24 */
25
26#include <android/input.h>
Garfield Tanab0ab9c2019-07-10 18:58:28 -070027#include <math.h>
Garfield Tan00f511d2019-06-12 16:55:40 -070028#include <stdint.h>
Michael Wrightd0bd3912014-03-19 12:06:10 -070029#include <utils/BitSet.h>
Jeff Brown5912f952013-07-01 19:10:31 -070030#include <utils/KeyedVector.h>
Jeff Brown5912f952013-07-01 19:10:31 -070031#include <utils/RefBase.h>
Michael Wrightd0bd3912014-03-19 12:06:10 -070032#include <utils/Timers.h>
33#include <utils/Vector.h>
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -060034#include <array>
Garfield Tan00f511d2019-06-12 16:55:40 -070035#include <limits>
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -080036#include <queue>
Jeff Brown5912f952013-07-01 19:10:31 -070037
Jeff Brown5912f952013-07-01 19:10:31 -070038/*
39 * Additional private constants not defined in ndk/ui/input.h.
40 */
41enum {
42 /* Signifies that the key is being predispatched */
43 AKEY_EVENT_FLAG_PREDISPATCH = 0x20000000,
44
45 /* Private control to determine when an app is tracking a key sequence. */
46 AKEY_EVENT_FLAG_START_TRACKING = 0x40000000,
47
48 /* Key event is inconsistent with previously sent key events. */
49 AKEY_EVENT_FLAG_TAINTED = 0x80000000,
50};
51
52enum {
Michael Wrightcdcd8f22016-03-22 16:52:13 -070053
54 /**
55 * This flag indicates that the window that received this motion event is partly
56 * or wholly obscured by another visible window above it. This flag is set to true
57 * even if the event did not directly pass through the obscured area.
58 * A security sensitive application can check this flag to identify situations in which
59 * a malicious application may have covered up part of its content for the purpose
60 * of misleading the user or hijacking touches. An appropriate response might be
61 * to drop the suspect touches or to take additional precautions to confirm the user's
62 * actual intent.
63 */
64 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED = 0x2,
65
Prabir Pradhan141a9862018-11-19 14:35:56 -080066 /**
67 * This flag indicates that the event has been generated by a gesture generator. It
68 * provides a hint to the GestureDetector to not apply any touch slop.
69 */
70 AMOTION_EVENT_FLAG_IS_GENERATED_GESTURE = 0x8,
71
Jeff Brown5912f952013-07-01 19:10:31 -070072 /* Motion event is inconsistent with previously sent motion events. */
73 AMOTION_EVENT_FLAG_TAINTED = 0x80000000,
74};
75
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -080076/**
77 * Allowed VerifiedKeyEvent flags. All other flags from KeyEvent do not get verified.
78 * These values must be kept in sync with VerifiedKeyEvent.java
79 */
80constexpr int32_t VERIFIED_KEY_EVENT_FLAGS = AKEY_EVENT_FLAG_CANCELED;
81
82/**
83 * Allowed VerifiedMotionEventFlags. All other flags from MotionEvent do not get verified.
84 * These values must be kept in sync with VerifiedMotionEvent.java
85 */
86constexpr int32_t VERIFIED_MOTION_EVENT_FLAGS =
87 AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED;
88
arthurhung65600042020-04-30 17:55:40 +080089/**
90 * This flag indicates that the point up event has been canceled.
91 * Typically this is used for palm event when the user has accidental touches.
92 * TODO: Adjust flag to public api
93 */
94constexpr int32_t AMOTION_EVENT_FLAG_CANCELED = 0x20;
95
Jeff Brown5912f952013-07-01 19:10:31 -070096enum {
97 /* Used when a motion event is not associated with any display.
98 * Typically used for non-pointer events. */
99 ADISPLAY_ID_NONE = -1,
100
101 /* The default display id. */
102 ADISPLAY_ID_DEFAULT = 0,
103};
104
105enum {
106 /*
107 * Indicates that an input device has switches.
108 * This input source flag is hidden from the API because switches are only used by the system
109 * and applications have no way to interact with them.
110 */
111 AINPUT_SOURCE_SWITCH = 0x80000000,
112};
113
Michael Wright962a1082013-10-17 17:35:53 -0700114enum {
115 /**
116 * Constants for LEDs. Hidden from the API since we don't actually expose a way to interact
117 * with LEDs to developers
118 *
Michael Wright872db4f2014-04-22 15:03:51 -0700119 * NOTE: If you add LEDs here, you must also add them to InputEventLabels.h
Michael Wright962a1082013-10-17 17:35:53 -0700120 */
121
122 ALED_NUM_LOCK = 0x00,
123 ALED_CAPS_LOCK = 0x01,
124 ALED_SCROLL_LOCK = 0x02,
125 ALED_COMPOSE = 0x03,
126 ALED_KANA = 0x04,
127 ALED_SLEEP = 0x05,
128 ALED_SUSPEND = 0x06,
129 ALED_MUTE = 0x07,
130 ALED_MISC = 0x08,
131 ALED_MAIL = 0x09,
132 ALED_CHARGING = 0x0a,
133 ALED_CONTROLLER_1 = 0x10,
134 ALED_CONTROLLER_2 = 0x11,
135 ALED_CONTROLLER_3 = 0x12,
136 ALED_CONTROLLER_4 = 0x13,
137};
138
Michael Wright9b04f862013-10-18 17:53:50 -0700139/* Maximum number of controller LEDs we support */
140#define MAX_CONTROLLER_LEDS 4
141
Jeff Brown5912f952013-07-01 19:10:31 -0700142/*
143 * SystemUiVisibility constants from View.
144 */
145enum {
146 ASYSTEM_UI_VISIBILITY_STATUS_BAR_VISIBLE = 0,
147 ASYSTEM_UI_VISIBILITY_STATUS_BAR_HIDDEN = 0x00000001,
148};
149
150/*
151 * Maximum number of pointers supported per motion event.
152 * Smallest number of pointers is 1.
153 * (We want at least 10 but some touch controllers obstensibly configured for 10 pointers
154 * will occasionally emit 11. There is not much harm making this constant bigger.)
155 */
156#define MAX_POINTERS 16
157
158/*
Flanker552a8a52015-09-07 15:28:58 +0800159 * Maximum number of samples supported per motion event.
160 */
161#define MAX_SAMPLES UINT16_MAX
162
163/*
Jeff Brown5912f952013-07-01 19:10:31 -0700164 * Maximum pointer id value supported in a motion event.
165 * Smallest pointer id is 0.
166 * (This is limited by our use of BitSet32 to track pointer assignments.)
167 */
168#define MAX_POINTER_ID 31
169
170/*
171 * Declare a concrete type for the NDK's input event forward declaration.
172 */
173struct AInputEvent {
174 virtual ~AInputEvent() { }
175};
176
177/*
178 * Declare a concrete type for the NDK's input device forward declaration.
179 */
180struct AInputDevice {
181 virtual ~AInputDevice() { }
182};
183
184
185namespace android {
186
Elliott Hughes6071da72015-08-12 15:27:47 -0700187#ifdef __ANDROID__
Jeff Brown5912f952013-07-01 19:10:31 -0700188class Parcel;
189#endif
190
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800191const char* inputEventTypeToString(int32_t type);
192
Jeff Brown5912f952013-07-01 19:10:31 -0700193/*
194 * Flags that flow alongside events in the input dispatch system to help with certain
195 * policy decisions such as waking from device sleep.
196 *
197 * These flags are also defined in frameworks/base/core/java/android/view/WindowManagerPolicy.java.
198 */
199enum {
200 /* These flags originate in RawEvents and are generally set in the key map.
Michael Wright872db4f2014-04-22 15:03:51 -0700201 * NOTE: If you want a flag to be able to set in a keylayout file, then you must add it to
202 * InputEventLabels.h as well. */
Jeff Brown5912f952013-07-01 19:10:31 -0700203
Jeff Brownc9aa6282015-02-11 19:03:28 -0800204 // Indicates that the event should wake the device.
Jeff Brown5912f952013-07-01 19:10:31 -0700205 POLICY_FLAG_WAKE = 0x00000001,
Jeff Brownc9aa6282015-02-11 19:03:28 -0800206
207 // Indicates that the key is virtual, such as a capacitive button, and should
208 // generate haptic feedback. Virtual keys may be suppressed for some time
209 // after a recent touch to prevent accidental activation of virtual keys adjacent
210 // to the touch screen during an edge swipe.
Michael Wright872db4f2014-04-22 15:03:51 -0700211 POLICY_FLAG_VIRTUAL = 0x00000002,
Jeff Brownc9aa6282015-02-11 19:03:28 -0800212
213 // Indicates that the key is the special function modifier.
Michael Wright872db4f2014-04-22 15:03:51 -0700214 POLICY_FLAG_FUNCTION = 0x00000004,
Jeff Brown5912f952013-07-01 19:10:31 -0700215
Jeff Brownc9aa6282015-02-11 19:03:28 -0800216 // Indicates that the key represents a special gesture that has been detected by
217 // the touch firmware or driver. Causes touch events from the same device to be canceled.
218 POLICY_FLAG_GESTURE = 0x00000008,
219
Jeff Brown5912f952013-07-01 19:10:31 -0700220 POLICY_FLAG_RAW_MASK = 0x0000ffff,
221
222 /* These flags are set by the input dispatcher. */
223
224 // Indicates that the input event was injected.
225 POLICY_FLAG_INJECTED = 0x01000000,
226
227 // Indicates that the input event is from a trusted source such as a directly attached
228 // input device or an application with system-wide event injection permission.
229 POLICY_FLAG_TRUSTED = 0x02000000,
230
231 // Indicates that the input event has passed through an input filter.
232 POLICY_FLAG_FILTERED = 0x04000000,
233
234 // Disables automatic key repeating behavior.
235 POLICY_FLAG_DISABLE_KEY_REPEAT = 0x08000000,
236
237 /* These flags are set by the input reader policy as it intercepts each event. */
238
Jeff Browndb19e462014-04-08 19:55:38 -0700239 // Indicates that the device was in an interactive state when the
240 // event was intercepted.
241 POLICY_FLAG_INTERACTIVE = 0x20000000,
Jeff Brown5912f952013-07-01 19:10:31 -0700242
243 // Indicates that the event should be dispatched to applications.
244 // The input event should still be sent to the InputDispatcher so that it can see all
245 // input events received include those that it will not deliver.
246 POLICY_FLAG_PASS_TO_USER = 0x40000000,
247};
248
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800249/**
250 * Classifications of the current gesture, if available.
251 *
252 * The following values must be kept in sync with MotionEvent.java
253 */
254enum class MotionClassification : uint8_t {
255 /**
256 * No classification is available.
257 */
258 NONE = 0,
259 /**
260 * Too early to classify the current gesture. Need more events. Look for changes in the
261 * upcoming motion events.
262 */
263 AMBIGUOUS_GESTURE = 1,
264 /**
265 * The current gesture likely represents a user intentionally exerting force on the touchscreen.
266 */
267 DEEP_PRESS = 2,
268};
269
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800270/**
271 * String representation of MotionClassification
272 */
273const char* motionClassificationToString(MotionClassification classification);
274
Garfield Tan00f511d2019-06-12 16:55:40 -0700275/**
Garfield Tanbd25f1c2020-01-23 10:49:05 -0800276 * Generator of unique numbers used to identify input events.
277 *
278 * Layout of ID:
279 * |--------------------------|---------------------------|
280 * | 2 bits for source | 30 bits for random number |
281 * |--------------------------|---------------------------|
282 */
283class IdGenerator {
284private:
285 static constexpr uint32_t SOURCE_SHIFT = 30;
286
287public:
288 // Used to divide integer space to ensure no conflict among these sources./
289 enum class Source : int32_t {
290 INPUT_READER = 0x0 << SOURCE_SHIFT,
291 INPUT_DISPATCHER = 0x1 << SOURCE_SHIFT,
292 OTHER = 0x3 << SOURCE_SHIFT, // E.g. app injected events
293 };
294 IdGenerator(Source source);
295
296 int32_t nextId() const;
297
298 // Extract source from given id.
299 static inline Source getSource(int32_t id) { return static_cast<Source>(SOURCE_MASK & id); }
300
301private:
302 const Source mSource;
303
304 static constexpr int32_t SOURCE_MASK = 0x3 << SOURCE_SHIFT;
305};
306
307/**
Garfield Tan00f511d2019-06-12 16:55:40 -0700308 * Invalid value for cursor position. Used for non-mouse events, tests and injected events. Don't
309 * use it for direct comparison with any other value, because NaN isn't equal to itself according to
310 * IEEE 754. Use isnan() instead to check if a cursor position is valid.
311 */
312constexpr float AMOTION_EVENT_INVALID_CURSOR_POSITION = std::numeric_limits<float>::quiet_NaN();
313
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600314/**
315 * Invalid value of HMAC - SHA256. Any events with this HMAC value will be marked as not verified.
316 */
317constexpr std::array<uint8_t, 32> INVALID_HMAC = {0};
318
Jeff Brown5912f952013-07-01 19:10:31 -0700319/*
320 * Pointer coordinate data.
321 */
322struct PointerCoords {
Michael Wright8f6710f2014-06-09 18:56:43 -0700323 enum { MAX_AXES = 30 }; // 30 so that sizeof(PointerCoords) == 128
Jeff Brown5912f952013-07-01 19:10:31 -0700324
325 // Bitfield of axes that are present in this structure.
Fengwei Yin83e0e422014-05-24 05:32:09 +0800326 uint64_t bits __attribute__((aligned(8)));
Jeff Brown5912f952013-07-01 19:10:31 -0700327
328 // Values of axes that are stored in this structure packed in order by axis id
329 // for each axis that is present in the structure according to 'bits'.
330 float values[MAX_AXES];
331
332 inline void clear() {
Michael Wrightd0bd3912014-03-19 12:06:10 -0700333 BitSet64::clear(bits);
334 }
335
336 bool isEmpty() const {
337 return BitSet64::isEmpty(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700338 }
339
340 float getAxisValue(int32_t axis) const;
341 status_t setAxisValue(int32_t axis, float value);
342
Robert Carre07e1032018-11-26 12:55:53 -0800343 void scale(float globalScale);
344
345 // Scale the pointer coordinates according to a global scale and a
346 // window scale. The global scale will be applied to TOUCH/TOOL_MAJOR/MINOR
347 // axes, however the window scaling will not.
348 void scale(float globalScale, float windowXScale, float windowYScale);
Jeff Browned4d28d2014-02-11 14:28:48 -0800349 void applyOffset(float xOffset, float yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700350
351 inline float getX() const {
352 return getAxisValue(AMOTION_EVENT_AXIS_X);
353 }
354
355 inline float getY() const {
356 return getAxisValue(AMOTION_EVENT_AXIS_Y);
357 }
358
Elliott Hughes6071da72015-08-12 15:27:47 -0700359#ifdef __ANDROID__
Jeff Brown5912f952013-07-01 19:10:31 -0700360 status_t readFromParcel(Parcel* parcel);
361 status_t writeToParcel(Parcel* parcel) const;
362#endif
363
364 bool operator==(const PointerCoords& other) const;
365 inline bool operator!=(const PointerCoords& other) const {
366 return !(*this == other);
367 }
368
369 void copyFrom(const PointerCoords& other);
370
371private:
372 void tooManyAxes(int axis);
373};
374
375/*
376 * Pointer property data.
377 */
378struct PointerProperties {
379 // The id of the pointer.
380 int32_t id;
381
382 // The pointer tool type.
383 int32_t toolType;
384
385 inline void clear() {
386 id = -1;
387 toolType = 0;
388 }
389
390 bool operator==(const PointerProperties& other) const;
391 inline bool operator!=(const PointerProperties& other) const {
392 return !(*this == other);
393 }
394
395 void copyFrom(const PointerProperties& other);
396};
397
398/*
399 * Input events.
400 */
401class InputEvent : public AInputEvent {
402public:
403 virtual ~InputEvent() { }
404
405 virtual int32_t getType() const = 0;
406
Garfield Tanfbe732e2020-01-24 11:26:14 -0800407 inline int32_t getId() const { return mId; }
408
Jeff Brown5912f952013-07-01 19:10:31 -0700409 inline int32_t getDeviceId() const { return mDeviceId; }
410
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600411 inline uint32_t getSource() const { return mSource; }
Jeff Brown5912f952013-07-01 19:10:31 -0700412
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600413 inline void setSource(uint32_t source) { mSource = source; }
Jeff Brown5912f952013-07-01 19:10:31 -0700414
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100415 inline int32_t getDisplayId() const { return mDisplayId; }
416
417 inline void setDisplayId(int32_t displayId) { mDisplayId = displayId; }
418
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600419 inline std::array<uint8_t, 32> getHmac() const { return mHmac; }
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100420
Garfield Tanfbe732e2020-01-24 11:26:14 -0800421 static int32_t nextId();
422
Jeff Brown5912f952013-07-01 19:10:31 -0700423protected:
Garfield Tanfbe732e2020-01-24 11:26:14 -0800424 void initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600425 std::array<uint8_t, 32> hmac);
Garfield Tanfbe732e2020-01-24 11:26:14 -0800426
Jeff Brown5912f952013-07-01 19:10:31 -0700427 void initialize(const InputEvent& from);
428
Garfield Tanfbe732e2020-01-24 11:26:14 -0800429 int32_t mId;
Jeff Brown5912f952013-07-01 19:10:31 -0700430 int32_t mDeviceId;
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600431 uint32_t mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100432 int32_t mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600433 std::array<uint8_t, 32> mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700434};
435
436/*
437 * Key events.
438 */
439class KeyEvent : public InputEvent {
440public:
441 virtual ~KeyEvent() { }
442
443 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_KEY; }
444
445 inline int32_t getAction() const { return mAction; }
446
447 inline int32_t getFlags() const { return mFlags; }
448
449 inline void setFlags(int32_t flags) { mFlags = flags; }
450
451 inline int32_t getKeyCode() const { return mKeyCode; }
452
453 inline int32_t getScanCode() const { return mScanCode; }
454
455 inline int32_t getMetaState() const { return mMetaState; }
456
457 inline int32_t getRepeatCount() const { return mRepeatCount; }
458
459 inline nsecs_t getDownTime() const { return mDownTime; }
460
461 inline nsecs_t getEventTime() const { return mEventTime; }
462
Michael Wright872db4f2014-04-22 15:03:51 -0700463 static const char* getLabel(int32_t keyCode);
464 static int32_t getKeyCodeFromLabel(const char* label);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800465
Garfield Tanfbe732e2020-01-24 11:26:14 -0800466 void initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600467 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags, int32_t keyCode,
468 int32_t scanCode, int32_t metaState, int32_t repeatCount, nsecs_t downTime,
469 nsecs_t eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -0700470 void initialize(const KeyEvent& from);
471
Siarhei Vishniakoue4623042020-03-25 16:16:40 -0700472 static const char* actionToString(int32_t action);
473
Jeff Brown5912f952013-07-01 19:10:31 -0700474protected:
475 int32_t mAction;
476 int32_t mFlags;
477 int32_t mKeyCode;
478 int32_t mScanCode;
479 int32_t mMetaState;
480 int32_t mRepeatCount;
481 nsecs_t mDownTime;
482 nsecs_t mEventTime;
483};
484
485/*
486 * Motion events.
487 */
488class MotionEvent : public InputEvent {
489public:
490 virtual ~MotionEvent() { }
491
492 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_MOTION; }
493
494 inline int32_t getAction() const { return mAction; }
495
496 inline int32_t getActionMasked() const { return mAction & AMOTION_EVENT_ACTION_MASK; }
497
498 inline int32_t getActionIndex() const {
499 return (mAction & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK)
500 >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
501 }
502
503 inline void setAction(int32_t action) { mAction = action; }
504
505 inline int32_t getFlags() const { return mFlags; }
506
507 inline void setFlags(int32_t flags) { mFlags = flags; }
508
509 inline int32_t getEdgeFlags() const { return mEdgeFlags; }
510
511 inline void setEdgeFlags(int32_t edgeFlags) { mEdgeFlags = edgeFlags; }
512
513 inline int32_t getMetaState() const { return mMetaState; }
514
515 inline void setMetaState(int32_t metaState) { mMetaState = metaState; }
516
517 inline int32_t getButtonState() const { return mButtonState; }
518
Michael Wright6db58792016-09-14 19:53:37 +0100519 inline void setButtonState(int32_t buttonState) { mButtonState = buttonState; }
Michael Wright7b159c92015-05-14 14:48:03 +0100520
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800521 inline MotionClassification getClassification() const { return mClassification; }
522
Michael Wright7b159c92015-05-14 14:48:03 +0100523 inline int32_t getActionButton() const { return mActionButton; }
524
Michael Wright21957b92015-06-17 21:06:54 +0100525 inline void setActionButton(int32_t button) { mActionButton = button; }
526
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600527 inline float getXScale() const { return mXScale; }
528
529 inline float getYScale() const { return mYScale; }
530
Jeff Brown5912f952013-07-01 19:10:31 -0700531 inline float getXOffset() const { return mXOffset; }
532
533 inline float getYOffset() const { return mYOffset; }
534
535 inline float getXPrecision() const { return mXPrecision; }
536
537 inline float getYPrecision() const { return mYPrecision; }
538
Garfield Tan937bb832019-07-25 17:48:31 -0700539 inline float getRawXCursorPosition() const { return mRawXCursorPosition; }
Garfield Tan00f511d2019-06-12 16:55:40 -0700540
541 float getXCursorPosition() const;
542
Garfield Tan937bb832019-07-25 17:48:31 -0700543 inline float getRawYCursorPosition() const { return mRawYCursorPosition; }
Garfield Tan00f511d2019-06-12 16:55:40 -0700544
545 float getYCursorPosition() const;
546
Garfield Tan937bb832019-07-25 17:48:31 -0700547 void setCursorPosition(float x, float y);
548
Garfield Tanab0ab9c2019-07-10 18:58:28 -0700549 static inline bool isValidCursorPosition(float x, float y) { return !isnan(x) && !isnan(y); }
550
Jeff Brown5912f952013-07-01 19:10:31 -0700551 inline nsecs_t getDownTime() const { return mDownTime; }
552
553 inline void setDownTime(nsecs_t downTime) { mDownTime = downTime; }
554
555 inline size_t getPointerCount() const { return mPointerProperties.size(); }
556
557 inline const PointerProperties* getPointerProperties(size_t pointerIndex) const {
558 return &mPointerProperties[pointerIndex];
559 }
560
561 inline int32_t getPointerId(size_t pointerIndex) const {
562 return mPointerProperties[pointerIndex].id;
563 }
564
565 inline int32_t getToolType(size_t pointerIndex) const {
566 return mPointerProperties[pointerIndex].toolType;
567 }
568
569 inline nsecs_t getEventTime() const { return mSampleEventTimes[getHistorySize()]; }
570
571 const PointerCoords* getRawPointerCoords(size_t pointerIndex) const;
572
573 float getRawAxisValue(int32_t axis, size_t pointerIndex) const;
574
575 inline float getRawX(size_t pointerIndex) const {
576 return getRawAxisValue(AMOTION_EVENT_AXIS_X, pointerIndex);
577 }
578
579 inline float getRawY(size_t pointerIndex) const {
580 return getRawAxisValue(AMOTION_EVENT_AXIS_Y, pointerIndex);
581 }
582
583 float getAxisValue(int32_t axis, size_t pointerIndex) const;
584
585 inline float getX(size_t pointerIndex) const {
586 return getAxisValue(AMOTION_EVENT_AXIS_X, pointerIndex);
587 }
588
589 inline float getY(size_t pointerIndex) const {
590 return getAxisValue(AMOTION_EVENT_AXIS_Y, pointerIndex);
591 }
592
593 inline float getPressure(size_t pointerIndex) const {
594 return getAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pointerIndex);
595 }
596
597 inline float getSize(size_t pointerIndex) const {
598 return getAxisValue(AMOTION_EVENT_AXIS_SIZE, pointerIndex);
599 }
600
601 inline float getTouchMajor(size_t pointerIndex) const {
602 return getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, pointerIndex);
603 }
604
605 inline float getTouchMinor(size_t pointerIndex) const {
606 return getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, pointerIndex);
607 }
608
609 inline float getToolMajor(size_t pointerIndex) const {
610 return getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, pointerIndex);
611 }
612
613 inline float getToolMinor(size_t pointerIndex) const {
614 return getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, pointerIndex);
615 }
616
617 inline float getOrientation(size_t pointerIndex) const {
618 return getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, pointerIndex);
619 }
620
621 inline size_t getHistorySize() const { return mSampleEventTimes.size() - 1; }
622
623 inline nsecs_t getHistoricalEventTime(size_t historicalIndex) const {
624 return mSampleEventTimes[historicalIndex];
625 }
626
627 const PointerCoords* getHistoricalRawPointerCoords(
628 size_t pointerIndex, size_t historicalIndex) const;
629
630 float getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
631 size_t historicalIndex) const;
632
633 inline float getHistoricalRawX(size_t pointerIndex, size_t historicalIndex) const {
634 return getHistoricalRawAxisValue(
635 AMOTION_EVENT_AXIS_X, pointerIndex, historicalIndex);
636 }
637
638 inline float getHistoricalRawY(size_t pointerIndex, size_t historicalIndex) const {
639 return getHistoricalRawAxisValue(
640 AMOTION_EVENT_AXIS_Y, pointerIndex, historicalIndex);
641 }
642
643 float getHistoricalAxisValue(int32_t axis, size_t pointerIndex, size_t historicalIndex) const;
644
645 inline float getHistoricalX(size_t pointerIndex, size_t historicalIndex) const {
646 return getHistoricalAxisValue(
647 AMOTION_EVENT_AXIS_X, pointerIndex, historicalIndex);
648 }
649
650 inline float getHistoricalY(size_t pointerIndex, size_t historicalIndex) const {
651 return getHistoricalAxisValue(
652 AMOTION_EVENT_AXIS_Y, pointerIndex, historicalIndex);
653 }
654
655 inline float getHistoricalPressure(size_t pointerIndex, size_t historicalIndex) const {
656 return getHistoricalAxisValue(
657 AMOTION_EVENT_AXIS_PRESSURE, pointerIndex, historicalIndex);
658 }
659
660 inline float getHistoricalSize(size_t pointerIndex, size_t historicalIndex) const {
661 return getHistoricalAxisValue(
662 AMOTION_EVENT_AXIS_SIZE, pointerIndex, historicalIndex);
663 }
664
665 inline float getHistoricalTouchMajor(size_t pointerIndex, size_t historicalIndex) const {
666 return getHistoricalAxisValue(
667 AMOTION_EVENT_AXIS_TOUCH_MAJOR, pointerIndex, historicalIndex);
668 }
669
670 inline float getHistoricalTouchMinor(size_t pointerIndex, size_t historicalIndex) const {
671 return getHistoricalAxisValue(
672 AMOTION_EVENT_AXIS_TOUCH_MINOR, pointerIndex, historicalIndex);
673 }
674
675 inline float getHistoricalToolMajor(size_t pointerIndex, size_t historicalIndex) const {
676 return getHistoricalAxisValue(
677 AMOTION_EVENT_AXIS_TOOL_MAJOR, pointerIndex, historicalIndex);
678 }
679
680 inline float getHistoricalToolMinor(size_t pointerIndex, size_t historicalIndex) const {
681 return getHistoricalAxisValue(
682 AMOTION_EVENT_AXIS_TOOL_MINOR, pointerIndex, historicalIndex);
683 }
684
685 inline float getHistoricalOrientation(size_t pointerIndex, size_t historicalIndex) const {
686 return getHistoricalAxisValue(
687 AMOTION_EVENT_AXIS_ORIENTATION, pointerIndex, historicalIndex);
688 }
689
690 ssize_t findPointerIndex(int32_t pointerId) const;
691
Garfield Tanfbe732e2020-01-24 11:26:14 -0800692 void initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600693 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
694 int32_t flags, int32_t edgeFlags, int32_t metaState, int32_t buttonState,
695 MotionClassification classification, float xScale, float yScale, float xOffset,
Garfield Tan937bb832019-07-25 17:48:31 -0700696 float yOffset, float xPrecision, float yPrecision, float rawXCursorPosition,
697 float rawYCursorPosition, nsecs_t downTime, nsecs_t eventTime,
Garfield Tan00f511d2019-06-12 16:55:40 -0700698 size_t pointerCount, const PointerProperties* pointerProperties,
699 const PointerCoords* pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -0700700
701 void copyFrom(const MotionEvent* other, bool keepHistory);
702
703 void addSample(
704 nsecs_t eventTime,
705 const PointerCoords* pointerCoords);
706
707 void offsetLocation(float xOffset, float yOffset);
708
Robert Carre07e1032018-11-26 12:55:53 -0800709 void scale(float globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700710
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700711 // Apply 3x3 perspective matrix transformation.
712 // Matrix is in row-major form and compatible with SkMatrix.
713 void transform(const float matrix[9]);
Jeff Brown5912f952013-07-01 19:10:31 -0700714
Elliott Hughes6071da72015-08-12 15:27:47 -0700715#ifdef __ANDROID__
Jeff Brown5912f952013-07-01 19:10:31 -0700716 status_t readFromParcel(Parcel* parcel);
717 status_t writeToParcel(Parcel* parcel) const;
718#endif
719
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600720 static bool isTouchEvent(uint32_t source, int32_t action);
Jeff Brown5912f952013-07-01 19:10:31 -0700721 inline bool isTouchEvent() const {
722 return isTouchEvent(mSource, mAction);
723 }
724
725 // Low-level accessors.
726 inline const PointerProperties* getPointerProperties() const {
727 return mPointerProperties.array();
728 }
729 inline const nsecs_t* getSampleEventTimes() const { return mSampleEventTimes.array(); }
730 inline const PointerCoords* getSamplePointerCoords() const {
731 return mSamplePointerCoords.array();
732 }
733
Michael Wright872db4f2014-04-22 15:03:51 -0700734 static const char* getLabel(int32_t axis);
735 static int32_t getAxisFromLabel(const char* label);
736
Siarhei Vishniakoue4623042020-03-25 16:16:40 -0700737 static const char* actionToString(int32_t action);
738
Jeff Brown5912f952013-07-01 19:10:31 -0700739protected:
740 int32_t mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100741 int32_t mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700742 int32_t mFlags;
743 int32_t mEdgeFlags;
744 int32_t mMetaState;
745 int32_t mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800746 MotionClassification mClassification;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600747 float mXScale;
748 float mYScale;
Jeff Brown5912f952013-07-01 19:10:31 -0700749 float mXOffset;
750 float mYOffset;
751 float mXPrecision;
752 float mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700753 float mRawXCursorPosition;
754 float mRawYCursorPosition;
Jeff Brown5912f952013-07-01 19:10:31 -0700755 nsecs_t mDownTime;
756 Vector<PointerProperties> mPointerProperties;
757 Vector<nsecs_t> mSampleEventTimes;
758 Vector<PointerCoords> mSamplePointerCoords;
759};
760
761/*
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800762 * Focus events.
763 */
764class FocusEvent : public InputEvent {
765public:
766 virtual ~FocusEvent() {}
767
768 virtual int32_t getType() const override { return AINPUT_EVENT_TYPE_FOCUS; }
769
770 inline bool getHasFocus() const { return mHasFocus; }
771
772 inline bool getInTouchMode() const { return mInTouchMode; }
773
Garfield Tanfbe732e2020-01-24 11:26:14 -0800774 void initialize(int32_t id, bool hasFocus, bool inTouchMode);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800775
776 void initialize(const FocusEvent& from);
777
778protected:
779 bool mHasFocus;
780 bool mInTouchMode;
781};
782
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800783/**
784 * Base class for verified events.
785 * Do not create a VerifiedInputEvent explicitly.
786 * Use helper functions to create them from InputEvents.
787 */
788struct __attribute__((__packed__)) VerifiedInputEvent {
789 enum class Type : int32_t {
790 KEY = AINPUT_EVENT_TYPE_KEY,
791 MOTION = AINPUT_EVENT_TYPE_MOTION,
792 };
793
794 Type type;
795 int32_t deviceId;
796 nsecs_t eventTimeNanos;
797 uint32_t source;
798 int32_t displayId;
799};
800
801/**
802 * Same as KeyEvent, but only contains the data that can be verified.
803 * If you update this class, you must also update VerifiedKeyEvent.java
804 */
805struct __attribute__((__packed__)) VerifiedKeyEvent : public VerifiedInputEvent {
806 int32_t action;
807 nsecs_t downTimeNanos;
808 int32_t flags;
809 int32_t keyCode;
810 int32_t scanCode;
811 int32_t metaState;
812 int32_t repeatCount;
813};
814
815/**
816 * Same as MotionEvent, but only contains the data that can be verified.
817 * If you update this class, you must also update VerifiedMotionEvent.java
818 */
819struct __attribute__((__packed__)) VerifiedMotionEvent : public VerifiedInputEvent {
820 float rawX;
821 float rawY;
822 int32_t actionMasked;
823 nsecs_t downTimeNanos;
824 int32_t flags;
825 int32_t metaState;
826 int32_t buttonState;
827};
828
829VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event);
830VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event);
831
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800832/*
Jeff Brown5912f952013-07-01 19:10:31 -0700833 * Input event factory.
834 */
835class InputEventFactoryInterface {
836protected:
837 virtual ~InputEventFactoryInterface() { }
838
839public:
840 InputEventFactoryInterface() { }
841
842 virtual KeyEvent* createKeyEvent() = 0;
843 virtual MotionEvent* createMotionEvent() = 0;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800844 virtual FocusEvent* createFocusEvent() = 0;
Jeff Brown5912f952013-07-01 19:10:31 -0700845};
846
847/*
848 * A simple input event factory implementation that uses a single preallocated instance
849 * of each type of input event that are reused for each request.
850 */
851class PreallocatedInputEventFactory : public InputEventFactoryInterface {
852public:
853 PreallocatedInputEventFactory() { }
854 virtual ~PreallocatedInputEventFactory() { }
855
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800856 virtual KeyEvent* createKeyEvent() override { return &mKeyEvent; }
857 virtual MotionEvent* createMotionEvent() override { return &mMotionEvent; }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800858 virtual FocusEvent* createFocusEvent() override { return &mFocusEvent; }
Jeff Brown5912f952013-07-01 19:10:31 -0700859
860private:
861 KeyEvent mKeyEvent;
862 MotionEvent mMotionEvent;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800863 FocusEvent mFocusEvent;
Jeff Brown5912f952013-07-01 19:10:31 -0700864};
865
866/*
867 * An input event factory implementation that maintains a pool of input events.
868 */
869class PooledInputEventFactory : public InputEventFactoryInterface {
870public:
Chih-Hung Hsiehf43b02c2018-12-20 15:45:56 -0800871 explicit PooledInputEventFactory(size_t maxPoolSize = 20);
Jeff Brown5912f952013-07-01 19:10:31 -0700872 virtual ~PooledInputEventFactory();
873
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800874 virtual KeyEvent* createKeyEvent() override;
875 virtual MotionEvent* createMotionEvent() override;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800876 virtual FocusEvent* createFocusEvent() override;
Jeff Brown5912f952013-07-01 19:10:31 -0700877
878 void recycle(InputEvent* event);
879
880private:
881 const size_t mMaxPoolSize;
882
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800883 std::queue<std::unique_ptr<KeyEvent>> mKeyEventPool;
884 std::queue<std::unique_ptr<MotionEvent>> mMotionEventPool;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800885 std::queue<std::unique_ptr<FocusEvent>> mFocusEventPool;
Jeff Brown5912f952013-07-01 19:10:31 -0700886};
887
888} // namespace android
889
890#endif // _LIBINPUT_INPUT_H