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