chiark / gitweb /
log: log_error() and friends add a newline after each line anyway, so avoid including...
[elogind.git] / src / udev / accelerometer / accelerometer.c
index b8ed7b8edd856ad407d0aba6e707abb6c756cc9d..babd66f6b5bf4b41193afb28bb3c3c41d60bc32b 100644 (file)
  *   Timo Rongas <ext-timo.2.rongas@nokia.com>
  *   Lihan Guo <lihan.guo@digia.com>
  *
- * 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 <stdio.h>
 #include <string.h>
+#include <stdbool.h>
 #include <math.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -70,6 +71,7 @@
 
 static int debug = 0;
 
+_printf_(6,0)
 static void log_fn(struct udev *udev, int priority,
                    const char *file, int line, const char *fn,
                    const char *format, va_list args)
@@ -122,7 +124,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 +180,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 SET_AXIS(axis, code_) if (ev[i].code == code_) { if (got_##axis == 0) { axis = ev[i].value; got_##axis = true; } }
 
 /* accelerometers */
 static void test_orientation(struct udev *udev,
@@ -186,22 +188,21 @@ static void test_orientation(struct udev *udev,
                              const char *devpath)
 {
         OrientationUp old, new;
-        int fd, r;
+        _cleanup_close_ int fd = -1;
         struct input_event ev[64];
-        int got_syn = 0;
-        int got_x, got_y, got_z;
+        bool got_syn = false;
+        bool got_x = false, got_y = false, got_z = false;
         int x = 0, y = 0, z = 0;
         char text[64];
 
         old = get_prev_orientation(dev);
 
-        if ((fd = open(devpath, O_RDONLY)) < 0)
+        fd = open(devpath, O_RDONLY);
+        if (fd < 0)
                 return;
 
-        got_x = got_y = got_z = 0;
-
         while (1) {
-                int i;
+                int i, r;
 
                 r = read(fd, ev, sizeof(struct input_event) * 64);
 
@@ -209,29 +210,24 @@ static void test_orientation(struct udev *udev,
                         return;
 
                 for (i = 0; i < r / (int) sizeof(struct input_event); i++) {
-                        if (got_syn == 1) {
+                        if (got_syn) {
                                 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 (ev[i].type == EV_SYN && ev[i].code == SYN_REPORT)
+                                got_syn = true;
                         if (got_x && got_y && got_z)
                                 goto read_dev;
                 }
         }
 
 read_dev:
-        close(fd);
-
-        if (!got_x || !got_y || !got_z)
-                return;
-
         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);
 }
 
@@ -255,7 +251,6 @@ 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;
 
@@ -301,18 +296,10 @@ int main (int argc, char** argv)
                 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_parent() instead */
         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;
@@ -348,7 +335,7 @@ int main (int argc, char** argv)
                 return 0;
         }
 
-        log_debug("opening accelerometer device %s\n", devnode);
+        log_debug("opening accelerometer device %s", devnode);
         test_orientation(udev, dev, devnode);
         free(devnode);
         log_close();