19663c01a13689d10f1563a40f1980f7e1ab678b
[xscreensaver] / hacks / spotlight.c
1 /*
2  * spotlight - an xscreensaver module
3  * Copyright (c) 1999, 2001 Rick Schultz <rick@skapunx.net>
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 "screenhack.h"
26
27 #define MINX 0.0
28 #define MINY 0.0
29 #define X_PERIOD 15000.0
30 #define Y_PERIOD 12000.0
31
32 struct state {
33   Display *dpy;
34   Window window;
35   Screen *screen;
36
37   int sizex, sizey; /* screen size */
38   int delay;
39   int duration;
40   time_t start_time;
41
42   GC window_gc;
43 #ifdef DEBUG
44   GC white_gc;
45 #endif
46   GC buffer_gc;     /* draw in buffer, then flush to screen
47                        to avoid flicker */
48   int radius;       /* radius of spotlight in pixels */
49
50   Pixmap pm;        /* pixmap grabbed from screen */
51   Pixmap buffer;    /* pixmap for the buffer */
52
53   int x, y, s;      /* x & y coords of buffer (upper left corner) */
54   /* s is the width of the buffer */
55
56   int off;      /* random offset from currentTimeInMs(), so that
57                    two concurrent copies of spotlight have different
58                    behavior. */
59
60   int oldx, oldy, max_x_speed, max_y_speed;
61   /* used to keep the new buffer position
62      over the old spotlight image to make sure 
63      the old image is completely erased */
64
65   Bool first_p;
66   async_load_state *img_loader;
67 };
68
69
70 /* The path the spotlight follows around the screen is sinusoidal.
71    This function is fed to sin() to get the x & y coords */
72 static long
73 currentTimeInMs(struct state *st)
74 {
75   struct timeval curTime;
76 #ifdef GETTIMEOFDAY_TWO_ARGS
77   struct timezone tz = {0,0};
78   gettimeofday(&curTime, &tz);
79 #else
80   gettimeofday(&curTime);
81 #endif
82   return curTime.tv_sec*1000 + curTime.tv_usec/1000.0;
83 }
84
85
86 static void *
87 spotlight_init (Display *dpy, Window window)
88 {
89   struct state *st = (struct state *) calloc (1, sizeof(*st));
90   XGCValues gcv;
91   XWindowAttributes xgwa;
92   long gcflags;
93   Colormap cmap;
94   unsigned long fg, bg;
95   GC clip_gc;
96   Pixmap clip_pm;
97
98   st->dpy = dpy;
99   st->window = window;
100   st->first_p = True;
101
102   XGetWindowAttributes (st->dpy, st->window, &xgwa);
103   st->screen = xgwa.screen;
104   st->sizex = xgwa.width;
105   st->sizey = xgwa.height;
106   cmap = xgwa.colormap;
107   fg = get_pixel_resource (st->dpy, cmap, "foreground", "Foreground");
108   bg = get_pixel_resource (st->dpy, cmap, "background", "Background");
109
110   /* read parameters, keep em sane */
111   st->delay = get_integer_resource (st->dpy, "delay", "Integer");
112   if (st->delay < 1) st->delay = 1;
113   st->duration = get_integer_resource (st->dpy, "duration", "Seconds");
114   if (st->duration < 1) st->duration = 1;
115   st->radius = get_integer_resource (st->dpy, "radius", "Integer");
116   if (st->radius < 0) st->radius = 125;
117
118   /* Don't let the spotlight be bigger than the window */
119   while (st->radius > xgwa.width * 0.45)
120     st->radius /= 2;
121   while (st->radius > xgwa.height * 0.45)
122     st->radius /= 2;
123
124   if (st->radius < 4)
125     st->radius = 4;
126
127   /* do the dance */
128   gcv.function = GXcopy;
129   gcv.subwindow_mode = IncludeInferiors;
130   gcflags = GCForeground | GCFunction;
131   gcv.foreground = bg;
132
133 #ifdef NOPE
134   if (use_subwindow_mode_p(xgwa.screen, st->window)) /* see grabscreen.c */
135     gcflags |= GCSubwindowMode;
136 #endif
137   st->window_gc = XCreateGC(st->dpy, st->window, gcflags, &gcv);
138
139   st->pm = XCreatePixmap(st->dpy, st->window, st->sizex, st->sizey, xgwa.depth);
140   XClearWindow(st->dpy, st->window);
141
142   /* create buffer to reduce flicker */
143 #ifdef HAVE_COCOA       /* Don't second-guess Quartz's double-buffering */
144   st->buffer = 0;
145 #else
146   st->buffer = XCreatePixmap(st->dpy, st->window, st->sizex, st->sizey, xgwa.depth);
147 #endif
148
149   st->buffer_gc = XCreateGC(st->dpy, (st->buffer ? st->buffer : window), gcflags, &gcv);
150   if (st->buffer)
151     XFillRectangle(st->dpy, st->buffer, st->buffer_gc, 0, 0, st->sizex, st->sizey);
152
153   /* blank out screen */
154   XFillRectangle(st->dpy, st->window, st->window_gc, 0, 0, st->sizex, st->sizey);
155   XSetWindowBackground (st->dpy, st->window, bg);
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 #ifdef DEBUG
184   /* create GC with white fg */
185   gcv.foreground = fg;
186   st->white_gc = XCreateGC(st->dpy, st->window, gcflags, &gcv);
187 #endif
188   return st;
189 }
190
191
192 /*
193  * perform one iteration
194  */
195 static void
196 onestep (struct state *st, Bool first_p)
197 {
198   long now;
199
200   if (st->img_loader)   /* still loading */
201     {
202       st->img_loader = load_image_async_simple (st->img_loader, 0, 0, 0, 0, 0);
203       if (! st->img_loader) {  /* just finished */
204         st->start_time = time ((time_t) 0);
205       }
206       return;
207     }
208
209   if (!st->img_loader &&
210       st->start_time + st->duration < time ((time_t) 0)) {
211     st->img_loader = load_image_async_simple (0, st->screen, st->window,
212                                               st->pm, 0, 0);
213     return;
214   }
215
216 #define nrnd(x) (random() % (x))
217
218   st->oldx = st->x;
219   st->oldy = st->y;
220
221   st->s = st->radius *4 ;   /* s = width of buffer */
222
223   now = currentTimeInMs(st) + st->off;
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       /* copy buffer to screen (window) */
257       XCopyArea(st->dpy, st->buffer, st->window, st->window_gc, st->x , st->y, st->s, st->s, st->x, st->y);
258     }
259
260 #ifdef DEBUG
261   /* draw a box around the buffer */
262   XDrawRectangle(st->dpy, st->window, st->white_gc, st->x , st->y, st->s, st->s);
263 #endif
264
265 }
266
267
268 static unsigned long
269 spotlight_draw (Display *dpy, Window window, void *closure)
270 {
271   struct state *st = (struct state *) closure;
272   onestep(st, st->first_p);
273   st->first_p = False;
274   return st->delay;
275 }
276   
277 static void
278 spotlight_reshape (Display *dpy, Window window, void *closure, 
279                  unsigned int w, unsigned int h)
280 {
281 }
282
283 static Bool
284 spotlight_event (Display *dpy, Window window, void *closure, XEvent *event)
285 {
286   return False;
287 }
288
289 static void
290 spotlight_free (Display *dpy, Window window, void *closure)
291 {
292   struct state *st = (struct state *) closure;
293   XFreeGC (dpy, st->window_gc);
294   XFreeGC (dpy, st->buffer_gc);
295   if (st->pm) XFreePixmap (dpy, st->pm);
296   if (st->buffer) XFreePixmap (dpy, st->buffer);
297   free (st);
298 }
299
300
301 \f
302
303 static const char *spotlight_defaults [] = {
304   ".background:                 black",
305   ".foreground:                 white",
306   "*dontClearRoot:              True",
307   "*fpsSolid:                   true",
308
309 #ifdef __sgi    /* really, HAVE_READ_DISPLAY_EXTENSION */
310   "*visualID:                   Best",
311 #endif
312
313   "*delay:                      10000",
314   "*duration:                   120",
315   "*radius:                     125",
316   0
317 };
318
319 static XrmOptionDescRec spotlight_options [] = {
320   { "-delay",           ".delay",               XrmoptionSepArg, 0 },
321   { "-duration",        ".duration",            XrmoptionSepArg, 0 },
322   { "-radius",          ".radius",              XrmoptionSepArg, 0 },
323   { 0, 0, 0, 0 }
324 };
325
326 XSCREENSAVER_MODULE ("Spotlight", spotlight)