chiark / gitweb /
machine: make sure unpriviliged "machinectl status" can show the machine's OS version
[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 <stdbool.h>
50 #include <math.h>
51 #include <sys/types.h>
52 #include <sys/stat.h>
53 #include <fcntl.h>
54 #include <stdlib.h>
55 #include <unistd.h>
56 #include <getopt.h>
57 #include <limits.h>
58 #include <linux/limits.h>
59 #include <linux/input.h>
60
61 #include "libudev.h"
62 #include "libudev-private.h"
63
64 /* we must use this kernel-compatible implementation */
65 #define BITS_PER_LONG (sizeof(unsigned long) * 8)
66 #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
67 #define OFF(x)  ((x)%BITS_PER_LONG)
68 #define BIT(x)  (1UL<<OFF(x))
69 #define LONG(x) ((x)/BITS_PER_LONG)
70 #define test_bit(bit, array)    ((array[LONG(bit)] >> OFF(bit)) & 1)
71
72 static int debug = 0;
73
74 _printf_(6,0)
75 static void log_fn(struct udev *udev, int priority,
76                    const char *file, int line, const char *fn,
77                    const char *format, va_list args)
78 {
79         if (debug) {
80                 fprintf(stderr, "%s: ", fn);
81                 vfprintf(stderr, format, args);
82         } else {
83                 vsyslog(priority, format, args);
84         }
85 }
86
87 typedef enum {
88         ORIENTATION_UNDEFINED,
89         ORIENTATION_NORMAL,
90         ORIENTATION_BOTTOM_UP,
91         ORIENTATION_LEFT_UP,
92         ORIENTATION_RIGHT_UP
93 } OrientationUp;
94
95 static const char *orientations[] = {
96         "undefined",
97         "normal",
98         "bottom-up",
99         "left-up",
100         "right-up",
101         NULL
102 };
103
104 #define ORIENTATION_UP_UP ORIENTATION_NORMAL
105
106 #define DEFAULT_THRESHOLD 250
107 #define RADIANS_TO_DEGREES 180.0/M_PI
108 #define SAME_AXIS_LIMIT 5
109
110 #define THRESHOLD_LANDSCAPE  25
111 #define THRESHOLD_PORTRAIT  20
112
113 static const char *
114 orientation_to_string (OrientationUp o)
115 {
116         return orientations[o];
117 }
118
119 static OrientationUp
120 string_to_orientation (const char *orientation)
121 {
122         int i;
123
124         if (orientation == NULL)
125                 return ORIENTATION_UNDEFINED;
126         for (i = 0; orientations[i] != NULL; i++) {
127                 if (streq (orientation, orientations[i]))
128                         return i;
129         }
130         return ORIENTATION_UNDEFINED;
131 }
132
133 static OrientationUp
134 orientation_calc (OrientationUp prev,
135                   int x, int y, int z)
136 {
137         int rotation;
138         OrientationUp ret = prev;
139
140         /* Portrait check */
141         rotation = round(atan((double) x / sqrt(y * y + z * z)) * RADIANS_TO_DEGREES);
142
143         if (abs(rotation) > THRESHOLD_PORTRAIT) {
144                 ret = (rotation < 0) ? ORIENTATION_LEFT_UP : ORIENTATION_RIGHT_UP;
145
146                 /* Some threshold to switching between portrait modes */
147                 if (prev == ORIENTATION_LEFT_UP || prev == ORIENTATION_RIGHT_UP) {
148                         if (abs(rotation) < SAME_AXIS_LIMIT) {
149                                 ret = prev;
150                         }
151                 }
152
153         } else {
154                 /* Landscape check */
155                 rotation = round(atan((double) y / sqrt(x * x + z * z)) * RADIANS_TO_DEGREES);
156
157                 if (abs(rotation) > THRESHOLD_LANDSCAPE) {
158                         ret = (rotation < 0) ? ORIENTATION_BOTTOM_UP : ORIENTATION_NORMAL;
159
160                         /* Some threshold to switching between landscape modes */
161                         if (prev == ORIENTATION_BOTTOM_UP || prev == ORIENTATION_NORMAL) {
162                                 if (abs(rotation) < SAME_AXIS_LIMIT) {
163                                         ret = prev;
164                                 }
165                         }
166                 }
167         }
168
169         return ret;
170 }
171
172 static OrientationUp
173 get_prev_orientation(struct udev_device *dev)
174 {
175         const char *value;
176
177         value = udev_device_get_property_value(dev, "ID_INPUT_ACCELEROMETER_ORIENTATION");
178         if (value == NULL)
179                 return ORIENTATION_UNDEFINED;
180         return string_to_orientation(value);
181 }
182
183 #define READ_AXIS(axis, var) { memzero(&abs_info, sizeof(abs_info)); r = ioctl(fd, EVIOCGABS(axis), &abs_info); if (r < 0) return; var = abs_info.value; }
184
185 /* accelerometers */
186 static void test_orientation(struct udev *udev,
187                              struct udev_device *dev,
188                              const char *devpath)
189 {
190         OrientationUp old, new;
191         _cleanup_close_ int fd = -1;
192         struct input_absinfo abs_info;
193         int x = 0, y = 0, z = 0;
194         int r;
195         char text[64];
196
197         old = get_prev_orientation(dev);
198
199         fd = open(devpath, O_RDONLY|O_CLOEXEC);
200         if (fd < 0)
201                 return;
202
203         READ_AXIS(ABS_X, x);
204         READ_AXIS(ABS_Y, y);
205         READ_AXIS(ABS_Z, z);
206
207         new = orientation_calc(old, x, y, z);
208         snprintf(text, sizeof(text),
209                  "ID_INPUT_ACCELEROMETER_ORIENTATION=%s", orientation_to_string(new));
210         puts(text);
211 }
212
213 static void help(void)
214 {
215         printf("Usage: accelerometer [options] <device path>\n"
216                "  --debug         debug to stderr\n"
217                "  --help          print this help text\n\n");
218 }
219
220 int main (int argc, char** argv)
221 {
222         struct udev *udev;
223         struct udev_device *dev;
224
225         static const struct option options[] = {
226                 { "debug", no_argument, NULL, 'd' },
227                 { "help", no_argument, NULL, 'h' },
228                 {}
229         };
230
231         char devpath[PATH_MAX];
232         char *devnode;
233         struct udev_enumerate *enumerate;
234         struct udev_list_entry *list_entry;
235
236         udev = udev_new();
237         if (udev == NULL)
238                 return 1;
239
240         log_open();
241         udev_set_log_fn(udev, log_fn);
242
243         /* CLI argument parsing */
244         while (1) {
245                 int option;
246
247                 option = getopt_long(argc, argv, "dxh", options, NULL);
248                 if (option == -1)
249                         break;
250
251                 switch (option) {
252                 case 'd':
253                         debug = 1;
254                         log_set_max_level(LOG_DEBUG);
255                         udev_set_log_priority(udev, LOG_DEBUG);
256                         break;
257                 case 'h':
258                         help();
259                         exit(0);
260                 default:
261                         exit(1);
262                 }
263         }
264
265         if (argv[optind] == NULL) {
266                 help();
267                 exit(1);
268         }
269
270         /* get the device */
271         snprintf(devpath, sizeof(devpath), "/sys/%s", argv[optind]);
272         dev = udev_device_new_from_syspath(udev, devpath);
273         if (dev == NULL) {
274                 fprintf(stderr, "unable to access '%s'\n", devpath);
275                 return 1;
276         }
277
278         /* Get the children devices and find the devnode */
279         devnode = NULL;
280         enumerate = udev_enumerate_new(udev);
281         udev_enumerate_add_match_parent(enumerate, dev);
282         udev_enumerate_scan_devices(enumerate);
283         udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(enumerate)) {
284                 struct udev_device *device;
285                 const char *node;
286
287                 device = udev_device_new_from_syspath(udev_enumerate_get_udev(enumerate),
288                                                       udev_list_entry_get_name(list_entry));
289                 if (device == NULL)
290                         continue;
291                 /* Already found it */
292                 if (devnode != NULL) {
293                         udev_device_unref(device);
294                         continue;
295                 }
296
297                 node = udev_device_get_devnode(device);
298                 if (node == NULL) {
299                         udev_device_unref(device);
300                         continue;
301                 }
302                 /* Use the event sub-device */
303                 if (strstr(node, "/event") == NULL) {
304                         udev_device_unref(device);
305                         continue;
306                 }
307
308                 devnode = strdup(node);
309                 udev_device_unref(device);
310         }
311
312         if (devnode == NULL) {
313                 fprintf(stderr, "unable to get device node for '%s'\n", devpath);
314                 return 0;
315         }
316
317         log_debug("opening accelerometer device %s", devnode);
318         test_orientation(udev, dev, devnode);
319         free(devnode);
320         log_close();
321         return 0;
322 }