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;
21 static int wxpos, wypos;
23 DEBUG_DEFINE_DEBUGF(pages)
25 static KeyCode keycode(KeySym sym) {
26 return XKeysymToKeycode(disp,sym);
29 void screenshot_startup(void) {
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 raise_and_get_details(void) {
157 int evbase,errbase,majver,minver;
161 debugf("PAGING raise_and_get_details\n");
163 r= XTestQueryExtension(disp, &evbase,&errbase,&majver,&minver);
166 r= XRaiseWindow(disp, id); eassert(r);
168 r= XGetWindowAttributes(disp, id, &attr); eassert(r);
169 r= XGetGeometry(disp,id, &attr.root,
170 &wxpos,&wypos, &wwidth,&wheight,
174 r= XTranslateCoordinates(disp, id,attr.root, 160,160, &wxpos,&wypos,
179 static void set_focus(void) {
180 int screen= XScreenNumberOfScreen(attr.screen);
182 debugf("PAGING set_focus\n");
184 XTestFakeMotionEvent(disp,screen, wxpos,wypos, 0);
186 XTestFakeButtonEvent(disp,1,1, 50);
187 XTestFakeButtonEvent(disp,1,0, 50);
190 debugf("PAGING raise_and_set_focus done.\n");
193 #define SAMPLEMASK 0xfful
199 static void compute_shift_mask(ShMask *sm, unsigned long ximage_mask) {
204 if (ximage_mask <= (SAMPLEMASK>>1)) {
205 sm->lshift++; ximage_mask <<= 1;
206 } else if (ximage_mask > SAMPLEMASK) {
207 sm->rshift++; ximage_mask >>= 1;
211 assert(!(sm->lshift && sm->rshift));
213 assert(sm->lshift < LONG_BIT);
214 assert(sm->rshift < LONG_BIT);
217 static CanonImage *convert_page(Snapshot *sn) {
218 ShMask shiftmasks[3];
221 fprintf(screenshots_file,
224 "255\n", sn->width, sn->height);
226 #define COMPUTE_SHIFT_MASK(ix, rgb) \
227 compute_shift_mask(&shiftmasks[ix], sn->rgb##_mask)
228 COMPUTE_SHIFT_MASK(0, red);
229 COMPUTE_SHIFT_MASK(1, green);
230 COMPUTE_SHIFT_MASK(2, blue);
232 CANONICALISE_IMAGE(im, sn->width, sn->height, {
233 long xrgb= XGetPixel(sn, x, y);
236 for (i=0; i<3; i++) {
238 unsigned long sample=
239 ((xrgb << shiftmasks[i].lshift)
240 >> shiftmasks[i].rshift) & SAMPLEMASK;
242 fputc(sample, screenshots_file);
246 eassert(!fflush(screenshots_file));
251 void take_screenshots(void) {
252 Snapshot *current=0, *last=0;
255 /* find the window and check it's on the right kind of screen */
256 raise_and_get_details();
257 wait_for_stability(¤t,0,0);
258 test= convert_page(current);
259 find_structure(test);
262 /* page to the top - keep pressing page up until the image stops changing */
264 wait_for_stability(¤t,0, send_pgup_many);
266 /* now to actually page down */
268 debugf("paging page %d\n",npages);
270 eassert(npages < MAX_PAGES);
271 page_images[npages]= convert_page(current);
272 free_snapshot(&last); last=current; current=0;
274 debugf("PAGING page %d converted\n",npages);
276 wait_for_stability(¤t,last, 0);
277 if (npages && /* first pagedown doesn't do much */
278 identical(current,last)) {
279 free_snapshot(¤t);
286 debugf("PAGING all done.\n");
289 void take_one_screenshot(void) {
292 raise_and_get_details();
294 wait_for_stability(¤t,0,0);
295 page_images[0]= convert_page(current);
299 void set_yppclient_window(unsigned long wul) {
303 DEBUG_DEFINE_SOME_DEBUGF(findypp,debugfind)
305 void find_yppclient_window(void) {
306 Window root, gotroot, gotparent;
312 static const char prefix[]= "Puzzle Pirates - ";
313 static const char onthe[]= " on the ";
314 static const char suffix[]= " ocean";
315 #define S(x) (sizeof((x))-1)
317 Atom wm_name= XInternAtom(disp,"WM_NAME",True);
318 eassert(wm_name != None);
320 for (screen=0; screen<ScreenCount(disp); screen++) {
321 debugfind("FINDYPP screen %d\n", screen);
322 root= RootWindow(disp,screen);
323 unsigned int nchildren1;
326 r= XQueryTree(disp,root,
328 &children1,&nchildren1);
330 debugfind("FINDYPP screen %d nchildren1=%d\n", screen, nchildren1);
333 for (i=0; i<nchildren1; i++) {
334 Window w1= children1[i];
335 unsigned int nchildren2;
338 r= XQueryTree(disp,w1,
340 &children2,&nchildren2);
342 debugfind("FINDYPP screen %d c1[%2d]=0x%08lx nchildren2=%d\n",
343 screen, i, (unsigned long)w1, nchildren2);
346 for (j=-1; j<(int)nchildren2; j++) {
347 Window w2= j<0 ? w1 : children2[j];
348 debugfind("FINDYPP screen %d c1[%2d]=0x%08lx c2[%2d]=0x%08lx",
349 screen, i, (unsigned long)w1, j, (unsigned long)w2);
353 unsigned long len, gotbytesafter;
355 unsigned char *gottitle=0;
356 r= XGetWindowProperty(disp,w2, wm_name,0,512, False,
357 AnyPropertyType,&gottype, &gotfmt, &len,
358 &gotbytesafter, &gottitle);
360 title= (char*)gottitle;
362 if (DEBUGP(findypp)) {
363 debugfind(" gf=%d len=%lu gba=%lu \"", gotfmt,len,gotbytesafter);
365 for (p=title; p < title+len; p++) {
367 if (c>=' ' && c<=126) fputc(c,debug);
368 else fprintf(debug,"\\x%02x",c & 0xff);
373 #define REQUIRE(pred) \
374 if (!(pred)) { debugfind(" failed test %s\n", #pred); continue; } \
377 REQUIRE( gottype!=None );
379 REQUIRE( gotfmt==8 );
381 REQUIRE( len >= S(prefix) + 1 + S(onthe) + 1 + S(suffix) );
383 char *spc1= strchr( title + S(prefix), ' '); REQUIRE(spc1);
384 char *spc2= strrchr((title + len) - S(suffix), ' '); REQUIRE(spc2);
386 REQUIRE( (title + len) - spc1 >= S(onthe) + S(suffix) );
387 REQUIRE( spc2 - title >= S(prefix) + S(onthe) );
389 REQUIRE( !memcmp(title, prefix, S(prefix)) );
390 REQUIRE( !memcmp(title + len - S(suffix), suffix, S(suffix)) );
391 REQUIRE( !memcmp(spc1, onthe, S(onthe)) );
393 #define ASSIGN(what, start, end) do { \
394 r= asprintf(&what, "%.*s", end - start, start); eassert(r>0); \
396 ASSIGN(ocean, title + S(prefix), spc1);
397 ASSIGN(pirate, spc1 + S(onthe), (title + len) - S(suffix));
399 debugfind(" YES!\n");
403 if (children2) XFree(children2);
405 if (children1) XFree(children1);