Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- # xstart - dead simple combination of xinit and startx
- # requires Xorg xset xauth timeout moreutils bash
- #log redirection
- logfile="$XDG_LOG_HOME"/xorg.log
- exec > >(ts '[%Y-%m-%d %H:%M:%S]'|tee -i $logfile)
- exec 2>&1
- echo Starting xstart
- trap 'cleanup "$pid"' EXIT
- readonly stty=$(stty -g)
- error() {
- # shellcheck disable=SC2059
- printf -- "xstart: $2" "${@:3}" >&2
- exit "$1"
- }
- warn() {
- # shellcheck disable=SC2059
- printf -- "xstart:" "${@:2}" >&2
- }
- cleanup() {
- if [ "$(ps -o comm= "$1")" = Xorg ]; then
- kill "$1"
- # Send SIGKILL after 10 seconds if the xserver is taking too long to
- # terminate.
- timeout 10 tail --pid="$1" -f /dev/null
- case $? in
- 124) kill -s KILL "$1"
- esac
- xauth remove :"$tty"
- fi
- if ! stty "$stty"; then
- stty sane
- fi
- exit
- }
- export XHOME=${XHOME:-$HOME}
- export XSERVERRC=${XSERVERRC:-$XHOME/.xserverrc}
- XAUTHORITY=${XAUTHORITY:-$XDG_RUNTIME_HOME/xauthority}
- export XDEFAULT=${XDEFAULT:-$XHOME/.xdesktop}
- export XPROFILE=${XPROFILE:-$XHOME/.xprofile}
- XPROFILESYS=/etc/xprofile
- export XRESOURCES=${XRESOURCES:-$XHOME/.Xresources}
- XRESOURCESSYS=/etc/X11/xinit/.Xresources
- export XMODMAP=${XMODMAP:-$XHOME/.Xmodmap}
- XMODMAPSYS=/etc/X11/xinit/.Xmodmap
- if [ "$DISPLAY" ]; then
- error 1 '%s: DISPLAY already present\n' "$DISPLAY"
- fi
- if ! readonly tty=$(fgconsole); then
- exit
- fi
- echo Creating Xauthority in "$XAUTHORITY"
- touch "$XAUTHORITY"
- export XAUTHORITY
- # NOTE: Clobbers existing entries.
- xauth add :"$tty" MIT-MAGIC-COOKIE-1 "$(mcookie)"
- echo Finished seting up Xauthority
- echo Loading Xserver
- $XSERVERRC :"$tty" -auth "$XAUTHORITY" & pid=$!
- export DISPLAY=:$tty
- echo Establishing connection to Xserver
- # Wait for the xserver to become available before continuing.
- # NOTE: This is the same logic xinit(1) uses.
- attempts=60
- until xset q > /dev/null 2>&1; do
- if [ "$attempts" -gt 0 ]; then
- attempts=$((attempts - 1))
- sleep 1
- else
- error 1 '%s: failed to connect to the server, giving up\n' "$DISPLAY"
- fi
- done
- echo Connected to Xserver to Xserver
- # merge in defaults and keymaps and source profile
- echo Parsing Xserver
- [ -f "$XPROFILESYS" ] && . "$XPROFILESYS"
- [ -f "$XPROFILE" ] && . "$XPROFILE"
- echo Loading Xreasources
- [ -f "$XRESOURCESSYS" ] && xrdb -merge "$XRESOURCESSYS"
- [ -f "$XRESOURCES" ] && xrdb -merge -I$XHOME $XRESOURCES
- echo loading key boad configuration
- [ -f "$XMODMAPSYS" ] && xmodmap "$XMODMAPSYS"
- [ -f "$XMODMAP" ] && xmodmap "$XMODMAP"
- echo configuration loaded
- # start some nice programs
- if [ -d /etc/X11/xinit/xinitrc.d ] ; then
- for f in /etc/X11/xinit/xinitrc.d/?*.sh ; do
- [ -x "$f" ] && . "$f"
- done
- unset f
- fi
- (
- unset XDG_SEAT
- unset XDG_SESSION_ID
- unset XDG_VTNR
- dbus-update-activation-environment --systemd --all
- )
- systemctl --user start graphical.target
- echo Loading the xclient
- session_default="$(cat "$XDEFAULT")"
- session=${1:-"$session_default"}
- if [ "x$session" == "x" ]; then
- warn NO VALID SESSION FOUND. FALLING BACK TO XTERM
- session=xterm
- fi
- case $session in
- kde|plasma)
- echo Loading KDE Plasma
- startkde
- ;;
- openbox|openbox3)
- echo Loading Openbox
- export XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-"$HOME/.config"}
- [ -f "/etc/xdg/openbox/environment" ] && . "/etc/xdg/openbox/environment"
- [ -f "$XDG_CONFIG_HOME/openbox/environment" ] && . "$XDG_CONFIG_HOME/openbox/environment"
- [ -f "/etc/xdg/openbox/autostart" ] && . "/etc/xdg/openbox/autostart"
- [ -f "$XDG_CONFIG_HOME/openbox/autostart" ] && . "$XDG_CONFIG_HOME/openbox/autostart"
- openbox
- ;;
- xinit)
- echo Loading the system default
- /etc/X11/xinitrc
- ;;
- # No known session, try to run it as command
- *)
- echo Loading $session
- "${1+"$@"}"
- ;;
- esac
- EXIT_CODE=$?
- systemctl --user stop graphical.target
- echo xclient terminated with exit $EXIT_CODE
- echo Terminating the Xserver
- cleanup "$pid"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement