accelerometer.c revision 090be8653471e1abe3f1cdd32eaad0fbd65f85cd
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * accelerometer - exports device orientation through property
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * When an "change" event is received on an accelerometer,
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * open its device node, and from the value, as well as the previous
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * value of the property, calculate the device's new orientation,
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * and export it as ID_INPUT_ACCELEROMETER_ORIENTATION.
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * Possible values are:
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * The property will be persistent across sessions, and the new
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * orientations can be deducted from the previous one (it allows
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * for a threshold for switching between opposite ends of the
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * Copyright (C) 2011 Red Hat, Inc.
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * Bastien Nocera <hadess@hadess.net>
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * orientation_calc() from the sensorfw package
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * Copyright (C) 2009-2010 Nokia Corporation
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * Üstün Ergenoglu <ext-ustun.ergenoglu@nokia.com>
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * Timo Rongas <ext-timo.2.rongas@nokia.com>
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * Lihan Guo <lihan.guo@digia.com>
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * This program is free software; you can redistribute it and/or modify
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * it under the terms of the GNU General Public License as published by
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * the Free Software Foundation; either version 2 of the License, or
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * (at your option) any later version.
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * This program is distributed in the hope that it will be useful,
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * but WITHOUT ANY WARRANTY; without even the implied warranty of
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * GNU General Public License for more details.
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * You should have received a copy of the GNU General Public License along
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * with this program; if not, write to the Free Software Foundation, Inc.,
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen/* we must use this kernel-compatible implementation */
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen#define BITS_PER_LONG (sizeof(unsigned long) * 8)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen#define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen#define OFF(x) ((x)%BITS_PER_LONG)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen#define LONG(x) ((x)/BITS_PER_LONG)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen#define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic void log_fn(struct udev *udev, int priority,
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen const char *file, int line, const char *fn,
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen const char *format, va_list args)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen vfprintf(stderr, format, args);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen vsyslog(priority, format, args);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic const char *orientations[] = {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen#define ORIENTATION_UP_UP ORIENTATION_NORMAL
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen#define RADIANS_TO_DEGREES 180.0/M_PI
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen#define THRESHOLD_LANDSCAPE 25
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic const char *
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenorientation_to_string (OrientationUp o)
b7e7184634d573fb73143210962acce205f37f61Michael Bieblstring_to_orientation (const char *orientation)
b7e7184634d573fb73143210962acce205f37f61Michael Biebl int x, int y, int z)
b7e7184634d573fb73143210962acce205f37f61Michael Biebl /* Portrait check */
b7e7184634d573fb73143210962acce205f37f61Michael Biebl rotation = round(atan((double) x / sqrt(y * y + z * z)) * RADIANS_TO_DEGREES);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (abs(rotation) > THRESHOLD_PORTRAIT) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen ret = (rotation < 0) ? ORIENTATION_LEFT_UP : ORIENTATION_RIGHT_UP;
91e7bad45dced1cb2dfaac79337bb08d6e2b74a9Andreas Henriksson /* Some threshold to switching between portrait modes */
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (prev == ORIENTATION_LEFT_UP || prev == ORIENTATION_RIGHT_UP) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (abs(rotation) < SAME_AXIS_LIMIT) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen /* Landscape check */
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen rotation = round(atan((double) y / sqrt(x * x + z * z)) * RADIANS_TO_DEGREES);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (abs(rotation) > THRESHOLD_LANDSCAPE) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen ret = (rotation < 0) ? ORIENTATION_BOTTOM_UP : ORIENTATION_NORMAL;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen /* Some threshold to switching between landscape modes */
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (prev == ORIENTATION_BOTTOM_UP || prev == ORIENTATION_NORMAL) {
260ad50f5b4a9795032e3119c64f838a2d03370dThomas Hindoe Paaboel Andersen if (abs(rotation) < SAME_AXIS_LIMIT) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenget_prev_orientation(struct udev_device *dev)
77354c7e6f096a447245a8781c1eaa4acbe67089Martin Pitt const char *value;
77354c7e6f096a447245a8781c1eaa4acbe67089Martin Pitt value = udev_device_get_property_value(dev, "ID_INPUT_ACCELEROMETER_ORIENTATION");
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt#define SET_AXIS(axis, code_) if (ev[i].code == code_) { if (got_##axis == 0) { axis = ev[i].value; got_##axis = 1; } }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen/* accelerometers */
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic void test_orientation(struct udev *udev,
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen int x = 0, y = 0, z = 0;
260ad50f5b4a9795032e3119c64f838a2d03370dThomas Hindoe Paaboel Andersen old = get_prev_orientation(dev);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if ((fd = open(devpath, O_RDONLY)) < 0)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = read(fd, ev, sizeof(struct input_event) * 64);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (r < (int) sizeof(struct input_event)) {
d171ed1c50ba64928b7fb30ee2ae729fdfe0826bThomas Hindoe Paaboel Andersen for (i = 0; i < r / (int) sizeof(struct input_event); i++) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (ev[i].type == EV_SYN && ev[i].code == SYN_REPORT) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (!got_x || !got_y || !got_z)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen new = orientation_calc(old, x, y, z);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen snprintf(text, sizeof(text), "ID_INPUT_ACCELEROMETER_ORIENTATION=%s", orientation_to_string(new));
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen printf("Usage: accelerometer [options] <device path>\n"
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen " --debug debug to stderr\n"
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen " --help print this help text\n\n");
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenint main (int argc, char** argv)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen static const struct option options[] = {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen { "debug", no_argument, NULL, 'd' },
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen { "help", no_argument, NULL, 'h' },
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen struct udev_enumerate *enumerate;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen struct udev_list_entry *list_entry;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen udev_set_log_fn(udev, log_fn);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen /* CLI argument parsing */
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen option = getopt_long(argc, argv, "dxh", options, NULL);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen log_set_max_level(LOG_DEBUG);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen udev_set_log_priority(udev, LOG_DEBUG);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen /* get the device */
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen snprintf(devpath, sizeof(devpath), "/sys/%s", argv[optind]);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen dev = udev_device_new_from_syspath(udev, devpath);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen fprintf(stderr, "unable to access '%s'\n", devpath);
29e0e6d8c1f7f648b7c998880d034eaa3e58c53aMartin Pitt /* Get the children devices and find the devnode */
29e0e6d8c1f7f648b7c998880d034eaa3e58c53aMartin Pitt udev_enumerate_add_match_parent(enumerate, dev);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen udev_enumerate_scan_devices(enumerate);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(enumerate)) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen device = udev_device_new_from_syspath(udev_enumerate_get_udev(enumerate),
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen udev_list_entry_get_name(list_entry));
29e0e6d8c1f7f648b7c998880d034eaa3e58c53aMartin Pitt /* Already found it */
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen node = udev_device_get_devnode(device);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen /* Use the event sub-device */
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (strstr(node, "/event") == NULL) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen fprintf(stderr, "unable to get device node for '%s'\n", devpath);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen log_debug("opening accelerometer device %s\n", devnode);