#!/bin/bash

# .xsession
#     executed by Xsession as part of standard desktop startup
#     symlink ".xinitrc" to this file for running startx

# I like to include the full paths to applications I start from here; no alarms
# and no surprises.  Anything that needs to keep running, I tend to start in a
# subshell to avoid zombies.

# Local environment setup

MACHINE=$(uname -n)
WM=/usr/bin/evilwm

PATH="/usr/local/bin:/usr/bin:/bin:/usr/games"
test -d "$HOME/bin" && PATH="$HOME/bin:$PATH"
export PATH

LANG=en_GB.UTF-8
LC_CTYPE=en_GB.UTF-8
LC_COLLATE=C
export LANG LC_CTYPE LC_COLLATE

# Ensure gpg-agent is running and relevant environment variables are present
# for all child processes.
eval $(/usr/bin/gpg-agent --daemon --sh)

# Merge .Xresources
test -f $HOME/.Xresources && /usr/bin/xrdb -merge $HOME/.Xresources

# Set monitor DPI - see also .Xresources, .xsettingsd
/usr/bin/xrandr --dpi 168

# Generated by arandr, configure multi-monitor geometry:
#/usr/bin/xrandr --output eDP1 --primary --mode 1920x1080 --pos 0x0 --rotate normal \
#	--output DP1 --off --output DP2 --off \
#	--output HDMI1 --mode 1920x1080 --pos 1920x0 --rotate normal \
#	--output HDMI2 --off --output VIRTUAL1 --off

# Load keymap for this machine, if present
test -f $HOME/.xkb-$MACHINE && /usr/bin/xkbcomp $HOME/.xkb-$MACHINE :0 >/dev/null 2>&1

# If a monitor calibration file exists for this machine, load it
test -f $HOME/.icc-$MACHINE && /usr/bin/xcalib $HOME/.icc-$MACHINE >/dev/null 2>&1

# XSETTINGS handling
( /usr/bin/xsettingsd >/dev/null 2>&1 & )

# Various things seem to need this running now
( /usr/lib/policykit-1-gnome/polkit-gnome-authentication-agent-1 >/dev/null 2>&1 & )

# Background power management
( /usr/bin/xfce4-power-manager >/dev/null 2>&1 & )

# NetworkManager applet doesn't seem to appear in xfce4-panel's "Add New
# Items", so start it here:
( /usr/bin/nm-applet >/dev/null 2>&1 & )

# xss-lock runs xscreensaver when handling screen saver/lock signals.
( /usr/bin/xss-lock -- /usr/bin/xscreensaver-command -lock >/dev/null 2>&1 & )
( /usr/bin/xscreensaver -no-splash >/dev/null 2>&1 & )

# Touchpad/mouse configuration.  This makes my Thinkpad behave:
/usr/bin/synclient TapButton1=1 TapButton2=2
# This approach may work better for certain devices:
#/usr/bin/xinput set-int-prop "SynPS/2 Synaptics TouchPad" "libinput Tapping Enabled" 8 1
#/usr/bin/xinput set-int-prop "SynPS/2 Synaptics TouchPad" "libinput Horizonal Scroll Enabled" 8 0
#/usr/bin/xinput set-float-prop "SynPS/2 Synaptics TouchPad" "libinput Accel Speed" 0.5
#/usr/bin/xinput set-button-map "SynPS/2 Synaptics TouchPad" 1 3 2 4 5 6 7

# Oh how I dislike the X server beeping at me
/usr/bin/xset b off

# Fire up window manager and hot key handler
( $WM >/dev/null 2>&1 & )
( /usr/bin/xbindkeys >/dev/null 2>&1 & )

# Set simple background pattern if present, else a solid colour
if test -f "$HOME/.bgbitmap"; then
	/usr/bin/xsetroot -bitmap "$HOME/.bgbitmap" -fg \#402040 -bg \#381838 -cursor_name left_ptr
else
	/usr/bin/xsetroot -solid \#402040 -cursor_name left_ptr
fi

# Lots of Xterms spawned in fixed locations by default
( /usr/bin/uxterm -g 80x11+0+0 >/dev/null 2>&1 & )
( /usr/bin/uxterm -g 80x11+646+0 >/dev/null 2>&1 & )
( /usr/bin/uxterm -g 64x11+1292+0 >/dev/null 2>&1 & )
( /usr/bin/uxterm -g 96x24+0+204 >/dev/null 2>&1 & )
( /usr/bin/uxterm -g 96x24+0-0 >/dev/null 2>&1 & )
( /usr/bin/uxterm -g 96x24+774+204 >/dev/null 2>&1 & )
( /usr/bin/uxterm -g 96x24+774-0 >/dev/null 2>&1 & )

# Hand children back to the nanny
disown -a

# For a laptop, xfce4-panel makes a good magic process, as it gives visibility
# to the NetworkManager control applet and provides a "Log Out" menu option.
# Be aware that Xfce is a victim of Client-Side Decoration. 
exec /usr/bin/xfce4-panel
