From 62c858892193b6dc7ae7f0adefe043f094d74cb6 Mon Sep 17 00:00:00 2001 From: David Härdeman Date: Wed, 12 Aug 2015 21:05:32 +0200 Subject: Add some basic use of libevdev --- Makefile | 2 +- rcm-server-evdev.c | 32 +++++++++++++++++++++++--------- rcm-server-evdev.h | 2 +- rcm-server-main.c | 2 +- rcm-server-main.h | 1 + 5 files changed, 27 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index e077b28..cef1614 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,7 @@ EXTRA_LDFLAGS = COMMON_HEADERS = linux-input-keycodes.h linux-input.h shared.h -RCM_PACKAGES = libudev libsystemd +RCM_PACKAGES = libudev libsystemd libevdev RCM_CFLAGS = ${GENERIC_CFLAGS} ${EXTRA_CFLAGS} $(shell pkg-config --cflags ${RCM_PACKAGES}) RCM_LDFLAGS = ${GENERIC_LDFLAGS} ${EXTRA_LDFLAGS} $(shell pkg-config --libs ${RCM_PACKAGES}) RCM_COMPILE = $(CC) $(RCM_CFLAGS) diff --git a/rcm-server-evdev.c b/rcm-server-evdev.c index 74cae10..afd89f7 100644 --- a/rcm-server-evdev.c +++ b/rcm-server-evdev.c @@ -9,6 +9,7 @@ #include #include #include +#include #include "utils.h" #include "shared.h" @@ -150,6 +151,7 @@ evdev_read(sd_event_source *s, int fd, uint32_t revents, void *userdata) static uint32_t scancode; static bool scancode_recv = false; unsigned i; + int r; if (fd != device->evdev_fd) fprintf(stderr, "evdev fd mismatch: %i != %i\n", device->evdev_fd, fd); @@ -166,7 +168,16 @@ evdev_read(sd_event_source *s, int fd, uint32_t revents, void *userdata) return 0; } - while (read(fd, &ev, sizeof(ev)) == sizeof(ev)) { + do { + r = libevdev_next_event(device->evdev_dev, LIBEVDEV_READ_FLAG_NORMAL, &ev); + if (r != LIBEVDEV_READ_STATUS_SUCCESS) + continue; + + printf("Event: %s %s %d\n", + libevdev_event_type_get_name(ev.type), + libevdev_event_code_get_name(ev.type, ev.code), + ev.value); + switch (ev.type) { case EV_KEY: if (keycode) @@ -242,13 +253,14 @@ evdev_read(sd_event_source *s, int fd, uint32_t revents, void *userdata) default: break; } - } + + } while (r == LIBEVDEV_READ_STATUS_SUCCESS || r == LIBEVDEV_READ_STATUS_SYNC); return 0; } int -evdev_setup(struct manager *mgr, struct device *device, const char *path) +evdev_setup(struct device *device, const char *path) { int r; struct keymap *keymap; @@ -262,13 +274,15 @@ evdev_setup(struct manager *mgr, struct device *device, const char *path) } device->evdev_fd = open(path, O_RDONLY | O_NONBLOCK); - if (device->evdev_fd < 0) { - printf("Failed to open evdev device %s: %s\n", - path, strerror(errno)); - return -errno; + r = libevdev_new_from_fd(device->evdev_fd, &device->evdev_dev); + if (r < 0) { + printf("Failed to open evdev device %s: %s\n", path, strerror(-r)); + close(device->evdev_fd); + return r; } - printf("Performing evdev setup for device %s\n", device->path); + printf("Performing evdev setup for device %s (%s)\n", + device->path, libevdev_get_name(device->evdev_dev)); r = evdev_clear_keymap(device); if (r < 0) return r; @@ -276,7 +290,7 @@ evdev_setup(struct manager *mgr, struct device *device, const char *path) list_for_each_entry(keymap, &device->keymaps, list) r = evdev_set_keymap(device, keymap); - if (sd_event_add_io(mgr->event, &device->evdev_ev, + if (sd_event_add_io(device->mgr->event, &device->evdev_ev, device->evdev_fd, EPOLLIN, evdev_read, device) < 0) { printf("Failed to add event source for evdev device %s: %s\n", path, strerror(errno)); diff --git a/rcm-server-evdev.h b/rcm-server-evdev.h index ed0942a..2a38852 100644 --- a/rcm-server-evdev.h +++ b/rcm-server-evdev.h @@ -1,7 +1,7 @@ #ifndef foorcmserverevdevhfoo #define foorcmserverevdevhfoo -int evdev_setup(struct manager *mgr, struct device *device, const char *path); +int evdev_setup(struct device *device, const char *path); #endif diff --git a/rcm-server-main.c b/rcm-server-main.c index 7fc6e85..bd48c80 100644 --- a/rcm-server-main.c +++ b/rcm-server-main.c @@ -742,7 +742,7 @@ add_device(struct manager *mgr, struct udev_device *udev) continue; } - r = evdev_setup(mgr, device, devnode); + r = evdev_setup(device, devnode); if (r < 0) { printf("Failed to setup evdev: %s\n", devnode); udev_device_unref(evdev); diff --git a/rcm-server-main.h b/rcm-server-main.h index 4455ad8..5124d84 100644 --- a/rcm-server-main.h +++ b/rcm-server-main.h @@ -8,6 +8,7 @@ struct device { char *keymap_name; int evdev_fd; sd_event_source *evdev_ev; + struct libevdev *evdev_dev; struct manager *mgr; struct list_head list; struct list_head keymaps; -- cgit v1.2.3