Advertisement
Guest User

xstart

a guest
Oct 23rd, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.02 KB | None | 0 0
  1. #!/usr/bin/env bash
  2. # xstart - dead simple combination of xinit and startx
  3.  
  4. # requires Xorg xset xauth timeout moreutils bash
  5.  
  6. #log redirection
  7. logfile="$XDG_LOG_HOME"/xorg.log
  8. exec > >(ts '[%Y-%m-%d %H:%M:%S]'|tee -i $logfile)
  9. exec 2>&1
  10.  
  11. echo Starting xstart
  12.  
  13. trap 'cleanup "$pid"' EXIT
  14. readonly stty=$(stty -g)
  15.  
  16. error() {
  17.     # shellcheck disable=SC2059
  18.     printf -- "xstart: $2" "${@:3}" >&2
  19.     exit "$1"
  20. }
  21.  
  22. warn() {
  23.     # shellcheck disable=SC2059
  24.     printf -- "xstart:" "${@:2}" >&2
  25. }
  26.  
  27. cleanup() {
  28.     if [ "$(ps -o comm= "$1")" = Xorg ]; then
  29.         kill "$1"
  30.  
  31.         # Send SIGKILL after 10 seconds if the xserver is taking too long to
  32.         # terminate.
  33.         timeout 10 tail --pid="$1" -f /dev/null
  34.  
  35.         case $? in
  36.             124) kill -s KILL "$1"
  37.         esac
  38.  
  39.         xauth remove :"$tty"
  40.     fi
  41.  
  42.     if ! stty "$stty"; then
  43.         stty sane
  44.     fi
  45.  
  46.     exit
  47. }
  48.  
  49. export XHOME=${XHOME:-$HOME}
  50. export XSERVERRC=${XSERVERRC:-$XHOME/.xserverrc}
  51. XAUTHORITY=${XAUTHORITY:-$XDG_RUNTIME_HOME/xauthority}
  52. export XDEFAULT=${XDEFAULT:-$XHOME/.xdesktop}
  53. export XPROFILE=${XPROFILE:-$XHOME/.xprofile}
  54. XPROFILESYS=/etc/xprofile
  55. export XRESOURCES=${XRESOURCES:-$XHOME/.Xresources}
  56. XRESOURCESSYS=/etc/X11/xinit/.Xresources
  57. export XMODMAP=${XMODMAP:-$XHOME/.Xmodmap}
  58. XMODMAPSYS=/etc/X11/xinit/.Xmodmap
  59.  
  60. if [ "$DISPLAY" ]; then
  61.     error 1 '%s: DISPLAY already present\n' "$DISPLAY"
  62. fi
  63.  
  64. if ! readonly tty=$(fgconsole); then
  65.     exit
  66. fi
  67.  
  68. echo Creating Xauthority in "$XAUTHORITY"
  69.  
  70. touch "$XAUTHORITY"
  71. export XAUTHORITY
  72.  
  73. # NOTE: Clobbers existing entries.
  74. xauth add :"$tty" MIT-MAGIC-COOKIE-1 "$(mcookie)"
  75.  
  76. echo Finished seting up Xauthority
  77.  
  78. echo Loading Xserver
  79.  
  80. $XSERVERRC :"$tty" -auth "$XAUTHORITY" & pid=$!
  81.  
  82. export DISPLAY=:$tty
  83.  
  84. echo Establishing connection to Xserver
  85.  
  86. # Wait for the xserver to become available before continuing.
  87. # NOTE: This is the same logic xinit(1) uses.
  88. attempts=60
  89. until xset q > /dev/null 2>&1; do
  90.     if [ "$attempts" -gt 0 ]; then
  91.         attempts=$((attempts - 1))
  92.         sleep 1
  93.     else
  94.         error 1 '%s: failed to connect to the server, giving up\n' "$DISPLAY"
  95.     fi
  96. done
  97.  
  98. echo Connected to Xserver to Xserver
  99.  
  100. # merge in defaults and keymaps and source profile
  101. echo Parsing Xserver
  102.  
  103. [ -f "$XPROFILESYS" ] && . "$XPROFILESYS"
  104. [ -f "$XPROFILE" ] && . "$XPROFILE"
  105.  
  106. echo Loading Xreasources
  107.  
  108. [ -f "$XRESOURCESSYS" ] && xrdb -merge "$XRESOURCESSYS"
  109. [ -f "$XRESOURCES" ] && xrdb -merge -I$XHOME $XRESOURCES
  110.  
  111. echo loading key boad configuration
  112.  
  113. [ -f "$XMODMAPSYS" ] && xmodmap "$XMODMAPSYS"
  114. [ -f "$XMODMAP" ] && xmodmap "$XMODMAP"
  115.  
  116. echo configuration loaded
  117. # start some nice programs
  118.  
  119. if [ -d /etc/X11/xinit/xinitrc.d ] ; then
  120.     for f in /etc/X11/xinit/xinitrc.d/?*.sh ; do
  121.         [ -x "$f" ] && . "$f"
  122.     done
  123.     unset f
  124. fi
  125.  
  126. (
  127.     unset XDG_SEAT
  128.     unset XDG_SESSION_ID
  129.     unset XDG_VTNR
  130.  
  131.     dbus-update-activation-environment --systemd --all
  132. )
  133.  
  134. systemctl --user start graphical.target
  135.  
  136. echo Loading the xclient
  137.  
  138. session_default="$(cat "$XDEFAULT")"
  139.  
  140. session=${1:-"$session_default"}
  141.  
  142. if [ "x$session" == "x" ]; then
  143.     warn NO VALID SESSION FOUND. FALLING BACK TO XTERM
  144.     session=xterm
  145. fi
  146.  
  147. case $session in
  148.     kde|plasma)
  149.         echo Loading KDE Plasma
  150.         startkde
  151.         ;;
  152.     openbox|openbox3)
  153.         echo Loading Openbox
  154.         export XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-"$HOME/.config"}
  155.         [ -f "/etc/xdg/openbox/environment" ] && . "/etc/xdg/openbox/environment"
  156.         [ -f "$XDG_CONFIG_HOME/openbox/environment" ] && . "$XDG_CONFIG_HOME/openbox/environment"
  157.         [ -f "/etc/xdg/openbox/autostart" ] && . "/etc/xdg/openbox/autostart"
  158.         [ -f "$XDG_CONFIG_HOME/openbox/autostart" ] && . "$XDG_CONFIG_HOME/openbox/autostart"
  159.         openbox
  160.         ;;
  161.     xinit)
  162.         echo Loading the system default
  163.         /etc/X11/xinitrc
  164.         ;;
  165.     # No known session, try to run it as command
  166.     *)
  167.         echo Loading $session
  168.         "${1+"$@"}"
  169.     ;;
  170. esac
  171.  
  172. EXIT_CODE=$?
  173.  
  174. systemctl --user stop graphical.target
  175.  
  176. echo xclient terminated with exit $EXIT_CODE
  177. echo Terminating the Xserver
  178.  
  179. cleanup "$pid"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement