aboutsummaryrefslogtreecommitdiff
path: root/.local/bin/sx
diff options
context:
space:
mode:
Diffstat (limited to '.local/bin/sx')
-rwxr-xr-x.local/bin/sx57
1 files changed, 57 insertions, 0 deletions
diff --git a/.local/bin/sx b/.local/bin/sx
new file mode 100755
index 0000000..bda1ba0
--- /dev/null
+++ b/.local/bin/sx
@@ -0,0 +1,57 @@
+#!/bin/sh --
+# LICENCE: MIT
+# https://github.com/Earnestly/sx
+
+stty=$(stty -g)
+tty=$(tty)
+tty=${tty#/dev/tty}
+
+cfgdir=${XDG_CONFIG_HOME:-$HOME/.config}/X11
+datadir=${XDG_CONFIG_HOME:-$HOME/.config}/X11
+
+cleanup() {
+ if [ "$pid" ] && kill -0 "$pid" 2> /dev/null; then
+ kill "$pid"
+ wait "$pid"
+ xorg=$?
+ fi
+
+ if ! stty "$stty"; then
+ stty sane
+ fi
+
+ xauth remove :"$tty"
+
+ if [ "$1" = exit ]; then
+ exit "${xorg:-0}"
+ fi
+}
+
+
+mkdir --parents -- "$cfgdir" "$datadir"
+export XAUTHORITY="${XAUTHORITY:-$datadir/Xauthority}"
+touch -- "$XAUTHORITY"
+export XDG_SESSION_TYPE=X11
+
+xauth add :"$tty" MIT-MAGIC-COOKIE-1 "$(od -An -N16 -tx /dev/urandom | tr -d ' ')"
+
+trap 'cleanup; trap - INT; kill -INT "$$"' INT
+trap 'cleanup exit' EXIT HUP TERM QUIT
+
+# Xorg will check whether it inherited a USR1 with a disposition of SIG_IGN and
+# use this state to reply back to the parent process with its own USR1.
+# This is done to indicate that the server is ready to accept connections.
+# Taking advantage of this feature allows launching the client directly from a
+# USR1 signal trap which obviates the need to poll for server readiness.
+
+trap 'DISPLAY=:$tty exec "${@:-$cfgdir/xinitrc}" & wait "$!"' USR1
+
+(trap '' USR1 && exec Xorg :"$tty" \
+ -keeptty vt"$tty" \
+ -noreset \
+ -auth "$XAUTHORITY" \
+ -configdir "$cfgdir/xorg.conf.d" \
+ -logfile "$HOME/.cache/xorg.log" \
+) & pid=$!
+
+wait "$pid"