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