From 2e4ce7008bb8b37851c357d365fbb65e83097f52 Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Thu, 6 Aug 2020 13:59:20 +1000 Subject: [PATCH] libei: implement ei_peek_event() At least for the test suite, I'll need something that allows me to look at an event without removing it from the event queue. Just the event type isn't enough, I need things like "what are the caps on the device". Simplest way to allow this is a peek function that doesn't remove the event from the queue. Signed-off-by: Peter Hutterer --- src/libei.c | 12 ++++++++++++ src/libei.h | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/libei.c b/src/libei.c index 150840b..b685784 100644 --- a/src/libei.c +++ b/src/libei.c @@ -99,6 +99,8 @@ ei_event_destroy(struct ei_event *event) static OBJECT_IMPLEMENT_INIT(ei_event); +static +OBJECT_IMPLEMENT_REF(ei_event); _public_ OBJECT_IMPLEMENT_UNREF(ei_event); _public_ @@ -468,6 +470,16 @@ ei_get_event(struct ei *ei) return e; } +_public_ struct ei_event* +ei_peek_event(struct ei *ei) +{ + if (list_empty(&ei->event_queue)) + return NULL; + + struct ei_event *e = list_first_entry(&ei->event_queue, e, link); + return ei_event_ref(e); +} + _public_ enum ei_event_type ei_next_event_type(struct ei *ei) { diff --git a/src/libei.h b/src/libei.h index 32be7b6..d598d78 100644 --- a/src/libei.h +++ b/src/libei.h @@ -175,6 +175,25 @@ ei_dispatch(struct ei *ei); struct ei_event * ei_get_event(struct ei *ei); +/** + * Returns the next event in the internal event queue (or NULL) without + * removing that event from the queue, i.e. the next call to ei_get_event() + * will return that same event. + * + * This call is useful for checking whether there is an event and/or what + * type of event it is. + * + * Repeated calls to ei_peek_event() return the same event. + * + * The returned event is refcounted, use ei_event_unref() to drop the + * reference. + * + * A caller must not call ei_get_event() while holding a ref to an event + * returned by ei_peek_event(). + */ +struct ei_event * +ei_peek_event(struct ei *ei); + /** * Return the type of the next event in the event queue or @ref * EI_EVENT_NONE if none are available. This function does not remove the