blob: 3b5a1ea0516894f0216bf3b73b2169ae692d6a40 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
Jeff Brown90655042010-12-02 13:50:46 -080021#include <ui/Input.h>
Jeff Brown6b53e8d2010-11-10 16:03:06 -080022#include <ui/Keyboard.h>
Jeff Brown90655042010-12-02 13:50:46 -080023#include <ui/KeyLayoutMap.h>
24#include <ui/KeyCharacterMap.h>
25#include <ui/VirtualKeyMap.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026#include <utils/String8.h>
27#include <utils/threads.h>
Mathias Agopian3b4062e2009-05-31 19:13:00 -070028#include <utils/Log.h>
29#include <utils/threads.h>
30#include <utils/List.h>
31#include <utils/Errors.h>
Jeff Brown47e6b1b2010-11-29 17:37:49 -080032#include <utils/PropertyMap.h>
Jeff Brown90655042010-12-02 13:50:46 -080033#include <utils/Vector.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034
35#include <linux/input.h>
36
Jeff Brown46b9ac02010-04-22 18:58:52 -070037/* These constants are not defined in linux/input.h but they are part of the multitouch
38 * input protocol. */
39
40#define ABS_MT_TOUCH_MAJOR 0x30 /* Major axis of touching ellipse */
41#define ABS_MT_TOUCH_MINOR 0x31 /* Minor axis (omit if circular) */
42#define ABS_MT_WIDTH_MAJOR 0x32 /* Major axis of approaching ellipse */
43#define ABS_MT_WIDTH_MINOR 0x33 /* Minor axis (omit if circular) */
44#define ABS_MT_ORIENTATION 0x34 /* Ellipse orientation */
45#define ABS_MT_POSITION_X 0x35 /* Center X ellipse position */
46#define ABS_MT_POSITION_Y 0x36 /* Center Y ellipse position */
47#define ABS_MT_TOOL_TYPE 0x37 /* Type of touching device (finger, pen, ...) */
48#define ABS_MT_BLOB_ID 0x38 /* Group a set of packets as a blob */
49#define ABS_MT_TRACKING_ID 0x39 /* Unique ID of initiated contact */
50#define ABS_MT_PRESSURE 0x3a /* Pressure on contact area */
51
52#define MT_TOOL_FINGER 0 /* Identifies a finger */
53#define MT_TOOL_PEN 1 /* Identifies a pen */
54
55#define SYN_MT_REPORT 2
56
57/* Convenience constants. */
58
59#define BTN_FIRST 0x100 // first button scancode
60#define BTN_LAST 0x15f // last button scancode
61
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062struct pollfd;
63
64namespace android {
65
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066/*
Jeff Brown6d0fec22010-07-23 21:28:06 -070067 * A raw event as retrieved from the EventHub.
68 */
69struct RawEvent {
70 nsecs_t when;
71 int32_t deviceId;
72 int32_t type;
73 int32_t scanCode;
74 int32_t keyCode;
75 int32_t value;
76 uint32_t flags;
77};
78
79/* Describes an absolute axis. */
80struct 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
Jeff Browncb1404e2011-01-15 18:14:15 -080088 inline int32_t getRange() const { return maxValue - minValue; }
Jeff Brown8d608662010-08-30 03:02:23 -070089
90 inline void clear() {
91 valid = false;
92 minValue = 0;
93 maxValue = 0;
94 flat = 0;
95 fuzz = 0;
96 }
Jeff Brown6d0fec22010-07-23 21:28:06 -070097};
98
99/*
Jeff Brownc5ed5912010-07-14 18:48:53 -0700100 * Input device classes.
101 */
102enum {
Jeff Browncb1404e2011-01-15 18:14:15 -0800103 /* The input device is a keyboard or has buttons. */
Jeff Brownc5ed5912010-07-14 18:48:53 -0700104 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 (either single-touch or multi-touch). */
110 INPUT_DEVICE_CLASS_TOUCHSCREEN = 0x00000004,
111
Jeff Brown83c09682010-12-23 17:50:18 -0800112 /* The input device is a cursor device such as a trackball or mouse. */
113 INPUT_DEVICE_CLASS_CURSOR = 0x00000008,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700114
115 /* The input device is a multi-touch touchscreen. */
116 INPUT_DEVICE_CLASS_TOUCHSCREEN_MT= 0x00000010,
117
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700118 /* The input device is a directional pad (implies keyboard, has DPAD keys). */
Jeff Brownc5ed5912010-07-14 18:48:53 -0700119 INPUT_DEVICE_CLASS_DPAD = 0x00000020,
120
Jeff Browndc1ab4b2010-09-14 18:03:38 -0700121 /* The input device is a gamepad (implies keyboard, has BUTTON keys). */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700122 INPUT_DEVICE_CLASS_GAMEPAD = 0x00000040,
123
124 /* The input device has switches. */
125 INPUT_DEVICE_CLASS_SWITCH = 0x00000080,
Jeff Browncb1404e2011-01-15 18:14:15 -0800126
127 /* The input device is a joystick (implies gamepad, has joystick absolute axes). */
128 INPUT_DEVICE_CLASS_JOYSTICK = 0x00000100,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700129};
130
131/*
Jeff Brown46b9ac02010-04-22 18:58:52 -0700132 * Grand Central Station for events.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 *
Jeff Brown46b9ac02010-04-22 18:58:52 -0700134 * The event hub aggregates input events received across all known input
135 * devices on the system, including devices that may be emulated by the simulator
136 * environment. In addition, the event hub generates fake input events to indicate
137 * when devices are added or removed.
138 *
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800139 * The event hub provides a stream of input events (via the getEvent function).
Jeff Brown46b9ac02010-04-22 18:58:52 -0700140 * It also supports querying the current actual state of input devices such as identifying
141 * which keys are currently down. Finally, the event hub keeps track of the capabilities of
142 * individual input devices, such as their class and the set of key codes that they support.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 */
Jeff Brown46b9ac02010-04-22 18:58:52 -0700144class EventHubInterface : public virtual RefBase {
145protected:
146 EventHubInterface() { }
147 virtual ~EventHubInterface() { }
148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149public:
Jeff Brown46b9ac02010-04-22 18:58:52 -0700150 // Synthetic raw event type codes produced when devices are added or removed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 enum {
Jeff Brown7342bb92010-10-01 18:55:43 -0700152 // Sent when a device is added.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 DEVICE_ADDED = 0x10000000,
Jeff Brown7342bb92010-10-01 18:55:43 -0700154 // Sent when a device is removed.
155 DEVICE_REMOVED = 0x20000000,
156 // Sent when all added/removed devices from the most recent scan have been reported.
157 // This event is always sent at least once.
158 FINISHED_DEVICE_SCAN = 0x30000000,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 };
Jeff Brown46b9ac02010-04-22 18:58:52 -0700160
161 virtual uint32_t getDeviceClasses(int32_t deviceId) const = 0;
162
163 virtual String8 getDeviceName(int32_t deviceId) const = 0;
164
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800165 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const = 0;
166
Jeff Brown6d0fec22010-07-23 21:28:06 -0700167 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
168 RawAbsoluteAxisInfo* outAxisInfo) const = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700169
170 virtual status_t scancodeToKeycode(int32_t deviceId, int scancode,
171 int32_t* outKeycode, uint32_t* outFlags) const = 0;
172
173 // exclude a particular device from opening
174 // this can be used to ignore input devices for sensors
175 virtual void addExcludedDevice(const char* deviceName) = 0;
176
177 /*
178 * Wait for the next event to become available and return it.
179 * After returning, the EventHub holds onto a wake lock until the next call to getEvent.
180 * This ensures that the device will not go to sleep while the event is being processed.
181 * If the device needs to remain awake longer than that, then the caller is responsible
182 * for taking care of it (say, by poking the power manager user activity timer).
183 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700184 virtual bool getEvent(RawEvent* outEvent) = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700185
186 /*
187 * Query current input state.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700188 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700189 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const = 0;
190 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const = 0;
191 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700192
193 /*
194 * Examine key input devices for specific framework keycode support
195 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700196 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700197 uint8_t* outFlags) const = 0;
Jeff Brownf2f48712010-10-01 17:46:21 -0700198
Jeff Brown497a92c2010-09-12 17:55:08 -0700199 virtual bool hasLed(int32_t deviceId, int32_t led) const = 0;
200 virtual void setLedState(int32_t deviceId, int32_t led, bool on) = 0;
201
Jeff Brown90655042010-12-02 13:50:46 -0800202 virtual void getVirtualKeyDefinitions(int32_t deviceId,
203 Vector<VirtualKeyDefinition>& outVirtualKeys) const = 0;
204
Jeff Brownf2f48712010-10-01 17:46:21 -0700205 virtual void dump(String8& dump) = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700206};
207
208class EventHub : public EventHubInterface
209{
210public:
211 EventHub();
212
213 status_t errorCheck() const;
214
215 virtual uint32_t getDeviceClasses(int32_t deviceId) const;
Jeff Brown497a92c2010-09-12 17:55:08 -0700216
Jeff Brown46b9ac02010-04-22 18:58:52 -0700217 virtual String8 getDeviceName(int32_t deviceId) const;
Jeff Brown497a92c2010-09-12 17:55:08 -0700218
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800219 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const;
220
Jeff Brown6d0fec22010-07-23 21:28:06 -0700221 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
222 RawAbsoluteAxisInfo* outAxisInfo) const;
223
Jeff Brown46b9ac02010-04-22 18:58:52 -0700224 virtual status_t scancodeToKeycode(int32_t deviceId, int scancode,
225 int32_t* outKeycode, uint32_t* outFlags) const;
226
227 virtual void addExcludedDevice(const char* deviceName);
228
Jeff Brown6d0fec22010-07-23 21:28:06 -0700229 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const;
230 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const;
231 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700232
Jeff Brown6d0fec22010-07-23 21:28:06 -0700233 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes,
234 const int32_t* keyCodes, uint8_t* outFlags) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235
Jeff Brown6d0fec22010-07-23 21:28:06 -0700236 virtual bool getEvent(RawEvent* outEvent);
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400237
Jeff Brown497a92c2010-09-12 17:55:08 -0700238 virtual bool hasLed(int32_t deviceId, int32_t led) const;
239 virtual void setLedState(int32_t deviceId, int32_t led, bool on);
240
Jeff Brown90655042010-12-02 13:50:46 -0800241 virtual void getVirtualKeyDefinitions(int32_t deviceId,
242 Vector<VirtualKeyDefinition>& outVirtualKeys) const;
243
Jeff Brownf2f48712010-10-01 17:46:21 -0700244 virtual void dump(String8& dump);
245
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246protected:
247 virtual ~EventHub();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248
249private:
250 bool openPlatformInput(void);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251
Jeff Brown90655042010-12-02 13:50:46 -0800252 int openDevice(const char *devicePath);
253 int closeDevice(const char *devicePath);
Jeff Brown7342bb92010-10-01 18:55:43 -0700254 int scanDir(const char *dirname);
255 int readNotify(int nfd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256
257 status_t mError;
258
Jeff Brown90655042010-12-02 13:50:46 -0800259 struct Device {
260 Device* next;
261
262 int fd;
263 const int32_t id;
264 const String8 path;
265 const InputDeviceIdentifier identifier;
266
267 uint32_t classes;
268 uint8_t* keyBitmask;
269 String8 configurationFile;
270 PropertyMap* configuration;
271 VirtualKeyMap* virtualKeyMap;
272 KeyMap keyMap;
273
274 Device(int fd, int32_t id, const String8& path, const InputDeviceIdentifier& identifier);
275 ~Device();
276
277 void close();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 };
279
Jeff Brown90655042010-12-02 13:50:46 -0800280 Device* getDeviceLocked(int32_t deviceId) const;
281 bool hasKeycodeLocked(Device* device, int keycode) const;
Jeff Brown497a92c2010-09-12 17:55:08 -0700282
Jeff Brown90655042010-12-02 13:50:46 -0800283 int32_t getScanCodeStateLocked(Device* device, int32_t scanCode) const;
284 int32_t getKeyCodeStateLocked(Device* device, int32_t keyCode) const;
285 int32_t getSwitchStateLocked(Device* device, int32_t sw) const;
286 bool markSupportedKeyCodesLocked(Device* device, size_t numCodes,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700287 const int32_t* keyCodes, uint8_t* outFlags) const;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700288
Jeff Brown90655042010-12-02 13:50:46 -0800289 void loadConfiguration(Device* device);
290 status_t loadVirtualKeyMap(Device* device);
291 status_t loadKeyMap(Device* device);
292 void setKeyboardProperties(Device* device, bool builtInKeyboard);
293 void clearKeyboardProperties(Device* device, bool builtInKeyboard);
Jeff Brown497a92c2010-09-12 17:55:08 -0700294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 // Protect all internal state.
Jeff Brown90655042010-12-02 13:50:46 -0800296 mutable Mutex mLock;
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400297
Jeff Brown90655042010-12-02 13:50:46 -0800298 // The actual id of the built-in keyboard, or -1 if none.
299 // EventHub remaps the built-in keyboard to id 0 externally as required by the API.
300 int32_t mBuiltInKeyboardId;
301
302 int32_t mNextDeviceId;
303
304 // Parallel arrays of fds and devices.
305 // First index is reserved for inotify.
306 Vector<struct pollfd> mFds;
307 Vector<Device*> mDevices;
308
309 Device *mOpeningDevices;
310 Device *mClosingDevices;
311
312 bool mOpened;
313 bool mNeedToSendFinishedDeviceScan;
314 List<String8> mExcludedDevices;
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 // device ids that report particular switches.
317#ifdef EV_SW
Jeff Brown90655042010-12-02 13:50:46 -0800318 int32_t mSwitches[SW_MAX + 1];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319#endif
Jeff Browncc2e7172010-08-17 16:48:25 -0700320
321 static const int INPUT_BUFFER_SIZE = 64;
322 struct input_event mInputBufferData[INPUT_BUFFER_SIZE];
Jeff Brown90655042010-12-02 13:50:46 -0800323 size_t mInputBufferIndex;
324 size_t mInputBufferCount;
325 size_t mInputFdIndex;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326};
327
328}; // namespace android
329
330#endif // _RUNTIME_EVENT_HUB_H