chiark / gitweb /
151b9a4b64e97d8da5e93751c76e03e96b793df8
[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 ## Program choice
29 pick_program () {
30   local what=$1; shift
31   local choice=false
32   for i in "$@"; do
33     if type -t >/dev/null "$i"; then choice=$i; break; fi
34   done
35   info "pick $what = $choice"
36   echo "$choice"
37 }
38
39 ###--------------------------------------------------------------------------
40 ### Parse arguments.
41
42 vnc=no
43 atomtag=
44 start=yes
45 wait=yes
46
47 for opt; do
48   case "$opt" in
49     help)
50       cat <<EOF
51 Options:
52   tag=TAG
53   [no]trace
54   [no]info
55   [no]run
56   [no]start
57   [no]wait
58   [no]vnc
59 EOF
60       exit
61       ;;
62
63     tag=*) atomtag=/${opt#tag=} ;;
64     trace) set -x ;;
65     notrace) set +x ;;
66     info | run | start | wait | vnc) eval "$opt=yes" ;;
67     noinfo | norun | nostart | nowait | novnc) eval "${opt#no}=no" ;;
68
69     *) echo "unknown option $opt" >&2; exit 1 ;;
70   esac
71 done
72
73 ###--------------------------------------------------------------------------
74 ### Iniitial settings.
75
76 ## Assume X sessions are secure.
77 export __mdw_sechost="`hostname`"
78
79 ## Obtain the screen dimensions.
80 case ",$XWIDTH,$XHEIGHT," in
81   *,,*) eval $(xscsize -bx) ;;
82 esac
83 info "screen size = $XWIDTH x $XHEIGHT"
84
85 initialize () {
86   ## Load the X resource database.
87   run init xrdb -override $HOME/.Xdefaults
88
89   ## Random xsettery.
90   run init xset b 5 2000 50
91   run init xset r rate 500 50
92   run init xset m 2 1
93
94   ## Key mappings.
95   xmodmap -e 'keysym BackSpace = BackSpace BackSpace'
96 }
97
98 ###--------------------------------------------------------------------------
99 ### Start a window manager.
100
101 wm=$(pick_program window-manager enlightenment e16 twm)
102 wmopts=""
103 case "$wm,$vnc" in
104   enlightenment,yes | e16,yes)
105     wmopts="$eopts -econfdir $HOME/.enlightenment-vnc"
106     ;;
107 esac
108
109 start-e16 () {
110   run bginit $wm $wmopts
111   win=nil
112   for i in $(seq 10); do
113     sleep 1
114     if eesh version >/dev/null 2>&1; then
115       win=t
116       break
117     fi
118   done
119   case $win in
120     t)
121       info "$wm started ok"
122       run init xsetroot -cursor_name left_ptr
123       ;;
124     nil)
125       info "$wm failed to start!"
126       ;;
127   esac
128 }
129
130 start-window-manager () {
131   case $(type -t start-$wm || echo "not-found") in
132     function)
133       start-$wm $wmopts
134       ;;
135     *)
136       run bginit $wm $wmopts
137       ;;
138   esac
139 }
140
141 ###--------------------------------------------------------------------------
142 ### Random useful clients.
143
144 start-clients-local () { :; }
145
146 start-clients () {
147   ## Gnome session.
148   case $vnc in no) run bginit gnome-session ;; esac
149
150   ## Local clients.
151   start-clients-local
152 }
153
154 ###--------------------------------------------------------------------------
155 ### Main screen layout.
156
157 ## Choose appropriate clients.
158 emacs=$(pick_program emacs emacs23 emacs22 emacs21 emacs)
159 term=$(pick_program terminal pterm Eterm xterm)
160
161 ## Emacs window measurements.
162 case "$emacs" in
163   emacs21 | emacs)
164     e_colwd=492 e_hextra=34
165     e_colchars=82 e_cextra=-2
166     e_lineht=13 e_vextra=52
167     ;;
168   emacs22 | emacs23)
169     e_colwd=492 e_hextra=8
170     e_colchars=82 e_cextra=-6
171     e_lineht=13 e_vextra=46
172     ;;
173 esac
174
175 ## Terminal window measurements.
176 case "$term" in
177   pterm) t_wd=504 t_lineht=13 t_vextra=23 geom=-geometry;;
178   Eterm) t_wd=504 t_lineht=13 t_vextra=23 geom=-g;;
179   xterm) t_wd=507 t_lineht=13 t_vextra=27 geom=-geometry;;
180 esac
181
182 ## GNOME stuff measurements.
183 declare -i xbound="XWIDTH - 113"
184
185 ## Choose a width for Emacs.
186 ##
187 ## We'd like it to be as wide as possible, allowing for a column of xterms
188 ## down the right hand side.  However, I'd prefer a double-width Emacs to a
189 ## single-width Emacs and xterms.  If it's not going to work at all, a single
190 ## Emacs column will have to do.  Also, there's a strange thing with Emacs21
191 ## and the toolbar, so we add on some rows which are later mysteriously
192 ## subtracted.
193
194 declare -i ecols="(xbound - t_wd - e_hextra)/e_colwd"
195 if (( ecols < 2 && xbound > e_colwd * 2 + e_hextra )); then
196   ecols=2
197 elif (( ecols < 1 )); then
198   ecols=1
199 fi
200
201 declare -i \
202   emacsx="ecols * e_colchars + e_cextra" \
203   emacsy="(XHEIGHT - e_vextra)/e_lineht"
204
205 start-emacs () {
206   GDK_NATIVE_WINDOWS=1 run bgclients noip \
207     $emacs -geometry ${emacsx}x${emacsy}+0+0
208 }
209
210 ## Now place some xterms.
211 ##
212 ## A few smaller xterms are in general better than one great big one.  35
213 ## lines is a good height for most terminals.  25 lines is a minimum.  The
214 ## strategy for doling out xterms into a column is to make as many 35-liners
215 ## as we can, until the remaining space would be too small for a 25-liner.
216 ## If we can get two 25s out of that then we do (largest first); otherwise
217 ## just make one big one.  We stop at the end of a page, once we've made
218 ## three xterms.
219
220 start-xterms () {
221
222   ## Initialize some parameters.
223   declare -i x="ecols * e_colwd + e_hextra" xb=xbound
224   declare -i n=0 pgx=0 l h y ht
225   declare -i hstd="35 * t_lineht + t_vextra" hmin="25 * t_lineht + t_vextra"
226
227   ## Do the placement.
228   while :; do
229
230     ## Start a new iteration.
231     if ((x + t_wd > xb)); then
232       if ((n >= 3)); then break; fi
233       x="pgx + XWIDTH" pgx="pgx + XWIDTH" xb="xb + XWIDTH"
234     fi
235
236     ## Make large xterms.
237     y=0 ht=XHEIGHT
238     while ((ht - hstd >= hmin)); do
239       run bgclients $term $geom 80x35+$x+$y
240       y="y + hstd" ht="ht - hstd" n="n + 1"
241     done
242
243     ## Fill the remaining space.
244     if ((ht >= 2 * hmin)); then h="ht - hmin"; else h=ht; fi
245     l="(h - t_vextra)/t_lineht" h="l * t_lineht + t_vextra"
246     run bgclients $term $geom 80x$l+$x+$y
247     y="y + h" ht="ht - h" n="n + 1"
248     if ((ht >= hmin)); then
249       run bgclients $term $geom 80x25+$x+$y
250       n="n + 1"
251     fi
252     x="x + t_wd"
253   done
254 }
255
256 ###--------------------------------------------------------------------------
257 ### Requesters.
258
259 req () {
260   declare title=$1 hist=$2; shift 2
261   cmd=$(xgetline -t "$title" -p "_Command:" -Hl "$HOME/$hist") &&
262   exec "$@" "$cmd"
263 }
264
265 ###--------------------------------------------------------------------------
266 ### Final waiting.
267
268 atom=XINIT_COMMAND$atomtag
269
270 xwait () {
271   while :; do
272     xatom delete $atom
273     info "waiting on $atom"
274     line=$(xatom wait $atom)
275     info "xatom: $line"
276
277     case "$line" in
278       :help)
279         xmsg -I -t "xinitrc help" -d "xinitrc commands" - <<EOF &
280 :help
281 :emacs :xterms :window-manager :clients
282 :ask-run :ask-command
283 :init
284 :terminal
285 ! SHELL-COMMAND
286 CLIENT
287 EOF
288         ;;
289       :emacs | :xterms | :window-manager | :clients)
290         start-${line#:}
291         ;;
292       :terminal)
293         run bgclients $term
294         ;;
295       :init)
296         initialize
297         ;;
298       :exec)
299         info "restarting xinitrc"
300         exec "$0" wait nostart
301         ;;
302       :ask-run)
303         req "Shell command" .cmd.hist xcatch -FMiscFixed6x13 -- sh -c&
304         ;;
305       :ask-command)
306         req "xinit command" .xinit.hist xatom set XINIT_COMMAND$atomtag&
307         ;;
308       :*)
309         xmsg -E -t "xinitrc error" "Unknown command \`$line'" &
310         ;;
311       !*)
312         eval "${line#!}"
313         ;;
314       *)
315         set -- $line
316         run bgclients "$@"
317         ;;
318     esac
319   done
320 }
321
322 ###--------------------------------------------------------------------------
323 ### Actually start things up.
324
325 if [ -f $HOME/.xinitrc-local ]; then
326   . $HOME/.xinitrc-local
327 fi
328
329 case "$start" in
330   yes)
331     info "starting standard clients"
332     initialize
333     start-window-manager
334     start-clients
335     start-emacs
336     start-xterms
337     ;;
338   no)
339     info "not starting standard clients"
340     ;;
341 esac
342
343 case "$wait" in
344   yes)
345     xwait
346     ;;
347   no)
348     info "not waiting before exit"
349     ;;
350 esac
351
352 ###----- That's all, folks --------------------------------------------------