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