Gnome hacking: Introduce a custom session file.
[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 () {
2e60c7f3 147
5587cf4f 148 ## Gnome session.
2e60c7f3
MW
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
54951353
MW
159
160 ## Local clients.
161 start-clients-local
8e08f814
MW
162}
163
164###--------------------------------------------------------------------------
165### Main screen layout.
f617db13 166
8e08f814 167## Choose appropriate clients.
0c47c911 168emacs=$(pick_program emacs emacs23 emacs22 emacs21 emacs)
8e08f814 169term=$(pick_program terminal pterm Eterm xterm)
f617db13 170
8e08f814 171## Emacs window measurements.
85bbf8d3 172case "$emacs" in
8e08f814 173 emacs21 | emacs)
85bbf8d3
MW
174 e_colwd=492 e_hextra=34
175 e_colchars=82 e_cextra=-2
176 e_lineht=13 e_vextra=52
177 ;;
0c47c911 178 emacs22 | emacs23)
85bbf8d3
MW
179 e_colwd=492 e_hextra=8
180 e_colchars=82 e_cextra=-6
181 e_lineht=13 e_vextra=46
182 ;;
183esac
f617db13 184
8e08f814 185## Terminal window measurements.
85bbf8d3 186case "$term" in
e2b44bd9 187 pterm) t_wd=504 t_lineht=13 t_vextra=23 geom=-geometry;;
85bbf8d3
MW
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;;
190esac
f617db13 191
8e08f814 192## GNOME stuff measurements.
f617db13
MW
193declare -i xbound="XWIDTH - 113"
194
8e08f814
MW
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.
f617db13 203
85bbf8d3
MW
204declare -i ecols="(xbound - t_wd - e_hextra)/e_colwd"
205if (( ecols < 2 && xbound > e_colwd * 2 + e_hextra )); then
f617db13
MW
206 ecols=2
207elif (( ecols < 1 )); then
208 ecols=1
209fi
210
8e08f814
MW
211declare -i \
212 emacsx="ecols * e_colchars + e_cextra" \
213 emacsy="(XHEIGHT - e_vextra)/e_lineht"
214
215start-emacs () {
bf057abe
MW
216 GDK_NATIVE_WINDOWS=1 run bgclients noip \
217 $emacs -geometry ${emacsx}x${emacsy}+0+0
8e08f814
MW
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
230start-xterms () {
231
232 ## Initialize some parameters.
6b4faba6 233 declare -i x="ecols * e_colwd + e_hextra" xb=xbound
8e08f814
MW
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.
6b4faba6 241 if ((x + t_wd > xb)); then
8e08f814 242 if ((n >= 3)); then break; fi
6b4faba6 243 x="pgx + XWIDTH" pgx="pgx + XWIDTH" xb="xb + XWIDTH"
8e08f814
MW
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"
f617db13 263 done
8e08f814 264}
f617db13 265
8e08f814 266###--------------------------------------------------------------------------
3bdada49
MW
267### Requesters.
268
269req () {
270 declare title=$1 hist=$2; shift 2
3f88bd1f 271 cmd=$(xgetline -t "$title" -p "_Command:" -Hl "$HOME/$hist") &&
3bdada49
MW
272 exec "$@" "$cmd"
273}
274
275###--------------------------------------------------------------------------
8e08f814
MW
276### Final waiting.
277
278atom=XINIT_COMMAND$atomtag
279
280xwait () {
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
3bdada49 292:ask-run :ask-command
8e08f814
MW
293:init
294:terminal
295! SHELL-COMMAND
296CLIENT
297EOF
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 ;;
3bdada49
MW
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 ;;
8e08f814
MW
318 :*)
319 xmsg -E -t "xinitrc error" "Unknown command \`$line'" &
320 ;;
321 !*)
322 eval "${line#!}"
323 ;;
324 *)
2f616761 325 set -- $line
8e08f814
MW
326 run bgclients "$@"
327 ;;
328 esac
329 done
330}
331
332###--------------------------------------------------------------------------
333### Actually start things up.
334
54951353
MW
335if [ -f $HOME/.xinitrc-local ]; then
336 . $HOME/.xinitrc-local
337fi
338
8e08f814
MW
339case "$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 ;;
351esac
352
353case "$wait" in
354 yes)
355 xwait
356 ;;
357 no)
358 info "not waiting before exit"
359 ;;
360esac
f617db13 361
8e08f814 362###----- That's all, folks --------------------------------------------------