]> ruderich.org/simon Gitweb - config/dotfiles.git/blob - xinitrc
xinitrc: Display a warning if xautolock is not available.
[config/dotfiles.git] / xinitrc
1 #!/bin/sh
2
3 # startx configuration file.
4
5 # Copyright (C) 2011-2013  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     # Debug function used in ~/.shell/env.
28     source_debug() {
29         # Do nothing.
30         :
31     }
32
33     . "$HOME/.shell/env"
34 fi
35
36
37 installed() {
38     type "$1" >/dev/null 2>&1
39 }
40 error() {
41     if installed xmessage; then
42         xmessage "$@" &
43     else
44         # Log message to a file which is displayed by my Zsh setup on startup.
45         echo "$@" >> "$HOME/.xinitrc.errors"
46     fi
47 }
48 # Save PID of the most recently started background process. Used to terminate
49 # them when logging out to prevent unused background processes. On some
50 # systems this is only necessary for some programs (like ssh-agent), haven't
51 # yet figured out why.
52 save_background_pid() {
53     background_pids="$background_pids $!"
54 }
55 background_pids=
56
57
58 # Options, can be modified in ~/.xinitrc.local. To disable a boolean option
59 # set it to an empty value.
60 #
61 # Default to use a background image.
62 set_background=yes
63 # Default to display a warning if `xautolock` is not available.
64 screen_lock_force=yes
65 # Lock screen after x minutes of inactivity, requires `xautolock` to be
66 # installed.
67 screen_lock_time=3
68 # Locker program to lock the screen, used below with `xautolock`.
69 screen_locker=xtrlock
70
71 # Load settings for the local system.
72 if test -f "$HOME/.xinitrc.local"; then
73     . "$HOME/.xinitrc.local"
74 fi
75
76
77 # Start ssh-agent if we have private keys, thanks to
78 # http://code.haskell.org/XMonadContrib/scripts/xinitrc (read on 2011-06-19).
79 if test -x /usr/bin/ssh-agent && test -f "$HOME/.ssh/id_rsa"; then
80     # -s to force bourne shell output. This file is a bourne shell script even
81     # if the environment might suggest otherwise.
82     eval `/usr/bin/ssh-agent -s` >/dev/null
83 fi
84
85 # Use a black background for the root window.
86 xsetroot -solid black
87
88 # Force English keyboard layout.
89 if installed setxkbmap; then
90     setxkbmap us
91 fi
92
93 # Use my X11 key bindings.
94 xmodmap "$HOME/.xmodmaprc"
95
96 # Change the background if available.
97 if test -f "$HOME/.background" && test -n "$set_background"; then
98     if installed hsetroot; then
99         hsetroot -full "$HOME/.background"
100     elif installed feh; then
101         feh --bg-scale "$HOME/.background"
102         # Remove the unnecessary ~/.fehbg file created by `feh`. I don't want
103         # to restore the background, I just run `feh` on each startx run.
104         # --no-fehbg is only available in recent feh releases.
105         rm -f "$HOME/.fehbg"
106     fi
107 fi
108
109 # Display timed reminders and recheck the file for changes every minute (-z1).
110 if installed remind && test -f "$HOME/.reminders"; then
111     remind -z1 -k'.shell/bin/notify.py %s &' "$HOME/.reminders" &
112     save_background_pid
113 fi
114
115 # Set color temperature of display according to time of day.
116 if installed redshift; then
117     redshift -l 49.9:10.9 -t 5500:4500 >/dev/null &
118 fi
119
120 # Automatically lock the screen after x minutes of inactivity if `xautolock`
121 # is available. Warn if locker wasn't found.
122 if installed xautolock; then
123     if installed "$screen_locker"; then
124         xautolock -secure -time "$screen_lock_time" -locker "$screen_locker" &
125     else
126         error "Locker '$screen_locker' not installed. Auto lock won't work!"
127     fi
128 elif test -n "$screen_lock_force"; then
129     error "'xautolock' not found. Auto lock won't work!"
130 fi
131
132
133 # XMonad doesn't start a terminal emulator on its own, start one when I login.
134 (urxvt || rxvt || xterm) &
135
136 # Start window manager.
137 xmonad
138
139 # Cleanup.
140 #
141 # Kill ssh-agent to prevent unused background processes.
142 if test -x /usr/bin/ssh-agent; then
143     /usr/bin/ssh-agent -k >/dev/null
144     # No `eval` necessary, we terminate anyway.
145 fi
146 # Kill remaining background processes.
147 kill $background_pids