xinitrc: Read a local script.
[profile] / xinitrc
1 #! /bin/bash
2 ###
3 ### X startup script
4
5 ###--------------------------------------------------------------------------
6 ### Utility functions.
7
8 ## Progress indicators.
9 info=yes
10 info () {
11 case $info in yes) echo "- $*" >&2 ;; esac
12 }
13
14 run=yes
15 run () {
16 local what=$1; shift
17 local bg=no
18
19 case $what in bg*) bg=yes what=${what#bg} ;; esac
20 info "run $what: $*"
21
22 case "$run,$bg" in
23 yes,no) "$@" ;;
24 yes,yes) "$@" & ;;
25 esac
26 }
27
28 ## Program choice
29 pick_program () {
30 local what=$1; shift
31 local choice=false
32 for i in "$@"; do
33 if type -t >/dev/null "$i"; then choice=$i; break; fi
34 done
35 info "pick $what = $choice"
36 echo "$choice"
37 }
38
39 ###--------------------------------------------------------------------------
40 ### Parse arguments.
41
42 vnc=no
43 atomtag=
44 start=yes
45 wait=yes
46
47 for opt; do
48 case "$opt" in
49 help)
50 cat <<EOF
51 Options:
52 tag=TAG
53 [no]trace
54 [no]info
55 [no]run
56 [no]start
57 [no]wait
58 [no]vnc
59 EOF
60 exit
61 ;;
62
63 tag=*) atomtag=/${opt#tag=} ;;
64 trace) set -x ;;
65 notrace) set +x ;;
66 info | run | start | wait | vnc) eval "$opt=yes" ;;
67 noinfo | norun | nostart | nowait | novnc) eval "${opt#no}=no" ;;
68
69 *) echo "unknown option $opt" >&2; exit 1 ;;
70 esac
71 done
72
73 ###--------------------------------------------------------------------------
74 ### Iniitial settings.
75
76 ## Assume X sessions are secure.
77 export __mdw_sechost="`hostname`"
78
79 ## Obtain the screen dimensions.
80 case ",$XWIDTH,$XHEIGHT," in
81 *,,*) eval $(xscsize -bx) ;;
82 esac
83 info "screen size = $XWIDTH x $XHEIGHT"
84
85 initialize () {
86 ## Load the X resource database.
87 run init xrdb -override $HOME/.Xdefaults
88
89 ## Random xsettery.
90 run init xset b 10 2000 50
91 run init xset r rate 500 50
92 run init xset m 2 1
93
94 ## Key mappings.
95 xmodmap -e 'keysym BackSpace = BackSpace BackSpace'
96
97 ## Gnome settings.
98 case $vnc in no) run bginit gnome-settings-daemon ;; esac
99 }
100
101 ###--------------------------------------------------------------------------
102 ### Start a window manager.
103
104 wm=$(pick_program window-manager enlightenment e16 twm)
105 wmopts=""
106 case "$wm,$vnc" in
107 enlightenment,yes | e16,yes)
108 wmopts="$eopts -econfdir $HOME/.enlightenment-vnc"
109 ;;
110 esac
111
112 start-window-manager () {
113 run bginit $wm $wmopts
114 }
115
116 ###--------------------------------------------------------------------------
117 ### Random useful clients.
118
119 start-clients-local () { :; }
120
121 start-clients () {
122 ## Mail notification.
123 run bginit mail-notification
124
125 ## System monitor.
126 case $vnc in no) run bginit gkrellm ;; esac
127
128 ## Screensaver.
129 case $vnc in
130 no)
131 run init xscreensaver-command -exit
132 run bginit xscreensaver -no-splash
133 ;;
134 esac
135
136 ## Panel.
137 case $vnc in no) run bginit gnome-panel ;; esac
138
139 ## Local clients.
140 start-clients-local
141 }
142
143 ###--------------------------------------------------------------------------
144 ### Main screen layout.
145
146 ## Choose appropriate clients.
147 emacs=$(pick_program emacs emacs22 emacs21 emacs)
148 term=$(pick_program terminal pterm Eterm xterm)
149
150 ## Emacs window measurements.
151 case "$emacs" in
152 emacs21 | emacs)
153 e_colwd=492 e_hextra=34
154 e_colchars=82 e_cextra=-2
155 e_lineht=13 e_vextra=52
156 ;;
157 emacs22)
158 e_colwd=492 e_hextra=8
159 e_colchars=82 e_cextra=-6
160 e_lineht=13 e_vextra=46
161 ;;
162 esac
163
164 ## Terminal window measurements.
165 case "$term" in
166 pterm) t_wd=503 t_lineht=13 t_vextra=23 geom=-geometry;;
167 Eterm) t_wd=504 t_lineht=13 t_vextra=23 geom=-g;;
168 xterm) t_wd=507 t_lineht=13 t_vextra=27 geom=-geometry;;
169 esac
170
171 ## GNOME stuff measurements.
172 declare -i xbound="XWIDTH - 113"
173
174 ## Choose a width for Emacs.
175 ##
176 ## We'd like it to be as wide as possible, allowing for a column of xterms
177 ## down the right hand side. However, I'd prefer a double-width Emacs to a
178 ## single-width Emacs and xterms. If it's not going to work at all, a single
179 ## Emacs column will have to do. Also, there's a strange thing with Emacs21
180 ## and the toolbar, so we add on some rows which are later mysteriously
181 ## subtracted.
182
183 declare -i ecols="(xbound - t_wd - e_hextra)/e_colwd"
184 if (( ecols < 2 && xbound > e_colwd * 2 + e_hextra )); then
185 ecols=2
186 elif (( ecols < 1 )); then
187 ecols=1
188 fi
189
190 declare -i \
191 emacsx="ecols * e_colchars + e_cextra" \
192 emacsy="(XHEIGHT - e_vextra)/e_lineht"
193
194 start-emacs () {
195 run bgclients noip $emacs -geometry ${emacsx}x${emacsy}+0+0
196 }
197
198 ## Now place some xterms.
199 ##
200 ## A few smaller xterms are in general better than one great big one. 35
201 ## lines is a good height for most terminals. 25 lines is a minimum. The
202 ## strategy for doling out xterms into a column is to make as many 35-liners
203 ## as we can, until the remaining space would be too small for a 25-liner.
204 ## If we can get two 25s out of that then we do (largest first); otherwise
205 ## just make one big one. We stop at the end of a page, once we've made
206 ## three xterms.
207
208 start-xterms () {
209
210 ## Initialize some parameters.
211 declare -i x="ecols * e_colwd + e_hextra"
212 declare -i n=0 pgx=0 l h y ht
213 declare -i hstd="35 * t_lineht + t_vextra" hmin="25 * t_lineht + t_vextra"
214
215 ## Do the placement.
216 while :; do
217
218 ## Start a new iteration.
219 if ((x + t_wd > xbound)); then
220 if ((n >= 3)); then break; fi
221 x="pgx + XWIDTH" pgx="pgx + XWIDTH" xbound="xbound + XWIDTH"
222 fi
223
224 ## Make large xterms.
225 y=0 ht=XHEIGHT
226 while ((ht - hstd >= hmin)); do
227 run bgclients $term $geom 80x35+$x+$y
228 y="y + hstd" ht="ht - hstd" n="n + 1"
229 done
230
231 ## Fill the remaining space.
232 if ((ht >= 2 * hmin)); then h="ht - hmin"; else h=ht; fi
233 l="(h - t_vextra)/t_lineht" h="l * t_lineht + t_vextra"
234 run bgclients $term $geom 80x$l+$x+$y
235 y="y + h" ht="ht - h" n="n + 1"
236 if ((ht >= hmin)); then
237 run bgclients $term $geom 80x25+$x+$y
238 n="n + 1"
239 fi
240 x="x + t_wd"
241 done
242 }
243
244 ###--------------------------------------------------------------------------
245 ### Final waiting.
246
247 atom=XINIT_COMMAND$atomtag
248
249 xwait () {
250 while :; do
251 xatom delete $atom
252 info "waiting on $atom"
253 line=$(xatom wait $atom)
254 info "xatom: $line"
255
256 case "$line" in
257 :help)
258 xmsg -I -t "xinitrc help" -d "xinitrc commands" - <<EOF &
259 :help
260 :emacs :xterms :window-manager :clients
261 :init
262 :terminal
263 ! SHELL-COMMAND
264 CLIENT
265 EOF
266 ;;
267 :emacs | :xterms | :window-manager | :clients)
268 start-${line#:}
269 ;;
270 :terminal)
271 run bgclients $term
272 ;;
273 :init)
274 initialize
275 ;;
276 :exec)
277 info "restarting xinitrc"
278 exec "$0" wait nostart
279 ;;
280 :*)
281 xmsg -E -t "xinitrc error" "Unknown command \`$line'" &
282 ;;
283 !*)
284 eval "${line#!}"
285 ;;
286 *)
287 set -- $line
288 run bgclients "$@"
289 ;;
290 esac
291 done
292 }
293
294 ###--------------------------------------------------------------------------
295 ### Actually start things up.
296
297 if [ -f $HOME/.xinitrc-local ]; then
298 . $HOME/.xinitrc-local
299 fi
300
301 case "$start" in
302 yes)
303 info "starting standard clients"
304 initialize
305 start-window-manager
306 start-clients
307 start-emacs
308 start-xterms
309 ;;
310 no)
311 info "not starting standard clients"
312 ;;
313 esac
314
315 case "$wait" in
316 yes)
317 xwait
318 ;;
319 no)
320 info "not waiting before exit"
321 ;;
322 esac
323
324 ###----- That's all, folks --------------------------------------------------