From 259b92a30280cdec2b7798df3c14da596c417ef1 Mon Sep 17 00:00:00 2001 From: Henrik Rydberg Date: Sat, 19 Jun 2010 12:10:27 +0200 Subject: Restructure mtdev api Split the api into plumbing and porcelain layers and move the plumbing part to its own optional header file. The main usecase is to fetch events from the device, route them through the converter and extract the processed events. To simplify the API, replace the intermediate mtdev_pull() function by the higher-level mtdev_get(). This function does all the required steps, and has the same semantics as read(). Signed-off-by: Henrik Rydberg --- include/mtdev-plumbing.h | 105 +++++++++++++++++++++++++++++++++++++++++++++++ include/mtdev.h | 99 +++++++------------------------------------- src/common.h | 1 + src/core.c | 12 +----- src/evbuf.h | 5 --- src/iobuf.c | 37 ++++++++++------- test/mtdev.c | 9 ++-- 7 files changed, 148 insertions(+), 120 deletions(-) create mode 100644 include/mtdev-plumbing.h diff --git a/include/mtdev-plumbing.h b/include/mtdev-plumbing.h new file mode 100644 index 0000000..b610873 --- /dev/null +++ b/include/mtdev-plumbing.h @@ -0,0 +1,105 @@ +/***************************************************************************** + * + * mtdev - MT device event converter (MIT license) + * + * Copyright (C) 2010 Henrik Rydberg + * Copyright (C) 2010 Canonical Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + ****************************************************************************/ + +#ifndef _MTDEV_PLUMBING_H +#define _MTDEV_PLUMBING_H + +#include + +/** + * mtdev_init - initialize mtdev converter + * @dev: the mtdev to initialize + * + * Sets up the internal data structures. + * + * Returns zero on success, negative error number otherwise. + */ +int mtdev_init(struct mtdev *dev); + +/** + * mtdev_configure - configure the mtdev converter + * @dev: the mtdev to configure + * @fd: file descriptor of the kernel device + * + * Reads the device properties to set up the protocol capabilities. + * If preferred, this can be done by hand, omitting this call. + * + * Returns zero on success, negative error number otherwise. + */ +int mtdev_configure(struct mtdev *dev, int fd); + +/** + * mtdev_fetch_event - fetch an event from the kernel device + * @dev: the mtdev in use + * @fd: file descriptor of the kernel device + * @ev: the kernel input event to fill + * + * Fetch a kernel event from the kernel device. The read operation + * behaves as dictated by the file descriptor; if O_NONBLOCK is not + * set, the read will block until an event is available. + * + * On success, returns the number of events read (0 or 1). Otherwise, + * a standard negative error number is returned. + */ +int mtdev_fetch_event(struct mtdev *dev, int fd, struct input_event *ev); + +/** + * mtdev_put_event - put an event into the converter + * @dev: the mtdev in use + * @ev: the kernel input event to put + * + * Put a kernel event into the mtdev converter. The event should + * come straight from the device. + * + * This call does not block; if the buffer becomes full, older events + * are dropped. The buffer is guaranteed to handle several complete MT + * packets. + */ +void mtdev_put_event(struct mtdev *dev, const struct input_event *ev); + +/** + * mtdev_empty - check if there are events to get + * @dev: the mtdev in use + * + * Returns true if the processed event queue is empty, false otherwise. + */ +int mtdev_empty(struct mtdev *dev); + +/** + * mtdev_get_event - get processed events from mtdev + * @dev: the mtdev in use + * @ev: the input event to fill + * + * Get a processed event from mtdev. The events appear as if they came + * from a type B device emitting MT slot events. + * + * The queue must be non-empty before calling this function. + */ +void mtdev_get_event(struct mtdev *dev, struct input_event* ev); + +#endif diff --git a/include/mtdev.h b/include/mtdev.h index 98e022a..b759803 100644 --- a/include/mtdev.h +++ b/include/mtdev.h @@ -108,28 +108,6 @@ struct mtdev { struct mtdev_state *state; }; -/** - * mtdev_init - initialize mtdev converter - * @dev: the mtdev to initialize - * - * Sets up the internal data structures. - * - * Returns zero on success, negative error number otherwise. - */ -int mtdev_init(struct mtdev *dev); - -/** - * mtdev_configure - configure the mtdev converter - * @dev: the mtdev to configure - * @fd: file descriptor of the kernel device - * - * Reads the device properties to set up the protocol capabilities. - * If preferred, this can be done by hand, omitting this call. - * - * Returns zero on success, negative error number otherwise. - */ -int mtdev_configure(struct mtdev *dev, int fd); - /** * mtdev_open - open an mtdev converter * @dev: the mtdev to open @@ -140,8 +118,8 @@ int mtdev_configure(struct mtdev *dev, int fd); * * Returns zero on success, negative error number otherwise. * - * This call combines mtdev_init() and mtdev_configure(), which - * may be used separately instead. + * This call combines the plumbing functions mtdev_init() and + * mtdev_configure(). */ int mtdev_open(struct mtdev *dev, int fd); @@ -157,73 +135,26 @@ int mtdev_open(struct mtdev *dev, int fd); int mtdev_idle(struct mtdev *dev, int fd, int ms); /** - * mtdev_fetch - fetch an event from the kernel device - * @dev: the mtdev in use - * @fd: file descriptor of the kernel device - * @ev: the kernel input event to fill - * - * Fetch a kernel event from the kernel device. The read operation - * behaves as dictated by the file descriptor; if O_NONBLOCK is not - * set, the read will block until an event is available. - * - * On success, returns the number of events read. Otherwise, a standard - * negative error number is returned. - */ -int mtdev_fetch(struct mtdev *dev, int fd, struct input_event *ev); - -/** - * mtdev_put - put an event into the converter - * @dev: the mtdev in use - * @ev: the kernel input event to put - * - * Put a kernel event into the mtdev converter. The event should - * come straight from the device. - * - * This call does not block; if the buffer becomes full, older events - * are dropped. The buffer is guaranteed to handle several complete MT - * packets. - */ -void mtdev_put(struct mtdev *dev, const struct input_event *ev); - -/** - * mtdev_pull - pull events from the kernel device + * mtdev_get - get processed events from mtdev * @dev: the mtdev in use * @fd: file descriptor of the kernel device - * @max_events: max number of events to read (zero for all) - * - * Read a maxmimum of max_events events from the device, and put them - * in the converter. If max_events is zero, all available events will - * be read. The read operation behaves as dictated by the file - * descriptor; if O_NONBLOCK is not set, the read will block until - * max_events events are available or the buffer is full. + * @ev: array of input events to fill + * @ev_max: maximum number of events to read * - * On success, returns the number of events read. Otherwise, a standard - * negative error number is returned. - * - * This call combines mtdev_fetch() with mtdev_put(), which - * may be used separately instead. - */ -int mtdev_pull(struct mtdev *dev, int fd, int max_events); - -/** - * mtdev_empty - check if there are events to get - * @dev: the mtdev in use + * Get a processed event from mtdev. The events appear as if they came + * from a type B device emitting MT slot events. * - * Returns true if the event queue is empty, false otherwise. - */ -int mtdev_empty(struct mtdev *dev); - -/** - * mtdev_get - get canonical events from mtdev - * @dev: the mtdev in use - * @ev: the input event to fill + * The read operations involved behave as dictated by the file + * descriptor; if O_NONBLOCK is not set, mtdev_get() will block until + * the specified number of processed events are available. * - * Get a canonical event from mtdev. The events appear as if they came - * from a type B device emitting MT slot events. + * On success, returns the number of events read. Otherwise, + * a standard negative error number is returned. * - * The queue must be non-empty before calling this function. + * This call combines the plumbing functions mtdev_fetch_event(), + * mtdev_put_event() and mtdev_get_event(). */ -void mtdev_get(struct mtdev *dev, struct input_event* ev); +int mtdev_get(struct mtdev *dev, int fd, struct input_event* ev, int ev_max); /** * mtdev_close - close the mtdev converter diff --git a/src/common.h b/src/common.h index 03bc7bd..57d47a0 100644 --- a/src/common.h +++ b/src/common.h @@ -30,6 +30,7 @@ #define COMMON_H #include +#include #include #include #include diff --git a/src/core.c b/src/core.c index 3df8257..8df2477 100644 --- a/src/core.c +++ b/src/core.c @@ -360,7 +360,7 @@ int mtdev_open(struct mtdev *dev, int fd) return ret; } -void mtdev_put(struct mtdev *dev, const struct input_event *ev) +void mtdev_put_event(struct mtdev *dev, const struct input_event *ev) { struct mtdev_state *state = dev->state; if (ev->type == EV_SYN && ev->code == SYN_REPORT) { @@ -376,16 +376,6 @@ void mtdev_put(struct mtdev *dev, const struct input_event *ev) } } -int mtdev_empty(struct mtdev *dev) -{ - return evbuf_empty(&dev->state->outbuf); -} - -void mtdev_get(struct mtdev *dev, struct input_event* ev) -{ - evbuf_get(&dev->state->outbuf, ev); -} - void mtdev_close(struct mtdev *dev) { free(dev->state); diff --git a/src/evbuf.h b/src/evbuf.h index ba61cd5..1afd9f5 100644 --- a/src/evbuf.h +++ b/src/evbuf.h @@ -42,11 +42,6 @@ static inline int evbuf_empty(const struct mtdev_evbuf *evbuf) return evbuf->head == evbuf->tail; } -static inline int evbuf_full(const struct mtdev_evbuf *evbuf) -{ - return ((evbuf->head + 1) & (DIM_EVENTS - 1)) == evbuf->tail; -} - static inline void evbuf_put(struct mtdev_evbuf *evbuf, const struct input_event *ev) { diff --git a/src/iobuf.c b/src/iobuf.c index 050f2ed..85a9f19 100644 --- a/src/iobuf.c +++ b/src/iobuf.c @@ -37,7 +37,7 @@ int mtdev_idle(struct mtdev *dev, int fd, int ms) return buf->head == buf->tail && poll(&fds, 1, ms) <= 0; } -int mtdev_fetch(struct mtdev *dev, int fd, struct input_event *ev) +int mtdev_fetch_event(struct mtdev *dev, int fd, struct input_event *ev) { struct mtdev_iobuf *buf = &dev->state->iobuf; int n = buf->head - buf->tail; @@ -59,21 +59,30 @@ int mtdev_fetch(struct mtdev *dev, int fd, struct input_event *ev) return 1; } -int mtdev_pull(struct mtdev *dev, int fd, int max_events) +int mtdev_empty(struct mtdev *dev) { - struct mtdev_state *state = dev->state; - struct input_event ev; + return evbuf_empty(&dev->state->outbuf); +} + +void mtdev_get_event(struct mtdev *dev, struct input_event* ev) +{ + evbuf_get(&dev->state->outbuf, ev); +} + +int mtdev_get(struct mtdev *dev, int fd, struct input_event* ev, int ev_max) +{ + struct input_event kev; int ret, count = 0; - if (max_events <= 0) - max_events = DIM_EVENTS; - while (max_events-- && !evbuf_full(&state->inbuf)) { - ret = mtdev_fetch(dev, fd, &ev); - if (ret < 0) - return ret; - if (ret == 0) - break; - mtdev_put(dev, &ev); - count++; + while (count < ev_max) { + while (mtdev_empty(dev)) { + ret = mtdev_fetch_event(dev, fd, &kev); + if (ret < 0) + return ret; + if (ret == 0) + return count; + mtdev_put_event(dev, &kev); + } + mtdev_get_event(dev, &ev[count++]); } return count; } diff --git a/test/mtdev.c b/test/mtdev.c index 60fae7b..a4858c5 100644 --- a/test/mtdev.c +++ b/test/mtdev.c @@ -26,7 +26,7 @@ * ****************************************************************************/ -#include +#include #include #include @@ -64,11 +64,8 @@ static void loop_device(int fd) } /* while the device has not been inactive for five seconds */ while (!mtdev_idle(&dev, fd, 5000)) { - /* fetch all available kernel events */ - mtdev_pull(&dev, fd, 0); - /* extract all available canonical events */ - while (!mtdev_empty(&dev)) { - mtdev_get(&dev, &ev); + /* extract all available processed events */ + while (mtdev_get(&dev, fd, &ev, 1) > 0) { if (use_event(&ev)) print_event(&ev); } -- cgit v1.2.1