launcher: Get the seat id from logind/dbus and drop fallbacks

The fallbacks were neccesarry for tests but we added some more advanced
logind mocking which will get us the right values from dbus. There is no
point in those fallbacks anymore.

Part-of: <https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/4022>
This commit is contained in:
Sebastian Wick
2024-11-12 19:19:08 +01:00
parent 302fb21a68
commit 1f945691e7
4 changed files with 35 additions and 99 deletions

View File

@ -54,7 +54,6 @@ struct _MetaLauncher
MetaBackend *backend; MetaBackend *backend;
MetaDBusLogin1Session *session_proxy; MetaDBusLogin1Session *session_proxy;
MetaDBusLogin1Seat *seat_proxy; MetaDBusLogin1Seat *seat_proxy;
char *seat_id;
gboolean session_active; gboolean session_active;
gboolean have_control; gboolean have_control;
@ -95,7 +94,6 @@ meta_launcher_dispose (GObject *object)
launcher->have_control = FALSE; launcher->have_control = FALSE;
} }
g_clear_pointer (&launcher->seat_id, g_free);
g_clear_object (&launcher->seat_proxy); g_clear_object (&launcher->seat_proxy);
g_clear_object (&launcher->session_proxy); g_clear_object (&launcher->session_proxy);
@ -305,8 +303,7 @@ find_systemd_session (char **session_id,
} }
static MetaDBusLogin1Session * static MetaDBusLogin1Session *
get_session_proxy (const char *fallback_session_id, get_session_proxy (GCancellable *cancellable,
GCancellable *cancellable,
GError **error) GError **error)
{ {
g_autofree char *proxy_path = NULL; g_autofree char *proxy_path = NULL;
@ -317,21 +314,10 @@ get_session_proxy (const char *fallback_session_id,
if (!find_systemd_session (&session_id, &local_error)) if (!find_systemd_session (&session_id, &local_error))
{ {
if (fallback_session_id) g_propagate_prefixed_error (error,
{ g_steal_pointer (&local_error),
meta_topic (META_DEBUG_BACKEND, "Could not get session ID: ");
"Failed to get seat ID: %s, using fallback (%s)", return NULL;
local_error->message, fallback_session_id);
g_clear_error (&local_error);
session_id = g_strdup (fallback_session_id);
}
else
{
g_propagate_prefixed_error (error,
g_steal_pointer (&local_error),
"Could not get session ID: ");
return NULL;
}
} }
proxy_path = proxy_path =
@ -352,27 +338,33 @@ get_session_proxy (const char *fallback_session_id,
} }
static MetaDBusLogin1Seat * static MetaDBusLogin1Seat *
get_seat_proxy (char *seat_id, get_seat_proxy (MetaDBusLogin1Session *session_proxy,
GCancellable *cancellable, GCancellable *cancellable,
GError **error) GError **error)
{ {
g_autofree char *seat_proxy_path = GVariant *seat_variant;
get_escaped_dbus_path ("/org/freedesktop/login1/seat", seat_id); MetaDBusLogin1Seat *seat_proxy;
const char *seat_path;
GDBusProxyFlags flags; GDBusProxyFlags flags;
MetaDBusLogin1Seat *seat;
seat_variant = meta_dbus_login1_session_get_seat (session_proxy);
g_variant_get (seat_variant, "(s&o)", NULL, &seat_path);
flags = G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START; flags = G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START;
seat = seat_proxy =
meta_dbus_login1_seat_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM, meta_dbus_login1_seat_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
flags, flags,
"org.freedesktop.login1", "org.freedesktop.login1",
seat_proxy_path, seat_path,
cancellable, cancellable,
error); error);
if (!seat) if (!seat_proxy)
g_prefix_error (error, "Could not get seat proxy: "); g_prefix_error (error, "Could not get seat proxy: ");
return seat; g_warn_if_fail (g_dbus_proxy_get_name_owner (G_DBUS_PROXY (seat_proxy)));
return seat_proxy;
} }
static void static void
@ -399,70 +391,29 @@ on_active_changed (MetaDBusLogin1Session *session,
sync_active (self); sync_active (self);
} }
static char *
get_seat_id (GError **error)
{
g_autoptr (GError) local_error = NULL;
g_autofree char *session_id = NULL;
char *seat_id = NULL;
int r;
if (!find_systemd_session (&session_id, &local_error))
{
g_propagate_prefixed_error (error,
g_steal_pointer (&local_error),
"Could not get session ID: ");
return NULL;
}
r = sd_session_get_seat (session_id, &seat_id);
if (r < 0)
{
g_set_error (error,
G_IO_ERROR,
G_IO_ERROR_NOT_FOUND,
"Could not get seat for session: %s", g_strerror (-r));
return NULL;
}
return seat_id;
}
MetaLauncher * MetaLauncher *
meta_launcher_new (MetaBackend *backend, meta_launcher_new (MetaBackend *backend,
const char *fallback_session_id,
const char *fallback_seat_id,
GError **error) GError **error)
{ {
g_autoptr (MetaLauncher) launcher = NULL; g_autoptr (MetaLauncher) launcher = NULL;
g_autoptr (MetaDBusLogin1Session) session_proxy = NULL; g_autoptr (MetaDBusLogin1Session) session_proxy = NULL;
g_autoptr (MetaDBusLogin1Seat) seat_proxy = NULL; g_autoptr (MetaDBusLogin1Seat) seat_proxy = NULL;
g_autoptr (GError) local_error = NULL; g_autoptr (GError) local_error = NULL;
g_autofree char *seat_id = NULL;
gboolean have_control = FALSE; gboolean have_control = FALSE;
session_proxy = get_session_proxy (fallback_session_id, NULL, error); session_proxy = get_session_proxy (NULL, error);
if (!session_proxy) if (!session_proxy)
return NULL; return NULL;
seat_id = get_seat_id (&local_error); seat_proxy = get_seat_proxy (session_proxy, NULL, &local_error);
if (!seat_id) if (!seat_proxy)
{ {
if (fallback_seat_id) meta_topic (META_DEBUG_BACKEND,
{ "Failed to get the seat of proxy %s: %s",
meta_topic (META_DEBUG_BACKEND, meta_dbus_login1_session_get_id (session_proxy),
"Failed to get seat ID: %s, using fallback (%s)", local_error->message);
local_error->message, fallback_seat_id);
g_clear_error (&local_error);
seat_id = g_strdup (fallback_seat_id);
}
}
if (seat_id) g_clear_error (&local_error);
{
seat_proxy = get_seat_proxy (seat_id, NULL, error);
if (!seat_proxy)
return NULL;
} }
if (!meta_dbus_login1_session_call_take_control_sync (session_proxy, if (!meta_dbus_login1_session_call_take_control_sync (session_proxy,
@ -486,7 +437,6 @@ meta_launcher_new (MetaBackend *backend,
launcher->session_active = TRUE; launcher->session_active = TRUE;
launcher->have_control = have_control; launcher->have_control = have_control;
launcher->seat_proxy = g_steal_pointer (&seat_proxy); launcher->seat_proxy = g_steal_pointer (&seat_proxy);
launcher->seat_id = g_steal_pointer (&seat_id);
g_signal_connect (launcher->session_proxy, g_signal_connect (launcher->session_proxy,
"notify::active", "notify::active",
@ -525,7 +475,10 @@ meta_launcher_is_session_controller (MetaLauncher *launcher)
const char * const char *
meta_launcher_get_seat_id (MetaLauncher *launcher) meta_launcher_get_seat_id (MetaLauncher *launcher)
{ {
return launcher->seat_id; if (!launcher->seat_proxy)
return NULL;
return meta_dbus_login1_seat_get_id (launcher->seat_proxy);
} }
MetaDBusLogin1Session * MetaDBusLogin1Session *

View File

@ -30,8 +30,6 @@ G_DECLARE_FINAL_TYPE (MetaLauncher,
typedef struct _MetaDBusLogin1Session MetaDBusLogin1Session; typedef struct _MetaDBusLogin1Session MetaDBusLogin1Session;
MetaLauncher *meta_launcher_new (MetaBackend *backend, MetaLauncher *meta_launcher_new (MetaBackend *backend,
const char *session_id,
const char *custom_seat_id,
GError **error); GError **error);
gboolean meta_launcher_activate_vt (MetaLauncher *self, gboolean meta_launcher_activate_vt (MetaLauncher *self,

View File

@ -745,29 +745,14 @@ meta_backend_native_create_launcher (MetaBackend *backend,
MetaBackendNativePrivate *priv = MetaBackendNativePrivate *priv =
meta_backend_native_get_instance_private (native); meta_backend_native_get_instance_private (native);
g_autoptr (MetaLauncher) launcher = NULL; g_autoptr (MetaLauncher) launcher = NULL;
const char *session_id = NULL;
const char *seat_id = NULL;
*launcher_out = NULL; *launcher_out = NULL;
switch (priv->mode)
{
case META_BACKEND_NATIVE_MODE_DEFAULT:
break;
case META_BACKEND_NATIVE_MODE_HEADLESS:
case META_BACKEND_NATIVE_MODE_TEST_HEADLESS:
break;
case META_BACKEND_NATIVE_MODE_TEST_VKMS:
session_id = "dummy";
seat_id = "seat0";
break;
}
if (priv->mode == META_BACKEND_NATIVE_MODE_HEADLESS || if (priv->mode == META_BACKEND_NATIVE_MODE_HEADLESS ||
priv->mode == META_BACKEND_NATIVE_MODE_TEST_HEADLESS) priv->mode == META_BACKEND_NATIVE_MODE_TEST_HEADLESS)
return TRUE; return TRUE;
launcher = meta_launcher_new (backend, session_id, seat_id, error); launcher = meta_launcher_new (backend, error);
if (!launcher) if (!launcher)
return FALSE; return FALSE;

View File

@ -622,7 +622,7 @@ meta_backend_x11_create_launcher (MetaBackend *backend,
*launcher_out = NULL; *launcher_out = NULL;
launcher = meta_launcher_new (backend, NULL, NULL, &local_error); launcher = meta_launcher_new (backend, &local_error);
if (!launcher) if (!launcher)
{ {