2 * accelerometer - exports device orientation through property
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.
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
21 * Copyright (C) 2011 Red Hat, Inc.
23 * Bastien Nocera <hadess@hadess.net>
25 * orientation_calc() from the sensorfw package
26 * Copyright (C) 2009-2010 Nokia Corporation
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>
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.
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.
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.
51 #include <sys/types.h>
58 #include <linux/limits.h>
59 #include <linux/input.h>
62 #include "libudev-private.h"
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)
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)
80 fprintf(stderr, "%s: ", fn);
81 vfprintf(stderr, format, args);
83 vsyslog(priority, format, args);
88 ORIENTATION_UNDEFINED,
90 ORIENTATION_BOTTOM_UP,
95 static const char *orientations[] = {
104 #define ORIENTATION_UP_UP ORIENTATION_NORMAL
106 #define DEFAULT_THRESHOLD 250
107 #define RADIANS_TO_DEGREES 180.0/M_PI
108 #define SAME_AXIS_LIMIT 5
110 #define THRESHOLD_LANDSCAPE 25
111 #define THRESHOLD_PORTRAIT 20
114 orientation_to_string (OrientationUp o)
116 return orientations[o];
120 string_to_orientation (const char *orientation)
124 if (orientation == NULL)
125 return ORIENTATION_UNDEFINED;
126 for (i = 0; orientations[i] != NULL; i++) {
127 if (streq (orientation, orientations[i]))
130 return ORIENTATION_UNDEFINED;
134 orientation_calc (OrientationUp prev,
138 OrientationUp ret = prev;
141 rotation = round(atan((double) x / sqrt(y * y + z * z)) * RADIANS_TO_DEGREES);
143 if (abs(rotation) > THRESHOLD_PORTRAIT) {
144 ret = (rotation < 0) ? ORIENTATION_LEFT_UP : ORIENTATION_RIGHT_UP;
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) {
154 /* Landscape check */
155 rotation = round(atan((double) y / sqrt(x * x + z * z)) * RADIANS_TO_DEGREES);
157 if (abs(rotation) > THRESHOLD_LANDSCAPE) {
158 ret = (rotation < 0) ? ORIENTATION_BOTTOM_UP : ORIENTATION_NORMAL;
160 /* Some threshold to switching between landscape modes */
161 if (prev == ORIENTATION_BOTTOM_UP || prev == ORIENTATION_NORMAL) {
162 if (abs(rotation) < SAME_AXIS_LIMIT) {
173 get_prev_orientation(struct udev_device *dev)
177 value = udev_device_get_property_value(dev, "ID_INPUT_ACCELEROMETER_ORIENTATION");
179 return ORIENTATION_UNDEFINED;
180 return string_to_orientation(value);
183 #define SET_AXIS(axis, code_) if (ev[i].code == code_) { if (got_##axis == 0) { axis = ev[i].value; got_##axis = true; } }
186 static void test_orientation(struct udev *udev,
187 struct udev_device *dev,
190 OrientationUp old, new;
191 _cleanup_close_ int fd = -1;
192 struct input_event ev[64];
193 bool got_syn = false;
194 bool got_x = false, got_y = false, got_z = false;
195 int x = 0, y = 0, z = 0;
198 old = get_prev_orientation(dev);
200 fd = open(devpath, O_RDONLY|O_CLOEXEC);
207 r = read(fd, ev, sizeof(struct input_event) * 64);
209 if (r < (int) sizeof(struct input_event))
212 for (i = 0; i < r / (int) sizeof(struct input_event); i++) {
214 if (ev[i].type == EV_ABS) {
220 if (ev[i].type == EV_SYN && ev[i].code == SYN_REPORT)
222 if (got_x && got_y && got_z)
228 new = orientation_calc(old, x, y, z);
229 snprintf(text, sizeof(text),
230 "ID_INPUT_ACCELEROMETER_ORIENTATION=%s", orientation_to_string(new));
234 static void help(void)
236 printf("Usage: accelerometer [options] <device path>\n"
237 " --debug debug to stderr\n"
238 " --help print this help text\n\n");
241 int main (int argc, char** argv)
244 struct udev_device *dev;
246 static const struct option options[] = {
247 { "debug", no_argument, NULL, 'd' },
248 { "help", no_argument, NULL, 'h' },
252 char devpath[PATH_MAX];
254 struct udev_enumerate *enumerate;
255 struct udev_list_entry *list_entry;
262 udev_set_log_fn(udev, log_fn);
264 /* CLI argument parsing */
268 option = getopt_long(argc, argv, "dxh", options, NULL);
275 log_set_max_level(LOG_DEBUG);
276 udev_set_log_priority(udev, LOG_DEBUG);
286 if (argv[optind] == NULL) {
292 snprintf(devpath, sizeof(devpath), "/sys/%s", argv[optind]);
293 dev = udev_device_new_from_syspath(udev, devpath);
295 fprintf(stderr, "unable to access '%s'\n", devpath);
299 /* Get the children devices and find the devnode */
301 enumerate = udev_enumerate_new(udev);
302 udev_enumerate_add_match_parent(enumerate, dev);
303 udev_enumerate_scan_devices(enumerate);
304 udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(enumerate)) {
305 struct udev_device *device;
308 device = udev_device_new_from_syspath(udev_enumerate_get_udev(enumerate),
309 udev_list_entry_get_name(list_entry));
312 /* Already found it */
313 if (devnode != NULL) {
314 udev_device_unref(device);
318 node = udev_device_get_devnode(device);
320 udev_device_unref(device);
323 /* Use the event sub-device */
324 if (strstr(node, "/event") == NULL) {
325 udev_device_unref(device);
329 devnode = strdup(node);
330 udev_device_unref(device);
333 if (devnode == NULL) {
334 fprintf(stderr, "unable to get device node for '%s'\n", devpath);
338 log_debug("opening accelerometer device %s", devnode);
339 test_orientation(udev, dev, devnode);