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