dot/xinitrc: Accept an `emacs-lucid' if we find one.
[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 manage () {
29 local when=$(date +%s) now
30 local fail=0 rc report
31
32 while :; do
33 "$@"; rc=$?
34 case $rc in
35 0) info "manage $1: successful exit"; break ;;
36 143) info "manage $1: terminated"; break ;;
37 esac
38 now=$(date +%s)
39 report="rc = $rc"
40 if (( $now - $when > 5 )); then
41 fail=0
42 else
43 report="$report, early failure"
44 fail=$(( $fail + 1 ))
45 if (( $fail >= 5 )); then
46 info "manage $1: exit ($report), giving up after $fail failures"
47 break
48 fi
49 fi
50 info "manage $1: exit ($report), restarting"
51 when=$now
52 done
53 }
54
55 ## Program choice
56 pick_program () {
57 local what=$1; shift
58 local choice=false
59 for i in "$@"; do
60 if type -t >/dev/null "$i"; then choice=$i; break; fi
61 done
62 info "pick $what = $choice"
63 echo "$choice"
64 }
65
66 ###--------------------------------------------------------------------------
67 ### Parse arguments.
68
69 vnc=no
70 atomtag=
71 start=yes
72 wait=yes
73
74 for opt; do
75 case "$opt" in
76 help)
77 cat <<EOF
78 Options:
79 tag=TAG
80 [no]trace
81 [no]info
82 [no]run
83 [no]start
84 [no]wait
85 [no]vnc
86 EOF
87 exit
88 ;;
89
90 tag=*) atomtag=/${opt#tag=} ;;
91 trace) set -x ;;
92 notrace) set +x ;;
93 info | run | start | wait | vnc) eval "$opt=yes" ;;
94 noinfo | norun | nostart | nowait | novnc) eval "${opt#no}=no" ;;
95
96 *) echo "unknown option $opt" >&2; exit 1 ;;
97 esac
98 done
99
100 ###--------------------------------------------------------------------------
101 ### Preliminary hook.
102
103 if [ -r $HOME/.xinitrc-prehook ]; then
104 . $HOME/.xinitrc-prehook
105 fi
106
107 ###--------------------------------------------------------------------------
108 ### Iniitial settings.
109
110 ## Assume X sessions are secure.
111 export __mdw_sechost="$(hostname)"
112
113 ## Tell things that XFCE is in charge. This is the most desktoppy thing that
114 ## I run, so it's not entirely wrong.
115 : ${XDG_CURRENT_DESKTOP=XFCE}; export XDG_CURRENT_DESKTOP
116
117 ## Obtain the screen dimensions.
118 case ",$XWIDTH,$XHEIGHT," in
119 *,,*) eval $(xscsize -bx; xscsize -bmx) ;;
120 esac
121 case ",$XNSCR," in
122 ,,)
123 XNSCR=1 XSCR0_X=0 XSCR0_Y=0 XSCR0_WIDTH=$XWIDTH XSCR0_HEIGHT=$XHEIGHT
124 ;;
125 esac
126 i=0; while (( i < XNSCR )); do
127 eval "x=\$XSCR${i}_X y=\$XSCR${i}_Y
128 wd=\$XSCR${i}_WIDTH ht=\$XSCR${i}_HEIGHT"
129 info "screen #$i = ${wd}x${ht}+${x}+${y}"
130 i=$(( i + 1 ))
131 done
132
133 initialize () {
134 ## Load the X resource database.
135 run init xrdb -override $HOME/.Xdefaults
136
137 ## Random xsettery.
138 run init xset b 5 2000 50
139 run init xset r rate 500 50
140 run init xset m 2 1
141
142 ## Key mappings.
143 xmodmap $HOME/.xmodmap
144 if [ -r $HOME/.xmodmap-local ]; then
145 xmodmap $HOME/.xmodmap-local
146 fi
147 }
148
149 ###--------------------------------------------------------------------------
150 ### Start a window manager.
151
152 wm=$(pick_program window-manager e16 compiz enlightenment e17 twm)
153 wmopts=""
154 case "$wm,$vnc" in
155 enlightenment,yes | e16,yes)
156 wmopts="$eopts -econfdir $HOME/.enlightenment-vnc"
157 ;;
158 esac
159
160 start-e16 () {
161 run bginit manage $wm $wmopts
162 win=nil
163 for i in $(seq 10); do
164 sleep 1
165 if eesh version >/dev/null 2>&1; then
166 win=t
167 break
168 fi
169 done
170 case $win in
171 t)
172 info "$wm started ok"
173 run init xsetroot -cursor_name left_ptr
174 ;;
175 nil)
176 info "$wm failed to start!"
177 ;;
178 esac
179 }
180
181 start-window-manager () {
182 case $(type -t start-$wm || echo "not-found") in
183 function)
184 start-$wm $wmopts
185 ;;
186 *)
187 run bginit manage $wm $wmopts
188 ;;
189 esac
190 }
191
192 ###--------------------------------------------------------------------------
193 ### Random useful clients.
194
195 start-clients-local () { :; }
196
197 start-clients () {
198
199 ## Gnome session.
200 case "$vnc,$(xfce4-session --version 2>&1),$(gnome-session --version 2>&1)"
201 in
202 no,xfce4-session*)
203 run bginit xfce4-session
204 ;;
205 no,*,gnome-session\ 2.3[2-9].* | \
206 no,*,gnome-session\ 2.4[0-9].* | \
207 no,*,gnome-session\ 2.[1-9][0-9][0-9]*)
208 run bginit gnome-session --session mdw
209 ;;
210 no,*,gnome-session*)
211 run bginit gnome-session
212 ;;
213 esac
214
215 ## Local clients.
216 start-clients-local
217 }
218
219 ###--------------------------------------------------------------------------
220 ### Main screen layout.
221
222 ## Choose appropriate clients.
223 emacs=$(pick_program emacs \
224 emacs24-lucid emacs23-lucid emacs24 emacs23 emacs22 emacs21 \
225 emacs-lucid emacs)
226 term=$(pick_program terminal pterm Eterm xterm)
227
228 ## Emacs window measurements.
229 ##
230 ## e_colsz = width of a column in characters (from `emacs-width' metaconfig)
231 ## e_charwd = width of a character in pixels (assume `6x13')
232 ## e_colextra = additional per-column overhead in pixels
233 ## e_colextrachars = additional per-column overhead in character units
234 ## e_colwd = basic width of a column in pixels
235 ## e_hextra = extra horizontal width in pixels
236 ## Width of an N-column Emacs frame in pixels will be
237 ## N*e_colwd + e_hextra
238 ## e_colchars = width of a column in Emacs `-geometry' units
239 ## e_cextra = extra horizontal width in Emacs `-geometry' units
240 ## So an N-column frame should be reported to Emacs as being
241 ## N*e_colchars + e_cextra geometry units wide
242 ## e_lineht = height of a character line in pixels
243 ## e_vextra = number of additional vertical cruft pixels
244 ## So an N-line Emacs frame takes N*e_lineht + e_vextra pixels
245 e_colsz=$(mdw-conf emacs-width 77) e_charwd=6
246 e_colextra=30 e_colextrachars=5 e_lineht=13
247 case "$emacs" in
248 emacs21 | emacs) e_hextra=34 e_cextra=-2 e_vextra=52 ;;
249 emacs22 | emacs23) e_hextra=8 e_cextra=-6 e_vextra=46 ;;
250 emacs24) e_hextra=5 e_cextra=-6 e_vextra=42 ;;
251 emacs23-lucid) e_hextra=7 e_cextra=-6 e_vextra=48 ;;
252 emacs24-lucid) e_hextra=7 e_cextra=-5 e_vextra=48 ;;
253 esac
254 e_colwd=$(( e_colsz*e_charwd + e_colextra ))
255 e_colchars=$(( e_colsz + e_colextrachars ))
256
257 ## Terminal window measurements.
258 ##
259 ## t_wd = the window width, in pixels
260 ## t_lineht, t_vextra = height parameters: if the window is N lines high,
261 ## then it will be N*t_lineht + t_vextra pixels high
262 case "$term" in
263 pterm)
264 ## The pterm width differs according to whether it's linked against Gtk 2
265 ## or 3. Let's find out...
266 case $(ldd $(command -v pterm) | grep libgtk) in
267 libgtk-2*) t_wd=504 ;;
268 *) t_wd=500 ;;
269 esac
270 t_lineht=13 t_vextra=23 geom=-geometry;;
271 Eterm) t_wd=504 t_lineht=13 t_vextra=23 geom=-g;;
272 xterm) t_wd=507 t_lineht=13 t_vextra=27 geom=-geometry;;
273 esac
274
275 ## GNOME stuff measurements.
276 declare -i panelwd=64 xbound=$(( XWIDTH - panelwd ))
277
278 ## Choose a width for Emacs.
279 ##
280 ## We're going to make a single frame of Emacs, which will be automatically
281 ## divvied into columns; the rest of the space will be tiled with xterms. If
282 ## we have multiple screens, then Emacs can have the first screen and we'll
283 ## fill the rest with terminals. Otherwise, we'll try to have N columns of
284 ## Emacs, and N - 1 columns of xterms; if that doesn't work, we'll make do
285 ## with N - 2 columns of xterms.
286 ##
287 ## So, let W be the available width; let E and T be the widths of an Emacs
288 ## column (e_colwd) and terminal (t_wd), respectively, and let E0 be the
289 ## additive width of Emacs's existence (e_hextra). If we have more than one
290 ## screen, then instead, let T be zero. To start out, then, we let N = 1 +
291 ## floor((W - E - E0)/(E + T)). If W - E0 - (N + 1) E - (N - 1)*T >= 0, then
292 ## we increase N by one.
293 declare -i lim=XSCR0_WIDTH
294 if (( lim > xbound )); then lim=xbound; fi
295 declare -i twd=t_wd; if (( XNSCR > 1 )); then twd=0; fi
296 declare -i ecols=$(( (lim - e_colwd - e_hextra)/(e_colwd + twd) + 1 ))
297 if (( lim - e_hextra - (ecols + 1)*e_colwd - (ecols - 1)*twd >= 0 )); then
298 ecols=$(( ecols + 1 ))
299 fi
300
301 declare -i \
302 emacsx=$(( ecols*e_colchars + e_cextra )) \
303 emacsy=$(( (XHEIGHT - e_vextra)/e_lineht ))
304
305 start-emacs () {
306 GDK_NATIVE_WINDOWS=1 run bgclients noip \
307 $emacs -bg black -geometry ${emacsx}x${emacsy}+${XSCR0_X}+${XSCR0_Y} \
308 --mdw-splashy-startup
309 }
310
311 ## Now place some xterms.
312 ##
313 ## A few smaller xterms are in general better than one great big one. 35
314 ## lines is a good height for most terminals. 25 lines is a minimum. The
315 ## strategy for doling out xterms into a column is to make as many 35-liners
316 ## as we can, until the remaining space would be too small for a 25-liner.
317 ## If we can get two 25s out of that then we do (largest first); otherwise
318 ## just make one big one. We stop at the end of a page, once we've made
319 ## three xterms.
320
321 start-xterms () {
322
323 ## Initialize some parameters.
324 declare -i x=$(( ecols*e_colwd + e_hextra + XSCR0_X )) xb=xbound
325 declare -i n=0 pgx=0 l h y ht scr=0 ll=lim
326 declare -i hstd=$(( 35*t_lineht + t_vextra ))
327 declare -i hmin=$(( 25*t_lineht + t_vextra ))
328 declare -i scrx scry scrwd scrht
329
330 eval "scrx=\$XSCR${scr}_X scry=\$XSCR${scr}_Y
331 scrwd=\$XSCR${scr}_WIDTH scrht=\$XSCR${scr}_HEIGHT"
332
333 ## Do the placement.
334 while :; do
335
336 ## Start a new iteration.
337 if (( x + t_wd > ll )); then
338 scr=$(( scr + 1 ))
339 if (( scr >= XNSCR )); then
340 if (( n >= 3 )); then break; fi
341 pgx=$(( pgx + XWIDTH )) xb=$(( xb + XWIDTH ))
342 scr=0
343 fi
344 eval "scrx=\$XSCR${scr}_X scry=\$XSCR${scr}_Y
345 scrwd=\$XSCR${scr}_WIDTH scrht=\$XSCR${scr}_HEIGHT"
346 x=$(( pgx + scrx ))
347 ll=$(( x + scrwd ))
348 if (( ll > xb )); then ll=xb; fi
349 fi
350
351 ## Make large xterms.
352 y=scry ht=scrht
353 while (( ht - hstd >= hmin )); do
354 run bgclients $term $geom 80x35+$x+$y
355 y=$(( y + hstd )) ht=$(( ht - hstd )) n=$(( n + 1 ))
356 done
357
358 ## Fill the remaining space.
359 if (( ht >= 2*hmin )); then h=$(( ht - hmin )); else h=ht; fi
360 l=$(( (h - t_vextra)/t_lineht )) h=$(( l*t_lineht + t_vextra ))
361 run bgclients $term $geom 80x$l+$x+$y
362 y=$(( y + h )) ht=$(( ht - h )) n=$(( n + 1 ))
363 if ((ht >= hmin)); then
364 run bgclients $term $geom 80x25+$x+$y
365 n=$(( n + 1 ))
366 fi
367 x=$(( x + t_wd ))
368 done
369 }
370
371 ###--------------------------------------------------------------------------
372 ### Requesters.
373
374 req () {
375 declare title=$1 hist=$2; shift 2
376 cmd=$(xgetline -t "$title" -p "_Command:" -Hl "$HOME/$hist") &&
377 exec "$@" "$cmd"
378 }
379
380 ###--------------------------------------------------------------------------
381 ### Final waiting.
382
383 atom=XINIT_COMMAND$atomtag
384
385 xwait () {
386 while :; do
387 xatom delete $atom
388 info "waiting on $atom"
389 line=$(xatom wait $atom)
390 info "xatom: $line"
391
392 case "$line" in
393 :help)
394 xmsg -I -t "xinitrc help" -d "xinitrc commands" - <<EOF &
395 :help
396 :emacs :xterms :window-manager :clients
397 :ask-run :ask-command
398 :init
399 :terminal
400 ! SHELL-COMMAND
401 CLIENT
402 EOF
403 ;;
404 :emacs | :xterms | :window-manager | :clients)
405 start-${line#:}
406 ;;
407 :terminal)
408 run bgclients $term
409 ;;
410 :init)
411 initialize
412 ;;
413 :exec)
414 info "restarting xinitrc"
415 exec "$0" wait nostart
416 ;;
417 :ask-run)
418 req "Shell command" .cmd.hist xcatch -F"Fixed 13" -- sh -c&
419 ;;
420 :ask-command)
421 req "xinit command" .xinit.hist xatom set XINIT_COMMAND$atomtag&
422 ;;
423 :*)
424 xmsg -E -t "xinitrc error" "Unknown command \`$line'" &
425 ;;
426 !*)
427 eval "${line#!}"
428 ;;
429 *)
430 set -- $line
431 run bgclients "$@"
432 ;;
433 esac
434 done
435 }
436
437 ###--------------------------------------------------------------------------
438 ### Gnome session care and feeding.
439
440 session-running-p () {
441 dbus-send --session --print-reply --dest=org.freedesktop.DBus / \
442 org.freedesktop.DBus.GetNameOwner string:org.gnome.SessionManager \
443 >/dev/null 2>&1
444 }
445
446 dbus-service-running-p () {
447 dbus-send >/dev/null 2>&1 --session --print-reply \
448 --dest=org.freedesktop.DBus / \
449 org.freedesktop.DBus.GetNameOwner string:$1
450 }
451
452 kill-gnome-session () {
453 win=nil
454 while read service object logout; do
455 if dbus-service-running-p $service; then win=t; break; fi
456 done <<EOF
457 org.xfce.SessionManager /org/xfce/SessionManager org.xfce.Session.Manager.Shutdown uint32:1 boolean:false
458 org.gnome.SessionManager /org/gnome/SessionManager org.gnome.SessionManager.Logout uint32:2
459 EOF
460 case $win in nil) return ;; esac
461 info "killing session manager"
462 dbus-send --session --dest=$service $object $logout
463 for i in 1 2 3 4 5; do
464 sleep 1
465 if ! dbus-service-running-p $service; then break; fi
466 done
467 }
468
469 ###--------------------------------------------------------------------------
470 ### Actually start things up.
471
472 if [ -f $HOME/.xinitrc-local ]; then
473 . $HOME/.xinitrc-local
474 fi
475
476 case "$start" in
477 yes)
478 info "starting standard clients"
479 initialize
480 start-window-manager
481 start-clients
482 start-emacs
483 start-xterms
484 ;;
485 no)
486 info "not starting standard clients"
487 ;;
488 esac
489
490 case "$wait" in
491 yes)
492 xwait
493 kill-gnome-session
494 ;;
495 no)
496 info "not waiting before exit"
497 ;;
498 esac
499
500 ###----- That's all, folks --------------------------------------------------