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)
74 static void log_fn(struct udev *udev, int priority,
75 const char *file, int line, const char *fn,
76 const char *format, va_list args)
79 fprintf(stderr, "%s: ", fn);
80 vfprintf(stderr, format, args);
82 vsyslog(priority, format, args);
87 ORIENTATION_UNDEFINED,
89 ORIENTATION_BOTTOM_UP,
94 static const char *orientations[] = {
103 #define ORIENTATION_UP_UP ORIENTATION_NORMAL
105 #define DEFAULT_THRESHOLD 250
106 #define RADIANS_TO_DEGREES 180.0/M_PI
107 #define SAME_AXIS_LIMIT 5
109 #define THRESHOLD_LANDSCAPE 25
110 #define THRESHOLD_PORTRAIT 20
113 orientation_to_string (OrientationUp o)
115 return orientations[o];
119 string_to_orientation (const char *orientation)
123 if (orientation == NULL)
124 return ORIENTATION_UNDEFINED;
125 for (i = 0; orientations[i] != NULL; i++) {
126 if (streq (orientation, orientations[i]))
129 return ORIENTATION_UNDEFINED;
133 orientation_calc (OrientationUp prev,
137 OrientationUp ret = prev;
140 rotation = round(atan((double) x / sqrt(y * y + z * z)) * RADIANS_TO_DEGREES);
142 if (abs(rotation) > THRESHOLD_PORTRAIT) {
143 ret = (rotation < 0) ? ORIENTATION_LEFT_UP : ORIENTATION_RIGHT_UP;
145 /* Some threshold to switching between portrait modes */
146 if (prev == ORIENTATION_LEFT_UP || prev == ORIENTATION_RIGHT_UP) {
147 if (abs(rotation) < SAME_AXIS_LIMIT) {
153 /* Landscape check */
154 rotation = round(atan((double) y / sqrt(x * x + z * z)) * RADIANS_TO_DEGREES);
156 if (abs(rotation) > THRESHOLD_LANDSCAPE) {
157 ret = (rotation < 0) ? ORIENTATION_BOTTOM_UP : ORIENTATION_NORMAL;
159 /* Some threshold to switching between landscape modes */
160 if (prev == ORIENTATION_BOTTOM_UP || prev == ORIENTATION_NORMAL) {
161 if (abs(rotation) < SAME_AXIS_LIMIT) {
172 get_prev_orientation(struct udev_device *dev)
176 value = udev_device_get_property_value(dev, "ID_INPUT_ACCELEROMETER_ORIENTATION");
178 return ORIENTATION_UNDEFINED;
179 return string_to_orientation(value);
182 #define SET_AXIS(axis, code_) if (ev[i].code == code_) { if (got_##axis == 0) { axis = ev[i].value; got_##axis = true; } }
185 static void test_orientation(struct udev *udev,
186 struct udev_device *dev,
189 OrientationUp old, new;
190 _cleanup_close_ int fd = -1;
191 struct input_event ev[64];
192 bool got_syn = false;
193 bool got_x = false, got_y = false, got_z = false;
194 int x = 0, y = 0, z = 0;
197 old = get_prev_orientation(dev);
199 fd = open(devpath, O_RDONLY);
206 r = read(fd, ev, sizeof(struct input_event) * 64);
208 if (r < (int) sizeof(struct input_event))
211 for (i = 0; i < r / (int) sizeof(struct input_event); i++) {
213 if (ev[i].type == EV_ABS) {
219 if (ev[i].type == EV_SYN && ev[i].code == SYN_REPORT)
221 if (got_x && got_y && got_z)
227 new = orientation_calc(old, x, y, z);
228 snprintf(text, sizeof(text),
229 "ID_INPUT_ACCELEROMETER_ORIENTATION=%s", orientation_to_string(new));
233 static void help(void)
235 printf("Usage: accelerometer [options] <device path>\n"
236 " --debug debug to stderr\n"
237 " --help print this help text\n\n");
240 int main (int argc, char** argv)
243 struct udev_device *dev;
245 static const struct option options[] = {
246 { "debug", no_argument, NULL, 'd' },
247 { "help", no_argument, NULL, 'h' },
251 char devpath[PATH_MAX];
253 struct udev_enumerate *enumerate;
254 struct udev_list_entry *list_entry;
261 udev_set_log_fn(udev, log_fn);
263 /* CLI argument parsing */
267 option = getopt_long(argc, argv, "dxh", options, NULL);
274 log_set_max_level(LOG_DEBUG);
275 udev_set_log_priority(udev, LOG_DEBUG);
285 if (argv[optind] == NULL) {
291 snprintf(devpath, sizeof(devpath), "/sys/%s", argv[optind]);
292 dev = udev_device_new_from_syspath(udev, devpath);
294 fprintf(stderr, "unable to access '%s'\n", devpath);
298 /* Get the children devices and find the devnode */
300 enumerate = udev_enumerate_new(udev);
301 udev_enumerate_add_match_parent(enumerate, dev);
302 udev_enumerate_scan_devices(enumerate);
303 udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(enumerate)) {
304 struct udev_device *device;
307 device = udev_device_new_from_syspath(udev_enumerate_get_udev(enumerate),
308 udev_list_entry_get_name(list_entry));
311 /* Already found it */
312 if (devnode != NULL) {
313 udev_device_unref(device);
317 node = udev_device_get_devnode(device);
319 udev_device_unref(device);
322 /* Use the event sub-device */
323 if (strstr(node, "/event") == NULL) {
324 udev_device_unref(device);
328 devnode = strdup(node);
329 udev_device_unref(device);
332 if (devnode == NULL) {
333 fprintf(stderr, "unable to get device node for '%s'\n", devpath);
337 log_debug("opening accelerometer device %s\n", devnode);
338 test_orientation(udev, dev, devnode);