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)
73 ORIENTATION_UNDEFINED,
75 ORIENTATION_BOTTOM_UP,
80 static const char *orientations[] = {
89 #define ORIENTATION_UP_UP ORIENTATION_NORMAL
91 #define DEFAULT_THRESHOLD 250
92 #define RADIANS_TO_DEGREES 180.0/M_PI
93 #define SAME_AXIS_LIMIT 5
95 #define THRESHOLD_LANDSCAPE 25
96 #define THRESHOLD_PORTRAIT 20
99 orientation_to_string (OrientationUp o)
101 return orientations[o];
105 string_to_orientation (const char *orientation)
109 if (orientation == NULL)
110 return ORIENTATION_UNDEFINED;
111 for (i = 0; orientations[i] != NULL; i++) {
112 if (streq (orientation, orientations[i]))
115 return ORIENTATION_UNDEFINED;
119 orientation_calc (OrientationUp prev,
123 OrientationUp ret = prev;
126 rotation = round(atan((double) x / sqrt(y * y + z * z)) * RADIANS_TO_DEGREES);
128 if (abs(rotation) > THRESHOLD_PORTRAIT) {
129 ret = (rotation < 0) ? ORIENTATION_LEFT_UP : ORIENTATION_RIGHT_UP;
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) {
139 /* Landscape check */
140 rotation = round(atan((double) y / sqrt(x * x + z * z)) * RADIANS_TO_DEGREES);
142 if (abs(rotation) > THRESHOLD_LANDSCAPE) {
143 ret = (rotation < 0) ? ORIENTATION_BOTTOM_UP : ORIENTATION_NORMAL;
145 /* Some threshold to switching between landscape modes */
146 if (prev == ORIENTATION_BOTTOM_UP || prev == ORIENTATION_NORMAL) {
147 if (abs(rotation) < SAME_AXIS_LIMIT) {
158 get_prev_orientation(struct udev_device *dev)
162 value = udev_device_get_property_value(dev, "ID_INPUT_ACCELEROMETER_ORIENTATION");
164 return ORIENTATION_UNDEFINED;
165 return string_to_orientation(value);
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; }
171 static void test_orientation(struct udev *udev,
172 struct udev_device *dev,
175 OrientationUp old, new;
176 _cleanup_close_ int fd = -1;
177 struct input_absinfo abs_info;
178 int x = 0, y = 0, z = 0;
182 old = get_prev_orientation(dev);
184 fd = open(devpath, O_RDONLY|O_CLOEXEC);
192 new = orientation_calc(old, x, y, z);
193 snprintf(text, sizeof(text),
194 "ID_INPUT_ACCELEROMETER_ORIENTATION=%s", orientation_to_string(new));
198 static void help(void) {
200 printf("%s [options] <device path>\n\n"
201 "Accelerometer device identification.\n\n"
202 " -h --help Print this message\n"
203 " -d --debug Debug to stderr\n"
204 , program_invocation_short_name);
207 int main (int argc, char** argv)
210 struct udev_device *dev;
212 static const struct option options[] = {
213 { "debug", no_argument, NULL, 'd' },
214 { "help", no_argument, NULL, 'h' },
218 char devpath[PATH_MAX];
220 struct udev_enumerate *enumerate;
221 struct udev_list_entry *list_entry;
223 log_parse_environment();
230 /* CLI argument parsing */
234 option = getopt_long(argc, argv, "dh", options, NULL);
240 log_set_target(LOG_TARGET_CONSOLE);
241 log_set_max_level(LOG_DEBUG);
252 if (argv[optind] == NULL) {
258 snprintf(devpath, sizeof(devpath), "/sys/%s", argv[optind]);
259 dev = udev_device_new_from_syspath(udev, devpath);
261 fprintf(stderr, "unable to access '%s'\n", devpath);
265 /* Get the children devices and find the devnode */
267 enumerate = udev_enumerate_new(udev);
268 udev_enumerate_add_match_parent(enumerate, dev);
269 udev_enumerate_scan_devices(enumerate);
270 udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(enumerate)) {
271 struct udev_device *device;
274 device = udev_device_new_from_syspath(udev_enumerate_get_udev(enumerate),
275 udev_list_entry_get_name(list_entry));
278 /* Already found it */
279 if (devnode != NULL) {
280 udev_device_unref(device);
284 node = udev_device_get_devnode(device);
286 udev_device_unref(device);
289 /* Use the event sub-device */
290 if (strstr(node, "/event") == NULL) {
291 udev_device_unref(device);
295 devnode = strdup(node);
296 udev_device_unref(device);
299 if (devnode == NULL) {
300 fprintf(stderr, "unable to get device node for '%s'\n", devpath);
304 log_debug("opening accelerometer device %s", devnode);
305 test_orientation(udev, dev, devnode);