blob: 235813e5212d85927bb8e3a7e0b115b7d444dd96 [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
20/**
21 * Native input event structures.
22 */
23
24#include <android/input.h>
Michael Wrightd0bd3912014-03-19 12:06:10 -070025#include <utils/BitSet.h>
Jeff Brown5912f952013-07-01 19:10:31 -070026#include <utils/KeyedVector.h>
Jeff Brown5912f952013-07-01 19:10:31 -070027#include <utils/RefBase.h>
28#include <utils/String8.h>
Michael Wrightd0bd3912014-03-19 12:06:10 -070029#include <utils/Timers.h>
30#include <utils/Vector.h>
Nick Kralevich834ac202015-10-22 07:09:23 -070031#include <stdint.h>
Jeff Brown5912f952013-07-01 19:10:31 -070032
Jeff Brown5912f952013-07-01 19:10:31 -070033/*
34 * Additional private constants not defined in ndk/ui/input.h.
35 */
36enum {
37 /* Signifies that the key is being predispatched */
38 AKEY_EVENT_FLAG_PREDISPATCH = 0x20000000,
39
40 /* Private control to determine when an app is tracking a key sequence. */
41 AKEY_EVENT_FLAG_START_TRACKING = 0x40000000,
42
43 /* Key event is inconsistent with previously sent key events. */
44 AKEY_EVENT_FLAG_TAINTED = 0x80000000,
45};
46
47enum {
48 /* Motion event is inconsistent with previously sent motion events. */
49 AMOTION_EVENT_FLAG_TAINTED = 0x80000000,
50};
51
52enum {
53 /* Used when a motion event is not associated with any display.
54 * Typically used for non-pointer events. */
55 ADISPLAY_ID_NONE = -1,
56
57 /* The default display id. */
58 ADISPLAY_ID_DEFAULT = 0,
59};
60
61enum {
62 /*
63 * Indicates that an input device has switches.
64 * This input source flag is hidden from the API because switches are only used by the system
65 * and applications have no way to interact with them.
66 */
67 AINPUT_SOURCE_SWITCH = 0x80000000,
68};
69
Michael Wright962a1082013-10-17 17:35:53 -070070enum {
71 /**
72 * Constants for LEDs. Hidden from the API since we don't actually expose a way to interact
73 * with LEDs to developers
74 *
75 * NOTE: If you add LEDs here, you must also add them to KeycodeLabels.h
76 */
77
78 ALED_NUM_LOCK = 0x00,
79 ALED_CAPS_LOCK = 0x01,
80 ALED_SCROLL_LOCK = 0x02,
81 ALED_COMPOSE = 0x03,
82 ALED_KANA = 0x04,
83 ALED_SLEEP = 0x05,
84 ALED_SUSPEND = 0x06,
85 ALED_MUTE = 0x07,
86 ALED_MISC = 0x08,
87 ALED_MAIL = 0x09,
88 ALED_CHARGING = 0x0a,
89 ALED_CONTROLLER_1 = 0x10,
90 ALED_CONTROLLER_2 = 0x11,
91 ALED_CONTROLLER_3 = 0x12,
92 ALED_CONTROLLER_4 = 0x13,
93};
94
Michael Wright9b04f862013-10-18 17:53:50 -070095/* Maximum number of controller LEDs we support */
96#define MAX_CONTROLLER_LEDS 4
97
Jeff Brown5912f952013-07-01 19:10:31 -070098/*
99 * SystemUiVisibility constants from View.
100 */
101enum {
102 ASYSTEM_UI_VISIBILITY_STATUS_BAR_VISIBLE = 0,
103 ASYSTEM_UI_VISIBILITY_STATUS_BAR_HIDDEN = 0x00000001,
104};
105
106/*
107 * Maximum number of pointers supported per motion event.
108 * Smallest number of pointers is 1.
109 * (We want at least 10 but some touch controllers obstensibly configured for 10 pointers
110 * will occasionally emit 11. There is not much harm making this constant bigger.)
111 */
112#define MAX_POINTERS 16
113
114/*
Flanker5d178382015-09-07 15:28:58 +0800115 * Maximum number of samples supported per motion event.
116 */
Nick Kralevich778b6f42015-10-22 14:48:50 -0700117#define MAX_SAMPLES 65535
Flanker5d178382015-09-07 15:28:58 +0800118
119/*
Jeff Brown5912f952013-07-01 19:10:31 -0700120 * Maximum pointer id value supported in a motion event.
121 * Smallest pointer id is 0.
122 * (This is limited by our use of BitSet32 to track pointer assignments.)
123 */
124#define MAX_POINTER_ID 31
125
126/*
127 * Declare a concrete type for the NDK's input event forward declaration.
128 */
129struct AInputEvent {
130 virtual ~AInputEvent() { }
131};
132
133/*
134 * Declare a concrete type for the NDK's input device forward declaration.
135 */
136struct AInputDevice {
137 virtual ~AInputDevice() { }
138};
139
140
141namespace android {
142
143#ifdef HAVE_ANDROID_OS
144class Parcel;
145#endif
146
147/*
148 * Flags that flow alongside events in the input dispatch system to help with certain
149 * policy decisions such as waking from device sleep.
150 *
151 * These flags are also defined in frameworks/base/core/java/android/view/WindowManagerPolicy.java.
152 */
153enum {
154 /* These flags originate in RawEvents and are generally set in the key map.
155 * NOTE: If you edit these flags, also edit labels in KeycodeLabels.h. */
156
157 POLICY_FLAG_WAKE = 0x00000001,
158 POLICY_FLAG_WAKE_DROPPED = 0x00000002,
159 POLICY_FLAG_SHIFT = 0x00000004,
160 POLICY_FLAG_CAPS_LOCK = 0x00000008,
161 POLICY_FLAG_ALT = 0x00000010,
162 POLICY_FLAG_ALT_GR = 0x00000020,
163 POLICY_FLAG_MENU = 0x00000040,
164 POLICY_FLAG_LAUNCHER = 0x00000080,
165 POLICY_FLAG_VIRTUAL = 0x00000100,
166 POLICY_FLAG_FUNCTION = 0x00000200,
167
168 POLICY_FLAG_RAW_MASK = 0x0000ffff,
169
170 /* These flags are set by the input dispatcher. */
171
172 // Indicates that the input event was injected.
173 POLICY_FLAG_INJECTED = 0x01000000,
174
175 // Indicates that the input event is from a trusted source such as a directly attached
176 // input device or an application with system-wide event injection permission.
177 POLICY_FLAG_TRUSTED = 0x02000000,
178
179 // Indicates that the input event has passed through an input filter.
180 POLICY_FLAG_FILTERED = 0x04000000,
181
182 // Disables automatic key repeating behavior.
183 POLICY_FLAG_DISABLE_KEY_REPEAT = 0x08000000,
184
185 /* These flags are set by the input reader policy as it intercepts each event. */
186
Jeff Browndb19e462014-04-08 19:55:38 -0700187 // Indicates that the device was in an interactive state when the
188 // event was intercepted.
189 POLICY_FLAG_INTERACTIVE = 0x20000000,
Jeff Brown5912f952013-07-01 19:10:31 -0700190
191 // Indicates that the event should be dispatched to applications.
192 // The input event should still be sent to the InputDispatcher so that it can see all
193 // input events received include those that it will not deliver.
194 POLICY_FLAG_PASS_TO_USER = 0x40000000,
195};
196
197/*
198 * Pointer coordinate data.
199 */
200struct PointerCoords {
201 enum { MAX_AXES = 14 }; // 14 so that sizeof(PointerCoords) == 64
202
203 // Bitfield of axes that are present in this structure.
204 uint64_t bits;
205
206 // Values of axes that are stored in this structure packed in order by axis id
207 // for each axis that is present in the structure according to 'bits'.
208 float values[MAX_AXES];
209
210 inline void clear() {
Michael Wrightd0bd3912014-03-19 12:06:10 -0700211 BitSet64::clear(bits);
212 }
213
214 bool isEmpty() const {
215 return BitSet64::isEmpty(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700216 }
217
218 float getAxisValue(int32_t axis) const;
219 status_t setAxisValue(int32_t axis, float value);
220
221 void scale(float scale);
Jeff Browned4d28d2014-02-11 14:28:48 -0800222 void applyOffset(float xOffset, float yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700223
224 inline float getX() const {
225 return getAxisValue(AMOTION_EVENT_AXIS_X);
226 }
227
228 inline float getY() const {
229 return getAxisValue(AMOTION_EVENT_AXIS_Y);
230 }
231
232#ifdef HAVE_ANDROID_OS
233 status_t readFromParcel(Parcel* parcel);
234 status_t writeToParcel(Parcel* parcel) const;
235#endif
236
237 bool operator==(const PointerCoords& other) const;
238 inline bool operator!=(const PointerCoords& other) const {
239 return !(*this == other);
240 }
241
242 void copyFrom(const PointerCoords& other);
243
244private:
245 void tooManyAxes(int axis);
246};
247
248/*
249 * Pointer property data.
250 */
251struct PointerProperties {
252 // The id of the pointer.
253 int32_t id;
254
255 // The pointer tool type.
256 int32_t toolType;
257
258 inline void clear() {
259 id = -1;
260 toolType = 0;
261 }
262
263 bool operator==(const PointerProperties& other) const;
264 inline bool operator!=(const PointerProperties& other) const {
265 return !(*this == other);
266 }
267
268 void copyFrom(const PointerProperties& other);
269};
270
271/*
272 * Input events.
273 */
274class InputEvent : public AInputEvent {
275public:
276 virtual ~InputEvent() { }
277
278 virtual int32_t getType() const = 0;
279
280 inline int32_t getDeviceId() const { return mDeviceId; }
281
282 inline int32_t getSource() const { return mSource; }
283
284 inline void setSource(int32_t source) { mSource = source; }
285
286protected:
287 void initialize(int32_t deviceId, int32_t source);
288 void initialize(const InputEvent& from);
289
290 int32_t mDeviceId;
291 int32_t mSource;
292};
293
294/*
295 * Key events.
296 */
297class KeyEvent : public InputEvent {
298public:
299 virtual ~KeyEvent() { }
300
301 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_KEY; }
302
303 inline int32_t getAction() const { return mAction; }
304
305 inline int32_t getFlags() const { return mFlags; }
306
307 inline void setFlags(int32_t flags) { mFlags = flags; }
308
309 inline int32_t getKeyCode() const { return mKeyCode; }
310
311 inline int32_t getScanCode() const { return mScanCode; }
312
313 inline int32_t getMetaState() const { return mMetaState; }
314
315 inline int32_t getRepeatCount() const { return mRepeatCount; }
316
317 inline nsecs_t getDownTime() const { return mDownTime; }
318
319 inline nsecs_t getEventTime() const { return mEventTime; }
320
321 // Return true if this event may have a default action implementation.
322 static bool hasDefaultAction(int32_t keyCode);
323 bool hasDefaultAction() const;
324
325 // Return true if this event represents a system key.
326 static bool isSystemKey(int32_t keyCode);
327 bool isSystemKey() const;
328
329 void initialize(
330 int32_t deviceId,
331 int32_t source,
332 int32_t action,
333 int32_t flags,
334 int32_t keyCode,
335 int32_t scanCode,
336 int32_t metaState,
337 int32_t repeatCount,
338 nsecs_t downTime,
339 nsecs_t eventTime);
340 void initialize(const KeyEvent& from);
341
342protected:
343 int32_t mAction;
344 int32_t mFlags;
345 int32_t mKeyCode;
346 int32_t mScanCode;
347 int32_t mMetaState;
348 int32_t mRepeatCount;
349 nsecs_t mDownTime;
350 nsecs_t mEventTime;
351};
352
353/*
354 * Motion events.
355 */
356class MotionEvent : public InputEvent {
357public:
358 virtual ~MotionEvent() { }
359
360 virtual int32_t getType() const { return AINPUT_EVENT_TYPE_MOTION; }
361
362 inline int32_t getAction() const { return mAction; }
363
364 inline int32_t getActionMasked() const { return mAction & AMOTION_EVENT_ACTION_MASK; }
365
366 inline int32_t getActionIndex() const {
367 return (mAction & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK)
368 >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
369 }
370
371 inline void setAction(int32_t action) { mAction = action; }
372
373 inline int32_t getFlags() const { return mFlags; }
374
375 inline void setFlags(int32_t flags) { mFlags = flags; }
376
377 inline int32_t getEdgeFlags() const { return mEdgeFlags; }
378
379 inline void setEdgeFlags(int32_t edgeFlags) { mEdgeFlags = edgeFlags; }
380
381 inline int32_t getMetaState() const { return mMetaState; }
382
383 inline void setMetaState(int32_t metaState) { mMetaState = metaState; }
384
385 inline int32_t getButtonState() const { return mButtonState; }
386
387 inline float getXOffset() const { return mXOffset; }
388
389 inline float getYOffset() const { return mYOffset; }
390
391 inline float getXPrecision() const { return mXPrecision; }
392
393 inline float getYPrecision() const { return mYPrecision; }
394
395 inline nsecs_t getDownTime() const { return mDownTime; }
396
397 inline void setDownTime(nsecs_t downTime) { mDownTime = downTime; }
398
399 inline size_t getPointerCount() const { return mPointerProperties.size(); }
400
401 inline const PointerProperties* getPointerProperties(size_t pointerIndex) const {
402 return &mPointerProperties[pointerIndex];
403 }
404
405 inline int32_t getPointerId(size_t pointerIndex) const {
406 return mPointerProperties[pointerIndex].id;
407 }
408
409 inline int32_t getToolType(size_t pointerIndex) const {
410 return mPointerProperties[pointerIndex].toolType;
411 }
412
413 inline nsecs_t getEventTime() const { return mSampleEventTimes[getHistorySize()]; }
414
415 const PointerCoords* getRawPointerCoords(size_t pointerIndex) const;
416
417 float getRawAxisValue(int32_t axis, size_t pointerIndex) const;
418
419 inline float getRawX(size_t pointerIndex) const {
420 return getRawAxisValue(AMOTION_EVENT_AXIS_X, pointerIndex);
421 }
422
423 inline float getRawY(size_t pointerIndex) const {
424 return getRawAxisValue(AMOTION_EVENT_AXIS_Y, pointerIndex);
425 }
426
427 float getAxisValue(int32_t axis, size_t pointerIndex) const;
428
429 inline float getX(size_t pointerIndex) const {
430 return getAxisValue(AMOTION_EVENT_AXIS_X, pointerIndex);
431 }
432
433 inline float getY(size_t pointerIndex) const {
434 return getAxisValue(AMOTION_EVENT_AXIS_Y, pointerIndex);
435 }
436
437 inline float getPressure(size_t pointerIndex) const {
438 return getAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pointerIndex);
439 }
440
441 inline float getSize(size_t pointerIndex) const {
442 return getAxisValue(AMOTION_EVENT_AXIS_SIZE, pointerIndex);
443 }
444
445 inline float getTouchMajor(size_t pointerIndex) const {
446 return getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, pointerIndex);
447 }
448
449 inline float getTouchMinor(size_t pointerIndex) const {
450 return getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, pointerIndex);
451 }
452
453 inline float getToolMajor(size_t pointerIndex) const {
454 return getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, pointerIndex);
455 }
456
457 inline float getToolMinor(size_t pointerIndex) const {
458 return getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, pointerIndex);
459 }
460
461 inline float getOrientation(size_t pointerIndex) const {
462 return getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, pointerIndex);
463 }
464
465 inline size_t getHistorySize() const { return mSampleEventTimes.size() - 1; }
466
467 inline nsecs_t getHistoricalEventTime(size_t historicalIndex) const {
468 return mSampleEventTimes[historicalIndex];
469 }
470
471 const PointerCoords* getHistoricalRawPointerCoords(
472 size_t pointerIndex, size_t historicalIndex) const;
473
474 float getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
475 size_t historicalIndex) const;
476
477 inline float getHistoricalRawX(size_t pointerIndex, size_t historicalIndex) const {
478 return getHistoricalRawAxisValue(
479 AMOTION_EVENT_AXIS_X, pointerIndex, historicalIndex);
480 }
481
482 inline float getHistoricalRawY(size_t pointerIndex, size_t historicalIndex) const {
483 return getHistoricalRawAxisValue(
484 AMOTION_EVENT_AXIS_Y, pointerIndex, historicalIndex);
485 }
486
487 float getHistoricalAxisValue(int32_t axis, size_t pointerIndex, size_t historicalIndex) const;
488
489 inline float getHistoricalX(size_t pointerIndex, size_t historicalIndex) const {
490 return getHistoricalAxisValue(
491 AMOTION_EVENT_AXIS_X, pointerIndex, historicalIndex);
492 }
493
494 inline float getHistoricalY(size_t pointerIndex, size_t historicalIndex) const {
495 return getHistoricalAxisValue(
496 AMOTION_EVENT_AXIS_Y, pointerIndex, historicalIndex);
497 }
498
499 inline float getHistoricalPressure(size_t pointerIndex, size_t historicalIndex) const {
500 return getHistoricalAxisValue(
501 AMOTION_EVENT_AXIS_PRESSURE, pointerIndex, historicalIndex);
502 }
503
504 inline float getHistoricalSize(size_t pointerIndex, size_t historicalIndex) const {
505 return getHistoricalAxisValue(
506 AMOTION_EVENT_AXIS_SIZE, pointerIndex, historicalIndex);
507 }
508
509 inline float getHistoricalTouchMajor(size_t pointerIndex, size_t historicalIndex) const {
510 return getHistoricalAxisValue(
511 AMOTION_EVENT_AXIS_TOUCH_MAJOR, pointerIndex, historicalIndex);
512 }
513
514 inline float getHistoricalTouchMinor(size_t pointerIndex, size_t historicalIndex) const {
515 return getHistoricalAxisValue(
516 AMOTION_EVENT_AXIS_TOUCH_MINOR, pointerIndex, historicalIndex);
517 }
518
519 inline float getHistoricalToolMajor(size_t pointerIndex, size_t historicalIndex) const {
520 return getHistoricalAxisValue(
521 AMOTION_EVENT_AXIS_TOOL_MAJOR, pointerIndex, historicalIndex);
522 }
523
524 inline float getHistoricalToolMinor(size_t pointerIndex, size_t historicalIndex) const {
525 return getHistoricalAxisValue(
526 AMOTION_EVENT_AXIS_TOOL_MINOR, pointerIndex, historicalIndex);
527 }
528
529 inline float getHistoricalOrientation(size_t pointerIndex, size_t historicalIndex) const {
530 return getHistoricalAxisValue(
531 AMOTION_EVENT_AXIS_ORIENTATION, pointerIndex, historicalIndex);
532 }
533
534 ssize_t findPointerIndex(int32_t pointerId) const;
535
536 void initialize(
537 int32_t deviceId,
538 int32_t source,
539 int32_t action,
540 int32_t flags,
541 int32_t edgeFlags,
542 int32_t metaState,
543 int32_t buttonState,
544 float xOffset,
545 float yOffset,
546 float xPrecision,
547 float yPrecision,
548 nsecs_t downTime,
549 nsecs_t eventTime,
550 size_t pointerCount,
551 const PointerProperties* pointerProperties,
552 const PointerCoords* pointerCoords);
553
554 void copyFrom(const MotionEvent* other, bool keepHistory);
555
556 void addSample(
557 nsecs_t eventTime,
558 const PointerCoords* pointerCoords);
559
560 void offsetLocation(float xOffset, float yOffset);
561
562 void scale(float scaleFactor);
563
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700564 // Apply 3x3 perspective matrix transformation.
565 // Matrix is in row-major form and compatible with SkMatrix.
566 void transform(const float matrix[9]);
Jeff Brown5912f952013-07-01 19:10:31 -0700567
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700568#ifdef HAVE_ANDROID_OS
Jeff Brown5912f952013-07-01 19:10:31 -0700569 status_t readFromParcel(Parcel* parcel);
570 status_t writeToParcel(Parcel* parcel) const;
571#endif
572
573 static bool isTouchEvent(int32_t source, int32_t action);
574 inline bool isTouchEvent() const {
575 return isTouchEvent(mSource, mAction);
576 }
577
578 // Low-level accessors.
579 inline const PointerProperties* getPointerProperties() const {
580 return mPointerProperties.array();
581 }
582 inline const nsecs_t* getSampleEventTimes() const { return mSampleEventTimes.array(); }
583 inline const PointerCoords* getSamplePointerCoords() const {
584 return mSamplePointerCoords.array();
585 }
586
587protected:
588 int32_t mAction;
589 int32_t mFlags;
590 int32_t mEdgeFlags;
591 int32_t mMetaState;
592 int32_t mButtonState;
593 float mXOffset;
594 float mYOffset;
595 float mXPrecision;
596 float mYPrecision;
597 nsecs_t mDownTime;
598 Vector<PointerProperties> mPointerProperties;
599 Vector<nsecs_t> mSampleEventTimes;
600 Vector<PointerCoords> mSamplePointerCoords;
601};
602
603/*
604 * Input event factory.
605 */
606class InputEventFactoryInterface {
607protected:
608 virtual ~InputEventFactoryInterface() { }
609
610public:
611 InputEventFactoryInterface() { }
612
613 virtual KeyEvent* createKeyEvent() = 0;
614 virtual MotionEvent* createMotionEvent() = 0;
615};
616
617/*
618 * A simple input event factory implementation that uses a single preallocated instance
619 * of each type of input event that are reused for each request.
620 */
621class PreallocatedInputEventFactory : public InputEventFactoryInterface {
622public:
623 PreallocatedInputEventFactory() { }
624 virtual ~PreallocatedInputEventFactory() { }
625
626 virtual KeyEvent* createKeyEvent() { return & mKeyEvent; }
627 virtual MotionEvent* createMotionEvent() { return & mMotionEvent; }
628
629private:
630 KeyEvent mKeyEvent;
631 MotionEvent mMotionEvent;
632};
633
634/*
635 * An input event factory implementation that maintains a pool of input events.
636 */
637class PooledInputEventFactory : public InputEventFactoryInterface {
638public:
639 PooledInputEventFactory(size_t maxPoolSize = 20);
640 virtual ~PooledInputEventFactory();
641
642 virtual KeyEvent* createKeyEvent();
643 virtual MotionEvent* createMotionEvent();
644
645 void recycle(InputEvent* event);
646
647private:
648 const size_t mMaxPoolSize;
649
650 Vector<KeyEvent*> mKeyEventPool;
651 Vector<MotionEvent*> mMotionEventPool;
652};
653
654} // namespace android
655
656#endif // _LIBINPUT_INPUT_H