dot/xinitrc: Document the various terminal and Emacs size parameters.
[profile] / dot / xinitrc
CommitLineData
f617db13 1#! /bin/bash
8e08f814
MW
2###
3### X startup script
f617db13 4
8e08f814
MW
5###--------------------------------------------------------------------------
6### Utility functions.
f617db13 7
8e08f814
MW
8## Progress indicators.
9info=yes
10info () {
11 case $info in yes) echo "- $*" >&2 ;; esac
12}
f617db13 13
8e08f814
MW
14run=yes
15run () {
16 local what=$1; shift
17 local bg=no
f617db13 18
8e08f814
MW
19 case $what in bg*) bg=yes what=${what#bg} ;; esac
20 info "run $what: $*"
f617db13 21
8e08f814
MW
22 case "$run,$bg" in
23 yes,no) "$@" ;;
24 yes,yes) "$@" & ;;
25 esac
26}
eebca092 27
74bd743a
MW
28manage () {
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
8e08f814 55## Program choice
eebca092 56pick_program () {
8e08f814
MW
57 local what=$1; shift
58 local choice=false
eebca092 59 for i in "$@"; do
8e08f814 60 if type -t >/dev/null "$i"; then choice=$i; break; fi
eebca092 61 done
8e08f814
MW
62 info "pick $what = $choice"
63 echo "$choice"
eebca092
MW
64}
65
8e08f814
MW
66###--------------------------------------------------------------------------
67### Parse arguments.
f617db13 68
8e08f814
MW
69vnc=no
70atomtag=
71start=yes
72wait=yes
f617db13 73
8e08f814
MW
74for opt; do
75 case "$opt" in
76 help)
77 cat <<EOF
78Options:
79 tag=TAG
80 [no]trace
81 [no]info
82 [no]run
83 [no]start
84 [no]wait
85 [no]vnc
86EOF
87 exit
88 ;;
f617db13 89
8e08f814
MW
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
98done
99
100###--------------------------------------------------------------------------
27b6787d
MW
101### Preliminary hook.
102
103if [ -r $HOME/.xinitrc-prehook ]; then
104 . $HOME/.xinitrc-prehook
105fi
106
107###--------------------------------------------------------------------------
8e08f814
MW
108### Iniitial settings.
109
110## Assume X sessions are secure.
111export __mdw_sechost="`hostname`"
112
113## Obtain the screen dimensions.
114case ",$XWIDTH,$XHEIGHT," in
115 *,,*) eval $(xscsize -bx) ;;
116esac
117info "screen size = $XWIDTH x $XHEIGHT"
118
119initialize () {
120 ## Load the X resource database.
121 run init xrdb -override $HOME/.Xdefaults
122
123 ## Random xsettery.
8ab9747e 124 run init xset b 5 2000 50
8e08f814
MW
125 run init xset r rate 500 50
126 run init xset m 2 1
127
f4450ac9 128 ## Key mappings.
cd38b61d
MW
129 xmodmap $HOME/.xmodmap
130 if [ -r $HOME/.xmodmap-local ]; then
131 xmodmap $HOME/.xmodmap-local
132 fi
8e08f814
MW
133}
134
135###--------------------------------------------------------------------------
136### Start a window manager.
137
6f6bd6da 138wm=$(pick_program window-manager e16 compiz enlightenment e17 twm)
8e08f814
MW
139wmopts=""
140case "$wm,$vnc" in
141 enlightenment,yes | e16,yes)
142 wmopts="$eopts -econfdir $HOME/.enlightenment-vnc"
143 ;;
144esac
145
16ee7a8c 146start-e16 () {
74bd743a 147 run bginit manage $wm $wmopts
16ee7a8c
MW
148 win=nil
149 for i in $(seq 10); do
b35a9c36 150 sleep 1
16ee7a8c
MW
151 if eesh version >/dev/null 2>&1; then
152 win=t
153 break
154 fi
16ee7a8c
MW
155 done
156 case $win in
157 t)
158 info "$wm started ok"
159 run init xsetroot -cursor_name left_ptr
160 ;;
161 nil)
162 info "$wm failed to start!"
163 ;;
164 esac
165}
166
167start-window-manager () {
168 case $(type -t start-$wm || echo "not-found") in
169 function)
170 start-$wm $wmopts
171 ;;
172 *)
74bd743a 173 run bginit manage $wm $wmopts
16ee7a8c
MW
174 ;;
175 esac
8e08f814
MW
176}
177
178###--------------------------------------------------------------------------
179### Random useful clients.
180
54951353
MW
181start-clients-local () { :; }
182
8e08f814 183start-clients () {
2e60c7f3 184
5587cf4f 185 ## Gnome session.
d83c96c9
MW
186 case "$vnc,$(xfce4-session --version 2>&1),$(gnome-session --version 2>&1)"
187 in
188 no,xfce4-session*)
189 run bginit xfce4-session
190 ;;
191 no,*,gnome-session\ 2.3[2-9].* | \
192 no,*,gnome-session\ 2.4[0-9].* | \
193 no,*,gnome-session\ 2.[1-9][0-9][0-9]*)
2e60c7f3
MW
194 run bginit gnome-session --session mdw
195 ;;
d83c96c9 196 no,*,gnome-session*)
2e60c7f3
MW
197 run bginit gnome-session
198 ;;
199 esac
54951353
MW
200
201 ## Local clients.
202 start-clients-local
8e08f814
MW
203}
204
205###--------------------------------------------------------------------------
206### Main screen layout.
f617db13 207
8e08f814 208## Choose appropriate clients.
384f691a 209emacs=$(pick_program emacs emacs23 emacs24 emacs22 emacs21 emacs)
8e08f814 210term=$(pick_program terminal pterm Eterm xterm)
f617db13 211
8e08f814 212## Emacs window measurements.
35f728b2
MW
213##
214## e_colwd = basic width of a column in pixels
215## e_hextra = extra horizontal width in pixels
216## Width of an N-column Emacs frame in pixels will be
217## N*e_colwd + e_hextra
218## e_colchars = width of a column in Emacs `-geometry' units
219## e_cextra = extra horizontal width in Emacs `-geometry' units
220## So an N-column frame should be reported to Emacs as being
221## N*e_colchars + e_cextra geometry units wide
222## e_lineht = height of a character line in pixels
223## e_vextra = number of additional vertical cruft pixels
224## So an N-line Emacs frame takes N*e_lineht + e_vextra pixels
85bbf8d3 225case "$emacs" in
8e08f814 226 emacs21 | emacs)
85bbf8d3
MW
227 e_colwd=492 e_hextra=34
228 e_colchars=82 e_cextra=-2
229 e_lineht=13 e_vextra=52
230 ;;
0c47c911 231 emacs22 | emacs23)
85bbf8d3
MW
232 e_colwd=492 e_hextra=8
233 e_colchars=82 e_cextra=-6
234 e_lineht=13 e_vextra=46
235 ;;
384f691a
MW
236 emacs24)
237 e_colwd=492 e_hextra=5
238 e_colchars=82 e_cextra=-6
239 e_lineht=13 e_vextra=42
240 ;;
85bbf8d3 241esac
f617db13 242
8e08f814 243## Terminal window measurements.
35f728b2
MW
244##
245## t_wd = the window width, in pixels
246## t_lineht, t_vextra = height parameters: if the window is N lines high,
247## then it will be N*t_lineht + t_vextra pixels high
85bbf8d3 248case "$term" in
e2b44bd9 249 pterm) t_wd=504 t_lineht=13 t_vextra=23 geom=-geometry;;
85bbf8d3
MW
250 Eterm) t_wd=504 t_lineht=13 t_vextra=23 geom=-g;;
251 xterm) t_wd=507 t_lineht=13 t_vextra=27 geom=-geometry;;
252esac
f617db13 253
8e08f814 254## GNOME stuff measurements.
f617db13
MW
255declare -i xbound="XWIDTH - 113"
256
8e08f814
MW
257## Choose a width for Emacs.
258##
259## We'd like it to be as wide as possible, allowing for a column of xterms
260## down the right hand side. However, I'd prefer a double-width Emacs to a
261## single-width Emacs and xterms. If it's not going to work at all, a single
262## Emacs column will have to do. Also, there's a strange thing with Emacs21
263## and the toolbar, so we add on some rows which are later mysteriously
264## subtracted.
f617db13 265
85bbf8d3
MW
266declare -i ecols="(xbound - t_wd - e_hextra)/e_colwd"
267if (( ecols < 2 && xbound > e_colwd * 2 + e_hextra )); then
f617db13
MW
268 ecols=2
269elif (( ecols < 1 )); then
270 ecols=1
271fi
272
8e08f814
MW
273declare -i \
274 emacsx="ecols * e_colchars + e_cextra" \
275 emacsy="(XHEIGHT - e_vextra)/e_lineht"
276
277start-emacs () {
bf057abe 278 GDK_NATIVE_WINDOWS=1 run bgclients noip \
89b55d90 279 $emacs -bg black -geometry ${emacsx}x${emacsy}+0+0
8e08f814
MW
280}
281
282## Now place some xterms.
283##
284## A few smaller xterms are in general better than one great big one. 35
285## lines is a good height for most terminals. 25 lines is a minimum. The
286## strategy for doling out xterms into a column is to make as many 35-liners
287## as we can, until the remaining space would be too small for a 25-liner.
288## If we can get two 25s out of that then we do (largest first); otherwise
289## just make one big one. We stop at the end of a page, once we've made
290## three xterms.
291
292start-xterms () {
293
294 ## Initialize some parameters.
6b4faba6 295 declare -i x="ecols * e_colwd + e_hextra" xb=xbound
8e08f814
MW
296 declare -i n=0 pgx=0 l h y ht
297 declare -i hstd="35 * t_lineht + t_vextra" hmin="25 * t_lineht + t_vextra"
298
299 ## Do the placement.
300 while :; do
301
302 ## Start a new iteration.
6b4faba6 303 if ((x + t_wd > xb)); then
8e08f814 304 if ((n >= 3)); then break; fi
6b4faba6 305 x="pgx + XWIDTH" pgx="pgx + XWIDTH" xb="xb + XWIDTH"
8e08f814
MW
306 fi
307
308 ## Make large xterms.
309 y=0 ht=XHEIGHT
310 while ((ht - hstd >= hmin)); do
311 run bgclients $term $geom 80x35+$x+$y
312 y="y + hstd" ht="ht - hstd" n="n + 1"
313 done
314
315 ## Fill the remaining space.
316 if ((ht >= 2 * hmin)); then h="ht - hmin"; else h=ht; fi
317 l="(h - t_vextra)/t_lineht" h="l * t_lineht + t_vextra"
318 run bgclients $term $geom 80x$l+$x+$y
319 y="y + h" ht="ht - h" n="n + 1"
320 if ((ht >= hmin)); then
321 run bgclients $term $geom 80x25+$x+$y
322 n="n + 1"
323 fi
324 x="x + t_wd"
f617db13 325 done
8e08f814 326}
f617db13 327
8e08f814 328###--------------------------------------------------------------------------
3bdada49
MW
329### Requesters.
330
331req () {
332 declare title=$1 hist=$2; shift 2
3f88bd1f 333 cmd=$(xgetline -t "$title" -p "_Command:" -Hl "$HOME/$hist") &&
3bdada49
MW
334 exec "$@" "$cmd"
335}
336
337###--------------------------------------------------------------------------
8e08f814
MW
338### Final waiting.
339
340atom=XINIT_COMMAND$atomtag
341
342xwait () {
343 while :; do
344 xatom delete $atom
345 info "waiting on $atom"
346 line=$(xatom wait $atom)
347 info "xatom: $line"
348
349 case "$line" in
350 :help)
351 xmsg -I -t "xinitrc help" -d "xinitrc commands" - <<EOF &
352:help
353:emacs :xterms :window-manager :clients
3bdada49 354:ask-run :ask-command
8e08f814
MW
355:init
356:terminal
357! SHELL-COMMAND
358CLIENT
359EOF
360 ;;
361 :emacs | :xterms | :window-manager | :clients)
362 start-${line#:}
363 ;;
364 :terminal)
365 run bgclients $term
366 ;;
367 :init)
368 initialize
369 ;;
370 :exec)
371 info "restarting xinitrc"
372 exec "$0" wait nostart
373 ;;
3bdada49 374 :ask-run)
a6780078 375 req "Shell command" .cmd.hist xcatch -F"Fixed 13" -- sh -c&
3bdada49
MW
376 ;;
377 :ask-command)
378 req "xinit command" .xinit.hist xatom set XINIT_COMMAND$atomtag&
379 ;;
8e08f814
MW
380 :*)
381 xmsg -E -t "xinitrc error" "Unknown command \`$line'" &
382 ;;
383 !*)
384 eval "${line#!}"
385 ;;
386 *)
2f616761 387 set -- $line
8e08f814
MW
388 run bgclients "$@"
389 ;;
390 esac
391 done
392}
393
394###--------------------------------------------------------------------------
8ca91005
MW
395### Gnome session care and feeding.
396
397session-running-p () {
398 dbus-send --session --print-reply --dest=org.freedesktop.DBus / \
399 org.freedesktop.DBus.GetNameOwner string:org.gnome.SessionManager \
400 >/dev/null 2>&1
401}
402
d83c96c9
MW
403dbus-service-running-p () {
404 dbus-send >/dev/null 2>&1 --session --print-reply \
405 --dest=org.freedesktop.DBus / \
406 org.freedesktop.DBus.GetNameOwner string:$1
407}
408
8ca91005 409kill-gnome-session () {
d83c96c9
MW
410 win=nil
411 while read service object logout; do
412 if dbus-service-running-p $service; then win=t; break; fi
413 done <<EOF
414org.xfce.SessionManager /org/xfce/SessionManager org.xfce.Session.Manager.Shutdown uint32:1 boolean:false
415org.gnome.SessionManager /org/gnome/SessionManager org.gnome.SessionManager.Logout uint32:2
416EOF
417 case $win in nil) return ;; esac
418 info "killing session manager"
419 dbus-send --session --dest=$service $object $logout
420 for i in 1 2 3 4 5; do
421 sleep 1
422 if ! dbus-service-running-p $service; then break; fi
423 done
8ca91005
MW
424}
425
426###--------------------------------------------------------------------------
8e08f814
MW
427### Actually start things up.
428
54951353
MW
429if [ -f $HOME/.xinitrc-local ]; then
430 . $HOME/.xinitrc-local
431fi
432
8e08f814
MW
433case "$start" in
434 yes)
435 info "starting standard clients"
436 initialize
437 start-window-manager
438 start-clients
439 start-emacs
440 start-xterms
441 ;;
442 no)
443 info "not starting standard clients"
444 ;;
445esac
446
447case "$wait" in
448 yes)
449 xwait
8ca91005 450 kill-gnome-session
8e08f814
MW
451 ;;
452 no)
453 info "not waiting before exit"
454 ;;
455esac
f617db13 456
8e08f814 457###----- That's all, folks --------------------------------------------------