7 #include <X11/extensions/XTest.h>
8 #include <X11/keysym.h>
11 CanonImage *page_images[MAX_PAGES];
16 static XWindowAttributes attr;
19 static struct timeval tv_startup;
20 static unsigned wwidth, wheight;
22 DEBUG_DEFINE_DEBUGF(pages)
24 #define xassert(what) \
26 fatal("X11 operation unexpectedly failed." \
27 " %s:%d: %s\n", __FILE__,__LINE__,#what))
29 static KeyCode keycode(KeySym sym) {
30 return XKeysymToKeycode(disp,sym);
33 void screenshot_startup(void) {
34 progress("starting...");
35 disp= XOpenDisplay(0);
36 if (!disp) fatal("Unable to open X11 display.");
37 sysassert(! gettimeofday(&tv_startup,0) );
40 /*---------- pager ----------*/
42 typedef XImage Snapshot;
44 static double last_input;
45 static const double min_update_allowance= 0.25;
47 static double timestamp(void) {
50 sysassert(! gettimeofday(&tv,0) );
51 double t= (tv.tv_sec - tv_startup.tv_sec) +
52 (tv.tv_usec - tv_startup.tv_usec) * 1e-6;
53 debugf("PAGING %f\n",t);
56 static void delay(double need_sleep) {
57 debugf("PAGING delay %f\n",need_sleep);
58 sysassert(! usleep(need_sleep * 1e6) );
61 static void sync_after_input(void) {
62 xassert( XSync(disp, False) );
63 last_input= timestamp();
66 static void translate_coords_toroot(int wx, int wy, int *rx, int *ry) {
68 xassert( XTranslateCoordinates(disp, id,attr.root, wx,wy, rx,ry, &dummy) );
71 static void check_client_window_all_on_screen(void) {
73 unsigned rwidth, rheight;
78 xassert( XGetGeometry(disp,attr.root, &dummy, &rxpos,&rypos,
82 translate_coords_toroot(0,0, &onroot.tl.x,&onroot.tl.y);
83 translate_coords_toroot(wwidth-1,wheight-1, &onroot.br.x,&onroot.br.y);
84 if (!(onroot.tl.x >= 0 &&
86 onroot.br.x < rwidth &&
87 onroot.br.y < rheight))
88 fatal("YPP client window is not entirely on the screen.");
91 static void check_not_disturbed(void) {
96 r= XCheckMaskEvent(disp, ~0, &ev);
100 case VisibilityNotify:
101 if (ev.xvisibility.state != VisibilityUnobscured)
102 fatal("YPP client window has become obscured.");
104 case ConfigureNotify:
105 check_client_window_all_on_screen();
108 fatal("Focus left YPP client window.");
111 warning("focus entered YPP client window ?!");
114 fatal("Received unexpected X11 event (type code %d)!", ev.type);
119 static void send_key(KeySym sym) {
120 XTestFakeKeyEvent(disp, keycode(sym),1, 10);
121 XTestFakeKeyEvent(disp, keycode(sym),0, 10);
124 static void send_pgup_many(void) {
128 debugf("PAGING PageUp x %d\n",i);
131 static void send_pgdown(void) {
133 debugf("PAGING PageDown\n");
137 static void free_snapshot(Snapshot **io) {
138 if (*io) XDestroyImage(*io);
142 static void snapshot(Snapshot **output) {
143 free_snapshot(output);
145 debugf("PAGING snapshot\n");
148 *output= XGetImage(disp,id, 0,0, wwidth,wheight, AllPlanes, ZPixmap);
151 check_not_disturbed();
153 debugf("PAGING snapshot done.\n");
156 static int identical(const Snapshot *a, const Snapshot *b) {
157 if (!(a->width == b->width &&
158 a->height == b->height &&
159 a->bytes_per_line == b->bytes_per_line &&
160 a->format == b->format))
162 return !memcmp(a->data, b->data, a->bytes_per_line * a->height);
165 static void wait_for_stability(Snapshot **output,
166 const Snapshot *previously,
167 void (*with_keypress)(void),
168 const char *fmt, ...)
171 static void wait_for_stability(Snapshot **output,
172 const Snapshot *previously,
173 void (*with_keypress)(void),
174 const char *fmt, ...) {
179 /* waits longer if we're going to return an image identical to previously
180 * if previously==0, all images are considered identical to it */
182 debugf("PAGING wait_for_stability"
183 " last_input=%f previously=%p\n",
184 last_input, previously);
187 sysassert( vasprintf(&doing,fmt,al) >=0);
189 progress("%s",doing);
192 double at_snapshot= timestamp();
193 double need_sleep= min_update_allowance - (at_snapshot - last_input);
194 if (need_sleep > 0) { delay(need_sleep); continue; }
198 if (!with_keypress &&
199 !(previously && identical(*output,previously))) {
200 debugf("PAGING wait_for_stability simple\n");
204 if (last && identical(*output,last)) {
205 debugf("PAGING wait_for_stability stabilised\n");
209 progress_spinner("%s",doing);
211 debugf("PAGING wait_for_stability retry\n");
213 free_snapshot(&last); last=*output; *output=0;
221 free_snapshot(&last);
223 debugf("PAGING wait_for_stability done.\n");
227 static void raise_and_get_details(void) {
228 int evbase,errbase,majver,minver;
232 progress("raising and checking YPP client window...");
234 debugf("PAGING raise_and_get_details\n");
236 int xtest= XTestQueryExtension(disp, &evbase,&errbase,&majver,&minver);
237 if (!xtest) fatal("X server does not support the XTEST extension.");
239 xassert( XRaiseWindow(disp, id) );
240 /* in case VisibilityNotify triggers right away before we have had a
241 * change to raise; to avoid falsely detecting lowering in that case */
243 xassert( XSelectInput(disp, id,
248 xassert( XRaiseWindow(disp, id) );
249 /* in case the window was lowered between our Raise and our SelectInput;
250 * to avoid failing to detect that lowering */
252 xassert( XGetWindowAttributes(disp, id, &attr) );
253 xassert( XGetGeometry(disp,id, &attr.root,
254 &wxpos,&wypos, &wwidth,&wheight,
257 if (!(wwidth >= 320 && wheight >= 320))
258 fatal("YPP client window is implausibly small?");
260 check_client_window_all_on_screen();
263 static void set_focus(void) {
264 int screen= XScreenNumberOfScreen(attr.screen);
266 progress("taking control of YPP client window...");
268 debugf("PAGING set_focus\n");
271 translate_coords_toroot(160,160, &xpos,&ypos);
272 XTestFakeMotionEvent(disp,screen, xpos,ypos, 0);
274 XTestFakeButtonEvent(disp,1,1, 50);
275 XTestFakeButtonEvent(disp,1,0, 50);
280 xassert( XSelectInput(disp, id,
282 VisibilityChangeMask|
286 translate_coords_toroot(10,10, &xpos,&ypos);
287 XTestFakeMotionEvent(disp,screen, xpos,ypos, 0);
291 debugf("PAGING raise_and_set_focus done.\n");
294 #define SAMPLEMASK 0xfful
300 static void compute_shift_mask(ShMask *sm, unsigned long ximage_mask) {
305 if (ximage_mask <= (SAMPLEMASK>>1)) {
306 sm->lshift++; ximage_mask <<= 1;
307 } else if (ximage_mask > SAMPLEMASK) {
308 sm->rshift++; ximage_mask >>= 1;
312 assert(!(sm->lshift && sm->rshift));
314 assert(sm->lshift < LONG_BIT);
315 assert(sm->rshift < LONG_BIT);
318 static CanonImage *convert_page(Snapshot *sn) {
319 ShMask shiftmasks[3];
322 fprintf(screenshots_file,
325 "255\n", sn->width, sn->height);
327 #define COMPUTE_SHIFT_MASK(ix, rgb) \
328 compute_shift_mask(&shiftmasks[ix], sn->rgb##_mask)
329 COMPUTE_SHIFT_MASK(0, red);
330 COMPUTE_SHIFT_MASK(1, green);
331 COMPUTE_SHIFT_MASK(2, blue);
333 CANONICALISE_IMAGE(im, sn->width, sn->height, {
334 long xrgb= XGetPixel(sn, x, y);
337 for (i=0; i<3; i++) {
339 unsigned long sample=
340 ((xrgb << shiftmasks[i].lshift)
341 >> shiftmasks[i].rshift) & SAMPLEMASK;
343 fputc(sample, screenshots_file);
347 sysassert(!ferror(screenshots_file));
348 sysassert(!fflush(screenshots_file));
353 void take_screenshots(void) {
354 Snapshot *current=0, *last=0;
357 /* find the window and check it's on the right kind of screen */
358 raise_and_get_details();
359 wait_for_stability(¤t,0,0, "checking current YPP client screen...");
360 test= convert_page(current);
361 find_structure(test);
364 /* page to the top - keep pressing page up until the image stops changing */
366 wait_for_stability(¤t,0, send_pgup_many,
367 "paging up to top of commodity list...");
369 /* now to actually page down */
371 debugf("paging page %d\n",npages);
373 if (!(npages < MAX_PAGES))
374 fatal("Paging down seems to generate too many pages - max is %d.",
377 page_images[npages]= convert_page(current);
378 free_snapshot(&last); last=current; current=0;
380 debugf("PAGING page %d converted\n",npages);
382 wait_for_stability(¤t,last, 0,
383 "collecting screenshot of page %d...",
386 if (npages && /* first pagedown doesn't do much */
387 identical(current,last)) {
388 free_snapshot(¤t);
395 debugf("PAGING all done.\n");
396 progress_log("collected %d screenshots.",npages);
399 void take_one_screenshot(void) {
402 raise_and_get_details();
404 wait_for_stability(¤t,0,0, "taking screenshot...");
405 page_images[0]= convert_page(current);
407 progress_log("collected single screenshot.");
410 void set_yppclient_window(unsigned long wul) {
414 DEBUG_DEFINE_SOME_DEBUGF(findypp,debugfind)
416 void find_yppclient_window(void) {
417 Window root, gotroot, gotparent;
423 progress("looking for YPP client window...");
425 static const char prefix[]= "Puzzle Pirates - ";
426 static const char onthe[]= " on the ";
427 static const char suffix[]= " ocean";
428 #define S(x) (sizeof((x))-1)
430 Atom wm_name= XInternAtom(disp,"WM_NAME",True);
431 xassert(wm_name != None);
433 for (screen=0; screen<ScreenCount(disp); screen++) {
434 debugfind("FINDYPP screen %d\n", screen);
435 root= RootWindow(disp,screen);
436 unsigned int nchildren1;
439 xassert( XQueryTree(disp,root,
441 &children1,&nchildren1) );
442 debugfind("FINDYPP screen %d nchildren1=%d\n", screen, nchildren1);
445 for (i=0; i<nchildren1; i++) {
446 Window w1= children1[i];
447 unsigned int nchildren2;
450 xassert( XQueryTree(disp,w1,
452 &children2,&nchildren2) );
453 debugfind("FINDYPP screen %d c1[%2d]=0x%08lx nchildren2=%d\n",
454 screen, i, (unsigned long)w1, nchildren2);
457 for (j=-1; j<(int)nchildren2; j++) {
458 Window w2= j<0 ? w1 : children2[j];
459 debugfind("FINDYPP screen %d c1[%2d]=0x%08lx c2[%2d]=0x%08lx",
460 screen, i, (unsigned long)w1, j, (unsigned long)w2);
464 unsigned long len, gotbytesafter;
466 unsigned char *gottitle=0;
467 xassert( !XGetWindowProperty(disp,w2, wm_name,0,512, False,
468 AnyPropertyType,&gottype, &gotfmt, &len,
469 &gotbytesafter, &gottitle) );
470 title= (char*)gottitle;
472 if (DEBUGP(findypp)) {
473 debugfind(" gf=%d len=%lu gba=%lu \"", gotfmt,len,gotbytesafter);
475 for (p=title; p < title+len; p++) {
477 if (c>=' ' && c<=126) fputc(c,debug);
478 else fprintf(debug,"\\x%02x",c & 0xff);
483 #define REQUIRE(pred) \
484 if (!(pred)) { debugfind(" failed test %s\n", #pred); continue; } \
487 REQUIRE( gottype!=None );
489 REQUIRE( gotfmt==8 );
491 REQUIRE( len >= S(prefix) + 1 + S(onthe) + 1 + S(suffix) );
493 char *spc1= strchr( title + S(prefix), ' '); REQUIRE(spc1);
494 char *spc2= strrchr((title + len) - S(suffix), ' '); REQUIRE(spc2);
496 REQUIRE( (title + len) - spc1 >= S(onthe) + S(suffix) );
497 REQUIRE( spc2 - title >= S(prefix) + S(onthe) );
499 REQUIRE( !memcmp(title, prefix, S(prefix)) );
500 REQUIRE( !memcmp(title + len - S(suffix), suffix, S(suffix)) );
501 REQUIRE( !memcmp(spc1, onthe, S(onthe)) );
503 #define ASSIGN(what, start, end) do { \
504 sysassert( asprintf(&what, "%.*s", (end)-(start), start) >0 ); \
506 ASSIGN(pirate, title + S(prefix), spc1);
507 ASSIGN(ocean, spc1 + S(onthe), (title + len) - S(suffix));
509 debugfind(" YES!\n");
512 progress_log("found YPP client (0x%lx):"
514 (unsigned long)id, ocean, pirate);
516 if (children2) XFree(children2);
518 if (children1) XFree(children1);
521 fatal("Found several YPP clients."
522 " Close one, or specify the windowid with --window-id.\n");
524 fatal("Did not find YPP client."
525 " Use --window-id and/or report this as a fault.\n");