chiark / gitweb /
Set page0_rgbimage when taking many screenshots
[ypp-sc-tools.db-test.git] / pctb / pages.c
1 /*
2  * Interaction with the YPP client via X11
3  */
4 /*
5  *  This is part of ypp-sc-tools, a set of third-party tools for assisting
6  *  players of Yohoho Puzzle Pirates.
7  * 
8  *  Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
9  * 
10  *  This program is free software: you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation, either version 3 of the License, or
13  *  (at your option) any later version.
14  * 
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  * 
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  * 
23  *  Yohoho and Puzzle Pirates are probably trademarks of Three Rings and
24  *  are used without permission.  This program is not endorsed or
25  *  sponsored by Three Rings.
26  */
27
28 /*
29  * Only this file #includes the X11 headers, as they're quite
30  * pollutant of the namespace.
31  */
32
33 #include "structure.h"
34
35 #include <X11/Xlib.h>
36 #include <X11/extensions/XTest.h>
37 #include <X11/keysym.h>
38 #include <X11/Xutil.h>
39
40 #include <X11/extensions/XShm.h>
41 #include <sys/ipc.h>
42 #include <sys/shm.h>
43
44 CanonImage *page_images[MAX_PAGES];
45 int npages;
46 RgbImage *page0_rgbimage;
47
48 const char *ocean, *pirate;
49
50 static XWindowAttributes attr;
51 static Window id;
52 static Display *disp;
53 static struct timeval tv_startup;
54 static unsigned wwidth, wheight;
55 static int max_relevant_y= -1;
56
57 static XImage *shmim;
58 static XShmSegmentInfo shminfo;
59
60 DEBUG_DEFINE_DEBUGF(pages)
61
62 #define xassert(what)                                   \
63   ((what) ? (void)0 :                                   \
64    fatal("X11 operation unexpectedly failed."           \
65          " %s:%d: %s\n", __FILE__,__LINE__,#what))
66
67 static KeyCode keycode(KeySym sym) {
68   return XKeysymToKeycode(disp,sym);
69 }
70
71 void screenshot_startup(void) {
72   progress("starting...");
73   disp= XOpenDisplay(0);
74   if (!disp) fatal("Unable to open X11 display.");
75   sysassert(! gettimeofday(&tv_startup,0) );
76 }
77
78 /*---------- pager ----------*/
79
80 typedef RgbImage Snapshot;
81
82 static double last_input;
83 static const double min_update_allowance= 0.25;
84
85 static double timestamp(void) {
86   struct timeval tv;
87   
88   sysassert(! gettimeofday(&tv,0) );
89   double t= (tv.tv_sec - tv_startup.tv_sec) +
90             (tv.tv_usec - tv_startup.tv_usec) * 1e-6;
91   debugf("PAGING %f\n",t);
92   return t;
93 }
94 static void delay(double need_sleep) {
95   debugf("PAGING     delay %f\n",need_sleep);
96   sysassert(! usleep(need_sleep * 1e6) );
97 }
98
99 static void sync_after_input(void) {
100   xassert( XSync(disp, False) );
101   last_input= timestamp();
102 }
103
104 static void translate_coords_toroot(int wx, int wy, int *rx, int *ry) {
105   Window dummy;
106   xassert( XTranslateCoordinates(disp, id,attr.root, wx,wy, rx,ry, &dummy) );
107 }
108
109 static void check_client_window_all_on_screen(void) {
110   Rect onroot;
111   unsigned rwidth, rheight;
112   Window dummy;
113   unsigned bd, depth;
114   int rxpos, rypos;
115
116   xassert( XGetGeometry(disp,attr.root, &dummy, &rxpos,&rypos,
117                         &rwidth, &rheight,
118                         &bd,&depth) );
119   
120   translate_coords_toroot(0,0, &onroot.tl.x,&onroot.tl.y);
121   translate_coords_toroot(wwidth-1,wheight-1, &onroot.br.x,&onroot.br.y);
122   if (!(onroot.tl.x >= 0 &&
123         onroot.tl.y >= 0 &&
124         onroot.br.x < rwidth &&
125         onroot.br.y < rheight))
126     fatal("YPP client window is not entirely on the screen.");
127 }
128
129 static void check_not_disturbed(void) {
130   XEvent ev;
131   int r;
132   
133   for (;;) {
134     r= XCheckMaskEvent(disp, ~0, &ev);
135     if (r==False) return;
136
137     switch (ev.type) {
138     case VisibilityNotify:
139       if (ev.xvisibility.state != VisibilityUnobscured)
140         fatal("YPP client window has become obscured.");
141       break;
142     case ConfigureNotify:
143       check_client_window_all_on_screen();
144       break;
145     case FocusOut:
146       fatal("Focus left YPP client window.");
147       break;
148     case FocusIn:
149       warning("focus entered YPP client window ?!");
150       break;
151     default:
152       fatal("Received unexpected X11 event (type code %d)!", ev.type);
153     }
154   }
155 }      
156
157 static void send_key(KeySym sym) {
158   check_not_disturbed();
159   XTestFakeKeyEvent(disp, keycode(sym),1, 10);
160   XTestFakeKeyEvent(disp, keycode(sym),0, 10);
161 }
162 static void mouse_1_updown_here(void) {
163   check_not_disturbed();
164   XTestFakeButtonEvent(disp,1,1, 50);
165   XTestFakeButtonEvent(disp,1,0, 50);
166 }
167 static void mouse_1_updown(int x, int y) {
168   check_not_disturbed();
169   int screen= XScreenNumberOfScreen(attr.screen);
170   int xpos, ypos;
171   translate_coords_toroot(x,y, &xpos,&ypos);
172   XTestFakeMotionEvent(disp, screen, xpos,ypos, 0);
173   mouse_1_updown_here();
174 }
175
176 static int pgupdown;
177
178 static void send_pgup_many(void) {
179   int i;
180   for (i=0; i<25; i++) {
181     send_key(XK_Prior);
182     pgupdown--;
183   }
184   debugf("PAGING   PageUp x %d\n",i);
185   sync_after_input();
186 }
187 static void send_pgdown(void) {
188   send_key(XK_Next);
189   pgupdown++;
190   debugf("PAGING   PageDown\n");
191   sync_after_input();
192 }
193 static void send_pgdown_torestore(void) {
194   debugf("PAGING   PageDown x %d\n", -pgupdown);
195   while (pgupdown < 0) {
196     send_key(XK_Next);
197     pgupdown++;
198   }
199   sync_after_input();
200 }
201
202 static void free_snapshot(Snapshot **io) {
203   free(*io);
204   *io= 0;
205 }
206
207 #define SAMPLEMASK 0xfful
208
209 typedef struct {
210   int lshift, rshift;
211 } ShMask;
212
213 static void compute_shift_mask(ShMask *sm, unsigned long ximage_mask) {
214   sm->lshift= 0;
215   sm->rshift= 0;
216   
217   for (;;) {
218     if (ximage_mask <= (SAMPLEMASK>>1)) {
219       sm->lshift++;  ximage_mask <<= 1;
220     } else if (ximage_mask > SAMPLEMASK) {
221       sm->rshift++;  ximage_mask >>= 1;
222     } else {
223       break;
224     }
225     assert(!(sm->lshift && sm->rshift));
226   }
227   assert(sm->lshift < LONG_BIT);
228   assert(sm->rshift < LONG_BIT);
229 }
230
231 static void rtimestamp(double *t, const char *wh) {
232   double n= timestamp();
233   debugf("PAGING                INTERVAL %f  %s\n", n-*t, wh);
234   *t= n;
235 }
236
237 static void snapshot(Snapshot **output) {
238   XImage *im_use, *im_free=0;
239
240   ShMask shiftmasks[3];
241
242   debugf("PAGING   snapshot\n");
243
244   double begin= timestamp();
245   if (shmim) {
246     rtimestamp(&begin, "XShmGetImage before");
247     xassert( XShmGetImage(disp,id,shmim, 0,0, AllPlanes) );
248     rtimestamp(&begin, "XShmGetImage");
249
250     size_t dsz= shmim->bytes_per_line * shmim->height;
251     im_use= im_free= mmalloc(sizeof(*im_use) + dsz);
252     *im_free= *shmim;
253     im_free->data= (void*)(im_free+1);
254     memcpy(im_free->data, shmim->data, dsz);
255     rtimestamp(&begin, "mmalloc/memcpy");
256   } else {
257     rtimestamp(&begin, "XGetImage before");
258     xassert( im_use= im_free=
259              XGetImage(disp,id, 0,0, wwidth,wheight, AllPlanes, ZPixmap) );
260     rtimestamp(&begin, "XGetImage");
261   }
262
263 #define COMPUTE_SHIFT_MASK(ix, rgb) \
264   compute_shift_mask(&shiftmasks[ix], im_use->rgb##_mask)
265   COMPUTE_SHIFT_MASK(0, red);
266   COMPUTE_SHIFT_MASK(1, green);
267   COMPUTE_SHIFT_MASK(2, blue);
268   
269   if (!*output)
270     *output= alloc_rgb_image(wwidth, wheight);
271
272   rtimestamp(&begin, "compute_shift_masks+alloc_rgb_image");
273
274   int x,y,i;
275   unsigned char *op= (*output)->data;
276   for (y=0; y<wheight; y++) {
277     for (x=0; x<wwidth; x++) {
278       long xrgb= XGetPixel(im_use,x,y);
279       for (i=0; i<3; i++) {
280         unsigned long sample=
281           ((xrgb << shiftmasks[i].lshift) >> shiftmasks[i].rshift)
282           & SAMPLEMASK;
283         *op++= sample;
284       }
285     }
286   }
287
288   rtimestamp(&begin,"w*h*XGetPixel");
289   if (im_free)
290     XDestroyImage(im_free);
291   
292   rtimestamp(&begin,"XDestroyImage");
293   check_not_disturbed();
294
295   debugf("PAGING   snapshot done.\n");
296 }
297
298 static int identical(const Snapshot *a, const Snapshot *b) {
299   if (!(a->w == b->w &&
300         a->h == b->h))
301     return 0;
302
303   int compare_to= a->h;
304   if (max_relevant_y>=0 && compare_to > max_relevant_y)
305     compare_to= max_relevant_y;
306   
307   return !memcmp(a->data, b->data, a->w * 3 * compare_to);
308 }
309
310 static void wait_for_stability(Snapshot **output,
311                                const Snapshot *previously,
312                                void (*with_keypress)(void),
313                                const char *fmt, ...)
314      FMT(4,5);
315
316 static void wait_for_stability(Snapshot **output,
317                                const Snapshot *previously,
318                                void (*with_keypress)(void),
319                                const char *fmt, ...) {
320   va_list al;
321   va_start(al,fmt);
322
323   Snapshot *last=0;
324   int nidentical=0;
325   /* waits longer if we're going to return an image identical to previously
326    * if previously==0, all images are considered identical to it */
327
328   char *doing;
329   sysassert( vasprintf(&doing,fmt,al) >=0 );
330
331   debugf("PAGING  wait_for_stability"
332           "  last_input=%f previously=%p `%s'\n",
333           last_input, previously, doing);
334
335   double min_interval= 25000; /*us*/
336   for (;;) {
337     progress_spinner("%s",doing);
338     
339     double since_last_input= timestamp() - last_input;
340     double this_interval= min_interval - since_last_input;
341
342     if (this_interval >= 0)
343       usleep(this_interval);
344
345     snapshot(output);
346
347     if (!last) {
348       debugf("PAGING  wait_for_stability first...\n");
349       last=*output; *output=0;
350     } else if (!identical(*output,last)) {
351       debugf("PAGING  wait_for_stability changed...\n");
352       free(last); last=*output; *output=0;
353       nidentical=0;
354       if (!with_keypress) {
355         min_interval *= 3.0;
356         min_interval += 0.5;
357       }
358     } else {
359       nidentical++;
360       int threshold=
361         !previously ? 3 :
362         identical(*output,previously) ? 5
363         : 1;
364       debugf("PAGING  wait_for_stability nidentical=%d threshold=%d\n",
365               nidentical, threshold);
366       if (nidentical >= threshold)
367         break;
368
369       min_interval += 0.5;
370       min_interval *= 2.0;
371     }
372
373     if (with_keypress)
374       with_keypress();
375   }
376
377   free_snapshot(&last);
378   free(doing);
379   debugf("PAGING  wait_for_stability done.\n");
380   va_end(al);
381 }
382
383 static void raise_and_get_details(void) {
384   int evbase,errbase,majver,minver;
385   int wxpos, wypos;
386   unsigned bd,depth;
387
388   progress("raising and checking YPP client window...");
389
390   debugf("PAGING raise_and_get_details\n");
391
392   int xtest= XTestQueryExtension(disp, &evbase,&errbase,&majver,&minver);
393   if (!xtest) fatal("X server does not support the XTEST extension.");
394
395   xassert( XRaiseWindow(disp, id) );
396   /* in case VisibilityNotify triggers right away before we have had a
397    * change to raise; to avoid falsely detecting lowering in that case */
398   
399   xassert( XSelectInput(disp, id,
400                         StructureNotifyMask|
401                         VisibilityChangeMask
402                         ) );
403
404   xassert( XRaiseWindow(disp, id) );
405   /* in case the window was lowered between our Raise and our SelectInput;
406    * to avoid failing to detect that lowering */
407
408   xassert( XGetWindowAttributes(disp, id, &attr) );
409   xassert( XGetGeometry(disp,id, &attr.root,
410                         &wxpos,&wypos, &wwidth,&wheight,
411                         &bd,&depth) );
412
413   if (!(wwidth >= 320 && wheight >= 320))
414     fatal("YPP client window is implausibly small?");
415
416   check_client_window_all_on_screen();
417
418   Bool shmpixmaps=0;
419   int major=0,minor=0;
420   int shm= XShmQueryVersion(disp, &major,&minor,&shmpixmaps);
421   debugf("PAGING shm=%d %d.%d pixmaps=%d\n",shm,major,minor,shmpixmaps);
422   if (shm) {
423     xassert( shmim= XShmCreateImage(disp, attr.visual, attr.depth, ZPixmap,
424                                     0,&shminfo, wwidth,wheight) );
425
426     sigset_t oldset, all;
427     sigfillset(&all);
428     sysassert(! sigprocmask(SIG_BLOCK,&all,&oldset) );
429
430     int pfd[2];
431     pid_t cleaner;
432     sysassert(! pipe(pfd) );
433     sysassert( (cleaner= fork()) != -1 );
434     if (!cleaner) {
435       sysassert(! close(pfd[1]) );
436       for (;;) {
437         int r= read(pfd[0], &shminfo.shmid, sizeof(shminfo.shmid));
438         if (!r) exit(0);
439         if (r==sizeof(shminfo.shmid)) break;
440         assert(r==-1 && errno==EINTR);
441       }
442       for (;;) {
443         char bc;
444         int r= read(pfd[0],&bc,1);
445         if (r>=0) break;
446         assert(r==-1 && errno==EINTR);
447       }
448       sysassert(! shmctl(shminfo.shmid,IPC_RMID,0) );
449       exit(0);
450     }
451     sysassert(! close(pfd[0]) );
452
453     sysassert(! sigprocmask(SIG_SETMASK,&oldset,0) );
454
455     assert(shmim->height == wheight);
456     sysassert( (shminfo.shmid=
457                 shmget(IPC_PRIVATE, shmim->bytes_per_line * wheight,
458                        IPC_CREAT|0600)) >= 0 );
459
460     sysassert( write(pfd[1],&shminfo.shmid,sizeof(shminfo.shmid)) ==
461                sizeof(shminfo.shmid) );
462     sysassert( shminfo.shmaddr= shmat(shminfo.shmid,0,0) );
463     shmim->data= shminfo.shmaddr;
464     shminfo.readOnly= False;
465     xassert( XShmAttach(disp,&shminfo) );
466
467     close(pfd[1]); /* causes IPC_RMID */
468   }
469 }
470
471 static void set_focus_commodity(void) {
472   int screen= XScreenNumberOfScreen(attr.screen);
473
474   progress("taking control of YPP client window...");
475
476   debugf("PAGING set_focus\n");
477
478   mouse_1_updown(160,160);
479   sync_after_input();
480
481   delay(0.5);
482   xassert( XSelectInput(disp, id,
483                         StructureNotifyMask|
484                         VisibilityChangeMask|
485                         FocusChangeMask
486                         ) );
487
488   int xpos,ypos;
489   translate_coords_toroot(10,10, &xpos,&ypos);
490   XTestFakeMotionEvent(disp,screen, xpos,ypos, 0);
491
492   sync_after_input();
493
494   debugf("PAGING raise_and_set_focus done.\n");
495 }
496
497 static CanonImage *convert_page(Snapshot *sn, RgbImage *ri) {
498   CanonImage *im;
499
500   fwrite_ppmraw(screenshot_file, sn);
501   if (ri) memcpy(ri->data, sn->data, ri->h * ri->w * 3);
502
503   unsigned char *pixel= sn->data;
504   CANONICALISE_IMAGE(im, sn->w, sn->h, {
505     rgb=
506       (pixel[0] << 16) |
507       (pixel[1] <<  8) |
508       (pixel[2]      );
509     pixel += 3;
510   });
511     
512   sysassert(!ferror(screenshot_file));
513   sysassert(!fflush(screenshot_file));
514
515   return im;
516 }
517
518 static void prepare_ypp_client(void) {
519   CanonImage *test;
520   Snapshot *current=0;
521   
522   /* find the window and check it's on the right kind of screen */
523   raise_and_get_details();
524   wait_for_stability(&current,0,0, "checking current YPP client screen...");
525
526   test= convert_page(current, 0);
527   find_structure(test, &max_relevant_y);
528   check_correct_commodities();
529   Rect sunshine= find_sunshine_widget();
530
531   progress("poking client...");
532   mouse_1_updown((sunshine.tl.x   + sunshine.br.x) / 2,
533                  (sunshine.tl.y*9 + sunshine.br.y) / 10);
534
535   free(test);
536
537   wait_for_stability(&current,0,0, "checking basic YPP client screen...");
538   mouse_1_updown(250, wheight-10);
539   mouse_1_updown_here();
540   mouse_1_updown_here();
541   XSync(disp,False);
542   check_not_disturbed();
543   send_key(XK_slash);
544   send_key(XK_w);
545   send_key(XK_Return);
546   sync_after_input();
547
548   Snapshot *status=0;
549   wait_for_stability(&status,current,0, "awaiting status information...");
550   free_snapshot(&current);
551   free_snapshot(&status);
552 }
553
554 void take_screenshots(void) {
555   Snapshot *current=0, *last=0;
556   RgbImage *page0_store;
557
558   prepare_ypp_client();
559   
560   /* page to the top - keep pressing page up until the image stops changing */
561   set_focus_commodity();
562   wait_for_stability(&current,0, send_pgup_many,
563                      "paging up to top of commodity list...");
564
565   page0_rgbimage= page0_store= alloc_rgb_image(current->w, current->h);
566
567   /* now to actually page down */
568   for (;;) {
569     debugf("paging page %d\n",npages);
570
571     if (!(npages < MAX_PAGES))
572       fatal("Paging down seems to generate too many pages - max is %d.",
573             MAX_PAGES);
574     
575     page_images[npages]= convert_page(current, page0_store);
576     free_snapshot(&last); last=current; current=0; page0_store=0;
577
578     debugf("PAGING page %d converted\n",npages);
579
580     wait_for_stability(&current,last, 0,
581                        "collecting screenshot of page %d...",
582                        npages+1);
583
584     if (npages &&  /* first pagedown doesn't do much */
585         identical(current,last)) {
586       free_snapshot(&current);
587       break;
588     }
589
590     send_pgdown();
591     npages++;
592   }
593   progress("finishing with the YPP client...");
594   send_pgdown_torestore();
595
596   debugf("PAGING all done.\n");
597   progress_log("collected %d screenshots.",npages);
598   assert(!page0_store);
599 }    
600
601 void take_one_screenshot(void) {
602   Snapshot *current=0;
603
604   prepare_ypp_client();
605   wait_for_stability(&current,0,0, "taking screenshot...");
606   page0_rgbimage= alloc_rgb_image(current->w, current->h);
607   page_images[0]= convert_page(current, page0_rgbimage);
608   npages= 1;
609   progress_log("collected single screenshot.");
610 }
611
612 void set_yppclient_window(unsigned long wul) {
613   id= wul;
614 }
615
616 DEBUG_DEFINE_SOME_DEBUGF(findypp,debugfind)
617
618 static int nfound;
619 static Atom wm_name;
620 static int screen;
621
622 static void findypp_recurse(int depth, int targetdepth, Window w) {
623   unsigned int nchildren;
624   int i;
625   Window *children=0;
626   Window gotroot, gotparent;
627
628   static const char prefix[]= "Puzzle Pirates - ";
629   static const char onthe[]= " on the ";
630   static const char suffix[]= " ocean";
631 #define S(x) (sizeof((x))-1)
632
633   debugfind("FINDYPP %d/%d screen %d  %*s %lx",
634             depth,targetdepth,screen,
635             depth,"",(unsigned long)w);
636   
637   if (depth!=targetdepth) {
638     xassert( XQueryTree(disp,w,
639                         &gotroot,&gotparent,
640                         &children,&nchildren) );
641     debugfind(" nchildren=%d\n",nchildren);
642   
643     for (i=0; i<nchildren; i++) {
644       Window child= children[i];
645       findypp_recurse(depth+1, targetdepth, child);
646     }
647     XFree(children);
648     return;
649   }
650     
651
652   int gotfmt;
653   Atom gottype;
654   unsigned long len, gotbytesafter;
655   char *title;
656   unsigned char *gottitle=0;
657   xassert( !XGetWindowProperty(disp,w, wm_name,0,512, False,
658                                AnyPropertyType,&gottype, &gotfmt, &len,
659                                &gotbytesafter, &gottitle) );
660   title= (char*)gottitle;
661
662   if (DEBUGP(findypp)) {
663     debugfind(" gf=%d len=%lu gba=%lu \"", gotfmt,len,gotbytesafter);
664     char *p;
665     for (p=title; p < title+len; p++) {
666       char c= *p;
667       if (c>=' ' && c<=126) fputc(c,debug);
668       else fprintf(debug,"\\x%02x",c & 0xff);
669     }
670     fputs("\": ",debug);
671   }
672
673 #define REQUIRE(pred)                                                   \
674   if (!(pred)) { debugfind(" failed test  %s\n", #pred); return; }      \
675   else
676
677   REQUIRE( gottype!=None );
678   REQUIRE( len );
679   REQUIRE( gotfmt==8 );
680
681   REQUIRE( len >= S(prefix) + 1 + S(onthe) + 1 + S(suffix) );
682
683   char *spc1= strchr(  title        + S(prefix), ' ');  REQUIRE(spc1);
684   char *spc2= strrchr((title + len) - S(suffix), ' ');  REQUIRE(spc2);
685
686   REQUIRE( (title + len) - spc1  >= S(onthe)  + S(suffix) );
687   REQUIRE(  spc2         - title >= S(prefix) + S(onthe) );
688
689   REQUIRE( !memcmp(title,                   prefix, S(prefix)) );
690   REQUIRE( !memcmp(title + len - S(suffix), suffix, S(suffix))  );
691   REQUIRE( !memcmp(spc1,                    onthe,  S(onthe))  );
692
693 #define ASSIGN(what, start, end)                        \
694   what= masprintf("%.*s", (end)-(start), start);        \
695   if (o_##what) REQUIRE( !strcasecmp(o_##what, what) ); \
696   else
697
698   ASSIGN(ocean,  spc1 + S(onthe),   (title + len) - S(suffix));
699   ASSIGN(pirate, title + S(prefix),  spc1);
700
701   debugfind(" YES!\n");
702   id= w;
703   nfound++;
704   progress_log("found YPP client (0x%lx):"
705                " %s ocean - %s.",
706                (unsigned long)id, ocean, pirate);
707 }
708
709 void find_yppclient_window(void) {
710   int targetdepth;
711
712   nfound=0;
713   
714   if (id) return;
715   
716   progress("looking for YPP client window...");
717
718   xassert( (wm_name= XInternAtom(disp,"WM_NAME",True)) != None);
719
720   for (targetdepth=1; targetdepth<4; targetdepth++) {
721     for (screen=0; screen<ScreenCount(disp); screen++) {
722       debugfind("FINDYPP screen %d\n", screen);
723       findypp_recurse(0,targetdepth, RootWindow(disp,screen));
724     }
725     if (nfound) break;
726   }
727
728   if (nfound>1)
729     fatal("Found several possible YPP clients.   Close one,\n"
730           " disambiguate with --pirate or --ocean,"
731           " or specify --window-id.\n");
732   if (nfound<1)
733     fatal("Did not find %sYPP client."
734           " Use --window-id and/or report this as a fault.\n",
735           o_ocean || o_pirate ? "matching ": "");
736 }