chiark / gitweb /
WIP island determination; ocr resolver can make query to user
authorIan Jackson <ian@liberator.relativity.greenend.org.uk>
Sun, 14 Jun 2009 23:53:47 +0000 (00:53 +0100)
committerIan Jackson <ian@liberator.relativity.greenend.org.uk>
Sun, 14 Jun 2009 23:53:47 +0000 (00:53 +0100)
pctb/resolve.c [new file with mode: 0644]
pctb/yppsc-ocr-resolver
pctb/yppsc-resolver-pixoptions [new file with mode: 0755]

diff --git a/pctb/resolve.c b/pctb/resolve.c
new file mode 100644 (file)
index 0000000..052ec32
--- /dev/null
@@ -0,0 +1,89 @@
+/*
+ * IPC to external resolver program
+ */
+/*
+ *  This is part of ypp-sc-tools, a set of third-party tools for assisting
+ *  players of Yohoho Puzzle Pirates.
+ * 
+ *  Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
+ * 
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ * 
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ * 
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ * 
+ *  Yohoho and Puzzle Pirates are probably trademarks of Three Rings and
+ *  are used without permission.  This program is not endorsed or
+ *  sponsored by Three Rings.
+ */
+
+#include "ocr.h"
+
+static FILE *resolver;
+static pid_t resolver_pid;
+static int resolver_done;
+
+FILE *resolve_start(void) {
+  int jobpipe[2],donepipe[2];
+
+  if (!o_resolver) return 0;
+
+  if (!resolver) {
+    sysassert(! pipe(jobpipe) );
+    sysassert(! pipe(donepipe) );
+    resolver_pid= fork();
+    sysassert(resolver_pid!=-1);
+    if (!resolver_pid) {
+      sysassert( dup2(jobpipe[0],0) ==0 );
+      sysassert(! close(jobpipe[1]) );
+      sysassert(! close(donepipe[0]) );
+      /* we know donepipe[1] is >= 4 and we have dealt with all the others
+       * so we aren't in any danger of overwriting some other fd 4: */
+      sysassert( dup2(donepipe[1],4) ==4 );
+      execlp(o_resolver, o_resolver,
+            DEBUGP(callout) ? "--debug" : "--noop-arg",
+            "--automatic-1",
+            (char*)0);
+      sysassert(!"execlp ocr-resolver failed");
+    }
+    sysassert(! close(jobpipe[0]) );
+    sysassert(! close(donepipe[1]) );
+    resolver= fdopen(jobpipe[1],"w"); sysassert(resolver);
+    resolver_done= donepipe[0];
+  }
+  return resolver;
+}
+
+void resolve_finish(void) {
+  char cb;
+  
+  sysassert(!ferror(resolver));
+  sysassert(!fflush(resolver));
+
+  sysassert(resolver);
+
+  int r;
+  for (;;) {
+    r= read(resolver_done,&cb,1);
+    if (r==-1) { sysassert(errno==EINTR); continue; }
+    break;
+  }
+
+  if (r==0) {
+    waitpid_check_exitstatus(resolver_pid, "character resolver");
+    fclose(resolver);
+    close(resolver_done);
+    resolver= 0;
+  } else {
+    assert(r==1);
+    sysassert(cb==0);
+  }
+}
index ec4473d2205ed19f7c114ad81775701490266916..6ff436b12858d831efd182d3515f7145f870b370 100755 (executable)
@@ -82,8 +82,12 @@ static unsigned char csr_bits[] = {
 
     frame .d.mi.csr_0 -bg white -width 1
     frame .d.mi.csr_1 -bg white -width 1
+    frame .d.pe
+    frame .d.pe.grid
+    button .d.pe.ok -text OK
+    pack .d.pe.grid .d.pe.ok -side left
 
-    pack .d.csr .d.mi .d.got .d.ctx -side top
+    pack .d.mi .d.got .d.ctx -side top
     pack .d
 
     frame .help
@@ -101,26 +105,15 @@ proc show_context {maxhv x ctxs} {
     if {$wh > $maxh} { set maxh $wh }
 }
 
-proc resize_widgets {} {
+proc resize_widgets_core {} {
     global mulcols mulrows csrh gotsh ctxh glyphsdone
     global unk_l unk_contexts
     
     foreach w {.d.csr .d.got .d.ctx} {
        $w configure -width $mulcols
     }
-    #.d configure -height [expr {$csrh+$mulrows+$gotsh+$ctxh}]
-    foreach w {0 1} {
-       .d.mi.csr_$w configure -height $mulrows
-    }
 
     eval destroy [winfo children .d.ctx]
-
-    set maxh 0
-    foreach {min max contexts got} $glyphsdone {
-       show_context maxh $min $contexts
-    }
-    show_context maxh $unk_l $unk_contexts
-    .d.ctx configure -height $maxh
 }
 
 
@@ -260,7 +253,6 @@ proc startup_cursor {} {
 
     set cur_0 $unk_l
     set cur_1 [expr {$unk_r+1}]
-    set last_ht {}
 
     recursor
 }
@@ -268,6 +260,8 @@ proc startup_cursor {} {
 
 #---------- runtime display and keystroke handling ----------
 
+set last_ht {}
+
 proc helptext {t} {
     global last_ht
     if {![string compare $t $last_ht]} return
@@ -525,26 +519,6 @@ proc RETURN_RESULT {how what} {
 proc main/default {} {
     puts stderr "Do not run this program directly."
     exit 12
-    
-    global glyphsdone unk_l unk_r unk_contexts
-
-    set glyphsdone {
-       7 11 1 M
-       13 17 0 a
-       19 23 0 n
-    }
-    set unk_l 25
-    set unk_r 29
-    set unk_contexts Test
-
-    set f [open text.xpm]
-    read_xpm $f
-    close $f
-
-    read_database
-    resize_widgets
-    draw_glyphsdone
-    startup_cursor
 }
 proc done/default {} {
 }
@@ -561,13 +535,97 @@ proc required {} {
     required/$l
 }
 
+
+proc foreach_pixmap_col {var body} {
+    global alloptions
+    upvar 1 $var col
+    for {set col 0} {$col < [llength $alloptions]/3} {incr col} {
+       uplevel 1 $body
+    }
+}
+
+proc pixmap_select {ncol} {
+    global alloptions
+    debug "PIX SELECT $ncol [llength $alloptions]"
+    foreach_pixmap_col col {
+       if {$col==$ncol} continue
+       .d.pe.grid.l$col selection clear 0 end
+    }
+    pixmap_maybe_ok
+}
+proc pixmap_maybe_ok {} {
+    global alloptions pixmap_selcol pixmap_selrow
+    set nsel 0
+    foreach_pixmap_col col {
+       set cs [.d.pe.grid.l$col curselection]
+       incr nsel [llength $cs]
+       set pixmap_selcol $col
+       set pixmap_selrow [lindex $cs 0]
+    }
+    if {$nsel==1} {
+       .d.pe.ok configure -state normal -command pixmap_ok
+    } else {
+       .d.pe.ok configure -state disabled -command {}
+    }
+}
+proc pixmap_ok {} {
+    foreach_pixmap_col col {
+       .d.pe.grid.l$col configure -state disabled
+    }
+    .d.pe.ok configure -state disabled
+    helptext {{{ Processing }}}
+}
+
 proc required/pixmap {} {
-    global unk_what
+    global unk_what ppm mulcols alloptions
     must_gets stdin unk_what
-    error nyi
+    debug "GOT pixmap $unk_what"
+    set ppm {}
+    while 1 {
+       must_gets stdin ppml
+       if {![string length $ppml]} break
+       append ppm $ppml "\n"
+    }
+    set data [exec pnmscale 2 << $ppm]
+    image create photo image/main -data $data
+
+    set alloptions [exec ./yppsc-resolver-pixoptions $unk_what]
+
+    #read_database_pixmaps
+
+    set mulcols [image width image/main]
+    set mulrows [image height image/main]
+    resize_widgets_core
+    place forget .d.mi.csr_0
+    place forget .d.mi.csr_1
+    .d.ctx configure -height 0
+    pack forget .d.csr
+    pack .d.pe -side top -before .d.mi -pady 10
+
+    eval destroy [winfo children .d.pe.grid]
+    set col 0; foreach {colname coldesc rows} $alloptions {
+       debug "INIT $col $colname \"$coldesc\""
+       label .d.pe.grid.t$col -text $colname
+       listbox .d.pe.grid.l$col
+       foreach {rowname rowdesc} $rows {
+           debug "INIT $col $colname \"$coldesc\" $rowname \"$rowdesc\""
+           .d.pe.grid.l$col insert end $rowdesc
+       }
+       bind .d.pe.grid.l$col <<ListboxSelect>> [list pixmap_select $col]
+       grid .d.pe.grid.t$col -column $col -row 0
+       grid .d.pe.grid.l$col -column $col -row 1
+       incr col
+    }
+    pixmap_maybe_ok
+    
+    helptext {
+       {{Indicate the correct parse of this image, and click OK.}}
+    }
 }
 
 proc required/char {} {
+    global mulrows
+    
     must_gets stdin l
 
     manyset [lrange $l 0 3] unk_l unk_r unk_contexts
@@ -578,7 +636,20 @@ proc required/char {} {
     fconfigure stdin -blocking yes
 
     read_xpm stdin
-    resize_widgets
+
+    resize_widgets_core
+    foreach w {0 1} {
+       .d.mi.csr_$w configure -height $mulrows
+    }
+    set maxh 0
+    foreach {min max contexts got} $glyphsdone {
+       show_context maxh $min $contexts
+    }
+    show_context maxh $unk_l $unk_contexts
+    .d.ctx configure -height $maxh
+    pack forget .d.pe
+    pack .d.csr -side top -before .d.mi
+
     read_database
     draw_glyphsdone
     startup_cursor
diff --git a/pctb/yppsc-resolver-pixoptions b/pctb/yppsc-resolver-pixoptions
new file mode 100755 (executable)
index 0000000..260c5a2
--- /dev/null
@@ -0,0 +1,52 @@
+#!/usr/bin/perl -w
+
+# helper program for determining pixmap resolution options
+
+# This is part of ypp-sc-tools, a set of third-party tools for assisting
+# players of Yohoho Puzzle Pirates.
+#
+# Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+# Yohoho and Puzzle Pirates are probably trademarks of Three Rings and
+# are used without permission.  This program is not endorsed or
+# sponsored by Three Rings.
+
+@ARGV==1 or die "You probably don't want to run this program directly.\n";
+our ($which) = shift @ARGV;
+
+$which =~ s/\W//g;
+
+&{"main__$which"}();
+
+sub main__sunshinewidget () {
+    print <<END
+Land {On land} {
+    Crew   Crew
+    Shoppe Shoppe
+    Ye     Ye
+    Booty  Booty
+    Ahoy!  Ahoy!
+}
+Ship {On board a ship} {
+    Crew   Crew
+    Vessel Vessel
+    Ye     Ye
+    Booty  Booty
+    Ahoy!  Ahoy!
+}
+END
+    or die $!;
+}