From f3a6b0c6854a59f4433217f279f76383687d0c3b Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Mon, 1 Jul 2013 15:46:23 +1000 Subject: [PATCH] Add a flag for blocking read Not all clients need nonblocking read, so add a flag to read in blocking mode. In that mode, events are only read from the fd when the queue is empty. Signed-off-by: Peter Hutterer --- libevdev/libevdev.c | 19 +++++++++++-------- libevdev/libevdev.h | 1 + tools/libevdev-events.c | 4 ++-- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/libevdev/libevdev.c b/libevdev/libevdev.c index 590e2d6..b625934 100644 --- a/libevdev/libevdev.c +++ b/libevdev/libevdev.c @@ -556,18 +556,21 @@ int libevdev_next_event(struct libevdev *dev, unsigned int flags, struct input_e update_state(dev, &e); } - /* FIXME: check for O_NONBLOCK and if not set, skip if we have an - * event in the queue from the previous read. - */ - /* FIXME: if the first event after syncing is a SYN_DROPPED, log this */ /* Always read in some more events. Best case this smoothes over a potential SYN_DROPPED, - worst case we don't read fast enough and end up with SYN_DROPPED anyway */ + worst case we don't read fast enough and end up with SYN_DROPPED anyway. + + Except if the fd is in blocking mode and we still have events from the last read, don't + read in any more. + */ do { - rc = read_more_events(dev); - if (rc < 0 && rc != -EAGAIN) - goto out; + if (!(flags & LIBEVDEV_READ_BLOCKING) || + queue_num_elements(dev) == 0) { + rc = read_more_events(dev); + if (rc < 0 && rc != -EAGAIN) + goto out; + } if (flags & LIBEVDEV_FORCE_SYNC) { dev->need_sync = 1; diff --git a/libevdev/libevdev.h b/libevdev/libevdev.h index 7e4c247..db8fd88 100644 --- a/libevdev/libevdev.h +++ b/libevdev/libevdev.h @@ -192,6 +192,7 @@ enum EvdevReadFlags { LIBEVDEV_FORCE_SYNC = 4, /**< Pretend the next event is a SYN_DROPPED. There is no reason to ever use this except for automated tests, so don't. */ + LIBEVDEV_READ_BLOCKING = 8, /**< The fd is not in O_NONBLOCK and a read may block */ }; /** diff --git a/tools/libevdev-events.c b/tools/libevdev-events.c index e7728e7..ee64cea 100644 --- a/tools/libevdev-events.c +++ b/tools/libevdev-events.c @@ -145,7 +145,7 @@ main(int argc, char **argv) goto out; file = argv[1]; - fd = open(file, O_RDONLY | O_NONBLOCK); + fd = open(file, O_RDONLY); if (fd < 0) { perror("Failed to open device"); goto out; @@ -170,7 +170,7 @@ main(int argc, char **argv) do { struct input_event ev; - rc = libevdev_next_event(dev, LIBEVDEV_READ_NORMAL, &ev); + rc = libevdev_next_event(dev, LIBEVDEV_READ_NORMAL|LIBEVDEV_READ_BLOCKING, &ev); if (rc == 1) { printf("::::::::::::::::::::: dropped ::::::::::::::::::::::\n"); while (rc == 1) { -- 2.34.1