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) {
92 free_snapshot(output);
94 debugf("PAGING snapshot\n");
97 *output= XGetImage(disp,id, 0,0, wwidth,wheight, AllPlanes, ZPixmap);
100 debugf("PAGING snapshot done.\n");
103 static int identical(const Snapshot *a, const Snapshot *b) {
104 if (!(a->width == b->width &&
105 a->height == b->height &&
106 a->bytes_per_line == b->bytes_per_line &&
107 a->format == b->format))
109 return !memcmp(a->data, b->data, a->bytes_per_line * a->height);
112 static void wait_for_stability(Snapshot **output,
113 const Snapshot *previously,
114 void (*with_keypress)(void)) {
116 /* waits longer if we're going to return an image identical to previously
117 * if previously==0, all images are considered identical to it */
119 debugf("PAGING wait_for_stability"
120 " last_input=%f previously=%p\n",
121 last_input, previously);
124 double at_snapshot= timestamp();
125 double need_sleep= min_update_allowance - (at_snapshot - last_input);
126 if (need_sleep > 0) { delay(need_sleep); continue; }
130 if (!with_keypress &&
131 !(previously && identical(*output,previously))) {
132 debugf("PAGING wait_for_stability simple\n");
136 if (last && identical(*output,last)) {
137 debugf("PAGING wait_for_stability stabilised\n");
141 debugf("PAGING wait_for_stability retry\n");
143 free_snapshot(&last); last=*output; *output=0;
151 free_snapshot(&last);
152 debugf("PAGING wait_for_stability done.\n");
155 static void translate_coords_toroot(int wx, int wy, int *rx, int *ry) {
158 r= XTranslateCoordinates(disp, id,attr.root, wx,wy, rx,ry, &dummy);
162 static void raise_and_get_details(void) {
164 int evbase,errbase,majver,minver;
169 progress("raising and checking YPP client window ...\n");
171 debugf("PAGING raise_and_get_details\n");
173 r= XTestQueryExtension(disp, &evbase,&errbase,&majver,&minver);
176 r= XRaiseWindow(disp, id); eassert(r);
177 /* in case VisibilityNotify triggers right away before we have had a
178 * change to raise; to avoid falsely detecting lowering in that case */
180 r= XSelectInput(disp, id,
185 r= XRaiseWindow(disp, id); eassert(r);
186 /* in case the window was lowered between our Raise and our SelectInput;
187 * to avoid failing to detect that lowering */
189 r= XGetWindowAttributes(disp, id, &attr); eassert(r);
190 r= XGetGeometry(disp,id, &attr.root,
191 &wxpos,&wypos, &wwidth,&wheight,
194 eassert(wwidth >= 320 && wheight >= 320);
197 unsigned rwidth, rheight;
198 r= XGetGeometry(disp,attr.root, &dummy, &rxpos,&rypos,
202 translate_coords_toroot(0,0, &rxpos,&rypos);
203 eassert(rxpos>=0 && rypos>=0);
205 translate_coords_toroot(wwidth-1,wheight-1, &rxpos,&rypos);
206 eassert(rxpos<rwidth && rypos<rheight);
209 static void set_focus(void) {
210 int screen= XScreenNumberOfScreen(attr.screen);
212 progress("taking control of YPP client window ...\n");
214 debugf("PAGING set_focus\n");
217 translate_coords_toroot(160,160, &xpos,&ypos);
218 XTestFakeMotionEvent(disp,screen, xpos,ypos, 0);
220 XTestFakeButtonEvent(disp,1,1, 50);
221 XTestFakeButtonEvent(disp,1,0, 50);
225 translate_coords_toroot(10,10, &xpos,&ypos);
226 XTestFakeMotionEvent(disp,screen, xpos,ypos, 0);
230 debugf("PAGING raise_and_set_focus done.\n");
233 #define SAMPLEMASK 0xfful
239 static void compute_shift_mask(ShMask *sm, unsigned long ximage_mask) {
244 if (ximage_mask <= (SAMPLEMASK>>1)) {
245 sm->lshift++; ximage_mask <<= 1;
246 } else if (ximage_mask > SAMPLEMASK) {
247 sm->rshift++; ximage_mask >>= 1;
251 assert(!(sm->lshift && sm->rshift));
253 assert(sm->lshift < LONG_BIT);
254 assert(sm->rshift < LONG_BIT);
257 static CanonImage *convert_page(Snapshot *sn) {
258 ShMask shiftmasks[3];
261 fprintf(screenshots_file,
264 "255\n", sn->width, sn->height);
266 #define COMPUTE_SHIFT_MASK(ix, rgb) \
267 compute_shift_mask(&shiftmasks[ix], sn->rgb##_mask)
268 COMPUTE_SHIFT_MASK(0, red);
269 COMPUTE_SHIFT_MASK(1, green);
270 COMPUTE_SHIFT_MASK(2, blue);
272 CANONICALISE_IMAGE(im, sn->width, sn->height, {
273 long xrgb= XGetPixel(sn, x, y);
276 for (i=0; i<3; i++) {
278 unsigned long sample=
279 ((xrgb << shiftmasks[i].lshift)
280 >> shiftmasks[i].rshift) & SAMPLEMASK;
282 fputc(sample, screenshots_file);
286 eassert(!fflush(screenshots_file));
291 void take_screenshots(void) {
292 Snapshot *current=0, *last=0;
295 /* find the window and check it's on the right kind of screen */
296 raise_and_get_details();
297 wait_for_stability(¤t,0,0);
298 test= convert_page(current);
299 find_structure(test);
302 /* page to the top - keep pressing page up until the image stops changing */
304 wait_for_stability(¤t,0, send_pgup_many);
306 /* now to actually page down */
308 debugf("paging page %d\n",npages);
310 eassert(npages < MAX_PAGES);
311 page_images[npages]= convert_page(current);
312 free_snapshot(&last); last=current; current=0;
314 debugf("PAGING page %d converted\n",npages);
316 wait_for_stability(¤t,last, 0);
317 if (npages && /* first pagedown doesn't do much */
318 identical(current,last)) {
319 free_snapshot(¤t);
326 debugf("PAGING all done.\n");
329 void take_one_screenshot(void) {
332 raise_and_get_details();
334 wait_for_stability(¤t,0,0);
335 page_images[0]= convert_page(current);
339 void set_yppclient_window(unsigned long wul) {
343 DEBUG_DEFINE_SOME_DEBUGF(findypp,debugfind)
345 void find_yppclient_window(void) {
346 Window root, gotroot, gotparent;
352 static const char prefix[]= "Puzzle Pirates - ";
353 static const char onthe[]= " on the ";
354 static const char suffix[]= " ocean";
355 #define S(x) (sizeof((x))-1)
357 Atom wm_name= XInternAtom(disp,"WM_NAME",True);
358 eassert(wm_name != None);
360 for (screen=0; screen<ScreenCount(disp); screen++) {
361 debugfind("FINDYPP screen %d\n", screen);
362 root= RootWindow(disp,screen);
363 unsigned int nchildren1;
366 r= XQueryTree(disp,root,
368 &children1,&nchildren1);
370 debugfind("FINDYPP screen %d nchildren1=%d\n", screen, nchildren1);
373 for (i=0; i<nchildren1; i++) {
374 Window w1= children1[i];
375 unsigned int nchildren2;
378 r= XQueryTree(disp,w1,
380 &children2,&nchildren2);
382 debugfind("FINDYPP screen %d c1[%2d]=0x%08lx nchildren2=%d\n",
383 screen, i, (unsigned long)w1, nchildren2);
386 for (j=-1; j<(int)nchildren2; j++) {
387 Window w2= j<0 ? w1 : children2[j];
388 debugfind("FINDYPP screen %d c1[%2d]=0x%08lx c2[%2d]=0x%08lx",
389 screen, i, (unsigned long)w1, j, (unsigned long)w2);
393 unsigned long len, gotbytesafter;
395 unsigned char *gottitle=0;
396 r= XGetWindowProperty(disp,w2, wm_name,0,512, False,
397 AnyPropertyType,&gottype, &gotfmt, &len,
398 &gotbytesafter, &gottitle);
400 title= (char*)gottitle;
402 if (DEBUGP(findypp)) {
403 debugfind(" gf=%d len=%lu gba=%lu \"", gotfmt,len,gotbytesafter);
405 for (p=title; p < title+len; p++) {
407 if (c>=' ' && c<=126) fputc(c,debug);
408 else fprintf(debug,"\\x%02x",c & 0xff);
413 #define REQUIRE(pred) \
414 if (!(pred)) { debugfind(" failed test %s\n", #pred); continue; } \
417 REQUIRE( gottype!=None );
419 REQUIRE( gotfmt==8 );
421 REQUIRE( len >= S(prefix) + 1 + S(onthe) + 1 + S(suffix) );
423 char *spc1= strchr( title + S(prefix), ' '); REQUIRE(spc1);
424 char *spc2= strrchr((title + len) - S(suffix), ' '); REQUIRE(spc2);
426 REQUIRE( (title + len) - spc1 >= S(onthe) + S(suffix) );
427 REQUIRE( spc2 - title >= S(prefix) + S(onthe) );
429 REQUIRE( !memcmp(title, prefix, S(prefix)) );
430 REQUIRE( !memcmp(title + len - S(suffix), suffix, S(suffix)) );
431 REQUIRE( !memcmp(spc1, onthe, S(onthe)) );
433 #define ASSIGN(what, start, end) do { \
434 r= asprintf(&what, "%.*s", end - start, start); eassert(r>0); \
436 ASSIGN(ocean, title + S(prefix), spc1);
437 ASSIGN(pirate, spc1 + S(onthe), (title + len) - S(suffix));
439 debugfind(" YES!\n");
442 progress_log("found YPP client window 0x%lx: %s on the %s ocean\n",
443 (unsigned long)id, pirate, ocean);
445 if (children2) XFree(children2);
447 if (children1) XFree(children1);