blob: e7d68fc3490884d982a6c114798a0765619e9eff [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>
Siarhei Vishniakou509724f2021-06-16 04:09:35 +000027#ifdef __linux__
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +000028#include <android/os/IInputConstants.h>
Siarhei Vishniakou509724f2021-06-16 04:09:35 +000029#endif
Garfield Tanab0ab9c2019-07-10 18:58:28 -070030#include <math.h>
Garfield Tan00f511d2019-06-12 16:55:40 -070031#include <stdint.h>
chaviwfd9c1ed2020-07-01 10:57:59 -070032#include <ui/Transform.h>
Michael Wrightd0bd3912014-03-19 12:06:10 -070033#include <utils/BitSet.h>
Jeff Brown5912f952013-07-01 19:10:31 -070034#include <utils/RefBase.h>
Michael Wrightd0bd3912014-03-19 12:06:10 -070035#include <utils/Timers.h>
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -060036#include <array>
Garfield Tan00f511d2019-06-12 16:55:40 -070037#include <limits>
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -080038#include <queue>
Jeff Brown5912f952013-07-01 19:10:31 -070039
Jeff Brown5912f952013-07-01 19:10:31 -070040/*
41 * Additional private constants not defined in ndk/ui/input.h.
42 */
43enum {
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +000044#ifdef __linux__
45 /* This event was generated or modified by accessibility service. */
46 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT =
47 android::os::IInputConstants::INPUT_EVENT_FLAG_IS_ACCESSIBILITY_EVENT, // 0x800,
48#else
Diego Perezcf43a952021-07-02 12:13:41 +010049 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT = 0x800,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +000050#endif
Jeff Brown5912f952013-07-01 19:10:31 -070051 /* Signifies that the key is being predispatched */
52 AKEY_EVENT_FLAG_PREDISPATCH = 0x20000000,
53
54 /* Private control to determine when an app is tracking a key sequence. */
55 AKEY_EVENT_FLAG_START_TRACKING = 0x40000000,
56
57 /* Key event is inconsistent with previously sent key events. */
58 AKEY_EVENT_FLAG_TAINTED = 0x80000000,
59};
60
61enum {
Michael Wrightcdcd8f22016-03-22 16:52:13 -070062
63 /**
64 * This flag indicates that the window that received this motion event is partly
65 * or wholly obscured by another visible window above it. This flag is set to true
66 * even if the event did not directly pass through the obscured area.
67 * A security sensitive application can check this flag to identify situations in which
68 * a malicious application may have covered up part of its content for the purpose
69 * of misleading the user or hijacking touches. An appropriate response might be
70 * to drop the suspect touches or to take additional precautions to confirm the user's
71 * actual intent.
72 */
73 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED = 0x2,
74
Prabir Pradhan141a9862018-11-19 14:35:56 -080075 /**
76 * This flag indicates that the event has been generated by a gesture generator. It
77 * provides a hint to the GestureDetector to not apply any touch slop.
78 */
79 AMOTION_EVENT_FLAG_IS_GENERATED_GESTURE = 0x8,
80
Prabir Pradhan47cf0a02021-03-11 20:30:57 -080081 /**
82 * This flag indicates that the event will not cause a focus change if it is directed to an
83 * unfocused window, even if it an ACTION_DOWN. This is typically used with pointer
84 * gestures to allow the user to direct gestures to an unfocused window without bringing it
85 * into focus.
86 */
87 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE = 0x40,
88
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -080089#if defined(__linux__)
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +000090 /**
91 * This event was generated or modified by accessibility service.
92 */
93 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT =
94 android::os::IInputConstants::INPUT_EVENT_FLAG_IS_ACCESSIBILITY_EVENT, // 0x800,
95#else
Diego Perezba0ce7d2021-07-06 12:35:16 +010096 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT = 0x800,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +000097#endif
98
Jeff Brown5912f952013-07-01 19:10:31 -070099 /* Motion event is inconsistent with previously sent motion events. */
100 AMOTION_EVENT_FLAG_TAINTED = 0x80000000,
101};
102
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800103/**
104 * Allowed VerifiedKeyEvent flags. All other flags from KeyEvent do not get verified.
105 * These values must be kept in sync with VerifiedKeyEvent.java
106 */
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +0000107constexpr int32_t VERIFIED_KEY_EVENT_FLAGS =
108 AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT;
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800109
110/**
111 * Allowed VerifiedMotionEventFlags. All other flags from MotionEvent do not get verified.
112 * These values must be kept in sync with VerifiedMotionEvent.java
113 */
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +0000114constexpr int32_t VERIFIED_MOTION_EVENT_FLAGS = AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED |
115 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED | AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT;
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800116
arthurhungcc7f9802020-04-30 17:55:40 +0800117/**
118 * This flag indicates that the point up event has been canceled.
119 * Typically this is used for palm event when the user has accidental touches.
120 * TODO: Adjust flag to public api
121 */
122constexpr int32_t AMOTION_EVENT_FLAG_CANCELED = 0x20;
123
Jeff Brown5912f952013-07-01 19:10:31 -0700124enum {
Jeff Brown5912f952013-07-01 19:10:31 -0700125 /*
126 * Indicates that an input device has switches.
127 * This input source flag is hidden from the API because switches are only used by the system
128 * and applications have no way to interact with them.
129 */
130 AINPUT_SOURCE_SWITCH = 0x80000000,
131};
132
Michael Wright962a1082013-10-17 17:35:53 -0700133enum {
134 /**
135 * Constants for LEDs. Hidden from the API since we don't actually expose a way to interact
136 * with LEDs to developers
137 *
Michael Wright872db4f2014-04-22 15:03:51 -0700138 * NOTE: If you add LEDs here, you must also add them to InputEventLabels.h
Michael Wright962a1082013-10-17 17:35:53 -0700139 */
140
141 ALED_NUM_LOCK = 0x00,
142 ALED_CAPS_LOCK = 0x01,
143 ALED_SCROLL_LOCK = 0x02,
144 ALED_COMPOSE = 0x03,
145 ALED_KANA = 0x04,
146 ALED_SLEEP = 0x05,
147 ALED_SUSPEND = 0x06,
148 ALED_MUTE = 0x07,
149 ALED_MISC = 0x08,
150 ALED_MAIL = 0x09,
151 ALED_CHARGING = 0x0a,
152 ALED_CONTROLLER_1 = 0x10,
153 ALED_CONTROLLER_2 = 0x11,
154 ALED_CONTROLLER_3 = 0x12,
155 ALED_CONTROLLER_4 = 0x13,
156};
157
Michael Wright9b04f862013-10-18 17:53:50 -0700158/* Maximum number of controller LEDs we support */
159#define MAX_CONTROLLER_LEDS 4
160
Jeff Brown5912f952013-07-01 19:10:31 -0700161/*
Jeff Brown5912f952013-07-01 19:10:31 -0700162 * Maximum number of pointers supported per motion event.
163 * Smallest number of pointers is 1.
164 * (We want at least 10 but some touch controllers obstensibly configured for 10 pointers
165 * will occasionally emit 11. There is not much harm making this constant bigger.)
166 */
Siarhei Vishniakou01747382022-01-20 13:23:27 -0800167static constexpr size_t MAX_POINTERS = 16;
Jeff Brown5912f952013-07-01 19:10:31 -0700168
169/*
Flanker552a8a52015-09-07 15:28:58 +0800170 * Maximum number of samples supported per motion event.
171 */
172#define MAX_SAMPLES UINT16_MAX
173
174/*
Jeff Brown5912f952013-07-01 19:10:31 -0700175 * Maximum pointer id value supported in a motion event.
176 * Smallest pointer id is 0.
177 * (This is limited by our use of BitSet32 to track pointer assignments.)
178 */
179#define MAX_POINTER_ID 31
180
181/*
182 * Declare a concrete type for the NDK's input event forward declaration.
183 */
184struct AInputEvent {
185 virtual ~AInputEvent() { }
186};
187
188/*
189 * Declare a concrete type for the NDK's input device forward declaration.
190 */
191struct AInputDevice {
192 virtual ~AInputDevice() { }
193};
194
195
196namespace android {
197
Brett Chabotfaa986c2020-11-04 17:39:36 -0800198#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -0700199class Parcel;
Brett Chabotfaa986c2020-11-04 17:39:36 -0800200#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700201
Prabir Pradhande69f8a2021-11-18 16:40:34 +0000202/*
203 * Apply the given transform to the point without applying any translation/offset.
204 */
205vec2 transformWithoutTranslation(const ui::Transform& transform, const vec2& xy);
206
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800207const char* inputEventTypeToString(int32_t type);
208
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800209std::string inputEventSourceToString(int32_t source);
210
211bool isFromSource(uint32_t source, uint32_t test);
212
Jeff Brown5912f952013-07-01 19:10:31 -0700213/*
214 * Flags that flow alongside events in the input dispatch system to help with certain
215 * policy decisions such as waking from device sleep.
216 *
217 * These flags are also defined in frameworks/base/core/java/android/view/WindowManagerPolicy.java.
218 */
219enum {
220 /* These flags originate in RawEvents and are generally set in the key map.
Michael Wright872db4f2014-04-22 15:03:51 -0700221 * NOTE: If you want a flag to be able to set in a keylayout file, then you must add it to
222 * InputEventLabels.h as well. */
Jeff Brown5912f952013-07-01 19:10:31 -0700223
Jeff Brownc9aa6282015-02-11 19:03:28 -0800224 // Indicates that the event should wake the device.
Jeff Brown5912f952013-07-01 19:10:31 -0700225 POLICY_FLAG_WAKE = 0x00000001,
Jeff Brownc9aa6282015-02-11 19:03:28 -0800226
227 // Indicates that the key is virtual, such as a capacitive button, and should
228 // generate haptic feedback. Virtual keys may be suppressed for some time
229 // after a recent touch to prevent accidental activation of virtual keys adjacent
230 // to the touch screen during an edge swipe.
Michael Wright872db4f2014-04-22 15:03:51 -0700231 POLICY_FLAG_VIRTUAL = 0x00000002,
Jeff Brownc9aa6282015-02-11 19:03:28 -0800232
233 // Indicates that the key is the special function modifier.
Michael Wright872db4f2014-04-22 15:03:51 -0700234 POLICY_FLAG_FUNCTION = 0x00000004,
Jeff Brown5912f952013-07-01 19:10:31 -0700235
Jeff Brownc9aa6282015-02-11 19:03:28 -0800236 // Indicates that the key represents a special gesture that has been detected by
237 // the touch firmware or driver. Causes touch events from the same device to be canceled.
238 POLICY_FLAG_GESTURE = 0x00000008,
239
Jeff Brown5912f952013-07-01 19:10:31 -0700240 POLICY_FLAG_RAW_MASK = 0x0000ffff,
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +0000241
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +0000242#ifdef __linux__
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +0000243 POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY =
244 android::os::IInputConstants::POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Siarhei Vishniakou509724f2021-06-16 04:09:35 +0000245#else
Siarhei Vishniakou509724f2021-06-16 04:09:35 +0000246 POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY = 0x20000,
247#endif
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +0000248
Jeff Brown5912f952013-07-01 19:10:31 -0700249 /* These flags are set by the input dispatcher. */
250
251 // Indicates that the input event was injected.
252 POLICY_FLAG_INJECTED = 0x01000000,
253
254 // Indicates that the input event is from a trusted source such as a directly attached
255 // input device or an application with system-wide event injection permission.
256 POLICY_FLAG_TRUSTED = 0x02000000,
257
258 // Indicates that the input event has passed through an input filter.
259 POLICY_FLAG_FILTERED = 0x04000000,
260
261 // Disables automatic key repeating behavior.
262 POLICY_FLAG_DISABLE_KEY_REPEAT = 0x08000000,
263
264 /* These flags are set by the input reader policy as it intercepts each event. */
265
Jeff Browndb19e462014-04-08 19:55:38 -0700266 // Indicates that the device was in an interactive state when the
267 // event was intercepted.
268 POLICY_FLAG_INTERACTIVE = 0x20000000,
Jeff Brown5912f952013-07-01 19:10:31 -0700269
270 // Indicates that the event should be dispatched to applications.
271 // The input event should still be sent to the InputDispatcher so that it can see all
272 // input events received include those that it will not deliver.
273 POLICY_FLAG_PASS_TO_USER = 0x40000000,
274};
275
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800276/**
277 * Classifications of the current gesture, if available.
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800278 */
279enum class MotionClassification : uint8_t {
280 /**
281 * No classification is available.
282 */
Vaibhav8576dd72022-02-11 18:19:06 +0530283 NONE = AMOTION_EVENT_CLASSIFICATION_NONE,
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800284 /**
285 * Too early to classify the current gesture. Need more events. Look for changes in the
286 * upcoming motion events.
287 */
Vaibhav8576dd72022-02-11 18:19:06 +0530288 AMBIGUOUS_GESTURE = AMOTION_EVENT_CLASSIFICATION_AMBIGUOUS_GESTURE,
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800289 /**
290 * The current gesture likely represents a user intentionally exerting force on the touchscreen.
291 */
Vaibhav8576dd72022-02-11 18:19:06 +0530292 DEEP_PRESS = AMOTION_EVENT_CLASSIFICATION_DEEP_PRESS,
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800293};
294
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800295/**
296 * String representation of MotionClassification
297 */
298const char* motionClassificationToString(MotionClassification classification);
299
Garfield Tan00f511d2019-06-12 16:55:40 -0700300/**
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000301 * Portion of FrameMetrics timeline of interest to input code.
302 */
303enum GraphicsTimeline : size_t {
304 /** Time when the app sent the buffer to SurfaceFlinger. */
305 GPU_COMPLETED_TIME = 0,
306
307 /** Time when the frame was presented on the display */
308 PRESENT_TIME = 1,
309
310 /** Total size of the 'GraphicsTimeline' array. Must always be last. */
311 SIZE = 2
312};
313
314/**
Garfield Tan84b087e2020-01-23 10:49:05 -0800315 * Generator of unique numbers used to identify input events.
316 *
317 * Layout of ID:
318 * |--------------------------|---------------------------|
319 * | 2 bits for source | 30 bits for random number |
320 * |--------------------------|---------------------------|
321 */
322class IdGenerator {
323private:
324 static constexpr uint32_t SOURCE_SHIFT = 30;
325
326public:
327 // Used to divide integer space to ensure no conflict among these sources./
328 enum class Source : int32_t {
Garfield Tan750c7f42020-05-18 17:41:46 -0700329 INPUT_READER = static_cast<int32_t>(0x0u << SOURCE_SHIFT),
330 INPUT_DISPATCHER = static_cast<int32_t>(0x1u << SOURCE_SHIFT),
331 OTHER = static_cast<int32_t>(0x3u << SOURCE_SHIFT), // E.g. app injected events
Garfield Tan84b087e2020-01-23 10:49:05 -0800332 };
333 IdGenerator(Source source);
334
335 int32_t nextId() const;
336
337 // Extract source from given id.
338 static inline Source getSource(int32_t id) { return static_cast<Source>(SOURCE_MASK & id); }
339
340private:
341 const Source mSource;
342
Garfield Tan750c7f42020-05-18 17:41:46 -0700343 static constexpr int32_t SOURCE_MASK = static_cast<int32_t>(0x3u << SOURCE_SHIFT);
Garfield Tan84b087e2020-01-23 10:49:05 -0800344};
345
346/**
Garfield Tan00f511d2019-06-12 16:55:40 -0700347 * Invalid value for cursor position. Used for non-mouse events, tests and injected events. Don't
348 * use it for direct comparison with any other value, because NaN isn't equal to itself according to
349 * IEEE 754. Use isnan() instead to check if a cursor position is valid.
350 */
351constexpr float AMOTION_EVENT_INVALID_CURSOR_POSITION = std::numeric_limits<float>::quiet_NaN();
352
Jeff Brown5912f952013-07-01 19:10:31 -0700353/*
354 * Pointer coordinate data.
355 */
356struct PointerCoords {
Michael Wright8f6710f2014-06-09 18:56:43 -0700357 enum { MAX_AXES = 30 }; // 30 so that sizeof(PointerCoords) == 128
Jeff Brown5912f952013-07-01 19:10:31 -0700358
359 // Bitfield of axes that are present in this structure.
Fengwei Yin83e0e422014-05-24 05:32:09 +0800360 uint64_t bits __attribute__((aligned(8)));
Jeff Brown5912f952013-07-01 19:10:31 -0700361
362 // Values of axes that are stored in this structure packed in order by axis id
363 // for each axis that is present in the structure according to 'bits'.
364 float values[MAX_AXES];
365
366 inline void clear() {
Michael Wrightd0bd3912014-03-19 12:06:10 -0700367 BitSet64::clear(bits);
368 }
369
370 bool isEmpty() const {
371 return BitSet64::isEmpty(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700372 }
373
374 float getAxisValue(int32_t axis) const;
375 status_t setAxisValue(int32_t axis, float value);
376
Robert Carre07e1032018-11-26 12:55:53 -0800377 // Scale the pointer coordinates according to a global scale and a
378 // window scale. The global scale will be applied to TOUCH/TOOL_MAJOR/MINOR
379 // axes, however the window scaling will not.
380 void scale(float globalScale, float windowXScale, float windowYScale);
Jeff Brown5912f952013-07-01 19:10:31 -0700381
chaviwc01e1372020-07-01 12:37:31 -0700382 void transform(const ui::Transform& transform);
383
Jeff Brown5912f952013-07-01 19:10:31 -0700384 inline float getX() const {
385 return getAxisValue(AMOTION_EVENT_AXIS_X);
386 }
387
388 inline float getY() const {
389 return getAxisValue(AMOTION_EVENT_AXIS_Y);
390 }
391
Evan Rosky84f07f02021-04-16 10:42:42 -0700392 vec2 getXYValue() const { return vec2(getX(), getY()); }
393
Brett Chabotfaa986c2020-11-04 17:39:36 -0800394#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -0700395 status_t readFromParcel(Parcel* parcel);
396 status_t writeToParcel(Parcel* parcel) const;
Brett Chabotfaa986c2020-11-04 17:39:36 -0800397#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700398
399 bool operator==(const PointerCoords& other) const;
400 inline bool operator!=(const PointerCoords& other) const {
401 return !(*this == other);
402 }
403
404 void copyFrom(const PointerCoords& other);
405
406private:
407 void tooManyAxes(int axis);
408};
409
410/*
411 * Pointer property data.
412 */
413struct PointerProperties {
414 // The id of the pointer.
415 int32_t id;
416
417 // The pointer tool type.
418 int32_t toolType;
419
420 inline void clear() {
421 id = -1;
422 toolType = 0;
423 }
424
425 bool operator==(const PointerProperties& other) const;
426 inline bool operator!=(const PointerProperties& other) const {
427 return !(*this == other);
428 }
429
430 void copyFrom(const PointerProperties& other);
431};
432
433/*
434 * Input events.
435 */
436class InputEvent : public AInputEvent {
437public:
438 virtual ~InputEvent() { }
439
440 virtual int32_t getType() const = 0;
441
Garfield Tan4cc839f2020-01-24 11:26:14 -0800442 inline int32_t getId() const { return mId; }
443
Jeff Brown5912f952013-07-01 19:10:31 -0700444 inline int32_t getDeviceId() const { return mDeviceId; }
445
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600446 inline uint32_t getSource() const { return mSource; }
Jeff Brown5912f952013-07-01 19:10:31 -0700447
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600448 inline void setSource(uint32_t source) { mSource = source; }
Jeff Brown5912f952013-07-01 19:10:31 -0700449
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100450 inline int32_t getDisplayId() const { return mDisplayId; }
451
452 inline void setDisplayId(int32_t displayId) { mDisplayId = displayId; }
453
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600454 inline std::array<uint8_t, 32> getHmac() const { return mHmac; }
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100455
Garfield Tan4cc839f2020-01-24 11:26:14 -0800456 static int32_t nextId();
457
Jeff Brown5912f952013-07-01 19:10:31 -0700458protected:
Garfield Tan4cc839f2020-01-24 11:26:14 -0800459 void initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600460 std::array<uint8_t, 32> hmac);
Garfield Tan4cc839f2020-01-24 11:26:14 -0800461
Jeff Brown5912f952013-07-01 19:10:31 -0700462 void initialize(const InputEvent& from);
463
Garfield Tan4cc839f2020-01-24 11:26:14 -0800464 int32_t mId;
Jeff Brown5912f952013-07-01 19:10:31 -0700465 int32_t mDeviceId;
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600466 uint32_t mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100467 int32_t mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600468 std::array<uint8_t, 32> mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700469};
470
471/*
472 * Key events.
473 */
474class KeyEvent : public InputEvent {
475public:
476 virtual ~KeyEvent() { }
477
478 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_KEY; }
479
480 inline int32_t getAction() const { return mAction; }
481
482 inline int32_t getFlags() const { return mFlags; }
483
484 inline void setFlags(int32_t flags) { mFlags = flags; }
485
486 inline int32_t getKeyCode() const { return mKeyCode; }
487
488 inline int32_t getScanCode() const { return mScanCode; }
489
490 inline int32_t getMetaState() const { return mMetaState; }
491
492 inline int32_t getRepeatCount() const { return mRepeatCount; }
493
494 inline nsecs_t getDownTime() const { return mDownTime; }
495
496 inline nsecs_t getEventTime() const { return mEventTime; }
497
Michael Wright872db4f2014-04-22 15:03:51 -0700498 static const char* getLabel(int32_t keyCode);
499 static int32_t getKeyCodeFromLabel(const char* label);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800500
Garfield Tan4cc839f2020-01-24 11:26:14 -0800501 void initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600502 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags, int32_t keyCode,
503 int32_t scanCode, int32_t metaState, int32_t repeatCount, nsecs_t downTime,
504 nsecs_t eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -0700505 void initialize(const KeyEvent& from);
506
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700507 static const char* actionToString(int32_t action);
508
Jeff Brown5912f952013-07-01 19:10:31 -0700509protected:
510 int32_t mAction;
511 int32_t mFlags;
512 int32_t mKeyCode;
513 int32_t mScanCode;
514 int32_t mMetaState;
515 int32_t mRepeatCount;
516 nsecs_t mDownTime;
517 nsecs_t mEventTime;
518};
519
520/*
521 * Motion events.
522 */
523class MotionEvent : public InputEvent {
524public:
525 virtual ~MotionEvent() { }
526
527 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_MOTION; }
528
529 inline int32_t getAction() const { return mAction; }
530
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000531 static int32_t getActionMasked(int32_t action) { return action & AMOTION_EVENT_ACTION_MASK; }
Jeff Brown5912f952013-07-01 19:10:31 -0700532
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000533 inline int32_t getActionMasked() const { return getActionMasked(mAction); }
534
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700535 static uint8_t getActionIndex(int32_t action) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000536 return (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >>
537 AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
Jeff Brown5912f952013-07-01 19:10:31 -0700538 }
539
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000540 inline int32_t getActionIndex() const { return getActionIndex(mAction); }
541
Jeff Brown5912f952013-07-01 19:10:31 -0700542 inline void setAction(int32_t action) { mAction = action; }
543
544 inline int32_t getFlags() const { return mFlags; }
545
546 inline void setFlags(int32_t flags) { mFlags = flags; }
547
548 inline int32_t getEdgeFlags() const { return mEdgeFlags; }
549
550 inline void setEdgeFlags(int32_t edgeFlags) { mEdgeFlags = edgeFlags; }
551
552 inline int32_t getMetaState() const { return mMetaState; }
553
554 inline void setMetaState(int32_t metaState) { mMetaState = metaState; }
555
556 inline int32_t getButtonState() const { return mButtonState; }
557
Michael Wright6db58792016-09-14 19:53:37 +0100558 inline void setButtonState(int32_t buttonState) { mButtonState = buttonState; }
Michael Wright7b159c92015-05-14 14:48:03 +0100559
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800560 inline MotionClassification getClassification() const { return mClassification; }
561
Michael Wright7b159c92015-05-14 14:48:03 +0100562 inline int32_t getActionButton() const { return mActionButton; }
563
Michael Wright21957b92015-06-17 21:06:54 +0100564 inline void setActionButton(int32_t button) { mActionButton = button; }
565
chaviw9eaa22c2020-07-01 16:21:27 -0700566 inline float getXOffset() const { return mTransform.tx(); }
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600567
chaviw9eaa22c2020-07-01 16:21:27 -0700568 inline float getYOffset() const { return mTransform.ty(); }
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600569
Prabir Pradhan5beda762021-12-10 09:30:08 +0000570 inline const ui::Transform& getTransform() const { return mTransform; }
Jeff Brown5912f952013-07-01 19:10:31 -0700571
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800572 int getSurfaceRotation() const;
573
Jeff Brown5912f952013-07-01 19:10:31 -0700574 inline float getXPrecision() const { return mXPrecision; }
575
576 inline float getYPrecision() const { return mYPrecision; }
577
Garfield Tan937bb832019-07-25 17:48:31 -0700578 inline float getRawXCursorPosition() const { return mRawXCursorPosition; }
Garfield Tan00f511d2019-06-12 16:55:40 -0700579
580 float getXCursorPosition() const;
581
Garfield Tan937bb832019-07-25 17:48:31 -0700582 inline float getRawYCursorPosition() const { return mRawYCursorPosition; }
Garfield Tan00f511d2019-06-12 16:55:40 -0700583
584 float getYCursorPosition() const;
585
Garfield Tan937bb832019-07-25 17:48:31 -0700586 void setCursorPosition(float x, float y);
587
Prabir Pradhan5beda762021-12-10 09:30:08 +0000588 inline const ui::Transform& getRawTransform() const { return mRawTransform; }
Evan Rosky84f07f02021-04-16 10:42:42 -0700589
Garfield Tanab0ab9c2019-07-10 18:58:28 -0700590 static inline bool isValidCursorPosition(float x, float y) { return !isnan(x) && !isnan(y); }
591
Jeff Brown5912f952013-07-01 19:10:31 -0700592 inline nsecs_t getDownTime() const { return mDownTime; }
593
594 inline void setDownTime(nsecs_t downTime) { mDownTime = downTime; }
595
596 inline size_t getPointerCount() const { return mPointerProperties.size(); }
597
598 inline const PointerProperties* getPointerProperties(size_t pointerIndex) const {
599 return &mPointerProperties[pointerIndex];
600 }
601
602 inline int32_t getPointerId(size_t pointerIndex) const {
603 return mPointerProperties[pointerIndex].id;
604 }
605
606 inline int32_t getToolType(size_t pointerIndex) const {
607 return mPointerProperties[pointerIndex].toolType;
608 }
609
610 inline nsecs_t getEventTime() const { return mSampleEventTimes[getHistorySize()]; }
611
Evan Rosky84f07f02021-04-16 10:42:42 -0700612 /**
613 * The actual raw pointer coords: whatever comes from the input device without any external
614 * transforms applied.
615 */
Jeff Brown5912f952013-07-01 19:10:31 -0700616 const PointerCoords* getRawPointerCoords(size_t pointerIndex) const;
617
Evan Rosky84f07f02021-04-16 10:42:42 -0700618 /**
619 * This is the raw axis value. However, for X/Y axes, this currently applies a "compat-raw"
620 * transform because many apps (incorrectly) assumed that raw == oriented-screen-space.
621 * "compat raw" is raw coordinates with screen rotation applied.
622 */
Jeff Brown5912f952013-07-01 19:10:31 -0700623 float getRawAxisValue(int32_t axis, size_t pointerIndex) const;
624
625 inline float getRawX(size_t pointerIndex) const {
626 return getRawAxisValue(AMOTION_EVENT_AXIS_X, pointerIndex);
627 }
628
629 inline float getRawY(size_t pointerIndex) const {
630 return getRawAxisValue(AMOTION_EVENT_AXIS_Y, pointerIndex);
631 }
632
633 float getAxisValue(int32_t axis, size_t pointerIndex) const;
634
Siarhei Vishniakou69e4d0f2020-09-14 19:53:29 -0500635 /**
636 * Get the X coordinate of the latest sample in this MotionEvent for pointer 'pointerIndex'.
637 * Identical to calling getHistoricalX(pointerIndex, getHistorySize()).
638 */
Jeff Brown5912f952013-07-01 19:10:31 -0700639 inline float getX(size_t pointerIndex) const {
640 return getAxisValue(AMOTION_EVENT_AXIS_X, pointerIndex);
641 }
642
Siarhei Vishniakou69e4d0f2020-09-14 19:53:29 -0500643 /**
644 * Get the Y coordinate of the latest sample in this MotionEvent for pointer 'pointerIndex'.
645 * Identical to calling getHistoricalX(pointerIndex, getHistorySize()).
646 */
Jeff Brown5912f952013-07-01 19:10:31 -0700647 inline float getY(size_t pointerIndex) const {
648 return getAxisValue(AMOTION_EVENT_AXIS_Y, pointerIndex);
649 }
650
651 inline float getPressure(size_t pointerIndex) const {
652 return getAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pointerIndex);
653 }
654
655 inline float getSize(size_t pointerIndex) const {
656 return getAxisValue(AMOTION_EVENT_AXIS_SIZE, pointerIndex);
657 }
658
659 inline float getTouchMajor(size_t pointerIndex) const {
660 return getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, pointerIndex);
661 }
662
663 inline float getTouchMinor(size_t pointerIndex) const {
664 return getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, pointerIndex);
665 }
666
667 inline float getToolMajor(size_t pointerIndex) const {
668 return getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, pointerIndex);
669 }
670
671 inline float getToolMinor(size_t pointerIndex) const {
672 return getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, pointerIndex);
673 }
674
675 inline float getOrientation(size_t pointerIndex) const {
676 return getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, pointerIndex);
677 }
678
679 inline size_t getHistorySize() const { return mSampleEventTimes.size() - 1; }
680
681 inline nsecs_t getHistoricalEventTime(size_t historicalIndex) const {
682 return mSampleEventTimes[historicalIndex];
683 }
684
Evan Rosky84f07f02021-04-16 10:42:42 -0700685 /**
686 * The actual raw pointer coords: whatever comes from the input device without any external
687 * transforms applied.
688 */
Jeff Brown5912f952013-07-01 19:10:31 -0700689 const PointerCoords* getHistoricalRawPointerCoords(
690 size_t pointerIndex, size_t historicalIndex) const;
691
Evan Rosky84f07f02021-04-16 10:42:42 -0700692 /**
693 * This is the raw axis value. However, for X/Y axes, this currently applies a "compat-raw"
694 * transform because many apps (incorrectly) assumed that raw == oriented-screen-space.
695 * "compat raw" is raw coordinates with screen rotation applied.
696 */
Jeff Brown5912f952013-07-01 19:10:31 -0700697 float getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
698 size_t historicalIndex) const;
699
700 inline float getHistoricalRawX(size_t pointerIndex, size_t historicalIndex) const {
701 return getHistoricalRawAxisValue(
702 AMOTION_EVENT_AXIS_X, pointerIndex, historicalIndex);
703 }
704
705 inline float getHistoricalRawY(size_t pointerIndex, size_t historicalIndex) const {
706 return getHistoricalRawAxisValue(
707 AMOTION_EVENT_AXIS_Y, pointerIndex, historicalIndex);
708 }
709
710 float getHistoricalAxisValue(int32_t axis, size_t pointerIndex, size_t historicalIndex) const;
711
712 inline float getHistoricalX(size_t pointerIndex, size_t historicalIndex) const {
713 return getHistoricalAxisValue(
714 AMOTION_EVENT_AXIS_X, pointerIndex, historicalIndex);
715 }
716
717 inline float getHistoricalY(size_t pointerIndex, size_t historicalIndex) const {
718 return getHistoricalAxisValue(
719 AMOTION_EVENT_AXIS_Y, pointerIndex, historicalIndex);
720 }
721
722 inline float getHistoricalPressure(size_t pointerIndex, size_t historicalIndex) const {
723 return getHistoricalAxisValue(
724 AMOTION_EVENT_AXIS_PRESSURE, pointerIndex, historicalIndex);
725 }
726
727 inline float getHistoricalSize(size_t pointerIndex, size_t historicalIndex) const {
728 return getHistoricalAxisValue(
729 AMOTION_EVENT_AXIS_SIZE, pointerIndex, historicalIndex);
730 }
731
732 inline float getHistoricalTouchMajor(size_t pointerIndex, size_t historicalIndex) const {
733 return getHistoricalAxisValue(
734 AMOTION_EVENT_AXIS_TOUCH_MAJOR, pointerIndex, historicalIndex);
735 }
736
737 inline float getHistoricalTouchMinor(size_t pointerIndex, size_t historicalIndex) const {
738 return getHistoricalAxisValue(
739 AMOTION_EVENT_AXIS_TOUCH_MINOR, pointerIndex, historicalIndex);
740 }
741
742 inline float getHistoricalToolMajor(size_t pointerIndex, size_t historicalIndex) const {
743 return getHistoricalAxisValue(
744 AMOTION_EVENT_AXIS_TOOL_MAJOR, pointerIndex, historicalIndex);
745 }
746
747 inline float getHistoricalToolMinor(size_t pointerIndex, size_t historicalIndex) const {
748 return getHistoricalAxisValue(
749 AMOTION_EVENT_AXIS_TOOL_MINOR, pointerIndex, historicalIndex);
750 }
751
752 inline float getHistoricalOrientation(size_t pointerIndex, size_t historicalIndex) const {
753 return getHistoricalAxisValue(
754 AMOTION_EVENT_AXIS_ORIENTATION, pointerIndex, historicalIndex);
755 }
756
757 ssize_t findPointerIndex(int32_t pointerId) const;
758
Garfield Tan4cc839f2020-01-24 11:26:14 -0800759 void initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600760 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
761 int32_t flags, int32_t edgeFlags, int32_t metaState, int32_t buttonState,
chaviw9eaa22c2020-07-01 16:21:27 -0700762 MotionClassification classification, const ui::Transform& transform,
763 float xPrecision, float yPrecision, float rawXCursorPosition,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700764 float rawYCursorPosition, const ui::Transform& rawTransform, nsecs_t downTime,
765 nsecs_t eventTime, size_t pointerCount,
Evan Rosky84f07f02021-04-16 10:42:42 -0700766 const PointerProperties* pointerProperties, const PointerCoords* pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -0700767
768 void copyFrom(const MotionEvent* other, bool keepHistory);
769
770 void addSample(
771 nsecs_t eventTime,
772 const PointerCoords* pointerCoords);
773
774 void offsetLocation(float xOffset, float yOffset);
775
Robert Carre07e1032018-11-26 12:55:53 -0800776 void scale(float globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700777
Evan Roskyd4d4d802021-05-03 20:12:21 -0700778 // Set 3x3 perspective matrix transformation.
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700779 // Matrix is in row-major form and compatible with SkMatrix.
chaviw9eaa22c2020-07-01 16:21:27 -0700780 void transform(const std::array<float, 9>& matrix);
Jeff Brown5912f952013-07-01 19:10:31 -0700781
Evan Roskyd4d4d802021-05-03 20:12:21 -0700782 // Apply 3x3 perspective matrix transformation only to content (do not modify mTransform).
783 // Matrix is in row-major form and compatible with SkMatrix.
784 void applyTransform(const std::array<float, 9>& matrix);
785
Brett Chabotfaa986c2020-11-04 17:39:36 -0800786#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -0700787 status_t readFromParcel(Parcel* parcel);
788 status_t writeToParcel(Parcel* parcel) const;
Brett Chabotfaa986c2020-11-04 17:39:36 -0800789#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700790
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600791 static bool isTouchEvent(uint32_t source, int32_t action);
Jeff Brown5912f952013-07-01 19:10:31 -0700792 inline bool isTouchEvent() const {
793 return isTouchEvent(mSource, mAction);
794 }
795
796 // Low-level accessors.
797 inline const PointerProperties* getPointerProperties() const {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800798 return mPointerProperties.data();
Jeff Brown5912f952013-07-01 19:10:31 -0700799 }
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500800 inline const nsecs_t* getSampleEventTimes() const { return mSampleEventTimes.data(); }
Jeff Brown5912f952013-07-01 19:10:31 -0700801 inline const PointerCoords* getSamplePointerCoords() const {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800802 return mSamplePointerCoords.data();
Jeff Brown5912f952013-07-01 19:10:31 -0700803 }
804
Michael Wright872db4f2014-04-22 15:03:51 -0700805 static const char* getLabel(int32_t axis);
806 static int32_t getAxisFromLabel(const char* label);
807
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500808 static std::string actionToString(int32_t action);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700809
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700810 // MotionEvent will transform various axes in different ways, based on the source. For
811 // example, the x and y axes will not have any offsets/translations applied if it comes from a
812 // relative mouse device (since SOURCE_RELATIVE_MOUSE is a non-pointer source). These methods
813 // are used to apply these transformations for different axes.
Prabir Pradhanb5cb9572021-09-24 06:35:16 -0700814 static vec2 calculateTransformedXY(uint32_t source, const ui::Transform&, const vec2& xy);
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700815 static float calculateTransformedAxisValue(int32_t axis, uint32_t source, const ui::Transform&,
816 const PointerCoords&);
Prabir Pradhan8e6ce222022-02-24 09:08:54 -0800817 static PointerCoords calculateTransformedCoords(uint32_t source, const ui::Transform&,
818 const PointerCoords&);
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700819
Jeff Brown5912f952013-07-01 19:10:31 -0700820protected:
821 int32_t mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100822 int32_t mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700823 int32_t mFlags;
824 int32_t mEdgeFlags;
825 int32_t mMetaState;
826 int32_t mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800827 MotionClassification mClassification;
chaviwfd9c1ed2020-07-01 10:57:59 -0700828 ui::Transform mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700829 float mXPrecision;
830 float mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700831 float mRawXCursorPosition;
832 float mRawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700833 ui::Transform mRawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700834 nsecs_t mDownTime;
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800835 std::vector<PointerProperties> mPointerProperties;
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500836 std::vector<nsecs_t> mSampleEventTimes;
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800837 std::vector<PointerCoords> mSamplePointerCoords;
Jeff Brown5912f952013-07-01 19:10:31 -0700838};
839
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000840std::ostream& operator<<(std::ostream& out, const MotionEvent& event);
841
Jeff Brown5912f952013-07-01 19:10:31 -0700842/*
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800843 * Focus events.
844 */
845class FocusEvent : public InputEvent {
846public:
847 virtual ~FocusEvent() {}
848
849 virtual int32_t getType() const override { return AINPUT_EVENT_TYPE_FOCUS; }
850
851 inline bool getHasFocus() const { return mHasFocus; }
852
Antonio Kantek3cfec7b2021-11-05 18:26:17 -0700853 void initialize(int32_t id, bool hasFocus);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800854
855 void initialize(const FocusEvent& from);
856
857protected:
858 bool mHasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800859};
860
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800861/*
862 * Capture events.
863 */
864class CaptureEvent : public InputEvent {
865public:
866 virtual ~CaptureEvent() {}
867
868 virtual int32_t getType() const override { return AINPUT_EVENT_TYPE_CAPTURE; }
869
870 inline bool getPointerCaptureEnabled() const { return mPointerCaptureEnabled; }
871
872 void initialize(int32_t id, bool pointerCaptureEnabled);
873
874 void initialize(const CaptureEvent& from);
875
876protected:
877 bool mPointerCaptureEnabled;
878};
879
arthurhung7632c332020-12-30 16:58:01 +0800880/*
881 * Drag events.
882 */
883class DragEvent : public InputEvent {
884public:
885 virtual ~DragEvent() {}
886
887 virtual int32_t getType() const override { return AINPUT_EVENT_TYPE_DRAG; }
888
889 inline bool isExiting() const { return mIsExiting; }
890
891 inline float getX() const { return mX; }
892
893 inline float getY() const { return mY; }
894
895 void initialize(int32_t id, float x, float y, bool isExiting);
896
897 void initialize(const DragEvent& from);
898
899protected:
900 bool mIsExiting;
901 float mX, mY;
902};
903
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700904/*
905 * Touch mode events.
906 */
907class TouchModeEvent : public InputEvent {
908public:
909 virtual ~TouchModeEvent() {}
910
911 virtual int32_t getType() const override { return AINPUT_EVENT_TYPE_TOUCH_MODE; }
912
913 inline bool isInTouchMode() const { return mIsInTouchMode; }
914
915 void initialize(int32_t id, bool isInTouchMode);
916
917 void initialize(const TouchModeEvent& from);
918
919protected:
920 bool mIsInTouchMode;
921};
922
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800923/**
924 * Base class for verified events.
925 * Do not create a VerifiedInputEvent explicitly.
926 * Use helper functions to create them from InputEvents.
927 */
928struct __attribute__((__packed__)) VerifiedInputEvent {
929 enum class Type : int32_t {
930 KEY = AINPUT_EVENT_TYPE_KEY,
931 MOTION = AINPUT_EVENT_TYPE_MOTION,
932 };
933
934 Type type;
935 int32_t deviceId;
936 nsecs_t eventTimeNanos;
937 uint32_t source;
938 int32_t displayId;
939};
940
941/**
942 * Same as KeyEvent, but only contains the data that can be verified.
943 * If you update this class, you must also update VerifiedKeyEvent.java
944 */
945struct __attribute__((__packed__)) VerifiedKeyEvent : public VerifiedInputEvent {
946 int32_t action;
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800947 int32_t flags;
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -0800948 nsecs_t downTimeNanos;
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800949 int32_t keyCode;
950 int32_t scanCode;
951 int32_t metaState;
952 int32_t repeatCount;
953};
954
955/**
956 * Same as MotionEvent, but only contains the data that can be verified.
957 * If you update this class, you must also update VerifiedMotionEvent.java
958 */
959struct __attribute__((__packed__)) VerifiedMotionEvent : public VerifiedInputEvent {
960 float rawX;
961 float rawY;
962 int32_t actionMasked;
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800963 int32_t flags;
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -0800964 nsecs_t downTimeNanos;
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800965 int32_t metaState;
966 int32_t buttonState;
967};
968
969VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event);
970VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event);
971
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800972/*
Jeff Brown5912f952013-07-01 19:10:31 -0700973 * Input event factory.
974 */
975class InputEventFactoryInterface {
976protected:
977 virtual ~InputEventFactoryInterface() { }
978
979public:
980 InputEventFactoryInterface() { }
981
982 virtual KeyEvent* createKeyEvent() = 0;
983 virtual MotionEvent* createMotionEvent() = 0;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800984 virtual FocusEvent* createFocusEvent() = 0;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800985 virtual CaptureEvent* createCaptureEvent() = 0;
arthurhung7632c332020-12-30 16:58:01 +0800986 virtual DragEvent* createDragEvent() = 0;
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700987 virtual TouchModeEvent* createTouchModeEvent() = 0;
Jeff Brown5912f952013-07-01 19:10:31 -0700988};
989
990/*
991 * A simple input event factory implementation that uses a single preallocated instance
992 * of each type of input event that are reused for each request.
993 */
994class PreallocatedInputEventFactory : public InputEventFactoryInterface {
995public:
996 PreallocatedInputEventFactory() { }
997 virtual ~PreallocatedInputEventFactory() { }
998
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800999 virtual KeyEvent* createKeyEvent() override { return &mKeyEvent; }
1000 virtual MotionEvent* createMotionEvent() override { return &mMotionEvent; }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001001 virtual FocusEvent* createFocusEvent() override { return &mFocusEvent; }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001002 virtual CaptureEvent* createCaptureEvent() override { return &mCaptureEvent; }
arthurhung7632c332020-12-30 16:58:01 +08001003 virtual DragEvent* createDragEvent() override { return &mDragEvent; }
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001004 virtual TouchModeEvent* createTouchModeEvent() override { return &mTouchModeEvent; }
Jeff Brown5912f952013-07-01 19:10:31 -07001005
1006private:
1007 KeyEvent mKeyEvent;
1008 MotionEvent mMotionEvent;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001009 FocusEvent mFocusEvent;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001010 CaptureEvent mCaptureEvent;
arthurhung7632c332020-12-30 16:58:01 +08001011 DragEvent mDragEvent;
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001012 TouchModeEvent mTouchModeEvent;
Jeff Brown5912f952013-07-01 19:10:31 -07001013};
1014
1015/*
1016 * An input event factory implementation that maintains a pool of input events.
1017 */
1018class PooledInputEventFactory : public InputEventFactoryInterface {
1019public:
Chih-Hung Hsiehf43b02c2018-12-20 15:45:56 -08001020 explicit PooledInputEventFactory(size_t maxPoolSize = 20);
Jeff Brown5912f952013-07-01 19:10:31 -07001021 virtual ~PooledInputEventFactory();
1022
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001023 virtual KeyEvent* createKeyEvent() override;
1024 virtual MotionEvent* createMotionEvent() override;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001025 virtual FocusEvent* createFocusEvent() override;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001026 virtual CaptureEvent* createCaptureEvent() override;
arthurhung7632c332020-12-30 16:58:01 +08001027 virtual DragEvent* createDragEvent() override;
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001028 virtual TouchModeEvent* createTouchModeEvent() override;
Jeff Brown5912f952013-07-01 19:10:31 -07001029
1030 void recycle(InputEvent* event);
1031
1032private:
1033 const size_t mMaxPoolSize;
1034
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001035 std::queue<std::unique_ptr<KeyEvent>> mKeyEventPool;
1036 std::queue<std::unique_ptr<MotionEvent>> mMotionEventPool;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001037 std::queue<std::unique_ptr<FocusEvent>> mFocusEventPool;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001038 std::queue<std::unique_ptr<CaptureEvent>> mCaptureEventPool;
arthurhung7632c332020-12-30 16:58:01 +08001039 std::queue<std::unique_ptr<DragEvent>> mDragEventPool;
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001040 std::queue<std::unique_ptr<TouchModeEvent>> mTouchModeEventPool;
Jeff Brown5912f952013-07-01 19:10:31 -07001041};
1042
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001043/*
1044 * Describes a unique request to enable or disable Pointer Capture.
1045 */
1046struct PointerCaptureRequest {
1047public:
1048 inline PointerCaptureRequest() : enable(false), seq(0) {}
1049 inline PointerCaptureRequest(bool enable, uint32_t seq) : enable(enable), seq(seq) {}
1050 inline bool operator==(const PointerCaptureRequest& other) const {
1051 return enable == other.enable && seq == other.seq;
1052 }
1053 explicit inline operator bool() const { return enable; }
1054
1055 // True iff this is a request to enable Pointer Capture.
1056 bool enable;
1057
1058 // The sequence number for the request.
1059 uint32_t seq;
1060};
1061
Jeff Brown5912f952013-07-01 19:10:31 -07001062} // namespace android
1063
1064#endif // _LIBINPUT_INPUT_H