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 send_key(KeySym sym) {
65 XTestFakeKeyEvent(disp, keycode(sym),1, 10);
66 XTestFakeKeyEvent(disp, keycode(sym),0, 10);
69 static void send_pgup_many(void) {
73 debugf("PAGING PageUp x %d\n",i);
76 static void send_pgdown(void) {
78 debugf("PAGING PageDown\n");
82 static void free_snapshot(Snapshot **io) {
83 if (*io) XDestroyImage(*io);
87 static void snapshot(Snapshot **output) {
88 free_snapshot(output);
90 debugf("PAGING snapshot\n");
93 *output= XGetImage(disp,id, 0,0, wwidth,wheight, AllPlanes, ZPixmap);
96 debugf("PAGING snapshot done.\n");
99 static int identical(const Snapshot *a, const Snapshot *b) {
100 if (!(a->width == b->width &&
101 a->height == b->height &&
102 a->bytes_per_line == b->bytes_per_line &&
103 a->format == b->format))
105 return !memcmp(a->data, b->data, a->bytes_per_line * a->height);
108 static void wait_for_stability(Snapshot **output,
109 const Snapshot *previously,
110 void (*with_keypress)(void),
111 const char *fmt, ...) {
114 /* waits longer if we're going to return an image identical to previously
115 * if previously==0, all images are considered identical to it */
117 debugf("PAGING wait_for_stability"
118 " last_input=%f previously=%p\n",
119 last_input, previously);
124 r= asprintf(&doing,fmt,al); eassert(r>=0);
130 double at_snapshot= timestamp();
131 double need_sleep= min_update_allowance - (at_snapshot - last_input);
132 if (need_sleep > 0) { delay(need_sleep); continue; }
136 if (!with_keypress &&
137 !(previously && identical(*output,previously))) {
138 debugf("PAGING wait_for_stability simple\n");
142 if (last && identical(*output,last)) {
143 debugf("PAGING wait_for_stability stabilised\n");
147 progress_spinner(doing);
149 debugf("PAGING wait_for_stability retry\n");
151 free_snapshot(&last); last=*output; *output=0;
159 free_snapshot(&last);
161 debugf("PAGING wait_for_stability done.\n");
164 static void translate_coords_toroot(int wx, int wy, int *rx, int *ry) {
167 r= XTranslateCoordinates(disp, id,attr.root, wx,wy, rx,ry, &dummy);
171 static void raise_and_get_details(void) {
173 int evbase,errbase,majver,minver;
178 progress("raising and checking YPP client window...");
180 debugf("PAGING raise_and_get_details\n");
182 r= XTestQueryExtension(disp, &evbase,&errbase,&majver,&minver);
185 r= XRaiseWindow(disp, id); eassert(r);
186 /* in case VisibilityNotify triggers right away before we have had a
187 * change to raise; to avoid falsely detecting lowering in that case */
189 r= XSelectInput(disp, id,
194 r= XRaiseWindow(disp, id); eassert(r);
195 /* in case the window was lowered between our Raise and our SelectInput;
196 * to avoid failing to detect that lowering */
198 r= XGetWindowAttributes(disp, id, &attr); eassert(r);
199 r= XGetGeometry(disp,id, &attr.root,
200 &wxpos,&wypos, &wwidth,&wheight,
203 eassert(wwidth >= 320 && wheight >= 320);
206 unsigned rwidth, rheight;
207 r= XGetGeometry(disp,attr.root, &dummy, &rxpos,&rypos,
211 translate_coords_toroot(0,0, &rxpos,&rypos);
212 eassert(rxpos>=0 && rypos>=0);
214 translate_coords_toroot(wwidth-1,wheight-1, &rxpos,&rypos);
215 eassert(rxpos<rwidth && rypos<rheight);
218 static void set_focus(void) {
219 int screen= XScreenNumberOfScreen(attr.screen);
221 progress("taking control of YPP client window...");
223 debugf("PAGING set_focus\n");
226 translate_coords_toroot(160,160, &xpos,&ypos);
227 XTestFakeMotionEvent(disp,screen, xpos,ypos, 0);
229 XTestFakeButtonEvent(disp,1,1, 50);
230 XTestFakeButtonEvent(disp,1,0, 50);
234 translate_coords_toroot(10,10, &xpos,&ypos);
235 XTestFakeMotionEvent(disp,screen, xpos,ypos, 0);
239 debugf("PAGING raise_and_set_focus done.\n");
242 #define SAMPLEMASK 0xfful
248 static void compute_shift_mask(ShMask *sm, unsigned long ximage_mask) {
253 if (ximage_mask <= (SAMPLEMASK>>1)) {
254 sm->lshift++; ximage_mask <<= 1;
255 } else if (ximage_mask > SAMPLEMASK) {
256 sm->rshift++; ximage_mask >>= 1;
260 assert(!(sm->lshift && sm->rshift));
262 assert(sm->lshift < LONG_BIT);
263 assert(sm->rshift < LONG_BIT);
266 static CanonImage *convert_page(Snapshot *sn) {
267 ShMask shiftmasks[3];
270 fprintf(screenshots_file,
273 "255\n", sn->width, sn->height);
275 #define COMPUTE_SHIFT_MASK(ix, rgb) \
276 compute_shift_mask(&shiftmasks[ix], sn->rgb##_mask)
277 COMPUTE_SHIFT_MASK(0, red);
278 COMPUTE_SHIFT_MASK(1, green);
279 COMPUTE_SHIFT_MASK(2, blue);
281 CANONICALISE_IMAGE(im, sn->width, sn->height, {
282 long xrgb= XGetPixel(sn, x, y);
285 for (i=0; i<3; i++) {
287 unsigned long sample=
288 ((xrgb << shiftmasks[i].lshift)
289 >> shiftmasks[i].rshift) & SAMPLEMASK;
291 fputc(sample, screenshots_file);
295 eassert(!fflush(screenshots_file));
300 void take_screenshots(void) {
301 Snapshot *current=0, *last=0;
304 /* find the window and check it's on the right kind of screen */
305 raise_and_get_details();
306 wait_for_stability(¤t,0,0, "checking current YPP client screen...");
307 test= convert_page(current);
308 find_structure(test);
311 /* page to the top - keep pressing page up until the image stops changing */
313 wait_for_stability(¤t,0, send_pgup_many,
314 "paging up to top of commodity list...");
316 /* now to actually page down */
318 debugf("paging page %d\n",npages);
320 eassert(npages < MAX_PAGES);
321 page_images[npages]= convert_page(current);
322 free_snapshot(&last); last=current; current=0;
324 debugf("PAGING page %d converted\n",npages);
326 wait_for_stability(¤t,last, 0,
327 "collecting screenshot of page %d...",npages+1);
329 if (npages && /* first pagedown doesn't do much */
330 identical(current,last)) {
331 free_snapshot(¤t);
338 debugf("PAGING all done.\n");
339 progress_log("collected %d screenshots.",npages);
342 void take_one_screenshot(void) {
345 raise_and_get_details();
347 wait_for_stability(¤t,0,0, "taking screenshot...");
348 page_images[0]= convert_page(current);
350 progress_log("collected single screenshot.");
353 void set_yppclient_window(unsigned long wul) {
357 DEBUG_DEFINE_SOME_DEBUGF(findypp,debugfind)
359 void find_yppclient_window(void) {
360 Window root, gotroot, gotparent;
366 progress("looking for YPP client window...");
368 static const char prefix[]= "Puzzle Pirates - ";
369 static const char onthe[]= " on the ";
370 static const char suffix[]= " ocean";
371 #define S(x) (sizeof((x))-1)
373 Atom wm_name= XInternAtom(disp,"WM_NAME",True);
374 eassert(wm_name != None);
376 for (screen=0; screen<ScreenCount(disp); screen++) {
377 debugfind("FINDYPP screen %d\n", screen);
378 root= RootWindow(disp,screen);
379 unsigned int nchildren1;
382 r= XQueryTree(disp,root,
384 &children1,&nchildren1);
386 debugfind("FINDYPP screen %d nchildren1=%d\n", screen, nchildren1);
389 for (i=0; i<nchildren1; i++) {
390 Window w1= children1[i];
391 unsigned int nchildren2;
394 r= XQueryTree(disp,w1,
396 &children2,&nchildren2);
398 debugfind("FINDYPP screen %d c1[%2d]=0x%08lx nchildren2=%d\n",
399 screen, i, (unsigned long)w1, nchildren2);
402 for (j=-1; j<(int)nchildren2; j++) {
403 Window w2= j<0 ? w1 : children2[j];
404 debugfind("FINDYPP screen %d c1[%2d]=0x%08lx c2[%2d]=0x%08lx",
405 screen, i, (unsigned long)w1, j, (unsigned long)w2);
409 unsigned long len, gotbytesafter;
411 unsigned char *gottitle=0;
412 r= XGetWindowProperty(disp,w2, wm_name,0,512, False,
413 AnyPropertyType,&gottype, &gotfmt, &len,
414 &gotbytesafter, &gottitle);
416 title= (char*)gottitle;
418 if (DEBUGP(findypp)) {
419 debugfind(" gf=%d len=%lu gba=%lu \"", gotfmt,len,gotbytesafter);
421 for (p=title; p < title+len; p++) {
423 if (c>=' ' && c<=126) fputc(c,debug);
424 else fprintf(debug,"\\x%02x",c & 0xff);
429 #define REQUIRE(pred) \
430 if (!(pred)) { debugfind(" failed test %s\n", #pred); continue; } \
433 REQUIRE( gottype!=None );
435 REQUIRE( gotfmt==8 );
437 REQUIRE( len >= S(prefix) + 1 + S(onthe) + 1 + S(suffix) );
439 char *spc1= strchr( title + S(prefix), ' '); REQUIRE(spc1);
440 char *spc2= strrchr((title + len) - S(suffix), ' '); REQUIRE(spc2);
442 REQUIRE( (title + len) - spc1 >= S(onthe) + S(suffix) );
443 REQUIRE( spc2 - title >= S(prefix) + S(onthe) );
445 REQUIRE( !memcmp(title, prefix, S(prefix)) );
446 REQUIRE( !memcmp(title + len - S(suffix), suffix, S(suffix)) );
447 REQUIRE( !memcmp(spc1, onthe, S(onthe)) );
449 #define ASSIGN(what, start, end) do { \
450 r= asprintf(&what, "%.*s", end - start, start); eassert(r>0); \
452 ASSIGN(ocean, title + S(prefix), spc1);
453 ASSIGN(pirate, spc1 + S(onthe), (title + len) - S(suffix));
455 debugfind(" YES!\n");
458 progress_log("found YPP client window (0x%lx): %s on the %s ocean.",
459 (unsigned long)id, pirate, ocean);
461 if (children2) XFree(children2);
463 if (children1) XFree(children1);