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