chiark / gitweb /
4c5e47d0b70f9b683fa7db817a7b1ae28176d924
[elogind.git] / src / udev / accelerometer / accelerometer.c
1 /*
2  * accelerometer - exports device orientation through property
3  *
4  * When an "change" event is received on an accelerometer,
5  * open its device node, and from the value, as well as the previous
6  * value of the property, calculate the device's new orientation,
7  * and export it as ID_INPUT_ACCELEROMETER_ORIENTATION.
8  *
9  * Possible values are:
10  * undefined
11  * * normal
12  * * bottom-up
13  * * left-up
14  * * right-up
15  *
16  * The property will be persistent across sessions, and the new
17  * orientations can be deducted from the previous one (it allows
18  * for a threshold for switching between opposite ends of the
19  * orientation).
20  *
21  * Copyright (C) 2011 Red Hat, Inc.
22  * Author:
23  *   Bastien Nocera <hadess@hadess.net>
24  *
25  * orientation_calc() from the sensorfw package
26  * Copyright (C) 2009-2010 Nokia Corporation
27  * Authors:
28  *   Üstün Ergenoglu <ext-ustun.ergenoglu@nokia.com>
29  *   Timo Rongas <ext-timo.2.rongas@nokia.com>
30  *   Lihan Guo <lihan.guo@digia.com>
31  *
32  * This program is free software; you can redistribute it and/or modify it
33  * under the terms of the GNU General Public License as published by
34  * the Free Software Foundation; either version 2 of the License, or
35  * (at your option) any later version.
36  *
37  * This program is distributed in the hope that it will be useful, but
38  * WITHOUT ANY WARRANTY; without even the implied warranty of
39  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
40  * General Public License for more details.
41  *
42  * You should have received a copy of the GNU General Public License
43  * along with keymap; if not, write to the Free Software Foundation,
44  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
45  */
46
47 #include <stdio.h>
48 #include <string.h>
49 #include <math.h>
50 #include <sys/types.h>
51 #include <sys/stat.h>
52 #include <fcntl.h>
53 #include <stdlib.h>
54 #include <unistd.h>
55 #include <getopt.h>
56 #include <limits.h>
57 #include <linux/limits.h>
58 #include <linux/input.h>
59
60 #include "libudev.h"
61 #include "libudev-private.h"
62
63 /* we must use this kernel-compatible implementation */
64 #define BITS_PER_LONG (sizeof(unsigned long) * 8)
65 #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
66 #define OFF(x)  ((x)%BITS_PER_LONG)
67 #define BIT(x)  (1UL<<OFF(x))
68 #define LONG(x) ((x)/BITS_PER_LONG)
69 #define test_bit(bit, array)    ((array[LONG(bit)] >> OFF(bit)) & 1)
70
71 static int debug = 0;
72
73 static void log_fn(struct udev *udev, int priority,
74                    const char *file, int line, const char *fn,
75                    const char *format, va_list args)
76 {
77         if (debug) {
78                 fprintf(stderr, "%s: ", fn);
79                 vfprintf(stderr, format, args);
80         } else {
81                 vsyslog(priority, format, args);
82         }
83 }
84
85 typedef enum {
86         ORIENTATION_UNDEFINED,
87         ORIENTATION_NORMAL,
88         ORIENTATION_BOTTOM_UP,
89         ORIENTATION_LEFT_UP,
90         ORIENTATION_RIGHT_UP
91 } OrientationUp;
92
93 static const char *orientations[] = {
94         "undefined",
95         "normal",
96         "bottom-up",
97         "left-up",
98         "right-up",
99         NULL
100 };
101
102 #define ORIENTATION_UP_UP ORIENTATION_NORMAL
103
104 #define DEFAULT_THRESHOLD 250
105 #define RADIANS_TO_DEGREES 180.0/M_PI
106 #define SAME_AXIS_LIMIT 5
107
108 #define THRESHOLD_LANDSCAPE  25
109 #define THRESHOLD_PORTRAIT  20
110
111 static const char *
112 orientation_to_string (OrientationUp o)
113 {
114         return orientations[o];
115 }
116
117 static OrientationUp
118 string_to_orientation (const char *orientation)
119 {
120         int i;
121
122         if (orientation == NULL)
123                 return ORIENTATION_UNDEFINED;
124         for (i = 0; orientations[i] != NULL; i++) {
125                 if (strcmp (orientation, orientations[i]) == 0)
126                         return i;
127         }
128         return ORIENTATION_UNDEFINED;
129 }
130
131 static OrientationUp
132 orientation_calc (OrientationUp prev,
133                   int x, int y, int z)
134 {
135         int rotation;
136         OrientationUp ret = prev;
137
138         /* Portrait check */
139         rotation = round(atan((double) x / sqrt(y * y + z * z)) * RADIANS_TO_DEGREES);
140
141         if (abs(rotation) > THRESHOLD_PORTRAIT) {
142                 ret = (rotation < 0) ? ORIENTATION_LEFT_UP : ORIENTATION_RIGHT_UP;
143
144                 /* Some threshold to switching between portrait modes */
145                 if (prev == ORIENTATION_LEFT_UP || prev == ORIENTATION_RIGHT_UP) {
146                         if (abs(rotation) < SAME_AXIS_LIMIT) {
147                                 ret = prev;
148                         }
149                 }
150
151         } else {
152                 /* Landscape check */
153                 rotation = round(atan((double) y / sqrt(x * x + z * z)) * RADIANS_TO_DEGREES);
154
155                 if (abs(rotation) > THRESHOLD_LANDSCAPE) {
156                         ret = (rotation < 0) ? ORIENTATION_BOTTOM_UP : ORIENTATION_NORMAL;
157
158                         /* Some threshold to switching between landscape modes */
159                         if (prev == ORIENTATION_BOTTOM_UP || prev == ORIENTATION_NORMAL) {
160                                 if (abs(rotation) < SAME_AXIS_LIMIT) {
161                                         ret = prev;
162                                 }
163                         }
164                 }
165         }
166
167         return ret;
168 }
169
170 static OrientationUp
171 get_prev_orientation(struct udev_device *dev)
172 {
173         const char *value;
174
175         value = udev_device_get_property_value(dev, "ID_INPUT_ACCELEROMETER_ORIENTATION");
176         if (value == NULL)
177                 return ORIENTATION_UNDEFINED;
178         return string_to_orientation(value);
179 }
180
181 #define SET_AXIS(axis, code_) if (ev[i].code == code_) { if (got_##axis == 0) { axis = ev[i].value; got_##axis = 1; } }
182
183 /* accelerometers */
184 static void test_orientation(struct udev *udev,
185                              struct udev_device *dev,
186                              const char *devpath)
187 {
188         OrientationUp old, new;
189         int fd, r;
190         struct input_event ev[64];
191         int got_syn = 0;
192         int got_x, got_y, got_z;
193         int x = 0, y = 0, z = 0;
194         char text[64];
195
196         old = get_prev_orientation(dev);
197
198         if ((fd = open(devpath, O_RDONLY)) < 0)
199                 return;
200
201         got_x = got_y = got_z = 0;
202
203         while (1) {
204                 int i;
205
206                 r = read(fd, ev, sizeof(struct input_event) * 64);
207
208                 if (r < (int) sizeof(struct input_event))
209                         return;
210
211                 for (i = 0; i < r / (int) sizeof(struct input_event); i++) {
212                         if (got_syn == 1) {
213                                 if (ev[i].type == EV_ABS) {
214                                         SET_AXIS(x, ABS_X);
215                                         SET_AXIS(y, ABS_Y);
216                                         SET_AXIS(z, ABS_Z);
217                                 }
218                         }
219                         if (ev[i].type == EV_SYN && ev[i].code == SYN_REPORT) {
220                                 got_syn = 1;
221                         }
222                         if (got_x && got_y && got_z)
223                                 goto read_dev;
224                 }
225         }
226
227 read_dev:
228         close(fd);
229
230         if (!got_x || !got_y || !got_z)
231                 return;
232
233         new = orientation_calc(old, x, y, z);
234         snprintf(text, sizeof(text), "ID_INPUT_ACCELEROMETER_ORIENTATION=%s", orientation_to_string(new));
235         puts(text);
236 }
237
238 static void help(void)
239 {
240         printf("Usage: accelerometer [options] <device path>\n"
241                "  --debug         debug to stderr\n"
242                "  --help          print this help text\n\n");
243 }
244
245 int main (int argc, char** argv)
246 {
247         struct udev *udev;
248         struct udev_device *dev;
249
250         static const struct option options[] = {
251                 { "debug", no_argument, NULL, 'd' },
252                 { "help", no_argument, NULL, 'h' },
253                 {}
254         };
255
256         char devpath[PATH_MAX];
257         char *devnode;
258         const char *id_path;
259         struct udev_enumerate *enumerate;
260         struct udev_list_entry *list_entry;
261
262         udev = udev_new();
263         if (udev == NULL)
264                 return 1;
265
266         log_open();
267         udev_set_log_fn(udev, log_fn);
268
269         /* CLI argument parsing */
270         while (1) {
271                 int option;
272
273                 option = getopt_long(argc, argv, "dxh", options, NULL);
274                 if (option == -1)
275                         break;
276
277                 switch (option) {
278                 case 'd':
279                         debug = 1;
280                         if (udev_get_log_priority(udev) < LOG_INFO)
281                                 udev_set_log_priority(udev, LOG_INFO);
282                         break;
283                 case 'h':
284                         help();
285                         exit(0);
286                 default:
287                         exit(1);
288                 }
289         }
290
291         if (argv[optind] == NULL) {
292                 help();
293                 exit(1);
294         }
295
296         /* get the device */
297         snprintf(devpath, sizeof(devpath), "%s/%s", udev_get_sys_path(udev), argv[optind]);
298         dev = udev_device_new_from_syspath(udev, devpath);
299         if (dev == NULL) {
300                 fprintf(stderr, "unable to access '%s'\n", devpath);
301                 return 1;
302         }
303
304         id_path = udev_device_get_property_value(dev, "ID_PATH");
305         if (id_path == NULL) {
306                 fprintf (stderr, "unable to get property ID_PATH for '%s'", devpath);
307                 return 0;
308         }
309
310         /* Get the children devices and find the devnode
311          * FIXME: use udev_enumerate_add_match_children() instead
312          * when it's available */
313         devnode = NULL;
314         enumerate = udev_enumerate_new(udev);
315         udev_enumerate_add_match_property(enumerate, "ID_PATH", id_path);
316         udev_enumerate_add_match_subsystem(enumerate, "input");
317         udev_enumerate_scan_devices(enumerate);
318         udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(enumerate)) {
319                 struct udev_device *device;
320                 const char *node;
321
322                 device = udev_device_new_from_syspath(udev_enumerate_get_udev(enumerate),
323                                                       udev_list_entry_get_name(list_entry));
324                 if (device == NULL)
325                         continue;
326                 /* Already found it */
327                 if (devnode != NULL) {
328                         udev_device_unref(device);
329                         continue;
330                 }
331
332                 node = udev_device_get_devnode(device);
333                 if (node == NULL) {
334                         udev_device_unref(device);
335                         continue;
336                 }
337                 /* Use the event sub-device */
338                 if (strstr(node, "/event") == NULL) {
339                         udev_device_unref(device);
340                         continue;
341                 }
342
343                 devnode = strdup(node);
344                 udev_device_unref(device);
345         }
346
347         if (devnode == NULL) {
348                 fprintf(stderr, "unable to get device node for '%s'\n", devpath);
349                 return 0;
350         }
351
352         log_debug("opening accelerometer device %s\n", devnode);
353         test_orientation(udev, dev, devnode);
354         free(devnode);
355         log_close();
356         return 0;
357 }