2 * Copyright (C) 2000 James Macnicol
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation. No representations are made about the suitability of this
9 * software for any purpose. It is provided "as is" without express or
14 #include "screenhack.h"
17 #define MIN(a, b) (((a) < (b))?(a):(b))
21 #define MAX(a, b) (((a) > (b))?(a):(b))
26 /* This should be *way* slower than the spotlight hack was */
27 #define X_PERIOD 45000.0
28 #define Y_PERIOD 36000.0
39 int pixwidth, pixheight, pixspacex, pixspacey, lensoffsetx, lensoffsety;
52 async_load_state *img_loader;
56 static long currentTimeInMs(struct state *st)
58 struct timeval curTime;
59 #ifdef GETTIMEOFDAY_TWO_ARGS
60 struct timezone tz = {0,0};
61 gettimeofday(&curTime, &tz);
63 gettimeofday(&curTime);
65 return curTime.tv_sec*1000 + curTime.tv_usec/1000.0;
69 zoom_init (Display *dpy, Window window)
71 struct state *st = (struct state *) calloc (1, sizeof(*st));
73 XWindowAttributes xgwa;
77 int nblocksx, nblocksy;
81 XGetWindowAttributes(st->dpy, st->window, &xgwa);
82 st->screen = xgwa.screen;
83 st->sizex = xgwa.width;
84 st->sizey = xgwa.height;
86 bg = get_pixel_resource(st->dpy, cmap, "background", "Background");
88 st->delay = get_integer_resource(st->dpy, "delay", "Integer");
91 st->duration = get_integer_resource (st->dpy, "duration", "Seconds");
94 st->pixwidth = get_integer_resource(st->dpy, "pixwidth", "Integer");
97 st->pixheight = get_integer_resource(st->dpy, "pixheight", "Integer");
98 if (st->pixheight < 1)
100 st->pixspacex = get_integer_resource(st->dpy, "pixspacex", "Integer");
101 if (st->pixspacex < 0)
103 st->pixspacey = get_integer_resource(st->dpy, "pixspacey", "Integer");
104 if (st->pixspacey < 0)
106 st->lenses = get_boolean_resource(st->dpy, "lenses", "Boolean");
107 st->lensoffsetx = get_integer_resource(st->dpy, "lensoffsetx", "Integer");
108 st->lensoffsetx = MAX(0, MIN(st->pixwidth, st->lensoffsetx));
109 st->lensoffsety = get_integer_resource(st->dpy, "lensoffsety", "Integer");
110 st->lensoffsety = MAX(0, MIN(st->pixwidth, st->lensoffsety));
112 gcv.function = GXcopy;
113 gcv.subwindow_mode = IncludeInferiors;
114 gcflags = GCForeground|GCFunction;
116 if (!st->lenses && use_subwindow_mode_p(xgwa.screen, st->window)) /* see grabscreen.c */
117 gcflags |= GCSubwindowMode;
118 st->window_gc = XCreateGC(st->dpy, st->window, gcflags, &gcv);
122 st->pm = XCreatePixmap(st->dpy, st->window, st->sizex, st->sizey, xgwa.depth);
124 XFillRectangle(st->dpy, st->window, st->window_gc, 0, 0, st->sizex, st->sizey);
125 XSetWindowBackground(st->dpy, st->window, bg);
127 st->start_time = time ((time_t) 0);
128 st->img_loader = load_image_async_simple (0, xgwa.screen, st->window,
131 /* We might have needed this to grab the image, but if we leave this set
132 to GCSubwindowMode, then we'll *draw* right over subwindows too. */
133 XSetSubwindowMode (st->dpy, st->window_gc, ClipByChildren);
136 nblocksx = (int)ceil((double)st->sizex / (double)(st->pixwidth + st->pixspacex));
137 nblocksy = (int)ceil((double)st->sizey / (double)(st->pixheight + st->pixspacey));
139 st->s = MAX((nblocksx - 1) * st->lensoffsetx + st->pixwidth,
140 (nblocksy - 1) * st->lensoffsety + st->pixheight) * 2;
142 st->s = MAX(nblocksx, nblocksy) * 2;
144 st->sinusoid_offset = random();
150 zoom_draw (Display *dpy, Window window, void *closure)
152 struct state *st = (struct state *) closure;
157 if (st->img_loader) /* still loading */
159 st->img_loader = load_image_async_simple (st->img_loader, 0, 0, 0, 0, 0);
160 if (! st->img_loader) { /* just finished */
161 XClearWindow (st->dpy, st->window);
162 st->start_time = time ((time_t) 0);
164 st->orig_map = XGetImage(st->dpy, st->pm, 0, 0, st->sizex, st->sizey, ~0L, ZPixmap);
165 /* XFreePixmap(st->dpy, st->pm);
172 if (!st->img_loader &&
173 st->start_time + st->duration < time ((time_t) 0)) {
174 st->img_loader = load_image_async_simple (0, st->screen, st->window,
179 #define nrnd(x) (random() % (x))
181 now = currentTimeInMs(st);
182 now += st->sinusoid_offset; /* don't run multiple screens in lock-step */
185 st->tlx = ((1. + sin(((double)now) / X_PERIOD * 2. * M_PI))/2.0)
186 * (st->sizex - st->s/2) /* -s/4 */ + MINX;
187 st->tly = ((1. + sin(((double)now) / Y_PERIOD * 2. * M_PI))/2.0)
188 * (st->sizey - st->s/2) /* -s/4 */ + MINY;
191 for (x = i = 0; x < st->sizex; x += (st->pixwidth + st->pixspacex), ++i)
192 for (y = j = 0; y < st->sizey; y += (st->pixheight + st->pixspacey), ++j) {
193 XCopyArea(st->dpy, st->pm /* src */, st->window /* dest */, st->window_gc,
194 st->tlx + i * st->lensoffsetx /* src_x */,
195 st->tly + j * st->lensoffsety /* src_y */,
196 st->pixwidth, st->pixheight,
197 x /* dest_x */, y /* dest_y */);
200 for (x = i = 0; x < st->sizex; x += (st->pixwidth + st->pixspacex), ++i)
201 for (y = j = 0; y < st->sizey; y += (st->pixheight + st->pixspacey), ++j) {
202 XSetForeground(st->dpy, st->window_gc, XGetPixel(st->orig_map, st->tlx+i, st->tly+j));
203 XFillRectangle(st->dpy, st->window, st->window_gc,
204 i * (st->pixwidth + st->pixspacex),
205 j * (st->pixheight + st->pixspacey), st->pixwidth, st->pixheight);
213 zoom_reshape (Display *dpy, Window window, void *closure,
214 unsigned int w, unsigned int h)
219 zoom_event (Display *dpy, Window window, void *closure, XEvent *event)
221 struct state *st = (struct state *) closure;
222 if (screenhack_event_helper (dpy, window, event))
231 zoom_free (Display *dpy, Window window, void *closure)
233 struct state *st = (struct state *) closure;
234 XFreeGC (st->dpy, st->window_gc);
235 if (st->orig_map) XDestroyImage (st->orig_map);
236 if (st->pm) XFreePixmap (st->dpy, st->pm);
241 static const char *zoom_defaults[] = {
242 "*dontClearRoot: True",
243 ".foreground: white",
244 ".background: #111111",
246 #ifdef __sgi /* really, HAVE_READ_DISPLAY_EXTENSION */
259 "*ignoreRotation: True",
260 "*rotateImages: True",
265 static XrmOptionDescRec zoom_options[] = {
266 { "-lenses", ".lenses", XrmoptionNoArg, "true" },
267 { "-no-lenses", ".lenses", XrmoptionNoArg, "false" },
268 { "-delay", ".delay", XrmoptionSepArg, 0 },
269 { "-duration", ".duration", XrmoptionSepArg, 0 },
270 { "-pixwidth", ".pixwidth", XrmoptionSepArg, 0 },
271 { "-pixheight", ".pixheight", XrmoptionSepArg, 0 },
272 { "-pixspacex", ".pixspacex", XrmoptionSepArg, 0 },
273 { "-pixspacey", ".pixspacey", XrmoptionSepArg, 0 },
274 { "-lensoffsetx", ".lensoffsetx", XrmoptionSepArg, 0 },
275 { "-lensoffsety", ".lensoffsety", XrmoptionSepArg, 0 },
279 XSCREENSAVER_MODULE ("Zoom", zoom)