7 #include <X11/extensions/XTest.h>
8 #include <X11/keysym.h>
13 CanonImage *page_images[MAX_PAGES];
18 static struct timeval tv_startup;
19 static unsigned wwidth, wheight;
21 static XpmAttributes xpma;
23 static KeyCode keycode(KeySym sym) {
24 return XKeysymToKeycode(disp,sym);
28 static void check_exitstatus(int st) {
29 eassert(WIFEXITED(st));
30 eassert(!WEXITSTATUS(st));
33 static void check_pclose(FILE *f, char *cmd) {
36 r= fgetc(f); eassert(r==EOF); eassert(feof(f));
37 r= pclose(f); eassert(r>=0); check_exitstatus(r);
41 static CanonImage *screenshot_now(void) {
46 r= asprintf(&cmd, "xwd -silent -id 0x%lx | xwdtopnm", (unsigned long)id);
48 FILE *f= popen(cmd,"r"); eassert(f);
49 ci= file_read_image(f);
55 static void screenshot_startup(void) {
57 disp= XOpenDisplay(0); eassert(disp);
58 r= gettimeofday(&tv_startup,0); eassert(!r);
62 static CanonImage *single_page(void) {
64 r= XRaiseWindow(disp, id); eassert(r);
65 r= XSync(disp, False); eassert(r);
66 return screenshot_now();
70 /*---------- pager ----------*/
72 typedef XImage Snapshot;
74 //static size_t snapshot_alloc= 1024;
75 static double last_input;
76 static const double min_update_allowance= 0.5;
78 static double timestamp(void) {
82 r= gettimeofday(&tv,0); eassert(!r);
83 double t= (tv.tv_sec - tv_startup.tv_sec) +
84 (tv.tv_usec - tv_startup.tv_usec) * 1e-6;
85 fprintf(stderr,"%f\n",t);
88 static void delay(double need_sleep) {
90 fprintf(stderr,"PAGING delay %f\n",need_sleep);
91 r= usleep(need_sleep * 1e6); eassert(!r);
94 static void sync_after_input(void) {
96 r= XSync(disp, False); eassert(r);
97 last_input= timestamp();
100 static void send_key(KeySym sym) {
101 XTestFakeKeyEvent(disp, keycode(sym),1, 10);
102 XTestFakeKeyEvent(disp, keycode(sym),0, 10);
105 static void send_pgup_many(void) {
109 fprintf(stderr,"PAGING PageUp x %d\n",i);
112 static void send_pgdown(void) {
114 fprintf(stderr,"PAGING PageDown\n");
118 static void free_snapshot(Snapshot **io) {
119 if (*io) XDestroyImage(*io);
123 static void snapshot(Snapshot **output) {
128 free_snapshot(output);
130 fprintf(stderr,"PAGING snapshot\n");
133 *output= XGetImage(disp,id, 0,0, wwidth,wheight, AllPlanes, ZPixmap);
136 fprintf(stderr,"PAGING snapshot done.\n");
139 static int identical(const Snapshot *a, const Snapshot *b) {
140 if (!(a->width == b->width &&
141 a->height == b->height &&
142 a->bytes_per_line == b->bytes_per_line &&
143 a->format == b->format))
145 return !memcmp(a->data, b->data, a->bytes_per_line * a->height);
148 static void wait_for_stability(Snapshot **output,
149 const Snapshot *previously,
150 void (*with_keypress)(void)) {
152 /* waits longer if we're going to return an image identical to previously
153 * if previously==0, all images are considered identical to it */
155 fprintf(stderr,"PAGING wait_for_stability"
156 " last_input=%f previously=%p\n",
157 last_input, previously);
160 double at_snapshot= timestamp();
161 double need_sleep= min_update_allowance - (at_snapshot - last_input);
162 if (need_sleep > 0) { delay(need_sleep); continue; }
166 if (!with_keypress &&
167 !(previously && identical(*output,previously))) {
168 fprintf(stderr,"PAGING wait_for_stability simple\n");
172 if (last && identical(*output,last)) {
173 fprintf(stderr,"PAGING wait_for_stability stabilised\n");
177 fprintf(stderr,"PAGING wait_for_stability retry\n");
179 free_snapshot(&last); last=*output; *output=0;
187 free_snapshot(&last);
188 fprintf(stderr,"PAGING wait_for_stability done.\n");
191 static void raise_and_set_focus(void) {
193 XWindowAttributes attr;
194 int xpos,ypos, evbase,errbase,majver,minver;
198 fprintf(stderr,"PAGING raise_and_set_focus\n");
200 r= XTestQueryExtension(disp, &evbase,&errbase,&majver,&minver);
203 r= XRaiseWindow(disp, id); eassert(r);
205 r= XGetWindowAttributes(disp, id, &attr); eassert(r);
206 r= XGetGeometry(disp,id, &attr.root,
207 &xpos,&ypos, &wwidth,&wheight,
211 r= XTranslateCoordinates(disp, id,attr.root, 160,160, &xpos,&ypos,
215 int screen= XScreenNumberOfScreen(attr.screen);
216 XTestFakeMotionEvent(disp,screen, xpos, ypos, 0);
218 XTestFakeButtonEvent(disp,1,1, 50);
219 XTestFakeButtonEvent(disp,1,0, 50);
222 fprintf(stderr,"PAGING raise_and_set_focus done.\n");
225 static void store_page(int pageno, Snapshot *sn) {
226 pid_t converter, paster;
227 eassert(pageno < MAX_PAGES);
228 int paste[2], results[2];
232 eassert(!fflush(stdout));
233 eassert(!fflush(stderr));
235 r= pipe(paste); eassert(!r);
236 r= pipe(results); eassert(!r);
237 err= tmpfile(); eassert(err);
239 converter= fork(); eassert(converter!=-1);
241 r= dup2(paste[0],0); eassert(r==0);
242 r= dup2(results[1],1); eassert(r==1);
243 r= dup2(2,4); eassert(r==4); /* fileno(errn) > 4, see above */
244 r= dup2(fileno(err),2); eassert(r==2);
249 execlp("xpmtoppm", "xpmtoppm",(char*)0);
251 eassert(!"xpmtoppm exec failure");
255 r= XpmCreateBufferFromImage(disp, &xpmdata, sn, 0, &xpma);
259 paster= fork(); eassert(paster!=-1);
261 FILE *f= fdopen(paste[1],"w"); eassert(f);
265 r= fputs(xpmdata,f); eassert(r!=EOF);
266 //size_t did= fwrite(sn->d, 1, sn->len, f);
267 //eassert(did==sn->len);
278 FILE *f= fdopen(results[0],"r");
282 page_images[pageno]= file_read_image(f);
283 r= fgetc(f); eassert(r==EOF); eassert(!ferror(f)); eassert(feof(f));
287 pid_t got_conv,got_paste;
288 int st_conv, st_paste;
290 got_conv= waitpid(converter,&st_conv,0); eassert(got_conv==converter);
291 got_paste= waitpid(paster,&st_paste,0); eassert(got_paste==paster);
294 (!st_paste || (WIFSIGNALED(st_paste) && WTERMSIG(st_paste)==SIGPIPE))
299 rewind(err); int c; while ((c=getc(err))!=EOF) fputc(c,stderr);
300 fprintf(stderr, "convert: subprocess statuses: %d %d\n", st_conv, st_paste);
304 static void read_pages(void) {
305 Snapshot *current=0, *last=0;
307 raise_and_set_focus();
309 /* page to the top - keep pressing page up until the image stops changing */
310 wait_for_stability(¤t,0, send_pgup_many);
312 /* now to actually page down */
314 fprintf(stderr,"PAGING page %d\n",npages);
315 store_page(npages, current);
316 free_snapshot(&last); last=current; current=0;
317 fprintf(stderr,"PAGING page %d converted\n",npages);
319 wait_for_stability(¤t,last, 0);
320 if (npages && /* first pagedown doesn't do much */
321 identical(current,last)) {
322 free_snapshot(¤t);
329 fprintf(stderr,"PAGING all done.\n");
332 int main(int argc, char **argv) {
333 screenshot_startup();
335 id= strtoul(*++argv,0,0);