Merge branch 'master' of git.distorted.org.uk:~mdw/publish/public-git/profile
[profile] / dot / xinitrc
index 9875cad..87e5bbd 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
@@ -117,7 +144,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
@@ -143,7 +170,7 @@ start-window-manager () {
       start-$wm $wmopts
       ;;
     *)
-      run bginit $wm $wmopts
+      run bginit manage $wm $wmopts
       ;;
   esac
 }
@@ -156,13 +183,17 @@ start-clients-local () { :; }
 start-clients () {
 
   ## Gnome session.
-  case "$vnc,$(gnome-session --version 2>&1)" in
-    no,gnome-session\ 2.3[2-9].* | \
-    no,gnome-session\ 2.4[0-9].* | \
-    no,gnome-session\ 2.[1-9][0-9][0-9]*)
+  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,*)
+    no,*,gnome-session*)
       run bginit gnome-session
       ;;
   esac
@@ -224,7 +255,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.
@@ -320,7 +351,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&
@@ -348,17 +379,27 @@ session-running-p () {
     >/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 () {
-  if session-running-p; then
-    info "killing Gnome session manager"
-    dbus-send --session \
-      --dest=org.gnome.SessionManager /org/gnome/SessionManager \
-      org.gnome.SessionManager.Logout uint32:2
-    for i in 1 2 3 4 5; do
-      sleep 1
-      if ! session-running-p; then break; fi
-    done
-  fi
+  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
 }
 
 ###--------------------------------------------------------------------------