chiark / gitweb /
Fix page number printing bug
[ypp-sc-tools.db-live.git] / pctb / pages.c
1 /*
2  * Interaction with the YPP client via X11
3  */
4 /*
5  *  This is part of ypp-sc-tools, a set of third-party tools for assisting
6  *  players of Yohoho Puzzle Pirates.
7  * 
8  *  Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
9  * 
10  *  This program is free software: you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation, either version 3 of the License, or
13  *  (at your option) any later version.
14  * 
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  * 
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  * 
23  *  Yohoho and Puzzle Pirates are probably trademarks of Three Rings and
24  *  are used without permission.  This program is not endorsed or
25  *  sponsored by Three Rings.
26  */
27
28 /*
29  * Only this file #includes the X11 headers, as they're quite
30  * pollutant of the namespace.
31  */
32
33 #include "structure.h"
34
35 #include <X11/Xlib.h>
36 #include <X11/extensions/XTest.h>
37 #include <X11/keysym.h>
38 #include <X11/Xutil.h>
39
40 #include <X11/extensions/XShm.h>
41 #include <sys/ipc.h>
42 #include <sys/shm.h>
43
44 CanonImage *page_images[MAX_PAGES];
45 int npages;
46 RgbImage *page0_rgbimage;
47
48 const char *ocean, *pirate;
49
50 static XWindowAttributes attr;
51 static Window id;
52 static Display *disp;
53 static struct timeval tv_startup;
54 static unsigned wwidth, wheight;
55 static int max_relevant_y= -1;
56
57 static XImage *shmim;
58 static XShmSegmentInfo shminfo;
59
60 DEBUG_DEFINE_DEBUGF(pages)
61
62 #define xassert(what)                                   \
63   ((what) ? (void)0 :                                   \
64    fatal("X11 operation unexpectedly failed."           \
65          " %s:%d: %s\n", __FILE__,__LINE__,#what))
66
67 static KeyCode keycode(KeySym sym) {
68   return XKeysymToKeycode(disp,sym);
69 }
70
71 void screenshot_startup(void) {
72   progress("starting...");
73   disp= XOpenDisplay(0);
74   if (!disp) fatal("Unable to open X11 display.");
75   sysassert(! gettimeofday(&tv_startup,0) );
76 }
77
78 /*---------- pager ----------*/
79
80 typedef RgbImage Snapshot;
81
82 static double last_input;
83 static const double min_update_allowance= 0.25;
84
85 static double timestamp(void) {
86   struct timeval tv;
87   
88   sysassert(! gettimeofday(&tv,0) );
89   double t= (tv.tv_sec - tv_startup.tv_sec) +
90             (tv.tv_usec - tv_startup.tv_usec) * 1e-6;
91   debugf("PAGING %f\n",t);
92   return t;
93 }
94 static void delay(double need_sleep) {
95   debugf("PAGING     delay %f\n",need_sleep);
96   sysassert(! usleep(need_sleep * 1e6) );
97 }
98
99 static void sync_after_input(void) {
100   xassert( XSync(disp, False) );
101   last_input= timestamp();
102 }
103
104 static void translate_coords_toroot(int wx, int wy, int *rx, int *ry) {
105   Window dummy;
106   xassert( XTranslateCoordinates(disp, id,attr.root, wx,wy, rx,ry, &dummy) );
107 }
108
109 static void check_client_window_all_on_screen(void) {
110   Rect onroot;
111   unsigned rwidth, rheight;
112   Window dummy;
113   unsigned bd, depth;
114   int rxpos, rypos;
115
116   xassert( XGetGeometry(disp,attr.root, &dummy, &rxpos,&rypos,
117                         &rwidth, &rheight,
118                         &bd,&depth) );
119   
120   translate_coords_toroot(0,0, &onroot.tl.x,&onroot.tl.y);
121   translate_coords_toroot(wwidth-1,wheight-1, &onroot.br.x,&onroot.br.y);
122   if (!(onroot.tl.x >= 0 &&
123         onroot.tl.y >= 0 &&
124         onroot.br.x < rwidth &&
125         onroot.br.y < rheight))
126     fatal("YPP client window is not entirely on the screen.");
127 }
128
129 static void check_not_disturbed(void) {
130   XEvent ev;
131   int r;
132   
133   for (;;) {
134     r= XCheckMaskEvent(disp, ~0, &ev);
135     if (r==False) return;
136
137     switch (ev.type) {
138     case VisibilityNotify:
139       if (ev.xvisibility.state != VisibilityUnobscured)
140         fatal("YPP client window has become obscured.");
141       break;
142     case ConfigureNotify:
143       check_client_window_all_on_screen();
144       break;
145     case FocusOut:
146       fatal("Focus left YPP client window.");
147       break;
148     case FocusIn:
149       warning("focus entered YPP client window ?!");
150       break;
151     default:
152       fatal("Received unexpected X11 event (type code %d)!", ev.type);
153     }
154   }
155 }      
156
157 static void send_key(KeySym sym) {
158   check_not_disturbed();
159   XTestFakeKeyEvent(disp, keycode(sym),1, 10);
160   XTestFakeKeyEvent(disp, keycode(sym),0, 10);
161 }
162 static void mouse_1_updown_here(void) {
163   check_not_disturbed();
164   XTestFakeButtonEvent(disp,1,1, 50);
165   XTestFakeButtonEvent(disp,1,0, 50);
166 }
167 static void mouse_1_updown(int x, int y) {
168   check_not_disturbed();
169   int screen= XScreenNumberOfScreen(attr.screen);
170   int xpos, ypos;
171   translate_coords_toroot(x,y, &xpos,&ypos);
172   XTestFakeMotionEvent(disp, screen, xpos,ypos, 0);
173   mouse_1_updown_here();
174 }
175
176 static int pgupdown;
177
178 static void send_pgup_many(void) {
179   int i;
180   for (i=0; i<25; i++) {
181     send_key(XK_Prior);
182     pgupdown--;
183   }
184   debugf("PAGING   PageUp x %d\n",i);
185   sync_after_input();
186 }
187 static void send_pgdown(void) {
188   send_key(XK_Next);
189   pgupdown++;
190   debugf("PAGING   PageDown\n");
191   sync_after_input();
192 }
193 static void send_pgdown_torestore(void) {
194   debugf("PAGING   PageDown x %d\n", -pgupdown);
195   while (pgupdown < 0) {
196     send_key(XK_Next);
197     pgupdown++;
198   }
199   sync_after_input();
200 }
201
202 static void free_snapshot(Snapshot **io) {
203   free(*io);
204   *io= 0;
205 }
206
207 #define SAMPLEMASK 0xfful
208
209 typedef struct {
210   int lshift, rshift;
211 } ShMask;
212
213 static void compute_shift_mask(ShMask *sm, unsigned long ximage_mask) {
214   sm->lshift= 0;
215   sm->rshift= 0;
216   
217   for (;;) {
218     if (ximage_mask <= (SAMPLEMASK>>1)) {
219       sm->lshift++;  ximage_mask <<= 1;
220     } else if (ximage_mask > SAMPLEMASK) {
221       sm->rshift++;  ximage_mask >>= 1;
222     } else {
223       break;
224     }
225     assert(!(sm->lshift && sm->rshift));
226   }
227   assert(sm->lshift < LONG_BIT);
228   assert(sm->rshift < LONG_BIT);
229 }
230
231 static void rtimestamp(double *t, const char *wh) {
232   double n= timestamp();
233   debugf("PAGING                INTERVAL %f  %s\n", n-*t, wh);
234   *t= n;
235 }
236
237 static void snapshot(Snapshot **output) {
238   XImage *im_use, *im_free=0;
239
240   ShMask shiftmasks[3];
241
242   debugf("PAGING   snapshot\n");
243
244   double begin= timestamp();
245   if (shmim) {
246     xassert( XShmGetImage(disp,id,shmim, 0,0, AllPlanes) );
247     rtimestamp(&begin, "XShmGetImage");
248
249     size_t dsz= shmim->bytes_per_line * shmim->height;
250     im_use= im_free= mmalloc(sizeof(*im_use) + dsz);
251     *im_free= *shmim;
252     im_free->data= (void*)(im_free+1);
253     memcpy(im_free->data, shmim->data, dsz);
254     rtimestamp(&begin, "mmalloc/memcpy");
255   } else {
256     xassert( im_use= im_free=
257              XGetImage(disp,id, 0,0, wwidth,wheight, AllPlanes, ZPixmap) );
258     rtimestamp(&begin, "XGetImage");
259   }
260
261 #define COMPUTE_SHIFT_MASK(ix, rgb) \
262   compute_shift_mask(&shiftmasks[ix], im_use->rgb##_mask)
263   COMPUTE_SHIFT_MASK(0, red);
264   COMPUTE_SHIFT_MASK(1, green);
265   COMPUTE_SHIFT_MASK(2, blue);
266   
267   if (!*output)
268     *output= alloc_rgb_image(wwidth, wheight);
269
270   rtimestamp(&begin, "compute_shift_masks+alloc_rgb_image");
271
272   int x,y,i;
273   unsigned char *op= (*output)->data;
274   for (y=0; y<wheight; y++) {
275     for (x=0; x<wwidth; x++) {
276       long xrgb= XGetPixel(im_use,x,y);
277       for (i=0; i<3; i++) {
278         unsigned long sample=
279           ((xrgb << shiftmasks[i].lshift) >> shiftmasks[i].rshift)
280           & SAMPLEMASK;
281         *op++= sample;
282       }
283     }
284   }
285
286   rtimestamp(&begin,"w*h*XGetPixel");
287   if (im_free)
288     XDestroyImage(im_free);
289   
290   rtimestamp(&begin,"XDestroyImage");
291   check_not_disturbed();
292
293   debugf("PAGING   snapshot done.\n");
294 }
295
296 static int identical(const Snapshot *a, const Snapshot *b) {
297   if (!(a->w == b->w &&
298         a->h == b->h))
299     return 0;
300
301   int compare_to= a->h;
302   if (max_relevant_y && compare_to > max_relevant_y)
303     compare_to= max_relevant_y;
304   
305   return !memcmp(a->data, b->data, a->w * 3 * compare_to);
306 }
307
308 static void wait_for_stability(Snapshot **output,
309                                const Snapshot *previously,
310                                void (*with_keypress)(void),
311                                const char *fmt, ...)
312      FMT(4,5);
313
314 static void wait_for_stability(Snapshot **output,
315                                const Snapshot *previously,
316                                void (*with_keypress)(void),
317                                const char *fmt, ...) {
318   va_list al;
319   va_start(al,fmt);
320
321   Snapshot *last=0;
322   /* waits longer if we're going to return an image identical to previously
323    * if previously==0, all images are considered identical to it */
324
325   char *doing;
326   sysassert( vasprintf(&doing,fmt,al) >=0 );
327   progress("%s",doing);
328
329   debugf("PAGING  wait_for_stability"
330           "  last_input=%f previously=%p `%s'\n",
331           last_input, previously, doing);
332
333   for (;;) {
334     double at_snapshot= timestamp();
335     double need_sleep= min_update_allowance - (at_snapshot - last_input);
336     if (need_sleep > 0) { delay(need_sleep); continue; }
337
338     snapshot(output);
339
340     if (!with_keypress &&
341         !(previously && identical(*output,previously))) {
342       debugf("PAGING  wait_for_stability  simple\n");
343       break;
344     }
345
346     if (last && identical(*output,last)) {
347       debugf("PAGING  wait_for_stability  stabilised\n");
348       break;
349     }
350     
351     progress_spinner("%s",doing);
352
353     debugf("PAGING  wait_for_stability  retry\n");
354
355     free_snapshot(&last); last=*output; *output=0;
356
357     if (with_keypress)
358       with_keypress();
359
360     delay(0.5);
361   }
362
363   free_snapshot(&last);
364   free(doing);
365   debugf("PAGING  wait_for_stability done.\n");
366   va_end(al);
367 }
368
369 static void raise_and_get_details(void) {
370   int evbase,errbase,majver,minver;
371   int wxpos, wypos;
372   unsigned bd,depth;
373
374   progress("raising and checking YPP client window...");
375
376   debugf("PAGING raise_and_get_details\n");
377
378   int xtest= XTestQueryExtension(disp, &evbase,&errbase,&majver,&minver);
379   if (!xtest) fatal("X server does not support the XTEST extension.");
380
381   xassert( XRaiseWindow(disp, id) );
382   /* in case VisibilityNotify triggers right away before we have had a
383    * change to raise; to avoid falsely detecting lowering in that case */
384   
385   xassert( XSelectInput(disp, id,
386                         StructureNotifyMask|
387                         VisibilityChangeMask
388                         ) );
389
390   xassert( XRaiseWindow(disp, id) );
391   /* in case the window was lowered between our Raise and our SelectInput;
392    * to avoid failing to detect that lowering */
393
394   xassert( XGetWindowAttributes(disp, id, &attr) );
395   xassert( XGetGeometry(disp,id, &attr.root,
396                         &wxpos,&wypos, &wwidth,&wheight,
397                         &bd,&depth) );
398
399   if (!(wwidth >= 320 && wheight >= 320))
400     fatal("YPP client window is implausibly small?");
401
402   check_client_window_all_on_screen();
403
404   int shm= XShmQueryExtension(disp);
405     shm=0;
406   if (shm) {
407     xassert( shmim= XShmCreateImage(disp, attr.visual, attr.depth, ZPixmap,
408                                     0,&shminfo, wwidth,wheight) );
409
410     sigset_t oldset, all;
411     sigfillset(&all);
412     sysassert(! sigprocmask(SIG_BLOCK,&all,&oldset) );
413
414     int pfd[2];
415     pid_t cleaner;
416     sysassert(! pipe(pfd) );
417     sysassert( (cleaner= fork()) != -1 );
418     if (!cleaner) {
419       sysassert(! close(pfd[1]) );
420       for (;;) {
421         int r= read(pfd[0], &shminfo.shmid, sizeof(shminfo.shmid));
422         if (!r) exit(0);
423         if (r==sizeof(shminfo.shmid)) break;
424         assert(r==-1 && errno==EINTR);
425       }
426       for (;;) {
427         char bc;
428         int r= read(pfd[0],&bc,1);
429         if (r>=0) break;
430         assert(r==-1 && errno==EINTR);
431       }
432       sysassert(! shmctl(shminfo.shmid,IPC_RMID,0) );
433       exit(0);
434     }
435     sysassert(! close(pfd[0]) );
436
437     sysassert(! sigprocmask(SIG_SETMASK,&oldset,0) );
438
439     assert(shmim->height == wheight);
440     sysassert( (shminfo.shmid=
441                 shmget(IPC_PRIVATE, shmim->bytes_per_line * wheight,
442                        IPC_CREAT|0600)) >= 0 );
443
444     sysassert( write(pfd[1],&shminfo.shmid,sizeof(shminfo.shmid)) ==
445                sizeof(shminfo.shmid) );
446     sysassert( shminfo.shmaddr= shmat(shminfo.shmid,0,0) );
447     shmim->data= shminfo.shmaddr;
448     shminfo.readOnly= False;
449     xassert( XShmAttach(disp,&shminfo) );
450
451     close(pfd[1]); /* causes IPC_RMID */
452   }
453 }
454
455 static void set_focus_commodity(void) {
456   int screen= XScreenNumberOfScreen(attr.screen);
457
458   progress("taking control of YPP client window...");
459
460   debugf("PAGING set_focus\n");
461
462   mouse_1_updown(160,160);
463   sync_after_input();
464
465   delay(0.5);
466   xassert( XSelectInput(disp, id,
467                         StructureNotifyMask|
468                         VisibilityChangeMask|
469                         FocusChangeMask
470                         ) );
471
472   int xpos,ypos;
473   translate_coords_toroot(10,10, &xpos,&ypos);
474   XTestFakeMotionEvent(disp,screen, xpos,ypos, 0);
475
476   sync_after_input();
477
478   debugf("PAGING raise_and_set_focus done.\n");
479 }
480
481 static CanonImage *convert_page(Snapshot *sn, RgbImage *ri) {
482   CanonImage *im;
483
484   fprintf(screenshot_file,
485           "P6\n"
486           "%d %d\n"
487           "255\n", sn->w, sn->h);
488
489   CANONICALISE_IMAGE(im, sn->w, sn->h, {
490     int i;
491     rgb= 0;
492     for (i=0; i<3; i++) {
493       rgb <<= 8;
494       unsigned long sample= RI_PIXEL(sn,x,y)[i];
495       rgb |= sample;
496       fputc(sample, screenshot_file);
497     }
498     if (ri)
499       CANONIMG_ALSO_STORERGB(ri);
500   });
501
502   sysassert(!ferror(screenshot_file));
503   sysassert(!fflush(screenshot_file));
504
505   return im;
506 }
507
508 static void prepare_ypp_client(void) {
509   CanonImage *test;
510   Snapshot *current=0;
511   
512   /* find the window and check it's on the right kind of screen */
513   raise_and_get_details();
514   wait_for_stability(&current,0,0, "checking current YPP client screen...");
515
516 #if 0
517 timestamp();
518 snapshot(&current);
519 snapshot(&current);
520 snapshot(&current);
521 snapshot(&current);
522 snapshot(&current);
523 snapshot(&current);
524 snapshot(&current);
525 snapshot(&current);
526 snapshot(&current);
527 snapshot(&current);
528 timestamp();
529 #endif
530
531   test= convert_page(current, 0);
532   find_structure(test, &max_relevant_y);
533   check_correct_commodities();
534   free(test);
535   free_snapshot(&current);
536
537   progress("requesting status information...");
538   mouse_1_updown(250, wheight-10);
539   mouse_1_updown_here();
540   mouse_1_updown_here();
541   XSync(disp,False);
542   check_not_disturbed();
543   send_key(XK_slash);
544   send_key(XK_w);
545   send_key(XK_Return);
546   sync_after_input();
547   sysassert(! usleep(1000000) );
548 }
549
550 void take_screenshots(void) {
551   Snapshot *current=0, *last=0;
552
553   prepare_ypp_client();
554   
555   /* page to the top - keep pressing page up until the image stops changing */
556   set_focus_commodity();
557   wait_for_stability(&current,0, send_pgup_many,
558                      "paging up to top of commodity list...");
559
560   /* now to actually page down */
561   for (;;) {
562     debugf("paging page %d\n",npages);
563
564     if (!(npages < MAX_PAGES))
565       fatal("Paging down seems to generate too many pages - max is %d.",
566             MAX_PAGES);
567     
568     page_images[npages]= convert_page(current, 0);
569     free_snapshot(&last); last=current; current=0;
570
571     debugf("PAGING page %d converted\n",npages);
572
573     wait_for_stability(&current,last, 0,
574                        "collecting screenshot of page %d...",
575                        npages+1);
576
577     if (npages &&  /* first pagedown doesn't do much */
578         identical(current,last)) {
579       free_snapshot(&current);
580       break;
581     }
582
583     send_pgdown();
584     npages++;
585   }
586   progress("finishing with the YPP client...");
587   send_pgdown_torestore();
588
589   debugf("PAGING all done.\n");
590   progress_log("collected %d screenshots.",npages);
591 }    
592
593 void take_one_screenshot(void) {
594   Snapshot *current=0;
595
596   prepare_ypp_client();
597   wait_for_stability(&current,0,0, "taking screenshot...");
598   page0_rgbimage= alloc_rgb_image(current->w, current->h);
599   page_images[0]= convert_page(current, page0_rgbimage);
600   npages= 1;
601   progress_log("collected single screenshot.");
602 }
603
604 void set_yppclient_window(unsigned long wul) {
605   id= wul;
606 }
607
608 DEBUG_DEFINE_SOME_DEBUGF(findypp,debugfind)
609
610 void find_yppclient_window(void) {
611   Window root, gotroot, gotparent;
612   int screen;
613   int nfound=0;
614   
615   if (id) return;
616   
617   progress("looking for YPP client window...");
618
619   static const char prefix[]= "Puzzle Pirates - ";
620   static const char onthe[]= " on the ";
621   static const char suffix[]= " ocean";
622 #define S(x) (sizeof((x))-1)
623
624   Atom wm_name= XInternAtom(disp,"WM_NAME",True);
625   xassert(wm_name != None);
626
627   for (screen=0; screen<ScreenCount(disp); screen++) {
628     debugfind("FINDYPP screen %d\n", screen);
629     root= RootWindow(disp,screen);
630     unsigned int nchildren1;
631     Window *children1=0;
632
633     xassert( XQueryTree(disp,root,
634                   &gotroot,&gotparent,
635                   &children1,&nchildren1) );
636     debugfind("FINDYPP screen %d nchildren1=%d\n", screen, nchildren1);
637
638     int i;
639     for (i=0; i<nchildren1; i++) {
640       Window w1= children1[i];
641       unsigned int nchildren2;
642       Window *children2=0;
643
644       xassert( XQueryTree(disp,w1,
645                           &gotroot,&gotparent,
646                           &children2,&nchildren2) );
647       debugfind("FINDYPP screen %d c1[%2d]=0x%08lx nchildren2=%d\n",
648                 screen, i, (unsigned long)w1, nchildren2);
649
650       int j;
651       for (j=-1; j<(int)nchildren2; j++) {
652         Window w2= j<0 ? w1 : children2[j];
653         debugfind("FINDYPP screen %d c1[%2d]=0x%08lx c2[%2d]=0x%08lx",
654                   screen, i, (unsigned long)w1, j, (unsigned long)w2);
655
656         int gotfmt;
657         Atom gottype;
658         unsigned long len, gotbytesafter;
659         char *title;
660         unsigned char *gottitle=0;
661         xassert( !XGetWindowProperty(disp,w2, wm_name,0,512, False,
662                                      AnyPropertyType,&gottype, &gotfmt, &len,
663                                      &gotbytesafter, &gottitle) );
664         title= (char*)gottitle;
665
666         if (DEBUGP(findypp)) {
667           debugfind(" gf=%d len=%lu gba=%lu \"", gotfmt,len,gotbytesafter);
668           char *p;
669           for (p=title; p < title+len; p++) {
670             char c= *p;
671             if (c>=' ' && c<=126) fputc(c,debug);
672             else fprintf(debug,"\\x%02x",c & 0xff);
673           }
674           fputs("\": ",debug);
675         }
676
677 #define REQUIRE(pred)                                                      \
678         if (!(pred)) { debugfind(" failed test  %s\n", #pred); continue; } \
679         else
680
681         REQUIRE( gottype!=None );
682         REQUIRE( len );
683         REQUIRE( gotfmt==8 );
684
685         REQUIRE( len >= S(prefix) + 1 + S(onthe) + 1 + S(suffix) );
686
687         char *spc1= strchr(  title        + S(prefix), ' ');  REQUIRE(spc1);
688         char *spc2= strrchr((title + len) - S(suffix), ' ');  REQUIRE(spc2);
689
690         REQUIRE( (title + len) - spc1  >= S(onthe)  + S(suffix) );
691         REQUIRE(  spc2         - title >= S(prefix) + S(onthe) );
692
693         REQUIRE( !memcmp(title,                   prefix, S(prefix)) );
694         REQUIRE( !memcmp(title + len - S(suffix), suffix, S(suffix))  );
695         REQUIRE( !memcmp(spc1,                    onthe,  S(onthe))  );
696
697 #define ASSIGN(what, start, end)                                \
698         what= masprintf("%.*s", (end)-(start), start);          \
699         if (o_##what) REQUIRE( !strcasecmp(o_##what, what) );   \
700         else
701
702         ASSIGN(ocean,  spc1 + S(onthe),   (title + len) - S(suffix));
703         ASSIGN(pirate, title + S(prefix),  spc1);
704
705         debugfind(" YES!\n");
706         id= w2;
707         nfound++;
708         progress_log("found YPP client (0x%lx):"
709                      " %s ocean - %s.",
710                      (unsigned long)id, ocean, pirate);
711       }
712       if (children2) XFree(children2);
713     }
714     if (children1) XFree(children1);
715   }
716   if (nfound>1)
717     fatal("Found several possible YPP clients.   Close one,\n"
718           " disambiguate with --pirate or --ocean,"
719           " or specify --window-id.\n");
720   if (nfound<1)
721     fatal("Did not find %sYPP client."
722           " Use --window-id and/or report this as a fault.\n",
723           o_ocean || o_pirate ? "matching ": "");
724 }