Gnome hacking: Introduce a custom session file.
[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
148 ## Gnome session.
149 case "$vnc,$(gnome-session --version 2>&1)" in
150 no,gnome-session\ 2.3[2-9].* | \
151 no,gnome-session\ 2.4[0-9].* | \
152 no,gnome-session\ 2.[1-9][0-9][0-9]*)
153 run bginit gnome-session --session mdw
154 ;;
155 no,*)
156 run bginit gnome-session
157 ;;
158 esac
159
160 ## Local clients.
161 start-clients-local
162 }
163
164 ###--------------------------------------------------------------------------
165 ### Main screen layout.
166
167 ## Choose appropriate clients.
168 emacs=$(pick_program emacs emacs23 emacs22 emacs21 emacs)
169 term=$(pick_program terminal pterm Eterm xterm)
170
171 ## Emacs window measurements.
172 case "$emacs" in
173 emacs21 | emacs)
174 e_colwd=492 e_hextra=34
175 e_colchars=82 e_cextra=-2
176 e_lineht=13 e_vextra=52
177 ;;
178 emacs22 | emacs23)
179 e_colwd=492 e_hextra=8
180 e_colchars=82 e_cextra=-6
181 e_lineht=13 e_vextra=46
182 ;;
183 esac
184
185 ## Terminal window measurements.
186 case "$term" in
187 pterm) t_wd=504 t_lineht=13 t_vextra=23 geom=-geometry;;
188 Eterm) t_wd=504 t_lineht=13 t_vextra=23 geom=-g;;
189 xterm) t_wd=507 t_lineht=13 t_vextra=27 geom=-geometry;;
190 esac
191
192 ## GNOME stuff measurements.
193 declare -i xbound="XWIDTH - 113"
194
195 ## Choose a width for Emacs.
196 ##
197 ## We'd like it to be as wide as possible, allowing for a column of xterms
198 ## down the right hand side. However, I'd prefer a double-width Emacs to a
199 ## single-width Emacs and xterms. If it's not going to work at all, a single
200 ## Emacs column will have to do. Also, there's a strange thing with Emacs21
201 ## and the toolbar, so we add on some rows which are later mysteriously
202 ## subtracted.
203
204 declare -i ecols="(xbound - t_wd - e_hextra)/e_colwd"
205 if (( ecols < 2 && xbound > e_colwd * 2 + e_hextra )); then
206 ecols=2
207 elif (( ecols < 1 )); then
208 ecols=1
209 fi
210
211 declare -i \
212 emacsx="ecols * e_colchars + e_cextra" \
213 emacsy="(XHEIGHT - e_vextra)/e_lineht"
214
215 start-emacs () {
216 GDK_NATIVE_WINDOWS=1 run bgclients noip \
217 $emacs -geometry ${emacsx}x${emacsy}+0+0
218 }
219
220 ## Now place some xterms.
221 ##
222 ## A few smaller xterms are in general better than one great big one. 35
223 ## lines is a good height for most terminals. 25 lines is a minimum. The
224 ## strategy for doling out xterms into a column is to make as many 35-liners
225 ## as we can, until the remaining space would be too small for a 25-liner.
226 ## If we can get two 25s out of that then we do (largest first); otherwise
227 ## just make one big one. We stop at the end of a page, once we've made
228 ## three xterms.
229
230 start-xterms () {
231
232 ## Initialize some parameters.
233 declare -i x="ecols * e_colwd + e_hextra" xb=xbound
234 declare -i n=0 pgx=0 l h y ht
235 declare -i hstd="35 * t_lineht + t_vextra" hmin="25 * t_lineht + t_vextra"
236
237 ## Do the placement.
238 while :; do
239
240 ## Start a new iteration.
241 if ((x + t_wd > xb)); then
242 if ((n >= 3)); then break; fi
243 x="pgx + XWIDTH" pgx="pgx + XWIDTH" xb="xb + XWIDTH"
244 fi
245
246 ## Make large xterms.
247 y=0 ht=XHEIGHT
248 while ((ht - hstd >= hmin)); do
249 run bgclients $term $geom 80x35+$x+$y
250 y="y + hstd" ht="ht - hstd" n="n + 1"
251 done
252
253 ## Fill the remaining space.
254 if ((ht >= 2 * hmin)); then h="ht - hmin"; else h=ht; fi
255 l="(h - t_vextra)/t_lineht" h="l * t_lineht + t_vextra"
256 run bgclients $term $geom 80x$l+$x+$y
257 y="y + h" ht="ht - h" n="n + 1"
258 if ((ht >= hmin)); then
259 run bgclients $term $geom 80x25+$x+$y
260 n="n + 1"
261 fi
262 x="x + t_wd"
263 done
264 }
265
266 ###--------------------------------------------------------------------------
267 ### Requesters.
268
269 req () {
270 declare title=$1 hist=$2; shift 2
271 cmd=$(xgetline -t "$title" -p "_Command:" -Hl "$HOME/$hist") &&
272 exec "$@" "$cmd"
273 }
274
275 ###--------------------------------------------------------------------------
276 ### Final waiting.
277
278 atom=XINIT_COMMAND$atomtag
279
280 xwait () {
281 while :; do
282 xatom delete $atom
283 info "waiting on $atom"
284 line=$(xatom wait $atom)
285 info "xatom: $line"
286
287 case "$line" in
288 :help)
289 xmsg -I -t "xinitrc help" -d "xinitrc commands" - <<EOF &
290 :help
291 :emacs :xterms :window-manager :clients
292 :ask-run :ask-command
293 :init
294 :terminal
295 ! SHELL-COMMAND
296 CLIENT
297 EOF
298 ;;
299 :emacs | :xterms | :window-manager | :clients)
300 start-${line#:}
301 ;;
302 :terminal)
303 run bgclients $term
304 ;;
305 :init)
306 initialize
307 ;;
308 :exec)
309 info "restarting xinitrc"
310 exec "$0" wait nostart
311 ;;
312 :ask-run)
313 req "Shell command" .cmd.hist xcatch -FMiscFixed6x13 -- sh -c&
314 ;;
315 :ask-command)
316 req "xinit command" .xinit.hist xatom set XINIT_COMMAND$atomtag&
317 ;;
318 :*)
319 xmsg -E -t "xinitrc error" "Unknown command \`$line'" &
320 ;;
321 !*)
322 eval "${line#!}"
323 ;;
324 *)
325 set -- $line
326 run bgclients "$@"
327 ;;
328 esac
329 done
330 }
331
332 ###--------------------------------------------------------------------------
333 ### Actually start things up.
334
335 if [ -f $HOME/.xinitrc-local ]; then
336 . $HOME/.xinitrc-local
337 fi
338
339 case "$start" in
340 yes)
341 info "starting standard clients"
342 initialize
343 start-window-manager
344 start-clients
345 start-emacs
346 start-xterms
347 ;;
348 no)
349 info "not starting standard clients"
350 ;;
351 esac
352
353 case "$wait" in
354 yes)
355 xwait
356 ;;
357 no)
358 info "not waiting before exit"
359 ;;
360 esac
361
362 ###----- That's all, folks --------------------------------------------------