]> ruderich.org/simon Gitweb - config/dotfiles.git/blobdiff - xinitrc
replace Xdefaults with Xresources
[config/dotfiles.git] / xinitrc
diff --git a/xinitrc b/xinitrc
index 201e6369a99fb01f1b79781c6270cd30217cc413..c4dd1c207c902d4b8945f4440849e47fdde888e2 100755 (executable)
--- a/xinitrc
+++ b/xinitrc
@@ -2,7 +2,7 @@
 
 # startx configuration file.
 
-# Copyright (C) 2011-2012  Simon Ruderich
+# Copyright (C) 2011-2014  Simon Ruderich
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 
-# Start ssh-agent if we have private keys, thanks to
+# Some systems don't set the LANG environment variable when starting xinitrc,
+# this causes problems for rxvt-unicode-256color which fails to display UTF-8
+# characters correctly when LANG is missing and for notify.py which also needs
+# LANG set (for Python's decode() function). Therefore load my general shell
+# environment which also includes $LANG.
+if test -f "$HOME/.shell/env"; then
+    # Debug function used in ~/.shell/env.
+    source_debug() {
+        # Do nothing.
+        :
+    }
+
+    . "$HOME/.shell/env"
+fi
+
+
+installed() {
+    type "$1" >/dev/null 2>&1
+}
+error() {
+    if installed xmessage; then
+        xmessage "$@" &
+    else
+        # Log message to a file which is displayed by my Zsh setup on startup.
+        printf '%s\n' "$*" >>"$HOME/.xinitrc.errors"
+    fi
+}
+# Save PID of the most recently started background process. Used to terminate
+# them when logging out to prevent unused background processes. On some
+# systems this is only necessary for some programs (like ssh-agent), haven't
+# yet figured out why.
+save_background_pid() {
+    background_pids="$background_pids $!"
+}
+background_pids=
+
+
+# Options, can be modified in ~/.xinitrc.local. To disable a boolean option
+# set it to an empty value.
+#
+# Default to use a background image.
+set_background=yes
+# Default to display a warning if `xautolock` is not available.
+screen_lock_force=yes
+# Lock screen after x minutes of inactivity, requires `xautolock` to be
+# installed.
+screen_lock_time=3
+# Locker program to lock the screen, used below with `xautolock`.
+screen_locker=xtrlock
+
+# Load settings for the local system.
+if test -f "$HOME/.xinitrc.local"; then
+    . "$HOME/.xinitrc.local"
+fi
+
+
+# Start ssh-agent if no agent is running and we have private keys, thanks to
 # http://code.haskell.org/XMonadContrib/scripts/xinitrc (read on 2011-06-19).
-if test -x /usr/bin/ssh-agent && test -f "$HOME/.ssh/id_rsa"; then
+if test -z "$SSH_AGENT_PID" \
+        && test -x /usr/bin/ssh-agent \
+        && test -f "$HOME/.ssh/id_rsa"; then
     # -s to force bourne shell output. This file is a bourne shell script even
     # if the environment might suggest otherwise.
-    eval `/usr/bin/ssh-agent -s`
+    eval `/usr/bin/ssh-agent -s` >/dev/null
 fi
 
 # Use a black background for the root window.
 xsetroot -solid black
 
 # Force English keyboard layout.
-if test -x /usr/bin/setxkbmap; then
+if installed setxkbmap; then
     setxkbmap us
 fi
 
-# Use my X11 key bindings.
-xmodmap "$HOME/.xmodmaprc"
+# Use the Caps lock as Ctrl because it's easier to reach and very useful for
+# all programs. Caps lock is removed completely. Thanks to
+# http://snarfed.org/space/switch%20control%20and%20caps%20lock
+if xmodmap | grep -E '^lock.*Caps_Lock' >/dev/null; then
+    xmodmap -e 'remove Lock = Caps_Lock'
+    xmodmap -e 'remove Control = Control_L'
+    xmodmap -e 'keysym Caps_Lock = Control_L'
+    xmodmap -e 'add Control = Control_L'
+fi
+# Move grave and tilde next to left shift key. This setting may only be
+# necessary because of my German keyboard which I use with English settings.
+xmodmap -e 'keycode 94 = grave asciitilde'
+
+# Disable annoying audio bell. Thanks to Sebastian Rachuj.
+xset b off
+
+# Load settings for programs. Xdefaults is deprecated.
+xrdb ~/.Xresources
 
 # Change the background if available.
-if test -f "$HOME/.background"; then
-    # But not on slow machines in the university.
-    HOSTNAME=`hostname`
-    if test "x`echo $HOSTNAME | grep faui0sr`" = x; then
-        if test -x /usr/bin/hsetroot; then
-            hsetroot -full "$HOME/.background"
-        elif test -x /usr/bin/feh; then
-            feh --bg-scale "$HOME/.background"
-        fi
+if test -f "$HOME/.background" && test -n "$set_background"; then
+    if installed hsetroot; then
+        hsetroot -full "$HOME/.background"
+    elif installed feh; then
+        feh --bg-scale "$HOME/.background"
+        # Remove the unnecessary ~/.fehbg file created by `feh`. I don't want
+        # to restore the background, I just run `feh` on each startx run.
+        # --no-fehbg is only available in recent feh releases.
+        rm -f "$HOME/.fehbg"
     fi
 fi
 
 # Display timed reminders and recheck the file for changes every minute (-z1).
-if test -x /usr/bin/remind && test -f "$HOME/.reminders"; then
+if installed remind && test -f "$HOME/.reminders"; then
     remind -z1 -k'.shell/bin/notify.py %s &' "$HOME/.reminders" &
+    save_background_pid
 fi
 
-# Start XMonad.
-xmonad
+# Set color temperature of display according to time of day.
+if installed redshift; then
+    redshift -l 49.9:10.9 -t 5500:4500 >/dev/null &
+fi
+
+# Automatically lock the screen after x minutes of inactivity if `xautolock`
+# is available. Warn if locker wasn't found.
+if installed xautolock; then
+    if installed "$screen_locker"; then
+        xautolock -secure -time "$screen_lock_time" -locker "$screen_locker" &
+    else
+        error "Locker '$screen_locker' not installed. Auto lock won't work!"
+    fi
+elif test -n "$screen_lock_force"; then
+    error "'xautolock' not found. Auto lock won't work!"
+fi
+
+# Use compton to get transparent windows. See ~/.Xresources for configuration
+# of urxvt.
+if installed compton; then
+    compton &
+fi
+
+
+# My window managers doesn't start a terminal emulator on its own, start one
+# when I login.
+(urxvt || rxvt || xterm) &
 
-# vim: ft=sh
+# Start window manager.
+failed=
+if installed herbstluftwm; then
+    herbstluftwm --locked >~/.config/herbstluftwm/log 2>&1 || failed=1
+elif installed xmonad; then
+    xmonad || failed=1
+elif installed stumpwm; then
+    stumpwm || failed=1
+else
+    failed='no window manager found'
+fi
+# Fallback in case my window manager failed to start. Otherwise X terminates
+# the session immediately (because xinitrc finishes).
+if test -n "$failed"; then
+    if test x"$failed" = x1; then
+        failed='failed to start window manager'
+    fi
+    error "$failed (spawning fallback terminal)"
+    urxvt || rxvt || xterm
+fi
+
+
+# Cleanup.
+#
+# Kill ssh-agent to prevent unused background processes.
+if test -x /usr/bin/ssh-agent; then
+    /usr/bin/ssh-agent -k >/dev/null
+    # No `eval` necessary, we terminate anyway.
+fi
+# Kill remaining background processes.
+kill $background_pids