chiark / gitweb /
copyright messages
[ypp-sc-tools.main.git] / pctb / pages.c
index 272e5cff6ab280d8e1dadafbbeff2b7a92b2554c..42dd5bc88413c35020674c3d67f9c70e722fb6b6 100644 (file)
 /*
-  */
+ * Interaction with the YPP client via X11
+ */
+/*
+ *  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.
+ */
+
+/*
+ * Only this file #includes the X11 headers, as they're quite
+ * pollutant of the namespace.
+ */
 
-#include "ocr.h"
+#include "structure.h"
 
 #include <X11/Xlib.h>
 #include <X11/extensions/XTest.h>
 #include <X11/keysym.h>
+#include <X11/Xutil.h>
+
+CanonImage *page_images[MAX_PAGES];
+int npages;
 
-#define NUM_PAGES 25
+char *ocean, *pirate;
 
+static XWindowAttributes attr;
+static Window id;
 static Display *disp;
+static struct timeval tv_startup;
+static unsigned wwidth, wheight;
+
+DEBUG_DEFINE_DEBUGF(pages)
+
+#define xassert(what)                                  \
+  ((what) ? (void)0 :                                  \
+   fatal("X11 operation unexpectedly failed."          \
+        " %s:%d: %s\n", __FILE__,__LINE__,#what))
 
 static KeyCode keycode(KeySym sym) {
   return XKeysymToKeycode(disp,sym);
 }
 
-static CanonImage *screenshot_now(Window id) {
-  char *cmd;
-  CanonImage *ci;
-  int r;
+void screenshot_startup(void) {
+  progress("starting...");
+  disp= XOpenDisplay(0);
+  if (!disp) fatal("Unable to open X11 display.");
+  sysassert(! gettimeofday(&tv_startup,0) );
+}
+
+/*---------- pager ----------*/
+
+typedef XImage Snapshot;
+
+static double last_input;
+static const double min_update_allowance= 0.25;
+
+static double timestamp(void) {
+  struct timeval tv;
   
-  r= asprintf(&cmd, "xwd -silent -id 0x%lx | xwdtopnm", (unsigned long)id);
-  eassert(r>=0);
-  FILE *f= popen(cmd,"r");  eassert(f);
-  ci= file_read_image(f);
-  r= fgetc(f);  eassert(r==EOF);  eassert(feof(f));
-  r= pclose(f);  eassert(r>=0);  eassert(WIFEXITED(r) && !WEXITSTATUS(r));
-  free(cmd);
-  return ci;
+  sysassert(! gettimeofday(&tv,0) );
+  double t= (tv.tv_sec - tv_startup.tv_sec) +
+            (tv.tv_usec - tv_startup.tv_usec) * 1e-6;
+  debugf("PAGING %f\n",t);
+  return t;
+}
+static void delay(double need_sleep) {
+  debugf("PAGING     delay %f\n",need_sleep);
+  sysassert(! usleep(need_sleep * 1e6) );
 }
 
-static void read_pages(Window id) {
-  int r;
-  XWindowAttributes attr;
-  int xpos,ypos, evbase,errbase,majver,minver;
-  unsigned width,height,bd,depth;
+static void sync_after_input(void) {
+  xassert( XSync(disp, False) );
+  last_input= timestamp();
+}
+
+static void translate_coords_toroot(int wx, int wy, int *rx, int *ry) {
+  Window dummy;
+  xassert( XTranslateCoordinates(disp, id,attr.root, wx,wy, rx,ry, &dummy) );
+}
+
+static void check_client_window_all_on_screen(void) {
+  Rect onroot;
+  unsigned rwidth, rheight;
   Window dummy;
+  unsigned bd, depth;
+  int rxpos, rypos;
+
+  xassert( XGetGeometry(disp,attr.root, &dummy, &rxpos,&rypos,
+                       &rwidth, &rheight,
+                       &bd,&depth) );
+  
+  translate_coords_toroot(0,0, &onroot.tl.x,&onroot.tl.y);
+  translate_coords_toroot(wwidth-1,wheight-1, &onroot.br.x,&onroot.br.y);
+  if (!(onroot.tl.x >= 0 &&
+       onroot.tl.y >= 0 &&
+       onroot.br.x < rwidth &&
+       onroot.br.y < rheight))
+    fatal("YPP client window is not entirely on the screen.");
+}
+
+static void check_not_disturbed(void) {
+  XEvent ev;
+  int r;
+  
+  for (;;) {
+    r= XCheckMaskEvent(disp, ~0, &ev);
+    if (r==False) return;
+
+    switch (ev.type) {
+    case VisibilityNotify:
+      if (ev.xvisibility.state != VisibilityUnobscured)
+       fatal("YPP client window has become obscured.");
+      break;
+    case ConfigureNotify:
+      check_client_window_all_on_screen();
+      break;
+    case FocusOut:
+      fatal("Focus left YPP client window.");
+      break;
+    case FocusIn:
+      warning("focus entered YPP client window ?!");
+      break;
+    default:
+      fatal("Received unexpected X11 event (type code %d)!", ev.type);
+    }
+  }
+}      
+
+static void send_key(KeySym sym) {
+  XTestFakeKeyEvent(disp, keycode(sym),1, 10);
+  XTestFakeKeyEvent(disp, keycode(sym),0, 10);
+}
+
+static void send_pgup_many(void) {
+  int i;
+  for (i=0; i<25; i++)
+    send_key(XK_Prior);
+  debugf("PAGING   PageUp x %d\n",i);
+  sync_after_input();
+}
+static void send_pgdown(void) {
+  send_key(XK_Next);
+  debugf("PAGING   PageDown\n");
+  sync_after_input();
+}
+
+static void free_snapshot(Snapshot **io) {
+  if (*io) XDestroyImage(*io);
+  *io= 0;
+}
+
+static void snapshot(Snapshot **output) {
+  free_snapshot(output);
+
+  debugf("PAGING   snapshot\n");
+
+  timestamp();
+  *output= XGetImage(disp,id, 0,0, wwidth,wheight, AllPlanes, ZPixmap);
+  timestamp();
+
+  check_not_disturbed();
+
+  debugf("PAGING   snapshot done.\n");
+}
+
+static int identical(const Snapshot *a, const Snapshot *b) {
+  if (!(a->width == b->width &&
+       a->height == b->height &&
+       a->bytes_per_line == b->bytes_per_line &&
+       a->format == b->format))
+    return 0;
+  return !memcmp(a->data, b->data, a->bytes_per_line * a->height);
+}
+
+static void wait_for_stability(Snapshot **output,
+                              const Snapshot *previously,
+                              void (*with_keypress)(void),
+                              const char *fmt, ...)
+     FMT(4,5);
+
+static void wait_for_stability(Snapshot **output,
+                              const Snapshot *previously,
+                              void (*with_keypress)(void),
+                              const char *fmt, ...) {
+  va_list al;
+  va_start(al,fmt);
+
+  Snapshot *last=0;
+  /* waits longer if we're going to return an image identical to previously
+   * if previously==0, all images are considered identical to it */
+
+  debugf("PAGING  wait_for_stability"
+         "  last_input=%f previously=%p\n",
+         last_input, previously);
+
+  char *doing;
+  sysassert( vasprintf(&doing,fmt,al) >=0);
+
+  progress("%s",doing);
+
+  for (;;) {
+    double at_snapshot= timestamp();
+    double need_sleep= min_update_allowance - (at_snapshot - last_input);
+    if (need_sleep > 0) { delay(need_sleep); continue; }
+
+    snapshot(output);
+
+    if (!with_keypress &&
+       !(previously && identical(*output,previously))) {
+      debugf("PAGING  wait_for_stability  simple\n");
+      break;
+    }
+
+    if (last && identical(*output,last)) {
+      debugf("PAGING  wait_for_stability  stabilised\n");
+      break;
+    }
+    
+    progress_spinner("%s",doing);
+
+    debugf("PAGING  wait_for_stability  retry\n");
+
+    free_snapshot(&last); last=*output; *output=0;
+
+    if (with_keypress)
+      with_keypress();
+
+    delay(0.5);
+  }
+
+  free_snapshot(&last);
+  free(doing);
+  debugf("PAGING  wait_for_stability done.\n");
+  va_end(al);
+}
+
+static void raise_and_get_details(void) {
+  int evbase,errbase,majver,minver;
+  int wxpos, wypos;
+  unsigned bd,depth;
+
+  progress("raising and checking YPP client window...");
+
+  debugf("PAGING raise_and_get_details\n");
+
+  int xtest= XTestQueryExtension(disp, &evbase,&errbase,&majver,&minver);
+  if (!xtest) fatal("X server does not support the XTEST extension.");
+
+  xassert( XRaiseWindow(disp, id) );
+  /* in case VisibilityNotify triggers right away before we have had a
+   * change to raise; to avoid falsely detecting lowering in that case */
   
-  disp= XOpenDisplay(0);  eassert(disp);
+  xassert( XSelectInput(disp, id,
+                       StructureNotifyMask|
+                       VisibilityChangeMask
+                       ) );
 
-  r= XTestQueryExtension(disp, &evbase,&errbase,&majver,&minver);
-  eassert(r==True);
+  xassert( XRaiseWindow(disp, id) );
+  /* in case the window was lowered between our Raise and our SelectInput;
+   * to avoid failing to detect that lowering */
 
-  r= XRaiseWindow(disp, id);  eassert(r);
+  xassert( XGetWindowAttributes(disp, id, &attr) );
+  xassert( XGetGeometry(disp,id, &attr.root,
+                       &wxpos,&wypos, &wwidth,&wheight,
+                       &bd,&depth) );
 
-  r= XGetWindowAttributes(disp, id, &attr);  eassert(r);
-  r= XGetGeometry(disp,id, &attr.root,&xpos,&ypos,&width,&height, &bd,&depth);
-  eassert(r);
+  if (!(wwidth >= 320 && wheight >= 320))
+    fatal("YPP client window is implausibly small?");
 
-  r= XTranslateCoordinates(disp, id,attr.root, 160,160, &xpos,&ypos,
-                          &dummy);
-  eassert(r);
+  check_client_window_all_on_screen();
+}
 
+static void set_focus(void) {
   int screen= XScreenNumberOfScreen(attr.screen);
-  XTestFakeMotionEvent(disp,screen, xpos, ypos, 0);
 
-  XTestFakeButtonEvent(disp,1,1, 0);
+  progress("taking control of YPP client window...");
+
+  debugf("PAGING set_focus\n");
+
+  int xpos, ypos;
+  translate_coords_toroot(160,160, &xpos,&ypos);
+  XTestFakeMotionEvent(disp,screen, xpos,ypos, 0);
+
+  XTestFakeButtonEvent(disp,1,1, 50);
   XTestFakeButtonEvent(disp,1,0, 50);
 
-  XTestFakeKeyEvent(disp, keycode(XK_Next),1, 50);
-  XTestFakeKeyEvent(disp, keycode(XK_Next),0, 50);
+  sync_after_input();
+
+  delay(0.5);
+  xassert( XSelectInput(disp, id,
+                       StructureNotifyMask|
+                       VisibilityChangeMask|
+                       FocusChangeMask
+                       ) );
+
+  translate_coords_toroot(10,10, &xpos,&ypos);
+  XTestFakeMotionEvent(disp,screen, xpos,ypos, 0);
+
+  sync_after_input();
+
+  debugf("PAGING raise_and_set_focus done.\n");
+}
+
+#define SAMPLEMASK 0xfful
+
+typedef struct {
+  int lshift, rshift;
+} ShMask;
+
+static void compute_shift_mask(ShMask *sm, unsigned long ximage_mask) {
+  sm->lshift= 0;
+  sm->rshift= 0;
   
-  r= XSync(disp, False);  eassert(r);
+  for (;;) {
+    if (ximage_mask <= (SAMPLEMASK>>1)) {
+      sm->lshift++;  ximage_mask <<= 1;
+    } else if (ximage_mask > SAMPLEMASK) {
+      sm->rshift++;  ximage_mask >>= 1;
+    } else {
+      break;
+    }
+    assert(!(sm->lshift && sm->rshift));
+  }
+  assert(sm->lshift < LONG_BIT);
+  assert(sm->rshift < LONG_BIT);
+}
+
+static CanonImage *convert_page(Snapshot *sn) {
+  ShMask shiftmasks[3];
+  CanonImage *im;
+
+  fprintf(screenshots_file,
+         "P6\n"
+         "%d %d\n"
+         "255\n", sn->width, sn->height);
+
+#define COMPUTE_SHIFT_MASK(ix, rgb) \
+  compute_shift_mask(&shiftmasks[ix], sn->rgb##_mask)
+  COMPUTE_SHIFT_MASK(0, red);
+  COMPUTE_SHIFT_MASK(1, green);
+  COMPUTE_SHIFT_MASK(2, blue);
+
+  CANONICALISE_IMAGE(im, sn->width, sn->height, {
+    long xrgb= XGetPixel(sn, x, y);
+    int i;
+    rgb= 0;
+    for (i=0; i<3; i++) {
+      rgb <<= 8;
+      unsigned long sample=
+       ((xrgb << shiftmasks[i].lshift)
+             >> shiftmasks[i].rshift) & SAMPLEMASK;
+      rgb |= sample;
+      fputc(sample, screenshots_file);
+    }
+  });
+
+  sysassert(!ferror(screenshots_file));
+  sysassert(!fflush(screenshots_file));
+
+  return im;
 }
 
-int main(int argc, char **argv) {
-  Window id;
-  CanonImage *ci;
+void take_screenshots(void) {
+  Snapshot *current=0, *last=0;
+  CanonImage *test;
+
+  /* find the window and check it's on the right kind of screen */
+  raise_and_get_details();
+  wait_for_stability(&current,0,0, "checking current YPP client screen...");
+  test= convert_page(current);
+  find_structure(test);
+  free(test);
+
+  /* page to the top - keep pressing page up until the image stops changing */
+  set_focus();
+  wait_for_stability(&current,0, send_pgup_many,
+                    "paging up to top of commodity list...");
+
+  /* now to actually page down */
+  for (;;) {
+    debugf("paging page %d\n",npages);
+
+    if (!(npages < MAX_PAGES))
+      fatal("Paging down seems to generate too many pages - max is %d.",
+           MAX_PAGES);
+    
+    page_images[npages]= convert_page(current);
+    free_snapshot(&last); last=current; current=0;
+
+    debugf("PAGING page %d converted\n",npages);
+
+    wait_for_stability(&current,last, 0,
+                      "collecting screenshot of page %d...",
+                      npages+1);
+
+    if (npages &&  /* first pagedown doesn't do much */
+       identical(current,last)) {
+      free_snapshot(&current);
+      break;
+    }
+
+    send_pgdown();
+    npages++;
+  }
+  debugf("PAGING all done.\n");
+  progress_log("collected %d screenshots.",npages);
+}    
+
+void take_one_screenshot(void) {
+  Snapshot *current=0;
+  
+  raise_and_get_details();
+  sync_after_input();
+  wait_for_stability(&current,0,0, "taking screenshot...");
+  page_images[0]= convert_page(current);
+  npages= 1;
+  progress_log("collected single screenshot.");
+}
+
+void set_yppclient_window(unsigned long wul) {
+  id= wul;
+}
+
+DEBUG_DEFINE_SOME_DEBUGF(findypp,debugfind)
+
+void find_yppclient_window(void) {
+  Window root, gotroot, gotparent;
+  int screen;
+  int nfound=0;
+  
+  if (id) return;
+  
+  progress("looking for YPP client window...");
+
+  static const char prefix[]= "Puzzle Pirates - ";
+  static const char onthe[]= " on the ";
+  static const char suffix[]= " ocean";
+#define S(x) (sizeof((x))-1)
+
+  Atom wm_name= XInternAtom(disp,"WM_NAME",True);
+  xassert(wm_name != None);
+
+  for (screen=0; screen<ScreenCount(disp); screen++) {
+    debugfind("FINDYPP screen %d\n", screen);
+    root= RootWindow(disp,screen);
+    unsigned int nchildren1;
+    Window *children1=0;
+
+    xassert( XQueryTree(disp,root,
+                 &gotroot,&gotparent,
+                 &children1,&nchildren1) );
+    debugfind("FINDYPP screen %d nchildren1=%d\n", screen, nchildren1);
+
+    int i;
+    for (i=0; i<nchildren1; i++) {
+      Window w1= children1[i];
+      unsigned int nchildren2;
+      Window *children2=0;
+
+      xassert( XQueryTree(disp,w1,
+                         &gotroot,&gotparent,
+                         &children2,&nchildren2) );
+      debugfind("FINDYPP screen %d c1[%2d]=0x%08lx nchildren2=%d\n",
+               screen, i, (unsigned long)w1, nchildren2);
+
+      int j;
+      for (j=-1; j<(int)nchildren2; j++) {
+       Window w2= j<0 ? w1 : children2[j];
+       debugfind("FINDYPP screen %d c1[%2d]=0x%08lx c2[%2d]=0x%08lx",
+                 screen, i, (unsigned long)w1, j, (unsigned long)w2);
+
+       int gotfmt;
+       Atom gottype;
+       unsigned long len, gotbytesafter;
+       char *title;
+       unsigned char *gottitle=0;
+       xassert( !XGetWindowProperty(disp,w2, wm_name,0,512, False,
+                                    AnyPropertyType,&gottype, &gotfmt, &len,
+                                    &gotbytesafter, &gottitle) );
+       title= (char*)gottitle;
+
+       if (DEBUGP(findypp)) {
+         debugfind(" gf=%d len=%lu gba=%lu \"", gotfmt,len,gotbytesafter);
+         char *p;
+         for (p=title; p < title+len; p++) {
+           char c= *p;
+           if (c>=' ' && c<=126) fputc(c,debug);
+           else fprintf(debug,"\\x%02x",c & 0xff);
+         }
+         fputs("\": ",debug);
+       }
+
+#define REQUIRE(pred)                                                     \
+        if (!(pred)) { debugfind(" failed test  %s\n", #pred); continue; } \
+        else
+
+       REQUIRE( gottype!=None );
+       REQUIRE( len );
+       REQUIRE( gotfmt==8 );
+
+       REQUIRE( len >= S(prefix) + 1 + S(onthe) + 1 + S(suffix) );
+
+       char *spc1= strchr(  title        + S(prefix), ' ');  REQUIRE(spc1);
+       char *spc2= strrchr((title + len) - S(suffix), ' ');  REQUIRE(spc2);
+
+       REQUIRE( (title + len) - spc1  >= S(onthe)  + S(suffix) );
+       REQUIRE(  spc2         - title >= S(prefix) + S(onthe) );
+
+       REQUIRE( !memcmp(title,                   prefix, S(prefix)) );
+       REQUIRE( !memcmp(title + len - S(suffix), suffix, S(suffix))  );
+       REQUIRE( !memcmp(spc1,                    onthe,  S(onthe))  );
+
+#define ASSIGN(what, start, end) do {                                  \
+       sysassert( asprintf(&what, "%.*s", (end)-(start), start) >0 );  \
+     }while(0)
+       ASSIGN(pirate, title + S(prefix),  spc1);
+       ASSIGN(ocean,  spc1 + S(onthe),   (title + len) - S(suffix));
 
-  id= strtoul(*++argv,0,0);
-  read_pages(id);
-  ci= screenshot_now(id);
-  return 0;
+       debugfind(" YES!\n");
+       id= w2;
+       nfound++;
+       progress_log("found YPP client (0x%lx):"
+                    " %s ocean - %s.",
+                    (unsigned long)id, ocean, pirate);
+      }
+      if (children2) XFree(children2);
+    }
+    if (children1) XFree(children1);
+  }
+  if (nfound>1)
+    fatal("Found several YPP clients."
+         " Close one, or specify the windowid with --window-id.\n");
+  if (nfound<1)
+    fatal("Did not find YPP client."
+         " Use --window-id and/or report this as a fault.\n");
 }