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