blob: cc45aefe9b4bd4785697a7fd416056a565227bcf [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>
Michael Wrightd0bd3912014-03-19 12:06:10 -070027#include <utils/BitSet.h>
Jeff Brown5912f952013-07-01 19:10:31 -070028#include <utils/KeyedVector.h>
Jeff Brown5912f952013-07-01 19:10:31 -070029#include <utils/RefBase.h>
Michael Wrightd0bd3912014-03-19 12:06:10 -070030#include <utils/Timers.h>
31#include <utils/Vector.h>
Nick Kralevich834ac202015-10-22 07:09:23 -070032#include <stdint.h>
Jeff Brown5912f952013-07-01 19:10:31 -070033
Jeff Brown5912f952013-07-01 19:10:31 -070034/*
35 * Additional private constants not defined in ndk/ui/input.h.
36 */
37enum {
38 /* Signifies that the key is being predispatched */
39 AKEY_EVENT_FLAG_PREDISPATCH = 0x20000000,
40
41 /* Private control to determine when an app is tracking a key sequence. */
42 AKEY_EVENT_FLAG_START_TRACKING = 0x40000000,
43
44 /* Key event is inconsistent with previously sent key events. */
45 AKEY_EVENT_FLAG_TAINTED = 0x80000000,
46};
47
48enum {
Michael Wrightcdcd8f22016-03-22 16:52:13 -070049
50 /**
51 * This flag indicates that the window that received this motion event is partly
52 * or wholly obscured by another visible window above it. This flag is set to true
53 * even if the event did not directly pass through the obscured area.
54 * A security sensitive application can check this flag to identify situations in which
55 * a malicious application may have covered up part of its content for the purpose
56 * of misleading the user or hijacking touches. An appropriate response might be
57 * to drop the suspect touches or to take additional precautions to confirm the user's
58 * actual intent.
59 */
60 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED = 0x2,
61
Prabir Pradhan141a9862018-11-19 14:35:56 -080062 /**
63 * This flag indicates that the event has been generated by a gesture generator. It
64 * provides a hint to the GestureDetector to not apply any touch slop.
65 */
66 AMOTION_EVENT_FLAG_IS_GENERATED_GESTURE = 0x8,
67
Jeff Brown5912f952013-07-01 19:10:31 -070068 /* Motion event is inconsistent with previously sent motion events. */
69 AMOTION_EVENT_FLAG_TAINTED = 0x80000000,
70};
71
72enum {
73 /* Used when a motion event is not associated with any display.
74 * Typically used for non-pointer events. */
75 ADISPLAY_ID_NONE = -1,
76
77 /* The default display id. */
78 ADISPLAY_ID_DEFAULT = 0,
79};
80
81enum {
82 /*
83 * Indicates that an input device has switches.
84 * This input source flag is hidden from the API because switches are only used by the system
85 * and applications have no way to interact with them.
86 */
87 AINPUT_SOURCE_SWITCH = 0x80000000,
88};
89
Michael Wright962a1082013-10-17 17:35:53 -070090enum {
91 /**
92 * Constants for LEDs. Hidden from the API since we don't actually expose a way to interact
93 * with LEDs to developers
94 *
Michael Wright872db4f2014-04-22 15:03:51 -070095 * NOTE: If you add LEDs here, you must also add them to InputEventLabels.h
Michael Wright962a1082013-10-17 17:35:53 -070096 */
97
98 ALED_NUM_LOCK = 0x00,
99 ALED_CAPS_LOCK = 0x01,
100 ALED_SCROLL_LOCK = 0x02,
101 ALED_COMPOSE = 0x03,
102 ALED_KANA = 0x04,
103 ALED_SLEEP = 0x05,
104 ALED_SUSPEND = 0x06,
105 ALED_MUTE = 0x07,
106 ALED_MISC = 0x08,
107 ALED_MAIL = 0x09,
108 ALED_CHARGING = 0x0a,
109 ALED_CONTROLLER_1 = 0x10,
110 ALED_CONTROLLER_2 = 0x11,
111 ALED_CONTROLLER_3 = 0x12,
112 ALED_CONTROLLER_4 = 0x13,
113};
114
Michael Wright9b04f862013-10-18 17:53:50 -0700115/* Maximum number of controller LEDs we support */
116#define MAX_CONTROLLER_LEDS 4
117
Jeff Brown5912f952013-07-01 19:10:31 -0700118/*
119 * SystemUiVisibility constants from View.
120 */
121enum {
122 ASYSTEM_UI_VISIBILITY_STATUS_BAR_VISIBLE = 0,
123 ASYSTEM_UI_VISIBILITY_STATUS_BAR_HIDDEN = 0x00000001,
124};
125
126/*
127 * Maximum number of pointers supported per motion event.
128 * Smallest number of pointers is 1.
129 * (We want at least 10 but some touch controllers obstensibly configured for 10 pointers
130 * will occasionally emit 11. There is not much harm making this constant bigger.)
131 */
132#define MAX_POINTERS 16
133
134/*
Flanker552a8a52015-09-07 15:28:58 +0800135 * Maximum number of samples supported per motion event.
136 */
137#define MAX_SAMPLES UINT16_MAX
138
139/*
Jeff Brown5912f952013-07-01 19:10:31 -0700140 * Maximum pointer id value supported in a motion event.
141 * Smallest pointer id is 0.
142 * (This is limited by our use of BitSet32 to track pointer assignments.)
143 */
144#define MAX_POINTER_ID 31
145
146/*
147 * Declare a concrete type for the NDK's input event forward declaration.
148 */
149struct AInputEvent {
150 virtual ~AInputEvent() { }
151};
152
153/*
154 * Declare a concrete type for the NDK's input device forward declaration.
155 */
156struct AInputDevice {
157 virtual ~AInputDevice() { }
158};
159
160
161namespace android {
162
Elliott Hughes6071da72015-08-12 15:27:47 -0700163#ifdef __ANDROID__
Jeff Brown5912f952013-07-01 19:10:31 -0700164class Parcel;
165#endif
166
167/*
168 * Flags that flow alongside events in the input dispatch system to help with certain
169 * policy decisions such as waking from device sleep.
170 *
171 * These flags are also defined in frameworks/base/core/java/android/view/WindowManagerPolicy.java.
172 */
173enum {
174 /* These flags originate in RawEvents and are generally set in the key map.
Michael Wright872db4f2014-04-22 15:03:51 -0700175 * NOTE: If you want a flag to be able to set in a keylayout file, then you must add it to
176 * InputEventLabels.h as well. */
Jeff Brown5912f952013-07-01 19:10:31 -0700177
Jeff Brownc9aa6282015-02-11 19:03:28 -0800178 // Indicates that the event should wake the device.
Jeff Brown5912f952013-07-01 19:10:31 -0700179 POLICY_FLAG_WAKE = 0x00000001,
Jeff Brownc9aa6282015-02-11 19:03:28 -0800180
181 // Indicates that the key is virtual, such as a capacitive button, and should
182 // generate haptic feedback. Virtual keys may be suppressed for some time
183 // after a recent touch to prevent accidental activation of virtual keys adjacent
184 // to the touch screen during an edge swipe.
Michael Wright872db4f2014-04-22 15:03:51 -0700185 POLICY_FLAG_VIRTUAL = 0x00000002,
Jeff Brownc9aa6282015-02-11 19:03:28 -0800186
187 // Indicates that the key is the special function modifier.
Michael Wright872db4f2014-04-22 15:03:51 -0700188 POLICY_FLAG_FUNCTION = 0x00000004,
Jeff Brown5912f952013-07-01 19:10:31 -0700189
Jeff Brownc9aa6282015-02-11 19:03:28 -0800190 // Indicates that the key represents a special gesture that has been detected by
191 // the touch firmware or driver. Causes touch events from the same device to be canceled.
192 POLICY_FLAG_GESTURE = 0x00000008,
193
Jeff Brown5912f952013-07-01 19:10:31 -0700194 POLICY_FLAG_RAW_MASK = 0x0000ffff,
195
196 /* These flags are set by the input dispatcher. */
197
198 // Indicates that the input event was injected.
199 POLICY_FLAG_INJECTED = 0x01000000,
200
201 // Indicates that the input event is from a trusted source such as a directly attached
202 // input device or an application with system-wide event injection permission.
203 POLICY_FLAG_TRUSTED = 0x02000000,
204
205 // Indicates that the input event has passed through an input filter.
206 POLICY_FLAG_FILTERED = 0x04000000,
207
208 // Disables automatic key repeating behavior.
209 POLICY_FLAG_DISABLE_KEY_REPEAT = 0x08000000,
210
211 /* These flags are set by the input reader policy as it intercepts each event. */
212
Jeff Browndb19e462014-04-08 19:55:38 -0700213 // Indicates that the device was in an interactive state when the
214 // event was intercepted.
215 POLICY_FLAG_INTERACTIVE = 0x20000000,
Jeff Brown5912f952013-07-01 19:10:31 -0700216
217 // Indicates that the event should be dispatched to applications.
218 // The input event should still be sent to the InputDispatcher so that it can see all
219 // input events received include those that it will not deliver.
220 POLICY_FLAG_PASS_TO_USER = 0x40000000,
221};
222
223/*
224 * Pointer coordinate data.
225 */
226struct PointerCoords {
Michael Wright8f6710f2014-06-09 18:56:43 -0700227 enum { MAX_AXES = 30 }; // 30 so that sizeof(PointerCoords) == 128
Jeff Brown5912f952013-07-01 19:10:31 -0700228
229 // Bitfield of axes that are present in this structure.
Fengwei Yin83e0e422014-05-24 05:32:09 +0800230 uint64_t bits __attribute__((aligned(8)));
Jeff Brown5912f952013-07-01 19:10:31 -0700231
232 // Values of axes that are stored in this structure packed in order by axis id
233 // for each axis that is present in the structure according to 'bits'.
234 float values[MAX_AXES];
235
236 inline void clear() {
Michael Wrightd0bd3912014-03-19 12:06:10 -0700237 BitSet64::clear(bits);
238 }
239
240 bool isEmpty() const {
241 return BitSet64::isEmpty(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700242 }
243
244 float getAxisValue(int32_t axis) const;
245 status_t setAxisValue(int32_t axis, float value);
246
247 void scale(float scale);
Jeff Browned4d28d2014-02-11 14:28:48 -0800248 void applyOffset(float xOffset, float yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700249
250 inline float getX() const {
251 return getAxisValue(AMOTION_EVENT_AXIS_X);
252 }
253
254 inline float getY() const {
255 return getAxisValue(AMOTION_EVENT_AXIS_Y);
256 }
257
Elliott Hughes6071da72015-08-12 15:27:47 -0700258#ifdef __ANDROID__
Jeff Brown5912f952013-07-01 19:10:31 -0700259 status_t readFromParcel(Parcel* parcel);
260 status_t writeToParcel(Parcel* parcel) const;
261#endif
262
263 bool operator==(const PointerCoords& other) const;
264 inline bool operator!=(const PointerCoords& other) const {
265 return !(*this == other);
266 }
267
268 void copyFrom(const PointerCoords& other);
269
270private:
271 void tooManyAxes(int axis);
272};
273
274/*
275 * Pointer property data.
276 */
277struct PointerProperties {
278 // The id of the pointer.
279 int32_t id;
280
281 // The pointer tool type.
282 int32_t toolType;
283
284 inline void clear() {
285 id = -1;
286 toolType = 0;
287 }
288
289 bool operator==(const PointerProperties& other) const;
290 inline bool operator!=(const PointerProperties& other) const {
291 return !(*this == other);
292 }
293
294 void copyFrom(const PointerProperties& other);
295};
296
297/*
298 * Input events.
299 */
300class InputEvent : public AInputEvent {
301public:
302 virtual ~InputEvent() { }
303
304 virtual int32_t getType() const = 0;
305
306 inline int32_t getDeviceId() const { return mDeviceId; }
307
308 inline int32_t getSource() const { return mSource; }
309
310 inline void setSource(int32_t source) { mSource = source; }
311
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100312 inline int32_t getDisplayId() const { return mDisplayId; }
313
314 inline void setDisplayId(int32_t displayId) { mDisplayId = displayId; }
315
316
Jeff Brown5912f952013-07-01 19:10:31 -0700317protected:
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100318 void initialize(int32_t deviceId, int32_t source, int32_t displayId);
Jeff Brown5912f952013-07-01 19:10:31 -0700319 void initialize(const InputEvent& from);
320
321 int32_t mDeviceId;
322 int32_t mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100323 int32_t mDisplayId;
Jeff Brown5912f952013-07-01 19:10:31 -0700324};
325
326/*
327 * Key events.
328 */
329class KeyEvent : public InputEvent {
330public:
331 virtual ~KeyEvent() { }
332
333 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_KEY; }
334
335 inline int32_t getAction() const { return mAction; }
336
337 inline int32_t getFlags() const { return mFlags; }
338
339 inline void setFlags(int32_t flags) { mFlags = flags; }
340
341 inline int32_t getKeyCode() const { return mKeyCode; }
342
343 inline int32_t getScanCode() const { return mScanCode; }
344
345 inline int32_t getMetaState() const { return mMetaState; }
346
347 inline int32_t getRepeatCount() const { return mRepeatCount; }
348
349 inline nsecs_t getDownTime() const { return mDownTime; }
350
351 inline nsecs_t getEventTime() const { return mEventTime; }
352
Michael Wright872db4f2014-04-22 15:03:51 -0700353 static const char* getLabel(int32_t keyCode);
354 static int32_t getKeyCodeFromLabel(const char* label);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800355
Jeff Brown5912f952013-07-01 19:10:31 -0700356 void initialize(
357 int32_t deviceId,
358 int32_t source,
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100359 int32_t displayId,
Jeff Brown5912f952013-07-01 19:10:31 -0700360 int32_t action,
361 int32_t flags,
362 int32_t keyCode,
363 int32_t scanCode,
364 int32_t metaState,
365 int32_t repeatCount,
366 nsecs_t downTime,
367 nsecs_t eventTime);
368 void initialize(const KeyEvent& from);
369
370protected:
371 int32_t mAction;
372 int32_t mFlags;
373 int32_t mKeyCode;
374 int32_t mScanCode;
375 int32_t mMetaState;
376 int32_t mRepeatCount;
377 nsecs_t mDownTime;
378 nsecs_t mEventTime;
379};
380
381/*
382 * Motion events.
383 */
384class MotionEvent : public InputEvent {
385public:
386 virtual ~MotionEvent() { }
387
388 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_MOTION; }
389
390 inline int32_t getAction() const { return mAction; }
391
392 inline int32_t getActionMasked() const { return mAction & AMOTION_EVENT_ACTION_MASK; }
393
394 inline int32_t getActionIndex() const {
395 return (mAction & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK)
396 >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
397 }
398
399 inline void setAction(int32_t action) { mAction = action; }
400
401 inline int32_t getFlags() const { return mFlags; }
402
403 inline void setFlags(int32_t flags) { mFlags = flags; }
404
405 inline int32_t getEdgeFlags() const { return mEdgeFlags; }
406
407 inline void setEdgeFlags(int32_t edgeFlags) { mEdgeFlags = edgeFlags; }
408
409 inline int32_t getMetaState() const { return mMetaState; }
410
411 inline void setMetaState(int32_t metaState) { mMetaState = metaState; }
412
413 inline int32_t getButtonState() const { return mButtonState; }
414
Michael Wright6db58792016-09-14 19:53:37 +0100415 inline void setButtonState(int32_t buttonState) { mButtonState = buttonState; }
Michael Wright7b159c92015-05-14 14:48:03 +0100416
417 inline int32_t getActionButton() const { return mActionButton; }
418
Michael Wright21957b92015-06-17 21:06:54 +0100419 inline void setActionButton(int32_t button) { mActionButton = button; }
420
Jeff Brown5912f952013-07-01 19:10:31 -0700421 inline float getXOffset() const { return mXOffset; }
422
423 inline float getYOffset() const { return mYOffset; }
424
425 inline float getXPrecision() const { return mXPrecision; }
426
427 inline float getYPrecision() const { return mYPrecision; }
428
429 inline nsecs_t getDownTime() const { return mDownTime; }
430
431 inline void setDownTime(nsecs_t downTime) { mDownTime = downTime; }
432
433 inline size_t getPointerCount() const { return mPointerProperties.size(); }
434
435 inline const PointerProperties* getPointerProperties(size_t pointerIndex) const {
436 return &mPointerProperties[pointerIndex];
437 }
438
439 inline int32_t getPointerId(size_t pointerIndex) const {
440 return mPointerProperties[pointerIndex].id;
441 }
442
443 inline int32_t getToolType(size_t pointerIndex) const {
444 return mPointerProperties[pointerIndex].toolType;
445 }
446
447 inline nsecs_t getEventTime() const { return mSampleEventTimes[getHistorySize()]; }
448
449 const PointerCoords* getRawPointerCoords(size_t pointerIndex) const;
450
451 float getRawAxisValue(int32_t axis, size_t pointerIndex) const;
452
453 inline float getRawX(size_t pointerIndex) const {
454 return getRawAxisValue(AMOTION_EVENT_AXIS_X, pointerIndex);
455 }
456
457 inline float getRawY(size_t pointerIndex) const {
458 return getRawAxisValue(AMOTION_EVENT_AXIS_Y, pointerIndex);
459 }
460
461 float getAxisValue(int32_t axis, size_t pointerIndex) const;
462
463 inline float getX(size_t pointerIndex) const {
464 return getAxisValue(AMOTION_EVENT_AXIS_X, pointerIndex);
465 }
466
467 inline float getY(size_t pointerIndex) const {
468 return getAxisValue(AMOTION_EVENT_AXIS_Y, pointerIndex);
469 }
470
471 inline float getPressure(size_t pointerIndex) const {
472 return getAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pointerIndex);
473 }
474
475 inline float getSize(size_t pointerIndex) const {
476 return getAxisValue(AMOTION_EVENT_AXIS_SIZE, pointerIndex);
477 }
478
479 inline float getTouchMajor(size_t pointerIndex) const {
480 return getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, pointerIndex);
481 }
482
483 inline float getTouchMinor(size_t pointerIndex) const {
484 return getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, pointerIndex);
485 }
486
487 inline float getToolMajor(size_t pointerIndex) const {
488 return getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, pointerIndex);
489 }
490
491 inline float getToolMinor(size_t pointerIndex) const {
492 return getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, pointerIndex);
493 }
494
495 inline float getOrientation(size_t pointerIndex) const {
496 return getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, pointerIndex);
497 }
498
499 inline size_t getHistorySize() const { return mSampleEventTimes.size() - 1; }
500
501 inline nsecs_t getHistoricalEventTime(size_t historicalIndex) const {
502 return mSampleEventTimes[historicalIndex];
503 }
504
505 const PointerCoords* getHistoricalRawPointerCoords(
506 size_t pointerIndex, size_t historicalIndex) const;
507
508 float getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
509 size_t historicalIndex) const;
510
511 inline float getHistoricalRawX(size_t pointerIndex, size_t historicalIndex) const {
512 return getHistoricalRawAxisValue(
513 AMOTION_EVENT_AXIS_X, pointerIndex, historicalIndex);
514 }
515
516 inline float getHistoricalRawY(size_t pointerIndex, size_t historicalIndex) const {
517 return getHistoricalRawAxisValue(
518 AMOTION_EVENT_AXIS_Y, pointerIndex, historicalIndex);
519 }
520
521 float getHistoricalAxisValue(int32_t axis, size_t pointerIndex, size_t historicalIndex) const;
522
523 inline float getHistoricalX(size_t pointerIndex, size_t historicalIndex) const {
524 return getHistoricalAxisValue(
525 AMOTION_EVENT_AXIS_X, pointerIndex, historicalIndex);
526 }
527
528 inline float getHistoricalY(size_t pointerIndex, size_t historicalIndex) const {
529 return getHistoricalAxisValue(
530 AMOTION_EVENT_AXIS_Y, pointerIndex, historicalIndex);
531 }
532
533 inline float getHistoricalPressure(size_t pointerIndex, size_t historicalIndex) const {
534 return getHistoricalAxisValue(
535 AMOTION_EVENT_AXIS_PRESSURE, pointerIndex, historicalIndex);
536 }
537
538 inline float getHistoricalSize(size_t pointerIndex, size_t historicalIndex) const {
539 return getHistoricalAxisValue(
540 AMOTION_EVENT_AXIS_SIZE, pointerIndex, historicalIndex);
541 }
542
543 inline float getHistoricalTouchMajor(size_t pointerIndex, size_t historicalIndex) const {
544 return getHistoricalAxisValue(
545 AMOTION_EVENT_AXIS_TOUCH_MAJOR, pointerIndex, historicalIndex);
546 }
547
548 inline float getHistoricalTouchMinor(size_t pointerIndex, size_t historicalIndex) const {
549 return getHistoricalAxisValue(
550 AMOTION_EVENT_AXIS_TOUCH_MINOR, pointerIndex, historicalIndex);
551 }
552
553 inline float getHistoricalToolMajor(size_t pointerIndex, size_t historicalIndex) const {
554 return getHistoricalAxisValue(
555 AMOTION_EVENT_AXIS_TOOL_MAJOR, pointerIndex, historicalIndex);
556 }
557
558 inline float getHistoricalToolMinor(size_t pointerIndex, size_t historicalIndex) const {
559 return getHistoricalAxisValue(
560 AMOTION_EVENT_AXIS_TOOL_MINOR, pointerIndex, historicalIndex);
561 }
562
563 inline float getHistoricalOrientation(size_t pointerIndex, size_t historicalIndex) const {
564 return getHistoricalAxisValue(
565 AMOTION_EVENT_AXIS_ORIENTATION, pointerIndex, historicalIndex);
566 }
567
568 ssize_t findPointerIndex(int32_t pointerId) const;
569
570 void initialize(
571 int32_t deviceId,
572 int32_t source,
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800573 int32_t displayId,
Jeff Brown5912f952013-07-01 19:10:31 -0700574 int32_t action,
Michael Wright7b159c92015-05-14 14:48:03 +0100575 int32_t actionButton,
Jeff Brown5912f952013-07-01 19:10:31 -0700576 int32_t flags,
577 int32_t edgeFlags,
578 int32_t metaState,
579 int32_t buttonState,
580 float xOffset,
581 float yOffset,
582 float xPrecision,
583 float yPrecision,
584 nsecs_t downTime,
585 nsecs_t eventTime,
586 size_t pointerCount,
587 const PointerProperties* pointerProperties,
588 const PointerCoords* pointerCoords);
589
590 void copyFrom(const MotionEvent* other, bool keepHistory);
591
592 void addSample(
593 nsecs_t eventTime,
594 const PointerCoords* pointerCoords);
595
596 void offsetLocation(float xOffset, float yOffset);
597
598 void scale(float scaleFactor);
599
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700600 // Apply 3x3 perspective matrix transformation.
601 // Matrix is in row-major form and compatible with SkMatrix.
602 void transform(const float matrix[9]);
Jeff Brown5912f952013-07-01 19:10:31 -0700603
Elliott Hughes6071da72015-08-12 15:27:47 -0700604#ifdef __ANDROID__
Jeff Brown5912f952013-07-01 19:10:31 -0700605 status_t readFromParcel(Parcel* parcel);
606 status_t writeToParcel(Parcel* parcel) const;
607#endif
608
609 static bool isTouchEvent(int32_t source, int32_t action);
610 inline bool isTouchEvent() const {
611 return isTouchEvent(mSource, mAction);
612 }
613
614 // Low-level accessors.
615 inline const PointerProperties* getPointerProperties() const {
616 return mPointerProperties.array();
617 }
618 inline const nsecs_t* getSampleEventTimes() const { return mSampleEventTimes.array(); }
619 inline const PointerCoords* getSamplePointerCoords() const {
620 return mSamplePointerCoords.array();
621 }
622
Michael Wright872db4f2014-04-22 15:03:51 -0700623 static const char* getLabel(int32_t axis);
624 static int32_t getAxisFromLabel(const char* label);
625
Jeff Brown5912f952013-07-01 19:10:31 -0700626protected:
627 int32_t mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100628 int32_t mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700629 int32_t mFlags;
630 int32_t mEdgeFlags;
631 int32_t mMetaState;
632 int32_t mButtonState;
633 float mXOffset;
634 float mYOffset;
635 float mXPrecision;
636 float mYPrecision;
637 nsecs_t mDownTime;
638 Vector<PointerProperties> mPointerProperties;
639 Vector<nsecs_t> mSampleEventTimes;
640 Vector<PointerCoords> mSamplePointerCoords;
641};
642
643/*
644 * Input event factory.
645 */
646class InputEventFactoryInterface {
647protected:
648 virtual ~InputEventFactoryInterface() { }
649
650public:
651 InputEventFactoryInterface() { }
652
653 virtual KeyEvent* createKeyEvent() = 0;
654 virtual MotionEvent* createMotionEvent() = 0;
655};
656
657/*
658 * A simple input event factory implementation that uses a single preallocated instance
659 * of each type of input event that are reused for each request.
660 */
661class PreallocatedInputEventFactory : public InputEventFactoryInterface {
662public:
663 PreallocatedInputEventFactory() { }
664 virtual ~PreallocatedInputEventFactory() { }
665
666 virtual KeyEvent* createKeyEvent() { return & mKeyEvent; }
667 virtual MotionEvent* createMotionEvent() { return & mMotionEvent; }
668
669private:
670 KeyEvent mKeyEvent;
671 MotionEvent mMotionEvent;
672};
673
674/*
675 * An input event factory implementation that maintains a pool of input events.
676 */
677class PooledInputEventFactory : public InputEventFactoryInterface {
678public:
679 PooledInputEventFactory(size_t maxPoolSize = 20);
680 virtual ~PooledInputEventFactory();
681
682 virtual KeyEvent* createKeyEvent();
683 virtual MotionEvent* createMotionEvent();
684
685 void recycle(InputEvent* event);
686
687private:
688 const size_t mMaxPoolSize;
689
690 Vector<KeyEvent*> mKeyEventPool;
691 Vector<MotionEvent*> mMotionEventPool;
692};
693
694} // namespace android
695
696#endif // _LIBINPUT_INPUT_H