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