• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 _UI_INPUT_H
18 #define _UI_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 /*
32  * Additional private constants not defined in ndk/ui/input.h.
33  */
34 enum {
35     /*
36      * Private control to determine when an app is tracking a key sequence.
37      */
38     AKEY_EVENT_FLAG_START_TRACKING = 0x40000000
39 };
40 
41 /*
42  * Maximum number of pointers supported per motion event.
43  * Smallest number of pointers is 1.
44  */
45 #define MAX_POINTERS 10
46 
47 /*
48  * Maximum pointer id value supported in a motion event.
49  * Smallest pointer id is 0.
50  * (This is limited by our use of BitSet32 to track pointer assignments.)
51  */
52 #define MAX_POINTER_ID 31
53 
54 /*
55  * Declare a concrete type for the NDK's input event forward declaration.
56  */
57 struct AInputEvent {
~AInputEventAInputEvent58     virtual ~AInputEvent() { }
59 };
60 
61 /*
62  * Declare a concrete type for the NDK's input device forward declaration.
63  */
64 struct AInputDevice {
~AInputDeviceAInputDevice65     virtual ~AInputDevice() { }
66 };
67 
68 
69 namespace android {
70 
71 /*
72  * Flags that flow alongside events in the input dispatch system to help with certain
73  * policy decisions such as waking from device sleep.
74  *
75  * These flags are also defined in frameworks/base/core/java/android/view/WindowManagerPolicy.java.
76  */
77 enum {
78     /* These flags originate in RawEvents and are generally set in the key map.
79      * See also labels for policy flags in KeycodeLabels.h. */
80 
81     POLICY_FLAG_WAKE = 0x00000001,
82     POLICY_FLAG_WAKE_DROPPED = 0x00000002,
83     POLICY_FLAG_SHIFT = 0x00000004,
84     POLICY_FLAG_CAPS_LOCK = 0x00000008,
85     POLICY_FLAG_ALT = 0x00000010,
86     POLICY_FLAG_ALT_GR = 0x00000020,
87     POLICY_FLAG_MENU = 0x00000040,
88     POLICY_FLAG_LAUNCHER = 0x00000080,
89     POLICY_FLAG_VIRTUAL = 0x00000100,
90 
91     POLICY_FLAG_RAW_MASK = 0x0000ffff,
92 
93     /* These flags are set by the input dispatcher. */
94 
95     // Indicates that the input event was injected.
96     POLICY_FLAG_INJECTED = 0x01000000,
97 
98     // Indicates that the input event is from a trusted source such as a directly attached
99     // input device or an application with system-wide event injection permission.
100     POLICY_FLAG_TRUSTED = 0x02000000,
101 
102     /* These flags are set by the input reader policy as it intercepts each event. */
103 
104     // Indicates that the screen was off when the event was received and the event
105     // should wake the device.
106     POLICY_FLAG_WOKE_HERE = 0x10000000,
107 
108     // Indicates that the screen was dim when the event was received and the event
109     // should brighten the device.
110     POLICY_FLAG_BRIGHT_HERE = 0x20000000,
111 
112     // Indicates that the event should be dispatched to applications.
113     // The input event should still be sent to the InputDispatcher so that it can see all
114     // input events received include those that it will not deliver.
115     POLICY_FLAG_PASS_TO_USER = 0x40000000,
116 };
117 
118 /*
119  * Describes the basic configuration of input devices that are present.
120  */
121 struct InputConfiguration {
122     enum {
123         TOUCHSCREEN_UNDEFINED = 0,
124         TOUCHSCREEN_NOTOUCH = 1,
125         TOUCHSCREEN_STYLUS = 2,
126         TOUCHSCREEN_FINGER = 3
127     };
128 
129     enum {
130         KEYBOARD_UNDEFINED = 0,
131         KEYBOARD_NOKEYS = 1,
132         KEYBOARD_QWERTY = 2,
133         KEYBOARD_12KEY = 3
134     };
135 
136     enum {
137         NAVIGATION_UNDEFINED = 0,
138         NAVIGATION_NONAV = 1,
139         NAVIGATION_DPAD = 2,
140         NAVIGATION_TRACKBALL = 3,
141         NAVIGATION_WHEEL = 4
142     };
143 
144     int32_t touchScreen;
145     int32_t keyboard;
146     int32_t navigation;
147 };
148 
149 /*
150  * Pointer coordinate data.
151  */
152 struct PointerCoords {
153     float x;
154     float y;
155     float pressure;
156     float size;
157     float touchMajor;
158     float touchMinor;
159     float toolMajor;
160     float toolMinor;
161     float orientation;
162 };
163 
164 /*
165  * Input events.
166  */
167 class InputEvent : public AInputEvent {
168 public:
~InputEvent()169     virtual ~InputEvent() { }
170 
171     virtual int32_t getType() const = 0;
172 
getDeviceId()173     inline int32_t getDeviceId() const { return mDeviceId; }
174 
getSource()175     inline int32_t getSource() const { return mSource; }
176 
177 protected:
178     void initialize(int32_t deviceId, int32_t source);
179     void initialize(const InputEvent& from);
180 
181 private:
182     int32_t mDeviceId;
183     int32_t mSource;
184 };
185 
186 /*
187  * Key events.
188  */
189 class KeyEvent : public InputEvent {
190 public:
~KeyEvent()191     virtual ~KeyEvent() { }
192 
getType()193     virtual int32_t getType() const { return AINPUT_EVENT_TYPE_KEY; }
194 
getAction()195     inline int32_t getAction() const { return mAction; }
196 
getFlags()197     inline int32_t getFlags() const { return mFlags; }
198 
getKeyCode()199     inline int32_t getKeyCode() const { return mKeyCode; }
200 
getScanCode()201     inline int32_t getScanCode() const { return mScanCode; }
202 
getMetaState()203     inline int32_t getMetaState() const { return mMetaState; }
204 
getRepeatCount()205     inline int32_t getRepeatCount() const { return mRepeatCount; }
206 
getDownTime()207     inline nsecs_t getDownTime() const { return mDownTime; }
208 
getEventTime()209     inline nsecs_t getEventTime() const { return mEventTime; }
210 
211     // Return true if this event may have a default action implementation.
212     static bool hasDefaultAction(int32_t keyCode);
213     bool hasDefaultAction() const;
214 
215     // Return true if this event represents a system key.
216     static bool isSystemKey(int32_t keyCode);
217     bool isSystemKey() const;
218 
219     void initialize(
220             int32_t deviceId,
221             int32_t source,
222             int32_t action,
223             int32_t flags,
224             int32_t keyCode,
225             int32_t scanCode,
226             int32_t metaState,
227             int32_t repeatCount,
228             nsecs_t downTime,
229             nsecs_t eventTime);
230     void initialize(const KeyEvent& from);
231 
232 private:
233     int32_t mAction;
234     int32_t mFlags;
235     int32_t mKeyCode;
236     int32_t mScanCode;
237     int32_t mMetaState;
238     int32_t mRepeatCount;
239     nsecs_t mDownTime;
240     nsecs_t mEventTime;
241 };
242 
243 /*
244  * Motion events.
245  */
246 class MotionEvent : public InputEvent {
247 public:
~MotionEvent()248     virtual ~MotionEvent() { }
249 
getType()250     virtual int32_t getType() const { return AINPUT_EVENT_TYPE_MOTION; }
251 
getAction()252     inline int32_t getAction() const { return mAction; }
253 
getFlags()254     inline int32_t getFlags() const { return mFlags; }
255 
getEdgeFlags()256     inline int32_t getEdgeFlags() const { return mEdgeFlags; }
257 
getMetaState()258     inline int32_t getMetaState() const { return mMetaState; }
259 
getXOffset()260     inline float getXOffset() const { return mXOffset; }
261 
getYOffset()262     inline float getYOffset() const { return mYOffset; }
263 
getXPrecision()264     inline float getXPrecision() const { return mXPrecision; }
265 
getYPrecision()266     inline float getYPrecision() const { return mYPrecision; }
267 
getDownTime()268     inline nsecs_t getDownTime() const { return mDownTime; }
269 
getPointerCount()270     inline size_t getPointerCount() const { return mPointerIds.size(); }
271 
getPointerId(size_t pointerIndex)272     inline int32_t getPointerId(size_t pointerIndex) const { return mPointerIds[pointerIndex]; }
273 
getEventTime()274     inline nsecs_t getEventTime() const { return mSampleEventTimes[getHistorySize()]; }
275 
getRawX(size_t pointerIndex)276     inline float getRawX(size_t pointerIndex) const {
277         return getCurrentPointerCoords(pointerIndex).x;
278     }
279 
getRawY(size_t pointerIndex)280     inline float getRawY(size_t pointerIndex) const {
281         return getCurrentPointerCoords(pointerIndex).y;
282     }
283 
getX(size_t pointerIndex)284     inline float getX(size_t pointerIndex) const {
285         return getRawX(pointerIndex) + mXOffset;
286     }
287 
getY(size_t pointerIndex)288     inline float getY(size_t pointerIndex) const {
289         return getRawY(pointerIndex) + mYOffset;
290     }
291 
getPressure(size_t pointerIndex)292     inline float getPressure(size_t pointerIndex) const {
293         return getCurrentPointerCoords(pointerIndex).pressure;
294     }
295 
getSize(size_t pointerIndex)296     inline float getSize(size_t pointerIndex) const {
297         return getCurrentPointerCoords(pointerIndex).size;
298     }
299 
getTouchMajor(size_t pointerIndex)300     inline float getTouchMajor(size_t pointerIndex) const {
301         return getCurrentPointerCoords(pointerIndex).touchMajor;
302     }
303 
getTouchMinor(size_t pointerIndex)304     inline float getTouchMinor(size_t pointerIndex) const {
305         return getCurrentPointerCoords(pointerIndex).touchMinor;
306     }
307 
getToolMajor(size_t pointerIndex)308     inline float getToolMajor(size_t pointerIndex) const {
309         return getCurrentPointerCoords(pointerIndex).toolMajor;
310     }
311 
getToolMinor(size_t pointerIndex)312     inline float getToolMinor(size_t pointerIndex) const {
313         return getCurrentPointerCoords(pointerIndex).toolMinor;
314     }
315 
getOrientation(size_t pointerIndex)316     inline float getOrientation(size_t pointerIndex) const {
317         return getCurrentPointerCoords(pointerIndex).orientation;
318     }
319 
getHistorySize()320     inline size_t getHistorySize() const { return mSampleEventTimes.size() - 1; }
321 
getHistoricalEventTime(size_t historicalIndex)322     inline nsecs_t getHistoricalEventTime(size_t historicalIndex) const {
323         return mSampleEventTimes[historicalIndex];
324     }
325 
getHistoricalRawX(size_t pointerIndex,size_t historicalIndex)326     inline float getHistoricalRawX(size_t pointerIndex, size_t historicalIndex) const {
327         return getHistoricalPointerCoords(pointerIndex, historicalIndex).x;
328     }
329 
getHistoricalRawY(size_t pointerIndex,size_t historicalIndex)330     inline float getHistoricalRawY(size_t pointerIndex, size_t historicalIndex) const {
331         return getHistoricalPointerCoords(pointerIndex, historicalIndex).y;
332     }
333 
getHistoricalX(size_t pointerIndex,size_t historicalIndex)334     inline float getHistoricalX(size_t pointerIndex, size_t historicalIndex) const {
335         return getHistoricalRawX(pointerIndex, historicalIndex) + mXOffset;
336     }
337 
getHistoricalY(size_t pointerIndex,size_t historicalIndex)338     inline float getHistoricalY(size_t pointerIndex, size_t historicalIndex) const {
339         return getHistoricalRawY(pointerIndex, historicalIndex) + mYOffset;
340     }
341 
getHistoricalPressure(size_t pointerIndex,size_t historicalIndex)342     inline float getHistoricalPressure(size_t pointerIndex, size_t historicalIndex) const {
343         return getHistoricalPointerCoords(pointerIndex, historicalIndex).pressure;
344     }
345 
getHistoricalSize(size_t pointerIndex,size_t historicalIndex)346     inline float getHistoricalSize(size_t pointerIndex, size_t historicalIndex) const {
347         return getHistoricalPointerCoords(pointerIndex, historicalIndex).size;
348     }
349 
getHistoricalTouchMajor(size_t pointerIndex,size_t historicalIndex)350     inline float getHistoricalTouchMajor(size_t pointerIndex, size_t historicalIndex) const {
351         return getHistoricalPointerCoords(pointerIndex, historicalIndex).touchMajor;
352     }
353 
getHistoricalTouchMinor(size_t pointerIndex,size_t historicalIndex)354     inline float getHistoricalTouchMinor(size_t pointerIndex, size_t historicalIndex) const {
355         return getHistoricalPointerCoords(pointerIndex, historicalIndex).touchMinor;
356     }
357 
getHistoricalToolMajor(size_t pointerIndex,size_t historicalIndex)358     inline float getHistoricalToolMajor(size_t pointerIndex, size_t historicalIndex) const {
359         return getHistoricalPointerCoords(pointerIndex, historicalIndex).toolMajor;
360     }
361 
getHistoricalToolMinor(size_t pointerIndex,size_t historicalIndex)362     inline float getHistoricalToolMinor(size_t pointerIndex, size_t historicalIndex) const {
363         return getHistoricalPointerCoords(pointerIndex, historicalIndex).toolMinor;
364     }
365 
getHistoricalOrientation(size_t pointerIndex,size_t historicalIndex)366     inline float getHistoricalOrientation(size_t pointerIndex, size_t historicalIndex) const {
367         return getHistoricalPointerCoords(pointerIndex, historicalIndex).orientation;
368     }
369 
370     void initialize(
371             int32_t deviceId,
372             int32_t source,
373             int32_t action,
374             int32_t flags,
375             int32_t edgeFlags,
376             int32_t metaState,
377             float xOffset,
378             float yOffset,
379             float xPrecision,
380             float yPrecision,
381             nsecs_t downTime,
382             nsecs_t eventTime,
383             size_t pointerCount,
384             const int32_t* pointerIds,
385             const PointerCoords* pointerCoords);
386 
387     void addSample(
388             nsecs_t eventTime,
389             const PointerCoords* pointerCoords);
390 
391     void offsetLocation(float xOffset, float yOffset);
392 
393     // Low-level accessors.
getPointerIds()394     inline const int32_t* getPointerIds() const { return mPointerIds.array(); }
getSampleEventTimes()395     inline const nsecs_t* getSampleEventTimes() const { return mSampleEventTimes.array(); }
getSamplePointerCoords()396     inline const PointerCoords* getSamplePointerCoords() const {
397             return mSamplePointerCoords.array();
398     }
399 
400 private:
401     int32_t mAction;
402     int32_t mFlags;
403     int32_t mEdgeFlags;
404     int32_t mMetaState;
405     float mXOffset;
406     float mYOffset;
407     float mXPrecision;
408     float mYPrecision;
409     nsecs_t mDownTime;
410     Vector<int32_t> mPointerIds;
411     Vector<nsecs_t> mSampleEventTimes;
412     Vector<PointerCoords> mSamplePointerCoords;
413 
getCurrentPointerCoords(size_t pointerIndex)414     inline const PointerCoords& getCurrentPointerCoords(size_t pointerIndex) const {
415         return mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex];
416     }
417 
getHistoricalPointerCoords(size_t pointerIndex,size_t historicalIndex)418     inline const PointerCoords& getHistoricalPointerCoords(
419             size_t pointerIndex, size_t historicalIndex) const {
420         return mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex];
421     }
422 };
423 
424 /*
425  * Input event factory.
426  */
427 class InputEventFactoryInterface {
428 protected:
~InputEventFactoryInterface()429     virtual ~InputEventFactoryInterface() { }
430 
431 public:
InputEventFactoryInterface()432     InputEventFactoryInterface() { }
433 
434     virtual KeyEvent* createKeyEvent() = 0;
435     virtual MotionEvent* createMotionEvent() = 0;
436 };
437 
438 /*
439  * A simple input event factory implementation that uses a single preallocated instance
440  * of each type of input event that are reused for each request.
441  */
442 class PreallocatedInputEventFactory : public InputEventFactoryInterface {
443 public:
PreallocatedInputEventFactory()444     PreallocatedInputEventFactory() { }
~PreallocatedInputEventFactory()445     virtual ~PreallocatedInputEventFactory() { }
446 
createKeyEvent()447     virtual KeyEvent* createKeyEvent() { return & mKeyEvent; }
createMotionEvent()448     virtual MotionEvent* createMotionEvent() { return & mMotionEvent; }
449 
450 private:
451     KeyEvent mKeyEvent;
452     MotionEvent mMotionEvent;
453 };
454 
455 /*
456  * Describes the characteristics and capabilities of an input device.
457  */
458 class InputDeviceInfo {
459 public:
460     InputDeviceInfo();
461     InputDeviceInfo(const InputDeviceInfo& other);
462     ~InputDeviceInfo();
463 
464     struct MotionRange {
465         float min;
466         float max;
467         float flat;
468         float fuzz;
469     };
470 
471     void initialize(int32_t id, const String8& name);
472 
getId()473     inline int32_t getId() const { return mId; }
getName()474     inline const String8 getName() const { return mName; }
getSources()475     inline uint32_t getSources() const { return mSources; }
476 
477     const MotionRange* getMotionRange(int32_t rangeType) const;
478 
479     void addSource(uint32_t source);
480     void addMotionRange(int32_t rangeType, float min, float max, float flat, float fuzz);
481     void addMotionRange(int32_t rangeType, const MotionRange& range);
482 
setKeyboardType(int32_t keyboardType)483     inline void setKeyboardType(int32_t keyboardType) { mKeyboardType = keyboardType; }
getKeyboardType()484     inline int32_t getKeyboardType() const { return mKeyboardType; }
485 
getMotionRanges()486     inline const KeyedVector<int32_t, MotionRange> getMotionRanges() const {
487         return mMotionRanges;
488     }
489 
490 private:
491     int32_t mId;
492     String8 mName;
493     uint32_t mSources;
494     int32_t mKeyboardType;
495 
496     KeyedVector<int32_t, MotionRange> mMotionRanges;
497 };
498 
499 
500 } // namespace android
501 
502 #endif // _UI_INPUT_H
503