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