7 #include <X11/extensions/XTest.h>
8 #include <X11/keysym.h>
10 CanonImage *page_images[MAX_PAGES];
15 static struct timeval tv_startup;
17 static KeyCode keycode(KeySym sym) {
18 return XKeysymToKeycode(disp,sym);
21 static void check_pclose(FILE *f, char *cmd) {
24 r= fgetc(f); eassert(r==EOF); eassert(feof(f));
25 r= pclose(f); eassert(r>=0); eassert(WIFEXITED(r) && !WEXITSTATUS(r));
30 static CanonImage *screenshot_now(void) {
35 r= asprintf(&cmd, "xwd -silent -id 0x%lx | xwdtopnm", (unsigned long)id);
37 FILE *f= popen(cmd,"r"); eassert(f);
38 ci= file_read_image(f);
44 static void screenshot_startup(void) {
46 disp= XOpenDisplay(0); eassert(disp);
47 r= gettimeofday(&tv_startup,0); eassert(!r);
51 static CanonImage *single_page(void) {
53 r= XRaiseWindow(disp, id); eassert(r);
54 r= XSync(disp, False); eassert(r);
55 return screenshot_now();
59 /*---------- pager ----------*/
66 static size_t snapshot_alloc= 1024;
67 static double last_input;
68 static const double min_update_allowance= 0.5;
70 static double timestamp(void) {
74 r= gettimeofday(&tv,0); eassert(!r);
75 double t= (tv.tv_sec - tv_startup.tv_sec) +
76 (tv.tv_usec - tv_startup.tv_usec) * 1e-6;
77 fprintf(stderr,"%f\n",t);
80 static void delay(double need_sleep) {
82 fprintf(stderr,"PAGING delay %f\n",need_sleep);
83 r= usleep(need_sleep * 1e6); eassert(!r);
86 static void sync_after_input(void) {
88 r= XSync(disp, False); eassert(r);
89 last_input= timestamp();
92 static void send_key(KeySym sym) {
93 XTestFakeKeyEvent(disp, keycode(sym),1, 10);
94 XTestFakeKeyEvent(disp, keycode(sym),0, 10);
97 static void send_pgup_many(void) {
101 fprintf(stderr,"PAGING PageUp x %d\n",i);
104 static void send_pgdown(void) {
106 fprintf(stderr,"PAGING PageDown\n");
110 static void snapshot(Snapshot **output) {
114 free(*output); *output=0;
116 fprintf(stderr,"PAGING snapshot\n");
118 r= asprintf(&cmd, "xwd -silent -id 0x%lx", (unsigned long)id); eassert(r>=0);
119 FILE *f= popen(cmd,"r"); eassert(f);
124 size_t allow= snapshot_alloc - used;
126 snapshot_alloc <<= 1;
128 fprintf(stderr,"PAGING snapshot grow %ld\n", (long)snapshot_alloc);
132 *output= realloc(*output, sizeof(**output) + snapshot_alloc);
136 size_t got= fread((*output)->d, 1, allow, f);
142 (*output)->len= used;
143 snapshot_alloc= used+1;
144 *output= realloc(*output, sizeof(**output) + snapshot_alloc);
147 fprintf(stderr,"PAGING snapshot len=%ld\n", (long)used);
150 static int identical(const Snapshot *a, const Snapshot *b) {
151 return !(memcmp(a, b, sizeof(*a)) ||
152 memcmp(a->d, b->d, a->len));
155 static void wait_for_stability(Snapshot **output,
156 const Snapshot *previously,
157 void (*with_keypress)(void)) {
159 /* waits longer if we're going to return an image identical to previously
160 * if previously==0, all images are considered identical to it */
162 fprintf(stderr,"PAGING wait_for_stability"
163 " last_input=%f previously=%p\n",
164 last_input, previously);
167 double at_snapshot= timestamp();
168 double need_sleep= min_update_allowance - (at_snapshot - last_input);
169 if (need_sleep > 0) { delay(need_sleep); continue; }
173 if (!with_keypress &&
174 !(previously && identical(*output,previously))) {
175 fprintf(stderr,"PAGING wait_for_stability simple\n");
179 if (last && identical(*output,last)) {
180 fprintf(stderr,"PAGING wait_for_stability stabilised\n");
184 fprintf(stderr,"PAGING wait_for_stability retry\n");
186 free(last); last=*output; *output=0;
195 fprintf(stderr,"PAGING wait_for_stability done.\n");
198 static void raise_and_set_focus(void) {
200 XWindowAttributes attr;
201 int xpos,ypos, evbase,errbase,majver,minver;
202 unsigned width,height,bd,depth;
205 fprintf(stderr,"PAGING raise_and_set_focus\n");
207 r= XTestQueryExtension(disp, &evbase,&errbase,&majver,&minver);
210 r= XRaiseWindow(disp, id); eassert(r);
212 r= XGetWindowAttributes(disp, id, &attr); eassert(r);
213 r= XGetGeometry(disp,id, &attr.root,&xpos,&ypos,&width,&height, &bd,&depth);
216 r= XTranslateCoordinates(disp, id,attr.root, 160,160, &xpos,&ypos,
220 int screen= XScreenNumberOfScreen(attr.screen);
221 XTestFakeMotionEvent(disp,screen, xpos, ypos, 0);
223 XTestFakeButtonEvent(disp,1,1, 50);
224 XTestFakeButtonEvent(disp,1,0, 50);
227 fprintf(stderr,"PAGING raise_and_set_focus done.\n");
232 static void read_pages(void) {
233 Snapshot *current=0, *last=0;
235 raise_and_set_focus();
237 /* page to the top - keep pressing page up until the image stops changing */
238 wait_for_stability(&last,0, send_pgup_many);
240 /* now to actually page down */
242 fprintf(stderr,"PAGING page %d\n",npages);
243 //eassert(npages < MAX_PAGES);
244 //page_images[npages++]=
246 wait_for_stability(¤t,last, 0);
247 if (npages && /* first pagedown doesn't do much */
248 identical(current,last)) {
253 free(last); last=current; current=0;
258 fprintf(stderr,"PAGING all done.\n");
261 int main(int argc, char **argv) {
262 screenshot_startup();
264 id= strtoul(*++argv,0,0);