chiark / gitweb /
Check we're on the right page
[ypp-sc-tools.main.git] / pctb / pages.c
index 5978f74e365d5e62866422e0d3a59edb43b48d35..9b05fcd046ae5989f25f1c9695837b66e910e619 100644 (file)
 CanonImage *page_images[MAX_PAGES];
 int npages;
 
+static XWindowAttributes attr;
 static Window id;
 static Display *disp;
 static struct timeval tv_startup;
 static unsigned wwidth, wheight;
+static int wxpos, wypos;
 
 static KeyCode keycode(KeySym sym) {
   return XKeysymToKeycode(disp,sym);
@@ -184,14 +186,13 @@ static void wait_for_stability(Snapshot **output,
   fprintf(stderr,"PAGING  wait_for_stability done.\n");
 }
 
-static void raise_and_set_focus(void) {
+static void raise_and_get_details(void) {
   int r;
-  XWindowAttributes attr;
-  int xpos,ypos, evbase,errbase,majver,minver;
+  int evbase,errbase,majver,minver;
   unsigned bd,depth;
   Window dummy;
   
-  fprintf(stderr,"PAGING raise_and_set_focus\n");
+  fprintf(stderr,"PAGING raise_and_get_details\n");
 
   r= XTestQueryExtension(disp, &evbase,&errbase,&majver,&minver);
   eassert(r==True);
@@ -200,16 +201,21 @@ static void raise_and_set_focus(void) {
 
   r= XGetWindowAttributes(disp, id, &attr);  eassert(r);
   r= XGetGeometry(disp,id, &attr.root,
-                 &xpos,&ypos, &wwidth,&wheight,
+                 &wxpos,&wypos, &wwidth,&wheight,
                  &bd,&depth);
   eassert(r);
 
-  r= XTranslateCoordinates(disp, id,attr.root, 160,160, &xpos,&ypos,
+  r= XTranslateCoordinates(disp, id,attr.root, 160,160, &wxpos,&wypos,
                           &dummy);
   eassert(r);
+}
 
+static void set_focus(void) {
   int screen= XScreenNumberOfScreen(attr.screen);
-  XTestFakeMotionEvent(disp,screen, xpos, ypos, 0);
+
+  fprintf(stderr,"PAGING set_focus\n");
+
+  XTestFakeMotionEvent(disp,screen, wxpos,wypos, 0);
 
   XTestFakeButtonEvent(disp,1,1, 50);
   XTestFakeButtonEvent(disp,1,0, 50);
@@ -246,8 +252,7 @@ static void compute_shift_mask(ShMask *sm, int targshift,
   assert(sm->rshift < LONG_BIT);
 }
 
-static void store_page(int pageno, Snapshot *sn) {
-  eassert(pageno < MAX_PAGES);
+static CanonImage *convert_page(Snapshot *sn) {
   ShMask shiftmasks[3];
   CanonImage *im;
 
@@ -265,23 +270,33 @@ static void store_page(int pageno, Snapshot *sn) {
       rgb |= ((xrgb << shiftmasks[i].lshift)
              >> shiftmasks[i].rshift) & shiftmasks[i].mask;
   });
-  
-  page_images[pageno]= im;
+
+  return im;
 }
 
 static void read_pages(void) {
   Snapshot *current=0, *last=0;
+  CanonImage *test;
 
-  raise_and_set_focus();
+  /* find the window and check it's on the right kind of screen */
+  raise_and_get_details();
+  snapshot(&current);
+  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);
 
   /* now to actually page down */
   for (;;) {
-    fprintf(stderr,"PAGING page %d\n",npages);
-    store_page(npages, current);
+    fprintf(stderr,"paging page %d\n",npages);
+
+    eassert(npages < MAX_PAGES);
+    page_images[npages]= convert_page(current);
     free_snapshot(&last); last=current; current=0;
+
     fprintf(stderr,"PAGING page %d converted\n",npages);
 
     wait_for_stability(&current,last, 0);