#include <stdio.h>
#include <string.h>
+#include <stdbool.h>
#include <math.h>
#include <sys/types.h>
#include <sys/stat.h>
#define LONG(x) ((x)/BITS_PER_LONG)
#define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1)
-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)
{
- if (debug) {
- fprintf(stderr, "%s: ", fn);
- vfprintf(stderr, format, args);
- } else {
- vsyslog(priority, format, args);
- }
+ log_metav(priority, file, line, fn, format, args);
}
typedef enum {
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;
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,
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)) {
- close(fd);
- 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);
}
struct udev_enumerate *enumerate;
struct udev_list_entry *list_entry;
+ log_parse_environment();
+ log_open();
+
udev = udev_new();
if (udev == NULL)
return 1;
- log_open();
udev_set_log_fn(udev, log_fn);
/* CLI argument parsing */
switch (option) {
case 'd':
- debug = 1;
+ log_set_target(LOG_TARGET_CONSOLE);
log_set_max_level(LOG_DEBUG);
udev_set_log_priority(udev, LOG_DEBUG);
+ log_open();
break;
case 'h':
help();
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();