From a8b013b006320c78de786398cd526ddcfe307b7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20=C3=85dahl?= Date: Wed, 30 Mar 2022 17:27:51 +0200 Subject: [PATCH] Add beginning of input capture API This API aims to provide a way for users to capture input devices under certain conditions, for example when a pointer crosses a specified barrier. So far only part of the API is implemented, specifially the session management as well as zone advertisement, where a zone refers to a region in the compositor which edges will eventually be made available for barrier placement. So far the remote access handle is created while the session is enable, despite the input capturing isn't actually active yet. This will change in the future once it can actually become active. v2: Remove absolute/relative pointer, keep only pointer (ofourdan) Part-of: --- .../org.gnome.Mutter.InputCapture.xml | 70 +++ src/backends/meta-backend.c | 8 + src/backends/meta-input-capture-session.c | 500 ++++++++++++++++++ src/backends/meta-input-capture-session.h | 42 ++ src/backends/meta-input-capture.c | 158 ++++++ src/backends/meta-input-capture.h | 35 ++ src/meson.build | 11 + src/tests/input-capture-test-client.c | 340 ++++++++++++ src/tests/input-capture-tests.c | 209 ++++++++ src/tests/meson.build | 22 + src/tests/meta-test-utils.h | 1 + 11 files changed, 1396 insertions(+) create mode 100644 data/dbus-interfaces/org.gnome.Mutter.InputCapture.xml create mode 100644 src/backends/meta-input-capture-session.c create mode 100644 src/backends/meta-input-capture-session.h create mode 100644 src/backends/meta-input-capture.c create mode 100644 src/backends/meta-input-capture.h create mode 100644 src/tests/input-capture-test-client.c create mode 100644 src/tests/input-capture-tests.c diff --git a/data/dbus-interfaces/org.gnome.Mutter.InputCapture.xml b/data/dbus-interfaces/org.gnome.Mutter.InputCapture.xml new file mode 100644 index 000000000..c361b9744 --- /dev/null +++ b/data/dbus-interfaces/org.gnome.Mutter.InputCapture.xml @@ -0,0 +1,70 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/backends/meta-backend.c b/src/backends/meta-backend.c index 0a2ff8bb9..331582912 100644 --- a/src/backends/meta-backend.c +++ b/src/backends/meta-backend.c @@ -59,6 +59,7 @@ #include "backends/meta-dbus-session-watcher.h" #include "backends/meta-idle-manager.h" #include "backends/meta-idle-monitor-private.h" +#include "backends/meta-input-capture.h" #include "backends/meta-input-mapper-private.h" #include "backends/meta-input-settings-private.h" #include "backends/meta-logical-monitor.h" @@ -139,6 +140,7 @@ struct _MetaBackendPrivate MetaScreenCast *screen_cast; MetaRemoteDesktop *remote_desktop; #endif + MetaInputCapture *input_capture; #ifdef HAVE_LIBWACOM WacomDeviceDatabase *wacom_db; @@ -207,6 +209,7 @@ meta_backend_dispose (GObject *object) g_clear_object (&priv->remote_desktop); g_clear_object (&priv->screen_cast); #endif + g_clear_object (&priv->input_capture); g_clear_object (&priv->dbus_session_watcher); g_clear_object (&priv->remote_access_controller); @@ -580,6 +583,11 @@ meta_backend_real_post_init (MetaBackend *backend) META_DBUS_SESSION_MANAGER (priv->remote_desktop)); #endif /* HAVE_REMOTE_DESKTOP */ + priv->input_capture = meta_input_capture_new (backend); + meta_remote_access_controller_add ( + priv->remote_access_controller, + META_DBUS_SESSION_MANAGER (priv->input_capture)); + if (!meta_monitor_manager_is_headless (priv->monitor_manager)) init_pointer_position (backend); diff --git a/src/backends/meta-input-capture-session.c b/src/backends/meta-input-capture-session.c new file mode 100644 index 000000000..47a8fd2aa --- /dev/null +++ b/src/backends/meta-input-capture-session.c @@ -0,0 +1,500 @@ +/* + * Copyright (C) 2022 Red Hat Inc. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + * + */ + +#include "config.h" + +#include "backends/meta-input-capture-session.h" + +#include + +#include "backends/meta-dbus-session-watcher.h" +#include "backends/meta-dbus-session-manager.h" +#include "backends/meta-monitor-manager-private.h" +#include "backends/meta-logical-monitor.h" +#include "backends/meta-remote-access-controller-private.h" +#include "meta/boxes.h" +#include "meta/meta-backend.h" + +#include "meta-dbus-input-capture.h" + +#define META_INPUT_CAPTURE_SESSION_DBUS_PATH "/org/gnome/Mutter/InputCapture/Session" + +enum +{ + PROP_0, + + N_PROPS +}; + +struct _MetaInputCaptureSession +{ + MetaDBusInputCaptureSessionSkeleton parent; + + MetaDbusSessionManager *session_manager; + + GDBusConnection *connection; + char *peer_name; + + char *session_id; + char *object_path; + + gboolean enabled; + + uint32_t serial; + + MetaInputCaptureSessionHandle *handle; +}; + +static void initable_init_iface (GInitableIface *iface); + +static void meta_input_capture_session_init_iface (MetaDBusInputCaptureSessionIface *iface); + +static void meta_dbus_session_init_iface (MetaDbusSessionInterface *iface); + +static MetaInputCaptureSessionHandle * meta_input_capture_session_handle_new (MetaInputCaptureSession *session); + +G_DEFINE_TYPE_WITH_CODE (MetaInputCaptureSession, + meta_input_capture_session, + META_DBUS_TYPE_INPUT_CAPTURE_SESSION_SKELETON, + G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, + initable_init_iface) + G_IMPLEMENT_INTERFACE (META_DBUS_TYPE_INPUT_CAPTURE_SESSION, + meta_input_capture_session_init_iface) + G_IMPLEMENT_INTERFACE (META_TYPE_DBUS_SESSION, + meta_dbus_session_init_iface)) + +struct _MetaInputCaptureSessionHandle +{ + MetaRemoteAccessHandle parent; + + MetaInputCaptureSession *session; +}; + +G_DEFINE_TYPE (MetaInputCaptureSessionHandle, + meta_input_capture_session_handle, + META_TYPE_REMOTE_ACCESS_HANDLE) + +static void +init_remote_access_handle (MetaInputCaptureSession *session) +{ + MetaBackend *backend = + meta_dbus_session_manager_get_backend (session->session_manager); + MetaRemoteAccessController *remote_access_controller; + MetaRemoteAccessHandle *remote_access_handle; + + session->handle = meta_input_capture_session_handle_new (session); + + remote_access_controller = meta_backend_get_remote_access_controller (backend); + remote_access_handle = META_REMOTE_ACCESS_HANDLE (session->handle); + meta_remote_access_controller_notify_new_handle (remote_access_controller, + remote_access_handle); +} + +static gboolean +meta_input_capture_session_enable (MetaInputCaptureSession *session, + GError **error) +{ + g_assert (!session->enabled); + + session->enabled = TRUE; + + init_remote_access_handle (session); + + return TRUE; +} + +static void +meta_input_capture_session_disable (MetaInputCaptureSession *session) +{ + session->enabled = FALSE; + + if (session->handle) + { + MetaRemoteAccessHandle *remote_access_handle = + META_REMOTE_ACCESS_HANDLE (session->handle); + + meta_remote_access_handle_notify_stopped (remote_access_handle); + } +} + +static void +meta_input_capture_session_close (MetaDbusSession *dbus_session) +{ + MetaInputCaptureSession *session = + META_INPUT_CAPTURE_SESSION (dbus_session); + MetaDBusInputCaptureSession *skeleton = + META_DBUS_INPUT_CAPTURE_SESSION (session); + + if (session->enabled) + meta_input_capture_session_disable (session); + + meta_dbus_session_notify_closed (META_DBUS_SESSION (session)); + meta_dbus_input_capture_session_emit_closed (skeleton); + g_dbus_interface_skeleton_unexport (G_DBUS_INTERFACE_SKELETON (session)); + + g_object_unref (session); +} + +static gboolean +check_permission (MetaInputCaptureSession *session, + GDBusMethodInvocation *invocation) +{ + return g_strcmp0 (session->peer_name, + g_dbus_method_invocation_get_sender (invocation)) == 0; +} + +static gboolean +handle_add_barrier (MetaDBusInputCaptureSession *object, + GDBusMethodInvocation *invocation, + unsigned int serial, + GVariant *position) +{ + g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, + G_DBUS_ERROR_FAILED, + "Not implemented"); + return G_DBUS_METHOD_INVOCATION_HANDLED; +} + +static gboolean +handle_get_zones (MetaDBusInputCaptureSession *object, + GDBusMethodInvocation *invocation) +{ + MetaInputCaptureSession *session = META_INPUT_CAPTURE_SESSION (object); + MetaBackend *backend = + meta_dbus_session_manager_get_backend (session->session_manager); + MetaMonitorManager *monitor_manager = + meta_backend_get_monitor_manager (backend); + GVariant *zones_variant; + GVariantBuilder zones_builder; + GList *logical_monitors; + GList *l; + + g_variant_builder_init (&zones_builder, G_VARIANT_TYPE ("a(uuii)")); + logical_monitors = meta_monitor_manager_get_logical_monitors (monitor_manager); + for (l = logical_monitors; l; l = l->next) + { + MetaLogicalMonitor *logical_monitor = l->data; + MetaRectangle layout; + + layout = meta_logical_monitor_get_layout (logical_monitor); + g_variant_builder_add (&zones_builder, "(uuii)", + layout.width, + layout.height, + layout.x, + layout.y); + } + + zones_variant = g_variant_builder_end (&zones_builder); + + meta_dbus_input_capture_session_complete_get_zones (object, invocation, + session->serial, + zones_variant); + return G_DBUS_METHOD_INVOCATION_HANDLED; +} + +static gboolean +handle_enable (MetaDBusInputCaptureSession *skeleton, + GDBusMethodInvocation *invocation) +{ + MetaInputCaptureSession *session = META_INPUT_CAPTURE_SESSION (skeleton); + g_autoptr (GError) error = NULL; + + if (!check_permission (session, invocation)) + { + g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, + G_DBUS_ERROR_ACCESS_DENIED, + "Permission denied"); + return G_DBUS_METHOD_INVOCATION_HANDLED; + } + + if (session->enabled) + { + g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, + G_DBUS_ERROR_FAILED, + "Already enabled"); + return G_DBUS_METHOD_INVOCATION_HANDLED; + } + + if (!meta_input_capture_session_enable (session, &error)) + { + g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, + G_DBUS_ERROR_FAILED, + "Failed to enable input capture: %s", + error->message); + + return G_DBUS_METHOD_INVOCATION_HANDLED; + } + + meta_dbus_input_capture_session_complete_enable (skeleton, invocation); + + return G_DBUS_METHOD_INVOCATION_HANDLED; +} + +static gboolean +handle_disable (MetaDBusInputCaptureSession *skeleton, + GDBusMethodInvocation *invocation) +{ + MetaInputCaptureSession *session = META_INPUT_CAPTURE_SESSION (skeleton); + + if (!check_permission (session, invocation)) + { + g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, + G_DBUS_ERROR_ACCESS_DENIED, + "Permission denied"); + return G_DBUS_METHOD_INVOCATION_HANDLED; + } + + if (!session->enabled) + { + g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, + G_DBUS_ERROR_FAILED, + "Session not enabled"); + return G_DBUS_METHOD_INVOCATION_HANDLED; + } + + meta_input_capture_session_disable (session); + + meta_dbus_input_capture_session_complete_disable (skeleton, invocation); + + return G_DBUS_METHOD_INVOCATION_HANDLED; +} + +static gboolean +handle_release (MetaDBusInputCaptureSession *object, + GDBusMethodInvocation *invocation, + GVariant *position) +{ + g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, + G_DBUS_ERROR_ACCESS_DENIED, + "Not implemented"); + return G_DBUS_METHOD_INVOCATION_HANDLED; +} + +static gboolean +handle_close (MetaDBusInputCaptureSession *object, + GDBusMethodInvocation *invocation) +{ + MetaInputCaptureSession *session = META_INPUT_CAPTURE_SESSION (object); + + if (!check_permission (session, invocation)) + { + g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, + G_DBUS_ERROR_ACCESS_DENIED, + "Permission denied"); + return G_DBUS_METHOD_INVOCATION_HANDLED; + } + + meta_dbus_session_close (META_DBUS_SESSION (session)); + + meta_dbus_input_capture_session_complete_close (object, invocation); + + return G_DBUS_METHOD_INVOCATION_HANDLED; +} + +static void +on_monitors_changed (MetaMonitorManager *monitor_manager, + MetaInputCaptureSession *session) +{ + MetaDBusInputCaptureSession *skeleton = + META_DBUS_INPUT_CAPTURE_SESSION (session); + + session->serial++; + meta_input_capture_session_disable (session); + meta_dbus_input_capture_session_emit_zones_changed (skeleton); +} + +static gboolean +meta_input_capture_session_initable_init (GInitable *initable, + GCancellable *cancellable, + GError **error) +{ + MetaInputCaptureSession *session = META_INPUT_CAPTURE_SESSION (initable); + GDBusInterfaceSkeleton *interface_skeleton = G_DBUS_INTERFACE_SKELETON (session); + MetaBackend *backend = + meta_dbus_session_manager_get_backend (session->session_manager); + MetaMonitorManager *monitor_manager = + meta_backend_get_monitor_manager (backend); + + session->connection = + meta_dbus_session_manager_get_connection (session->session_manager); + if (!g_dbus_interface_skeleton_export (interface_skeleton, + session->connection, + session->object_path, + error)) + return FALSE; + + g_signal_connect_object (monitor_manager, "monitors-changed", + G_CALLBACK (on_monitors_changed), + session, 0); + + return TRUE; +} + +static void +initable_init_iface (GInitableIface *iface) +{ + iface->init = meta_input_capture_session_initable_init; +} + +static void +meta_input_capture_session_init_iface (MetaDBusInputCaptureSessionIface *iface) +{ + iface->handle_add_barrier = handle_add_barrier; + iface->handle_enable = handle_enable; + iface->handle_disable = handle_disable; + iface->handle_release = handle_release; + iface->handle_close = handle_close; + iface->handle_get_zones = handle_get_zones; +} + +static void +meta_dbus_session_init_iface (MetaDbusSessionInterface *iface) +{ + iface->close = meta_input_capture_session_close; +} + +static void +meta_input_capture_session_finalize (GObject *object) +{ + MetaInputCaptureSession *session = META_INPUT_CAPTURE_SESSION (object); + + g_clear_object (&session->handle); + g_free (session->peer_name); + g_free (session->session_id); + g_free (session->object_path); + + G_OBJECT_CLASS (meta_input_capture_session_parent_class)->finalize (object); +} + +static void +meta_input_capture_session_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + MetaInputCaptureSession *session = META_INPUT_CAPTURE_SESSION (object); + + switch (prop_id) + { + case N_PROPS + META_DBUS_SESSION_PROP_SESSION_MANAGER: + session->session_manager = g_value_get_object (value); + break; + case N_PROPS + META_DBUS_SESSION_PROP_PEER_NAME: + session->peer_name = g_value_dup_string (value); + break; + case N_PROPS + META_DBUS_SESSION_PROP_ID: + session->session_id = g_value_dup_string (value); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +meta_input_capture_session_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + MetaInputCaptureSession *session = META_INPUT_CAPTURE_SESSION (object); + + switch (prop_id) + { + case N_PROPS + META_DBUS_SESSION_PROP_SESSION_MANAGER: + g_value_set_object (value, session->session_manager); + break; + case N_PROPS + META_DBUS_SESSION_PROP_PEER_NAME: + g_value_set_string (value, session->peer_name); + break; + case N_PROPS + META_DBUS_SESSION_PROP_ID: + g_value_set_string (value, session->session_id); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +meta_input_capture_session_class_init (MetaInputCaptureSessionClass *klass) +{ + + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + object_class->finalize = meta_input_capture_session_finalize; + object_class->set_property = meta_input_capture_session_set_property; + object_class->get_property = meta_input_capture_session_get_property; + + meta_dbus_session_install_properties (object_class, N_PROPS); +} + +static void +meta_input_capture_session_init (MetaInputCaptureSession *session) +{ + static unsigned int global_session_number = 0; + + session->object_path = + g_strdup_printf (META_INPUT_CAPTURE_SESSION_DBUS_PATH "/u%u", + ++global_session_number); +} + +char * +meta_input_capture_session_get_object_path (MetaInputCaptureSession *session) +{ + return session->object_path; +} + +static MetaInputCaptureSessionHandle * +meta_input_capture_session_handle_new (MetaInputCaptureSession *session) +{ + MetaInputCaptureSessionHandle *handle; + + handle = g_object_new (META_TYPE_INPUT_CAPTURE_SESSION_HANDLE, NULL); + handle->session = session; + + return handle; +} + +static void +meta_input_capture_session_handle_stop (MetaRemoteAccessHandle *handle) +{ + MetaInputCaptureSession *session; + + session = META_INPUT_CAPTURE_SESSION_HANDLE (handle)->session; + if (!session) + return; + + meta_dbus_session_close (META_DBUS_SESSION (session)); +} + +static void +meta_input_capture_session_handle_class_init (MetaInputCaptureSessionHandleClass *klass) +{ + MetaRemoteAccessHandleClass *remote_access_handle_class = + META_REMOTE_ACCESS_HANDLE_CLASS (klass); + + remote_access_handle_class->stop = meta_input_capture_session_handle_stop; +} + +static void +meta_input_capture_session_handle_init (MetaInputCaptureSessionHandle *handle) +{ +} diff --git a/src/backends/meta-input-capture-session.h b/src/backends/meta-input-capture-session.h new file mode 100644 index 000000000..22553d1aa --- /dev/null +++ b/src/backends/meta-input-capture-session.h @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2022 Red Hat Inc. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + * + */ + +#ifndef META_INPUT_CAPTURE_SESSION_H +#define META_INPUT_CAPTURE_SESSION_H + +#include + +#include "backends/meta-input-capture.h" +#include "meta/meta-remote-access-controller.h" + +#define META_TYPE_INPUT_CAPTURE_SESSION (meta_input_capture_session_get_type ()) +G_DECLARE_FINAL_TYPE (MetaInputCaptureSession, meta_input_capture_session, + META, INPUT_CAPTURE_SESSION, + MetaDBusInputCaptureSessionSkeleton) + +#define META_TYPE_INPUT_CAPTURE_SESSION_HANDLE (meta_input_capture_session_handle_get_type ()) +G_DECLARE_FINAL_TYPE (MetaInputCaptureSessionHandle, + meta_input_capture_session_handle, + META, INPUT_CAPTURE_SESSION_HANDLE, + MetaRemoteAccessHandle) + +char *meta_input_capture_session_get_object_path (MetaInputCaptureSession *session); + +#endif /* META_INPUT_CAPTURE_SESSION_H */ diff --git a/src/backends/meta-input-capture.c b/src/backends/meta-input-capture.c new file mode 100644 index 000000000..3c5b3e835 --- /dev/null +++ b/src/backends/meta-input-capture.c @@ -0,0 +1,158 @@ +/* + * Copyright (C) 2022 Red Hat Inc. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + * + */ + +#include "config.h" + +#include "backends/meta-input-capture.h" + +#include "backends/meta-input-capture-session.h" +#include "backends/meta-backend-private.h" +#include "clutter/clutter.h" + +#include "meta-dbus-input-capture.h" + +#define META_INPUT_CAPTURE_DBUS_SERVICE "org.gnome.Mutter.InputCapture" +#define META_INPUT_CAPTURE_DBUS_PATH "/org/gnome/Mutter/InputCapture" + +typedef enum _MetaInputCaptureCapabilities +{ + META_INPUT_CAPTURE_CAPABILITY_NONE = 1 << 0, + META_INPUT_CAPTURE_CAPABILITY_KEYBOARD = 1 << 1, + META_INPUT_CAPTURE_CAPABILITY_POINTER = 1 << 2, + META_INPUT_CAPTURE_CAPABILITY_TOUCH = 1 << 3, +} MetaInputCaptureCapabilities; + +struct _MetaInputCapture +{ + MetaDbusSessionManager parent; +}; + +G_DEFINE_TYPE (MetaInputCapture, meta_input_capture, + META_TYPE_DBUS_SESSION_MANAGER) + +static gboolean +handle_create_session (MetaDBusInputCapture *skeleton, + GDBusMethodInvocation *invocation, + uint32_t capabilities, + MetaInputCapture *input_capture) +{ + MetaDbusSessionManager *session_manager = + META_DBUS_SESSION_MANAGER (input_capture); + MetaDbusSession *dbus_session; + MetaInputCaptureSession *session; + g_autoptr (GError) error = NULL; + char *session_path; + + dbus_session = + meta_dbus_session_manager_create_session (session_manager, + invocation, + &error, + NULL); + if (!dbus_session) + { + g_dbus_method_invocation_return_error_literal (invocation, G_DBUS_ERROR, + G_DBUS_ERROR_FAILED, + error->message); + return G_DBUS_METHOD_INVOCATION_HANDLED; + } + session = META_INPUT_CAPTURE_SESSION (dbus_session); + + session_path = meta_input_capture_session_get_object_path (session); + meta_dbus_input_capture_complete_create_session (skeleton, + invocation, + session_path); + + return G_DBUS_METHOD_INVOCATION_HANDLED; +} + +static void +meta_input_capture_constructed (GObject *object) +{ + MetaInputCapture *input_capture = META_INPUT_CAPTURE (object); + MetaDbusSessionManager *session_manager = + META_DBUS_SESSION_MANAGER (input_capture); + GDBusInterfaceSkeleton *interface_skeleton = + meta_dbus_session_manager_get_interface_skeleton (session_manager); + + g_signal_connect (interface_skeleton, "handle-create-session", + G_CALLBACK (handle_create_session), input_capture); + + G_OBJECT_CLASS (meta_input_capture_parent_class)->constructed (object); +} + +static void +meta_input_capture_class_init (MetaInputCaptureClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + object_class->constructed = meta_input_capture_constructed; +} + +static void +meta_input_capture_init (MetaInputCapture *input_capture) +{ +} + +static MetaInputCaptureCapabilities +calculate_supported_capabilities (MetaInputCapture *input_capture) +{ + MetaDbusSessionManager *session_manager = + META_DBUS_SESSION_MANAGER (input_capture); + MetaBackend *backend = + meta_dbus_session_manager_get_backend (session_manager); + ClutterSeat *seat = meta_backend_get_default_seat (backend); + ClutterVirtualDeviceType device_types; + MetaInputCaptureCapabilities supported_capabilities = + META_INPUT_CAPTURE_CAPABILITY_NONE; + + device_types = + clutter_seat_get_supported_virtual_device_types (seat); + + if (device_types & CLUTTER_VIRTUAL_DEVICE_TYPE_KEYBOARD) + supported_capabilities |= META_INPUT_CAPTURE_CAPABILITY_KEYBOARD; + if (device_types & CLUTTER_VIRTUAL_DEVICE_TYPE_POINTER) + supported_capabilities |= META_INPUT_CAPTURE_CAPABILITY_POINTER; + if (device_types & CLUTTER_VIRTUAL_DEVICE_TYPE_TOUCHSCREEN) + supported_capabilities |= META_INPUT_CAPTURE_CAPABILITY_TOUCH; + + return supported_capabilities; +} + +MetaInputCapture * +meta_input_capture_new (MetaBackend *backend) +{ + MetaInputCapture *input_capture; + g_autoptr (MetaDBusInputCapture) skeleton = NULL; + + skeleton = meta_dbus_input_capture_skeleton_new (); + input_capture = g_object_new (META_TYPE_INPUT_CAPTURE, + "backend", backend, + "service-name", META_INPUT_CAPTURE_DBUS_SERVICE, + "service-path", META_INPUT_CAPTURE_DBUS_PATH, + "session-gtype", META_TYPE_INPUT_CAPTURE_SESSION, + "interface-skeleton", skeleton, + NULL); + + meta_dbus_input_capture_set_supported_capabilities ( + META_DBUS_INPUT_CAPTURE (skeleton), + calculate_supported_capabilities (input_capture)); + + return input_capture; +} diff --git a/src/backends/meta-input-capture.h b/src/backends/meta-input-capture.h new file mode 100644 index 000000000..e229f473f --- /dev/null +++ b/src/backends/meta-input-capture.h @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2022 Red Hat Inc. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + * + */ + +#ifndef META_INPUT_CAPTURE_H +#define META_INPUT_CAPTURE_H + +#include "backends/meta-dbus-session-manager.h" + +#include "meta-dbus-input-capture.h" + +#define META_TYPE_INPUT_CAPTURE (meta_input_capture_get_type ()) +G_DECLARE_FINAL_TYPE (MetaInputCapture, meta_input_capture, + META, INPUT_CAPTURE, + MetaDbusSessionManager) + +MetaInputCapture *meta_input_capture_new (MetaBackend *backend); + +#endif /* META_INPUT_CAPTURE_H */ diff --git a/src/meson.build b/src/meson.build index 4af81aa8c..ba35c9279 100644 --- a/src/meson.build +++ b/src/meson.build @@ -226,6 +226,10 @@ mutter_sources = [ 'backends/meta-idle-manager.c', 'backends/meta-idle-manager.h', 'backends/meta-idle-monitor-private.h', + 'backends/meta-input-capture.c', + 'backends/meta-input-capture.h', + 'backends/meta-input-capture-session.c', + 'backends/meta-input-capture-session.h', 'backends/meta-input-device.c', 'backends/meta-input-mapper.c', 'backends/meta-input-mapper-private.h', @@ -1001,6 +1005,13 @@ foreach dbus_interface: dbus_interfaces mutter_built_sources += dbus_sources endforeach +dbus_input_capture_built_sources = gnome.gdbus_codegen('meta-dbus-input-capture', + dbus_interfaces_dir / 'org.gnome.Mutter.InputCapture.xml', + interface_prefix: 'org.gnome.Mutter', + namespace: 'MetaDBus', +) +mutter_built_sources += dbus_input_capture_built_sources + wayland_protocol_server_headers = [] wayland_protocol_client_headers = [] wayland_protocol_sources = [] diff --git a/src/tests/input-capture-test-client.c b/src/tests/input-capture-test-client.c new file mode 100644 index 000000000..4f6bb9856 --- /dev/null +++ b/src/tests/input-capture-test-client.c @@ -0,0 +1,340 @@ +/* + * Copyright (C) 2022 Red Hat Inc. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + * + */ + +#include "config.h" + +#include +#include + +#include "meta-dbus-input-capture.h" + +typedef struct +{ + unsigned int width; + unsigned int height; + int x; + int y; +} Zone; + +G_DEFINE_AUTOPTR_CLEANUP_FUNC (Zone, g_free) + +typedef enum _Capabilities +{ + CAPABILITY_NONE = 0, + CAPABILITY_KEYBOARD = 1, + CAPABILITY_POINTER = 2, + CAPABILITY_TOUCH = 4, +} Capabilities; + +typedef struct _InputCapture +{ + MetaDBusInputCapture *proxy; +} InputCapture; + +typedef struct _InputCaptureSession +{ + MetaDBusInputCaptureSession *proxy; + unsigned int serial; +} InputCaptureSession; + +static void +ping_mutter (InputCaptureSession *session) +{ + GDBusProxy *proxy = G_DBUS_PROXY (session->proxy); + GError *error = NULL; + + if (!g_dbus_connection_call_sync (g_dbus_proxy_get_connection (proxy), + "org.gnome.Mutter.InputCapture", + g_dbus_proxy_get_object_path (proxy), + "org.freedesktop.DBus.Peer", + "Ping", + NULL, + NULL, G_DBUS_CALL_FLAGS_NO_AUTO_START, -1, + NULL, &error)) + g_error ("Failed to ping D-Bus peer: %s", error->message); +} + +static void +write_state (InputCaptureSession *session, + const char *state) +{ + ping_mutter (session); + fprintf (stdout, "%s\n", state); + fflush (stdout); +} + +static InputCapture * +input_capture_new (void) +{ + InputCapture *input_capture; + GError *error = NULL; + + input_capture = g_new0 (InputCapture, 1); + input_capture->proxy = meta_dbus_input_capture_proxy_new_for_bus_sync ( + G_BUS_TYPE_SESSION, + G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START, + "org.gnome.Mutter.InputCapture", + "/org/gnome/Mutter/InputCapture", + NULL, + &error); + if (!input_capture->proxy) + g_error ("Failed to acquire proxy: %s", error->message); + + return input_capture; +} + +static InputCaptureSession * +input_capture_create_session (InputCapture *input_capture) +{ + GError *error = NULL; + InputCaptureSession *session; + g_autofree char *session_path = NULL; + + if (!meta_dbus_input_capture_call_create_session_sync (input_capture->proxy, + CAPABILITY_POINTER, + &session_path, + NULL, + &error)) + g_error ("Failed to create input capture session: %s", error->message); + + session = g_new0 (InputCaptureSession, 1); + session->proxy = meta_dbus_input_capture_session_proxy_new_for_bus_sync ( + G_BUS_TYPE_SESSION, + G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START, + "org.gnome.Mutter.InputCapture", + session_path, + NULL, &error); + if (!session->proxy) + g_error ("Failed to acquire proxy: %s", error->message); + + return session; +} + +static void +input_capture_session_close (InputCaptureSession *session) +{ + GError *error = NULL; + + if (!meta_dbus_input_capture_session_call_close_sync (session->proxy, + NULL, &error)) + g_error ("Failed to close session: %s", error->message); + + g_object_unref (session->proxy); + g_free (session); +} + +static GList * +input_capture_session_get_zones (InputCaptureSession *session) +{ + GError *error = NULL; + g_autoptr (GVariant) zones_variant = NULL; + GVariantIter iter; + GList *zones = NULL; + unsigned int width, height; + int x, y; + + if (!meta_dbus_input_capture_session_call_get_zones_sync (session->proxy, + &session->serial, + &zones_variant, + NULL, &error)) + g_error ("Failed to get zones: %s", error->message); + + g_variant_iter_init (&iter, zones_variant); + while (g_variant_iter_next (&iter, "(uuii)", &width, &height, &x, &y)) + { + Zone *zone; + + zone = g_new0 (Zone, 1); + *zone = (Zone) { + .width = width, + .height = height, + .x = x, + .y = y, + }; + zones = g_list_append (zones, zone); + } + + return zones; +} + +static void +input_capture_session_enable (InputCaptureSession *session) +{ + g_autoptr (GError) error = NULL; + + if (!meta_dbus_input_capture_session_call_enable_sync (session->proxy, + NULL, &error)) + g_warning ("Failed to enable session: %s", error->message); +} + +static void +input_capture_session_disable (InputCaptureSession *session) +{ + g_autoptr (GError) error = NULL; + + if (!meta_dbus_input_capture_session_call_disable_sync (session->proxy, + NULL, &error)) + g_warning ("Failed to disable session: %s", error->message); +} + +static void +test_sanity (void) +{ + InputCapture *input_capture; + InputCaptureSession *session; + + input_capture = input_capture_new (); + session = input_capture_create_session (input_capture); + + g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, + "*org.freedesktop.DBus.Error.Failed: Session not enabled*"); + input_capture_session_disable (session); + g_test_assert_expected_messages (); + + input_capture_session_enable (session); + + g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, + "*org.freedesktop.DBus.Error.Failed: Already enabled*"); + input_capture_session_enable (session); + g_test_assert_expected_messages (); + + input_capture_session_disable (session); + + g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, + "*org.freedesktop.DBus.Error.Failed: Session not enabled*"); + input_capture_session_disable (session); + g_test_assert_expected_messages (); + + input_capture_session_close (session); +} + +static void +on_zones_changed (MetaDBusInputCaptureSession *proxy, + int *zones_changed_count) +{ + *zones_changed_count += 1; +} + +static void +assert_zones (GList *zones, + const Zone *expected_zones, + int n_expected_zones) +{ + GList *l; + int i; + + g_assert_cmpuint (g_list_length (zones), ==, n_expected_zones); + + for (l = zones, i = 0; l; l = l->next, i++) + { + Zone *zone = l->data; + + g_assert_cmpint (zone->width, ==, expected_zones[i].width); + g_assert_cmpint (zone->height, ==, expected_zones[i].height); + g_assert_cmpint (zone->x, ==, expected_zones[i].x); + g_assert_cmpint (zone->y, ==, expected_zones[i].y); + } +} + +static void +test_zones (void) +{ + InputCapture *input_capture; + InputCaptureSession *session; + static const Zone expected_zones1[] = { + { .width = 800, .height = 600, .x = 0, .y = 0 }, + { .width = 1024, .height = 768, .x = 800, .y = 0 }, + }; + static const Zone expected_zones2[] = { + { .width = 1024, .height = 768, .x = 0, .y = 0 }, + }; + GList *zones; + int zones_changed_count = 0; + unsigned int serial; + + input_capture = input_capture_new (); + session = input_capture_create_session (input_capture); + + g_signal_connect (session->proxy, "zones-changed", + G_CALLBACK (on_zones_changed), + &zones_changed_count); + + zones = input_capture_session_get_zones (session); + assert_zones (zones, expected_zones1, G_N_ELEMENTS (expected_zones1)); + g_clear_list (&zones, g_free); + + write_state (session, "1"); + + while (zones_changed_count == 0) + g_main_context_iteration (NULL, TRUE); + + serial = session->serial; + g_clear_list (&zones, g_free); + + zones = input_capture_session_get_zones (session); + g_assert_cmpuint (session->serial, >, serial); + assert_zones (zones, expected_zones2, G_N_ELEMENTS (expected_zones2)); + g_clear_list (&zones, g_free); + + input_capture_session_close (session); +} + +static const struct +{ + const char *name; + void (* func) (void); +} test_cases[] = { + { "sanity", test_sanity, }, + { "zones", test_zones, }, +}; + +static void +print_to_stderr (const char *text) +{ + fputs (text, stderr); + fflush (stderr); +} + +int +main (int argc, + char **argv) +{ + const char *test_case; + int i; + + g_assert_cmpint (argc, ==, 2); + + test_case = argv[1]; + + g_set_print_handler (print_to_stderr); + g_test_init (&argc, &argv, NULL); + + for (i = 0; i < G_N_ELEMENTS (test_cases); i++) + { + if (g_strcmp0 (test_cases[i].name, test_case) == 0) + { + test_cases[i].func (); + return EXIT_SUCCESS; + } + } + + g_warning ("Invalid test case '%s'", test_case); + return EXIT_FAILURE; +} diff --git a/src/tests/input-capture-tests.c b/src/tests/input-capture-tests.c new file mode 100644 index 000000000..cb319aff0 --- /dev/null +++ b/src/tests/input-capture-tests.c @@ -0,0 +1,209 @@ +/* + * Copyright (C) 2022 Red Hat Inc. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + * + */ + +#include "config.h" + +#include + +#include "meta-test/meta-context-test.h" +#include "tests/meta-test-utils.h" + +typedef struct _InputCaptureTestClient +{ + GSubprocess *subprocess; + char *path; + GMainLoop *main_loop; + GDataInputStream *line_reader; +} InputCaptureTestClient; + +static MetaContext *test_context; + +static InputCaptureTestClient * +input_capture_test_client_new (const char *test_case) +{ + g_autofree char *test_client_path = NULL; + g_autoptr (GSubprocessLauncher) launcher = NULL; + GSubprocess *subprocess; + GError *error = NULL; + InputCaptureTestClient *test_client; + GInputStream *stdout_stream; + GDataInputStream *line_reader; + + test_client_path = g_test_build_filename (G_TEST_BUILT, + "src", + "tests", + "mutter-input-capture-test-client", + NULL); + launcher = g_subprocess_launcher_new (G_SUBPROCESS_FLAGS_STDOUT_PIPE); + subprocess = g_subprocess_launcher_spawn (launcher, + &error, + test_client_path, + test_case, + NULL); + if (!subprocess) + g_error ("Failed to launch input capture test client: %s", error->message); + + stdout_stream = g_subprocess_get_stdout_pipe (subprocess); + line_reader = g_data_input_stream_new (stdout_stream); + + test_client = g_new0 (InputCaptureTestClient, 1); + test_client->subprocess = subprocess; + test_client->main_loop = g_main_loop_new (NULL, FALSE); + test_client->line_reader = line_reader; + + return test_client; +} + +typedef struct +{ + GMainLoop *loop; + const char *expected_state; +} WaitData; + +static void +on_line_read (GObject *source_object, + GAsyncResult *res, + gpointer user_data) +{ + WaitData *data = user_data; + g_autofree char *line = NULL; + g_autoptr (GError) error = NULL; + + line = + g_data_input_stream_read_line_finish (G_DATA_INPUT_STREAM (source_object), + res, NULL, &error); + if (error) + g_error ("Failed to read line from test client: %s", error->message); + if (!line) + g_error ("Unexpected EOF"); + + g_assert_cmpstr (data->expected_state, ==, line); + + g_main_loop_quit (data->loop); +} + +static void +input_capture_test_client_wait_for_state (InputCaptureTestClient *test_client, + const char *expected_state) +{ + WaitData data; + + data.loop = g_main_loop_new (NULL, FALSE); + data.expected_state = expected_state; + + g_data_input_stream_read_line_async (test_client->line_reader, + G_PRIORITY_DEFAULT, + NULL, + on_line_read, + &data); + + g_main_loop_run (data.loop); + g_main_loop_unref (data.loop); +} + +static void +input_capture_test_client_finished (GObject *source_object, + GAsyncResult *res, + gpointer user_data) +{ + InputCaptureTestClient *test_client = user_data; + GError *error = NULL; + + if (!g_subprocess_wait_finish (test_client->subprocess, + res, + &error)) + { + g_error ("Failed to wait for input capture test client: %s", + error->message); + } + + g_main_loop_quit (test_client->main_loop); +} + +static void +input_capture_test_client_finish (InputCaptureTestClient *test_client) +{ + g_subprocess_wait_async (test_client->subprocess, NULL, + input_capture_test_client_finished, test_client); + + g_main_loop_run (test_client->main_loop); + + g_assert_true (g_subprocess_get_successful (test_client->subprocess)); + + g_main_loop_unref (test_client->main_loop); + g_object_unref (test_client->line_reader); + g_object_unref (test_client->subprocess); + g_free (test_client); +} + +static void +meta_test_input_capture_sanity (void) +{ + InputCaptureTestClient *test_client; + + test_client = input_capture_test_client_new ("sanity"); + input_capture_test_client_finish (test_client); +} + +static void +meta_test_input_capture_zones (void) +{ + g_autoptr (MetaVirtualMonitor) virtual_monitor1 = NULL; + g_autoptr (MetaVirtualMonitor) virtual_monitor2 = NULL; + InputCaptureTestClient *test_client; + + virtual_monitor1 = meta_create_test_monitor (test_context, 800, 600, 20.0); + virtual_monitor2 = meta_create_test_monitor (test_context, 1024, 768, 20.0); + + test_client = input_capture_test_client_new ("zones"); + + input_capture_test_client_wait_for_state (test_client, "1"); + + g_clear_object (&virtual_monitor1); + + input_capture_test_client_finish (test_client); +} + +static void +init_tests (void) +{ + g_test_add_func ("/backends/native/input-capture/sanity", + meta_test_input_capture_sanity); + g_test_add_func ("/backends/native/input-capture/zones", + meta_test_input_capture_zones); +} + +int +main (int argc, + char **argv) +{ + g_autoptr (MetaContext) context = NULL; + g_autoptr (GError) error = NULL; + + context = test_context = + meta_create_test_context (META_CONTEXT_TEST_TYPE_HEADLESS, + META_CONTEXT_TEST_FLAG_NO_X11); + g_assert (meta_context_configure (context, &argc, &argv, NULL)); + + init_tests (); + + return meta_context_test_run_tests (META_CONTEXT_TEST (context), + META_TEST_RUN_FLAG_NONE); +} diff --git a/src/tests/meson.build b/src/tests/meson.build index cdf3530fd..508059678 100644 --- a/src/tests/meson.build +++ b/src/tests/meson.build @@ -308,6 +308,23 @@ if have_native_tests install_rpath: pkglibdir, ) + input_capture_client = executable('mutter-input-capture-test-client', + sources: [ + 'input-capture-test-client.c', + dbus_input_capture_built_sources, + ], + include_directories: tests_includes, + c_args: [ + tests_c_args, + '-DG_LOG_DOMAIN="mutter-input-capture-test-client"', + ], + dependencies: [ + gio_unix_dep, + ], + install: have_installed_tests, + install_dir: mutter_installed_tests_libexecdir, + ) + # Native backend tests test_cases += [ { @@ -345,6 +362,11 @@ if have_native_tests 'suite': 'backends/native', 'sources': [ 'native-persistent-virtual-monitor.c' ], }, + { + 'name': 'input-capture', + 'suite': 'backends/native', + 'sources': [ 'input-capture-tests.c' ], + }, ] # KMS tests diff --git a/src/tests/meta-test-utils.h b/src/tests/meta-test-utils.h index 0232276a7..e89d016df 100644 --- a/src/tests/meta-test-utils.h +++ b/src/tests/meta-test-utils.h @@ -25,6 +25,7 @@ #include #include "backends/meta-backend-types.h" +#include "backends/meta-virtual-monitor.h" #include "meta/window.h" #define META_TEST_CLIENT_ERROR meta_test_client_error_quark ()