chiark / gitweb /
dot/xinitrc: Decrease the amount of space reserved on the right.
[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 manage () {
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
55 ## Program choice
56 pick_program () {
57   local what=$1; shift
58   local choice=false
59   for i in "$@"; do
60     if type -t >/dev/null "$i"; then choice=$i; break; fi
61   done
62   info "pick $what = $choice"
63   echo "$choice"
64 }
65
66 ###--------------------------------------------------------------------------
67 ### Parse arguments.
68
69 vnc=no
70 atomtag=
71 start=yes
72 wait=yes
73
74 for opt; do
75   case "$opt" in
76     help)
77       cat <<EOF
78 Options:
79   tag=TAG
80   [no]trace
81   [no]info
82   [no]run
83   [no]start
84   [no]wait
85   [no]vnc
86 EOF
87       exit
88       ;;
89
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
98 done
99
100 ###--------------------------------------------------------------------------
101 ### Preliminary hook.
102
103 if [ -r $HOME/.xinitrc-prehook ]; then
104   . $HOME/.xinitrc-prehook
105 fi
106
107 ###--------------------------------------------------------------------------
108 ### Iniitial settings.
109
110 ## Assume X sessions are secure.
111 export __mdw_sechost="`hostname`"
112
113 ## Obtain the screen dimensions.
114 case ",$XWIDTH,$XHEIGHT," in
115   *,,*) eval $(xscsize -bx) ;;
116 esac
117 info "screen size = $XWIDTH x $XHEIGHT"
118
119 initialize () {
120   ## Load the X resource database.
121   run init xrdb -override $HOME/.Xdefaults
122
123   ## Random xsettery.
124   run init xset b 5 2000 50
125   run init xset r rate 500 50
126   run init xset m 2 1
127
128   ## Key mappings.
129   xmodmap $HOME/.xmodmap
130   if [ -r $HOME/.xmodmap-local ]; then
131     xmodmap $HOME/.xmodmap-local
132   fi
133 }
134
135 ###--------------------------------------------------------------------------
136 ### Start a window manager.
137
138 wm=$(pick_program window-manager e16 compiz enlightenment e17 twm)
139 wmopts=""
140 case "$wm,$vnc" in
141   enlightenment,yes | e16,yes)
142     wmopts="$eopts -econfdir $HOME/.enlightenment-vnc"
143     ;;
144 esac
145
146 start-e16 () {
147   run bginit manage $wm $wmopts
148   win=nil
149   for i in $(seq 10); do
150     sleep 1
151     if eesh version >/dev/null 2>&1; then
152       win=t
153       break
154     fi
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
167 start-window-manager () {
168   case $(type -t start-$wm || echo "not-found") in
169     function)
170       start-$wm $wmopts
171       ;;
172     *)
173       run bginit manage $wm $wmopts
174       ;;
175   esac
176 }
177
178 ###--------------------------------------------------------------------------
179 ### Random useful clients.
180
181 start-clients-local () { :; }
182
183 start-clients () {
184
185   ## Gnome session.
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]*)
194       run bginit gnome-session --session mdw
195       ;;
196     no,*,gnome-session*)
197       run bginit gnome-session
198       ;;
199   esac
200
201   ## Local clients.
202   start-clients-local
203 }
204
205 ###--------------------------------------------------------------------------
206 ### Main screen layout.
207
208 ## Choose appropriate clients.
209 emacs=$(pick_program emacs emacs23 emacs24 emacs22 emacs21 emacs)
210 term=$(pick_program terminal pterm Eterm xterm)
211
212 ## Emacs window measurements.
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
225 case "$emacs" in
226   emacs21 | emacs)
227     e_colwd=492 e_hextra=34
228     e_colchars=82 e_cextra=-2
229     e_lineht=13 e_vextra=52
230     ;;
231   emacs22 | emacs23)
232     e_colwd=492 e_hextra=8
233     e_colchars=82 e_cextra=-6
234     e_lineht=13 e_vextra=46
235     ;;
236   emacs24)
237     e_colwd=492 e_hextra=5
238     e_colchars=82 e_cextra=-6
239     e_lineht=13 e_vextra=42
240     ;;
241 esac
242
243 ## Terminal window measurements.
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
248 case "$term" in
249   pterm) t_wd=504 t_lineht=13 t_vextra=23 geom=-geometry;;
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;;
252 esac
253
254 ## GNOME stuff measurements.
255 declare -i panelwd=64 xbound=$(( XWIDTH - panelwd ))
256
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.
265
266 declare -i ecols="(xbound - t_wd - e_hextra)/e_colwd"
267 if (( ecols < 2 && xbound > e_colwd * 2 + e_hextra )); then
268   ecols=2
269 elif (( ecols < 1 )); then
270   ecols=1
271 fi
272
273 declare -i \
274   emacsx="ecols * e_colchars + e_cextra" \
275   emacsy="(XHEIGHT - e_vextra)/e_lineht"
276
277 start-emacs () {
278   GDK_NATIVE_WINDOWS=1 run bgclients noip \
279     $emacs -bg black -geometry ${emacsx}x${emacsy}+0+0
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
292 start-xterms () {
293
294   ## Initialize some parameters.
295   declare -i x="ecols * e_colwd + e_hextra" xb=xbound
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.
303     if ((x + t_wd > xb)); then
304       if ((n >= 3)); then break; fi
305       x="pgx + XWIDTH" pgx="pgx + XWIDTH" xb="xb + XWIDTH"
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"
325   done
326 }
327
328 ###--------------------------------------------------------------------------
329 ### Requesters.
330
331 req () {
332   declare title=$1 hist=$2; shift 2
333   cmd=$(xgetline -t "$title" -p "_Command:" -Hl "$HOME/$hist") &&
334   exec "$@" "$cmd"
335 }
336
337 ###--------------------------------------------------------------------------
338 ### Final waiting.
339
340 atom=XINIT_COMMAND$atomtag
341
342 xwait () {
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
354 :ask-run :ask-command
355 :init
356 :terminal
357 ! SHELL-COMMAND
358 CLIENT
359 EOF
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         ;;
374       :ask-run)
375         req "Shell command" .cmd.hist xcatch -F"Fixed 13" -- sh -c&
376         ;;
377       :ask-command)
378         req "xinit command" .xinit.hist xatom set XINIT_COMMAND$atomtag&
379         ;;
380       :*)
381         xmsg -E -t "xinitrc error" "Unknown command \`$line'" &
382         ;;
383       !*)
384         eval "${line#!}"
385         ;;
386       *)
387         set -- $line
388         run bgclients "$@"
389         ;;
390     esac
391   done
392 }
393
394 ###--------------------------------------------------------------------------
395 ### Gnome session care and feeding.
396
397 session-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
403 dbus-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
409 kill-gnome-session () {
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
414 org.xfce.SessionManager /org/xfce/SessionManager org.xfce.Session.Manager.Shutdown uint32:1 boolean:false
415 org.gnome.SessionManager /org/gnome/SessionManager org.gnome.SessionManager.Logout uint32:2
416 EOF
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
424 }
425
426 ###--------------------------------------------------------------------------
427 ### Actually start things up.
428
429 if [ -f $HOME/.xinitrc-local ]; then
430   . $HOME/.xinitrc-local
431 fi
432
433 case "$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     ;;
445 esac
446
447 case "$wait" in
448   yes)
449     xwait
450     kill-gnome-session
451     ;;
452   no)
453     info "not waiting before exit"
454     ;;
455 esac
456
457 ###----- That's all, folks --------------------------------------------------