accelerometer.c revision 090be8653471e1abe3f1cdd32eaad0fbd65f85cd
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen/*
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * accelerometer - exports device orientation through property
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen *
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 *
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * Possible values are:
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * undefined
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * * normal
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * * bottom-up
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * * left-up
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * * right-up
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen *
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 * orientation).
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen *
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * Copyright (C) 2011 Red Hat, Inc.
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * Author:
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * Bastien Nocera <hadess@hadess.net>
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen *
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * orientation_calc() from the sensorfw package
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * Copyright (C) 2009-2010 Nokia Corporation
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * Authors:
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 *
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 *
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 *
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 */
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen#include <stdio.h>
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen#include <string.h>
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen#include <math.h>
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen#include <sys/types.h>
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen#include <sys/stat.h>
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen#include <fcntl.h>
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen#include <stdlib.h>
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen#include <unistd.h>
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen#include <getopt.h>
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen#include <limits.h>
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen#include <linux/limits.h>
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen#include <linux/input.h>
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen#include "libudev.h"
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen#include "libudev-private.h"
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
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 BIT(x) (1UL<<OFF(x))
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 Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic int debug = 0;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
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{
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (debug) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen fprintf(stderr, "%s: ", fn);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen vfprintf(stderr, format, args);
260ad50f5b4a9795032e3119c64f838a2d03370dThomas Hindoe Paaboel Andersen } else {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen vsyslog(priority, format, args);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen}
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersentypedef enum {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen ORIENTATION_UNDEFINED,
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen ORIENTATION_NORMAL,
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen ORIENTATION_BOTTOM_UP,
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen ORIENTATION_LEFT_UP,
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen ORIENTATION_RIGHT_UP
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen} OrientationUp;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic const char *orientations[] = {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen "undefined",
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen "normal",
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen "bottom-up",
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen "left-up",
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen "right-up",
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen NULL
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen};
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen#define ORIENTATION_UP_UP ORIENTATION_NORMAL
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen#define DEFAULT_THRESHOLD 250
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen#define RADIANS_TO_DEGREES 180.0/M_PI
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen#define SAME_AXIS_LIMIT 5
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen#define THRESHOLD_LANDSCAPE 25
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen#define THRESHOLD_PORTRAIT 20
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic const char *
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenorientation_to_string (OrientationUp o)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen{
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return orientations[o];
b7e7184634d573fb73143210962acce205f37f61Michael Biebl}
b7e7184634d573fb73143210962acce205f37f61Michael Biebl
b7e7184634d573fb73143210962acce205f37f61Michael Bieblstatic OrientationUp
b7e7184634d573fb73143210962acce205f37f61Michael Bieblstring_to_orientation (const char *orientation)
b7e7184634d573fb73143210962acce205f37f61Michael Biebl{
b7e7184634d573fb73143210962acce205f37f61Michael Biebl int i;
b7e7184634d573fb73143210962acce205f37f61Michael Biebl
29e0e6d8c1f7f648b7c998880d034eaa3e58c53aMartin Pitt if (orientation == NULL)
29e0e6d8c1f7f648b7c998880d034eaa3e58c53aMartin Pitt return ORIENTATION_UNDEFINED;
29e0e6d8c1f7f648b7c998880d034eaa3e58c53aMartin Pitt for (i = 0; orientations[i] != NULL; i++) {
29e0e6d8c1f7f648b7c998880d034eaa3e58c53aMartin Pitt if (streq (orientation, orientations[i]))
29e0e6d8c1f7f648b7c998880d034eaa3e58c53aMartin Pitt return i;
b7e7184634d573fb73143210962acce205f37f61Michael Biebl }
b7e7184634d573fb73143210962acce205f37f61Michael Biebl return ORIENTATION_UNDEFINED;
b7e7184634d573fb73143210962acce205f37f61Michael Biebl}
b7e7184634d573fb73143210962acce205f37f61Michael Biebl
b7e7184634d573fb73143210962acce205f37f61Michael Bieblstatic OrientationUp
b7e7184634d573fb73143210962acce205f37f61Michael Bieblorientation_calc (OrientationUp prev,
b7e7184634d573fb73143210962acce205f37f61Michael Biebl int x, int y, int z)
b7e7184634d573fb73143210962acce205f37f61Michael Biebl{
b7e7184634d573fb73143210962acce205f37f61Michael Biebl int rotation;
b7e7184634d573fb73143210962acce205f37f61Michael Biebl OrientationUp ret = prev;
b7e7184634d573fb73143210962acce205f37f61Michael Biebl
b7e7184634d573fb73143210962acce205f37f61Michael Biebl /* Portrait check */
b7e7184634d573fb73143210962acce205f37f61Michael Biebl rotation = round(atan((double) x / sqrt(y * y + z * z)) * RADIANS_TO_DEGREES);
b7e7184634d573fb73143210962acce205f37f61Michael Biebl
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (abs(rotation) > THRESHOLD_PORTRAIT) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen ret = (rotation < 0) ? ORIENTATION_LEFT_UP : ORIENTATION_RIGHT_UP;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
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) {
260ad50f5b4a9795032e3119c64f838a2d03370dThomas Hindoe Paaboel Andersen ret = prev;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen }
77354c7e6f096a447245a8781c1eaa4acbe67089Martin Pitt
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen } else {
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
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
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) {
260ad50f5b4a9795032e3119c64f838a2d03370dThomas Hindoe Paaboel Andersen ret = prev;
260ad50f5b4a9795032e3119c64f838a2d03370dThomas Hindoe Paaboel Andersen }
260ad50f5b4a9795032e3119c64f838a2d03370dThomas Hindoe Paaboel Andersen }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return ret;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen}
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic OrientationUp
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenget_prev_orientation(struct udev_device *dev)
77354c7e6f096a447245a8781c1eaa4acbe67089Martin Pitt{
77354c7e6f096a447245a8781c1eaa4acbe67089Martin Pitt const char *value;
77354c7e6f096a447245a8781c1eaa4acbe67089Martin Pitt
77354c7e6f096a447245a8781c1eaa4acbe67089Martin Pitt value = udev_device_get_property_value(dev, "ID_INPUT_ACCELEROMETER_ORIENTATION");
77354c7e6f096a447245a8781c1eaa4acbe67089Martin Pitt if (value == NULL)
77354c7e6f096a447245a8781c1eaa4acbe67089Martin Pitt return ORIENTATION_UNDEFINED;
77354c7e6f096a447245a8781c1eaa4acbe67089Martin Pitt return string_to_orientation(value);
77354c7e6f096a447245a8781c1eaa4acbe67089Martin Pitt}
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt#define SET_AXIS(axis, code_) if (ev[i].code == code_) { if (got_##axis == 0) { axis = ev[i].value; got_##axis = 1; } }
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen/* accelerometers */
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic void test_orientation(struct udev *udev,
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen struct udev_device *dev,
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen const char *devpath)
aad0a2c80097926757d4385e5f5492082d47f006Zbigniew Jędrzejewski-Szmek{
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen OrientationUp old, new;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen int fd, r;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen struct input_event ev[64];
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen int got_syn = 0;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen int got_x, got_y, got_z;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen int x = 0, y = 0, z = 0;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen char text[64];
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
260ad50f5b4a9795032e3119c64f838a2d03370dThomas Hindoe Paaboel Andersen old = get_prev_orientation(dev);
260ad50f5b4a9795032e3119c64f838a2d03370dThomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if ((fd = open(devpath, O_RDONLY)) < 0)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen got_x = got_y = got_z = 0;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen while (1) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen int i;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = read(fd, ev, sizeof(struct input_event) * 64);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (r < (int) sizeof(struct input_event)) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen close(fd);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
d171ed1c50ba64928b7fb30ee2ae729fdfe0826bThomas Hindoe Paaboel Andersen for (i = 0; i < r / (int) sizeof(struct input_event); i++) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (got_syn == 1) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (ev[i].type == EV_ABS) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen SET_AXIS(x, ABS_X);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen SET_AXIS(y, ABS_Y);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen SET_AXIS(z, ABS_Z);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (ev[i].type == EV_SYN && ev[i].code == SYN_REPORT) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen got_syn = 1;
260ad50f5b4a9795032e3119c64f838a2d03370dThomas Hindoe Paaboel Andersen }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (got_x && got_y && got_z)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen goto read_dev;
31938a8560a664c32a9d72f1fc2d4347b232e6e9Michal Schmidt }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenread_dev:
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen close(fd);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (!got_x || !got_y || !got_z)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
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 puts(text);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen}
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic void help(void)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen{
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 Andersen}
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenint main (int argc, char** argv)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen{
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen struct udev *udev;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen struct udev_device *dev;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
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 {}
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen };
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen char devpath[PATH_MAX];
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen char *devnode;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen struct udev_enumerate *enumerate;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen struct udev_list_entry *list_entry;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen udev = udev_new();
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (udev == NULL)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return 1;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen log_open();
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen udev_set_log_fn(udev, log_fn);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen /* CLI argument parsing */
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen while (1) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen int option;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen option = getopt_long(argc, argv, "dxh", options, NULL);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (option == -1)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen break;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen switch (option) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen case 'd':
29e0e6d8c1f7f648b7c998880d034eaa3e58c53aMartin Pitt debug = 1;
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 break;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen case 'h':
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen help();
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen exit(0);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen default:
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen exit(1);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (argv[optind] == NULL) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen help();
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen exit(1);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
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 if (dev == NULL) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen fprintf(stderr, "unable to access '%s'\n", devpath);
29e0e6d8c1f7f648b7c998880d034eaa3e58c53aMartin Pitt return 1;
29e0e6d8c1f7f648b7c998880d034eaa3e58c53aMartin Pitt }
29e0e6d8c1f7f648b7c998880d034eaa3e58c53aMartin Pitt
29e0e6d8c1f7f648b7c998880d034eaa3e58c53aMartin Pitt /* Get the children devices and find the devnode */
29e0e6d8c1f7f648b7c998880d034eaa3e58c53aMartin Pitt devnode = NULL;
29e0e6d8c1f7f648b7c998880d034eaa3e58c53aMartin Pitt enumerate = udev_enumerate_new(udev);
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 struct udev_device *device;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen const char *node;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
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));
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (device == NULL)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen continue;
29e0e6d8c1f7f648b7c998880d034eaa3e58c53aMartin Pitt /* Already found it */
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (devnode != NULL) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen udev_device_unref(device);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen continue;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen node = udev_device_get_devnode(device);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (node == NULL) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen udev_device_unref(device);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen continue;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen /* Use the event sub-device */
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (strstr(node, "/event") == NULL) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen udev_device_unref(device);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen continue;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen devnode = strdup(node);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen udev_device_unref(device);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (devnode == NULL) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen fprintf(stderr, "unable to get device node for '%s'\n", devpath);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return 0;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen log_debug("opening accelerometer device %s\n", devnode);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen test_orientation(udev, dev, devnode);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen free(devnode);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen log_close();
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return 0;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen}
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen