chiark / gitweb /
works again
[chiark-tcl-applet.git] / example
diff --git a/example b/example
index 4a6a16a2e886bbacfb88d9d6096f6383a97e1d27..3b45d0bd19e76c23033e9ccb4fa7a89f1258f95d 100755 (executable)
--- a/example
+++ b/example
@@ -16,13 +16,73 @@ image create bitmap ims -file gs_s.xbm
 setuptooltip
 settooltip "line\nanother"
 
-proc innerwindow {id} {
-    puts IICONFIG
-    puts "IC $id"
+fconfigure stdout -buffering line
 
-    .i.i.b configure -background darkblue -bd 1
-exec /home/ian/things/Chiark-utils/chiark-utils.git/cprogs/xacpi-simple \
-       -into $id &
+set status none
+
+proc innerwindow-destroying {} {
+    global status
+    puts "DESTROYING $status"
+    switch -exact $status {
+       none { }
+       old { }
+       default { kill $status; set status old }
+    }
+}
+
+proc innerwindow-ready {} {
+    global status
+    puts "READY $status"
+    switch -exact $status {
+       none {
+           run-child
+       }
+       old {
+           # wait for it to die
+       }
+       default {
+           error "unexpected state $status"
+       }
+    }
+    puts "READY-done $status"
+}
+
+set ratelimit 0
+
+proc run-child {} {
+    global status ratelimit
+
+    puts "RUN-CHILD $status"
+    set now [clock seconds]
+    lappend ratelimit $now
+    while {[lindex $ratelimit 0] < {$now - 10}} {
+       set ratelimit [lrange $ratelimit 1 end]
+    }
+    if {[llength $ratelimit] > 10} {
+       puts stderr "crashing repeatedly, quitting $ratelimit"
+       exit 127
+    }
+
+    set status none
+    set status [fork-then child-died {
+       execl xacpi-simple [list -into [winfo id .i.i.b.c]]
+    }]
+    puts "FORKED $status"
+}
+
+proc child-died {how how2} {
+    puts "DIED $how $how2"
+    global status
+    switch -exact $status {
+       old {
+           set status none
+           run-child
+       }
+       default {
+           set status none
+           innerwindow-resetup-required "child died"
+       }
+    }
 }
 
 setupinnerwindow 40