7 #include <X11/extensions/XTest.h>
8 #include <X11/keysym.h>
11 CanonImage *page_images[MAX_PAGES];
14 static XWindowAttributes attr;
17 static struct timeval tv_startup;
18 static unsigned wwidth, wheight;
19 static int wxpos, wypos;
21 DEBUG_DEFINE_DEBUGF(pages)
23 static KeyCode keycode(KeySym sym) {
24 return XKeysymToKeycode(disp,sym);
27 void screenshot_startup(void) {
29 disp= XOpenDisplay(0); eassert(disp);
30 r= gettimeofday(&tv_startup,0); eassert(!r);
33 /*---------- pager ----------*/
35 typedef XImage Snapshot;
37 static double last_input;
38 static const double min_update_allowance= 0.25;
40 static double timestamp(void) {
44 r= gettimeofday(&tv,0); eassert(!r);
45 double t= (tv.tv_sec - tv_startup.tv_sec) +
46 (tv.tv_usec - tv_startup.tv_usec) * 1e-6;
47 debugf("PAGING %f\n",t);
50 static void delay(double need_sleep) {
52 debugf("PAGING delay %f\n",need_sleep);
53 r= usleep(need_sleep * 1e6); eassert(!r);
56 static void sync_after_input(void) {
58 r= XSync(disp, False); eassert(r);
59 last_input= timestamp();
62 static void send_key(KeySym sym) {
63 XTestFakeKeyEvent(disp, keycode(sym),1, 10);
64 XTestFakeKeyEvent(disp, keycode(sym),0, 10);
67 static void send_pgup_many(void) {
71 debugf("PAGING PageUp x %d\n",i);
74 static void send_pgdown(void) {
76 debugf("PAGING PageDown\n");
80 static void free_snapshot(Snapshot **io) {
81 if (*io) XDestroyImage(*io);
85 static void snapshot(Snapshot **output) {
90 free_snapshot(output);
92 debugf("PAGING snapshot\n");
95 *output= XGetImage(disp,id, 0,0, wwidth,wheight, AllPlanes, ZPixmap);
98 debugf("PAGING snapshot done.\n");
101 static int identical(const Snapshot *a, const Snapshot *b) {
102 if (!(a->width == b->width &&
103 a->height == b->height &&
104 a->bytes_per_line == b->bytes_per_line &&
105 a->format == b->format))
107 return !memcmp(a->data, b->data, a->bytes_per_line * a->height);
110 static void wait_for_stability(Snapshot **output,
111 const Snapshot *previously,
112 void (*with_keypress)(void)) {
114 /* waits longer if we're going to return an image identical to previously
115 * if previously==0, all images are considered identical to it */
117 debugf("PAGING wait_for_stability"
118 " last_input=%f previously=%p\n",
119 last_input, previously);
122 double at_snapshot= timestamp();
123 double need_sleep= min_update_allowance - (at_snapshot - last_input);
124 if (need_sleep > 0) { delay(need_sleep); continue; }
128 if (!with_keypress &&
129 !(previously && identical(*output,previously))) {
130 debugf("PAGING wait_for_stability simple\n");
134 if (last && identical(*output,last)) {
135 debugf("PAGING wait_for_stability stabilised\n");
139 debugf("PAGING wait_for_stability retry\n");
141 free_snapshot(&last); last=*output; *output=0;
149 free_snapshot(&last);
150 debugf("PAGING wait_for_stability done.\n");
153 static void raise_and_get_details(void) {
155 int evbase,errbase,majver,minver;
159 debugf("PAGING raise_and_get_details\n");
161 r= XTestQueryExtension(disp, &evbase,&errbase,&majver,&minver);
164 r= XRaiseWindow(disp, id); eassert(r);
166 r= XGetWindowAttributes(disp, id, &attr); eassert(r);
167 r= XGetGeometry(disp,id, &attr.root,
168 &wxpos,&wypos, &wwidth,&wheight,
172 r= XTranslateCoordinates(disp, id,attr.root, 160,160, &wxpos,&wypos,
177 static void set_focus(void) {
178 int screen= XScreenNumberOfScreen(attr.screen);
180 debugf("PAGING set_focus\n");
182 XTestFakeMotionEvent(disp,screen, wxpos,wypos, 0);
184 XTestFakeButtonEvent(disp,1,1, 50);
185 XTestFakeButtonEvent(disp,1,0, 50);
188 debugf("PAGING raise_and_set_focus done.\n");
191 #define SAMPLEMASK 0xfful
197 static void compute_shift_mask(ShMask *sm, unsigned long ximage_mask) {
202 if (ximage_mask <= (SAMPLEMASK>>1)) {
203 sm->lshift++; ximage_mask <<= 1;
204 } else if (ximage_mask > SAMPLEMASK) {
205 sm->rshift++; ximage_mask >>= 1;
209 assert(!(sm->lshift && sm->rshift));
211 assert(sm->lshift < LONG_BIT);
212 assert(sm->rshift < LONG_BIT);
215 static CanonImage *convert_page(Snapshot *sn) {
216 ShMask shiftmasks[3];
219 fprintf(screenshots_file,
222 "255\n", sn->width, sn->height);
224 #define COMPUTE_SHIFT_MASK(ix, rgb) \
225 compute_shift_mask(&shiftmasks[ix], sn->rgb##_mask)
226 COMPUTE_SHIFT_MASK(0, red);
227 COMPUTE_SHIFT_MASK(1, green);
228 COMPUTE_SHIFT_MASK(2, blue);
230 CANONICALISE_IMAGE(im, sn->width, sn->height, {
231 long xrgb= XGetPixel(sn, x, y);
234 for (i=0; i<3; i++) {
236 unsigned long sample=
237 ((xrgb << shiftmasks[i].lshift)
238 >> shiftmasks[i].rshift) & SAMPLEMASK;
240 fputc(sample, screenshots_file);
244 eassert(!fflush(screenshots_file));
249 void take_screenshots(void) {
250 Snapshot *current=0, *last=0;
253 /* find the window and check it's on the right kind of screen */
254 raise_and_get_details();
255 wait_for_stability(¤t,0,0);
256 test= convert_page(current);
257 find_structure(test);
260 /* page to the top - keep pressing page up until the image stops changing */
262 wait_for_stability(¤t,0, send_pgup_many);
264 /* now to actually page down */
266 debugf("paging page %d\n",npages);
268 eassert(npages < MAX_PAGES);
269 page_images[npages]= convert_page(current);
270 free_snapshot(&last); last=current; current=0;
272 debugf("PAGING page %d converted\n",npages);
274 wait_for_stability(¤t,last, 0);
275 if (npages && /* first pagedown doesn't do much */
276 identical(current,last)) {
277 free_snapshot(¤t);
284 debugf("PAGING all done.\n");
287 void take_one_screenshot(void) {
290 raise_and_get_details();
292 wait_for_stability(¤t,0,0);
293 page_images[0]= convert_page(current);
297 void set_yppclient_window(unsigned long wul) {
301 void find_yppclient_window(void) {
303 eassert(!"finding client window NYI");