From ca3f3ea9ce435eb0a53a4b8eaec5d0a6ddc3fbf1 Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Wed, 20 Aug 2014 17:15:50 +1000 Subject: [PATCH] test: add helpers to wait for specific events litest_wait_for_event() returns if any event is available. litest_wait_for_event_of_type(... type, type, type, -1) returns if any of the given event types is availble. All other events are discarded. Signed-off-by: Peter Hutterer Reviewed-by: Hans de Goede --- test/litest.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ test/litest.h | 2 ++ 2 files changed, 48 insertions(+) diff --git a/test/litest.c b/test/litest.c index 00db464b..fcc8de1c 100644 --- a/test/litest.c +++ b/test/litest.c @@ -780,6 +780,52 @@ litest_scale(const struct litest_device *d, unsigned int axis, double val) return (max - min) * val/100.0 + min; } +void +litest_wait_for_event(struct libinput *li) +{ + return litest_wait_for_event_of_type(li, -1); +} + +void +litest_wait_for_event_of_type(struct libinput *li, ...) +{ + va_list args; + enum libinput_event_type types[32] = {LIBINPUT_EVENT_NONE}; + size_t ntypes = 0; + enum libinput_event_type type; + + va_start(args, li); + type = va_arg(args, int); + while ((int)type != -1) { + assert(type > 0); + assert(ntypes < ARRAY_LENGTH(types)); + types[ntypes++] = type; + } + va_end(args); + + while (1) { + size_t i; + struct libinput_event *event; + + while ((type = libinput_next_event_type(li)) == LIBINPUT_EVENT_NONE) { + msleep(10); + libinput_dispatch(li); + } + + /* no event mask means wait for any event */ + if (ntypes == 0) + return; + + for (i = 0; i < ntypes; i++) { + if (type == types[i]) + return; + } + + event = libinput_get_event(li); + libinput_event_destroy(event); + } +} + void litest_drain_events(struct libinput *li) { diff --git a/test/litest.h b/test/litest.h index 2dcb7b20..815a571b 100644 --- a/test/litest.h +++ b/test/litest.h @@ -137,6 +137,8 @@ void litest_button_click(struct litest_device *d, void litest_keyboard_key(struct litest_device *d, unsigned int key, bool is_press); +void litest_wait_for_event(struct libinput *li); +void litest_wait_for_event_of_type(struct libinput *li, ...); void litest_drain_events(struct libinput *li); void litest_assert_empty_queue(struct libinput *li); -- 2.34.1