X-Git-Url: https://git.xandkar.net/?a=blobdiff_plain;f=home%2F.xlaunch;h=5c37cbe7d658f0bb8d2f869909c62b953f20430c;hb=6db098476f8d99b62a990c6d3ff27c29562145b9;hp=be3cc3617f24077ceaabb86820488e0e9648c5c5;hpb=d6c84e8b89440d9a31e6acfdd0d66092528e1948;p=khome.git diff --git a/home/.xlaunch b/home/.xlaunch index be3cc36..5c37cbe 100755 --- a/home/.xlaunch +++ b/home/.xlaunch @@ -2,25 +2,98 @@ set -e +SOCK="$HOME/.xlaunch.sock" +SESSION='xlaunch' +TMUX="tmux -S $SOCK" +LOG_FILE=~/var/log/xlaunch.log -launch_common() { - xbindkeys - xscreensaver & - mpd --kill || true - mpd - for script in ~/.xlaunch.d/*; do - "$script" - done +log() { + local -r fmt="$1" + shift + + printf "${fmt}\n" $@ | twrap.sh >> "$LOG_FILE" +} + +counter_next() { + local -r file="$_counter_file" + + awk '{n = $1} END {print n + 1}' "$file" | sponge "$file" + cat "$file" +} + +tmux_new_win() { + local -r command="$1" + local -r window_id=$(counter_next) + local -r window_name=$(basename "$command") + local -r pane=0 + + log \ + '[debug] tmux window_id:"%s", window_name:"%s", command:"%s"' \ + "$window_id" "$window_name" "$command" + + $TMUX new-window -t "$SESSION" -n "$window_name" + $TMUX send-keys -t "$SESSION":"$window_id"."$pane" "$command" ENTER +} + +launch_from_directory() { + local -r scripts_dir="$1" + + log '[info] Looking for scripts in directory: "%s"' "$scripts_dir" + if test -d "$scripts_dir" + then + for script in "$scripts_dir"/*; do + log '[debug] Launching script: "%s"' "$script" + tmux_new_win "$script" + sleep 0.1 # TODO Find a way to block between starts instead. + done + else + log '[warning] Directory not found: %s' "$scripts_dir" + fi } -launch_specialized() { - # XXX dunst lazily started by dbus - for script in ~/.xlaunch.d."$(hostname)"/*; do - "$script" - done +_start() { + $TMUX new-session -d -s "$SESSION" + $TMUX set-option -gt "$SESSION" allow-rename off + + _counter_file=$(mktemp) + launch_from_directory ~/.xlaunch.d + launch_from_directory ~/.xlaunch.d."$(hostname)" +} + +_startx() { + log '[info] Starting X11' + _restart + log '[info] Launching dwm' + exec dwm } -launch_common -launch_specialized +_stop() { + $TMUX kill-session -t "$SESSION" +} + +_restart() { + _stop || true + _start +} + +_attach() { + $TMUX attach -t "$SESSION" +} + +main() { + local -r command="$1" + + case "$command" in + #'' ) _startx;; + 'startx' ) _startx;; + 'start' ) _start;; + 'stop' ) _stop;; + 'restart' ) _restart;; + 'attach' ) _attach;; + *) + echo "[error] Unknown command: \"$command\". Known: startx, start, stop, restart, attach." + exit 1;; + esac +} -exec dwm +main "$*"