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