EventHub.h revision b3a2d1330716812784aee91b6d6275764b5e4210
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 35#include <linux/input.h> 36 37/* Convenience constants. */ 38 39#define BTN_FIRST 0x100 // first button scancode 40#define BTN_LAST 0x15f // last button scancode 41 42struct pollfd; 43 44namespace android { 45 46/* 47 * A raw event as retrieved from the EventHub. 48 */ 49struct 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. */ 60struct 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 */ 82enum { 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 * Grand Central Station for events. 116 * 117 * The event hub aggregates input events received across all known input 118 * devices on the system, including devices that may be emulated by the simulator 119 * environment. In addition, the event hub generates fake input events to indicate 120 * when devices are added or removed. 121 * 122 * The event hub provides a stream of input events (via the getEvent function). 123 * It also supports querying the current actual state of input devices such as identifying 124 * which keys are currently down. Finally, the event hub keeps track of the capabilities of 125 * individual input devices, such as their class and the set of key codes that they support. 126 */ 127class EventHubInterface : public virtual RefBase { 128protected: 129 EventHubInterface() { } 130 virtual ~EventHubInterface() { } 131 132public: 133 // Synthetic raw event type codes produced when devices are added or removed. 134 enum { 135 // Sent when a device is added. 136 DEVICE_ADDED = 0x10000000, 137 // Sent when a device is removed. 138 DEVICE_REMOVED = 0x20000000, 139 // Sent when all added/removed devices from the most recent scan have been reported. 140 // This event is always sent at least once. 141 FINISHED_DEVICE_SCAN = 0x30000000, 142 143 FIRST_SYNTHETIC_EVENT = DEVICE_ADDED, 144 }; 145 146 virtual uint32_t getDeviceClasses(int32_t deviceId) const = 0; 147 148 virtual String8 getDeviceName(int32_t deviceId) const = 0; 149 150 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const = 0; 151 152 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis, 153 RawAbsoluteAxisInfo* outAxisInfo) const = 0; 154 155 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const = 0; 156 157 virtual bool hasInputProperty(int32_t deviceId, int property) const = 0; 158 159 virtual status_t mapKey(int32_t deviceId, int scancode, 160 int32_t* outKeycode, uint32_t* outFlags) const = 0; 161 162 virtual status_t mapAxis(int32_t deviceId, int scancode, 163 AxisInfo* outAxisInfo) const = 0; 164 165 // Sets devices that are excluded from opening. 166 // This can be used to ignore input devices for sensors. 167 virtual void setExcludedDevices(const Vector<String8>& devices) = 0; 168 169 /* 170 * Wait for events to become available and returns them. 171 * After returning, the EventHub holds onto a wake lock until the next call to getEvent. 172 * This ensures that the device will not go to sleep while the event is being processed. 173 * If the device needs to remain awake longer than that, then the caller is responsible 174 * for taking care of it (say, by poking the power manager user activity timer). 175 * 176 * The timeout is advisory only. If the device is asleep, it will not wake just to 177 * service the timeout. 178 * 179 * Returns the number of events obtained, or 0 if the timeout expired. 180 */ 181 virtual size_t getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) = 0; 182 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 190 /* 191 * Examine key input devices for specific framework keycode support 192 */ 193 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes, 194 uint8_t* outFlags) const = 0; 195 196 virtual bool hasLed(int32_t deviceId, int32_t led) const = 0; 197 virtual void setLedState(int32_t deviceId, int32_t led, bool on) = 0; 198 199 virtual void getVirtualKeyDefinitions(int32_t deviceId, 200 Vector<VirtualKeyDefinition>& outVirtualKeys) const = 0; 201 202 virtual void reopenDevices() = 0; 203 204 virtual void dump(String8& dump) = 0; 205}; 206 207class EventHub : public EventHubInterface 208{ 209public: 210 EventHub(); 211 212 status_t errorCheck() const; 213 214 virtual uint32_t getDeviceClasses(int32_t deviceId) const; 215 216 virtual String8 getDeviceName(int32_t deviceId) const; 217 218 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const; 219 220 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis, 221 RawAbsoluteAxisInfo* outAxisInfo) const; 222 223 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const; 224 225 virtual bool hasInputProperty(int32_t deviceId, int property) const; 226 227 virtual status_t mapKey(int32_t deviceId, int scancode, 228 int32_t* outKeycode, uint32_t* outFlags) const; 229 230 virtual status_t mapAxis(int32_t deviceId, int scancode, 231 AxisInfo* outAxisInfo) const; 232 233 virtual void setExcludedDevices(const Vector<String8>& devices); 234 235 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const; 236 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const; 237 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const; 238 239 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, 240 const int32_t* keyCodes, uint8_t* outFlags) const; 241 242 virtual size_t getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize); 243 244 virtual bool hasLed(int32_t deviceId, int32_t led) const; 245 virtual void setLedState(int32_t deviceId, int32_t led, bool on); 246 247 virtual void getVirtualKeyDefinitions(int32_t deviceId, 248 Vector<VirtualKeyDefinition>& outVirtualKeys) const; 249 250 virtual void reopenDevices(); 251 252 virtual void dump(String8& dump); 253 254protected: 255 virtual ~EventHub(); 256 257private: 258 bool openPlatformInput(void); 259 260 int openDevice(const char *devicePath); 261 int closeDevice(const char *devicePath); 262 int closeDeviceAtIndexLocked(int index); 263 int scanDir(const char *dirname); 264 void scanDevices(); 265 int readNotify(int nfd); 266 267 status_t mError; 268 269 struct Device { 270 Device* next; 271 272 int fd; 273 const int32_t id; 274 const String8 path; 275 const InputDeviceIdentifier identifier; 276 277 uint32_t classes; 278 uint8_t* keyBitmask; 279 uint8_t* relBitmask; 280 uint8_t* propBitmask; 281 String8 configurationFile; 282 PropertyMap* configuration; 283 VirtualKeyMap* virtualKeyMap; 284 KeyMap keyMap; 285 286 Device(int fd, int32_t id, const String8& path, const InputDeviceIdentifier& identifier); 287 ~Device(); 288 289 void close(); 290 }; 291 292 Device* getDeviceLocked(int32_t deviceId) const; 293 bool hasKeycodeLocked(Device* device, int keycode) const; 294 295 int32_t getScanCodeStateLocked(Device* device, int32_t scanCode) const; 296 int32_t getKeyCodeStateLocked(Device* device, int32_t keyCode) const; 297 int32_t getSwitchStateLocked(Device* device, int32_t sw) const; 298 bool markSupportedKeyCodesLocked(Device* device, size_t numCodes, 299 const int32_t* keyCodes, uint8_t* outFlags) const; 300 301 void loadConfiguration(Device* device); 302 status_t loadVirtualKeyMap(Device* device); 303 status_t loadKeyMap(Device* device); 304 void setKeyboardProperties(Device* device, bool builtInKeyboard); 305 void clearKeyboardProperties(Device* device, bool builtInKeyboard); 306 307 bool isExternalDevice(Device* device); 308 309 // Protect all internal state. 310 mutable Mutex mLock; 311 312 // The actual id of the built-in keyboard, or -1 if none. 313 // EventHub remaps the built-in keyboard to id 0 externally as required by the API. 314 int32_t mBuiltInKeyboardId; 315 316 int32_t mNextDeviceId; 317 318 // Parallel arrays of fds and devices. 319 // First index is reserved for inotify. 320 Vector<struct pollfd> mFds; 321 Vector<Device*> mDevices; 322 323 Device *mOpeningDevices; 324 Device *mClosingDevices; 325 326 bool mOpened; 327 bool mNeedToSendFinishedDeviceScan; 328 volatile int32_t mNeedToReopenDevices; // must be modified atomically 329 bool mNeedToScanDevices; 330 Vector<String8> mExcludedDevices; 331 332 // device ids that report particular switches. 333 int32_t mSwitches[SW_MAX + 1]; 334 335 // The index of the next file descriptor that needs to be read. 336 size_t mInputFdIndex; 337 338 // Set to the number of CPUs. 339 int32_t mNumCpus; 340}; 341 342}; // namespace android 343 344#endif // _RUNTIME_EVENT_HUB_H 345