2 * Interaction with the YPP client via X11
5 * This is part of ypp-sc-tools, a set of third-party tools for assisting
6 * players of Yohoho Puzzle Pirates.
8 * Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
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.
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.
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/>.
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.
29 * Only this file #includes the X11 headers, as they're quite
30 * pollutant of the namespace.
33 #include "structure.h"
36 #include <X11/extensions/XTest.h>
37 #include <X11/keysym.h>
38 #include <X11/Xutil.h>
40 #include <X11/extensions/XShm.h>
44 CanonImage *page_images[MAX_PAGES];
47 const char *ocean, *pirate;
49 static XWindowAttributes attr;
52 static struct timeval tv_startup;
53 static unsigned wwidth, wheight;
54 static int max_relevant_y= -1;
57 static XShmSegmentInfo shminfo;
59 DEBUG_DEFINE_DEBUGF(pages)
61 #define xassert(what) \
63 fatal("X11 operation unexpectedly failed." \
64 " %s:%d: %s\n", __FILE__,__LINE__,#what))
66 static KeyCode keycode(KeySym sym) {
67 return XKeysymToKeycode(disp,sym);
70 void screenshot_startup(void) {
71 progress("starting...");
72 disp= XOpenDisplay(0);
73 if (!disp) fatal("Unable to open X11 display.");
74 sysassert(! gettimeofday(&tv_startup,0) );
77 /*---------- pager ----------*/
79 typedef RgbImage Snapshot;
81 static double last_input;
82 static const double min_update_allowance= 0.25;
84 static double timestamp(void) {
87 sysassert(! gettimeofday(&tv,0) );
88 double t= (tv.tv_sec - tv_startup.tv_sec) +
89 (tv.tv_usec - tv_startup.tv_usec) * 1e-6;
90 debugf("PAGING %f\n",t);
93 static void delay(double need_sleep) {
94 debugf("PAGING delay %f\n",need_sleep);
95 sysassert(! usleep(need_sleep * 1e6) );
98 static void sync_after_input(void) {
99 xassert( XSync(disp, False) );
100 last_input= timestamp();
103 static void translate_coords_toroot(int wx, int wy, int *rx, int *ry) {
105 xassert( XTranslateCoordinates(disp, id,attr.root, wx,wy, rx,ry, &dummy) );
108 static void check_client_window_all_on_screen(void) {
110 unsigned rwidth, rheight;
115 xassert( XGetGeometry(disp,attr.root, &dummy, &rxpos,&rypos,
119 translate_coords_toroot(0,0, &onroot.tl.x,&onroot.tl.y);
120 translate_coords_toroot(wwidth-1,wheight-1, &onroot.br.x,&onroot.br.y);
121 if (!(onroot.tl.x >= 0 &&
123 onroot.br.x < rwidth &&
124 onroot.br.y < rheight))
125 fatal("YPP client window is not entirely on the screen.");
128 static void check_not_disturbed(void) {
133 r= XCheckMaskEvent(disp, ~0, &ev);
134 if (r==False) return;
137 case VisibilityNotify:
138 if (ev.xvisibility.state != VisibilityUnobscured)
139 fatal("YPP client window has become obscured.");
141 case ConfigureNotify:
142 check_client_window_all_on_screen();
145 fatal("Focus left YPP client window.");
148 warning("focus entered YPP client window ?!");
151 fatal("Received unexpected X11 event (type code %d)!", ev.type);
156 static void send_key(KeySym sym) {
157 check_not_disturbed();
158 XTestFakeKeyEvent(disp, keycode(sym),1, 10);
159 XTestFakeKeyEvent(disp, keycode(sym),0, 10);
161 static void mouse_1_updown_here(void) {
162 check_not_disturbed();
163 XTestFakeButtonEvent(disp,1,1, 50);
164 XTestFakeButtonEvent(disp,1,0, 50);
166 static void mouse_1_updown(int x, int y) {
167 check_not_disturbed();
168 int screen= XScreenNumberOfScreen(attr.screen);
170 translate_coords_toroot(x,y, &xpos,&ypos);
171 XTestFakeMotionEvent(disp, screen, xpos,ypos, 0);
172 mouse_1_updown_here();
177 static void send_pgup_many(void) {
179 for (i=0; i<25; i++) {
183 debugf("PAGING PageUp x %d\n",i);
186 static void send_pgdown(void) {
189 debugf("PAGING PageDown\n");
192 static void send_pgdown_torestore(void) {
193 debugf("PAGING PageDown x %d\n", -pgupdown);
194 while (pgupdown < 0) {
201 static void free_snapshot(Snapshot **io) {
206 #define SAMPLEMASK 0xfful
212 static void compute_shift_mask(ShMask *sm, unsigned long ximage_mask) {
217 if (ximage_mask <= (SAMPLEMASK>>1)) {
218 sm->lshift++; ximage_mask <<= 1;
219 } else if (ximage_mask > SAMPLEMASK) {
220 sm->rshift++; ximage_mask >>= 1;
224 assert(!(sm->lshift && sm->rshift));
226 assert(sm->lshift < LONG_BIT);
227 assert(sm->rshift < LONG_BIT);
228 debugf("SHIFTMASK %p={.lshift=%d, .rshift=%d} image_mask=%lx\n",
229 sm, sm->lshift, sm->rshift, ximage_mask);
232 static void rtimestamp(double *t, const char *wh) {
233 double n= timestamp();
234 debugf("PAGING INTERVAL %f %s\n", n-*t, wh);
238 static void snapshot(Snapshot **output) {
239 XImage *im_use, *im_free=0;
241 ShMask shiftmasks[3];
243 debugf("PAGING snapshot\n");
245 double begin= timestamp();
247 rtimestamp(&begin, "XShmGetImage before");
248 xassert( XShmGetImage(disp,id,shmim, 0,0, AllPlanes) );
249 rtimestamp(&begin, "XShmGetImage");
251 size_t dsz= shmim->bytes_per_line * shmim->height;
252 im_use= im_free= mmalloc(sizeof(*im_use) + dsz);
254 im_free->data= (void*)(im_free+1);
255 memcpy(im_free->data, shmim->data, dsz);
256 rtimestamp(&begin, "mmalloc/memcpy");
258 rtimestamp(&begin, "XGetImage before");
259 xassert( im_use= im_free=
260 XGetImage(disp,id, 0,0, wwidth,wheight, AllPlanes, ZPixmap) );
261 rtimestamp(&begin, "XGetImage");
264 #define COMPUTE_SHIFT_MASK(ix, rgb) \
265 compute_shift_mask(&shiftmasks[ix], im_use->rgb##_mask)
266 COMPUTE_SHIFT_MASK(0, red);
267 COMPUTE_SHIFT_MASK(1, green);
268 COMPUTE_SHIFT_MASK(2, blue);
271 *output= alloc_rgb_image(wwidth, wheight);
273 rtimestamp(&begin, "compute_shift_masks+alloc_rgb_image");
276 unsigned char *op= (*output)->data;
277 for (y=0; y<wheight; y++) {
278 if (im_use->xoffset == 0 &&
279 im_use->format == ZPixmap &&
280 im_use->byte_order == LSBFirst &&
281 im_use->depth == 24 &&
282 im_use->bits_per_pixel == 32 &&
283 im_use->red_mask == 0x0000ffU &&
284 im_use->green_mask == 0x00ff00U &&
285 im_use->blue_mask == 0xff0000U) {
286 const char *p= im_use->data + y * im_use->bytes_per_line;
287 // debugf("optimised copy y=%d",y);
288 for (x=0; x<wwidth; x++) {
295 for (x=0; x<wwidth; x++) {
296 long xrgb= XGetPixel(im_use,x,y);
297 for (i=0; i<3; i++) {
298 unsigned long sample=
299 ((xrgb << shiftmasks[i].lshift) >> shiftmasks[i].rshift)
307 rtimestamp(&begin,"w*h*XGetPixel");
309 XDestroyImage(im_free);
311 rtimestamp(&begin,"XDestroyImage");
312 check_not_disturbed();
314 debugf("PAGING snapshot done.\n");
317 static int identical(const Snapshot *a, const Snapshot *b) {
318 if (!(a->w == b->w &&
322 int compare_to= a->h;
323 if (max_relevant_y>=0 && compare_to > max_relevant_y)
324 compare_to= max_relevant_y;
326 return !memcmp(a->data, b->data, a->w * 3 * compare_to);
329 static void wait_for_stability(Snapshot **output,
330 const Snapshot *previously,
331 void (*with_keypress)(void),
332 const char *fmt, ...)
335 static void wait_for_stability(Snapshot **output,
336 const Snapshot *previously,
337 void (*with_keypress)(void),
338 const char *fmt, ...) {
344 /* waits longer if we're going to return an image identical to previously
345 * if previously==0, all images are considered identical to it */
348 sysassert( vasprintf(&doing,fmt,al) >=0 );
350 debugf("PAGING wait_for_stability"
351 " last_input=%f previously=%p `%s'\n",
352 last_input, previously, doing);
354 double min_interval= 25000; /*us*/
356 progress_spinner("%s",doing);
358 double since_last_input= timestamp() - last_input;
359 double this_interval= min_interval - since_last_input;
361 if (this_interval >= 0)
362 usleep(this_interval);
367 debugf("PAGING wait_for_stability first...\n");
368 last=*output; *output=0;
369 } else if (!identical(*output,last)) {
370 debugf("PAGING wait_for_stability changed...\n");
371 free_snapshot(&last); last=*output; *output=0;
373 if (!with_keypress) {
381 identical(*output,previously) ? 5
383 debugf("PAGING wait_for_stability nidentical=%d threshold=%d\n",
384 nidentical, threshold);
385 if (nidentical >= threshold)
396 free_snapshot(&last);
398 debugf("PAGING wait_for_stability done.\n");
402 static void raise_and_get_details(void) {
403 int evbase,errbase,majver,minver;
407 progress("raising and checking YPP client window...");
409 debugf("PAGING raise_and_get_details\n");
411 int xtest= XTestQueryExtension(disp, &evbase,&errbase,&majver,&minver);
412 if (!xtest) fatal("X server does not support the XTEST extension.");
414 xassert( XRaiseWindow(disp, id) );
415 /* in case VisibilityNotify triggers right away before we have had a
416 * change to raise; to avoid falsely detecting lowering in that case */
418 xassert( XSelectInput(disp, id,
423 xassert( XRaiseWindow(disp, id) );
424 /* in case the window was lowered between our Raise and our SelectInput;
425 * to avoid failing to detect that lowering */
427 xassert( XGetWindowAttributes(disp, id, &attr) );
428 xassert( XGetGeometry(disp,id, &attr.root,
429 &wxpos,&wypos, &wwidth,&wheight,
432 if (!(wwidth >= 320 && wheight >= 320))
433 fatal("YPP client window is implausibly small?");
436 fatal("Display is not 24bpp.");
438 check_client_window_all_on_screen();
442 int shm= XShmQueryVersion(disp, &major,&minor,&shmpixmaps);
443 debugf("PAGING shm=%d %d.%d pixmaps=%d\n",shm,major,minor,shmpixmaps);
445 xassert( shmim= XShmCreateImage(disp, attr.visual, attr.depth, ZPixmap,
446 0,&shminfo, wwidth,wheight) );
448 sigset_t oldset, all;
450 sysassert(! sigprocmask(SIG_BLOCK,&all,&oldset) );
454 sysassert(! pipe(pfd) );
455 sysassert( (cleaner= fork()) != -1 );
457 sysassert(! close(pfd[1]) );
459 int r= read(pfd[0], &shminfo.shmid, sizeof(shminfo.shmid));
461 if (r==sizeof(shminfo.shmid)) break;
462 assert(r==-1 && errno==EINTR);
466 int r= read(pfd[0],&bc,1);
468 assert(r==-1 && errno==EINTR);
470 sysassert(! shmctl(shminfo.shmid,IPC_RMID,0) );
473 sysassert(! close(pfd[0]) );
475 sysassert(! sigprocmask(SIG_SETMASK,&oldset,0) );
477 assert(shmim->height == wheight);
478 sysassert( (shminfo.shmid=
479 shmget(IPC_PRIVATE, shmim->bytes_per_line * wheight,
480 IPC_CREAT|0600)) >= 0 );
482 sysassert( write(pfd[1],&shminfo.shmid,sizeof(shminfo.shmid)) ==
483 sizeof(shminfo.shmid) );
484 sysassert( shminfo.shmaddr= shmat(shminfo.shmid,0,0) );
485 shmim->data= shminfo.shmaddr;
486 shminfo.readOnly= False;
487 xassert( XShmAttach(disp,&shminfo) );
489 close(pfd[1]); /* causes IPC_RMID */
493 static void set_focus_commodity(void) {
494 int screen= XScreenNumberOfScreen(attr.screen);
496 progress("taking control of YPP client window...");
498 debugf("PAGING set_focus\n");
500 mouse_1_updown(160,160);
504 xassert( XSelectInput(disp, id,
506 VisibilityChangeMask|
511 translate_coords_toroot(10,10, &xpos,&ypos);
512 XTestFakeMotionEvent(disp,screen, xpos,ypos, 0);
516 debugf("PAGING raise_and_set_focus done.\n");
519 static CanonImage *convert_page(Snapshot *sn) {
522 fwrite_ppmraw(screenshot_file, sn);
524 unsigned char *pixel= sn->data;
525 CANONICALISE_IMAGE(im, sn->w, sn->h, {
531 sysassert(!ferror(screenshot_file));
532 sysassert(!fflush(screenshot_file));
537 static void prepare_ypp_client(void) {
541 /* find the window and check it's on the right kind of screen */
542 raise_and_get_details();
543 wait_for_stability(¤t,0,0, "checking current YPP client screen...");
545 test= convert_page(current);
546 find_structure(test, &max_relevant_y);
547 check_correct_commodities();
548 Rect sunshine= find_sunshine_widget();
550 progress("poking client...");
551 mouse_1_updown((sunshine.tl.x + sunshine.br.x) / 2,
552 (sunshine.tl.y*9 + sunshine.br.y) / 10);
556 wait_for_stability(¤t,0,0, "checking basic YPP client screen...");
557 mouse_1_updown(250, wheight-10);
558 mouse_1_updown_here();
559 mouse_1_updown_here();
561 check_not_disturbed();
568 wait_for_stability(&status,current,0, "awaiting status information...");
569 free_snapshot(¤t);
570 free_snapshot(&status);
573 void take_screenshots(void) {
574 Snapshot *current=0, *last=0;
576 prepare_ypp_client();
578 /* page to the top - keep pressing page up until the image stops changing */
579 set_focus_commodity();
580 wait_for_stability(¤t,0, send_pgup_many,
581 "paging up to top of commodity list...");
583 /* now to actually page down */
585 debugf("paging page %d\n",npages);
587 if (!(npages < MAX_PAGES))
588 fatal("Paging down seems to generate too many pages - max is %d.",
591 page_images[npages]= convert_page(current);
592 free_snapshot(&last); last=current; current=0;
594 debugf("PAGING page %d converted\n",npages);
596 wait_for_stability(¤t,last, 0,
597 "collecting screenshot of page %d...",
600 if (npages && /* first pagedown doesn't do much */
601 identical(current,last)) {
602 free_snapshot(¤t);
609 progress("finishing with the YPP client...");
610 send_pgdown_torestore();
612 debugf("PAGING all done.\n");
613 progress_log("collected %d screenshots.",npages);
616 void take_one_screenshot(void) {
619 prepare_ypp_client();
620 wait_for_stability(¤t,0,0, "taking screenshot...");
621 page_images[0]= convert_page(current);
623 progress_log("collected single screenshot.");
626 void set_yppclient_window(unsigned long wul) {
630 DEBUG_DEFINE_SOME_DEBUGF(findypp,debugfind)
636 static void findypp_recurse(int depth, int targetdepth, Window w) {
637 unsigned int nchildren;
640 Window gotroot, gotparent;
642 static const char prefix[]= "Puzzle Pirates - ";
643 static const char onthe[]= " on the ";
644 static const char suffix[]= " ocean";
645 #define S(x) ((int)sizeof((x))-1)
647 debugfind("FINDYPP %d/%d screen %d %*s %lx",
648 depth,targetdepth,screen,
649 depth,"",(unsigned long)w);
651 if (depth!=targetdepth) {
652 xassert( XQueryTree(disp,w,
654 &children,&nchildren) );
655 debugfind(" nchildren=%d\n",nchildren);
657 for (i=0; i<nchildren; i++) {
658 Window child= children[i];
659 findypp_recurse(depth+1, targetdepth, child);
668 unsigned long len, gotbytesafter;
670 unsigned char *gottitle=0;
671 xassert( !XGetWindowProperty(disp,w, wm_name,0,512, False,
672 AnyPropertyType,&gottype, &gotfmt, &len,
673 &gotbytesafter, &gottitle) );
674 title= (char*)gottitle;
676 if (DEBUGP(findypp)) {
677 debugfind(" gf=%d len=%lu gba=%lu \"", gotfmt,len,gotbytesafter);
679 for (p=title; p < title+len; p++) {
681 if (c>=' ' && c<=126) fputc(c,debug);
682 else fprintf(debug,"\\x%02x",c & 0xff);
687 #define REQUIRE(pred) \
688 if (!(pred)) { debugfind(" failed test %s\n", #pred); return; } \
691 REQUIRE( gottype!=None );
693 REQUIRE( gotfmt==8 );
695 REQUIRE( len >= S(prefix) + 1 + S(onthe) + 1 + S(suffix) );
697 char *spc1= strchr( title + S(prefix), ' '); REQUIRE(spc1);
698 char *spc2= strrchr((title + len) - S(suffix), ' '); REQUIRE(spc2);
700 REQUIRE( (title + len) - spc1 >= S(onthe) + S(suffix) );
701 REQUIRE( spc2 - title >= S(prefix) + S(onthe) );
703 REQUIRE( !memcmp(title, prefix, S(prefix)) );
704 REQUIRE( !memcmp(title + len - S(suffix), suffix, S(suffix)) );
705 REQUIRE( !memcmp(spc1, onthe, S(onthe)) );
707 #define ASSIGN(what, start, end) \
708 what= masprintf("%.*s", (int)((end)-(start)), start); \
709 if (o_##what) REQUIRE( !strcasecmp(o_##what, what) ); \
712 ASSIGN(ocean, spc1 + S(onthe), (title + len) - S(suffix));
713 ASSIGN(pirate, title + S(prefix), spc1);
715 debugfind(" YES!\n");
718 progress_log("found YPP client (0x%lx):"
720 (unsigned long)id, ocean, pirate);
723 void find_yppclient_window(void) {
730 progress("looking for YPP client window...");
732 xassert( (wm_name= XInternAtom(disp,"WM_NAME",True)) != None);
734 for (targetdepth=1; targetdepth<4; targetdepth++) {
735 for (screen=0; screen<ScreenCount(disp); screen++) {
736 debugfind("FINDYPP screen %d\n", screen);
737 findypp_recurse(0,targetdepth, RootWindow(disp,screen));
743 fatal("Found several possible YPP clients. Close one,\n"
744 " disambiguate with --pirate or --ocean,"
745 " or specify --window-id.\n");
747 fatal("Did not find %sYPP client."
748 " Use --window-id and/or report this as a fault.\n",
749 o_ocean || o_pirate ? "matching ": "");