7 #include <X11/extensions/XTest.h>
8 #include <X11/keysym.h>
11 CanonImage *page_images[MAX_PAGES];
16 static struct timeval tv_startup;
17 static unsigned wwidth, wheight;
19 static KeyCode keycode(KeySym sym) {
20 return XKeysymToKeycode(disp,sym);
24 static void check_exitstatus(int st) {
25 eassert(WIFEXITED(st));
26 eassert(!WEXITSTATUS(st));
29 static void check_pclose(FILE *f, char *cmd) {
32 r= fgetc(f); eassert(r==EOF); eassert(feof(f));
33 r= pclose(f); eassert(r>=0); check_exitstatus(r);
37 static CanonImage *screenshot_now(void) {
42 r= asprintf(&cmd, "xwd -silent -id 0x%lx | xwdtopnm", (unsigned long)id);
44 FILE *f= popen(cmd,"r"); eassert(f);
45 ci= file_read_image(f);
51 static void screenshot_startup(void) {
53 disp= XOpenDisplay(0); eassert(disp);
54 r= gettimeofday(&tv_startup,0); eassert(!r);
58 static CanonImage *single_page(void) {
60 r= XRaiseWindow(disp, id); eassert(r);
61 r= XSync(disp, False); eassert(r);
62 return screenshot_now();
66 /*---------- pager ----------*/
68 typedef XImage Snapshot;
70 //static size_t snapshot_alloc= 1024;
71 static double last_input;
72 static const double min_update_allowance= 0.25;
74 static double timestamp(void) {
78 r= gettimeofday(&tv,0); eassert(!r);
79 double t= (tv.tv_sec - tv_startup.tv_sec) +
80 (tv.tv_usec - tv_startup.tv_usec) * 1e-6;
81 fprintf(stderr,"%f\n",t);
84 static void delay(double need_sleep) {
86 fprintf(stderr,"PAGING delay %f\n",need_sleep);
87 r= usleep(need_sleep * 1e6); eassert(!r);
90 static void sync_after_input(void) {
92 r= XSync(disp, False); eassert(r);
93 last_input= timestamp();
96 static void send_key(KeySym sym) {
97 XTestFakeKeyEvent(disp, keycode(sym),1, 10);
98 XTestFakeKeyEvent(disp, keycode(sym),0, 10);
101 static void send_pgup_many(void) {
105 fprintf(stderr,"PAGING PageUp x %d\n",i);
108 static void send_pgdown(void) {
110 fprintf(stderr,"PAGING PageDown\n");
114 static void free_snapshot(Snapshot **io) {
115 if (*io) XDestroyImage(*io);
119 static void snapshot(Snapshot **output) {
124 free_snapshot(output);
126 fprintf(stderr,"PAGING snapshot\n");
129 *output= XGetImage(disp,id, 0,0, wwidth,wheight, AllPlanes, ZPixmap);
132 fprintf(stderr,"PAGING snapshot done.\n");
135 static int identical(const Snapshot *a, const Snapshot *b) {
136 if (!(a->width == b->width &&
137 a->height == b->height &&
138 a->bytes_per_line == b->bytes_per_line &&
139 a->format == b->format))
141 return !memcmp(a->data, b->data, a->bytes_per_line * a->height);
144 static void wait_for_stability(Snapshot **output,
145 const Snapshot *previously,
146 void (*with_keypress)(void)) {
148 /* waits longer if we're going to return an image identical to previously
149 * if previously==0, all images are considered identical to it */
151 fprintf(stderr,"PAGING wait_for_stability"
152 " last_input=%f previously=%p\n",
153 last_input, previously);
156 double at_snapshot= timestamp();
157 double need_sleep= min_update_allowance - (at_snapshot - last_input);
158 if (need_sleep > 0) { delay(need_sleep); continue; }
162 if (!with_keypress &&
163 !(previously && identical(*output,previously))) {
164 fprintf(stderr,"PAGING wait_for_stability simple\n");
168 if (last && identical(*output,last)) {
169 fprintf(stderr,"PAGING wait_for_stability stabilised\n");
173 fprintf(stderr,"PAGING wait_for_stability retry\n");
175 free_snapshot(&last); last=*output; *output=0;
183 free_snapshot(&last);
184 fprintf(stderr,"PAGING wait_for_stability done.\n");
187 static void raise_and_set_focus(void) {
189 XWindowAttributes attr;
190 int xpos,ypos, evbase,errbase,majver,minver;
194 fprintf(stderr,"PAGING raise_and_set_focus\n");
196 r= XTestQueryExtension(disp, &evbase,&errbase,&majver,&minver);
199 r= XRaiseWindow(disp, id); eassert(r);
201 r= XGetWindowAttributes(disp, id, &attr); eassert(r);
202 r= XGetGeometry(disp,id, &attr.root,
203 &xpos,&ypos, &wwidth,&wheight,
207 r= XTranslateCoordinates(disp, id,attr.root, 160,160, &xpos,&ypos,
211 int screen= XScreenNumberOfScreen(attr.screen);
212 XTestFakeMotionEvent(disp,screen, xpos, ypos, 0);
214 XTestFakeButtonEvent(disp,1,1, 50);
215 XTestFakeButtonEvent(disp,1,0, 50);
218 fprintf(stderr,"PAGING raise_and_set_focus done.\n");
226 static void compute_shift_mask(ShMask *sm, int targshift,
227 unsigned long ximage_mask) {
232 sm->mask= 0xfful << targshift;
233 below= ~0ul << targshift;
236 if (ximage_mask < sm->mask) {
237 sm->lshift++; ximage_mask <<= 1;
238 } else if ((ximage_mask & ~below) > sm->mask) {
239 sm->rshift++; ximage_mask >>= 1;
243 assert(!(sm->lshift && sm->rshift));
245 assert(sm->lshift < LONG_BIT);
246 assert(sm->rshift < LONG_BIT);
249 static void store_page(int pageno, Snapshot *sn) {
250 eassert(pageno < MAX_PAGES);
251 ShMask shiftmasks[3];
254 #define COMPUTE_SHIFT_MASK(ix, targshift, rgb) \
255 compute_shift_mask(&shiftmasks[ix], targshift, sn->rgb##_mask)
256 COMPUTE_SHIFT_MASK(0, 16, red);
257 COMPUTE_SHIFT_MASK(1, 8, green);
258 COMPUTE_SHIFT_MASK(2, 0, blue);
260 CANONICALISE_IMAGE(im, sn->width, sn->height, {
261 long xrgb= XGetPixel(sn, x, y);
265 rgb |= ((xrgb << shiftmasks[i].lshift)
266 >> shiftmasks[i].rshift) & shiftmasks[i].mask;
269 page_images[pageno]= im;
272 static void read_pages(void) {
273 Snapshot *current=0, *last=0;
275 raise_and_set_focus();
277 /* page to the top - keep pressing page up until the image stops changing */
278 wait_for_stability(¤t,0, send_pgup_many);
280 /* now to actually page down */
282 fprintf(stderr,"PAGING page %d\n",npages);
283 store_page(npages, current);
284 free_snapshot(&last); last=current; current=0;
285 fprintf(stderr,"PAGING page %d converted\n",npages);
287 wait_for_stability(¤t,last, 0);
288 if (npages && /* first pagedown doesn't do much */
289 identical(current,last)) {
290 free_snapshot(¤t);
297 fprintf(stderr,"PAGING all done.\n");
300 int main(int argc, char **argv) {
301 screenshot_startup();
303 id= strtoul(*++argv,0,0);