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 static KeyCode keycode(KeySym sym) {
25 return XKeysymToKeycode(disp,sym);
28 void screenshot_startup(void) {
30 progress("starting...");
31 disp= XOpenDisplay(0); eassert(disp);
32 r= gettimeofday(&tv_startup,0); eassert(!r);
35 /*---------- pager ----------*/
37 typedef XImage Snapshot;
39 static double last_input;
40 static const double min_update_allowance= 0.25;
42 static double timestamp(void) {
46 r= gettimeofday(&tv,0); eassert(!r);
47 double t= (tv.tv_sec - tv_startup.tv_sec) +
48 (tv.tv_usec - tv_startup.tv_usec) * 1e-6;
49 debugf("PAGING %f\n",t);
52 static void delay(double need_sleep) {
54 debugf("PAGING delay %f\n",need_sleep);
55 r= usleep(need_sleep * 1e6); eassert(!r);
58 static void sync_after_input(void) {
60 r= XSync(disp, False); eassert(r);
61 last_input= timestamp();
64 static void translate_coords_toroot(int wx, int wy, int *rx, int *ry) {
67 r= XTranslateCoordinates(disp, id,attr.root, wx,wy, rx,ry, &dummy);
71 static void check_client_window_all_on_screen(void) {
74 unsigned rwidth, rheight;
78 r= XGetGeometry(disp,attr.root, &dummy, &rxpos,&rypos,
82 translate_coords_toroot(0,0, &rxpos,&rypos);
83 eassert(rxpos>=0 && rypos>=0);
85 translate_coords_toroot(wwidth-1,wheight-1, &rxpos,&rypos);
86 eassert(rxpos<rwidth && rypos<rheight);
89 static void check_not_disturbed(void) {
94 r= XCheckMaskEvent(disp, ~0, &ev);
98 case VisibilityNotify:
99 eassert(ev.xvisibility.state == VisibilityUnobscured);
101 case ConfigureNotify:
102 check_client_window_all_on_screen();
105 eassert(!"focus left YPP client window");
108 warning("focus entered YPP client window ?!");
111 eassert(!"unexpected X11 event");
116 static void send_key(KeySym sym) {
117 XTestFakeKeyEvent(disp, keycode(sym),1, 10);
118 XTestFakeKeyEvent(disp, keycode(sym),0, 10);
121 static void send_pgup_many(void) {
125 debugf("PAGING PageUp x %d\n",i);
128 static void send_pgdown(void) {
130 debugf("PAGING PageDown\n");
134 static void free_snapshot(Snapshot **io) {
135 if (*io) XDestroyImage(*io);
139 static void snapshot(Snapshot **output) {
140 free_snapshot(output);
142 debugf("PAGING snapshot\n");
145 *output= XGetImage(disp,id, 0,0, wwidth,wheight, AllPlanes, ZPixmap);
148 check_not_disturbed();
150 debugf("PAGING snapshot done.\n");
153 static int identical(const Snapshot *a, const Snapshot *b) {
154 if (!(a->width == b->width &&
155 a->height == b->height &&
156 a->bytes_per_line == b->bytes_per_line &&
157 a->format == b->format))
159 return !memcmp(a->data, b->data, a->bytes_per_line * a->height);
162 static void wait_for_stability(Snapshot **output,
163 const Snapshot *previously,
164 void (*with_keypress)(void),
165 const char *fmt, ...)
168 static void wait_for_stability(Snapshot **output,
169 const Snapshot *previously,
170 void (*with_keypress)(void),
171 const char *fmt, ...) {
177 /* waits longer if we're going to return an image identical to previously
178 * if previously==0, all images are considered identical to it */
180 debugf("PAGING wait_for_stability"
181 " last_input=%f previously=%p\n",
182 last_input, previously);
185 r= vasprintf(&doing,fmt,al); eassert(r>=0);
187 progress("%s",doing);
190 double at_snapshot= timestamp();
191 double need_sleep= min_update_allowance - (at_snapshot - last_input);
192 if (need_sleep > 0) { delay(need_sleep); continue; }
196 if (!with_keypress &&
197 !(previously && identical(*output,previously))) {
198 debugf("PAGING wait_for_stability simple\n");
202 if (last && identical(*output,last)) {
203 debugf("PAGING wait_for_stability stabilised\n");
207 progress_spinner("%s",doing);
209 debugf("PAGING wait_for_stability retry\n");
211 free_snapshot(&last); last=*output; *output=0;
219 free_snapshot(&last);
221 debugf("PAGING wait_for_stability done.\n");
225 static void raise_and_get_details(void) {
227 int evbase,errbase,majver,minver;
231 progress("raising and checking YPP client window...");
233 debugf("PAGING raise_and_get_details\n");
235 r= XTestQueryExtension(disp, &evbase,&errbase,&majver,&minver);
238 r= XRaiseWindow(disp, id); eassert(r);
239 /* in case VisibilityNotify triggers right away before we have had a
240 * change to raise; to avoid falsely detecting lowering in that case */
242 r= XSelectInput(disp, id,
247 r= XRaiseWindow(disp, id); eassert(r);
248 /* in case the window was lowered between our Raise and our SelectInput;
249 * to avoid failing to detect that lowering */
251 r= XGetWindowAttributes(disp, id, &attr); eassert(r);
252 r= XGetGeometry(disp,id, &attr.root,
253 &wxpos,&wypos, &wwidth,&wheight,
256 eassert(wwidth >= 320 && wheight >= 320);
258 check_client_window_all_on_screen();
261 static void set_focus(void) {
263 int screen= XScreenNumberOfScreen(attr.screen);
265 progress("taking control of YPP client window...");
267 debugf("PAGING set_focus\n");
270 translate_coords_toroot(160,160, &xpos,&ypos);
271 XTestFakeMotionEvent(disp,screen, xpos,ypos, 0);
273 XTestFakeButtonEvent(disp,1,1, 50);
274 XTestFakeButtonEvent(disp,1,0, 50);
279 r= XSelectInput(disp, id,
281 VisibilityChangeMask|
285 translate_coords_toroot(10,10, &xpos,&ypos);
286 XTestFakeMotionEvent(disp,screen, xpos,ypos, 0);
290 debugf("PAGING raise_and_set_focus done.\n");
293 #define SAMPLEMASK 0xfful
299 static void compute_shift_mask(ShMask *sm, unsigned long ximage_mask) {
304 if (ximage_mask <= (SAMPLEMASK>>1)) {
305 sm->lshift++; ximage_mask <<= 1;
306 } else if (ximage_mask > SAMPLEMASK) {
307 sm->rshift++; ximage_mask >>= 1;
311 assert(!(sm->lshift && sm->rshift));
313 assert(sm->lshift < LONG_BIT);
314 assert(sm->rshift < LONG_BIT);
317 static CanonImage *convert_page(Snapshot *sn) {
318 ShMask shiftmasks[3];
321 fprintf(screenshots_file,
324 "255\n", sn->width, sn->height);
326 #define COMPUTE_SHIFT_MASK(ix, rgb) \
327 compute_shift_mask(&shiftmasks[ix], sn->rgb##_mask)
328 COMPUTE_SHIFT_MASK(0, red);
329 COMPUTE_SHIFT_MASK(1, green);
330 COMPUTE_SHIFT_MASK(2, blue);
332 CANONICALISE_IMAGE(im, sn->width, sn->height, {
333 long xrgb= XGetPixel(sn, x, y);
336 for (i=0; i<3; i++) {
338 unsigned long sample=
339 ((xrgb << shiftmasks[i].lshift)
340 >> shiftmasks[i].rshift) & SAMPLEMASK;
342 fputc(sample, screenshots_file);
346 eassert(!fflush(screenshots_file));
351 void take_screenshots(void) {
352 Snapshot *current=0, *last=0;
355 /* find the window and check it's on the right kind of screen */
356 raise_and_get_details();
357 wait_for_stability(¤t,0,0, "checking current YPP client screen...");
358 test= convert_page(current);
359 find_structure(test);
362 /* page to the top - keep pressing page up until the image stops changing */
364 wait_for_stability(¤t,0, send_pgup_many,
365 "paging up to top of commodity list...");
367 /* now to actually page down */
369 debugf("paging page %d\n",npages);
371 eassert(npages < MAX_PAGES);
372 page_images[npages]= convert_page(current);
373 free_snapshot(&last); last=current; current=0;
375 debugf("PAGING page %d converted\n",npages);
377 wait_for_stability(¤t,last, 0,
378 "collecting screenshot of page %d...",
381 if (npages && /* first pagedown doesn't do much */
382 identical(current,last)) {
383 free_snapshot(¤t);
390 debugf("PAGING all done.\n");
391 progress_log("collected %d screenshots.",npages);
394 void take_one_screenshot(void) {
397 raise_and_get_details();
399 wait_for_stability(¤t,0,0, "taking screenshot...");
400 page_images[0]= convert_page(current);
402 progress_log("collected single screenshot.");
405 void set_yppclient_window(unsigned long wul) {
409 DEBUG_DEFINE_SOME_DEBUGF(findypp,debugfind)
411 void find_yppclient_window(void) {
412 Window root, gotroot, gotparent;
418 progress("looking for YPP client window...");
420 static const char prefix[]= "Puzzle Pirates - ";
421 static const char onthe[]= " on the ";
422 static const char suffix[]= " ocean";
423 #define S(x) (sizeof((x))-1)
425 Atom wm_name= XInternAtom(disp,"WM_NAME",True);
426 eassert(wm_name != None);
428 for (screen=0; screen<ScreenCount(disp); screen++) {
429 debugfind("FINDYPP screen %d\n", screen);
430 root= RootWindow(disp,screen);
431 unsigned int nchildren1;
434 r= XQueryTree(disp,root,
436 &children1,&nchildren1);
438 debugfind("FINDYPP screen %d nchildren1=%d\n", screen, nchildren1);
441 for (i=0; i<nchildren1; i++) {
442 Window w1= children1[i];
443 unsigned int nchildren2;
446 r= XQueryTree(disp,w1,
448 &children2,&nchildren2);
450 debugfind("FINDYPP screen %d c1[%2d]=0x%08lx nchildren2=%d\n",
451 screen, i, (unsigned long)w1, nchildren2);
454 for (j=-1; j<(int)nchildren2; j++) {
455 Window w2= j<0 ? w1 : children2[j];
456 debugfind("FINDYPP screen %d c1[%2d]=0x%08lx c2[%2d]=0x%08lx",
457 screen, i, (unsigned long)w1, j, (unsigned long)w2);
461 unsigned long len, gotbytesafter;
463 unsigned char *gottitle=0;
464 r= XGetWindowProperty(disp,w2, wm_name,0,512, False,
465 AnyPropertyType,&gottype, &gotfmt, &len,
466 &gotbytesafter, &gottitle);
468 title= (char*)gottitle;
470 if (DEBUGP(findypp)) {
471 debugfind(" gf=%d len=%lu gba=%lu \"", gotfmt,len,gotbytesafter);
473 for (p=title; p < title+len; p++) {
475 if (c>=' ' && c<=126) fputc(c,debug);
476 else fprintf(debug,"\\x%02x",c & 0xff);
481 #define REQUIRE(pred) \
482 if (!(pred)) { debugfind(" failed test %s\n", #pred); continue; } \
485 REQUIRE( gottype!=None );
487 REQUIRE( gotfmt==8 );
489 REQUIRE( len >= S(prefix) + 1 + S(onthe) + 1 + S(suffix) );
491 char *spc1= strchr( title + S(prefix), ' '); REQUIRE(spc1);
492 char *spc2= strrchr((title + len) - S(suffix), ' '); REQUIRE(spc2);
494 REQUIRE( (title + len) - spc1 >= S(onthe) + S(suffix) );
495 REQUIRE( spc2 - title >= S(prefix) + S(onthe) );
497 REQUIRE( !memcmp(title, prefix, S(prefix)) );
498 REQUIRE( !memcmp(title + len - S(suffix), suffix, S(suffix)) );
499 REQUIRE( !memcmp(spc1, onthe, S(onthe)) );
501 #define ASSIGN(what, start, end) do { \
502 r= asprintf(&what, "%.*s", end - start, start); eassert(r>0); \
504 ASSIGN(ocean, title + S(prefix), spc1);
505 ASSIGN(pirate, spc1 + S(onthe), (title + len) - S(suffix));
507 debugfind(" YES!\n");
510 progress_log("found YPP client window (0x%lx): %s on the %s ocean.",
511 (unsigned long)id, pirate, ocean);
513 if (children2) XFree(children2);
515 if (children1) XFree(children1);