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