From: Consolatis <35009135+Consolatis@users.noreply.github.com> Date: Wed, 27 Jul 2022 23:06:56 +0000 (+0200) Subject: src/config/session.c: Update dbus / systemd activation environment X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=d6ea0d68cfbf1b0b3e174db9919a0f8411a1b43b;p=proto%2Flabwc.git src/config/session.c: Update dbus / systemd activation environment This allows xdg-desktop-portal-wlr to work out of the box for screen-recording. If systemd or dbus is not available the environment update will fail gracefully. This patch will set XDG_CURRENT_DESKTOP=wlroots but a user may change this by either having the environment variable set before starting labwc or by having a different value set in ~/.config/labwc/environment. Based on PR #461 by @Joshua-Ashton --- diff --git a/docs/environment b/docs/environment index cf2948af..d89ffdb7 100644 --- a/docs/environment +++ b/docs/environment @@ -1,5 +1,8 @@ # Example environment file +# This allows xdg-desktop-portal-wlr to function (e.g. for screen-recording) +XDG_CURRENT_DESKTOP=wlroots + # Set keyboard layout to Swedish XKB_DEFAULT_LAYOUT=se diff --git a/docs/labwc.desktop b/docs/labwc.desktop index 880fbd57..8df6c6c7 100644 --- a/docs/labwc.desktop +++ b/docs/labwc.desktop @@ -3,4 +3,4 @@ Name=labwc Comment=A wayland stacking compositor Exec=labwc Type=Application - +DesktopNames=wlroots diff --git a/src/config/session.c b/src/config/session.c index 39e23f12..cadb6834 100644 --- a/src/config/session.c +++ b/src/config/session.c @@ -78,16 +78,51 @@ build_path(const char *dir, const char *filename) return NULL; } int len = strlen(dir) + strlen(filename) + 2; - char *buffer = calloc(len, 1); + char *buffer = calloc(1, len); strcat(buffer, dir); strcat(buffer, "/"); strcat(buffer, filename); return buffer; } +static void +update_activation_env(const char *env_keys) +{ + if (!getenv("DBUS_SESSION_BUS_ADDRESS")) { + /* Prevent accidentally auto-launching a dbus session */ + wlr_log(WLR_INFO, "Not updating dbus execution environment: " + "DBUS_SESSION_BUS_ADDRESS not set"); + return; + } + wlr_log(WLR_INFO, "Updating dbus execution environment"); + + char *cmd; + const char *dbus = "dbus-update-activation-environment "; + const char *systemd = "systemctl --user import-environment "; + + cmd = calloc(1, strlen(dbus) + strlen(env_keys) + 1); + strcat(cmd, dbus); + strcat(cmd, env_keys); + spawn_async_no_shell(cmd); + free(cmd); + + cmd = calloc(1, strlen(systemd) + strlen(env_keys) + 1); + strcat(cmd, systemd); + strcat(cmd, env_keys); + spawn_async_no_shell(cmd); + free(cmd); +} + void session_environment_init(const char *dir) { + /* + * Set default for XDG_CURRENT_DESKTOP so xdg-desktop-portal-wlr is happy. + * May be overriden either by already having a value set or by the user + * supplied environment file. + */ + setenv("XDG_CURRENT_DESKTOP", "wlroots", 0); + const char *environment = build_path(dir, "environment"); if (!environment) { return; @@ -99,6 +134,9 @@ session_environment_init(const char *dir) void session_autostart_init(const char *dir) { + /* Update dbus and systemd user environment, each may fail gracefully */ + update_activation_env("DISPLAY WAYLAND_DISPLAY XDG_CURRENT_DESKTOP"); + const char *autostart = build_path(dir, "autostart"); if (!autostart) { return; @@ -109,7 +147,7 @@ session_autostart_init(const char *dir) } wlr_log(WLR_INFO, "run autostart file %s", autostart); int len = strlen(autostart) + 4; - char *cmd = calloc(len, 1); + char *cmd = calloc(1, len); strcat(cmd, "sh "); strcat(cmd, autostart); spawn_async_no_shell(cmd);