Merge remote-tracking branch 'crybaby'
[profile] / dot / 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 ### Preliminary hook.
75
76 if [ -r $HOME/.xinitrc-prehook ]; then
77 . $HOME/.xinitrc-prehook
78 fi
79
80 ###--------------------------------------------------------------------------
81 ### Iniitial settings.
82
83 ## Assume X sessions are secure.
84 export __mdw_sechost="`hostname`"
85
86 ## Obtain the screen dimensions.
87 case ",$XWIDTH,$XHEIGHT," in
88 *,,*) eval $(xscsize -bx) ;;
89 esac
90 info "screen size = $XWIDTH x $XHEIGHT"
91
92 initialize () {
93 ## Load the X resource database.
94 run init xrdb -override $HOME/.Xdefaults
95
96 ## Random xsettery.
97 run init xset b 5 2000 50
98 run init xset r rate 500 50
99 run init xset m 2 1
100
101 ## Key mappings.
102 xmodmap -e 'keysym BackSpace = BackSpace BackSpace'
103 }
104
105 ###--------------------------------------------------------------------------
106 ### Start a window manager.
107
108 wm=$(pick_program window-manager e16 compiz enlightenment e17 twm)
109 wmopts=""
110 case "$wm,$vnc" in
111 enlightenment,yes | e16,yes)
112 wmopts="$eopts -econfdir $HOME/.enlightenment-vnc"
113 ;;
114 esac
115
116 start-e16 () {
117 run bginit $wm $wmopts
118 win=nil
119 for i in $(seq 10); do
120 sleep 1
121 if eesh version >/dev/null 2>&1; then
122 win=t
123 break
124 fi
125 done
126 case $win in
127 t)
128 info "$wm started ok"
129 run init xsetroot -cursor_name left_ptr
130 ;;
131 nil)
132 info "$wm failed to start!"
133 ;;
134 esac
135 }
136
137 start-window-manager () {
138 case $(type -t start-$wm || echo "not-found") in
139 function)
140 start-$wm $wmopts
141 ;;
142 *)
143 run bginit $wm $wmopts
144 ;;
145 esac
146 }
147
148 ###--------------------------------------------------------------------------
149 ### Random useful clients.
150
151 start-clients-local () { :; }
152
153 start-clients () {
154
155 ## Gnome session.
156 case "$vnc,$(gnome-session --version 2>&1)" in
157 no,gnome-session\ 2.3[2-9].* | \
158 no,gnome-session\ 2.4[0-9].* | \
159 no,gnome-session\ 2.[1-9][0-9][0-9]*)
160 run bginit gnome-session --session mdw
161 ;;
162 no,*)
163 run bginit gnome-session
164 ;;
165 esac
166
167 ## Local clients.
168 start-clients-local
169 }
170
171 ###--------------------------------------------------------------------------
172 ### Main screen layout.
173
174 ## Choose appropriate clients.
175 emacs=$(pick_program emacs emacs23 emacs22 emacs21 emacs)
176 term=$(pick_program terminal pterm Eterm xterm)
177
178 ## Emacs window measurements.
179 case "$emacs" in
180 emacs21 | emacs)
181 e_colwd=492 e_hextra=34
182 e_colchars=82 e_cextra=-2
183 e_lineht=13 e_vextra=52
184 ;;
185 emacs22 | emacs23)
186 e_colwd=492 e_hextra=8
187 e_colchars=82 e_cextra=-6
188 e_lineht=13 e_vextra=46
189 ;;
190 esac
191
192 ## Terminal window measurements.
193 case "$term" in
194 pterm) t_wd=504 t_lineht=13 t_vextra=23 geom=-geometry;;
195 Eterm) t_wd=504 t_lineht=13 t_vextra=23 geom=-g;;
196 xterm) t_wd=507 t_lineht=13 t_vextra=27 geom=-geometry;;
197 esac
198
199 ## GNOME stuff measurements.
200 declare -i xbound="XWIDTH - 113"
201
202 ## Choose a width for Emacs.
203 ##
204 ## We'd like it to be as wide as possible, allowing for a column of xterms
205 ## down the right hand side. However, I'd prefer a double-width Emacs to a
206 ## single-width Emacs and xterms. If it's not going to work at all, a single
207 ## Emacs column will have to do. Also, there's a strange thing with Emacs21
208 ## and the toolbar, so we add on some rows which are later mysteriously
209 ## subtracted.
210
211 declare -i ecols="(xbound - t_wd - e_hextra)/e_colwd"
212 if (( ecols < 2 && xbound > e_colwd * 2 + e_hextra )); then
213 ecols=2
214 elif (( ecols < 1 )); then
215 ecols=1
216 fi
217
218 declare -i \
219 emacsx="ecols * e_colchars + e_cextra" \
220 emacsy="(XHEIGHT - e_vextra)/e_lineht"
221
222 start-emacs () {
223 GDK_NATIVE_WINDOWS=1 run bgclients noip \
224 $emacs -geometry ${emacsx}x${emacsy}+0+0
225 }
226
227 ## Now place some xterms.
228 ##
229 ## A few smaller xterms are in general better than one great big one. 35
230 ## lines is a good height for most terminals. 25 lines is a minimum. The
231 ## strategy for doling out xterms into a column is to make as many 35-liners
232 ## as we can, until the remaining space would be too small for a 25-liner.
233 ## If we can get two 25s out of that then we do (largest first); otherwise
234 ## just make one big one. We stop at the end of a page, once we've made
235 ## three xterms.
236
237 start-xterms () {
238
239 ## Initialize some parameters.
240 declare -i x="ecols * e_colwd + e_hextra" xb=xbound
241 declare -i n=0 pgx=0 l h y ht
242 declare -i hstd="35 * t_lineht + t_vextra" hmin="25 * t_lineht + t_vextra"
243
244 ## Do the placement.
245 while :; do
246
247 ## Start a new iteration.
248 if ((x + t_wd > xb)); then
249 if ((n >= 3)); then break; fi
250 x="pgx + XWIDTH" pgx="pgx + XWIDTH" xb="xb + XWIDTH"
251 fi
252
253 ## Make large xterms.
254 y=0 ht=XHEIGHT
255 while ((ht - hstd >= hmin)); do
256 run bgclients $term $geom 80x35+$x+$y
257 y="y + hstd" ht="ht - hstd" n="n + 1"
258 done
259
260 ## Fill the remaining space.
261 if ((ht >= 2 * hmin)); then h="ht - hmin"; else h=ht; fi
262 l="(h - t_vextra)/t_lineht" h="l * t_lineht + t_vextra"
263 run bgclients $term $geom 80x$l+$x+$y
264 y="y + h" ht="ht - h" n="n + 1"
265 if ((ht >= hmin)); then
266 run bgclients $term $geom 80x25+$x+$y
267 n="n + 1"
268 fi
269 x="x + t_wd"
270 done
271 }
272
273 ###--------------------------------------------------------------------------
274 ### Requesters.
275
276 req () {
277 declare title=$1 hist=$2; shift 2
278 cmd=$(xgetline -t "$title" -p "_Command:" -Hl "$HOME/$hist") &&
279 exec "$@" "$cmd"
280 }
281
282 ###--------------------------------------------------------------------------
283 ### Final waiting.
284
285 atom=XINIT_COMMAND$atomtag
286
287 xwait () {
288 while :; do
289 xatom delete $atom
290 info "waiting on $atom"
291 line=$(xatom wait $atom)
292 info "xatom: $line"
293
294 case "$line" in
295 :help)
296 xmsg -I -t "xinitrc help" -d "xinitrc commands" - <<EOF &
297 :help
298 :emacs :xterms :window-manager :clients
299 :ask-run :ask-command
300 :init
301 :terminal
302 ! SHELL-COMMAND
303 CLIENT
304 EOF
305 ;;
306 :emacs | :xterms | :window-manager | :clients)
307 start-${line#:}
308 ;;
309 :terminal)
310 run bgclients $term
311 ;;
312 :init)
313 initialize
314 ;;
315 :exec)
316 info "restarting xinitrc"
317 exec "$0" wait nostart
318 ;;
319 :ask-run)
320 req "Shell command" .cmd.hist xcatch -FMiscFixed6x13 -- sh -c&
321 ;;
322 :ask-command)
323 req "xinit command" .xinit.hist xatom set XINIT_COMMAND$atomtag&
324 ;;
325 :*)
326 xmsg -E -t "xinitrc error" "Unknown command \`$line'" &
327 ;;
328 !*)
329 eval "${line#!}"
330 ;;
331 *)
332 set -- $line
333 run bgclients "$@"
334 ;;
335 esac
336 done
337 }
338
339 ###--------------------------------------------------------------------------
340 ### Gnome session care and feeding.
341
342 session-running-p () {
343 dbus-send --session --print-reply --dest=org.freedesktop.DBus / \
344 org.freedesktop.DBus.GetNameOwner string:org.gnome.SessionManager \
345 >/dev/null 2>&1
346 }
347
348 kill-gnome-session () {
349 if session-running-p; then
350 info "killing Gnome session manager"
351 dbus-send --session \
352 --dest=org.gnome.SessionManager /org/gnome/SessionManager \
353 org.gnome.SessionManager.Logout uint32:2
354 for i in 1 2 3 4 5; do
355 sleep 1
356 if ! session-running-p; then break; fi
357 done
358 fi
359 }
360
361 ###--------------------------------------------------------------------------
362 ### Actually start things up.
363
364 if [ -f $HOME/.xinitrc-local ]; then
365 . $HOME/.xinitrc-local
366 fi
367
368 case "$start" in
369 yes)
370 info "starting standard clients"
371 initialize
372 start-window-manager
373 start-clients
374 start-emacs
375 start-xterms
376 ;;
377 no)
378 info "not starting standard clients"
379 ;;
380 esac
381
382 case "$wait" in
383 yes)
384 xwait
385 kill-gnome-session
386 ;;
387 no)
388 info "not waiting before exit"
389 ;;
390 esac
391
392 ###----- That's all, folks --------------------------------------------------