EventHub.h 16 KB

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