From http://www.jwz.org/xscreensaver/xscreensaver-5.35.tar.gz
[xscreensaver] / hacks / spotlight.c
1 /*
2  * spotlight - an xscreensaver module
3  * Copyright (c) 1999, 2001 Rick Schultz <rick.schultz@gmail.com>
4  *
5  * loosely based on the BackSpace module "StefView" by Darcy Brockbank
6  */
7
8 /* modified from a module from the xscreensaver distribution */
9
10 /*
11  * xscreensaver, Copyright (c) 1992-2006 Jamie Zawinski <jwz@jwz.org>
12  *
13  * Permission to use, copy, modify, distribute, and sell this software and its
14  * documentation for any purpose is hereby granted without fee, provided that
15  * the above copyright notice appear in all copies and that both that
16  * copyright notice and this permission notice appear in supporting
17  * documentation.  No representations are made about the suitability of this
18  * software for any purpose.  It is provided "as is" without express or 
19  * implied warranty.
20  */
21
22
23 /* #define DEBUG */
24 #include <math.h>
25 #include <limits.h>
26 #include "screenhack.h"
27
28 #define MINX 0.0
29 #define MINY 0.0
30 #define X_PERIOD 15000.0
31 #define Y_PERIOD 12000.0
32
33 struct state {
34   Display *dpy;
35   Window window;
36   Screen *screen;
37
38   int sizex, sizey; /* screen size */
39   int delay;
40   int duration;
41   time_t start_time;
42   int first_time;
43   GC window_gc;
44 #ifdef DEBUG
45   GC white_gc;
46 #endif
47   GC buffer_gc;     /* draw in buffer, then flush to screen
48                        to avoid flicker */
49   int radius;       /* radius of spotlight in pixels */
50
51   Pixmap pm;        /* pixmap grabbed from screen */
52   Pixmap buffer;    /* pixmap for the buffer */
53
54   int x, y, s;      /* x & y coords of buffer (upper left corner) */
55   /* s is the width of the buffer */
56
57   int off;      /* random offset from currentTimeInMs(), so that
58                    two concurrent copies of spotlight have different
59                    behavior. */
60
61   int oldx, oldy, max_x_speed, max_y_speed;
62   /* used to keep the new buffer position
63      over the old spotlight image to make sure 
64      the old image is completely erased */
65
66   Bool first_p;
67   async_load_state *img_loader;
68 };
69
70
71 /* The path the spotlight follows around the screen is sinusoidal.
72    This function is fed to sin() to get the x & y coords */
73 static long
74 currentTimeInMs(struct state *st)
75 {
76   struct timeval curTime;
77   unsigned long ret_unsigned;
78 #ifdef GETTIMEOFDAY_TWO_ARGS
79   struct timezone tz = {0,0};
80   gettimeofday(&curTime, &tz);
81 #else
82   gettimeofday(&curTime);
83 #endif
84   ret_unsigned = curTime.tv_sec *1000U + curTime.tv_usec / 1000;
85   return (ret_unsigned <= LONG_MAX) ? ret_unsigned : -1 - (long)(ULONG_MAX - ret_unsigned);
86 }
87
88
89 static void *
90 spotlight_init (Display *dpy, Window window)
91 {
92   struct state *st = (struct state *) calloc (1, sizeof(*st));
93   XGCValues gcv;
94   XWindowAttributes xgwa;
95   long gcflags;
96   Colormap cmap;
97   unsigned long bg;
98   GC clip_gc;
99   Pixmap clip_pm;
100
101   st->dpy = dpy;
102   st->window = window;
103   st->first_p = True;
104
105   XGetWindowAttributes (st->dpy, st->window, &xgwa);
106   st->screen = xgwa.screen;
107   st->sizex = xgwa.width;
108   st->sizey = xgwa.height;
109   cmap = xgwa.colormap;
110   bg = get_pixel_resource (st->dpy, cmap, "background", "Background");
111
112   /* read parameters, keep em sane */
113   st->delay = get_integer_resource (st->dpy, "delay", "Integer");
114   if (st->delay < 1) st->delay = 1;
115   st->duration = get_integer_resource (st->dpy, "duration", "Seconds");
116   if (st->duration < 1) st->duration = 1;
117   st->radius = get_integer_resource (st->dpy, "radius", "Integer");
118   if (st->radius < 0) st->radius = 125;
119
120   /* Don't let the spotlight be bigger than the window */
121   while (st->radius > xgwa.width * 0.45)
122     st->radius /= 2;
123   while (st->radius > xgwa.height * 0.45)
124     st->radius /= 2;
125
126   if (st->radius < 4)
127     st->radius = 4;
128
129   /* do the dance */
130   gcv.function = GXcopy;
131   gcv.subwindow_mode = IncludeInferiors;
132   gcflags = GCForeground | GCFunction;
133   gcv.foreground = bg;
134
135 #ifdef NOPE
136   if (use_subwindow_mode_p(xgwa.screen, st->window)) /* see grabscreen.c */
137     gcflags |= GCSubwindowMode;
138 #endif
139   st->window_gc = XCreateGC(st->dpy, st->window, gcflags, &gcv);
140
141   st->pm = XCreatePixmap(st->dpy, st->window, st->sizex, st->sizey, xgwa.depth);
142   XClearWindow(st->dpy, st->window);
143
144   st->first_time = 1;
145
146   /* create buffer to reduce flicker */
147 #ifdef HAVE_JWXYZ       /* Don't second-guess Quartz's double-buffering */
148   st->buffer = 0;
149 #else
150   st->buffer = XCreatePixmap(st->dpy, st->window, st->sizex, st->sizey, xgwa.depth);
151 #endif
152
153   st->buffer_gc = XCreateGC(st->dpy, (st->buffer ? st->buffer : window), gcflags, &gcv);
154   if (st->buffer)
155     XFillRectangle(st->dpy, st->buffer, st->buffer_gc, 0, 0, st->sizex, st->sizey);
156
157   /* create clip mask (so it's a circle, not a square) */
158   clip_pm = XCreatePixmap(st->dpy, st->window, st->radius*4, st->radius*4, 1);
159   st->img_loader = load_image_async_simple (0, xgwa.screen, st->window, st->pm,
160                                             0, 0);
161   st->start_time = time ((time_t *) 0);
162
163   gcv.foreground = 0L;
164   clip_gc = XCreateGC(st->dpy, clip_pm, gcflags, &gcv);
165   XFillRectangle(st->dpy, clip_pm, clip_gc, 0, 0, st->radius*4, st->radius*4);
166
167   XSetForeground(st->dpy, clip_gc, 1L);
168   XFillArc(st->dpy, clip_pm, clip_gc, st->radius , st->radius,
169            st->radius*2, st->radius*2, 0, 360*64);
170
171   /* set buffer's clip mask to the one we just made */
172   XSetClipMask(st->dpy, st->buffer_gc, clip_pm);
173
174   /* free everything */
175   XFreeGC(st->dpy, clip_gc);
176   XFreePixmap(st->dpy, clip_pm);
177
178   /* avoid remants */
179   st->max_x_speed = st->max_y_speed = st->radius;
180   
181   st->off = random();
182
183   /* blank out screen */
184   XFillRectangle(st->dpy, st->window, st->window_gc, 0, 0, st->sizex, st->sizey);
185
186   return st;
187 }
188
189
190 /*
191  * perform one iteration
192  */
193 static void
194 onestep (struct state *st, Bool first_p)
195 {
196   long now;
197   unsigned long now_unsigned;
198
199   if (st->img_loader)   /* still loading */
200     {
201       st->img_loader = load_image_async_simple (st->img_loader, 0, 0, 0, 0, 0);
202       if (! st->img_loader) {  /* just finished */
203         st->start_time = time ((time_t *) 0);
204       }
205       return;
206     }
207
208   if (!st->img_loader &&
209       st->start_time + st->duration < time ((time_t *) 0)) {
210     st->img_loader = load_image_async_simple (0, st->screen, st->window,
211                                               st->pm, 0, 0);
212     return;
213   }
214
215 #define nrnd(x) (random() % (x))
216
217   st->oldx = st->x;
218   st->oldy = st->y;
219
220   st->s = st->radius *4 ;   /* s = width of buffer */
221
222   now_unsigned = (unsigned long) currentTimeInMs(st) + st->off;
223   now = (now_unsigned <= LONG_MAX) ? now_unsigned : -1 - (long)(ULONG_MAX - now_unsigned);
224
225   /* find new x,y */
226   st->x = ((1 + sin(((double)now) / X_PERIOD * 2. * M_PI))/2.0) 
227     * (st->sizex - st->s/2) -st->s/4  + MINX;
228   st->y = ((1 + sin(((double)now) / Y_PERIOD * 2. * M_PI))/2.0) 
229     * (st->sizey - st->s/2) -st->s/4  + MINY;
230     
231   if (!st->first_p)
232     {
233       /* limit change in x and y to buffer width */
234       if ( st->x < (st->oldx - st->max_x_speed) ) st->x = st->oldx - st->max_x_speed;
235       if ( st->x > (st->oldx + st->max_x_speed) ) st->x = st->oldx + st->max_x_speed;
236       if ( st->y < (st->oldy - st->max_y_speed) ) st->y = st->oldy - st->max_y_speed;
237       if ( st->y > (st->oldy + st->max_y_speed) ) st->y = st->oldy + st->max_y_speed;
238     }
239
240   if (! st->buffer)
241     {
242       XClearWindow (st->dpy, st->window);
243       XSetClipOrigin(st->dpy, st->buffer_gc, st->x,st->y);
244       XCopyArea(st->dpy, st->pm, st->window, st->buffer_gc, st->x, st->y, st->s, st->s, st->x, st->y);
245     }
246   else
247     {
248       /* clear buffer */
249       XFillRectangle(st->dpy, st->buffer, st->buffer_gc, st->x, st->y, st->s, st->s);
250
251       /* copy area of screen image (pm) to buffer
252          Clip to a circle */
253       XSetClipOrigin(st->dpy, st->buffer_gc, st->x,st->y);
254       XCopyArea(st->dpy, st->pm, st->buffer, st->buffer_gc, st->x, st->y, st->s, st->s, st->x, st->y);
255
256       if (st->first_time) {
257         /* blank out screen */
258         XFillRectangle(st->dpy, st->window, st->window_gc, 0, 0, st->sizex, st->sizey);
259         st->first_time = 0;
260       }
261
262       /* copy buffer to screen (window) */
263       XCopyArea(st->dpy, st->buffer, st->window, st->window_gc, st->x , st->y, st->s, st->s, st->x, st->y);
264     }
265
266 #ifdef DEBUG
267   /* draw a box around the buffer */
268   XDrawRectangle(st->dpy, st->window, st->white_gc, st->x , st->y, st->s, st->s);
269 #endif
270
271 }
272
273
274 static unsigned long
275 spotlight_draw (Display *dpy, Window window, void *closure)
276 {
277   struct state *st = (struct state *) closure;
278   onestep(st, st->first_p);
279   st->first_p = False;
280   return st->delay;
281 }
282   
283 static void
284 spotlight_reshape (Display *dpy, Window window, void *closure, 
285                  unsigned int w, unsigned int h)
286 {
287 }
288
289 static Bool
290 spotlight_event (Display *dpy, Window window, void *closure, XEvent *event)
291 {
292   struct state *st = (struct state *) closure;
293   if (screenhack_event_helper (dpy, window, event))
294     {
295       st->start_time = 0;
296       return True;
297     }
298   return False;
299 }
300
301 static void
302 spotlight_free (Display *dpy, Window window, void *closure)
303 {
304   struct state *st = (struct state *) closure;
305   XFreeGC (dpy, st->window_gc);
306   XFreeGC (dpy, st->buffer_gc);
307   if (st->pm) XFreePixmap (dpy, st->pm);
308   if (st->buffer) XFreePixmap (dpy, st->buffer);
309   free (st);
310 }
311
312
313 \f
314
315 static const char *spotlight_defaults [] = {
316   ".background:                 black",
317   ".foreground:                 white",
318   "*dontClearRoot:              True",
319   "*fpsSolid:                   true",
320
321 #ifdef __sgi    /* really, HAVE_READ_DISPLAY_EXTENSION */
322   "*visualID:                   Best",
323 #endif
324
325   "*delay:                      10000",
326   "*duration:                   120",
327   "*radius:                     125",
328 #ifdef HAVE_MOBILE
329   "*ignoreRotation:             True",
330   "*rotateImages:               True",
331 #endif
332   0
333 };
334
335 static XrmOptionDescRec spotlight_options [] = {
336   { "-delay",           ".delay",               XrmoptionSepArg, 0 },
337   { "-duration",        ".duration",            XrmoptionSepArg, 0 },
338   { "-radius",          ".radius",              XrmoptionSepArg, 0 },
339   { 0, 0, 0, 0 }
340 };
341
342 XSCREENSAVER_MODULE ("Spotlight", spotlight)