EventHub.h revision aa3855d5836d2a2d83baafdf6e40caf90d3dad1c
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/* 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
62struct pollfd;
63
64namespace android {
65
66/*
67 * 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
88    inline void clear() {
89        valid = false;
90        minValue = 0;
91        maxValue = 0;
92        flat = 0;
93        fuzz = 0;
94    }
95};
96
97/*
98 * Input device classes.
99 */
100enum {
101    /* The input device is a keyboard or has buttons. */
102    INPUT_DEVICE_CLASS_KEYBOARD      = 0x00000001,
103
104    /* The input device is an alpha-numeric keyboard (not just a dial pad). */
105    INPUT_DEVICE_CLASS_ALPHAKEY      = 0x00000002,
106
107    /* The input device is a touchscreen or a touchpad (either single-touch or multi-touch). */
108    INPUT_DEVICE_CLASS_TOUCH         = 0x00000004,
109
110    /* The input device is a cursor device such as a trackball or mouse. */
111    INPUT_DEVICE_CLASS_CURSOR        = 0x00000008,
112
113    /* The input device is a multi-touch touchscreen. */
114    INPUT_DEVICE_CLASS_TOUCH_MT      = 0x00000010,
115
116    /* The input device is a directional pad (implies keyboard, has DPAD keys). */
117    INPUT_DEVICE_CLASS_DPAD          = 0x00000020,
118
119    /* The input device is a gamepad (implies keyboard, has BUTTON keys). */
120    INPUT_DEVICE_CLASS_GAMEPAD       = 0x00000040,
121
122    /* The input device has switches. */
123    INPUT_DEVICE_CLASS_SWITCH        = 0x00000080,
124
125    /* The input device is a joystick (implies gamepad, has joystick absolute axes). */
126    INPUT_DEVICE_CLASS_JOYSTICK      = 0x00000100,
127
128    /* The input device is external (not built-in). */
129    INPUT_DEVICE_CLASS_EXTERNAL      = 0x80000000,
130};
131
132/*
133 * Grand Central Station for events.
134 *
135 * The event hub aggregates input events received across all known input
136 * devices on the system, including devices that may be emulated by the simulator
137 * environment.  In addition, the event hub generates fake input events to indicate
138 * when devices are added or removed.
139 *
140 * The event hub provides a stream of input events (via the getEvent function).
141 * It also supports querying the current actual state of input devices such as identifying
142 * which keys are currently down.  Finally, the event hub keeps track of the capabilities of
143 * individual input devices, such as their class and the set of key codes that they support.
144 */
145class EventHubInterface : public virtual RefBase {
146protected:
147    EventHubInterface() { }
148    virtual ~EventHubInterface() { }
149
150public:
151    // Synthetic raw event type codes produced when devices are added or removed.
152    enum {
153        // Sent when a device is added.
154        DEVICE_ADDED = 0x10000000,
155        // Sent when a device is removed.
156        DEVICE_REMOVED = 0x20000000,
157        // Sent when all added/removed devices from the most recent scan have been reported.
158        // This event is always sent at least once.
159        FINISHED_DEVICE_SCAN = 0x30000000,
160    };
161
162    virtual uint32_t getDeviceClasses(int32_t deviceId) const = 0;
163
164    virtual String8 getDeviceName(int32_t deviceId) const = 0;
165
166    virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const = 0;
167
168    virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
169            RawAbsoluteAxisInfo* outAxisInfo) const = 0;
170
171    virtual bool hasRelativeAxis(int32_t deviceId, int axis) const = 0;
172
173    virtual status_t mapKey(int32_t deviceId, int scancode,
174            int32_t* outKeycode, uint32_t* outFlags) const = 0;
175
176    virtual status_t mapAxis(int32_t deviceId, int scancode,
177            AxisInfo* outAxisInfo) const = 0;
178
179    // exclude a particular device from opening
180    // this can be used to ignore input devices for sensors
181    virtual void addExcludedDevice(const char* deviceName) = 0;
182
183    /*
184     * Wait for the next event to become available and return it.
185     * After returning, the EventHub holds onto a wake lock until the next call to getEvent.
186     * This ensures that the device will not go to sleep while the event is being processed.
187     * If the device needs to remain awake longer than that, then the caller is responsible
188     * for taking care of it (say, by poking the power manager user activity timer).
189     *
190     * The timeout is advisory only.  If the device is asleep, it will not wake just to
191     * service the timeout.
192     *
193     * Returns true if an event was obtained, false if the timeout expired.
194     */
195    virtual bool getEvent(int timeoutMillis, RawEvent* outEvent) = 0;
196
197    /*
198     * Query current input state.
199     */
200    virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const = 0;
201    virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const = 0;
202    virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const = 0;
203
204    /*
205     * Examine key input devices for specific framework keycode support
206     */
207    virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
208            uint8_t* outFlags) const = 0;
209
210    virtual bool hasLed(int32_t deviceId, int32_t led) const = 0;
211    virtual void setLedState(int32_t deviceId, int32_t led, bool on) = 0;
212
213    virtual void getVirtualKeyDefinitions(int32_t deviceId,
214            Vector<VirtualKeyDefinition>& outVirtualKeys) const = 0;
215
216    virtual void dump(String8& dump) = 0;
217};
218
219class EventHub : public EventHubInterface
220{
221public:
222    EventHub();
223
224    status_t errorCheck() const;
225
226    virtual uint32_t getDeviceClasses(int32_t deviceId) const;
227
228    virtual String8 getDeviceName(int32_t deviceId) const;
229
230    virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const;
231
232    virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
233            RawAbsoluteAxisInfo* outAxisInfo) const;
234
235    virtual bool hasRelativeAxis(int32_t deviceId, int axis) const;
236
237    virtual status_t mapKey(int32_t deviceId, int scancode,
238            int32_t* outKeycode, uint32_t* outFlags) const;
239
240    virtual status_t mapAxis(int32_t deviceId, int scancode,
241            AxisInfo* outAxisInfo) const;
242
243    virtual void addExcludedDevice(const char* deviceName);
244
245    virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const;
246    virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const;
247    virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const;
248
249    virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes,
250            const int32_t* keyCodes, uint8_t* outFlags) const;
251
252    virtual bool getEvent(int timeoutMillis, RawEvent* outEvent);
253
254    virtual bool hasLed(int32_t deviceId, int32_t led) const;
255    virtual void setLedState(int32_t deviceId, int32_t led, bool on);
256
257    virtual void getVirtualKeyDefinitions(int32_t deviceId,
258            Vector<VirtualKeyDefinition>& outVirtualKeys) const;
259
260    virtual void dump(String8& dump);
261
262protected:
263    virtual ~EventHub();
264
265private:
266    bool openPlatformInput(void);
267
268    int openDevice(const char *devicePath);
269    int closeDevice(const char *devicePath);
270    int closeDeviceAtIndexLocked(int index);
271    int scanDir(const char *dirname);
272    int readNotify(int nfd);
273
274    status_t mError;
275
276    struct Device {
277        Device* next;
278
279        int fd;
280        const int32_t id;
281        const String8 path;
282        const InputDeviceIdentifier identifier;
283
284        uint32_t classes;
285        uint8_t* keyBitmask;
286        uint8_t* relBitmask;
287        String8 configurationFile;
288        PropertyMap* configuration;
289        VirtualKeyMap* virtualKeyMap;
290        KeyMap keyMap;
291
292        Device(int fd, int32_t id, const String8& path, const InputDeviceIdentifier& identifier);
293        ~Device();
294
295        void close();
296    };
297
298    Device* getDeviceLocked(int32_t deviceId) const;
299    bool hasKeycodeLocked(Device* device, int keycode) const;
300
301    int32_t getScanCodeStateLocked(Device* device, int32_t scanCode) const;
302    int32_t getKeyCodeStateLocked(Device* device, int32_t keyCode) const;
303    int32_t getSwitchStateLocked(Device* device, int32_t sw) const;
304    bool markSupportedKeyCodesLocked(Device* device, size_t numCodes,
305            const int32_t* keyCodes, uint8_t* outFlags) const;
306
307    void loadConfiguration(Device* device);
308    status_t loadVirtualKeyMap(Device* device);
309    status_t loadKeyMap(Device* device);
310    void setKeyboardProperties(Device* device, bool builtInKeyboard);
311    void clearKeyboardProperties(Device* device, bool builtInKeyboard);
312
313    bool isExternalDevice(Device* device);
314
315    // Protect all internal state.
316    mutable Mutex mLock;
317
318    // The actual id of the built-in keyboard, or -1 if none.
319    // EventHub remaps the built-in keyboard to id 0 externally as required by the API.
320    int32_t mBuiltInKeyboardId;
321
322    int32_t mNextDeviceId;
323
324    // Parallel arrays of fds and devices.
325    // First index is reserved for inotify.
326    Vector<struct pollfd> mFds;
327    Vector<Device*> mDevices;
328
329    Device *mOpeningDevices;
330    Device *mClosingDevices;
331
332    bool mOpened;
333    bool mNeedToSendFinishedDeviceScan;
334    List<String8> mExcludedDevices;
335
336    // device ids that report particular switches.
337    int32_t mSwitches[SW_MAX + 1];
338
339    static const int INPUT_BUFFER_SIZE = 64;
340    struct input_event mInputBufferData[INPUT_BUFFER_SIZE];
341    size_t mInputBufferIndex;
342    size_t mInputBufferCount;
343    size_t mInputFdIndex;
344};
345
346}; // namespace android
347
348#endif // _RUNTIME_EVENT_HUB_H
349