From 7d392652bae52b76cb5fe907cc1e86e0e0c4ced9 Mon Sep 17 00:00:00 2001 From: Henrik Rydberg Date: Thu, 17 Jun 2010 20:15:44 +0200 Subject: Add idle function This patch adds the mtdev_idle() function, which checks the kernel device for activity. Useful when implementing timing logic. Signed-off-by: Henrik Rydberg --- include/mtdev.h | 11 +++++++++++ src/iobuf.c | 12 ++++++++++++ 2 files changed, 23 insertions(+) diff --git a/include/mtdev.h b/include/mtdev.h index 5f2f6a3..e2fdc9d 100644 --- a/include/mtdev.h +++ b/include/mtdev.h @@ -145,6 +145,17 @@ int mtdev_configure(struct mtdev *dev, int fd); */ int mtdev_open(struct mtdev *dev, int fd); +/** + * mtdev_idle - check state of kernel device + * @dev: the mtdev in use + * @fd: file descriptor of the kernel device + * @ms: number of milliseconds to wait for activity + * + * Returns true if the device is idle, i.e., there are no buffered + * events and there is nothing to fetch from the kernel device. + */ +int mtdev_idle(struct mtdev *dev, int fd, int ms); + /** * mtdev_fetch - fetch an event from the kernel device * @dev: the mtdev in use diff --git a/src/iobuf.c b/src/iobuf.c index 6d238ec..83e531a 100644 --- a/src/iobuf.c +++ b/src/iobuf.c @@ -28,6 +28,18 @@ #include "iobuf.h" #include "state.h" +#include + +int mtdev_idle(struct mtdev *dev, int fd, int ms) +{ + struct mtdev_state *state = dev->state; + struct mtdev_iobuf *buf = &state->iobuf; + struct pollfd fds = { fd, POLLIN, 0 }; + return evbuf_empty(&state->outbuf) && + evbuf_empty(&state->inbuf) && + buf->head == buf->tail && + poll(&fds, 1, ms) <= 0; +} int mtdev_fetch(struct mtdev *dev, struct input_event *ev, int fd) { -- cgit v1.2.3