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
This commit is contained in:
Consolatis 2022-07-28 01:06:56 +02:00 committed by Johan Malm
parent b8995e45dc
commit d6ea0d68cf
3 changed files with 44 additions and 3 deletions

View 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

View file

@ -3,4 +3,4 @@ Name=labwc
Comment=A wayland stacking compositor
Exec=labwc
Type=Application
DesktopNames=wlroots

View 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);