accelerometer.c revision 23757887f768b0b7339239cc98aee879d9f4d87f
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani/*
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * accelerometer - exports device orientation through property
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani *
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 *
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * Possible values are:
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * undefined
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * * normal
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * * bottom-up
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * * left-up
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * * right-up
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani *
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 *
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * Copyright (C) 2011 Red Hat, Inc.
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * Author:
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * Bastien Nocera <hadess@hadess.net>
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani *
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * orientation_calc() from the sensorfw package
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * Copyright (C) 2009-2010 Nokia Corporation
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * Authors:
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 *
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 *
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 *
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 */
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani#include <stdio.h>
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani#include <string.h>
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani#include <math.h>
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani#include <sys/types.h>
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani#include <sys/stat.h>
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani#include <fcntl.h>
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani#include <stdlib.h>
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani#include <unistd.h>
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani#include <getopt.h>
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani#include <limits.h>
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani#include <linux/limits.h>
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani#include <linux/input.h>
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani#include "libudev.h"
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani#include "libudev-private.h"
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
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 OFF(x) ((x)%BITS_PER_LONG)
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani#define BIT(x) (1UL<<OFF(x))
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani#define LONG(x) ((x)/BITS_PER_LONG)
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani#define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1)
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvanistatic int debug = 0;
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvanistatic void log_fn(struct udev *udev, int priority,
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani const char *file, int line, const char *fn,
176c355b43e616b61552566303ad59d5fd910333Beniamino Galvani const char *format, va_list args)
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani{
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (debug) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani fprintf(stderr, "%s: ", fn);
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani vfprintf(stderr, format, args);
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani } else {
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani vsyslog(priority, format, args);
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani }
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani}
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvanitypedef enum {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani ORIENTATION_UNDEFINED,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani ORIENTATION_NORMAL,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani ORIENTATION_BOTTOM_UP,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani ORIENTATION_LEFT_UP,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani ORIENTATION_RIGHT_UP
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani} OrientationUp;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahanistatic const char *orientations[] = {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani "undefined",
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani "normal",
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani "bottom-up",
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani "left-up",
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani "right-up",
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani NULL
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani};
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani#define ORIENTATION_UP_UP ORIENTATION_NORMAL
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani#define DEFAULT_THRESHOLD 250
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani#define RADIANS_TO_DEGREES 180.0/M_PI
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani#define SAME_AXIS_LIMIT 5
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani#define THRESHOLD_LANDSCAPE 25
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani#define THRESHOLD_PORTRAIT 20
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahanistatic const char *
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahaniorientation_to_string (OrientationUp o)
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani{
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani return orientations[o];
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani}
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahanistatic OrientationUp
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahanistring_to_orientation (const char *orientation)
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani{
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani int i;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (orientation == NULL)
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani return ORIENTATION_UNDEFINED;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani for (i = 0; orientations[i] != NULL; i++) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (strcmp (orientation, orientations[i]) == 0)
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani return i;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani }
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani return ORIENTATION_UNDEFINED;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani}
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahanistatic OrientationUp
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahaniorientation_calc (OrientationUp prev,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani int x, int y, int z)
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani{
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani int rotation;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani OrientationUp ret = prev;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani /* Portrait check */
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani rotation = round(atan((double) x / sqrt(y * y + z * z)) * RADIANS_TO_DEGREES);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (abs(rotation) > THRESHOLD_PORTRAIT) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani ret = (rotation < 0) ? ORIENTATION_LEFT_UP : ORIENTATION_RIGHT_UP;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani /* Some threshold to switching between portrait modes */
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (prev == ORIENTATION_LEFT_UP || prev == ORIENTATION_RIGHT_UP) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (abs(rotation) < SAME_AXIS_LIMIT) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani ret = prev;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani }
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani }
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani } else {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani /* Landscape check */
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani rotation = round(atan((double) y / sqrt(x * x + z * z)) * RADIANS_TO_DEGREES);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (abs(rotation) > THRESHOLD_LANDSCAPE) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani ret = (rotation < 0) ? ORIENTATION_BOTTOM_UP : ORIENTATION_NORMAL;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani /* Some threshold to switching between landscape modes */
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (prev == ORIENTATION_BOTTOM_UP || prev == ORIENTATION_NORMAL) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (abs(rotation) < SAME_AXIS_LIMIT) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani ret = prev;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani }
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani }
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani }
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani }
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani return ret;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani}
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahanistatic OrientationUp
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahaniget_prev_orientation(struct udev_device *dev)
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani{
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani const char *value;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani value = udev_device_get_property_value(dev, "ID_INPUT_ACCELEROMETER_ORIENTATION");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (value == NULL)
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani return ORIENTATION_UNDEFINED;
a7f7d1bde43fc825c49afea3f946f5b4b3d563e0Harald Hoyer return string_to_orientation(value);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani}
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
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
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani/* accelerometers */
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahanistatic void test_orientation(struct udev *udev,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani struct udev_device *dev,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani const char *devpath)
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani{
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani OrientationUp old, new;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani int fd, r;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani struct input_event ev[64];
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani int got_syn = 0;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani int got_x, got_y, got_z;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani int x = 0, y = 0, z = 0;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani char text[64];
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
a7f7d1bde43fc825c49afea3f946f5b4b3d563e0Harald Hoyer old = get_prev_orientation(dev);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if ((fd = open(devpath, O_RDONLY)) < 0)
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani return;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani got_x = got_y = got_z = 0;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani while (1) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani int i;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani r = read(fd, ev, sizeof(struct input_event) * 64);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (r < (int) sizeof(struct input_event)) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani close(fd);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani return;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani }
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani for (i = 0; i < r / (int) sizeof(struct input_event); i++) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (got_syn == 1) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (ev[i].type == EV_ABS) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani SET_AXIS(x, ABS_X);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani SET_AXIS(y, ABS_Y);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani SET_AXIS(z, ABS_Z);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani }
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani }
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (ev[i].type == EV_SYN && ev[i].code == SYN_REPORT) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani got_syn = 1;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani }
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (got_x && got_y && got_z)
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani goto read_dev;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani }
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani }
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahaniread_dev:
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani close(fd);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (!got_x || !got_y || !got_z)
a7f7d1bde43fc825c49afea3f946f5b4b3d563e0Harald Hoyer return;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani new = orientation_calc(old, x, y, z);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani snprintf(text, sizeof(text), "ID_INPUT_ACCELEROMETER_ORIENTATION=%s", orientation_to_string(new));
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani puts(text);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani}
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahanistatic void help(void)
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani{
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}
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahaniint main (int argc, char** argv)
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani{
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani struct udev *udev;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani struct udev_device *dev;
a7f7d1bde43fc825c49afea3f946f5b4b3d563e0Harald Hoyer
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani static const struct option options[] = {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani { "debug", no_argument, NULL, 'd' },
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani { "help", no_argument, NULL, 'h' },
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani {}
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani };
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani char devpath[PATH_MAX];
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani char *devnode;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani const char *id_path;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani struct udev_enumerate *enumerate;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani struct udev_list_entry *list_entry;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani udev = udev_new();
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (udev == NULL)
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani return 1;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani log_open();
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani udev_set_log_fn(udev, log_fn);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani /* CLI argument parsing */
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani while (1) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani int option;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani option = getopt_long(argc, argv, "dxh", options, NULL);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (option == -1)
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani break;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani switch (option) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani case 'd':
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani debug = 1;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani log_set_max_level(LOG_DEBUG);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani udev_set_log_priority(udev, LOG_DEBUG);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani break;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani case 'h':
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani help();
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani exit(0);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani default:
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani exit(1);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani }
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani }
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (argv[optind] == NULL) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani help();
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani exit(1);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani }
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
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 if (dev == NULL) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani fprintf(stderr, "unable to access '%s'\n", devpath);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani return 1;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani }
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani id_path = udev_device_get_property_value(dev, "ID_PATH");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (id_path == NULL) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani fprintf (stderr, "unable to get property ID_PATH for '%s'", devpath);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani return 0;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani }
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani /* Get the children devices and find the devnode */
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani /* FIXME: use udev_enumerate_add_match_parent() instead */
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani devnode = NULL;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani enumerate = udev_enumerate_new(udev);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani udev_enumerate_add_match_property(enumerate, "ID_PATH", id_path);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani udev_enumerate_add_match_subsystem(enumerate, "input");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani udev_enumerate_scan_devices(enumerate);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(enumerate)) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani struct udev_device *device;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani const char *node;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani device = udev_device_new_from_syspath(udev_enumerate_get_udev(enumerate),
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani udev_list_entry_get_name(list_entry));
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (device == NULL)
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani continue;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani /* Already found it */
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (devnode != NULL) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani udev_device_unref(device);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani continue;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani }
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani node = udev_device_get_devnode(device);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (node == NULL) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani udev_device_unref(device);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani continue;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani }
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani /* Use the event sub-device */
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (strstr(node, "/event") == NULL) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani udev_device_unref(device);
continue;
}
devnode = strdup(node);
udev_device_unref(device);
}
if (devnode == NULL) {
fprintf(stderr, "unable to get device node for '%s'\n", devpath);
return 0;
}
log_debug("opening accelerometer device %s\n", devnode);
test_orientation(udev, dev, devnode);
free(devnode);
log_close();
return 0;
}