dot/bash*: Reformat.
[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
8e08f814 28## Program choice
eebca092 29pick_program () {
8e08f814
MW
30 local what=$1; shift
31 local choice=false
eebca092 32 for i in "$@"; do
8e08f814 33 if type -t >/dev/null "$i"; then choice=$i; break; fi
eebca092 34 done
8e08f814
MW
35 info "pick $what = $choice"
36 echo "$choice"
eebca092
MW
37}
38
8e08f814
MW
39###--------------------------------------------------------------------------
40### Parse arguments.
f617db13 41
8e08f814
MW
42vnc=no
43atomtag=
44start=yes
45wait=yes
f617db13 46
8e08f814
MW
47for opt; do
48 case "$opt" in
49 help)
50 cat <<EOF
51Options:
52 tag=TAG
53 [no]trace
54 [no]info
55 [no]run
56 [no]start
57 [no]wait
58 [no]vnc
59EOF
60 exit
61 ;;
f617db13 62
8e08f814
MW
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
71done
72
73###--------------------------------------------------------------------------
74### Iniitial settings.
75
76## Assume X sessions are secure.
77export __mdw_sechost="`hostname`"
78
79## Obtain the screen dimensions.
80case ",$XWIDTH,$XHEIGHT," in
81 *,,*) eval $(xscsize -bx) ;;
82esac
83info "screen size = $XWIDTH x $XHEIGHT"
84
85initialize () {
86 ## Load the X resource database.
87 run init xrdb -override $HOME/.Xdefaults
88
89 ## Random xsettery.
8ab9747e 90 run init xset b 5 2000 50
8e08f814
MW
91 run init xset r rate 500 50
92 run init xset m 2 1
93
f4450ac9
MW
94 ## Key mappings.
95 xmodmap -e 'keysym BackSpace = BackSpace BackSpace'
8e08f814
MW
96}
97
98###--------------------------------------------------------------------------
99### Start a window manager.
100
101wm=$(pick_program window-manager enlightenment e16 twm)
102wmopts=""
103case "$wm,$vnc" in
104 enlightenment,yes | e16,yes)
105 wmopts="$eopts -econfdir $HOME/.enlightenment-vnc"
106 ;;
107esac
108
16ee7a8c 109start-e16 () {
8e08f814 110 run bginit $wm $wmopts
16ee7a8c
MW
111 win=nil
112 for i in $(seq 10); do
b35a9c36 113 sleep 1
16ee7a8c
MW
114 if eesh version >/dev/null 2>&1; then
115 win=t
116 break
117 fi
16ee7a8c
MW
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
130start-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
8e08f814
MW
139}
140
141###--------------------------------------------------------------------------
142### Random useful clients.
143
54951353
MW
144start-clients-local () { :; }
145
8e08f814 146start-clients () {
5587cf4f
MW
147 ## Gnome session.
148 case $vnc in no) run bginit gnome-session ;; esac
54951353
MW
149
150 ## Local clients.
151 start-clients-local
8e08f814
MW
152}
153
154###--------------------------------------------------------------------------
155### Main screen layout.
f617db13 156
8e08f814 157## Choose appropriate clients.
0c47c911 158emacs=$(pick_program emacs emacs23 emacs22 emacs21 emacs)
8e08f814 159term=$(pick_program terminal pterm Eterm xterm)
f617db13 160
8e08f814 161## Emacs window measurements.
85bbf8d3 162case "$emacs" in
8e08f814 163 emacs21 | emacs)
85bbf8d3
MW
164 e_colwd=492 e_hextra=34
165 e_colchars=82 e_cextra=-2
166 e_lineht=13 e_vextra=52
167 ;;
0c47c911 168 emacs22 | emacs23)
85bbf8d3
MW
169 e_colwd=492 e_hextra=8
170 e_colchars=82 e_cextra=-6
171 e_lineht=13 e_vextra=46
172 ;;
173esac
f617db13 174
8e08f814 175## Terminal window measurements.
85bbf8d3 176case "$term" in
e2b44bd9 177 pterm) t_wd=504 t_lineht=13 t_vextra=23 geom=-geometry;;
85bbf8d3
MW
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;;
180esac
f617db13 181
8e08f814 182## GNOME stuff measurements.
f617db13
MW
183declare -i xbound="XWIDTH - 113"
184
8e08f814
MW
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.
f617db13 193
85bbf8d3
MW
194declare -i ecols="(xbound - t_wd - e_hextra)/e_colwd"
195if (( ecols < 2 && xbound > e_colwd * 2 + e_hextra )); then
f617db13
MW
196 ecols=2
197elif (( ecols < 1 )); then
198 ecols=1
199fi
200
8e08f814
MW
201declare -i \
202 emacsx="ecols * e_colchars + e_cextra" \
203 emacsy="(XHEIGHT - e_vextra)/e_lineht"
204
205start-emacs () {
bf057abe
MW
206 GDK_NATIVE_WINDOWS=1 run bgclients noip \
207 $emacs -geometry ${emacsx}x${emacsy}+0+0
8e08f814
MW
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
220start-xterms () {
221
222 ## Initialize some parameters.
6b4faba6 223 declare -i x="ecols * e_colwd + e_hextra" xb=xbound
8e08f814
MW
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.
6b4faba6 231 if ((x + t_wd > xb)); then
8e08f814 232 if ((n >= 3)); then break; fi
6b4faba6 233 x="pgx + XWIDTH" pgx="pgx + XWIDTH" xb="xb + XWIDTH"
8e08f814
MW
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"
f617db13 253 done
8e08f814 254}
f617db13 255
8e08f814 256###--------------------------------------------------------------------------
3bdada49
MW
257### Requesters.
258
259req () {
260 declare title=$1 hist=$2; shift 2
3f88bd1f 261 cmd=$(xgetline -t "$title" -p "_Command:" -Hl "$HOME/$hist") &&
3bdada49
MW
262 exec "$@" "$cmd"
263}
264
265###--------------------------------------------------------------------------
8e08f814
MW
266### Final waiting.
267
268atom=XINIT_COMMAND$atomtag
269
270xwait () {
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
3bdada49 282:ask-run :ask-command
8e08f814
MW
283:init
284:terminal
285! SHELL-COMMAND
286CLIENT
287EOF
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 ;;
3bdada49
MW
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 ;;
8e08f814
MW
308 :*)
309 xmsg -E -t "xinitrc error" "Unknown command \`$line'" &
310 ;;
311 !*)
312 eval "${line#!}"
313 ;;
314 *)
2f616761 315 set -- $line
8e08f814
MW
316 run bgclients "$@"
317 ;;
318 esac
319 done
320}
321
322###--------------------------------------------------------------------------
323### Actually start things up.
324
54951353
MW
325if [ -f $HOME/.xinitrc-local ]; then
326 . $HOME/.xinitrc-local
327fi
328
8e08f814
MW
329case "$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 ;;
341esac
342
343case "$wait" in
344 yes)
345 xwait
346 ;;
347 no)
348 info "not waiting before exit"
349 ;;
350esac
f617db13 351
8e08f814 352###----- That's all, folks --------------------------------------------------