chiark / gitweb /
Merge branch 'master' of metalzone:etc/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###--------------------------------------------------------------------------
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.
90 run init xset b 10 2000 50
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'
96
8e08f814
MW
97 ## Gnome settings.
98 case $vnc in no) run bginit gnome-settings-daemon ;; esac
99}
100
101###--------------------------------------------------------------------------
102### Start a window manager.
103
104wm=$(pick_program window-manager enlightenment e16 twm)
105wmopts=""
106case "$wm,$vnc" in
107 enlightenment,yes | e16,yes)
108 wmopts="$eopts -econfdir $HOME/.enlightenment-vnc"
109 ;;
110esac
111
112start-window-manager () {
113 run bginit $wm $wmopts
114}
115
116###--------------------------------------------------------------------------
117### Random useful clients.
118
54951353
MW
119start-clients-local () { :; }
120
8e08f814
MW
121start-clients () {
122 ## Mail notification.
123 run bginit mail-notification
124
59a62625 125 ## Policykit authentication agent.
4906574e 126 agent=/usr/lib/policykit-1-gnome/polkit-gnome-authentication-agent-1
59a62625
MW
127 if [ -x $agent ]; then run bginit $agent; fi
128
8e08f814
MW
129 ## System monitor.
130 case $vnc in no) run bginit gkrellm ;; esac
131
c02999e8
MW
132 ## Screensaver.
133 case $vnc in
134 no)
135 run init xscreensaver-command -exit
136 run bginit xscreensaver -no-splash
137 ;;
138 esac
139
8e08f814
MW
140 ## Panel.
141 case $vnc in no) run bginit gnome-panel ;; esac
54951353 142
e517857c 143 ## System tray.
079b6c2b 144 ## run bginit stalonetray
e517857c 145
54951353
MW
146 ## Local clients.
147 start-clients-local
8e08f814
MW
148}
149
150###--------------------------------------------------------------------------
151### Main screen layout.
f617db13 152
8e08f814 153## Choose appropriate clients.
0c47c911 154emacs=$(pick_program emacs emacs23 emacs22 emacs21 emacs)
8e08f814 155term=$(pick_program terminal pterm Eterm xterm)
f617db13 156
8e08f814 157## Emacs window measurements.
85bbf8d3 158case "$emacs" in
8e08f814 159 emacs21 | emacs)
85bbf8d3
MW
160 e_colwd=492 e_hextra=34
161 e_colchars=82 e_cextra=-2
162 e_lineht=13 e_vextra=52
163 ;;
0c47c911 164 emacs22 | emacs23)
85bbf8d3
MW
165 e_colwd=492 e_hextra=8
166 e_colchars=82 e_cextra=-6
167 e_lineht=13 e_vextra=46
168 ;;
169esac
f617db13 170
8e08f814 171## Terminal window measurements.
85bbf8d3 172case "$term" in
e2b44bd9 173 pterm) t_wd=504 t_lineht=13 t_vextra=23 geom=-geometry;;
85bbf8d3
MW
174 Eterm) t_wd=504 t_lineht=13 t_vextra=23 geom=-g;;
175 xterm) t_wd=507 t_lineht=13 t_vextra=27 geom=-geometry;;
176esac
f617db13 177
8e08f814 178## GNOME stuff measurements.
f617db13
MW
179declare -i xbound="XWIDTH - 113"
180
8e08f814
MW
181## Choose a width for Emacs.
182##
183## We'd like it to be as wide as possible, allowing for a column of xterms
184## down the right hand side. However, I'd prefer a double-width Emacs to a
185## single-width Emacs and xterms. If it's not going to work at all, a single
186## Emacs column will have to do. Also, there's a strange thing with Emacs21
187## and the toolbar, so we add on some rows which are later mysteriously
188## subtracted.
f617db13 189
85bbf8d3
MW
190declare -i ecols="(xbound - t_wd - e_hextra)/e_colwd"
191if (( ecols < 2 && xbound > e_colwd * 2 + e_hextra )); then
f617db13
MW
192 ecols=2
193elif (( ecols < 1 )); then
194 ecols=1
195fi
196
8e08f814
MW
197declare -i \
198 emacsx="ecols * e_colchars + e_cextra" \
199 emacsy="(XHEIGHT - e_vextra)/e_lineht"
200
201start-emacs () {
bf057abe
MW
202 GDK_NATIVE_WINDOWS=1 run bgclients noip \
203 $emacs -geometry ${emacsx}x${emacsy}+0+0
8e08f814
MW
204}
205
206## Now place some xterms.
207##
208## A few smaller xterms are in general better than one great big one. 35
209## lines is a good height for most terminals. 25 lines is a minimum. The
210## strategy for doling out xterms into a column is to make as many 35-liners
211## as we can, until the remaining space would be too small for a 25-liner.
212## If we can get two 25s out of that then we do (largest first); otherwise
213## just make one big one. We stop at the end of a page, once we've made
214## three xterms.
215
216start-xterms () {
217
218 ## Initialize some parameters.
6b4faba6 219 declare -i x="ecols * e_colwd + e_hextra" xb=xbound
8e08f814
MW
220 declare -i n=0 pgx=0 l h y ht
221 declare -i hstd="35 * t_lineht + t_vextra" hmin="25 * t_lineht + t_vextra"
222
223 ## Do the placement.
224 while :; do
225
226 ## Start a new iteration.
6b4faba6 227 if ((x + t_wd > xb)); then
8e08f814 228 if ((n >= 3)); then break; fi
6b4faba6 229 x="pgx + XWIDTH" pgx="pgx + XWIDTH" xb="xb + XWIDTH"
8e08f814
MW
230 fi
231
232 ## Make large xterms.
233 y=0 ht=XHEIGHT
234 while ((ht - hstd >= hmin)); do
235 run bgclients $term $geom 80x35+$x+$y
236 y="y + hstd" ht="ht - hstd" n="n + 1"
237 done
238
239 ## Fill the remaining space.
240 if ((ht >= 2 * hmin)); then h="ht - hmin"; else h=ht; fi
241 l="(h - t_vextra)/t_lineht" h="l * t_lineht + t_vextra"
242 run bgclients $term $geom 80x$l+$x+$y
243 y="y + h" ht="ht - h" n="n + 1"
244 if ((ht >= hmin)); then
245 run bgclients $term $geom 80x25+$x+$y
246 n="n + 1"
247 fi
248 x="x + t_wd"
f617db13 249 done
8e08f814 250}
f617db13 251
3bdada49
MW
252###--------------------------------------------------------------------------
253### Requesters.
254
255req () {
256 declare title=$1 hist=$2; shift 2
3f88bd1f 257 cmd=$(xgetline -t "$title" -p "_Command:" -Hl "$HOME/$hist") &&
3bdada49
MW
258 exec "$@" "$cmd"
259}
260
8e08f814
MW
261###--------------------------------------------------------------------------
262### Final waiting.
263
264atom=XINIT_COMMAND$atomtag
265
266xwait () {
267 while :; do
268 xatom delete $atom
269 info "waiting on $atom"
270 line=$(xatom wait $atom)
271 info "xatom: $line"
272
273 case "$line" in
274 :help)
275 xmsg -I -t "xinitrc help" -d "xinitrc commands" - <<EOF &
276:help
277:emacs :xterms :window-manager :clients
3bdada49 278:ask-run :ask-command
8e08f814
MW
279:init
280:terminal
281! SHELL-COMMAND
282CLIENT
283EOF
284 ;;
285 :emacs | :xterms | :window-manager | :clients)
286 start-${line#:}
287 ;;
288 :terminal)
289 run bgclients $term
290 ;;
291 :init)
292 initialize
293 ;;
294 :exec)
295 info "restarting xinitrc"
296 exec "$0" wait nostart
297 ;;
3bdada49
MW
298 :ask-run)
299 req "Shell command" .cmd.hist xcatch -FMiscFixed6x13 -- sh -c&
300 ;;
301 :ask-command)
302 req "xinit command" .xinit.hist xatom set XINIT_COMMAND$atomtag&
303 ;;
8e08f814
MW
304 :*)
305 xmsg -E -t "xinitrc error" "Unknown command \`$line'" &
306 ;;
307 !*)
308 eval "${line#!}"
309 ;;
310 *)
311 set -- $line
312 run bgclients "$@"
313 ;;
314 esac
315 done
316}
317
318###--------------------------------------------------------------------------
319### Actually start things up.
320
54951353
MW
321if [ -f $HOME/.xinitrc-local ]; then
322 . $HOME/.xinitrc-local
323fi
324
8e08f814
MW
325case "$start" in
326 yes)
327 info "starting standard clients"
328 initialize
329 start-window-manager
330 start-clients
331 start-emacs
332 start-xterms
333 ;;
334 no)
335 info "not starting standard clients"
336 ;;
337esac
338
339case "$wait" in
340 yes)
341 xwait
342 ;;
343 no)
344 info "not waiting before exit"
345 ;;
346esac
f617db13 347
8e08f814 348###----- That's all, folks --------------------------------------------------