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