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