Home | History | Annotate | Download | only in input
      1 /*
      2  * Copyright (C) 2005 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 //
     18 #ifndef _RUNTIME_EVENT_HUB_H
     19 #define _RUNTIME_EVENT_HUB_H
     20 
     21 #include <androidfw/Input.h>
     22 #include <androidfw/InputDevice.h>
     23 #include <androidfw/Keyboard.h>
     24 #include <androidfw/KeyLayoutMap.h>
     25 #include <androidfw/KeyCharacterMap.h>
     26 #include <androidfw/VirtualKeyMap.h>
     27 #include <utils/String8.h>
     28 #include <utils/threads.h>
     29 #include <utils/Log.h>
     30 #include <utils/threads.h>
     31 #include <utils/List.h>
     32 #include <utils/Errors.h>
     33 #include <utils/PropertyMap.h>
     34 #include <utils/Vector.h>
     35 #include <utils/KeyedVector.h>
     36 
     37 #include <linux/input.h>
     38 #include <sys/epoll.h>
     39 
     40 /* Convenience constants. */
     41 
     42 #define BTN_FIRST 0x100  // first button code
     43 #define BTN_LAST 0x15f   // last button code
     44 
     45 namespace android {
     46 
     47 enum {
     48     // Device id of a special "virtual" keyboard that is always present.
     49     VIRTUAL_KEYBOARD_ID = -1,
     50     // Device id of the "built-in" keyboard if there is one.
     51     BUILT_IN_KEYBOARD_ID = 0,
     52 };
     53 
     54 /*
     55  * A raw event as retrieved from the EventHub.
     56  */
     57 struct RawEvent {
     58     nsecs_t when;
     59     int32_t deviceId;
     60     int32_t type;
     61     int32_t code;
     62     int32_t value;
     63 };
     64 
     65 /* Describes an absolute axis. */
     66 struct RawAbsoluteAxisInfo {
     67     bool valid; // true if the information is valid, false otherwise
     68 
     69     int32_t minValue;  // minimum value
     70     int32_t maxValue;  // maximum value
     71     int32_t flat;      // center flat position, eg. flat == 8 means center is between -8 and 8
     72     int32_t fuzz;      // error tolerance, eg. fuzz == 4 means value is +/- 4 due to noise
     73     int32_t resolution; // resolution in units per mm or radians per mm
     74 
     75     inline void clear() {
     76         valid = false;
     77         minValue = 0;
     78         maxValue = 0;
     79         flat = 0;
     80         fuzz = 0;
     81         resolution = 0;
     82     }
     83 };
     84 
     85 /*
     86  * Input device classes.
     87  */
     88 enum {
     89     /* The input device is a keyboard or has buttons. */
     90     INPUT_DEVICE_CLASS_KEYBOARD      = 0x00000001,
     91 
     92     /* The input device is an alpha-numeric keyboard (not just a dial pad). */
     93     INPUT_DEVICE_CLASS_ALPHAKEY      = 0x00000002,
     94 
     95     /* The input device is a touchscreen or a touchpad (either single-touch or multi-touch). */
     96     INPUT_DEVICE_CLASS_TOUCH         = 0x00000004,
     97 
     98     /* The input device is a cursor device such as a trackball or mouse. */
     99     INPUT_DEVICE_CLASS_CURSOR        = 0x00000008,
    100 
    101     /* The input device is a multi-touch touchscreen. */
    102     INPUT_DEVICE_CLASS_TOUCH_MT      = 0x00000010,
    103 
    104     /* The input device is a directional pad (implies keyboard, has DPAD keys). */
    105     INPUT_DEVICE_CLASS_DPAD          = 0x00000020,
    106 
    107     /* The input device is a gamepad (implies keyboard, has BUTTON keys). */
    108     INPUT_DEVICE_CLASS_GAMEPAD       = 0x00000040,
    109 
    110     /* The input device has switches. */
    111     INPUT_DEVICE_CLASS_SWITCH        = 0x00000080,
    112 
    113     /* The input device is a joystick (implies gamepad, has joystick absolute axes). */
    114     INPUT_DEVICE_CLASS_JOYSTICK      = 0x00000100,
    115 
    116     /* The input device has a vibrator (supports FF_RUMBLE). */
    117     INPUT_DEVICE_CLASS_VIBRATOR      = 0x00000200,
    118 
    119     /* The input device is virtual (not a real device, not part of UI configuration). */
    120     INPUT_DEVICE_CLASS_VIRTUAL       = 0x40000000,
    121 
    122     /* The input device is external (not built-in). */
    123     INPUT_DEVICE_CLASS_EXTERNAL      = 0x80000000,
    124 };
    125 
    126 /*
    127  * Gets the class that owns an axis, in cases where multiple classes might claim
    128  * the same axis for different purposes.
    129  */
    130 extern uint32_t getAbsAxisUsage(int32_t axis, uint32_t deviceClasses);
    131 
    132 /*
    133  * Grand Central Station for events.
    134  *
    135  * The event hub aggregates input events received across all known input
    136  * devices on the system, including devices that may be emulated by the simulator
    137  * environment.  In addition, the event hub generates fake input events to indicate
    138  * when devices are added or removed.
    139  *
    140  * The event hub provides a stream of input events (via the getEvent function).
    141  * It also supports querying the current actual state of input devices such as identifying
    142  * which keys are currently down.  Finally, the event hub keeps track of the capabilities of
    143  * individual input devices, such as their class and the set of key codes that they support.
    144  */
    145 class EventHubInterface : public virtual RefBase {
    146 protected:
    147     EventHubInterface() { }
    148     virtual ~EventHubInterface() { }
    149 
    150 public:
    151     // Synthetic raw event type codes produced when devices are added or removed.
    152     enum {
    153         // Sent when a device is added.
    154         DEVICE_ADDED = 0x10000000,
    155         // Sent when a device is removed.
    156         DEVICE_REMOVED = 0x20000000,
    157         // Sent when all added/removed devices from the most recent scan have been reported.
    158         // This event is always sent at least once.
    159         FINISHED_DEVICE_SCAN = 0x30000000,
    160 
    161         FIRST_SYNTHETIC_EVENT = DEVICE_ADDED,
    162     };
    163 
    164     virtual uint32_t getDeviceClasses(int32_t deviceId) const = 0;
    165 
    166     virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const = 0;
    167 
    168     virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const = 0;
    169 
    170     virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
    171             RawAbsoluteAxisInfo* outAxisInfo) const = 0;
    172 
    173     virtual bool hasRelativeAxis(int32_t deviceId, int axis) const = 0;
    174 
    175     virtual bool hasInputProperty(int32_t deviceId, int property) const = 0;
    176 
    177     virtual status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
    178             int32_t* outKeycode, uint32_t* outFlags) const = 0;
    179 
    180     virtual status_t mapAxis(int32_t deviceId, int32_t scanCode,
    181             AxisInfo* outAxisInfo) const = 0;
    182 
    183     // Sets devices that are excluded from opening.
    184     // This can be used to ignore input devices for sensors.
    185     virtual void setExcludedDevices(const Vector<String8>& devices) = 0;
    186 
    187     /*
    188      * Wait for events to become available and returns them.
    189      * After returning, the EventHub holds onto a wake lock until the next call to getEvent.
    190      * This ensures that the device will not go to sleep while the event is being processed.
    191      * If the device needs to remain awake longer than that, then the caller is responsible
    192      * for taking care of it (say, by poking the power manager user activity timer).
    193      *
    194      * The timeout is advisory only.  If the device is asleep, it will not wake just to
    195      * service the timeout.
    196      *
    197      * Returns the number of events obtained, or 0 if the timeout expired.
    198      */
    199     virtual size_t getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) = 0;
    200 
    201     /*
    202      * Query current input state.
    203      */
    204     virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const = 0;
    205     virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const = 0;
    206     virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const = 0;
    207     virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
    208             int32_t* outValue) const = 0;
    209 
    210     /*
    211      * Examine key input devices for specific framework keycode support
    212      */
    213     virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
    214             uint8_t* outFlags) const = 0;
    215 
    216     virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const = 0;
    217     virtual bool hasLed(int32_t deviceId, int32_t led) const = 0;
    218     virtual void setLedState(int32_t deviceId, int32_t led, bool on) = 0;
    219 
    220     virtual void getVirtualKeyDefinitions(int32_t deviceId,
    221             Vector<VirtualKeyDefinition>& outVirtualKeys) const = 0;
    222 
    223     virtual sp<KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const = 0;
    224     virtual bool setKeyboardLayoutOverlay(int32_t deviceId, const sp<KeyCharacterMap>& map) = 0;
    225 
    226     /* Control the vibrator. */
    227     virtual void vibrate(int32_t deviceId, nsecs_t duration) = 0;
    228     virtual void cancelVibrate(int32_t deviceId) = 0;
    229 
    230     /* Requests the EventHub to reopen all input devices on the next call to getEvents(). */
    231     virtual void requestReopenDevices() = 0;
    232 
    233     /* Wakes up getEvents() if it is blocked on a read. */
    234     virtual void wake() = 0;
    235 
    236     /* Dump EventHub state to a string. */
    237     virtual void dump(String8& dump) = 0;
    238 
    239     /* Called by the heatbeat to ensures that the reader has not deadlocked. */
    240     virtual void monitor() = 0;
    241 };
    242 
    243 class EventHub : public EventHubInterface
    244 {
    245 public:
    246     EventHub();
    247 
    248     virtual uint32_t getDeviceClasses(int32_t deviceId) const;
    249 
    250     virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const;
    251 
    252     virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const;
    253 
    254     virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
    255             RawAbsoluteAxisInfo* outAxisInfo) const;
    256 
    257     virtual bool hasRelativeAxis(int32_t deviceId, int axis) const;
    258 
    259     virtual bool hasInputProperty(int32_t deviceId, int property) const;
    260 
    261     virtual status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
    262             int32_t* outKeycode, uint32_t* outFlags) const;
    263 
    264     virtual status_t mapAxis(int32_t deviceId, int32_t scanCode,
    265             AxisInfo* outAxisInfo) const;
    266 
    267     virtual void setExcludedDevices(const Vector<String8>& devices);
    268 
    269     virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const;
    270     virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const;
    271     virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const;
    272     virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t* outValue) const;
    273 
    274     virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes,
    275             const int32_t* keyCodes, uint8_t* outFlags) const;
    276 
    277     virtual size_t getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize);
    278 
    279     virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const;
    280     virtual bool hasLed(int32_t deviceId, int32_t led) const;
    281     virtual void setLedState(int32_t deviceId, int32_t led, bool on);
    282 
    283     virtual void getVirtualKeyDefinitions(int32_t deviceId,
    284             Vector<VirtualKeyDefinition>& outVirtualKeys) const;
    285 
    286     virtual sp<KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const;
    287     virtual bool setKeyboardLayoutOverlay(int32_t deviceId, const sp<KeyCharacterMap>& map);
    288 
    289     virtual void vibrate(int32_t deviceId, nsecs_t duration);
    290     virtual void cancelVibrate(int32_t deviceId);
    291 
    292     virtual void requestReopenDevices();
    293 
    294     virtual void wake();
    295 
    296     virtual void dump(String8& dump);
    297     virtual void monitor();
    298 
    299 protected:
    300     virtual ~EventHub();
    301 
    302 private:
    303     struct Device {
    304         Device* next;
    305 
    306         int fd; // may be -1 if device is virtual
    307         const int32_t id;
    308         const String8 path;
    309         const InputDeviceIdentifier identifier;
    310 
    311         uint32_t classes;
    312 
    313         uint8_t keyBitmask[(KEY_MAX + 1) / 8];
    314         uint8_t absBitmask[(ABS_MAX + 1) / 8];
    315         uint8_t relBitmask[(REL_MAX + 1) / 8];
    316         uint8_t swBitmask[(SW_MAX + 1) / 8];
    317         uint8_t ledBitmask[(LED_MAX + 1) / 8];
    318         uint8_t ffBitmask[(FF_MAX + 1) / 8];
    319         uint8_t propBitmask[(INPUT_PROP_MAX + 1) / 8];
    320 
    321         String8 configurationFile;
    322         PropertyMap* configuration;
    323         VirtualKeyMap* virtualKeyMap;
    324         KeyMap keyMap;
    325 
    326         sp<KeyCharacterMap> overlayKeyMap;
    327         sp<KeyCharacterMap> combinedKeyMap;
    328 
    329         bool ffEffectPlaying;
    330         int16_t ffEffectId; // initially -1
    331 
    332         Device(int fd, int32_t id, const String8& path, const InputDeviceIdentifier& identifier);
    333         ~Device();
    334 
    335         void close();
    336 
    337         inline bool isVirtual() const { return fd < 0; }
    338 
    339         const sp<KeyCharacterMap>& getKeyCharacterMap() const {
    340             if (combinedKeyMap != NULL) {
    341                 return combinedKeyMap;
    342             }
    343             return keyMap.keyCharacterMap;
    344         }
    345     };
    346 
    347     status_t openDeviceLocked(const char *devicePath);
    348     void createVirtualKeyboardLocked();
    349     void addDeviceLocked(Device* device);
    350 
    351     status_t closeDeviceByPathLocked(const char *devicePath);
    352     void closeDeviceLocked(Device* device);
    353     void closeAllDevicesLocked();
    354 
    355     status_t scanDirLocked(const char *dirname);
    356     void scanDevicesLocked();
    357     status_t readNotifyLocked();
    358 
    359     Device* getDeviceLocked(int32_t deviceId) const;
    360     Device* getDeviceByPathLocked(const char* devicePath) const;
    361 
    362     bool hasKeycodeLocked(Device* device, int keycode) const;
    363 
    364     void loadConfigurationLocked(Device* device);
    365     status_t loadVirtualKeyMapLocked(Device* device);
    366     status_t loadKeyMapLocked(Device* device);
    367 
    368     bool isExternalDeviceLocked(Device* device);
    369 
    370     // Protect all internal state.
    371     mutable Mutex mLock;
    372 
    373     // The actual id of the built-in keyboard, or NO_BUILT_IN_KEYBOARD if none.
    374     // EventHub remaps the built-in keyboard to id 0 externally as required by the API.
    375     enum {
    376         // Must not conflict with any other assigned device ids, including
    377         // the virtual keyboard id (-1).
    378         NO_BUILT_IN_KEYBOARD = -2,
    379     };
    380     int32_t mBuiltInKeyboardId;
    381 
    382     int32_t mNextDeviceId;
    383 
    384     KeyedVector<int32_t, Device*> mDevices;
    385 
    386     Device *mOpeningDevices;
    387     Device *mClosingDevices;
    388 
    389     bool mNeedToSendFinishedDeviceScan;
    390     bool mNeedToReopenDevices;
    391     bool mNeedToScanDevices;
    392     Vector<String8> mExcludedDevices;
    393 
    394     int mEpollFd;
    395     int mINotifyFd;
    396     int mWakeReadPipeFd;
    397     int mWakeWritePipeFd;
    398 
    399     // Ids used for epoll notifications not associated with devices.
    400     static const uint32_t EPOLL_ID_INOTIFY = 0x80000001;
    401     static const uint32_t EPOLL_ID_WAKE = 0x80000002;
    402 
    403     // Epoll FD list size hint.
    404     static const int EPOLL_SIZE_HINT = 8;
    405 
    406     // Maximum number of signalled FDs to handle at a time.
    407     static const int EPOLL_MAX_EVENTS = 16;
    408 
    409     // The array of pending epoll events and the index of the next event to be handled.
    410     struct epoll_event mPendingEventItems[EPOLL_MAX_EVENTS];
    411     size_t mPendingEventCount;
    412     size_t mPendingEventIndex;
    413     bool mPendingINotify;
    414 };
    415 
    416 }; // namespace android
    417 
    418 #endif // _RUNTIME_EVENT_HUB_H
    419