]> ruderich.org/simon Gitweb - config/dotfiles.git/blob - x11/xinitrc
vim: syntax: update perl
[config/dotfiles.git] / x11 / xinitrc
1 #!/bin/sh
2
3 # startx configuration file.
4
5 # Copyright (C) 2011-2014  Simon Ruderich
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20
21 # Some systems don't set the LANG environment variable when starting xinitrc,
22 # this causes problems for rxvt-unicode-256color which fails to display UTF-8
23 # characters correctly when LANG is missing and for notify.py which also needs
24 # LANG set (for Python's decode() function). Therefore load my general shell
25 # environment which also includes $LANG.
26 if test -f "$HOME/.shell/env"; then
27     . "$HOME/.shell/env"
28 fi
29
30
31 installed() {
32     type "$1" >/dev/null 2>&1
33 }
34 error() {
35     if installed xmessage; then
36         xmessage "$@" &
37     else
38         # Log message to a file which is displayed by my Zsh setup on startup.
39         printf '%s\n' "$*" >>"$HOME/.xinitrc.errors"
40     fi
41 }
42 # Save PID of the most recently started background process. Used to terminate
43 # them when logging out to prevent unused background processes. On some
44 # systems this is only necessary for some programs (like ssh-agent), haven't
45 # yet figured out why.
46 save_background_pid() {
47     background_pids="$background_pids $!"
48 }
49 background_pids=
50
51
52 # Options, can be modified in ~/.xinitrc.local. To disable a boolean option
53 # set it to an empty value.
54 #
55 # Default to use a background image.
56 set_background=yes
57 # Default to display a warning if no screen locker is not available.
58 screen_lock_force=yes
59 # Lock screen after x minutes of inactivity.
60 screen_lock_time=3
61 # Locker program to lock the screen (with xautolock).
62 screen_locker=xtrlock
63
64 # Load settings for the local system.
65 if test -f "$HOME/.xinitrc.local"; then
66     . "$HOME/.xinitrc.local"
67 fi
68
69
70 # Start ssh-agent if no agent is running, thanks to
71 # http://code.haskell.org/XMonadContrib/scripts/xinitrc (read on 2011-06-19).
72 if test -z "$SSH_AGENT_PID" && test -x /usr/bin/ssh-agent; then
73     # -s to force bourne shell output. This file is a bourne shell script even
74     # if the environment might suggest otherwise.
75     eval `/usr/bin/ssh-agent -s` >/dev/null
76 fi
77
78 # Use a black background for the root window.
79 xsetroot -solid black
80
81 # Force English keyboard layout.
82 if installed setxkbmap; then
83     setxkbmap -option compose:menu us
84 fi
85
86 # Use the Caps lock as Ctrl because it's easier to reach and very useful for
87 # all programs. Caps lock is removed completely. Thanks to
88 # http://snarfed.org/space/switch%20control%20and%20caps%20lock
89 if xmodmap | grep -E '^lock.*Caps_Lock' >/dev/null; then
90     xmodmap -e 'remove Lock = Caps_Lock'
91     xmodmap -e 'remove Control = Control_L'
92     xmodmap -e 'keysym Caps_Lock = Control_L'
93     xmodmap -e 'add Control = Control_L'
94 fi
95 # Move grave and tilde next to left shift key. This setting may only be
96 # necessary because of my German keyboard which I use with an English layout.
97 xmodmap -e 'keycode 94 = grave asciitilde'
98
99 # Enable scrolling with trackpoint and middle mouse on Thinkpads, also disable
100 # touchpad. Thanks to DerRoteBaron.
101 if xinput 2>/dev/null | grep 'TPPS/2 IBM TrackPoint' >/dev/null; then
102     xinput set-int-prop "TPPS/2 IBM TrackPoint" "Evdev Wheel Emulation" 8 1
103     xinput set-int-prop "TPPS/2 IBM TrackPoint" "Evdev Wheel Emulation Button" 8 2
104     xinput set-int-prop "TPPS/2 IBM TrackPoint" "Evdev Wheel Emulation Axes" 8 6 7 4 5
105     xinput --disable "SynPS/2 Synaptics TouchPad"
106 fi
107
108 # Disable annoying audio bell. Thanks to Sebastian Rachuj.
109 xset b off
110
111 # Load settings for X programs. Xdefaults is deprecated.
112 xrdb "$HOME/.Xresources"
113
114 # Change the background if available.
115 if test -f "$HOME/.background" && test -n "$set_background"; then
116     if installed hsetroot; then
117         hsetroot -full "$HOME/.background"
118     elif installed feh; then
119         feh --bg-scale "$HOME/.background"
120         # Remove the unnecessary ~/.fehbg file created by `feh`. I don't want
121         # to restore the background, I just run `feh` on each startx run.
122         # --no-fehbg is only available in recent feh releases.
123         rm -f "$HOME/.fehbg"
124     fi
125 fi
126
127 # Display timed reminders and recheck the file for changes every minute (-z1).
128 if installed remind && test -f "$HOME/.reminders"; then
129     remind -z1 -k'.shell/bin/notify.py %s &' "$HOME/.reminders" &
130     save_background_pid
131 fi
132
133 # Set color temperature of display according to time of day.
134 if installed redshift; then
135     redshift -l 49.9:10.9 -t 5500:4500 >/dev/null &
136 fi
137
138
139 # Start best available screen locker.
140 xlockscreen=`"$HOME/.xlockscreen" start "$screen_lock_time" "$screen_locker"`
141 if test $? -ne 0 && test -n "$screen_lock_force"; then
142     error "$xlockscreen"
143 fi
144
145
146 # Use compton to get transparent windows. See ~/.Xresources for configuration
147 # of urxvt.
148 if installed compton; then
149     compton &
150 # xcompmgr as fallback.
151 elif installed xcompmgr; then
152     xcompmgr &
153 fi
154
155
156 # My window manager doesn't start a terminal emulator on its own, start one
157 # when I login.
158 (urxvt || rxvt || xterm) &
159
160 # Start window manager.
161 failed=
162 if installed herbstluftwm; then
163     herbstluftwm --locked || failed=1
164 elif installed xmonad; then
165     xmonad || failed=1
166 elif installed stumpwm; then
167     stumpwm || failed=1
168 else
169     failed='no window manager found'
170 fi
171 # Fallback in case my window manager failed to start. Otherwise X terminates
172 # the session immediately (because xinitrc finishes).
173 if test -n "$failed"; then
174     if test x"$failed" = x1; then
175         failed='failed to start window manager'
176     fi
177     error "$failed (spawning fallback terminal)"
178     urxvt || rxvt || xterm
179 fi
180
181
182 # Cleanup.
183 #
184 # Kill ssh-agent to prevent unused background processes.
185 if test -x /usr/bin/ssh-agent; then
186     /usr/bin/ssh-agent -k >/dev/null
187     # No `eval` necessary, we terminate anyway.
188 fi
189 # Kill remaining background processes.
190 if test -n "$background_pids"; then
191     kill $background_pids
192 fi