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