]> git.mdlowis.com Git - proto/labwc.git/commitdiff
src/config/session.c: Update dbus / systemd activation environment
authorConsolatis <35009135+Consolatis@users.noreply.github.com>
Wed, 27 Jul 2022 23:06:56 +0000 (01:06 +0200)
committerJohan Malm <johanmalm@users.noreply.github.com>
Fri, 29 Jul 2022 05:45:24 +0000 (06:45 +0100)
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

docs/environment
docs/labwc.desktop
src/config/session.c

index cf2948afc321a687fe4857c0aad3c93d137b9aed..d89ffdb7c285331b3cc7af5418c199d502f71896 100644 (file)
@@ -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
 
index 880fbd57c54b2a61cceff9093dd76e4bc160ce4c..8df6c6c732f5f5ccc9fc9fc8b83eb9b90925e364 100644 (file)
@@ -3,4 +3,4 @@ Name=labwc
 Comment=A wayland stacking compositor
 Exec=labwc
 Type=Application
-
+DesktopNames=wlroots
index 39e23f12849dd3e9dcaa9742ecefeb8a493cd7ad..cadb6834bb67b5d2451ac13d7fb556ae69b95380 100644 (file)
@@ -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);