Manage xlaunch scripts as a tmux session, a la pista
authorSiraaj Khandkar <siraaj@khandkar.net>
Mon, 29 Aug 2022 13:58:07 +0000 (09:58 -0400)
committerSiraaj Khandkar <siraaj@khandkar.net>
Mon, 29 Aug 2022 13:58:07 +0000 (09:58 -0400)
home/.xinitrc
home/.xlaunch
home/.xlaunch.d.horlamca/xinput [new file with mode: 0755]
home/.xlaunch.d.horlamca/xset [new file with mode: 0755]
home/.xlaunch.d/0-dpi_scale [new file with mode: 0755]
home/.xlaunch.d/99-dmesg-notifier-error [new file with mode: 0755]
home/.xlaunch.d/99-dmesg-notifier-warn [new file with mode: 0755]
home/.xlaunch.d/dunst [new file with mode: 0755]
home/.xlaunch.d/mpd [new file with mode: 0755]
home/.xlaunch.d/xbindkeys [new file with mode: 0755]
home/.xlaunch.d/xscreensaver [new file with mode: 0755]

index a329c4b..27af33d 100755 (executable)
@@ -9,4 +9,4 @@ else
 fi
 
 dbus-update-activation-environment $opt_systemd DBUS_SESSION_BUS_ADDRESS DISPLAY XAUTHORITY
-exec dbus-launch --sh-syntax --exit-with-session ~/.xlaunch
+exec dbus-launch --sh-syntax --exit-with-session ~/.xlaunch startx
index 9f33d9b..5c37cbe 100755 (executable)
@@ -2,6 +2,9 @@
 
 set -e
 
+SOCK="$HOME/.xlaunch.sock"
+SESSION='xlaunch'
+TMUX="tmux -S $SOCK"
 LOG_FILE=~/var/log/xlaunch.log
 
 log() {
@@ -11,74 +14,86 @@ log() {
     printf "${fmt}\n" $@ | twrap.sh >> "$LOG_FILE"
 }
 
-launch_then_killall() {
-    local -r program="$1"
-    local -r timeout="${2:-1}" # 2nd arg or default to 1.
+counter_next() {
+    local -r file="$_counter_file"
 
-    "$program"&
-    sleep "$timeout"
-    killall "$program"
+    awk '{n = $1} END {print n + 1}' "$file" | sponge "$file"
+    cat "$file"
 }
 
-dpi_scale() {
-    # IDK what magic is at work here, but launching mate-appearance-properties
-    # does the job better than setting scaling variables.
-    # TODO Get to the bottom of how it works and replicate directly.
-    # TODO Try this: https://wiki.archlinux.org/title/HiDPI#Xorg
-    launch_then_killall 'mate-appearance-properties' 1
-
-    # GDK 3 (GTK 3)
-    # https://wiki.archlinux.org/index.php/HiDPI#GDK_3_(GTK_3)
-    #export GDK_SCALE=2
-
-    # QT
-    # https://wiki.archlinux.org/index.php/HiDPI#Qt_5
-    # https://doc.qt.io/qt-5/highdpi.html
-    # https://blog.qt.io/blog/2016/01/26/high-dpi-support-in-qt-5-6/
-    #export QT_SCALE_FACTOR=2  # Causes qutebrowser UI fonts to have large gaps.
-    #export QT_FONT_DPI=192  # Scales qutebrowser UI fonts as expected.
-}
+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"
 
-launch_common() {
-    local -r scripts_dir=~/.xlaunch.d
-
-    # XXX dunst lazily started by dbus?
-    dunst --startup_notification -conf ~/.config/dunst/dunstrc &
-    xbindkeys
-    xscreensaver &
-    dpi_scale&
-    #mpd --kill || true
-    #mpd
-    log '[error] Looking for scripts in directory: "%s"' "$scripts_dir"
-    for script in "$scripts_dir"/*; do
-        log '[debug] Launching script: "%s"' "$script"
-        "$script"
-    done
+    $TMUX new-window -t "$SESSION" -n "$window_name"
+    $TMUX send-keys  -t "$SESSION":"$window_id"."$pane" "$command" ENTER
 }
 
-launch_specialized() {
-    local -r scripts_dir=~/.xlaunch.d."$(hostname)"
+launch_from_directory() {
+    local -r scripts_dir="$1"
 
-    log '[error] Looking for scripts in directory: "%s"' "$scripts_dir"
+    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"
-            "$script"
+            tmux_new_win "$script"
+            sleep 0.1 # TODO Find a way to block between starts instead.
         done
     else
-        log '[error] scripts_dir not found: %s' "$scripts_dir"
+        log '[warning] Directory not found: %s' "$scripts_dir"
     fi
 }
 
-main() {
-    log '[info] Starting X11'
+_start() {
+    $TMUX new-session -d -s "$SESSION"
+    $TMUX set-option -gt "$SESSION" allow-rename off
 
-    launch_common
-    launch_specialized
+    _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
 }
 
-main
+_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
+}
+
+main "$*"
diff --git a/home/.xlaunch.d.horlamca/xinput b/home/.xlaunch.d.horlamca/xinput
new file mode 100755 (executable)
index 0000000..1e00a82
--- /dev/null
@@ -0,0 +1,8 @@
+#! /bin/bash
+
+set -e
+
+#touchpad_name='DELL0A6A:00 0488:120A Touchpad'  # Dell Precision 7760
+
+# Enable tap-to-click
+#xinput set-prop "$touchpad_name" 'Synaptics Tap Action' 0 0 0 0 1 3 2
diff --git a/home/.xlaunch.d.horlamca/xset b/home/.xlaunch.d.horlamca/xset
new file mode 100755 (executable)
index 0000000..7cd9a12
--- /dev/null
@@ -0,0 +1,5 @@
+#! /bin/sh
+
+set -e
+
+xset b off  # Turn off the bell. Which the speaker crackles on - why?
diff --git a/home/.xlaunch.d/0-dpi_scale b/home/.xlaunch.d/0-dpi_scale
new file mode 100755 (executable)
index 0000000..55dcb7d
--- /dev/null
@@ -0,0 +1,33 @@
+#! /bin/bash
+
+set -e
+
+launch_then_killall() {
+    local -r program="$1"
+    local -r timeout="${2:-1}" # 2nd arg or default to 1.
+
+    "$program"&
+    sleep "$timeout"
+    killall "$program"
+}
+
+main() {
+    # IDK what magic is at work here, but launching mate-appearance-properties
+    # does the job better than setting scaling variables.
+    # TODO Get to the bottom of how it works and replicate directly.
+    # TODO Try this: https://wiki.archlinux.org/title/HiDPI#Xorg
+    launch_then_killall 'mate-appearance-properties' 1
+
+    # GDK 3 (GTK 3)
+    # https://wiki.archlinux.org/index.php/HiDPI#GDK_3_(GTK_3)
+    #export GDK_SCALE=2
+
+    # QT
+    # https://wiki.archlinux.org/index.php/HiDPI#Qt_5
+    # https://doc.qt.io/qt-5/highdpi.html
+    # https://blog.qt.io/blog/2016/01/26/high-dpi-support-in-qt-5-6/
+    #export QT_SCALE_FACTOR=2  # Causes qutebrowser UI fonts to have large gaps.
+    #export QT_FONT_DPI=192  # Scales qutebrowser UI fonts as expected.
+}
+
+main
diff --git a/home/.xlaunch.d/99-dmesg-notifier-error b/home/.xlaunch.d/99-dmesg-notifier-error
new file mode 100755 (executable)
index 0000000..40a2d10
--- /dev/null
@@ -0,0 +1,10 @@
+#! /bin/bash
+
+set -e
+
+sleep 3 # wait for dunst to hopefully restart
+sudo dmesg --follow --level=emerg,alert,crit,err --since=now \
+| while read line
+    do
+        notify-send -u critical 'dmesg error' "$line"
+    done
diff --git a/home/.xlaunch.d/99-dmesg-notifier-warn b/home/.xlaunch.d/99-dmesg-notifier-warn
new file mode 100755 (executable)
index 0000000..a021734
--- /dev/null
@@ -0,0 +1,10 @@
+#! /bin/bash
+
+set -e
+
+sleep 3 # wait for dunst to hopefully restart
+sudo dmesg --follow --level=warn --since=now \
+| while read line
+    do
+        notify-send -u low 'dmesg warning' "$line"
+    done
diff --git a/home/.xlaunch.d/dunst b/home/.xlaunch.d/dunst
new file mode 100755 (executable)
index 0000000..fb39ad2
--- /dev/null
@@ -0,0 +1,25 @@
+#! /bin/sh
+
+set -e
+
+# XXX dunst lazily started by dbus, but need to remove competing notification systems, like:
+#
+#         $ grep -rIHn 'org.freedesktop.Notifications' /usr/share/dbus-1/services
+#         $ sudo rm /usr/share/dbus-1/services/org.freedesktop.mate.Notifications.service
+#
+#dunst --startup_notification -conf ~/.config/dunst/dunstrc
+
+# Initially dunst is started before hidpi settings are complete, so we need to
+# restarted it after they have done so.
+
+# Hope this is enough for the initial dunst start to have been triggered.
+sleep 1
+
+# XXX Not using pkill to avoid killing self, which is expected to be named dunst as well.
+ps -eo pid,cmd | awk '$2 ~ /\/usr\/bin\/dunst\>/ {print $1}' | xargs -I% kill %
+
+# Hope this is enough for hidpi stuff to finish setting.
+sleep 1
+
+# Now dunst should start scaled:
+dunst --startup_notification
diff --git a/home/.xlaunch.d/mpd b/home/.xlaunch.d/mpd
new file mode 100755 (executable)
index 0000000..af02b8f
--- /dev/null
@@ -0,0 +1,5 @@
+#! /bin/sh
+
+set -e
+
+mpd --no-daemon
diff --git a/home/.xlaunch.d/xbindkeys b/home/.xlaunch.d/xbindkeys
new file mode 100755 (executable)
index 0000000..73cb8e9
--- /dev/null
@@ -0,0 +1,5 @@
+#! /bin/sh
+
+set -e
+
+xbindkeys -n
diff --git a/home/.xlaunch.d/xscreensaver b/home/.xlaunch.d/xscreensaver
new file mode 100755 (executable)
index 0000000..7e202a2
--- /dev/null
@@ -0,0 +1,5 @@
+#! /bin/sh
+
+set -e
+
+xscreensaver
This page took 0.029886 seconds and 4 git commands to generate.