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 /* 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 89 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 is virtual (not a real device, not part of UI configuration). */ 134 INPUT_DEVICE_CLASS_VIRTUAL = 0x40000000, 135 136 /* The input device is external (not built-in). */ 137 INPUT_DEVICE_CLASS_EXTERNAL = 0x80000000, 138 }; 139 140 /* 141 * Gets the class that owns an axis, in cases where multiple classes might claim 142 * the same axis for different purposes. 143 */ 144 extern uint32_t getAbsAxisUsage(int32_t axis, uint32_t deviceClasses); 145 146 /* 147 * Grand Central Station for events. 148 * 149 * The event hub aggregates input events received across all known input 150 * devices on the system, including devices that may be emulated by the simulator 151 * environment. In addition, the event hub generates fake input events to indicate 152 * when devices are added or removed. 153 * 154 * The event hub provides a stream of input events (via the getEvent function). 155 * It also supports querying the current actual state of input devices such as identifying 156 * which keys are currently down. Finally, the event hub keeps track of the capabilities of 157 * individual input devices, such as their class and the set of key codes that they support. 158 */ 159 class EventHubInterface : public virtual RefBase { 160 protected: 161 EventHubInterface() { } 162 virtual ~EventHubInterface() { } 163 164 public: 165 // Synthetic raw event type codes produced when devices are added or removed. 166 enum { 167 // Sent when a device is added. 168 DEVICE_ADDED = 0x10000000, 169 // Sent when a device is removed. 170 DEVICE_REMOVED = 0x20000000, 171 // Sent when all added/removed devices from the most recent scan have been reported. 172 // This event is always sent at least once. 173 FINISHED_DEVICE_SCAN = 0x30000000, 174 175 FIRST_SYNTHETIC_EVENT = DEVICE_ADDED, 176 }; 177 178 virtual uint32_t getDeviceClasses(int32_t deviceId) const = 0; 179 180 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const = 0; 181 182 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const = 0; 183 184 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis, 185 RawAbsoluteAxisInfo* outAxisInfo) const = 0; 186 187 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const = 0; 188 189 virtual bool hasInputProperty(int32_t deviceId, int property) const = 0; 190 191 virtual status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, 192 int32_t* outKeycode, uint32_t* outFlags) const = 0; 193 194 virtual status_t mapAxis(int32_t deviceId, int32_t scanCode, 195 AxisInfo* outAxisInfo) const = 0; 196 197 // Sets devices that are excluded from opening. 198 // This can be used to ignore input devices for sensors. 199 virtual void setExcludedDevices(const Vector<String8>& devices) = 0; 200 201 /* 202 * Wait for events to become available and returns them. 203 * After returning, the EventHub holds onto a wake lock until the next call to getEvent. 204 * This ensures that the device will not go to sleep while the event is being processed. 205 * If the device needs to remain awake longer than that, then the caller is responsible 206 * for taking care of it (say, by poking the power manager user activity timer). 207 * 208 * The timeout is advisory only. If the device is asleep, it will not wake just to 209 * service the timeout. 210 * 211 * Returns the number of events obtained, or 0 if the timeout expired. 212 */ 213 virtual size_t getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) = 0; 214 215 /* 216 * Query current input state. 217 */ 218 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const = 0; 219 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const = 0; 220 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const = 0; 221 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis, 222 int32_t* outValue) const = 0; 223 224 /* 225 * Examine key input devices for specific framework keycode support 226 */ 227 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes, 228 uint8_t* outFlags) const = 0; 229 230 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const = 0; 231 virtual bool hasLed(int32_t deviceId, int32_t led) const = 0; 232 virtual void setLedState(int32_t deviceId, int32_t led, bool on) = 0; 233 234 virtual void getVirtualKeyDefinitions(int32_t deviceId, 235 Vector<VirtualKeyDefinition>& outVirtualKeys) const = 0; 236 237 virtual sp<KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const = 0; 238 virtual bool setKeyboardLayoutOverlay(int32_t deviceId, const sp<KeyCharacterMap>& map) = 0; 239 240 /* Control the vibrator. */ 241 virtual void vibrate(int32_t deviceId, nsecs_t duration) = 0; 242 virtual void cancelVibrate(int32_t deviceId) = 0; 243 244 /* Requests the EventHub to reopen all input devices on the next call to getEvents(). */ 245 virtual void requestReopenDevices() = 0; 246 247 /* Wakes up getEvents() if it is blocked on a read. */ 248 virtual void wake() = 0; 249 250 /* Dump EventHub state to a string. */ 251 virtual void dump(String8& dump) = 0; 252 253 /* Called by the heatbeat to ensures that the reader has not deadlocked. */ 254 virtual void monitor() = 0; 255 }; 256 257 class EventHub : public EventHubInterface 258 { 259 public: 260 EventHub(); 261 262 virtual uint32_t getDeviceClasses(int32_t deviceId) const; 263 264 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const; 265 266 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const; 267 268 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis, 269 RawAbsoluteAxisInfo* outAxisInfo) const; 270 271 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const; 272 273 virtual bool hasInputProperty(int32_t deviceId, int property) const; 274 275 virtual status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, 276 int32_t* outKeycode, uint32_t* outFlags) const; 277 278 virtual status_t mapAxis(int32_t deviceId, int32_t scanCode, 279 AxisInfo* outAxisInfo) const; 280 281 virtual void setExcludedDevices(const Vector<String8>& devices); 282 283 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const; 284 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const; 285 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const; 286 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t* outValue) const; 287 288 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, 289 const int32_t* keyCodes, uint8_t* outFlags) const; 290 291 virtual size_t getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize); 292 293 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const; 294 virtual bool hasLed(int32_t deviceId, int32_t led) const; 295 virtual void setLedState(int32_t deviceId, int32_t led, bool on); 296 297 virtual void getVirtualKeyDefinitions(int32_t deviceId, 298 Vector<VirtualKeyDefinition>& outVirtualKeys) const; 299 300 virtual sp<KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const; 301 virtual bool setKeyboardLayoutOverlay(int32_t deviceId, const sp<KeyCharacterMap>& map); 302 303 virtual void vibrate(int32_t deviceId, nsecs_t duration); 304 virtual void cancelVibrate(int32_t deviceId); 305 306 virtual void requestReopenDevices(); 307 308 virtual void wake(); 309 310 virtual void dump(String8& dump); 311 virtual void monitor(); 312 313 protected: 314 virtual ~EventHub(); 315 316 private: 317 struct Device { 318 Device* next; 319 320 int fd; // may be -1 if device is virtual 321 const int32_t id; 322 const String8 path; 323 const InputDeviceIdentifier identifier; 324 325 uint32_t classes; 326 327 uint8_t keyBitmask[(KEY_MAX + 1) / 8]; 328 uint8_t absBitmask[(ABS_MAX + 1) / 8]; 329 uint8_t relBitmask[(REL_MAX + 1) / 8]; 330 uint8_t swBitmask[(SW_MAX + 1) / 8]; 331 uint8_t ledBitmask[(LED_MAX + 1) / 8]; 332 uint8_t ffBitmask[(FF_MAX + 1) / 8]; 333 uint8_t propBitmask[(INPUT_PROP_MAX + 1) / 8]; 334 335 String8 configurationFile; 336 PropertyMap* configuration; 337 VirtualKeyMap* virtualKeyMap; 338 KeyMap keyMap; 339 340 sp<KeyCharacterMap> overlayKeyMap; 341 sp<KeyCharacterMap> combinedKeyMap; 342 343 bool ffEffectPlaying; 344 int16_t ffEffectId; // initially -1 345 346 int32_t timestampOverrideSec; 347 int32_t timestampOverrideUsec; 348 349 Device(int fd, int32_t id, const String8& path, const InputDeviceIdentifier& identifier); 350 ~Device(); 351 352 void close(); 353 354 inline bool isVirtual() const { return fd < 0; } 355 356 const sp<KeyCharacterMap>& getKeyCharacterMap() const { 357 if (combinedKeyMap != NULL) { 358 return combinedKeyMap; 359 } 360 return keyMap.keyCharacterMap; 361 } 362 }; 363 364 status_t openDeviceLocked(const char *devicePath); 365 void createVirtualKeyboardLocked(); 366 void addDeviceLocked(Device* device); 367 368 status_t closeDeviceByPathLocked(const char *devicePath); 369 void closeDeviceLocked(Device* device); 370 void closeAllDevicesLocked(); 371 372 status_t scanDirLocked(const char *dirname); 373 void scanDevicesLocked(); 374 status_t readNotifyLocked(); 375 376 Device* getDeviceLocked(int32_t deviceId) const; 377 Device* getDeviceByPathLocked(const char* devicePath) const; 378 379 bool hasKeycodeLocked(Device* device, int keycode) const; 380 381 void loadConfigurationLocked(Device* device); 382 status_t loadVirtualKeyMapLocked(Device* device); 383 status_t loadKeyMapLocked(Device* device); 384 385 bool isExternalDeviceLocked(Device* device); 386 387 // Protect all internal state. 388 mutable Mutex mLock; 389 390 // The actual id of the built-in keyboard, or NO_BUILT_IN_KEYBOARD if none. 391 // EventHub remaps the built-in keyboard to id 0 externally as required by the API. 392 enum { 393 // Must not conflict with any other assigned device ids, including 394 // the virtual keyboard id (-1). 395 NO_BUILT_IN_KEYBOARD = -2, 396 }; 397 int32_t mBuiltInKeyboardId; 398 399 int32_t mNextDeviceId; 400 401 KeyedVector<int32_t, Device*> mDevices; 402 403 Device *mOpeningDevices; 404 Device *mClosingDevices; 405 406 bool mNeedToSendFinishedDeviceScan; 407 bool mNeedToReopenDevices; 408 bool mNeedToScanDevices; 409 Vector<String8> mExcludedDevices; 410 411 int mEpollFd; 412 int mINotifyFd; 413 int mWakeReadPipeFd; 414 int mWakeWritePipeFd; 415 416 // Ids used for epoll notifications not associated with devices. 417 static const uint32_t EPOLL_ID_INOTIFY = 0x80000001; 418 static const uint32_t EPOLL_ID_WAKE = 0x80000002; 419 420 // Epoll FD list size hint. 421 static const int EPOLL_SIZE_HINT = 8; 422 423 // Maximum number of signalled FDs to handle at a time. 424 static const int EPOLL_MAX_EVENTS = 16; 425 426 // The array of pending epoll events and the index of the next event to be handled. 427 struct epoll_event mPendingEventItems[EPOLL_MAX_EVENTS]; 428 size_t mPendingEventCount; 429 size_t mPendingEventIndex; 430 bool mPendingINotify; 431 }; 432 433 }; // namespace android 434 435 #endif // _RUNTIME_EVENT_HUB_H 436