blob: 7ea297049b0f1088a1264b2e4806346b19255979 [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
Siarhei Vishniakoud5fe5182022-07-20 23:28:40 +0000300const char* motionToolTypeToString(int32_t toolType);
301
Garfield Tan00f511d2019-06-12 16:55:40 -0700302/**
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000303 * Portion of FrameMetrics timeline of interest to input code.
304 */
305enum GraphicsTimeline : size_t {
306 /** Time when the app sent the buffer to SurfaceFlinger. */
307 GPU_COMPLETED_TIME = 0,
308
309 /** Time when the frame was presented on the display */
310 PRESENT_TIME = 1,
311
312 /** Total size of the 'GraphicsTimeline' array. Must always be last. */
313 SIZE = 2
314};
315
316/**
Garfield Tan84b087e2020-01-23 10:49:05 -0800317 * Generator of unique numbers used to identify input events.
318 *
319 * Layout of ID:
320 * |--------------------------|---------------------------|
321 * | 2 bits for source | 30 bits for random number |
322 * |--------------------------|---------------------------|
323 */
324class IdGenerator {
325private:
326 static constexpr uint32_t SOURCE_SHIFT = 30;
327
328public:
329 // Used to divide integer space to ensure no conflict among these sources./
330 enum class Source : int32_t {
Garfield Tan750c7f42020-05-18 17:41:46 -0700331 INPUT_READER = static_cast<int32_t>(0x0u << SOURCE_SHIFT),
332 INPUT_DISPATCHER = static_cast<int32_t>(0x1u << SOURCE_SHIFT),
333 OTHER = static_cast<int32_t>(0x3u << SOURCE_SHIFT), // E.g. app injected events
Garfield Tan84b087e2020-01-23 10:49:05 -0800334 };
335 IdGenerator(Source source);
336
337 int32_t nextId() const;
338
339 // Extract source from given id.
340 static inline Source getSource(int32_t id) { return static_cast<Source>(SOURCE_MASK & id); }
341
342private:
343 const Source mSource;
344
Garfield Tan750c7f42020-05-18 17:41:46 -0700345 static constexpr int32_t SOURCE_MASK = static_cast<int32_t>(0x3u << SOURCE_SHIFT);
Garfield Tan84b087e2020-01-23 10:49:05 -0800346};
347
348/**
Garfield Tan00f511d2019-06-12 16:55:40 -0700349 * Invalid value for cursor position. Used for non-mouse events, tests and injected events. Don't
350 * use it for direct comparison with any other value, because NaN isn't equal to itself according to
351 * IEEE 754. Use isnan() instead to check if a cursor position is valid.
352 */
353constexpr float AMOTION_EVENT_INVALID_CURSOR_POSITION = std::numeric_limits<float>::quiet_NaN();
354
Jeff Brown5912f952013-07-01 19:10:31 -0700355/*
356 * Pointer coordinate data.
357 */
358struct PointerCoords {
Michael Wright8f6710f2014-06-09 18:56:43 -0700359 enum { MAX_AXES = 30 }; // 30 so that sizeof(PointerCoords) == 128
Jeff Brown5912f952013-07-01 19:10:31 -0700360
361 // Bitfield of axes that are present in this structure.
Fengwei Yin83e0e422014-05-24 05:32:09 +0800362 uint64_t bits __attribute__((aligned(8)));
Jeff Brown5912f952013-07-01 19:10:31 -0700363
364 // Values of axes that are stored in this structure packed in order by axis id
365 // for each axis that is present in the structure according to 'bits'.
366 float values[MAX_AXES];
367
368 inline void clear() {
Michael Wrightd0bd3912014-03-19 12:06:10 -0700369 BitSet64::clear(bits);
370 }
371
372 bool isEmpty() const {
373 return BitSet64::isEmpty(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700374 }
375
376 float getAxisValue(int32_t axis) const;
377 status_t setAxisValue(int32_t axis, float value);
378
Robert Carre07e1032018-11-26 12:55:53 -0800379 // Scale the pointer coordinates according to a global scale and a
380 // window scale. The global scale will be applied to TOUCH/TOOL_MAJOR/MINOR
381 // axes, however the window scaling will not.
382 void scale(float globalScale, float windowXScale, float windowYScale);
Jeff Brown5912f952013-07-01 19:10:31 -0700383
chaviwc01e1372020-07-01 12:37:31 -0700384 void transform(const ui::Transform& transform);
385
Jeff Brown5912f952013-07-01 19:10:31 -0700386 inline float getX() const {
387 return getAxisValue(AMOTION_EVENT_AXIS_X);
388 }
389
390 inline float getY() const {
391 return getAxisValue(AMOTION_EVENT_AXIS_Y);
392 }
393
Evan Rosky84f07f02021-04-16 10:42:42 -0700394 vec2 getXYValue() const { return vec2(getX(), getY()); }
395
Brett Chabotfaa986c2020-11-04 17:39:36 -0800396#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -0700397 status_t readFromParcel(Parcel* parcel);
398 status_t writeToParcel(Parcel* parcel) const;
Brett Chabotfaa986c2020-11-04 17:39:36 -0800399#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700400
401 bool operator==(const PointerCoords& other) const;
402 inline bool operator!=(const PointerCoords& other) const {
403 return !(*this == other);
404 }
405
406 void copyFrom(const PointerCoords& other);
407
408private:
409 void tooManyAxes(int axis);
410};
411
412/*
413 * Pointer property data.
414 */
415struct PointerProperties {
416 // The id of the pointer.
417 int32_t id;
418
419 // The pointer tool type.
420 int32_t toolType;
421
422 inline void clear() {
423 id = -1;
424 toolType = 0;
425 }
426
427 bool operator==(const PointerProperties& other) const;
428 inline bool operator!=(const PointerProperties& other) const {
429 return !(*this == other);
430 }
431
432 void copyFrom(const PointerProperties& other);
433};
434
435/*
436 * Input events.
437 */
438class InputEvent : public AInputEvent {
439public:
440 virtual ~InputEvent() { }
441
442 virtual int32_t getType() const = 0;
443
Garfield Tan4cc839f2020-01-24 11:26:14 -0800444 inline int32_t getId() const { return mId; }
445
Jeff Brown5912f952013-07-01 19:10:31 -0700446 inline int32_t getDeviceId() const { return mDeviceId; }
447
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600448 inline uint32_t getSource() const { return mSource; }
Jeff Brown5912f952013-07-01 19:10:31 -0700449
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600450 inline void setSource(uint32_t source) { mSource = source; }
Jeff Brown5912f952013-07-01 19:10:31 -0700451
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100452 inline int32_t getDisplayId() const { return mDisplayId; }
453
454 inline void setDisplayId(int32_t displayId) { mDisplayId = displayId; }
455
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600456 inline std::array<uint8_t, 32> getHmac() const { return mHmac; }
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100457
Garfield Tan4cc839f2020-01-24 11:26:14 -0800458 static int32_t nextId();
459
Jeff Brown5912f952013-07-01 19:10:31 -0700460protected:
Garfield Tan4cc839f2020-01-24 11:26:14 -0800461 void initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600462 std::array<uint8_t, 32> hmac);
Garfield Tan4cc839f2020-01-24 11:26:14 -0800463
Jeff Brown5912f952013-07-01 19:10:31 -0700464 void initialize(const InputEvent& from);
465
Garfield Tan4cc839f2020-01-24 11:26:14 -0800466 int32_t mId;
Jeff Brown5912f952013-07-01 19:10:31 -0700467 int32_t mDeviceId;
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600468 uint32_t mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100469 int32_t mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600470 std::array<uint8_t, 32> mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700471};
472
473/*
474 * Key events.
475 */
476class KeyEvent : public InputEvent {
477public:
478 virtual ~KeyEvent() { }
479
480 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_KEY; }
481
482 inline int32_t getAction() const { return mAction; }
483
484 inline int32_t getFlags() const { return mFlags; }
485
486 inline void setFlags(int32_t flags) { mFlags = flags; }
487
488 inline int32_t getKeyCode() const { return mKeyCode; }
489
490 inline int32_t getScanCode() const { return mScanCode; }
491
492 inline int32_t getMetaState() const { return mMetaState; }
493
494 inline int32_t getRepeatCount() const { return mRepeatCount; }
495
496 inline nsecs_t getDownTime() const { return mDownTime; }
497
498 inline nsecs_t getEventTime() const { return mEventTime; }
499
Michael Wright872db4f2014-04-22 15:03:51 -0700500 static const char* getLabel(int32_t keyCode);
501 static int32_t getKeyCodeFromLabel(const char* label);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800502
Garfield Tan4cc839f2020-01-24 11:26:14 -0800503 void initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600504 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags, int32_t keyCode,
505 int32_t scanCode, int32_t metaState, int32_t repeatCount, nsecs_t downTime,
506 nsecs_t eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -0700507 void initialize(const KeyEvent& from);
508
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700509 static const char* actionToString(int32_t action);
510
Jeff Brown5912f952013-07-01 19:10:31 -0700511protected:
512 int32_t mAction;
513 int32_t mFlags;
514 int32_t mKeyCode;
515 int32_t mScanCode;
516 int32_t mMetaState;
517 int32_t mRepeatCount;
518 nsecs_t mDownTime;
519 nsecs_t mEventTime;
520};
521
522/*
523 * Motion events.
524 */
525class MotionEvent : public InputEvent {
526public:
527 virtual ~MotionEvent() { }
528
529 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_MOTION; }
530
531 inline int32_t getAction() const { return mAction; }
532
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000533 static int32_t getActionMasked(int32_t action) { return action & AMOTION_EVENT_ACTION_MASK; }
Jeff Brown5912f952013-07-01 19:10:31 -0700534
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000535 inline int32_t getActionMasked() const { return getActionMasked(mAction); }
536
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700537 static uint8_t getActionIndex(int32_t action) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000538 return (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >>
539 AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
Jeff Brown5912f952013-07-01 19:10:31 -0700540 }
541
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000542 inline int32_t getActionIndex() const { return getActionIndex(mAction); }
543
Jeff Brown5912f952013-07-01 19:10:31 -0700544 inline void setAction(int32_t action) { mAction = action; }
545
546 inline int32_t getFlags() const { return mFlags; }
547
548 inline void setFlags(int32_t flags) { mFlags = flags; }
549
550 inline int32_t getEdgeFlags() const { return mEdgeFlags; }
551
552 inline void setEdgeFlags(int32_t edgeFlags) { mEdgeFlags = edgeFlags; }
553
554 inline int32_t getMetaState() const { return mMetaState; }
555
556 inline void setMetaState(int32_t metaState) { mMetaState = metaState; }
557
558 inline int32_t getButtonState() const { return mButtonState; }
559
Michael Wright6db58792016-09-14 19:53:37 +0100560 inline void setButtonState(int32_t buttonState) { mButtonState = buttonState; }
Michael Wright7b159c92015-05-14 14:48:03 +0100561
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800562 inline MotionClassification getClassification() const { return mClassification; }
563
Michael Wright7b159c92015-05-14 14:48:03 +0100564 inline int32_t getActionButton() const { return mActionButton; }
565
Michael Wright21957b92015-06-17 21:06:54 +0100566 inline void setActionButton(int32_t button) { mActionButton = button; }
567
chaviw9eaa22c2020-07-01 16:21:27 -0700568 inline float getXOffset() const { return mTransform.tx(); }
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600569
chaviw9eaa22c2020-07-01 16:21:27 -0700570 inline float getYOffset() const { return mTransform.ty(); }
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600571
Prabir Pradhan5beda762021-12-10 09:30:08 +0000572 inline const ui::Transform& getTransform() const { return mTransform; }
Jeff Brown5912f952013-07-01 19:10:31 -0700573
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800574 int getSurfaceRotation() const;
575
Jeff Brown5912f952013-07-01 19:10:31 -0700576 inline float getXPrecision() const { return mXPrecision; }
577
578 inline float getYPrecision() const { return mYPrecision; }
579
Garfield Tan937bb832019-07-25 17:48:31 -0700580 inline float getRawXCursorPosition() const { return mRawXCursorPosition; }
Garfield Tan00f511d2019-06-12 16:55:40 -0700581
582 float getXCursorPosition() const;
583
Garfield Tan937bb832019-07-25 17:48:31 -0700584 inline float getRawYCursorPosition() const { return mRawYCursorPosition; }
Garfield Tan00f511d2019-06-12 16:55:40 -0700585
586 float getYCursorPosition() const;
587
Garfield Tan937bb832019-07-25 17:48:31 -0700588 void setCursorPosition(float x, float y);
589
Prabir Pradhan5beda762021-12-10 09:30:08 +0000590 inline const ui::Transform& getRawTransform() const { return mRawTransform; }
Evan Rosky84f07f02021-04-16 10:42:42 -0700591
Garfield Tanab0ab9c2019-07-10 18:58:28 -0700592 static inline bool isValidCursorPosition(float x, float y) { return !isnan(x) && !isnan(y); }
593
Jeff Brown5912f952013-07-01 19:10:31 -0700594 inline nsecs_t getDownTime() const { return mDownTime; }
595
596 inline void setDownTime(nsecs_t downTime) { mDownTime = downTime; }
597
598 inline size_t getPointerCount() const { return mPointerProperties.size(); }
599
600 inline const PointerProperties* getPointerProperties(size_t pointerIndex) const {
601 return &mPointerProperties[pointerIndex];
602 }
603
604 inline int32_t getPointerId(size_t pointerIndex) const {
605 return mPointerProperties[pointerIndex].id;
606 }
607
608 inline int32_t getToolType(size_t pointerIndex) const {
609 return mPointerProperties[pointerIndex].toolType;
610 }
611
612 inline nsecs_t getEventTime() const { return mSampleEventTimes[getHistorySize()]; }
613
Evan Rosky84f07f02021-04-16 10:42:42 -0700614 /**
615 * The actual raw pointer coords: whatever comes from the input device without any external
616 * transforms applied.
617 */
Jeff Brown5912f952013-07-01 19:10:31 -0700618 const PointerCoords* getRawPointerCoords(size_t pointerIndex) const;
619
Evan Rosky84f07f02021-04-16 10:42:42 -0700620 /**
621 * This is the raw axis value. However, for X/Y axes, this currently applies a "compat-raw"
622 * transform because many apps (incorrectly) assumed that raw == oriented-screen-space.
623 * "compat raw" is raw coordinates with screen rotation applied.
624 */
Jeff Brown5912f952013-07-01 19:10:31 -0700625 float getRawAxisValue(int32_t axis, size_t pointerIndex) const;
626
627 inline float getRawX(size_t pointerIndex) const {
628 return getRawAxisValue(AMOTION_EVENT_AXIS_X, pointerIndex);
629 }
630
631 inline float getRawY(size_t pointerIndex) const {
632 return getRawAxisValue(AMOTION_EVENT_AXIS_Y, pointerIndex);
633 }
634
635 float getAxisValue(int32_t axis, size_t pointerIndex) const;
636
Siarhei Vishniakou69e4d0f2020-09-14 19:53:29 -0500637 /**
638 * Get the X coordinate of the latest sample in this MotionEvent for pointer 'pointerIndex'.
639 * Identical to calling getHistoricalX(pointerIndex, getHistorySize()).
640 */
Jeff Brown5912f952013-07-01 19:10:31 -0700641 inline float getX(size_t pointerIndex) const {
642 return getAxisValue(AMOTION_EVENT_AXIS_X, pointerIndex);
643 }
644
Siarhei Vishniakou69e4d0f2020-09-14 19:53:29 -0500645 /**
646 * Get the Y coordinate of the latest sample in this MotionEvent for pointer 'pointerIndex'.
647 * Identical to calling getHistoricalX(pointerIndex, getHistorySize()).
648 */
Jeff Brown5912f952013-07-01 19:10:31 -0700649 inline float getY(size_t pointerIndex) const {
650 return getAxisValue(AMOTION_EVENT_AXIS_Y, pointerIndex);
651 }
652
653 inline float getPressure(size_t pointerIndex) const {
654 return getAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pointerIndex);
655 }
656
657 inline float getSize(size_t pointerIndex) const {
658 return getAxisValue(AMOTION_EVENT_AXIS_SIZE, pointerIndex);
659 }
660
661 inline float getTouchMajor(size_t pointerIndex) const {
662 return getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, pointerIndex);
663 }
664
665 inline float getTouchMinor(size_t pointerIndex) const {
666 return getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, pointerIndex);
667 }
668
669 inline float getToolMajor(size_t pointerIndex) const {
670 return getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, pointerIndex);
671 }
672
673 inline float getToolMinor(size_t pointerIndex) const {
674 return getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, pointerIndex);
675 }
676
677 inline float getOrientation(size_t pointerIndex) const {
678 return getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, pointerIndex);
679 }
680
681 inline size_t getHistorySize() const { return mSampleEventTimes.size() - 1; }
682
683 inline nsecs_t getHistoricalEventTime(size_t historicalIndex) const {
684 return mSampleEventTimes[historicalIndex];
685 }
686
Evan Rosky84f07f02021-04-16 10:42:42 -0700687 /**
688 * The actual raw pointer coords: whatever comes from the input device without any external
689 * transforms applied.
690 */
Jeff Brown5912f952013-07-01 19:10:31 -0700691 const PointerCoords* getHistoricalRawPointerCoords(
692 size_t pointerIndex, size_t historicalIndex) const;
693
Evan Rosky84f07f02021-04-16 10:42:42 -0700694 /**
695 * This is the raw axis value. However, for X/Y axes, this currently applies a "compat-raw"
696 * transform because many apps (incorrectly) assumed that raw == oriented-screen-space.
697 * "compat raw" is raw coordinates with screen rotation applied.
698 */
Jeff Brown5912f952013-07-01 19:10:31 -0700699 float getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
700 size_t historicalIndex) const;
701
702 inline float getHistoricalRawX(size_t pointerIndex, size_t historicalIndex) const {
703 return getHistoricalRawAxisValue(
704 AMOTION_EVENT_AXIS_X, pointerIndex, historicalIndex);
705 }
706
707 inline float getHistoricalRawY(size_t pointerIndex, size_t historicalIndex) const {
708 return getHistoricalRawAxisValue(
709 AMOTION_EVENT_AXIS_Y, pointerIndex, historicalIndex);
710 }
711
712 float getHistoricalAxisValue(int32_t axis, size_t pointerIndex, size_t historicalIndex) const;
713
714 inline float getHistoricalX(size_t pointerIndex, size_t historicalIndex) const {
715 return getHistoricalAxisValue(
716 AMOTION_EVENT_AXIS_X, pointerIndex, historicalIndex);
717 }
718
719 inline float getHistoricalY(size_t pointerIndex, size_t historicalIndex) const {
720 return getHistoricalAxisValue(
721 AMOTION_EVENT_AXIS_Y, pointerIndex, historicalIndex);
722 }
723
724 inline float getHistoricalPressure(size_t pointerIndex, size_t historicalIndex) const {
725 return getHistoricalAxisValue(
726 AMOTION_EVENT_AXIS_PRESSURE, pointerIndex, historicalIndex);
727 }
728
729 inline float getHistoricalSize(size_t pointerIndex, size_t historicalIndex) const {
730 return getHistoricalAxisValue(
731 AMOTION_EVENT_AXIS_SIZE, pointerIndex, historicalIndex);
732 }
733
734 inline float getHistoricalTouchMajor(size_t pointerIndex, size_t historicalIndex) const {
735 return getHistoricalAxisValue(
736 AMOTION_EVENT_AXIS_TOUCH_MAJOR, pointerIndex, historicalIndex);
737 }
738
739 inline float getHistoricalTouchMinor(size_t pointerIndex, size_t historicalIndex) const {
740 return getHistoricalAxisValue(
741 AMOTION_EVENT_AXIS_TOUCH_MINOR, pointerIndex, historicalIndex);
742 }
743
744 inline float getHistoricalToolMajor(size_t pointerIndex, size_t historicalIndex) const {
745 return getHistoricalAxisValue(
746 AMOTION_EVENT_AXIS_TOOL_MAJOR, pointerIndex, historicalIndex);
747 }
748
749 inline float getHistoricalToolMinor(size_t pointerIndex, size_t historicalIndex) const {
750 return getHistoricalAxisValue(
751 AMOTION_EVENT_AXIS_TOOL_MINOR, pointerIndex, historicalIndex);
752 }
753
754 inline float getHistoricalOrientation(size_t pointerIndex, size_t historicalIndex) const {
755 return getHistoricalAxisValue(
756 AMOTION_EVENT_AXIS_ORIENTATION, pointerIndex, historicalIndex);
757 }
758
759 ssize_t findPointerIndex(int32_t pointerId) const;
760
Garfield Tan4cc839f2020-01-24 11:26:14 -0800761 void initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600762 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
763 int32_t flags, int32_t edgeFlags, int32_t metaState, int32_t buttonState,
chaviw9eaa22c2020-07-01 16:21:27 -0700764 MotionClassification classification, const ui::Transform& transform,
765 float xPrecision, float yPrecision, float rawXCursorPosition,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700766 float rawYCursorPosition, const ui::Transform& rawTransform, nsecs_t downTime,
767 nsecs_t eventTime, size_t pointerCount,
Evan Rosky84f07f02021-04-16 10:42:42 -0700768 const PointerProperties* pointerProperties, const PointerCoords* pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -0700769
770 void copyFrom(const MotionEvent* other, bool keepHistory);
771
772 void addSample(
773 nsecs_t eventTime,
774 const PointerCoords* pointerCoords);
775
776 void offsetLocation(float xOffset, float yOffset);
777
Robert Carre07e1032018-11-26 12:55:53 -0800778 void scale(float globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700779
Evan Roskyd4d4d802021-05-03 20:12:21 -0700780 // Set 3x3 perspective matrix transformation.
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700781 // Matrix is in row-major form and compatible with SkMatrix.
chaviw9eaa22c2020-07-01 16:21:27 -0700782 void transform(const std::array<float, 9>& matrix);
Jeff Brown5912f952013-07-01 19:10:31 -0700783
Evan Roskyd4d4d802021-05-03 20:12:21 -0700784 // Apply 3x3 perspective matrix transformation only to content (do not modify mTransform).
785 // Matrix is in row-major form and compatible with SkMatrix.
786 void applyTransform(const std::array<float, 9>& matrix);
787
Brett Chabotfaa986c2020-11-04 17:39:36 -0800788#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -0700789 status_t readFromParcel(Parcel* parcel);
790 status_t writeToParcel(Parcel* parcel) const;
Brett Chabotfaa986c2020-11-04 17:39:36 -0800791#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700792
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600793 static bool isTouchEvent(uint32_t source, int32_t action);
Jeff Brown5912f952013-07-01 19:10:31 -0700794 inline bool isTouchEvent() const {
795 return isTouchEvent(mSource, mAction);
796 }
797
798 // Low-level accessors.
799 inline const PointerProperties* getPointerProperties() const {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800800 return mPointerProperties.data();
Jeff Brown5912f952013-07-01 19:10:31 -0700801 }
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500802 inline const nsecs_t* getSampleEventTimes() const { return mSampleEventTimes.data(); }
Jeff Brown5912f952013-07-01 19:10:31 -0700803 inline const PointerCoords* getSamplePointerCoords() const {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800804 return mSamplePointerCoords.data();
Jeff Brown5912f952013-07-01 19:10:31 -0700805 }
806
Michael Wright872db4f2014-04-22 15:03:51 -0700807 static const char* getLabel(int32_t axis);
808 static int32_t getAxisFromLabel(const char* label);
809
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500810 static std::string actionToString(int32_t action);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700811
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700812 // MotionEvent will transform various axes in different ways, based on the source. For
813 // example, the x and y axes will not have any offsets/translations applied if it comes from a
814 // relative mouse device (since SOURCE_RELATIVE_MOUSE is a non-pointer source). These methods
815 // are used to apply these transformations for different axes.
Prabir Pradhanb5cb9572021-09-24 06:35:16 -0700816 static vec2 calculateTransformedXY(uint32_t source, const ui::Transform&, const vec2& xy);
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700817 static float calculateTransformedAxisValue(int32_t axis, uint32_t source, const ui::Transform&,
818 const PointerCoords&);
Prabir Pradhan8e6ce222022-02-24 09:08:54 -0800819 static PointerCoords calculateTransformedCoords(uint32_t source, const ui::Transform&,
820 const PointerCoords&);
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700821
Jeff Brown5912f952013-07-01 19:10:31 -0700822protected:
823 int32_t mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100824 int32_t mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700825 int32_t mFlags;
826 int32_t mEdgeFlags;
827 int32_t mMetaState;
828 int32_t mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800829 MotionClassification mClassification;
chaviwfd9c1ed2020-07-01 10:57:59 -0700830 ui::Transform mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700831 float mXPrecision;
832 float mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700833 float mRawXCursorPosition;
834 float mRawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700835 ui::Transform mRawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700836 nsecs_t mDownTime;
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800837 std::vector<PointerProperties> mPointerProperties;
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500838 std::vector<nsecs_t> mSampleEventTimes;
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800839 std::vector<PointerCoords> mSamplePointerCoords;
Jeff Brown5912f952013-07-01 19:10:31 -0700840};
841
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000842std::ostream& operator<<(std::ostream& out, const MotionEvent& event);
843
Jeff Brown5912f952013-07-01 19:10:31 -0700844/*
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800845 * Focus events.
846 */
847class FocusEvent : public InputEvent {
848public:
849 virtual ~FocusEvent() {}
850
851 virtual int32_t getType() const override { return AINPUT_EVENT_TYPE_FOCUS; }
852
853 inline bool getHasFocus() const { return mHasFocus; }
854
Antonio Kantek3cfec7b2021-11-05 18:26:17 -0700855 void initialize(int32_t id, bool hasFocus);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800856
857 void initialize(const FocusEvent& from);
858
859protected:
860 bool mHasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800861};
862
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800863/*
864 * Capture events.
865 */
866class CaptureEvent : public InputEvent {
867public:
868 virtual ~CaptureEvent() {}
869
870 virtual int32_t getType() const override { return AINPUT_EVENT_TYPE_CAPTURE; }
871
872 inline bool getPointerCaptureEnabled() const { return mPointerCaptureEnabled; }
873
874 void initialize(int32_t id, bool pointerCaptureEnabled);
875
876 void initialize(const CaptureEvent& from);
877
878protected:
879 bool mPointerCaptureEnabled;
880};
881
arthurhung7632c332020-12-30 16:58:01 +0800882/*
883 * Drag events.
884 */
885class DragEvent : public InputEvent {
886public:
887 virtual ~DragEvent() {}
888
889 virtual int32_t getType() const override { return AINPUT_EVENT_TYPE_DRAG; }
890
891 inline bool isExiting() const { return mIsExiting; }
892
893 inline float getX() const { return mX; }
894
895 inline float getY() const { return mY; }
896
897 void initialize(int32_t id, float x, float y, bool isExiting);
898
899 void initialize(const DragEvent& from);
900
901protected:
902 bool mIsExiting;
903 float mX, mY;
904};
905
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700906/*
907 * Touch mode events.
908 */
909class TouchModeEvent : public InputEvent {
910public:
911 virtual ~TouchModeEvent() {}
912
913 virtual int32_t getType() const override { return AINPUT_EVENT_TYPE_TOUCH_MODE; }
914
915 inline bool isInTouchMode() const { return mIsInTouchMode; }
916
917 void initialize(int32_t id, bool isInTouchMode);
918
919 void initialize(const TouchModeEvent& from);
920
921protected:
922 bool mIsInTouchMode;
923};
924
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800925/**
926 * Base class for verified events.
927 * Do not create a VerifiedInputEvent explicitly.
928 * Use helper functions to create them from InputEvents.
929 */
930struct __attribute__((__packed__)) VerifiedInputEvent {
931 enum class Type : int32_t {
932 KEY = AINPUT_EVENT_TYPE_KEY,
933 MOTION = AINPUT_EVENT_TYPE_MOTION,
934 };
935
936 Type type;
937 int32_t deviceId;
938 nsecs_t eventTimeNanos;
939 uint32_t source;
940 int32_t displayId;
941};
942
943/**
944 * Same as KeyEvent, but only contains the data that can be verified.
945 * If you update this class, you must also update VerifiedKeyEvent.java
946 */
947struct __attribute__((__packed__)) VerifiedKeyEvent : public VerifiedInputEvent {
948 int32_t action;
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800949 int32_t flags;
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -0800950 nsecs_t downTimeNanos;
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800951 int32_t keyCode;
952 int32_t scanCode;
953 int32_t metaState;
954 int32_t repeatCount;
955};
956
957/**
958 * Same as MotionEvent, but only contains the data that can be verified.
959 * If you update this class, you must also update VerifiedMotionEvent.java
960 */
961struct __attribute__((__packed__)) VerifiedMotionEvent : public VerifiedInputEvent {
962 float rawX;
963 float rawY;
964 int32_t actionMasked;
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800965 int32_t flags;
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -0800966 nsecs_t downTimeNanos;
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800967 int32_t metaState;
968 int32_t buttonState;
969};
970
971VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event);
972VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event);
973
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800974/*
Jeff Brown5912f952013-07-01 19:10:31 -0700975 * Input event factory.
976 */
977class InputEventFactoryInterface {
978protected:
979 virtual ~InputEventFactoryInterface() { }
980
981public:
982 InputEventFactoryInterface() { }
983
984 virtual KeyEvent* createKeyEvent() = 0;
985 virtual MotionEvent* createMotionEvent() = 0;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800986 virtual FocusEvent* createFocusEvent() = 0;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800987 virtual CaptureEvent* createCaptureEvent() = 0;
arthurhung7632c332020-12-30 16:58:01 +0800988 virtual DragEvent* createDragEvent() = 0;
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700989 virtual TouchModeEvent* createTouchModeEvent() = 0;
Jeff Brown5912f952013-07-01 19:10:31 -0700990};
991
992/*
993 * A simple input event factory implementation that uses a single preallocated instance
994 * of each type of input event that are reused for each request.
995 */
996class PreallocatedInputEventFactory : public InputEventFactoryInterface {
997public:
998 PreallocatedInputEventFactory() { }
999 virtual ~PreallocatedInputEventFactory() { }
1000
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001001 virtual KeyEvent* createKeyEvent() override { return &mKeyEvent; }
1002 virtual MotionEvent* createMotionEvent() override { return &mMotionEvent; }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001003 virtual FocusEvent* createFocusEvent() override { return &mFocusEvent; }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001004 virtual CaptureEvent* createCaptureEvent() override { return &mCaptureEvent; }
arthurhung7632c332020-12-30 16:58:01 +08001005 virtual DragEvent* createDragEvent() override { return &mDragEvent; }
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001006 virtual TouchModeEvent* createTouchModeEvent() override { return &mTouchModeEvent; }
Jeff Brown5912f952013-07-01 19:10:31 -07001007
1008private:
1009 KeyEvent mKeyEvent;
1010 MotionEvent mMotionEvent;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001011 FocusEvent mFocusEvent;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001012 CaptureEvent mCaptureEvent;
arthurhung7632c332020-12-30 16:58:01 +08001013 DragEvent mDragEvent;
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001014 TouchModeEvent mTouchModeEvent;
Jeff Brown5912f952013-07-01 19:10:31 -07001015};
1016
1017/*
1018 * An input event factory implementation that maintains a pool of input events.
1019 */
1020class PooledInputEventFactory : public InputEventFactoryInterface {
1021public:
Chih-Hung Hsiehf43b02c2018-12-20 15:45:56 -08001022 explicit PooledInputEventFactory(size_t maxPoolSize = 20);
Jeff Brown5912f952013-07-01 19:10:31 -07001023 virtual ~PooledInputEventFactory();
1024
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001025 virtual KeyEvent* createKeyEvent() override;
1026 virtual MotionEvent* createMotionEvent() override;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001027 virtual FocusEvent* createFocusEvent() override;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001028 virtual CaptureEvent* createCaptureEvent() override;
arthurhung7632c332020-12-30 16:58:01 +08001029 virtual DragEvent* createDragEvent() override;
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001030 virtual TouchModeEvent* createTouchModeEvent() override;
Jeff Brown5912f952013-07-01 19:10:31 -07001031
1032 void recycle(InputEvent* event);
1033
1034private:
1035 const size_t mMaxPoolSize;
1036
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001037 std::queue<std::unique_ptr<KeyEvent>> mKeyEventPool;
1038 std::queue<std::unique_ptr<MotionEvent>> mMotionEventPool;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001039 std::queue<std::unique_ptr<FocusEvent>> mFocusEventPool;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001040 std::queue<std::unique_ptr<CaptureEvent>> mCaptureEventPool;
arthurhung7632c332020-12-30 16:58:01 +08001041 std::queue<std::unique_ptr<DragEvent>> mDragEventPool;
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001042 std::queue<std::unique_ptr<TouchModeEvent>> mTouchModeEventPool;
Jeff Brown5912f952013-07-01 19:10:31 -07001043};
1044
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001045/*
1046 * Describes a unique request to enable or disable Pointer Capture.
1047 */
1048struct PointerCaptureRequest {
1049public:
1050 inline PointerCaptureRequest() : enable(false), seq(0) {}
1051 inline PointerCaptureRequest(bool enable, uint32_t seq) : enable(enable), seq(seq) {}
1052 inline bool operator==(const PointerCaptureRequest& other) const {
1053 return enable == other.enable && seq == other.seq;
1054 }
1055 explicit inline operator bool() const { return enable; }
1056
1057 // True iff this is a request to enable Pointer Capture.
1058 bool enable;
1059
1060 // The sequence number for the request.
1061 uint32_t seq;
1062};
1063
Jeff Brown5912f952013-07-01 19:10:31 -07001064} // namespace android
1065
1066#endif // _LIBINPUT_INPUT_H