From e1ef91b696da9fc3b9a388320084d6a64eb82f28 Mon Sep 17 00:00:00 2001 From: Tobias Reisinger Date: Thu, 13 Oct 2022 16:08:14 +0200 Subject: [PATCH] Add systemd based autostart system --- .bin/autostart-manage | 66 +++++++++++++++++++++++++++ .bin/{start_audio => start-audio} | 0 .config/completionsrc | 16 ++++++- .config/systemd/user/autostart.target | 5 ++ .xprofile | 2 +- 5 files changed, 87 insertions(+), 2 deletions(-) create mode 100755 .bin/autostart-manage rename .bin/{start_audio => start-audio} (100%) create mode 100644 .config/systemd/user/autostart.target diff --git a/.bin/autostart-manage b/.bin/autostart-manage new file mode 100755 index 0000000..470f153 --- /dev/null +++ b/.bin/autostart-manage @@ -0,0 +1,66 @@ +#!/usr/bin/env bash + +base_dir="$HOME/.config/systemd/user/" + +_filename () { + echo "autostart-$1.service" +} + +_systemctl () { + systemctl --user "${@:2}" "$(_filename "$1")" +} + +_echo_table () { + printf "Unit\tFilename\tEnabled\n" + for unit in "$@" + do + printf "%s\t%s\t%s\n" \ + "$unit" \ + "$(_filename "$unit")" \ + "$(_systemctl "$unit" is-enabled)" + done +} + +_autostart_manage_add () { + cat <"$base_dir$(_filename "$1")" +[Unit] +Description=$1 + +[Service] +ExecStart=$2 +EOF +} + +_autostart_manage_list () { + systemctl --user list-unit-files \ + | grep "autostart-" \ + | awk '{print $1}' \ + | sed -e 's/^autostart-//' -e 's/.service$//' +} + +_autostart_manage_info () { + mapfile -t autostart_units < <( _autostart_manage_list ) + _echo_table "${autostart_units[@]}" | column -t -s$'\t' +} + +_autostart_manage_enable () { + _systemctl "$1" add-wants autostart.target +} + +_autostart_manage_disable () { + _systemctl "$1" disable +} + +_autostart_manage_rm () { + rm "$base_dir$(_filename "$1")" +} + +case $1 in + list) _autostart_manage_list ;; + info) _autostart_manage_info ;; + add) _autostart_manage_add "$2" "$3" ;; + enable) _autostart_manage_enable "$2" ;; + disable) _autostart_manage_disable "$2" ;; + rm) _autostart_manage_rm "$2" ;; + *) echo "'$1' is not valid" ;; +esac diff --git a/.bin/start_audio b/.bin/start-audio similarity index 100% rename from .bin/start_audio rename to .bin/start-audio diff --git a/.config/completionsrc b/.config/completionsrc index b1549bb..e6a88e1 100644 --- a/.config/completionsrc +++ b/.config/completionsrc @@ -2,7 +2,7 @@ _dote_completions() { - if [ "${#COMP_WORDS[@]}" != "2" ]; then + if [ "${#COMP_WORDS[@]}" == "1" ]; then return fi @@ -13,3 +13,17 @@ _dote_completions() done < <(compgen -W "$(.e -l)" "${COMP_WORDS[1]}") } complete -F _dote_completions ".e" + +_autostart_manage_completions() +{ + if [ "${#COMP_WORDS[@]}" == "2" ]; then + return + fi + + COMPREPLY=() + while IFS='' read -r line + do + COMPREPLY+=("$line") + done < <(compgen -W "$(autostart-manage list)" "${COMP_WORDS[1]}") +} +complete -F _autostart_manage_completions "autostart-manage" diff --git a/.config/systemd/user/autostart.target b/.config/systemd/user/autostart.target new file mode 100644 index 0000000..3a5ccb0 --- /dev/null +++ b/.config/systemd/user/autostart.target @@ -0,0 +1,5 @@ +[Unit] +Description=Current graphical user session +Documentation=man:systemd.special(7) +RefuseManualStart=no +StopWhenUnneeded=no diff --git a/.xprofile b/.xprofile index dbca9ef..3be3943 100644 --- a/.xprofile +++ b/.xprofile @@ -29,7 +29,7 @@ _autostart_apps() _autostart() { ### wait for audio - start_audio pipewire + start-audio pipewire ### wait for internet wait_for_service "network-online.target"