#!/usr/bin/env zsh export XDG_CURRENT_DESKTOP=sway export XDG_SESSION_DESKTOP="${XDG_SESSION_DESKTOP:-sway}" export XDG_SESSION_TYPE=wayland VARIABLES="DESKTOP_SESSION XDG_CURRENT_DESKTOP XDG_SESSION_DESKTOP XDG_SESSION_TYPE" VARIABLES="${VARIABLES} DISPLAY I3SOCK SWAYSOCK WAYLAND_DISPLAY" VARIABLES="${VARIABLES} XCURSOR_THEME XCURSOR_SIZE" SESSION_TARGET="sway-session.target" SESSION_SHUTDOWN_TARGET="sway-session-shutdown.target" SWAY_UNIT="sway.scope" print_usage() { cat < [ARGS] --help Show this help message and exit. --add-env=NAME, -E NAME Add an variable to the environment imported into systemd and purged on exit. setup Perform sway session systemd setup. This will fail if the command is not run as a child of 'sway' (via an 'exec' or 'exec_always' directive or command). cleanup Meant to be called by the service manager as a part of sway-session-shutdown.target. EOF } while [ $# -gt 0 ]; do case "$1" in --help) print_usage exit 0 ;; --add-env=?*) VARIABLES="${VARIABLES} ${1#*=}" ;; --add-env | -E) shift VARIABLES="${VARIABLES} ${1}" ;; cleanup) TASK=cleanup ;; setup) TASK=setup ;; -*) echo "Unexpected option: $1" >&2 print_usage exit 1 ;; *) break ;; esac shift done function setup() { # Get process stack ppid=$(ps -o ppid= $$ | sed 's/ //g') ppcomm=$(ps -o comm= $ppid) if [[ $ppcomm == 'sh' ]]; then # sway exec command (a shell) forked before exec, get its parent ppid=$(ps -o ppid= $ppid | sed 's/ //g') ppcomm=$(ps -o comm= $ppid) fi if ! [[ $ppcomm == 'sway' ]]; then echo "Error: attempting to run setup while not invoked by sway!" exit 2 fi # Reset failed units systemctl --user reset-failed # Import environment echo $VARIABLES | xargs -- systemctl --user import-environment echo $VARIABLES | xargs -- dbus-update-activation-environment # Load scope unit busctl --user call org.freedesktop.systemd1 /org/freedesktop/systemd1 org.freedesktop.systemd1.Manager StartTransientUnit 'ssa(sv)a(sa(sv))' \ "$SWAY_UNIT" \ fail \ 7 \ PIDs au 1 $ppid \ Slice s session.slice \ Wants as 1 $SESSION_TARGET \ Before as 1 $SESSION_TARGET \ PropagatesStopTo as 1 $SESSION_TARGET \ OnSuccess as 1 $SESSION_SHUTDOWN_TARGET \ OnFailure as 1 $SESSION_SHUTDOWN_TARGET \ 0 } function cleanup() { systemctl --user unset-environment $VARIABLES } case $TASK in setup) setup ;; cleanup) cleanup ;; esac