From 489bc65381074124be3e8216634b237e6094d3c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20=C3=85dahl?= Date: Fri, 1 Apr 2022 08:34:46 +0200 Subject: [PATCH] tests/utils: Add helper to flush the input thread Add a helper function that ensures any queued virtual input events have been flushed from the input thread. This works by posting a task to the input thread, which will itself queue another callback back to the main thread. Once the main thread callback is invoked, the flush call is unblocked and the function returns. Upon this, any previously emitted virtual input event should have already passed through the input thread back into the main thread, however not necessarily fully processed. For making sure it has been processed, one also have to make sure the stage has been updated, e.g. via `meta_wait_for_paint()`. Part-of: --- src/backends/native/meta-seat-impl.h | 1 + src/backends/native/meta-seat-native.h | 2 ++ src/tests/meta-test-utils.c | 47 ++++++++++++++++++++++++++ src/tests/meta-test-utils.h | 3 ++ 4 files changed, 53 insertions(+) diff --git a/src/backends/native/meta-seat-impl.h b/src/backends/native/meta-seat-impl.h index 995582bcd..fee0bd638 100644 --- a/src/backends/native/meta-seat-impl.h +++ b/src/backends/native/meta-seat-impl.h @@ -133,6 +133,7 @@ MetaSeatImpl * meta_seat_impl_new (MetaSeatNative *seat_native, void meta_seat_impl_destroy (MetaSeatImpl *seat_impl); +META_EXPORT_TEST void meta_seat_impl_run_input_task (MetaSeatImpl *seat_impl, GTask *task, GSourceFunc dispatch_func); diff --git a/src/backends/native/meta-seat-native.h b/src/backends/native/meta-seat-native.h index c9ddcf63e..750d3c14c 100644 --- a/src/backends/native/meta-seat-native.h +++ b/src/backends/native/meta-seat-native.h @@ -34,6 +34,7 @@ #include "backends/native/meta-pointer-constraint-native.h" #include "backends/native/meta-xkb-utils.h" #include "clutter/clutter.h" +#include "core/util-private.h" typedef struct _MetaSeatNative MetaSeatNative; @@ -66,6 +67,7 @@ struct _MetaSeatNative }; #define META_TYPE_SEAT_NATIVE meta_seat_native_get_type () +META_EXPORT_TEST G_DECLARE_FINAL_TYPE (MetaSeatNative, meta_seat_native, META, SEAT_NATIVE, ClutterSeat) diff --git a/src/tests/meta-test-utils.c b/src/tests/meta-test-utils.c index 313792c2c..155460d4a 100644 --- a/src/tests/meta-test-utils.c +++ b/src/tests/meta-test-utils.c @@ -27,6 +27,9 @@ #include "backends/meta-monitor-config-store.h" #include "backends/meta-virtual-monitor.h" +#include "backends/native/meta-backend-native.h" +#include "backends/native/meta-input-thread.h" +#include "backends/native/meta-seat-native.h" #include "core/display-private.h" #include "core/window-private.h" #include "meta-test/meta-context-test.h" @@ -795,3 +798,47 @@ meta_create_test_monitor (MetaContext *context, return virtual_monitor; } + +#ifdef HAVE_NATIVE_BACKEND +static gboolean +callback_idle (gpointer user_data) +{ + GMainLoop *loop = user_data; + + g_main_loop_quit (loop); + return G_SOURCE_REMOVE; +} + +static gboolean +queue_callback (GTask *task) +{ + g_idle_add (callback_idle, g_task_get_task_data (task)); + return G_SOURCE_REMOVE; +} +#endif + +void +meta_flush_input (MetaContext *context) +{ +#ifdef HAVE_NATIVE_BACKEND + MetaBackend *backend = meta_context_get_backend (context); + ClutterSeat *seat; + MetaSeatNative *seat_native; + g_autoptr (GTask) task = NULL; + g_autoptr (GMainLoop) loop = NULL; + + g_assert_true (META_IS_BACKEND_NATIVE (backend)); + + seat = meta_backend_get_default_seat (backend); + seat_native = META_SEAT_NATIVE (seat); + + task = g_task_new (backend, NULL, NULL, NULL); + loop = g_main_loop_new (NULL, FALSE); + g_task_set_task_data (task, loop, NULL); + + meta_seat_impl_run_input_task (seat_native->impl, task, + (GSourceFunc) queue_callback); + + g_main_loop_run (loop); +#endif +} diff --git a/src/tests/meta-test-utils.h b/src/tests/meta-test-utils.h index 6122e2e4f..f4490be7d 100644 --- a/src/tests/meta-test-utils.h +++ b/src/tests/meta-test-utils.h @@ -122,4 +122,7 @@ MetaVirtualMonitor * meta_create_test_monitor (MetaContext *context, int height, float refresh_rate); +META_EXPORT +void meta_flush_input (MetaContext *context); + #endif /* TEST_UTILS_H */