blob: a97624658cd951ee11e7fe7612846ab9155d3a4c [file] [log] [blame]
Jeff Brown5912f952013-07-01 19:10:31 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _LIBINPUT_INPUT_H
18#define _LIBINPUT_INPUT_H
19
Robert Carr2c358bf2018-08-08 15:58:15 -070020#pragma GCC system_header
21
Jeff Brown5912f952013-07-01 19:10:31 -070022/**
23 * Native input event structures.
24 */
25
26#include <android/input.h>
Garfield Tan00f511d2019-06-12 16:55:40 -070027#include <stdint.h>
Michael Wrightd0bd3912014-03-19 12:06:10 -070028#include <utils/BitSet.h>
Jeff Brown5912f952013-07-01 19:10:31 -070029#include <utils/KeyedVector.h>
Jeff Brown5912f952013-07-01 19:10:31 -070030#include <utils/RefBase.h>
Michael Wrightd0bd3912014-03-19 12:06:10 -070031#include <utils/Timers.h>
32#include <utils/Vector.h>
Garfield Tan00f511d2019-06-12 16:55:40 -070033
34#include <limits>
Jeff Brown5912f952013-07-01 19:10:31 -070035
Jeff Brown5912f952013-07-01 19:10:31 -070036/*
37 * Additional private constants not defined in ndk/ui/input.h.
38 */
39enum {
40 /* Signifies that the key is being predispatched */
41 AKEY_EVENT_FLAG_PREDISPATCH = 0x20000000,
42
43 /* Private control to determine when an app is tracking a key sequence. */
44 AKEY_EVENT_FLAG_START_TRACKING = 0x40000000,
45
46 /* Key event is inconsistent with previously sent key events. */
47 AKEY_EVENT_FLAG_TAINTED = 0x80000000,
48};
49
50enum {
Michael Wrightcdcd8f22016-03-22 16:52:13 -070051
52 /**
53 * This flag indicates that the window that received this motion event is partly
54 * or wholly obscured by another visible window above it. This flag is set to true
55 * even if the event did not directly pass through the obscured area.
56 * A security sensitive application can check this flag to identify situations in which
57 * a malicious application may have covered up part of its content for the purpose
58 * of misleading the user or hijacking touches. An appropriate response might be
59 * to drop the suspect touches or to take additional precautions to confirm the user's
60 * actual intent.
61 */
62 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED = 0x2,
63
Prabir Pradhan141a9862018-11-19 14:35:56 -080064 /**
65 * This flag indicates that the event has been generated by a gesture generator. It
66 * provides a hint to the GestureDetector to not apply any touch slop.
67 */
68 AMOTION_EVENT_FLAG_IS_GENERATED_GESTURE = 0x8,
69
Jeff Brown5912f952013-07-01 19:10:31 -070070 /* Motion event is inconsistent with previously sent motion events. */
71 AMOTION_EVENT_FLAG_TAINTED = 0x80000000,
72};
73
74enum {
75 /* Used when a motion event is not associated with any display.
76 * Typically used for non-pointer events. */
77 ADISPLAY_ID_NONE = -1,
78
79 /* The default display id. */
80 ADISPLAY_ID_DEFAULT = 0,
81};
82
83enum {
84 /*
85 * Indicates that an input device has switches.
86 * This input source flag is hidden from the API because switches are only used by the system
87 * and applications have no way to interact with them.
88 */
89 AINPUT_SOURCE_SWITCH = 0x80000000,
90};
91
Michael Wright962a1082013-10-17 17:35:53 -070092enum {
93 /**
94 * Constants for LEDs. Hidden from the API since we don't actually expose a way to interact
95 * with LEDs to developers
96 *
Michael Wright872db4f2014-04-22 15:03:51 -070097 * NOTE: If you add LEDs here, you must also add them to InputEventLabels.h
Michael Wright962a1082013-10-17 17:35:53 -070098 */
99
100 ALED_NUM_LOCK = 0x00,
101 ALED_CAPS_LOCK = 0x01,
102 ALED_SCROLL_LOCK = 0x02,
103 ALED_COMPOSE = 0x03,
104 ALED_KANA = 0x04,
105 ALED_SLEEP = 0x05,
106 ALED_SUSPEND = 0x06,
107 ALED_MUTE = 0x07,
108 ALED_MISC = 0x08,
109 ALED_MAIL = 0x09,
110 ALED_CHARGING = 0x0a,
111 ALED_CONTROLLER_1 = 0x10,
112 ALED_CONTROLLER_2 = 0x11,
113 ALED_CONTROLLER_3 = 0x12,
114 ALED_CONTROLLER_4 = 0x13,
115};
116
Michael Wright9b04f862013-10-18 17:53:50 -0700117/* Maximum number of controller LEDs we support */
118#define MAX_CONTROLLER_LEDS 4
119
Jeff Brown5912f952013-07-01 19:10:31 -0700120/*
121 * SystemUiVisibility constants from View.
122 */
123enum {
124 ASYSTEM_UI_VISIBILITY_STATUS_BAR_VISIBLE = 0,
125 ASYSTEM_UI_VISIBILITY_STATUS_BAR_HIDDEN = 0x00000001,
126};
127
128/*
129 * Maximum number of pointers supported per motion event.
130 * Smallest number of pointers is 1.
131 * (We want at least 10 but some touch controllers obstensibly configured for 10 pointers
132 * will occasionally emit 11. There is not much harm making this constant bigger.)
133 */
134#define MAX_POINTERS 16
135
136/*
Flanker552a8a52015-09-07 15:28:58 +0800137 * Maximum number of samples supported per motion event.
138 */
139#define MAX_SAMPLES UINT16_MAX
140
141/*
Jeff Brown5912f952013-07-01 19:10:31 -0700142 * Maximum pointer id value supported in a motion event.
143 * Smallest pointer id is 0.
144 * (This is limited by our use of BitSet32 to track pointer assignments.)
145 */
146#define MAX_POINTER_ID 31
147
148/*
149 * Declare a concrete type for the NDK's input event forward declaration.
150 */
151struct AInputEvent {
152 virtual ~AInputEvent() { }
153};
154
155/*
156 * Declare a concrete type for the NDK's input device forward declaration.
157 */
158struct AInputDevice {
159 virtual ~AInputDevice() { }
160};
161
162
163namespace android {
164
Elliott Hughes6071da72015-08-12 15:27:47 -0700165#ifdef __ANDROID__
Jeff Brown5912f952013-07-01 19:10:31 -0700166class Parcel;
167#endif
168
169/*
170 * Flags that flow alongside events in the input dispatch system to help with certain
171 * policy decisions such as waking from device sleep.
172 *
173 * These flags are also defined in frameworks/base/core/java/android/view/WindowManagerPolicy.java.
174 */
175enum {
176 /* These flags originate in RawEvents and are generally set in the key map.
Michael Wright872db4f2014-04-22 15:03:51 -0700177 * NOTE: If you want a flag to be able to set in a keylayout file, then you must add it to
178 * InputEventLabels.h as well. */
Jeff Brown5912f952013-07-01 19:10:31 -0700179
Jeff Brownc9aa6282015-02-11 19:03:28 -0800180 // Indicates that the event should wake the device.
Jeff Brown5912f952013-07-01 19:10:31 -0700181 POLICY_FLAG_WAKE = 0x00000001,
Jeff Brownc9aa6282015-02-11 19:03:28 -0800182
183 // Indicates that the key is virtual, such as a capacitive button, and should
184 // generate haptic feedback. Virtual keys may be suppressed for some time
185 // after a recent touch to prevent accidental activation of virtual keys adjacent
186 // to the touch screen during an edge swipe.
Michael Wright872db4f2014-04-22 15:03:51 -0700187 POLICY_FLAG_VIRTUAL = 0x00000002,
Jeff Brownc9aa6282015-02-11 19:03:28 -0800188
189 // Indicates that the key is the special function modifier.
Michael Wright872db4f2014-04-22 15:03:51 -0700190 POLICY_FLAG_FUNCTION = 0x00000004,
Jeff Brown5912f952013-07-01 19:10:31 -0700191
Jeff Brownc9aa6282015-02-11 19:03:28 -0800192 // Indicates that the key represents a special gesture that has been detected by
193 // the touch firmware or driver. Causes touch events from the same device to be canceled.
194 POLICY_FLAG_GESTURE = 0x00000008,
195
Jeff Brown5912f952013-07-01 19:10:31 -0700196 POLICY_FLAG_RAW_MASK = 0x0000ffff,
197
198 /* These flags are set by the input dispatcher. */
199
200 // Indicates that the input event was injected.
201 POLICY_FLAG_INJECTED = 0x01000000,
202
203 // Indicates that the input event is from a trusted source such as a directly attached
204 // input device or an application with system-wide event injection permission.
205 POLICY_FLAG_TRUSTED = 0x02000000,
206
207 // Indicates that the input event has passed through an input filter.
208 POLICY_FLAG_FILTERED = 0x04000000,
209
210 // Disables automatic key repeating behavior.
211 POLICY_FLAG_DISABLE_KEY_REPEAT = 0x08000000,
212
213 /* These flags are set by the input reader policy as it intercepts each event. */
214
Jeff Browndb19e462014-04-08 19:55:38 -0700215 // Indicates that the device was in an interactive state when the
216 // event was intercepted.
217 POLICY_FLAG_INTERACTIVE = 0x20000000,
Jeff Brown5912f952013-07-01 19:10:31 -0700218
219 // Indicates that the event should be dispatched to applications.
220 // The input event should still be sent to the InputDispatcher so that it can see all
221 // input events received include those that it will not deliver.
222 POLICY_FLAG_PASS_TO_USER = 0x40000000,
223};
224
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800225/**
226 * Classifications of the current gesture, if available.
227 *
228 * The following values must be kept in sync with MotionEvent.java
229 */
230enum class MotionClassification : uint8_t {
231 /**
232 * No classification is available.
233 */
234 NONE = 0,
235 /**
236 * Too early to classify the current gesture. Need more events. Look for changes in the
237 * upcoming motion events.
238 */
239 AMBIGUOUS_GESTURE = 1,
240 /**
241 * The current gesture likely represents a user intentionally exerting force on the touchscreen.
242 */
243 DEEP_PRESS = 2,
244};
245
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800246/**
247 * String representation of MotionClassification
248 */
249const char* motionClassificationToString(MotionClassification classification);
250
Garfield Tan00f511d2019-06-12 16:55:40 -0700251/**
252 * Invalid value for cursor position. Used for non-mouse events, tests and injected events. Don't
253 * use it for direct comparison with any other value, because NaN isn't equal to itself according to
254 * IEEE 754. Use isnan() instead to check if a cursor position is valid.
255 */
256constexpr float AMOTION_EVENT_INVALID_CURSOR_POSITION = std::numeric_limits<float>::quiet_NaN();
257
Jeff Brown5912f952013-07-01 19:10:31 -0700258/*
259 * Pointer coordinate data.
260 */
261struct PointerCoords {
Michael Wright8f6710f2014-06-09 18:56:43 -0700262 enum { MAX_AXES = 30 }; // 30 so that sizeof(PointerCoords) == 128
Jeff Brown5912f952013-07-01 19:10:31 -0700263
264 // Bitfield of axes that are present in this structure.
Fengwei Yin83e0e422014-05-24 05:32:09 +0800265 uint64_t bits __attribute__((aligned(8)));
Jeff Brown5912f952013-07-01 19:10:31 -0700266
267 // Values of axes that are stored in this structure packed in order by axis id
268 // for each axis that is present in the structure according to 'bits'.
269 float values[MAX_AXES];
270
271 inline void clear() {
Michael Wrightd0bd3912014-03-19 12:06:10 -0700272 BitSet64::clear(bits);
273 }
274
275 bool isEmpty() const {
276 return BitSet64::isEmpty(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700277 }
278
279 float getAxisValue(int32_t axis) const;
280 status_t setAxisValue(int32_t axis, float value);
281
Robert Carre07e1032018-11-26 12:55:53 -0800282 void scale(float globalScale);
283
284 // Scale the pointer coordinates according to a global scale and a
285 // window scale. The global scale will be applied to TOUCH/TOOL_MAJOR/MINOR
286 // axes, however the window scaling will not.
287 void scale(float globalScale, float windowXScale, float windowYScale);
Jeff Browned4d28d2014-02-11 14:28:48 -0800288 void applyOffset(float xOffset, float yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700289
290 inline float getX() const {
291 return getAxisValue(AMOTION_EVENT_AXIS_X);
292 }
293
294 inline float getY() const {
295 return getAxisValue(AMOTION_EVENT_AXIS_Y);
296 }
297
Elliott Hughes6071da72015-08-12 15:27:47 -0700298#ifdef __ANDROID__
Jeff Brown5912f952013-07-01 19:10:31 -0700299 status_t readFromParcel(Parcel* parcel);
300 status_t writeToParcel(Parcel* parcel) const;
301#endif
302
303 bool operator==(const PointerCoords& other) const;
304 inline bool operator!=(const PointerCoords& other) const {
305 return !(*this == other);
306 }
307
308 void copyFrom(const PointerCoords& other);
309
310private:
311 void tooManyAxes(int axis);
312};
313
314/*
315 * Pointer property data.
316 */
317struct PointerProperties {
318 // The id of the pointer.
319 int32_t id;
320
321 // The pointer tool type.
322 int32_t toolType;
323
324 inline void clear() {
325 id = -1;
326 toolType = 0;
327 }
328
329 bool operator==(const PointerProperties& other) const;
330 inline bool operator!=(const PointerProperties& other) const {
331 return !(*this == other);
332 }
333
334 void copyFrom(const PointerProperties& other);
335};
336
337/*
338 * Input events.
339 */
340class InputEvent : public AInputEvent {
341public:
342 virtual ~InputEvent() { }
343
344 virtual int32_t getType() const = 0;
345
346 inline int32_t getDeviceId() const { return mDeviceId; }
347
348 inline int32_t getSource() const { return mSource; }
349
350 inline void setSource(int32_t source) { mSource = source; }
351
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100352 inline int32_t getDisplayId() const { return mDisplayId; }
353
354 inline void setDisplayId(int32_t displayId) { mDisplayId = displayId; }
355
356
Jeff Brown5912f952013-07-01 19:10:31 -0700357protected:
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100358 void initialize(int32_t deviceId, int32_t source, int32_t displayId);
Jeff Brown5912f952013-07-01 19:10:31 -0700359 void initialize(const InputEvent& from);
360
361 int32_t mDeviceId;
362 int32_t mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100363 int32_t mDisplayId;
Jeff Brown5912f952013-07-01 19:10:31 -0700364};
365
366/*
367 * Key events.
368 */
369class KeyEvent : public InputEvent {
370public:
371 virtual ~KeyEvent() { }
372
373 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_KEY; }
374
375 inline int32_t getAction() const { return mAction; }
376
377 inline int32_t getFlags() const { return mFlags; }
378
379 inline void setFlags(int32_t flags) { mFlags = flags; }
380
381 inline int32_t getKeyCode() const { return mKeyCode; }
382
383 inline int32_t getScanCode() const { return mScanCode; }
384
385 inline int32_t getMetaState() const { return mMetaState; }
386
387 inline int32_t getRepeatCount() const { return mRepeatCount; }
388
389 inline nsecs_t getDownTime() const { return mDownTime; }
390
391 inline nsecs_t getEventTime() const { return mEventTime; }
392
Michael Wright872db4f2014-04-22 15:03:51 -0700393 static const char* getLabel(int32_t keyCode);
394 static int32_t getKeyCodeFromLabel(const char* label);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800395
Jeff Brown5912f952013-07-01 19:10:31 -0700396 void initialize(
397 int32_t deviceId,
398 int32_t source,
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100399 int32_t displayId,
Jeff Brown5912f952013-07-01 19:10:31 -0700400 int32_t action,
401 int32_t flags,
402 int32_t keyCode,
403 int32_t scanCode,
404 int32_t metaState,
405 int32_t repeatCount,
406 nsecs_t downTime,
407 nsecs_t eventTime);
408 void initialize(const KeyEvent& from);
409
410protected:
411 int32_t mAction;
412 int32_t mFlags;
413 int32_t mKeyCode;
414 int32_t mScanCode;
415 int32_t mMetaState;
416 int32_t mRepeatCount;
417 nsecs_t mDownTime;
418 nsecs_t mEventTime;
419};
420
421/*
422 * Motion events.
423 */
424class MotionEvent : public InputEvent {
425public:
426 virtual ~MotionEvent() { }
427
428 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_MOTION; }
429
430 inline int32_t getAction() const { return mAction; }
431
432 inline int32_t getActionMasked() const { return mAction & AMOTION_EVENT_ACTION_MASK; }
433
434 inline int32_t getActionIndex() const {
435 return (mAction & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK)
436 >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
437 }
438
439 inline void setAction(int32_t action) { mAction = action; }
440
441 inline int32_t getFlags() const { return mFlags; }
442
443 inline void setFlags(int32_t flags) { mFlags = flags; }
444
445 inline int32_t getEdgeFlags() const { return mEdgeFlags; }
446
447 inline void setEdgeFlags(int32_t edgeFlags) { mEdgeFlags = edgeFlags; }
448
449 inline int32_t getMetaState() const { return mMetaState; }
450
451 inline void setMetaState(int32_t metaState) { mMetaState = metaState; }
452
453 inline int32_t getButtonState() const { return mButtonState; }
454
Michael Wright6db58792016-09-14 19:53:37 +0100455 inline void setButtonState(int32_t buttonState) { mButtonState = buttonState; }
Michael Wright7b159c92015-05-14 14:48:03 +0100456
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800457 inline MotionClassification getClassification() const { return mClassification; }
458
Michael Wright7b159c92015-05-14 14:48:03 +0100459 inline int32_t getActionButton() const { return mActionButton; }
460
Michael Wright21957b92015-06-17 21:06:54 +0100461 inline void setActionButton(int32_t button) { mActionButton = button; }
462
Jeff Brown5912f952013-07-01 19:10:31 -0700463 inline float getXOffset() const { return mXOffset; }
464
465 inline float getYOffset() const { return mYOffset; }
466
467 inline float getXPrecision() const { return mXPrecision; }
468
469 inline float getYPrecision() const { return mYPrecision; }
470
Garfield Tan00f511d2019-06-12 16:55:40 -0700471 inline float getRawXCursorPosition() const { return mXCursorPosition; }
472
473 float getXCursorPosition() const;
474
475 inline float getRawYCursorPosition() const { return mYCursorPosition; }
476
477 float getYCursorPosition() const;
478
Jeff Brown5912f952013-07-01 19:10:31 -0700479 inline nsecs_t getDownTime() const { return mDownTime; }
480
481 inline void setDownTime(nsecs_t downTime) { mDownTime = downTime; }
482
483 inline size_t getPointerCount() const { return mPointerProperties.size(); }
484
485 inline const PointerProperties* getPointerProperties(size_t pointerIndex) const {
486 return &mPointerProperties[pointerIndex];
487 }
488
489 inline int32_t getPointerId(size_t pointerIndex) const {
490 return mPointerProperties[pointerIndex].id;
491 }
492
493 inline int32_t getToolType(size_t pointerIndex) const {
494 return mPointerProperties[pointerIndex].toolType;
495 }
496
497 inline nsecs_t getEventTime() const { return mSampleEventTimes[getHistorySize()]; }
498
499 const PointerCoords* getRawPointerCoords(size_t pointerIndex) const;
500
501 float getRawAxisValue(int32_t axis, size_t pointerIndex) const;
502
503 inline float getRawX(size_t pointerIndex) const {
504 return getRawAxisValue(AMOTION_EVENT_AXIS_X, pointerIndex);
505 }
506
507 inline float getRawY(size_t pointerIndex) const {
508 return getRawAxisValue(AMOTION_EVENT_AXIS_Y, pointerIndex);
509 }
510
511 float getAxisValue(int32_t axis, size_t pointerIndex) const;
512
513 inline float getX(size_t pointerIndex) const {
514 return getAxisValue(AMOTION_EVENT_AXIS_X, pointerIndex);
515 }
516
517 inline float getY(size_t pointerIndex) const {
518 return getAxisValue(AMOTION_EVENT_AXIS_Y, pointerIndex);
519 }
520
521 inline float getPressure(size_t pointerIndex) const {
522 return getAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pointerIndex);
523 }
524
525 inline float getSize(size_t pointerIndex) const {
526 return getAxisValue(AMOTION_EVENT_AXIS_SIZE, pointerIndex);
527 }
528
529 inline float getTouchMajor(size_t pointerIndex) const {
530 return getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, pointerIndex);
531 }
532
533 inline float getTouchMinor(size_t pointerIndex) const {
534 return getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, pointerIndex);
535 }
536
537 inline float getToolMajor(size_t pointerIndex) const {
538 return getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, pointerIndex);
539 }
540
541 inline float getToolMinor(size_t pointerIndex) const {
542 return getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, pointerIndex);
543 }
544
545 inline float getOrientation(size_t pointerIndex) const {
546 return getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, pointerIndex);
547 }
548
549 inline size_t getHistorySize() const { return mSampleEventTimes.size() - 1; }
550
551 inline nsecs_t getHistoricalEventTime(size_t historicalIndex) const {
552 return mSampleEventTimes[historicalIndex];
553 }
554
555 const PointerCoords* getHistoricalRawPointerCoords(
556 size_t pointerIndex, size_t historicalIndex) const;
557
558 float getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
559 size_t historicalIndex) const;
560
561 inline float getHistoricalRawX(size_t pointerIndex, size_t historicalIndex) const {
562 return getHistoricalRawAxisValue(
563 AMOTION_EVENT_AXIS_X, pointerIndex, historicalIndex);
564 }
565
566 inline float getHistoricalRawY(size_t pointerIndex, size_t historicalIndex) const {
567 return getHistoricalRawAxisValue(
568 AMOTION_EVENT_AXIS_Y, pointerIndex, historicalIndex);
569 }
570
571 float getHistoricalAxisValue(int32_t axis, size_t pointerIndex, size_t historicalIndex) const;
572
573 inline float getHistoricalX(size_t pointerIndex, size_t historicalIndex) const {
574 return getHistoricalAxisValue(
575 AMOTION_EVENT_AXIS_X, pointerIndex, historicalIndex);
576 }
577
578 inline float getHistoricalY(size_t pointerIndex, size_t historicalIndex) const {
579 return getHistoricalAxisValue(
580 AMOTION_EVENT_AXIS_Y, pointerIndex, historicalIndex);
581 }
582
583 inline float getHistoricalPressure(size_t pointerIndex, size_t historicalIndex) const {
584 return getHistoricalAxisValue(
585 AMOTION_EVENT_AXIS_PRESSURE, pointerIndex, historicalIndex);
586 }
587
588 inline float getHistoricalSize(size_t pointerIndex, size_t historicalIndex) const {
589 return getHistoricalAxisValue(
590 AMOTION_EVENT_AXIS_SIZE, pointerIndex, historicalIndex);
591 }
592
593 inline float getHistoricalTouchMajor(size_t pointerIndex, size_t historicalIndex) const {
594 return getHistoricalAxisValue(
595 AMOTION_EVENT_AXIS_TOUCH_MAJOR, pointerIndex, historicalIndex);
596 }
597
598 inline float getHistoricalTouchMinor(size_t pointerIndex, size_t historicalIndex) const {
599 return getHistoricalAxisValue(
600 AMOTION_EVENT_AXIS_TOUCH_MINOR, pointerIndex, historicalIndex);
601 }
602
603 inline float getHistoricalToolMajor(size_t pointerIndex, size_t historicalIndex) const {
604 return getHistoricalAxisValue(
605 AMOTION_EVENT_AXIS_TOOL_MAJOR, pointerIndex, historicalIndex);
606 }
607
608 inline float getHistoricalToolMinor(size_t pointerIndex, size_t historicalIndex) const {
609 return getHistoricalAxisValue(
610 AMOTION_EVENT_AXIS_TOOL_MINOR, pointerIndex, historicalIndex);
611 }
612
613 inline float getHistoricalOrientation(size_t pointerIndex, size_t historicalIndex) const {
614 return getHistoricalAxisValue(
615 AMOTION_EVENT_AXIS_ORIENTATION, pointerIndex, historicalIndex);
616 }
617
618 ssize_t findPointerIndex(int32_t pointerId) const;
619
Garfield Tan00f511d2019-06-12 16:55:40 -0700620 void initialize(int32_t deviceId, int32_t source, int32_t displayId, int32_t action,
621 int32_t actionButton, int32_t flags, int32_t edgeFlags, int32_t metaState,
622 int32_t buttonState, MotionClassification classification, float xOffset,
623 float yOffset, float xPrecision, float yPrecision, float mXCursorPosition,
624 float mYCursorPosition, nsecs_t downTime, nsecs_t eventTime,
625 size_t pointerCount, const PointerProperties* pointerProperties,
626 const PointerCoords* pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -0700627
628 void copyFrom(const MotionEvent* other, bool keepHistory);
629
630 void addSample(
631 nsecs_t eventTime,
632 const PointerCoords* pointerCoords);
633
634 void offsetLocation(float xOffset, float yOffset);
635
Robert Carre07e1032018-11-26 12:55:53 -0800636 void scale(float globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700637
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700638 // Apply 3x3 perspective matrix transformation.
639 // Matrix is in row-major form and compatible with SkMatrix.
640 void transform(const float matrix[9]);
Jeff Brown5912f952013-07-01 19:10:31 -0700641
Elliott Hughes6071da72015-08-12 15:27:47 -0700642#ifdef __ANDROID__
Jeff Brown5912f952013-07-01 19:10:31 -0700643 status_t readFromParcel(Parcel* parcel);
644 status_t writeToParcel(Parcel* parcel) const;
645#endif
646
647 static bool isTouchEvent(int32_t source, int32_t action);
648 inline bool isTouchEvent() const {
649 return isTouchEvent(mSource, mAction);
650 }
651
652 // Low-level accessors.
653 inline const PointerProperties* getPointerProperties() const {
654 return mPointerProperties.array();
655 }
656 inline const nsecs_t* getSampleEventTimes() const { return mSampleEventTimes.array(); }
657 inline const PointerCoords* getSamplePointerCoords() const {
658 return mSamplePointerCoords.array();
659 }
660
Michael Wright872db4f2014-04-22 15:03:51 -0700661 static const char* getLabel(int32_t axis);
662 static int32_t getAxisFromLabel(const char* label);
663
Jeff Brown5912f952013-07-01 19:10:31 -0700664protected:
665 int32_t mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100666 int32_t mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700667 int32_t mFlags;
668 int32_t mEdgeFlags;
669 int32_t mMetaState;
670 int32_t mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800671 MotionClassification mClassification;
Jeff Brown5912f952013-07-01 19:10:31 -0700672 float mXOffset;
673 float mYOffset;
674 float mXPrecision;
675 float mYPrecision;
Garfield Tan00f511d2019-06-12 16:55:40 -0700676 float mXCursorPosition;
677 float mYCursorPosition;
Jeff Brown5912f952013-07-01 19:10:31 -0700678 nsecs_t mDownTime;
679 Vector<PointerProperties> mPointerProperties;
680 Vector<nsecs_t> mSampleEventTimes;
681 Vector<PointerCoords> mSamplePointerCoords;
682};
683
684/*
685 * Input event factory.
686 */
687class InputEventFactoryInterface {
688protected:
689 virtual ~InputEventFactoryInterface() { }
690
691public:
692 InputEventFactoryInterface() { }
693
694 virtual KeyEvent* createKeyEvent() = 0;
695 virtual MotionEvent* createMotionEvent() = 0;
696};
697
698/*
699 * A simple input event factory implementation that uses a single preallocated instance
700 * of each type of input event that are reused for each request.
701 */
702class PreallocatedInputEventFactory : public InputEventFactoryInterface {
703public:
704 PreallocatedInputEventFactory() { }
705 virtual ~PreallocatedInputEventFactory() { }
706
707 virtual KeyEvent* createKeyEvent() { return & mKeyEvent; }
708 virtual MotionEvent* createMotionEvent() { return & mMotionEvent; }
709
710private:
711 KeyEvent mKeyEvent;
712 MotionEvent mMotionEvent;
713};
714
715/*
716 * An input event factory implementation that maintains a pool of input events.
717 */
718class PooledInputEventFactory : public InputEventFactoryInterface {
719public:
Chih-Hung Hsiehf43b02c2018-12-20 15:45:56 -0800720 explicit PooledInputEventFactory(size_t maxPoolSize = 20);
Jeff Brown5912f952013-07-01 19:10:31 -0700721 virtual ~PooledInputEventFactory();
722
723 virtual KeyEvent* createKeyEvent();
724 virtual MotionEvent* createMotionEvent();
725
726 void recycle(InputEvent* event);
727
728private:
729 const size_t mMaxPoolSize;
730
731 Vector<KeyEvent*> mKeyEventPool;
732 Vector<MotionEvent*> mMotionEventPool;
733};
734
735} // namespace android
736
737#endif // _LIBINPUT_INPUT_H