chiark / gitweb /
use streq instead of strcmp
[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
33  * it 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,
38  * but WITHOUT ANY WARRANTY; without even the implied warranty of
39  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
40  * GNU General Public License for more details.
41  *
42  * You should have received a copy of the GNU General Public License along
43  * with this program; if not, write to the Free Software Foundation, Inc.,
44  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 (streq (orientation, orientations[i]))
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                         close(fd);
210                         return;
211                 }
212
213                 for (i = 0; i < r / (int) sizeof(struct input_event); i++) {
214                         if (got_syn == 1) {
215                                 if (ev[i].type == EV_ABS) {
216                                         SET_AXIS(x, ABS_X);
217                                         SET_AXIS(y, ABS_Y);
218                                         SET_AXIS(z, ABS_Z);
219                                 }
220                         }
221                         if (ev[i].type == EV_SYN && ev[i].code == SYN_REPORT) {
222                                 got_syn = 1;
223                         }
224                         if (got_x && got_y && got_z)
225                                 goto read_dev;
226                 }
227         }
228
229 read_dev:
230         close(fd);
231
232         if (!got_x || !got_y || !got_z)
233                 return;
234
235         new = orientation_calc(old, x, y, z);
236         snprintf(text, sizeof(text), "ID_INPUT_ACCELEROMETER_ORIENTATION=%s", orientation_to_string(new));
237         puts(text);
238 }
239
240 static void help(void)
241 {
242         printf("Usage: accelerometer [options] <device path>\n"
243                "  --debug         debug to stderr\n"
244                "  --help          print this help text\n\n");
245 }
246
247 int main (int argc, char** argv)
248 {
249         struct udev *udev;
250         struct udev_device *dev;
251
252         static const struct option options[] = {
253                 { "debug", no_argument, NULL, 'd' },
254                 { "help", no_argument, NULL, 'h' },
255                 {}
256         };
257
258         char devpath[PATH_MAX];
259         char *devnode;
260         struct udev_enumerate *enumerate;
261         struct udev_list_entry *list_entry;
262
263         udev = udev_new();
264         if (udev == NULL)
265                 return 1;
266
267         log_open();
268         udev_set_log_fn(udev, log_fn);
269
270         /* CLI argument parsing */
271         while (1) {
272                 int option;
273
274                 option = getopt_long(argc, argv, "dxh", options, NULL);
275                 if (option == -1)
276                         break;
277
278                 switch (option) {
279                 case 'd':
280                         debug = 1;
281                         log_set_max_level(LOG_DEBUG);
282                         udev_set_log_priority(udev, LOG_DEBUG);
283                         break;
284                 case 'h':
285                         help();
286                         exit(0);
287                 default:
288                         exit(1);
289                 }
290         }
291
292         if (argv[optind] == NULL) {
293                 help();
294                 exit(1);
295         }
296
297         /* get the device */
298         snprintf(devpath, sizeof(devpath), "/sys/%s", argv[optind]);
299         dev = udev_device_new_from_syspath(udev, devpath);
300         if (dev == NULL) {
301                 fprintf(stderr, "unable to access '%s'\n", devpath);
302                 return 1;
303         }
304
305         /* Get the children devices and find the devnode */
306         devnode = NULL;
307         enumerate = udev_enumerate_new(udev);
308         udev_enumerate_add_match_parent(enumerate, dev);
309         udev_enumerate_scan_devices(enumerate);
310         udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(enumerate)) {
311                 struct udev_device *device;
312                 const char *node;
313
314                 device = udev_device_new_from_syspath(udev_enumerate_get_udev(enumerate),
315                                                       udev_list_entry_get_name(list_entry));
316                 if (device == NULL)
317                         continue;
318                 /* Already found it */
319                 if (devnode != NULL) {
320                         udev_device_unref(device);
321                         continue;
322                 }
323
324                 node = udev_device_get_devnode(device);
325                 if (node == NULL) {
326                         udev_device_unref(device);
327                         continue;
328                 }
329                 /* Use the event sub-device */
330                 if (strstr(node, "/event") == NULL) {
331                         udev_device_unref(device);
332                         continue;
333                 }
334
335                 devnode = strdup(node);
336                 udev_device_unref(device);
337         }
338
339         if (devnode == NULL) {
340                 fprintf(stderr, "unable to get device node for '%s'\n", devpath);
341                 return 0;
342         }
343
344         log_debug("opening accelerometer device %s\n", devnode);
345         test_orientation(udev, dev, devnode);
346         free(devnode);
347         log_close();
348         return 0;
349 }