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