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