dot/xinitrc: Document the various terminal and Emacs size parameters.
[profile] / dot / xinitrc
index 1ba8b2c..9b4ab28 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
@@ -71,6 +98,13 @@ EOF
 done
 
 ###--------------------------------------------------------------------------
+### Preliminary hook.
+
+if [ -r $HOME/.xinitrc-prehook ]; then
+  . $HOME/.xinitrc-prehook
+fi
+
+###--------------------------------------------------------------------------
 ### Iniitial settings.
 
 ## Assume X sessions are secure.
@@ -87,21 +121,21 @@ initialize () {
   run init xrdb -override $HOME/.Xdefaults
 
   ## Random xsettery.
-  run init xset b 10 2000 50
+  run init xset b 5 2000 50
   run init xset r rate 500 50
   run init xset m 2 1
 
   ## Key mappings.
-  xmodmap -e 'keysym BackSpace = BackSpace BackSpace'
-
-  ## Gnome settings.
-  case $vnc in no) run bginit gnome-settings-daemon ;; esac
+  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)
@@ -109,8 +143,36 @@ case "$wm,$vnc" in
     ;;
 esac
 
+start-e16 () {
+  run bginit manage $wm $wmopts
+  win=nil
+  for i in $(seq 10); do
+    sleep 1
+    if eesh version >/dev/null 2>&1; then
+      win=t
+      break
+    fi
+  done
+  case $win in
+    t)
+      info "$wm started ok"
+      run init xsetroot -cursor_name left_ptr
+      ;;
+    nil)
+      info "$wm failed to start!"
+      ;;
+  esac
+}
+
 start-window-manager () {
-  run bginit $wm $wmopts
+  case $(type -t start-$wm || echo "not-found") in
+    function)
+      start-$wm $wmopts
+      ;;
+    *)
+      run bginit manage $wm $wmopts
+      ;;
+  esac
 }
 
 ###--------------------------------------------------------------------------
@@ -119,26 +181,23 @@ start-window-manager () {
 start-clients-local () { :; }
 
 start-clients () {
-  ## Mail notification.
-  run bginit mail-notification
-
-  ## System monitor.
-  case $vnc in no) run bginit gkrellm ;; esac
 
-  ## Screensaver.
-  case $vnc in
-    no)
-      run init xscreensaver-command -exit
-      run bginit xscreensaver -no-splash
+  ## Gnome session.
+  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
 
-  ## Panel.
-  case $vnc in no) run bginit gnome-panel ;; esac
-
-  ## System tray.
-  ## run bginit stalonetray
-
   ## Local clients.
   start-clients-local
 }
@@ -147,10 +206,22 @@ start-clients () {
 ### Main screen layout.
 
 ## Choose appropriate clients.
-emacs=$(pick_program emacs emacs23 emacs22 emacs21 emacs)
+emacs=$(pick_program emacs emacs23 emacs24 emacs22 emacs21 emacs)
 term=$(pick_program terminal pterm Eterm xterm)
 
 ## Emacs window measurements.
+##
+## 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
 case "$emacs" in
   emacs21 | emacs)
     e_colwd=492 e_hextra=34
@@ -162,9 +233,18 @@ case "$emacs" in
     e_colchars=82 e_cextra=-6
     e_lineht=13 e_vextra=46
     ;;
+  emacs24)
+    e_colwd=492 e_hextra=5
+    e_colchars=82 e_cextra=-6
+    e_lineht=13 e_vextra=42
+    ;;
 esac
 
 ## 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;;
@@ -196,7 +276,7 @@ declare -i \
 
 start-emacs () {
   GDK_NATIVE_WINDOWS=1 run bgclients noip \
-    $emacs -geometry ${emacsx}x${emacsy}+0+0
+    $emacs -bg black -geometry ${emacsx}x${emacsy}+0+0
 }
 
 ## Now place some xterms.
@@ -292,7 +372,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&
@@ -304,7 +384,7 @@ EOF
        eval "${line#!}"
        ;;
       *)
-        set -- $line
+       set -- $line
        run bgclients "$@"
        ;;
     esac
@@ -312,6 +392,38 @@ EOF
 }
 
 ###--------------------------------------------------------------------------
+### 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.
 
 if [ -f $HOME/.xinitrc-local ]; then
@@ -335,6 +447,7 @@ esac
 case "$wait" in
   yes)
     xwait
+    kill-gnome-session
     ;;
   no)
     info "not waiting before exit"