OSDN Git Service

c2ebc55078ae44a81739fad722d5c54f2efa72a7
[android-x86/hardware-libsensors.git] / kbdsensor.cpp
1 /**
2  *
3  * Atkbd style sensor
4  *
5  * Copyright (C) 2011-2013 The Android-x86 Open Source Project
6  *
7  * by Chih-Wei Huang <cwhuang@linux.org.tw>
8  *
9  * Licensed under GPLv2 or later
10  *
11  **/
12
13 #define LOG_TAG "KbdSensor"
14
15 #include <cmath>
16 #include <cerrno>
17 #include <cstdlib>
18 #include <cstring>
19 #include <sys/stat.h>
20 #include <poll.h>
21 #include <fcntl.h>
22 #include <dirent.h>
23 #include <cutils/log.h>
24 #include <linux/input.h>
25 #include <linux/uinput.h>
26 #include <hardware/sensors.h>
27 #include <cutils/properties.h>
28
29 struct KbdSensorKeys {
30         char name[64];
31         int keys[8];
32 } KeysType[] = {
33         { "", { } },
34         { "AT Translated Set 2 keyboard", { EV_KEY, KEY_UP, KEY_RIGHT, KEY_DOWN, KEY_LEFT, KEY_LEFTALT, KEY_LEFTCTRL, 1 } },
35         { "AT Translated Set 2 keyboard", { EV_MSC, 91, 115, 123, 109, KEY_LEFTALT, KEY_LEFTCTRL, 3 } },
36         { "AT Translated Set 2 keyboard", { EV_KEY, KEY_F5, KEY_F8, KEY_F6, KEY_F7, KEY_LEFTALT, KEY_LEFTCTRL, 1 } },
37         { "AT Translated Set 2 keyboard", { EV_KEY, KEY_F9, KEY_F12, KEY_F10, KEY_F11, KEY_LEFTALT, KEY_LEFTCTRL, 1 } },
38         { "Asus Laptop extra buttons", { EV_KEY, KEY_F9, KEY_F12, KEY_F10, KEY_F11, KEY_LEFTALT, KEY_LEFTCTRL, 2 } },
39 };
40
41 const int ID_ACCELERATION = (SENSORS_HANDLE_BASE + 0);
42
43 template <typename T> struct SensorFd : T {
44         SensorFd(const struct hw_module_t *module, struct hw_device_t **device);
45 };
46
47 template <typename T> SensorFd<T>::SensorFd(const struct hw_module_t *module, struct hw_device_t **device)
48 {
49         this->common.tag     = HARDWARE_DEVICE_TAG;
50         this->common.version = 0;
51         this->common.module  = const_cast<struct hw_module_t *>(module);
52         *device              = &this->common;
53         LOGD("%s: module=%p dev=%p", __FUNCTION__, module, *device);
54 }
55
56 struct SensorPollContext : SensorFd<sensors_poll_device_t> {
57   public:
58         SensorPollContext(const struct hw_module_t *module, struct hw_device_t **device);
59         ~SensorPollContext();
60
61   private:
62         static int poll_close(struct hw_device_t *dev);
63         static int poll_activate(struct sensors_poll_device_t *dev, int handle, int enabled);
64         static int poll_setDelay(struct sensors_poll_device_t *dev, int handle, int64_t ns);
65         static int poll_poll(struct sensors_poll_device_t *dev, sensors_event_t *data, int count);
66
67         int doPoll(sensors_event_t *data, int count);
68
69         enum {
70                 ROT_0,
71                 ROT_90,
72                 ROT_180,
73                 ROT_270
74         };
75
76         bool enabled;
77         int rotation;
78         struct timespec delay;
79         struct pollfd pfd;
80         sensors_event_t orients[4];
81         KbdSensorKeys *ktype;
82 };
83
84 SensorPollContext::SensorPollContext(const struct hw_module_t *module, struct hw_device_t **device)
85       : SensorFd<sensors_poll_device_t>(module, device), enabled(false), rotation(ROT_0), ktype(KeysType)
86 {
87         common.close = poll_close;
88         activate     = poll_activate;
89         setDelay     = poll_setDelay;
90         poll         = poll_poll;
91
92         int &fd = pfd.fd;
93         const char *dirname = "/dev/input";
94         char prop[PROPERTY_VALUE_MAX];
95         if (property_get("hal.sensors.kbd.keys", prop, 0))
96                 sscanf(prop, "%s,%d,%d,%d,%d,%d,%d,%d,%d", ktype->name, ktype->keys,
97                                 ktype->keys + 1, ktype->keys + 2, ktype->keys + 3, ktype->keys + 4, ktype->keys + 5, ktype->keys + 6, ktype->keys + 7);
98         else if (property_get("hal.sensors.kbd.type", prop, 0))
99                 ktype = &KeysType[atoi(prop)];
100         else
101                 ktype = 0;
102         if (DIR *dir = opendir(dirname)) {
103                 char name[PATH_MAX];
104                 while (struct dirent *de = readdir(dir)) {
105                         if (de->d_name[0] != 'e') // not eventX
106                                 continue;
107                         snprintf(name, PATH_MAX, "%s/%s", dirname, de->d_name);
108                         fd = open(name, O_RDWR);
109                         if (fd < 0) {
110                                 LOGE("could not open %s, %s", name, strerror(errno));
111                                 continue;
112                         }
113                         name[sizeof(name) - 1] = '\0';
114                         if (ioctl(fd, EVIOCGNAME(sizeof(name) - 1), &name) < 1) {
115                                 LOGE("could not get device name for %s, %s\n", name, strerror(errno));
116                                 name[0] = '\0';
117                         }
118
119                         if (ktype) {
120                                 if (!strcmp(name, ktype->name))
121                                         break;
122                         } else {
123                                 ktype = KeysType + (sizeof(KeysType) / sizeof(KeysType[0]));
124                                 while (--ktype != KeysType)
125                                         if (!strcmp(name, ktype->name))
126                                                 break;
127                                 if (ktype != KeysType)
128                                         break;
129                                 else
130                                         ktype = 0;
131                         }
132                         close(fd);
133                         fd = -1;
134                 }
135                 LOGI_IF(fd >= 0, "Open %s ok, fd=%d", name, fd);
136                 closedir(dir);
137         }
138
139         pfd.events = POLLIN;
140         orients[ROT_0].version = sizeof(sensors_event_t);
141         orients[ROT_0].sensor = ID_ACCELERATION;
142         orients[ROT_0].type = SENSOR_TYPE_ACCELEROMETER;
143         orients[ROT_0].acceleration.status = SENSOR_STATUS_ACCURACY_HIGH;
144         orients[ROT_270] = orients[ROT_180] = orients[ROT_90] = orients[ROT_0];
145         const double angle = 20.0;
146         const double cos_angle = GRAVITY_EARTH * cos(angle / M_PI);
147         const double sin_angle = GRAVITY_EARTH * sin(angle / M_PI);
148         orients[ROT_0].acceleration.x   = 0.0;
149         orients[ROT_0].acceleration.y   = cos_angle;
150         orients[ROT_0].acceleration.z   = sin_angle;
151         orients[ROT_90].acceleration.x  = cos_angle;
152         orients[ROT_90].acceleration.y  = 0.0;
153         orients[ROT_90].acceleration.z  = sin_angle;
154         orients[ROT_180].acceleration.x = 0.0;
155         orients[ROT_180].acceleration.y = -cos_angle;
156         orients[ROT_180].acceleration.z = -sin_angle;
157         orients[ROT_270].acceleration.x = -cos_angle;
158         orients[ROT_270].acceleration.y = 0.0;
159         orients[ROT_270].acceleration.z = -sin_angle;
160
161         delay.tv_sec = 0;
162         delay.tv_nsec = 200000000L;
163
164         LOGD("%s: dev=%p fd=%d", __FUNCTION__, this, fd);
165 }
166
167 SensorPollContext::~SensorPollContext()
168 {
169         close(pfd.fd);
170 }
171
172 int SensorPollContext::poll_close(struct hw_device_t *dev)
173 {
174         LOGD("%s: dev=%p", __FUNCTION__, dev);
175         delete reinterpret_cast<SensorPollContext *>(dev);
176         return 0;
177 }
178
179 int SensorPollContext::poll_activate(struct sensors_poll_device_t *dev, int handle, int enabled)
180 {
181         LOGD("%s: dev=%p handle=%d enabled=%d", __FUNCTION__, dev, handle, enabled);
182         SensorPollContext *ctx = reinterpret_cast<SensorPollContext *>(dev);
183         ctx->enabled = enabled;
184         return 0;
185 }
186
187 int SensorPollContext::poll_setDelay(struct sensors_poll_device_t *dev, int handle, int64_t ns)
188 {
189         LOGD("%s: dev=%p delay-ns=%lld", __FUNCTION__, dev, ns);
190         return 0;
191 }
192
193 int SensorPollContext::poll_poll(struct sensors_poll_device_t *dev, sensors_event_t *data, int count)
194 {
195         LOGD("%s: dev=%p data=%p count=%d", __FUNCTION__, dev, data, count);
196         SensorPollContext *ctx = reinterpret_cast<SensorPollContext *>(dev);
197         return ctx->doPoll(data, count);
198 }
199
200 int SensorPollContext::doPoll(sensors_event_t *data, int count)
201 {
202         nanosleep(&delay, 0);
203         int *keys = ktype->keys;
204         while (int pollres = ::poll(&pfd, 1, -1)) {
205                 if (pollres < 0) {
206                         LOGE("%s: poll %d error: %s", __FUNCTION__, pfd.fd, strerror(errno));
207                         break;
208                 }
209                 if (!(pfd.revents & POLLIN)) {
210                         LOGW("%s: ignore revents %d", __FUNCTION__, pfd.revents);
211                         continue;
212                 }
213
214                 struct input_event iev;
215                 size_t res = ::read(pfd.fd, &iev, sizeof(iev));
216                 if (res < sizeof(iev)) {
217                         LOGW("insufficient input data(%d)? fd=%d", res, pfd.fd);
218                         continue;
219                 }
220                 LOGD("type=%d scancode=%d value=%d from fd=%d", iev.type, iev.code, iev.value, pfd.fd);
221                 if (iev.type == keys[0]) {
222                         int rot;
223                         int input = (keys[0] == EV_MSC) ? iev.value : iev.code;
224                         if (input == keys[1])
225                                 rot = ROT_0;
226                         else if (input == keys[2])
227                                 rot = ROT_90;
228                         else if (input == keys[3])
229                                 rot = ROT_180;
230                         else if (input == keys[4])
231                                 rot = ROT_270;
232                         else if (input == keys[5] || input == keys[6])
233                                 rot = rotation;
234                         else
235                                 rot = -1;
236
237                         if (rot >= 0) {
238                                 if (rot != rotation) {
239                                         LOGI("orientation changed from %d to %d", rotation * 90, rot * 90);
240                                         rotation = rot;
241                                 }
242                                 if (enabled && count > 0)
243                                         break;
244                         }
245                 }
246         }
247
248         int cnt;
249         struct timespec t;
250         LOGV("%s: dev=%p fd=%d rotation=%d", __FUNCTION__, this, pfd.fd, rotation * 90);
251         data[0] = orients[rotation];
252         t.tv_sec = t.tv_nsec = 0;
253         clock_gettime(CLOCK_MONOTONIC, &t);
254         data[0].timestamp = int64_t(t.tv_sec) * 1000000000LL + t.tv_nsec;
255         for (cnt = 1; cnt < keys[7] && cnt < count; ++cnt) {
256                 data[cnt] = data[cnt - 1];
257                 data[cnt].timestamp += delay.tv_nsec;
258                 nanosleep(&delay, 0);
259         }
260         return cnt;
261 }
262
263 static int open_kbd_sensor(const struct hw_module_t *module, const char *id, struct hw_device_t **device)
264 {
265         LOGD("%s: id=%s", __FUNCTION__, id);
266         return new SensorPollContext(module, device) ? 0 : -EINVAL;
267 }
268
269 static struct sensor_t sSensorListInit[] = {
270         {
271                 name: "Kbd Orientation Sensor",
272                 vendor: "Android-x86 Open Source Project",
273                 version: 1,
274                 handle: ID_ACCELERATION,
275                 type: SENSOR_TYPE_ACCELEROMETER,
276                 maxRange: 2.8f,
277                 resolution: 1.0f/4032.0f,
278                 power: 3.0f,
279                 minDelay: 0,
280                 reserved: { }
281         }
282 };
283
284 static int sensors_get_sensors_list(struct sensors_module_t *module, struct sensor_t const **list)
285 {
286         *list = sSensorListInit;
287         return sizeof(sSensorListInit) / sizeof(struct sensor_t);
288 }
289
290 static struct hw_module_methods_t sensors_methods = {
291         open: open_kbd_sensor
292 };
293
294 struct sensors_module_t HAL_MODULE_INFO_SYM = {
295         common: {
296                 tag: HARDWARE_MODULE_TAG,
297                 version_major: 2,
298                 version_minor: 3,
299                 id: SENSORS_HARDWARE_MODULE_ID,
300                 name: "Kbd Orientation Sensor",
301                 author: "Chih-Wei Huang",
302                 methods: &sensors_methods,
303                 dso: 0,
304                 reserved: { }
305         },
306         get_sensors_list: sensors_get_sensors_list
307 };