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 <utils/String8.h>
22#include <utils/threads.h>
23#include <utils/Log.h>
24#include <utils/threads.h>
25#include <utils/List.h>
26#include <utils/Errors.h>
27
28#include <linux/input.h>
29
30struct pollfd;
31
32namespace android {
33
34class KeyLayoutMap;
35
36/*
37 * Grand Central Station for events.  With a single call to waitEvent()
38 * you can wait for:
39 *  - input events from the keypad of a real device
40 *  - input events and meta-events (e.g. "quit") from the simulator
41 *  - synthetic events from the runtime (e.g. "URL fetch completed")
42 *  - real or forged "vsync" events
43 *
44 * Do not instantiate this class.  Instead, call startUp().
45 */
46class EventHub : public RefBase
47{
48public:
49    EventHub();
50
51    status_t errorCheck() const;
52
53    // bit fields for classes of devices.
54    enum {
55        CLASS_KEYBOARD      = 0x00000001,
56        CLASS_ALPHAKEY      = 0x00000002,
57        CLASS_TOUCHSCREEN   = 0x00000004,
58        CLASS_TRACKBALL     = 0x00000008,
59        CLASS_TOUCHSCREEN_MT= 0x00000010,
60        CLASS_DPAD          = 0x00000020
61    };
62    uint32_t getDeviceClasses(int32_t deviceId) const;
63
64    String8 getDeviceName(int32_t deviceId) const;
65
66    int getAbsoluteInfo(int32_t deviceId, int axis, int *outMinValue,
67            int* outMaxValue, int* outFlat, int* outFuzz) const;
68
69    int getSwitchState(int sw) const;
70    int getSwitchState(int32_t deviceId, int sw) const;
71
72    int getScancodeState(int key) const;
73    int getScancodeState(int32_t deviceId, int key) const;
74
75    int getKeycodeState(int key) const;
76    int getKeycodeState(int32_t deviceId, int key) const;
77
78    status_t scancodeToKeycode(int32_t deviceId, int scancode,
79            int32_t* outKeycode, uint32_t* outFlags) const;
80
81    // exclude a particular device from opening
82    // this can be used to ignore input devices for sensors
83    void addExcludedDevice(const char* deviceName);
84
85    // special type codes when devices are added/removed.
86    enum {
87        DEVICE_ADDED = 0x10000000,
88        DEVICE_REMOVED = 0x20000000
89    };
90
91    // examine key input devices for specific framework keycode support
92    bool hasKeys(size_t numCodes, int32_t* keyCodes, uint8_t* outFlags);
93
94    virtual bool getEvent(int32_t* outDeviceId, int32_t* outType,
95            int32_t* outScancode, int32_t* outKeycode, uint32_t *outFlags,
96            int32_t* outValue, nsecs_t* outWhen);
97
98protected:
99    virtual ~EventHub();
100
101private:
102    bool openPlatformInput(void);
103    int32_t convertDeviceKey_TI_P2(int code);
104
105    int open_device(const char *device);
106    int close_device(const char *device);
107    int scan_dir(const char *dirname);
108    int read_notify(int nfd);
109
110    status_t mError;
111
112    struct device_t {
113        const int32_t   id;
114        const String8   path;
115        String8         name;
116        uint32_t        classes;
117        uint8_t*        keyBitmask;
118        KeyLayoutMap*   layoutMap;
119        String8         keylayoutFilename;
120        device_t*       next;
121
122        device_t(int32_t _id, const char* _path, const char* name);
123        ~device_t();
124    };
125
126    device_t* getDevice(int32_t deviceId) const;
127    bool hasKeycode(device_t* device, int keycode) const;
128
129    // Protect all internal state.
130    mutable Mutex   mLock;
131
132    bool            mHaveFirstKeyboard;
133    int32_t         mFirstKeyboardId; // the API is that the built-in keyboard is id 0, so map it
134
135    struct device_ent {
136        device_t* device;
137        uint32_t seq;
138    };
139    device_ent      *mDevicesById;
140    int             mNumDevicesById;
141
142    device_t        *mOpeningDevices;
143    device_t        *mClosingDevices;
144
145    device_t        **mDevices;
146    struct pollfd   *mFDs;
147    int             mFDCount;
148
149    bool            mOpened;
150    List<String8>   mExcludedDevices;
151
152    // device ids that report particular switches.
153#ifdef EV_SW
154    int32_t         mSwitches[SW_MAX+1];
155#endif
156};
157
158}; // namespace android
159
160#endif // _RUNTIME_EVENT_HUB_H
161