aboutsummaryrefslogtreecommitdiff
path: root/.local/bin/sx
blob: bda1ba018ad53fb0d78dda2bd8d725017a2e621f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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"