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