X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fudev%2Faccelerometer%2Faccelerometer.c;h=9e2c590c15397e7d712fe065aded050bd31bdf37;hb=2c971c6fef4fdba5ea2cba5891a4a8f4736b87e4;hp=bc9715b26401fbb926a8a92fc34223750ecc9879;hpb=3e2147858f21943d5f4a781c60f33ac22c6096ed;p=elogind.git diff --git a/src/udev/accelerometer/accelerometer.c b/src/udev/accelerometer/accelerometer.c index bc9715b26..9e2c590c1 100644 --- a/src/udev/accelerometer/accelerometer.c +++ b/src/udev/accelerometer/accelerometer.c @@ -29,32 +29,27 @@ * Timo Rongas * Lihan Guo * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License - * along with keymap; if not, write to the Free Software Foundation, - * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include #include #include -#include -#include -#include #include -#include #include #include -#include #include #include "libudev.h" @@ -68,20 +63,6 @@ #define LONG(x) ((x)/BITS_PER_LONG) #define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1) -static int debug = 0; - -static void log_fn(struct udev *udev, int priority, - const char *file, int line, const char *fn, - const char *format, va_list args) -{ - if (debug) { - fprintf(stderr, "%s: ", fn); - vfprintf(stderr, format, args); - } else { - vsyslog(priority, format, args); - } -} - typedef enum { ORIENTATION_UNDEFINED, ORIENTATION_NORMAL, @@ -122,7 +103,7 @@ string_to_orientation (const char *orientation) if (orientation == NULL) return ORIENTATION_UNDEFINED; for (i = 0; orientations[i] != NULL; i++) { - if (strcmp (orientation, orientations[i]) == 0) + if (streq (orientation, orientations[i])) return i; } return ORIENTATION_UNDEFINED; @@ -178,7 +159,7 @@ get_prev_orientation(struct udev_device *dev) return string_to_orientation(value); } -#define SET_AXIS(axis, code_) if (ev[i].code == code_) { if (got_##axis == 0) { axis = ev[i].value; got_##axis = 1; } } +#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; } /* accelerometers */ static void test_orientation(struct udev *udev, @@ -186,60 +167,35 @@ static void test_orientation(struct udev *udev, const char *devpath) { OrientationUp old, new; - int fd, r; - struct input_event ev[64]; - int got_syn = 0; - int got_x, got_y, got_z; + _cleanup_close_ int fd = -1; + struct input_absinfo abs_info; int x = 0, y = 0, z = 0; + int r; char text[64]; old = get_prev_orientation(dev); - if ((fd = open(devpath, O_RDONLY)) < 0) + fd = open(devpath, O_RDONLY|O_CLOEXEC); + if (fd < 0) return; - got_x = got_y = got_z = 0; - - while (1) { - int i; - - r = read(fd, ev, sizeof(struct input_event) * 64); - - if (r < (int) sizeof(struct input_event)) - return; - - for (i = 0; i < r / (int) sizeof(struct input_event); i++) { - if (got_syn == 1) { - if (ev[i].type == EV_ABS) { - SET_AXIS(x, ABS_X); - SET_AXIS(y, ABS_Y); - SET_AXIS(z, ABS_Z); - } - } - if (ev[i].type == EV_SYN && ev[i].code == SYN_REPORT) { - got_syn = 1; - } - if (got_x && got_y && got_z) - goto read_dev; - } - } - -read_dev: - close(fd); - - if (!got_x || !got_y || !got_z) - return; + READ_AXIS(ABS_X, x); + READ_AXIS(ABS_Y, y); + READ_AXIS(ABS_Z, z); new = orientation_calc(old, x, y, z); - snprintf(text, sizeof(text), "ID_INPUT_ACCELEROMETER_ORIENTATION=%s", orientation_to_string(new)); + snprintf(text, sizeof(text), + "ID_INPUT_ACCELEROMETER_ORIENTATION=%s", orientation_to_string(new)); puts(text); } -static void help(void) -{ - printf("Usage: accelerometer [options] \n" - " --debug debug to stderr\n" - " --help print this help text\n\n"); +static void help(void) { + + printf("%s [options] \n\n" + "Accelerometer device identification.\n\n" + " -h --help Print this message\n" + " -d --debug Debug to stderr\n" + , program_invocation_short_name); } int main (int argc, char** argv) @@ -255,30 +211,29 @@ int main (int argc, char** argv) char devpath[PATH_MAX]; char *devnode; - const char *id_path; struct udev_enumerate *enumerate; struct udev_list_entry *list_entry; + log_parse_environment(); + log_open(); + udev = udev_new(); if (udev == NULL) return 1; - udev_log_init("input_id"); - udev_set_log_fn(udev, log_fn); - /* CLI argument parsing */ while (1) { int option; - option = getopt_long(argc, argv, "dxh", options, NULL); + option = getopt_long(argc, argv, "dh", options, NULL); if (option == -1) break; switch (option) { case 'd': - debug = 1; - if (udev_get_log_priority(udev) < LOG_INFO) - udev_set_log_priority(udev, LOG_INFO); + log_set_target(LOG_TARGET_CONSOLE); + log_set_max_level(LOG_DEBUG); + log_open(); break; case 'h': help(); @@ -294,26 +249,17 @@ int main (int argc, char** argv) } /* get the device */ - snprintf(devpath, sizeof(devpath), "%s/%s", udev_get_sys_path(udev), argv[optind]); + snprintf(devpath, sizeof(devpath), "/sys/%s", argv[optind]); dev = udev_device_new_from_syspath(udev, devpath); if (dev == NULL) { fprintf(stderr, "unable to access '%s'\n", devpath); return 1; } - id_path = udev_device_get_property_value(dev, "ID_PATH"); - if (id_path == NULL) { - fprintf (stderr, "unable to get property ID_PATH for '%s'", devpath); - return 0; - } - - /* Get the children devices and find the devnode - * FIXME: use udev_enumerate_add_match_children() instead - * when it's available */ + /* Get the children devices and find the devnode */ devnode = NULL; enumerate = udev_enumerate_new(udev); - udev_enumerate_add_match_property(enumerate, "ID_PATH", id_path); - udev_enumerate_add_match_subsystem(enumerate, "input"); + udev_enumerate_add_match_parent(enumerate, dev); udev_enumerate_scan_devices(enumerate); udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(enumerate)) { struct udev_device *device; @@ -349,9 +295,9 @@ int main (int argc, char** argv) return 0; } - info(udev, "Opening accelerometer device %s\n", devnode); + log_debug("opening accelerometer device %s", devnode); test_orientation(udev, dev, devnode); free(devnode); - + log_close(); return 0; }