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