accelerometer.c revision 23757887f768b0b7339239cc98aee879d9f4d87f
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * accelerometer - exports device orientation through property
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * When an "change" event is received on an accelerometer,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * open its device node, and from the value, as well as the previous
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * value of the property, calculate the device's new orientation,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * and export it as ID_INPUT_ACCELEROMETER_ORIENTATION.
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * Possible values are:
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * * bottom-up
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * The property will be persistent across sessions, and the new
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * orientations can be deducted from the previous one (it allows
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * for a threshold for switching between opposite ends of the
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * orientation).
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * Copyright (C) 2011 Red Hat, Inc.
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * Bastien Nocera <hadess@hadess.net>
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * orientation_calc() from the sensorfw package
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * Copyright (C) 2009-2010 Nokia Corporation
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * Üstün Ergenoglu <ext-ustun.ergenoglu@nokia.com>
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * Timo Rongas <ext-timo.2.rongas@nokia.com>
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * Lihan Guo <lihan.guo@digia.com>
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * This program is free software; you can redistribute it and/or modify
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * it under the terms of the GNU General Public License as published by
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * the Free Software Foundation; either version 2 of the License, or
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * (at your option) any later version.
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * This program is distributed in the hope that it will be useful,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * but WITHOUT ANY WARRANTY; without even the implied warranty of
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * GNU General Public License for more details.
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * You should have received a copy of the GNU General Public License along
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * with this program; if not, write to the Free Software Foundation, Inc.,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani/* we must use this kernel-compatible implementation */
176c355b43e616b61552566303ad59d5fd910333Beniamino Galvani#define BITS_PER_LONG (sizeof(unsigned long) * 8)
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani#define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani#define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1)
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvanistatic int debug = 0;
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvanistatic void log_fn(struct udev *udev, int priority,
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani const char *file, int line, const char *fn,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahanistatic const char *orientations[] = {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahanistatic const char *
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahanistring_to_orientation (const char *orientation)
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (strcmp (orientation, orientations[i]) == 0)
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani int x, int y, int z)
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani /* Portrait check */
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani rotation = round(atan((double) x / sqrt(y * y + z * z)) * RADIANS_TO_DEGREES);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani ret = (rotation < 0) ? ORIENTATION_LEFT_UP : ORIENTATION_RIGHT_UP;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani /* Some threshold to switching between portrait modes */
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (prev == ORIENTATION_LEFT_UP || prev == ORIENTATION_RIGHT_UP) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani /* Landscape check */
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani rotation = round(atan((double) y / sqrt(x * x + z * z)) * RADIANS_TO_DEGREES);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani ret = (rotation < 0) ? ORIENTATION_BOTTOM_UP : ORIENTATION_NORMAL;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani /* Some threshold to switching between landscape modes */
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (prev == ORIENTATION_BOTTOM_UP || prev == ORIENTATION_NORMAL) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani value = udev_device_get_property_value(dev, "ID_INPUT_ACCELEROMETER_ORIENTATION");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani#define SET_AXIS(axis, code_) if (ev[i].code == code_) { if (got_##axis == 0) { axis = ev[i].value; got_##axis = 1; } }
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani/* accelerometers */
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahanistatic void test_orientation(struct udev *udev,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani int x = 0, y = 0, z = 0;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani r = read(fd, ev, sizeof(struct input_event) * 64);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (r < (int) sizeof(struct input_event)) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani for (i = 0; i < r / (int) sizeof(struct input_event); i++) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (ev[i].type == EV_SYN && ev[i].code == SYN_REPORT) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani snprintf(text, sizeof(text), "ID_INPUT_ACCELEROMETER_ORIENTATION=%s", orientation_to_string(new));
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahanistatic void help(void)
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani printf("Usage: accelerometer [options] <device path>\n"
920b52e4909d9dc812817fd8b82f83ca23a11c91Thomas Hindoe Paaboel Andersen " --debug debug to stderr\n"
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani " --help print this help text\n\n");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani /* CLI argument parsing */
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani option = getopt_long(argc, argv, "dxh", options, NULL);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani /* get the device */
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani snprintf(devpath, sizeof(devpath), "/sys/%s", argv[optind]);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani dev = udev_device_new_from_syspath(udev, devpath);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani fprintf(stderr, "unable to access '%s'\n", devpath);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani id_path = udev_device_get_property_value(dev, "ID_PATH");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani fprintf (stderr, "unable to get property ID_PATH for '%s'", devpath);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani /* Get the children devices and find the devnode */
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani /* FIXME: use udev_enumerate_add_match_parent() instead */
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani udev_enumerate_add_match_property(enumerate, "ID_PATH", id_path);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani udev_enumerate_add_match_subsystem(enumerate, "input");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(enumerate)) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani device = udev_device_new_from_syspath(udev_enumerate_get_udev(enumerate),
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani /* Already found it */
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani /* Use the event sub-device */
log_close();