OSDN Git Service

Remove unused variables and labels
[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 <cinttypes>
20 #include <sys/stat.h>
21 #include <poll.h>
22 #include <fcntl.h>
23 #include <dirent.h>
24 #include <cutils/log.h>
25 #include <linux/input.h>
26 #include <linux/uinput.h>
27 #include <hardware/sensors.h>
28 #include <cutils/properties.h>
29
30 struct KbdSensorKeys {
31         char name[64];
32         int keys[8];
33 } KeysType[] = {
34         { "", { } },
35         { "AT Translated Set 2 keyboard", { EV_KEY, KEY_UP, KEY_RIGHT, KEY_DOWN, KEY_LEFT, KEY_LEFTALT, KEY_LEFTCTRL, 1 } },
36         { "AT Translated Set 2 keyboard", { EV_MSC, 91, 115, 123, 109, KEY_LEFTALT, KEY_LEFTCTRL, 3 } },
37         { "AT Translated Set 2 keyboard", { EV_KEY, KEY_F5, KEY_F8, KEY_F6, KEY_F7, KEY_LEFTALT, KEY_LEFTCTRL, 1 } },
38         { "AT Translated Set 2 keyboard", { EV_KEY, KEY_F9, KEY_F12, KEY_F10, KEY_F11, KEY_LEFTALT, KEY_LEFTCTRL, 1 } },
39         { "Asus Laptop extra buttons", { EV_KEY, KEY_F9, KEY_F12, KEY_F10, KEY_F11, KEY_LEFTALT, KEY_LEFTCTRL, 2 } },
40         { "HP WMI hotkeys", { -1, KEY_DIRECTION, 0, 0, 0, 0, 0, 3 } },
41 };
42
43 const int ID_ACCELERATION = (SENSORS_HANDLE_BASE + 0);
44
45 template <typename T> struct SensorFd : T {
46         SensorFd(const struct hw_module_t *module);
47 };
48
49 template <typename T> SensorFd<T>::SensorFd(const struct hw_module_t *module)
50 {
51         this->common.tag     = HARDWARE_DEVICE_TAG;
52         this->common.version = SENSORS_DEVICE_API_VERSION_1_3;
53         this->common.module  = const_cast<struct hw_module_t *>(module);
54 }
55
56 struct SensorPollContext : SensorFd<sensors_poll_device_1> {
57   public:
58         SensorPollContext(const struct hw_module_t *module, struct hw_device_t **device);
59         ~SensorPollContext();
60         bool isValid() const { return (pfd.fd >= 0); }
61
62   private:
63         static int poll_close(struct hw_device_t *dev);
64         static int poll_activate(struct sensors_poll_device_t *dev, int handle, int enabled);
65         static int poll_setDelay(struct sensors_poll_device_t *dev, int handle, int64_t ns);
66         static int poll_poll(struct sensors_poll_device_t *dev, sensors_event_t *data, int count);
67         static int poll_batch(struct sensors_poll_device_1* dev, int sensor_handle, int flags, int64_t sampling_period_ns, int64_t max_report_latency_ns);
68         static int poll_flush(struct sensors_poll_device_1* dev, int sensor_handle);
69
70         int doPoll(sensors_event_t *data, int count);
71
72         enum {
73                 ROT_0,
74                 ROT_90,
75                 ROT_180,
76                 ROT_270
77         };
78
79         bool enabled;
80         int rotation;
81         int64_t sampling_period_ns;
82         struct pollfd pfd;
83         sensors_event_t orients[4];
84         KbdSensorKeys *ktype;
85 };
86
87 SensorPollContext::SensorPollContext(const struct hw_module_t *module, struct hw_device_t **device)
88       : SensorFd<sensors_poll_device_1>(module), enabled(false), rotation(ROT_0), ktype(KeysType)
89 {
90         common.close = poll_close;
91         activate     = poll_activate;
92         setDelay     = poll_setDelay;
93         poll         = poll_poll;
94         batch        = poll_batch;
95         flush        = poll_flush;
96
97         int &fd = pfd.fd;
98         fd = -1;
99         const char *dirname = "/dev/input";
100         char prop[PROPERTY_VALUE_MAX];
101         if (property_get("hal.sensors.kbd.keys", prop, 0))
102                 sscanf(prop, "%s,%d,%d,%d,%d,%d,%d,%d,%d", ktype->name, ktype->keys,
103                                 ktype->keys + 1, ktype->keys + 2, ktype->keys + 3, ktype->keys + 4, ktype->keys + 5, ktype->keys + 6, ktype->keys + 7);
104         else if (property_get("hal.sensors.kbd.type", prop, 0))
105                 ktype = &KeysType[atoi(prop)];
106         else
107                 ktype = 0;
108         if (DIR *dir = opendir(dirname)) {
109                 char name[PATH_MAX];
110                 while (struct dirent *de = readdir(dir)) {
111                         if (de->d_name[0] != 'e') // not eventX
112                                 continue;
113                         snprintf(name, PATH_MAX, "%s/%s", dirname, de->d_name);
114                         fd = open(name, O_RDWR);
115                         if (fd < 0) {
116                                 ALOGE("could not open %s, %s", name, strerror(errno));
117                                 continue;
118                         }
119                         name[sizeof(name) - 1] = '\0';
120                         if (ioctl(fd, EVIOCGNAME(sizeof(name) - 1), &name) < 1) {
121                                 ALOGE("could not get device name for %s, %s\n", name, strerror(errno));
122                                 name[0] = '\0';
123                         }
124
125                         if (ktype) {
126                                 if (!strcmp(name, ktype->name))
127                                         break;
128                         } else {
129                                 ktype = KeysType + (sizeof(KeysType) / sizeof(KeysType[0]));
130                                 while (--ktype != KeysType)
131                                         if (!strcmp(name, ktype->name))
132                                                 break;
133                                 if (ktype != KeysType)
134                                         break;
135                                 else
136                                         ktype = 0;
137                         }
138                         close(fd);
139                         fd = -1;
140                 }
141                 closedir(dir);
142                 if (fd < 0) {
143                         ALOGW("could not find any kbdsensor device");
144                         return;
145                 }
146                 *device = &common;
147                 ALOGI("Open %s ok, fd=%d", name, fd);
148         }
149
150         pfd.events = POLLIN;
151         orients[ROT_0].version = sizeof(sensors_event_t);
152         orients[ROT_0].sensor = ID_ACCELERATION;
153         orients[ROT_0].type = SENSOR_TYPE_ACCELEROMETER;
154         orients[ROT_0].acceleration.status = SENSOR_STATUS_ACCURACY_HIGH;
155         orients[ROT_270] = orients[ROT_180] = orients[ROT_90] = orients[ROT_0];
156         const double angle = 20.0;
157         const double cos_angle = GRAVITY_EARTH * cos(angle / M_PI);
158         const double sin_angle = GRAVITY_EARTH * sin(angle / M_PI);
159         orients[ROT_0].acceleration.x   = 0.0;
160         orients[ROT_0].acceleration.y   = cos_angle;
161         orients[ROT_0].acceleration.z   = sin_angle;
162         orients[ROT_90].acceleration.x  = cos_angle;
163         orients[ROT_90].acceleration.y  = 0.0;
164         orients[ROT_90].acceleration.z  = sin_angle;
165         orients[ROT_180].acceleration.x = 0.0;
166         orients[ROT_180].acceleration.y = -cos_angle;
167         orients[ROT_180].acceleration.z = -sin_angle;
168         orients[ROT_270].acceleration.x = -cos_angle;
169         orients[ROT_270].acceleration.y = 0.0;
170         orients[ROT_270].acceleration.z = -sin_angle;
171
172         ALOGD("%s: module=%p dev=%p fd=%d", __FUNCTION__, module, this, fd);
173 }
174
175 SensorPollContext::~SensorPollContext()
176 {
177         close(pfd.fd);
178 }
179
180 int SensorPollContext::poll_close(struct hw_device_t *dev)
181 {
182         ALOGD("%s: dev=%p", __FUNCTION__, dev);
183         delete reinterpret_cast<SensorPollContext *>(dev);
184         return 0;
185 }
186
187 int SensorPollContext::poll_activate(struct sensors_poll_device_t *dev, int handle, int enabled)
188 {
189         ALOGD("%s: dev=%p handle=%d enabled=%d", __FUNCTION__, dev, handle, enabled);
190         SensorPollContext *ctx = reinterpret_cast<SensorPollContext *>(dev);
191         ctx->enabled = enabled;
192         return 0;
193 }
194
195 int SensorPollContext::poll_setDelay(struct sensors_poll_device_t *dev, int handle, int64_t ns)
196 {
197         ALOGD("%s: dev=%p handle=%d ns=%" PRId64, __FUNCTION__, dev, handle, ns);
198         SensorPollContext *ctx = reinterpret_cast<SensorPollContext *>(dev);
199         ctx->sampling_period_ns = ns;
200         return EXIT_SUCCESS;
201 }
202
203 int SensorPollContext::poll_poll(struct sensors_poll_device_t *dev, sensors_event_t *data, int count)
204 {
205         ALOGV("%s: dev=%p data=%p count=%d", __FUNCTION__, dev, data, count);
206         SensorPollContext *ctx = reinterpret_cast<SensorPollContext *>(dev);
207         return ctx->doPoll(data, count);
208 }
209
210 int SensorPollContext::poll_batch(struct sensors_poll_device_1* dev, int sensor_handle, int flags, int64_t sampling_period_ns, int64_t max_report_latency_ns)
211 {
212         ALOGD("%s: dev=%p sensor_handle=%d flags=%d sampling_period_ns=%" PRId64 " max_report_latency_ns=%" PRId64,
213                         __FUNCTION__, dev, sensor_handle, flags, sampling_period_ns, max_report_latency_ns);
214         return poll_setDelay(&dev->v0, sensor_handle, sampling_period_ns);
215 }
216
217 int SensorPollContext::poll_flush(struct sensors_poll_device_1* dev, int sensor_handle)
218 {
219         ALOGD("%s: dev=%p sensor_handle=%d", __FUNCTION__, dev, sensor_handle);
220         return EXIT_SUCCESS;
221 }
222
223 int SensorPollContext::doPoll(sensors_event_t *data, int count)
224 {
225         if (!isValid())
226                 return 0;
227
228         int *keys = ktype->keys;
229         while (int pollres = ::poll(&pfd, 1, -1)) {
230                 if (pollres < 0) {
231                         ALOGE("%s: poll %d error: %s", __FUNCTION__, pfd.fd, strerror(errno));
232                         break;
233                 }
234                 if (!(pfd.revents & POLLIN)) {
235                         ALOGW("%s: ignore revents %d", __FUNCTION__, pfd.revents);
236                         continue;
237                 }
238
239                 struct input_event iev;
240                 size_t res = ::read(pfd.fd, &iev, sizeof(iev));
241                 if (res < sizeof(iev)) {
242                         ALOGW("insufficient input data(%zu)? fd=%d", res, pfd.fd);
243                         continue;
244                 }
245                 ALOGV("type=%d scancode=%d value=%d from fd=%d", iev.type, iev.code, iev.value, pfd.fd);
246                 if (iev.type == keys[0]) {
247                         int rot;
248                         int input = (keys[0] == EV_MSC) ? iev.value : iev.code;
249                         if (input == keys[1])
250                                 rot = ROT_0;
251                         else if (input == keys[2])
252                                 rot = ROT_90;
253                         else if (input == keys[3])
254                                 rot = ROT_180;
255                         else if (input == keys[4])
256                                 rot = ROT_270;
257                         else if (input == keys[5] || input == keys[6])
258                                 rot = rotation;
259                         else
260                                 rot = -1;
261
262                         if (rot >= 0) {
263                                 if (rot != rotation) {
264                                         ALOGI("orientation changed from %d to %d", rotation * 90, rot * 90);
265                                         rotation = rot;
266                                 }
267                                 if (enabled && count > 0)
268                                         break;
269                         }
270                 } else if (iev.type == EV_KEY) {
271                         if (iev.code == keys[1] && iev.value) {
272                                 if (rotation == ROT_270)
273                                         rotation = ROT_0;
274                                 else
275                                         rotation++;
276                         }
277                         if (iev.code == keys[2] && iev.value) {
278                                 if (rotation == ROT_0)
279                                         rotation = ROT_270;
280                                 else
281                                         rotation--;
282                         }
283                         break;
284                 } else if (iev.type == EV_SW && iev.code == SW_TABLET_MODE) {
285                         if (!iev.value)
286                                 rotation = ROT_0;
287                         else if (rotation == ROT_0)
288                                 rotation = ROT_90;
289                         break;
290                 }
291         }
292
293         int cnt;
294         struct timespec t = { 0, 0 };
295         data[0] = orients[rotation];
296         clock_gettime(CLOCK_MONOTONIC, &t);
297         data[0].timestamp = int64_t(t.tv_sec) * 1000000000LL + t.tv_nsec;
298         struct timespec delay = { 0, static_cast<long>(sampling_period_ns) };
299         for (cnt = 1; !nanosleep(&delay, 0) && cnt < keys[7] && cnt < count; ++cnt) {
300                 data[cnt] = data[cnt - 1];
301                 data[cnt].timestamp += sampling_period_ns;
302         }
303         ALOGV("%s: dev=%p fd=%d rotation=%d cnt=%d", __FUNCTION__, this, pfd.fd, rotation * 90, cnt);
304         return cnt;
305 }
306
307 static int open_kbd_sensor(const struct hw_module_t *module, const char *id, struct hw_device_t **device)
308 {
309         ALOGD("%s: id=%s", __FUNCTION__, id);
310         SensorPollContext *ctx = new SensorPollContext(module, device);
311         return (ctx && ctx->isValid()) ? 0 : -EINVAL;
312 }
313
314 static struct sensor_t sSensorListInit[] = {
315         {
316                 .name = "Kbd Orientation Sensor",
317                 .vendor = "Android-x86 Open Source Project",
318                 .version = 2,
319                 .handle = ID_ACCELERATION,
320                 .type = SENSOR_TYPE_ACCELEROMETER,
321                 .maxRange = 2.8f,
322                 .resolution = 1.0f/4032.0f,
323                 .power = 3.0f,
324                 .minDelay = 0,
325                 .fifoReservedEventCount = 0,
326                 .fifoMaxEventCount = 0,
327                 .stringType = 0,
328                 .requiredPermission = 0,
329                 .maxDelay = 2000,
330                 .flags = SENSOR_FLAG_CONTINUOUS_MODE,
331                 .reserved = { }
332         }
333 };
334
335 static int sensors_get_sensors_list(struct sensors_module_t *, struct sensor_t const **list)
336 {
337         *list = sSensorListInit;
338         return sizeof(sSensorListInit) / sizeof(struct sensor_t);
339 }
340
341 static struct hw_module_methods_t sensors_methods = {
342         .open = open_kbd_sensor
343 };
344
345 struct sensors_module_t HAL_MODULE_INFO_SYM = {
346         .common = {
347                 .tag = HARDWARE_MODULE_TAG,
348                 .module_api_version = 2,
349                 .hal_api_version = 0,
350                 .id = SENSORS_HARDWARE_MODULE_ID,
351                 .name = "Kbd Orientation Sensor",
352                 .author = "Chih-Wei Huang",
353                 .methods = &sensors_methods,
354                 .dso = 0,
355                 .reserved = { }
356         },
357         .get_sensors_list = sensors_get_sensors_list
358 };