| 1 | #! /bin/bash |
| 2 | ### |
| 3 | ### X startup script |
| 4 | |
| 5 | ###-------------------------------------------------------------------------- |
| 6 | ### Utility functions. |
| 7 | |
| 8 | ## Progress indicators. |
| 9 | info=yes |
| 10 | info () { |
| 11 | case $info in yes) echo "- $*" >&2 ;; esac |
| 12 | } |
| 13 | |
| 14 | run=yes |
| 15 | run () { |
| 16 | local what=$1; shift |
| 17 | local bg=no |
| 18 | |
| 19 | case $what in bg*) bg=yes what=${what#bg} ;; esac |
| 20 | info "run $what: $*" |
| 21 | |
| 22 | case "$run,$bg" in |
| 23 | yes,no) "$@" ;; |
| 24 | yes,yes) "$@" & ;; |
| 25 | esac |
| 26 | } |
| 27 | |
| 28 | ## Program choice |
| 29 | pick_program () { |
| 30 | local what=$1; shift |
| 31 | local choice=false |
| 32 | for i in "$@"; do |
| 33 | if type -t >/dev/null "$i"; then choice=$i; break; fi |
| 34 | done |
| 35 | info "pick $what = $choice" |
| 36 | echo "$choice" |
| 37 | } |
| 38 | |
| 39 | ###-------------------------------------------------------------------------- |
| 40 | ### Parse arguments. |
| 41 | |
| 42 | vnc=no |
| 43 | atomtag= |
| 44 | start=yes |
| 45 | wait=yes |
| 46 | |
| 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 | ;; |
| 62 | |
| 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 | ### Preliminary hook. |
| 75 | |
| 76 | if [ -r $HOME/.xinitrc-prehook ]; then |
| 77 | . $HOME/.xinitrc-prehook |
| 78 | fi |
| 79 | |
| 80 | ###-------------------------------------------------------------------------- |
| 81 | ### Iniitial settings. |
| 82 | |
| 83 | ## Assume X sessions are secure. |
| 84 | export __mdw_sechost="`hostname`" |
| 85 | |
| 86 | ## Obtain the screen dimensions. |
| 87 | case ",$XWIDTH,$XHEIGHT," in |
| 88 | *,,*) eval $(xscsize -bx) ;; |
| 89 | esac |
| 90 | info "screen size = $XWIDTH x $XHEIGHT" |
| 91 | |
| 92 | initialize () { |
| 93 | ## Load the X resource database. |
| 94 | run init xrdb -override $HOME/.Xdefaults |
| 95 | |
| 96 | ## Random xsettery. |
| 97 | run init xset b 5 2000 50 |
| 98 | run init xset r rate 500 50 |
| 99 | run init xset m 2 1 |
| 100 | |
| 101 | ## Key mappings. |
| 102 | xmodmap $HOME/.xmodmap |
| 103 | if [ -r $HOME/.xmodmap-local ]; then |
| 104 | xmodmap $HOME/.xmodmap-local |
| 105 | fi |
| 106 | } |
| 107 | |
| 108 | ###-------------------------------------------------------------------------- |
| 109 | ### Start a window manager. |
| 110 | |
| 111 | wm=$(pick_program window-manager e16 compiz enlightenment e17 twm) |
| 112 | wmopts="" |
| 113 | case "$wm,$vnc" in |
| 114 | enlightenment,yes | e16,yes) |
| 115 | wmopts="$eopts -econfdir $HOME/.enlightenment-vnc" |
| 116 | ;; |
| 117 | esac |
| 118 | |
| 119 | start-e16 () { |
| 120 | run bginit $wm $wmopts |
| 121 | win=nil |
| 122 | for i in $(seq 10); do |
| 123 | sleep 1 |
| 124 | if eesh version >/dev/null 2>&1; then |
| 125 | win=t |
| 126 | break |
| 127 | fi |
| 128 | done |
| 129 | case $win in |
| 130 | t) |
| 131 | info "$wm started ok" |
| 132 | run init xsetroot -cursor_name left_ptr |
| 133 | ;; |
| 134 | nil) |
| 135 | info "$wm failed to start!" |
| 136 | ;; |
| 137 | esac |
| 138 | } |
| 139 | |
| 140 | start-window-manager () { |
| 141 | case $(type -t start-$wm || echo "not-found") in |
| 142 | function) |
| 143 | start-$wm $wmopts |
| 144 | ;; |
| 145 | *) |
| 146 | run bginit $wm $wmopts |
| 147 | ;; |
| 148 | esac |
| 149 | } |
| 150 | |
| 151 | ###-------------------------------------------------------------------------- |
| 152 | ### Random useful clients. |
| 153 | |
| 154 | start-clients-local () { :; } |
| 155 | |
| 156 | start-clients () { |
| 157 | |
| 158 | ## Gnome session. |
| 159 | case "$vnc,$(xfce4-session --version 2>&1),$(gnome-session --version 2>&1)" |
| 160 | in |
| 161 | no,xfce4-session*) |
| 162 | run bginit xfce4-session |
| 163 | ;; |
| 164 | no,*,gnome-session\ 2.3[2-9].* | \ |
| 165 | no,*,gnome-session\ 2.4[0-9].* | \ |
| 166 | no,*,gnome-session\ 2.[1-9][0-9][0-9]*) |
| 167 | run bginit gnome-session --session mdw |
| 168 | ;; |
| 169 | no,*,gnome-session*) |
| 170 | run bginit gnome-session |
| 171 | ;; |
| 172 | esac |
| 173 | |
| 174 | ## Local clients. |
| 175 | start-clients-local |
| 176 | } |
| 177 | |
| 178 | ###-------------------------------------------------------------------------- |
| 179 | ### Main screen layout. |
| 180 | |
| 181 | ## Choose appropriate clients. |
| 182 | emacs=$(pick_program emacs emacs23 emacs22 emacs21 emacs) |
| 183 | term=$(pick_program terminal pterm Eterm xterm) |
| 184 | |
| 185 | ## Emacs window measurements. |
| 186 | case "$emacs" in |
| 187 | emacs21 | emacs) |
| 188 | e_colwd=492 e_hextra=34 |
| 189 | e_colchars=82 e_cextra=-2 |
| 190 | e_lineht=13 e_vextra=52 |
| 191 | ;; |
| 192 | emacs22 | emacs23) |
| 193 | e_colwd=492 e_hextra=8 |
| 194 | e_colchars=82 e_cextra=-6 |
| 195 | e_lineht=13 e_vextra=46 |
| 196 | ;; |
| 197 | esac |
| 198 | |
| 199 | ## Terminal window measurements. |
| 200 | case "$term" in |
| 201 | pterm) t_wd=504 t_lineht=13 t_vextra=23 geom=-geometry;; |
| 202 | Eterm) t_wd=504 t_lineht=13 t_vextra=23 geom=-g;; |
| 203 | xterm) t_wd=507 t_lineht=13 t_vextra=27 geom=-geometry;; |
| 204 | esac |
| 205 | |
| 206 | ## GNOME stuff measurements. |
| 207 | declare -i xbound="XWIDTH - 113" |
| 208 | |
| 209 | ## Choose a width for Emacs. |
| 210 | ## |
| 211 | ## We'd like it to be as wide as possible, allowing for a column of xterms |
| 212 | ## down the right hand side. However, I'd prefer a double-width Emacs to a |
| 213 | ## single-width Emacs and xterms. If it's not going to work at all, a single |
| 214 | ## Emacs column will have to do. Also, there's a strange thing with Emacs21 |
| 215 | ## and the toolbar, so we add on some rows which are later mysteriously |
| 216 | ## subtracted. |
| 217 | |
| 218 | declare -i ecols="(xbound - t_wd - e_hextra)/e_colwd" |
| 219 | if (( ecols < 2 && xbound > e_colwd * 2 + e_hextra )); then |
| 220 | ecols=2 |
| 221 | elif (( ecols < 1 )); then |
| 222 | ecols=1 |
| 223 | fi |
| 224 | |
| 225 | declare -i \ |
| 226 | emacsx="ecols * e_colchars + e_cextra" \ |
| 227 | emacsy="(XHEIGHT - e_vextra)/e_lineht" |
| 228 | |
| 229 | start-emacs () { |
| 230 | GDK_NATIVE_WINDOWS=1 run bgclients noip \ |
| 231 | $emacs -geometry ${emacsx}x${emacsy}+0+0 |
| 232 | } |
| 233 | |
| 234 | ## Now place some xterms. |
| 235 | ## |
| 236 | ## A few smaller xterms are in general better than one great big one. 35 |
| 237 | ## lines is a good height for most terminals. 25 lines is a minimum. The |
| 238 | ## strategy for doling out xterms into a column is to make as many 35-liners |
| 239 | ## as we can, until the remaining space would be too small for a 25-liner. |
| 240 | ## If we can get two 25s out of that then we do (largest first); otherwise |
| 241 | ## just make one big one. We stop at the end of a page, once we've made |
| 242 | ## three xterms. |
| 243 | |
| 244 | start-xterms () { |
| 245 | |
| 246 | ## Initialize some parameters. |
| 247 | declare -i x="ecols * e_colwd + e_hextra" xb=xbound |
| 248 | declare -i n=0 pgx=0 l h y ht |
| 249 | declare -i hstd="35 * t_lineht + t_vextra" hmin="25 * t_lineht + t_vextra" |
| 250 | |
| 251 | ## Do the placement. |
| 252 | while :; do |
| 253 | |
| 254 | ## Start a new iteration. |
| 255 | if ((x + t_wd > xb)); then |
| 256 | if ((n >= 3)); then break; fi |
| 257 | x="pgx + XWIDTH" pgx="pgx + XWIDTH" xb="xb + XWIDTH" |
| 258 | fi |
| 259 | |
| 260 | ## Make large xterms. |
| 261 | y=0 ht=XHEIGHT |
| 262 | while ((ht - hstd >= hmin)); do |
| 263 | run bgclients $term $geom 80x35+$x+$y |
| 264 | y="y + hstd" ht="ht - hstd" n="n + 1" |
| 265 | done |
| 266 | |
| 267 | ## Fill the remaining space. |
| 268 | if ((ht >= 2 * hmin)); then h="ht - hmin"; else h=ht; fi |
| 269 | l="(h - t_vextra)/t_lineht" h="l * t_lineht + t_vextra" |
| 270 | run bgclients $term $geom 80x$l+$x+$y |
| 271 | y="y + h" ht="ht - h" n="n + 1" |
| 272 | if ((ht >= hmin)); then |
| 273 | run bgclients $term $geom 80x25+$x+$y |
| 274 | n="n + 1" |
| 275 | fi |
| 276 | x="x + t_wd" |
| 277 | done |
| 278 | } |
| 279 | |
| 280 | ###-------------------------------------------------------------------------- |
| 281 | ### Requesters. |
| 282 | |
| 283 | req () { |
| 284 | declare title=$1 hist=$2; shift 2 |
| 285 | cmd=$(xgetline -t "$title" -p "_Command:" -Hl "$HOME/$hist") && |
| 286 | exec "$@" "$cmd" |
| 287 | } |
| 288 | |
| 289 | ###-------------------------------------------------------------------------- |
| 290 | ### Final waiting. |
| 291 | |
| 292 | atom=XINIT_COMMAND$atomtag |
| 293 | |
| 294 | xwait () { |
| 295 | while :; do |
| 296 | xatom delete $atom |
| 297 | info "waiting on $atom" |
| 298 | line=$(xatom wait $atom) |
| 299 | info "xatom: $line" |
| 300 | |
| 301 | case "$line" in |
| 302 | :help) |
| 303 | xmsg -I -t "xinitrc help" -d "xinitrc commands" - <<EOF & |
| 304 | :help |
| 305 | :emacs :xterms :window-manager :clients |
| 306 | :ask-run :ask-command |
| 307 | :init |
| 308 | :terminal |
| 309 | ! SHELL-COMMAND |
| 310 | CLIENT |
| 311 | EOF |
| 312 | ;; |
| 313 | :emacs | :xterms | :window-manager | :clients) |
| 314 | start-${line#:} |
| 315 | ;; |
| 316 | :terminal) |
| 317 | run bgclients $term |
| 318 | ;; |
| 319 | :init) |
| 320 | initialize |
| 321 | ;; |
| 322 | :exec) |
| 323 | info "restarting xinitrc" |
| 324 | exec "$0" wait nostart |
| 325 | ;; |
| 326 | :ask-run) |
| 327 | req "Shell command" .cmd.hist xcatch -FMiscFixed6x13 -- sh -c& |
| 328 | ;; |
| 329 | :ask-command) |
| 330 | req "xinit command" .xinit.hist xatom set XINIT_COMMAND$atomtag& |
| 331 | ;; |
| 332 | :*) |
| 333 | xmsg -E -t "xinitrc error" "Unknown command \`$line'" & |
| 334 | ;; |
| 335 | !*) |
| 336 | eval "${line#!}" |
| 337 | ;; |
| 338 | *) |
| 339 | set -- $line |
| 340 | run bgclients "$@" |
| 341 | ;; |
| 342 | esac |
| 343 | done |
| 344 | } |
| 345 | |
| 346 | ###-------------------------------------------------------------------------- |
| 347 | ### Gnome session care and feeding. |
| 348 | |
| 349 | session-running-p () { |
| 350 | dbus-send --session --print-reply --dest=org.freedesktop.DBus / \ |
| 351 | org.freedesktop.DBus.GetNameOwner string:org.gnome.SessionManager \ |
| 352 | >/dev/null 2>&1 |
| 353 | } |
| 354 | |
| 355 | dbus-service-running-p () { |
| 356 | dbus-send >/dev/null 2>&1 --session --print-reply \ |
| 357 | --dest=org.freedesktop.DBus / \ |
| 358 | org.freedesktop.DBus.GetNameOwner string:$1 |
| 359 | } |
| 360 | |
| 361 | kill-gnome-session () { |
| 362 | win=nil |
| 363 | while read service object logout; do |
| 364 | if dbus-service-running-p $service; then win=t; break; fi |
| 365 | done <<EOF |
| 366 | org.xfce.SessionManager /org/xfce/SessionManager org.xfce.Session.Manager.Shutdown uint32:1 boolean:false |
| 367 | org.gnome.SessionManager /org/gnome/SessionManager org.gnome.SessionManager.Logout uint32:2 |
| 368 | EOF |
| 369 | case $win in nil) return ;; esac |
| 370 | info "killing session manager" |
| 371 | dbus-send --session --dest=$service $object $logout |
| 372 | for i in 1 2 3 4 5; do |
| 373 | sleep 1 |
| 374 | if ! dbus-service-running-p $service; then break; fi |
| 375 | done |
| 376 | } |
| 377 | |
| 378 | ###-------------------------------------------------------------------------- |
| 379 | ### Actually start things up. |
| 380 | |
| 381 | if [ -f $HOME/.xinitrc-local ]; then |
| 382 | . $HOME/.xinitrc-local |
| 383 | fi |
| 384 | |
| 385 | case "$start" in |
| 386 | yes) |
| 387 | info "starting standard clients" |
| 388 | initialize |
| 389 | start-window-manager |
| 390 | start-clients |
| 391 | start-emacs |
| 392 | start-xterms |
| 393 | ;; |
| 394 | no) |
| 395 | info "not starting standard clients" |
| 396 | ;; |
| 397 | esac |
| 398 | |
| 399 | case "$wait" in |
| 400 | yes) |
| 401 | xwait |
| 402 | kill-gnome-session |
| 403 | ;; |
| 404 | no) |
| 405 | info "not waiting before exit" |
| 406 | ;; |
| 407 | esac |
| 408 | |
| 409 | ###----- That's all, folks -------------------------------------------------- |