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