chiark / gitweb /
el/dot-emacs.el (mdw-fontify-rust): Fix integer literal syntax.
[profile] / dot / xinitrc
index 151b9a4b64e97d8da5e93751c76e03e96b793df8..51a9caeefde41e9a6bf2ef3371e489acd031d508 100755 (executable)
@@ -25,6 +25,33 @@ run () {
   esac
 }
 
+manage () {
+  local when=$(date +%s) now
+  local fail=0 rc report
+
+  while :; do
+    "$@"; rc=$?
+    case $rc in
+      0) info "manage $1: successful exit"; break ;;
+      143) info "manage $1: terminated"; break ;;
+    esac
+    now=$(date +%s)
+    report="rc = $rc"
+    if (( $now - $when > 5 )); then
+      fail=0
+    else
+      report="$report, early failure"
+      fail=$(( $fail + 1 ))
+      if (( $fail >= 5 )); then
+       info "manage $1: exit ($report), giving up after $fail failures"
+       break
+      fi
+    fi
+    info "manage $1: exit ($report), restarting"
+    when=$now
+  done
+}
+
 ## Program choice
 pick_program () {
   local what=$1; shift
@@ -70,6 +97,13 @@ EOF
   esac
 done
 
+###--------------------------------------------------------------------------
+### Preliminary hook.
+
+if [ -r $HOME/.xinitrc-prehook ]; then
+  . $HOME/.xinitrc-prehook
+fi
+
 ###--------------------------------------------------------------------------
 ### Iniitial settings.
 
@@ -78,9 +112,19 @@ export __mdw_sechost="`hostname`"
 
 ## Obtain the screen dimensions.
 case ",$XWIDTH,$XHEIGHT," in
-  *,,*) eval $(xscsize -bx) ;;
+  *,,*) eval $(xscsize -bx; xscsize -bmx) ;;
+esac
+case ",$XNSCR," in
+  ,,)
+    XNSCR=1 XSCR0_X=0 XSCR0_Y=0 XSCR0_WIDTH=$XWIDTH XSCR0_HEIGHT=$XHEIGHT
+    ;;
 esac
-info "screen size = $XWIDTH x $XHEIGHT"
+i=0; while (( i < XNSCR )); do
+  eval "x=\$XSCR${i}_X y=\$XSCR${i}_Y
+       wd=\$XSCR${i}_WIDTH ht=\$XSCR${i}_HEIGHT"
+  info "screen #$i = ${wd}x${ht}+${x}+${y}"
+  i=$(( i + 1 ))
+done
 
 initialize () {
   ## Load the X resource database.
@@ -92,13 +136,16 @@ initialize () {
   run init xset m 2 1
 
   ## Key mappings.
-  xmodmap -e 'keysym BackSpace = BackSpace BackSpace'
+  xmodmap $HOME/.xmodmap
+  if [ -r $HOME/.xmodmap-local ]; then
+    xmodmap $HOME/.xmodmap-local
+  fi
 }
 
 ###--------------------------------------------------------------------------
 ### Start a window manager.
 
-wm=$(pick_program window-manager enlightenment e16 twm)
+wm=$(pick_program window-manager e16 compiz enlightenment e17 twm)
 wmopts=""
 case "$wm,$vnc" in
   enlightenment,yes | e16,yes)
@@ -107,7 +154,7 @@ case "$wm,$vnc" in
 esac
 
 start-e16 () {
-  run bginit $wm $wmopts
+  run bginit manage $wm $wmopts
   win=nil
   for i in $(seq 10); do
     sleep 1
@@ -133,7 +180,7 @@ start-window-manager () {
       start-$wm $wmopts
       ;;
     *)
-      run bginit $wm $wmopts
+      run bginit manage $wm $wmopts
       ;;
   esac
 }
@@ -144,8 +191,22 @@ start-window-manager () {
 start-clients-local () { :; }
 
 start-clients () {
+
   ## Gnome session.
-  case $vnc in no) run bginit gnome-session ;; esac
+  case "$vnc,$(xfce4-session --version 2>&1),$(gnome-session --version 2>&1)"
+  in
+    no,xfce4-session*)
+      run bginit xfce4-session
+      ;;
+    no,*,gnome-session\ 2.3[2-9].* | \
+    no,*,gnome-session\ 2.4[0-9].* | \
+    no,*,gnome-session\ 2.[1-9][0-9][0-9]*)
+      run bginit gnome-session --session mdw
+      ;;
+    no,*,gnome-session*)
+      run bginit gnome-session
+      ;;
+  esac
 
   ## Local clients.
   start-clients-local
@@ -155,24 +216,44 @@ start-clients () {
 ### Main screen layout.
 
 ## Choose appropriate clients.
-emacs=$(pick_program emacs emacs23 emacs22 emacs21 emacs)
+emacs=$(pick_program emacs \
+  emacs24-lucid emacs23-lucid emacs24 emacs23 emacs22 emacs21 emacs)
 term=$(pick_program terminal pterm Eterm xterm)
 
 ## Emacs window measurements.
+##
+## e_colsz = width of a column in characters (from `emacs-width' metaconfig)
+## e_charwd = width of a character in pixels (assume `6x13')
+## e_colextra = additional per-column overhead in pixels
+## e_colextrachars = additional per-column overhead in character units
+## e_colwd = basic width of a column in pixels
+## e_hextra = extra horizontal width in pixels
+##     Width of an N-column Emacs frame in pixels will be
+##     N*e_colwd + e_hextra
+## e_colchars = width of a column in Emacs `-geometry' units
+## e_cextra = extra horizontal width in Emacs `-geometry' units
+##     So an N-column frame should be reported to Emacs as being
+##     N*e_colchars + e_cextra geometry units wide
+## e_lineht = height of a character line in pixels
+## e_vextra = number of additional vertical cruft pixels
+##     So an N-line Emacs frame takes N*e_lineht + e_vextra pixels
+e_colsz=$(mdw-conf emacs-width 77) e_charwd=6
+e_colextra=30 e_colextrachars=5 e_lineht=13
 case "$emacs" in
-  emacs21 | emacs)
-    e_colwd=492 e_hextra=34
-    e_colchars=82 e_cextra=-2
-    e_lineht=13 e_vextra=52
-    ;;
-  emacs22 | emacs23)
-    e_colwd=492 e_hextra=8
-    e_colchars=82 e_cextra=-6
-    e_lineht=13 e_vextra=46
-    ;;
+  emacs21 | emacs) e_hextra=34 e_cextra=-2 e_vextra=52 ;;
+  emacs22 | emacs23) e_hextra=8 e_cextra=-6 e_vextra=46 ;;
+  emacs24) e_hextra=5 e_cextra=-6 e_vextra=42 ;;
+  emacs23-lucid) e_hextra=7 e_cextra=-6 e_vextra=48 ;;
+  emacs24-lucid) e_hextra=7 e_cextra=-5 e_vextra=48 ;;
 esac
+e_colwd=$(( e_colsz*e_charwd + e_colextra ))
+e_colchars=$(( e_colsz + e_colextrachars ))
 
 ## Terminal window measurements.
+##
+## t_wd = the window width, in pixels
+## t_lineht, t_vextra = height parameters: if the window is N lines high,
+##     then it will be N*t_lineht + t_vextra pixels high
 case "$term" in
   pterm) t_wd=504 t_lineht=13 t_vextra=23 geom=-geometry;;
   Eterm) t_wd=504 t_lineht=13 t_vextra=23 geom=-g;;
@@ -180,31 +261,38 @@ case "$term" in
 esac
 
 ## GNOME stuff measurements.
-declare -i xbound="XWIDTH - 113"
+declare -i panelwd=64 xbound=$(( XWIDTH - panelwd ))
 
 ## Choose a width for Emacs.
 ##
-## We'd like it to be as wide as possible, allowing for a column of xterms
-## down the right hand side.  However, I'd prefer a double-width Emacs to a
-## single-width Emacs and xterms.  If it's not going to work at all, a single
-## Emacs column will have to do.  Also, there's a strange thing with Emacs21
-## and the toolbar, so we add on some rows which are later mysteriously
-## subtracted.
-
-declare -i ecols="(xbound - t_wd - e_hextra)/e_colwd"
-if (( ecols < 2 && xbound > e_colwd * 2 + e_hextra )); then
-  ecols=2
-elif (( ecols < 1 )); then
-  ecols=1
+## We're going to make a single frame of Emacs, which will be automatically
+## divvied into columns; the rest of the space will be tiled with xterms.  If
+## we have multiple screens, then Emacs can have the first screen and we'll
+## fill the rest with terminals.  Otherwise, we'll try to have N columns of
+## Emacs, and N - 1 columns of xterms; if that doesn't work, we'll make do
+## with N - 2 columns of xterms.
+##
+## So, let W be the available width; let E and T be the widths of an Emacs
+## column (e_colwd) and terminal (t_wd), respectively, and let E0 be the
+## additive width of Emacs's existence (e_hextra).  If we have more than one
+## screen, then instead, let T be zero.  To start out, then, we let N = 1 +
+## floor((W - E - E0)/(E + T)).  If W - E0 - (N + 1) E - (N - 1)*T >= 0, then
+## we increase N by one.
+declare -i lim=XSCR0_WIDTH
+if (( lim > xbound )); then lim=xbound; fi
+declare -i twd=t_wd; if (( XNSCR > 1 )); then twd=0; fi
+declare -i ecols=$(( (lim - e_colwd - e_hextra)/(e_colwd + twd) + 1 ))
+if (( lim - e_hextra - (ecols + 1)*e_colwd - (ecols - 1)*twd >= 0 )); then
+  ecols=$(( ecols + 1 ))
 fi
 
 declare -i \
-  emacsx="ecols * e_colchars + e_cextra" \
-  emacsy="(XHEIGHT - e_vextra)/e_lineht"
+  emacsx=$(( ecols*e_colchars + e_cextra )) \
+  emacsy=$(( (XHEIGHT - e_vextra)/e_lineht ))
 
 start-emacs () {
   GDK_NATIVE_WINDOWS=1 run bgclients noip \
-    $emacs -geometry ${emacsx}x${emacsy}+0+0
+    $emacs -bg black -geometry ${emacsx}x${emacsy}+${XSCR0_X}+${XSCR0_Y}
 }
 
 ## Now place some xterms.
@@ -220,36 +308,50 @@ start-emacs () {
 start-xterms () {
 
   ## Initialize some parameters.
-  declare -i x="ecols * e_colwd + e_hextra" xb=xbound
-  declare -i n=0 pgx=0 l h y ht
-  declare -i hstd="35 * t_lineht + t_vextra" hmin="25 * t_lineht + t_vextra"
+  declare -i x=$(( ecols*e_colwd + e_hextra + XSCR0_X )) xb=xbound
+  declare -i n=0 pgx=0 l h y ht scr=0 ll=lim
+  declare -i hstd=$(( 35*t_lineht + t_vextra ))
+  declare -i hmin=$(( 25*t_lineht + t_vextra ))
+  declare -i scrx scry scrwd scrht
+
+  eval "scrx=\$XSCR${scr}_X scry=\$XSCR${scr}_Y
+       scrwd=\$XSCR${scr}_WIDTH scrht=\$XSCR${scr}_HEIGHT"
 
   ## Do the placement.
   while :; do
 
     ## Start a new iteration.
-    if ((x + t_wd > xb)); then
-      if ((n >= 3)); then break; fi
-      x="pgx + XWIDTH" pgx="pgx + XWIDTH" xb="xb + XWIDTH"
+    if (( x + t_wd > ll )); then
+      scr=$(( scr + 1 ))
+      if (( scr >= XNSCR )); then
+       if (( n >= 3 )); then break; fi
+       pgx=$(( pgx + XWIDTH )) xb=$(( xb + XWIDTH ))
+       scr=0
+      fi
+      eval "scrx=\$XSCR${scr}_X scry=\$XSCR${scr}_Y
+           scrwd=\$XSCR${scr}_WIDTH scrht=\$XSCR${scr}_HEIGHT"
+      x=$(( pgx + scrx ))
+      ll=$(( x + scrwd ))
+      if (( ll > xb )); then ll=xb; fi
     fi
 
     ## Make large xterms.
-    y=0 ht=XHEIGHT
-    while ((ht - hstd >= hmin)); do
+    y=scry ht=scrht
+    while (( ht - hstd >= hmin )); do
       run bgclients $term $geom 80x35+$x+$y
-      y="y + hstd" ht="ht - hstd" n="n + 1"
+      y=$(( y + hstd )) ht=$(( ht - hstd )) n=$(( n + 1 ))
     done
 
     ## Fill the remaining space.
-    if ((ht >= 2 * hmin)); then h="ht - hmin"; else h=ht; fi
-    l="(h - t_vextra)/t_lineht" h="l * t_lineht + t_vextra"
+    if (( ht >= 2*hmin )); then h=$(( ht - hmin )); else h=ht; fi
+    l=$(( (h - t_vextra)/t_lineht )) h=$(( l*t_lineht + t_vextra ))
     run bgclients $term $geom 80x$l+$x+$y
-    y="y + h" ht="ht - h" n="n + 1"
+    y=$(( y + h )) ht=$(( ht - h )) n=$(( n + 1 ))
     if ((ht >= hmin)); then
       run bgclients $term $geom 80x25+$x+$y
-      n="n + 1"
+      n=$(( n + 1 ))
     fi
-    x="x + t_wd"
+    x=$(( x + t_wd ))
   done
 }
 
@@ -300,7 +402,7 @@ EOF
        exec "$0" wait nostart
        ;;
       :ask-run)
-       req "Shell command" .cmd.hist xcatch -FMiscFixed6x13 -- sh -c&
+       req "Shell command" .cmd.hist xcatch -F"Fixed 13" -- sh -c&
        ;;
       :ask-command)
        req "xinit command" .xinit.hist xatom set XINIT_COMMAND$atomtag&
@@ -319,6 +421,38 @@ EOF
   done
 }
 
+###--------------------------------------------------------------------------
+### Gnome session care and feeding.
+
+session-running-p () {
+  dbus-send --session --print-reply --dest=org.freedesktop.DBus / \
+    org.freedesktop.DBus.GetNameOwner string:org.gnome.SessionManager \
+    >/dev/null 2>&1
+}
+
+dbus-service-running-p () {
+  dbus-send >/dev/null 2>&1 --session --print-reply \
+    --dest=org.freedesktop.DBus / \
+    org.freedesktop.DBus.GetNameOwner string:$1
+}
+
+kill-gnome-session () {
+  win=nil
+  while read service object logout; do
+    if dbus-service-running-p $service; then win=t; break; fi
+  done <<EOF
+org.xfce.SessionManager /org/xfce/SessionManager org.xfce.Session.Manager.Shutdown uint32:1 boolean:false
+org.gnome.SessionManager /org/gnome/SessionManager org.gnome.SessionManager.Logout uint32:2
+EOF
+  case $win in nil) return ;; esac
+  info "killing session manager"
+  dbus-send --session --dest=$service $object $logout
+  for i in 1 2 3 4 5; do
+    sleep 1
+    if ! dbus-service-running-p $service; then break; fi
+  done
+}
+
 ###--------------------------------------------------------------------------
 ### Actually start things up.
 
@@ -343,6 +477,7 @@ esac
 case "$wait" in
   yes)
     xwait
+    kill-gnome-session
     ;;
   no)
     info "not waiting before exit"