widget/gonk/libui/EventHub.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial