labwc/src/config/session.c

144 lines
3 KiB
C
Raw Normal View History

2021-09-24 21:45:48 +01:00
// SPDX-License-Identifier: GPL-2.0-only
#define _POSIX_C_SOURCE 200809L
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
2021-07-22 21:30:17 +01:00
#include <wlr/util/log.h>
#include "common/buf.h"
#include "common/mem.h"
#include "common/spawn.h"
2020-10-09 19:46:59 +01:00
#include "common/string-helpers.h"
2023-01-31 03:35:13 +01:00
#include "config/session.h"
static bool
isfile(const char *path)
{
struct stat st;
return (!stat(path, &st));
}
static bool
string_empty(const char *s)
{
return !s || !*s;
}
static void
process_line(char *line)
{
if (string_empty(line) || line[0] == '#') {
return;
}
char *key = NULL;
char *p = strchr(line, '=');
if (!p) {
return;
}
*p = '\0';
2020-10-09 19:46:59 +01:00
key = string_strip(line);
struct buf value;
buf_init(&value);
buf_add(&value, string_strip(++p));
buf_expand_shell_variables(&value);
if (string_empty(key) || !value.len) {
goto error;
}
setenv(key, value.buf, 1);
error:
free(value.buf);
}
2023-01-31 03:35:13 +01:00
static void
read_environment_file(const char *filename)
{
char *line = NULL;
size_t len = 0;
FILE *stream = fopen(filename, "r");
if (!stream) {
return;
}
2021-07-22 21:30:17 +01:00
wlr_log(WLR_INFO, "read environment file %s", filename);
while (getline(&line, &len, stream) != -1) {
char *p = strrchr(line, '\n');
if (p) {
*p = '\0';
}
process_line(line);
}
free(line);
fclose(stream);
}
static char *
build_path(const char *dir, const char *filename)
{
if (string_empty(dir) || string_empty(filename)) {
return NULL;
}
return strdup_printf("%s/%s", dir, filename);
}
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 = strdup_printf("dbus-update-activation-environment %s", env_keys);
spawn_async_no_shell(cmd);
free(cmd);
cmd = strdup_printf("systemctl --user import-environment %s", 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);
char *environment = build_path(dir, "environment");
2020-10-22 19:42:06 +01:00
if (!environment) {
return;
2020-10-22 19:42:06 +01:00
}
read_environment_file(environment);
free(environment);
}
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");
char *autostart = build_path(dir, "autostart");
2020-10-22 19:42:06 +01:00
if (!autostart) {
return;
2020-10-22 19:42:06 +01:00
}
if (!isfile(autostart)) {
wlr_log(WLR_ERROR, "no autostart file");
goto out;
}
2021-07-22 21:30:17 +01:00
wlr_log(WLR_INFO, "run autostart file %s", autostart);
char *cmd = strdup_printf("sh %s", autostart);
spawn_async_no_shell(cmd);
2020-10-22 19:42:06 +01:00
free(cmd);
out:
free(autostart);
}