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