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