http://apple.doit.wisc.edu/mirrors/amug/linux/linuxppc/sources/tarballs/xscreensaver...
[xscreensaver] / hacks / spotlight.c
1 /*
2  * spotlight - an xscreensaver module
3  * Copyright (c) 1999 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, 1993, 1994, 1996, 1997, 1998
12  * Jamie Zawinski <jwz@jwz.org>
13  *
14  * Permission to use, copy, modify, distribute, and sell this software and its
15  * documentation for any purpose is hereby granted without fee, provided that
16  * the above copyright notice appear in all copies and that both that
17  * copyright notice and this permission notice appear in supporting
18  * documentation.  No representations are made about the suitability of this
19  * software for any purpose.  It is provided "as is" without express or 
20  * implied warranty.
21  */
22
23 /* #define DEBUG */
24 #include <math.h>
25 #include "screenhack.h"
26 #include <X11/Xutil.h>
27 #include <sys/time.h>
28
29 #define MINX 0.0
30 #define MINY 0.0
31 #define X_PERIOD 15000.0
32 #define Y_PERIOD 12000.0
33
34 static int sizex, sizey; /* screen size */
35 static int delay;        /* in case it's too fast... */
36 static GC gc;
37 #ifdef DEBUG
38 static GC white_gc;
39 #endif
40 static GC buffer_gc;     /* draw in buffer, then flush to screen
41                             to avoid flicker */
42 static int radius;       /* radius of spotlight in pixels */
43 static XGCValues gcv;
44
45 static Pixmap pm;        /* pixmap grabbed from screen */
46 static Pixmap clip_pm;   /* pixmap for clipping (spotlight shape) */
47 static Pixmap buffer;    /* pixmap for the buffer */
48
49 static GC clip_gc;       /* GC for the clip pixmap */
50
51 int x, y, s;             /* x & y coords of buffer (upper left corner) */
52                          /* s is the width of the buffer */
53
54 int oldx, oldy, max_x_speed, max_y_speed;
55                          /* used to keep the new buffer position
56                             over the old spotlight image to make sure 
57                             the old image is completely erased */
58
59 /* The path the spotlight follows around the screen is sinusoidal.
60    This function is fed to sin() to get the x & y coords */
61 static long
62 currentTimeInMs(void)
63 {
64   struct timeval curTime;
65 #ifdef GETTIMEOFDAY_TWO_ARGS
66   struct timezone tz = {0,0};
67   gettimeofday(&curTime, &tz);
68 #else
69   gettimeofday(&curTime);
70 #endif
71   return curTime.tv_sec*1000 + curTime.tv_usec/1000.0;
72 }
73
74
75 static void
76 init_hack (Display *dpy, Window window)
77 {
78   XWindowAttributes xgwa;
79   long gcflags;
80   Colormap cmap;
81   unsigned long fg, bg;
82
83   XGetWindowAttributes (dpy, window, &xgwa);
84   sizex = xgwa.width;
85   sizey = xgwa.height;
86   cmap = xgwa.colormap;
87   fg = get_pixel_resource ("foreground", "Foreground", dpy, cmap);
88   bg = get_pixel_resource ("background", "Background", dpy, cmap);
89
90   /* read parameters, keep em sane */
91   delay = get_integer_resource ("delay", "Integer");
92   if (delay < 1) delay = 1;
93   radius = get_integer_resource ("radius", "Integer");
94   if (radius < 0) radius = 125;
95
96   /* do the dance */
97   gcv.function = GXcopy;
98   gcv.subwindow_mode = IncludeInferiors;
99   gcflags = GCForeground | GCFunction;
100   gcv.foreground = bg;
101   gcv.background = fg;
102
103 #ifdef NOPE
104   if (use_subwindow_mode_p(xgwa.screen, window)) /* see grabscreen.c */
105     gcflags |= GCSubwindowMode;
106 #endif
107   gc = XCreateGC (dpy, window, gcflags, &gcv);
108
109
110   /* grab screen to window */
111   grab_screen_image (xgwa.screen, window);
112
113   /* save screen to pixmap for copying later */
114   pm = XCreatePixmap(dpy, window, sizex, sizey, xgwa.depth);
115   XCopyArea(dpy, window, pm, gc, 0, 0, sizex, sizey, 0, 0);
116
117
118   /* create buffer to reduce flicker */
119   buffer = XCreatePixmap(dpy, window, sizex, sizey, xgwa.depth);
120   buffer_gc = XCreateGC (dpy, buffer, gcflags, &gcv);
121
122   /* blank out screen */
123   XFillRectangle(dpy,window,gc,0,0,sizex,sizey);
124
125   /* create clip mask (so it's a circle, not a square) */
126   clip_pm = XCreatePixmap(dpy, window, radius*4, radius*4, 1);
127   gcv.foreground=bg;
128   gcv.background=bg;
129   clip_gc = XCreateGC(dpy, clip_pm, gcflags, &gcv);
130   XFillRectangle(dpy,clip_pm,clip_gc,0,0,radius*4, radius*4);
131   XSetForeground(dpy,clip_gc,fg);
132   XFillArc(dpy, clip_pm, clip_gc, radius , radius,
133            radius*2, radius*2, 0, 360*64);
134   /* set buffer's clip mask to the one we just made */
135   XSetClipMask(dpy, buffer_gc, clip_pm);
136   /* free everything */
137   XFreeGC(dpy, clip_gc);
138   XFreePixmap(dpy, clip_pm);
139
140   /* avoid remants */
141   max_x_speed = max_y_speed = radius;
142
143 #ifdef DEBUG
144   /* create GC with white fg */
145   gcv.foreground = fg;
146   white_gc = XCreateGC (dpy, window, gcflags, &gcv);
147 #endif
148   
149   /* initialize x and y to avoid initial `jump' across screen */
150   x = ((1 + sin(((float)currentTimeInMs()) / X_PERIOD * 2. * M_PI))/2.0) 
151     * (sizex - s/2) -s/4  + MINX;
152   y = ((1 + sin(((float)currentTimeInMs()) / Y_PERIOD * 2. * M_PI))/2.0) 
153     * (sizey - s/2) -s/4  + MINY;
154
155 }
156
157
158 /*
159  * perform one iteration
160  */
161 static void
162 onestep (Display *dpy, Window window)
163 {
164     long now;
165
166     /* clear buffer */
167     XFillRectangle(dpy, buffer, buffer_gc, x, y, s, s);
168
169     
170 #define nrnd(x) (random() % (x))
171
172     oldx = x;
173     oldy = y;
174
175     s = radius *4 ;   /* s = width of buffer */
176
177     now = currentTimeInMs();
178
179     /* find new x,y */
180     x = ((1 + sin(((float)now) / X_PERIOD * 2. * M_PI))/2.0) 
181       * (sizex - s/2) -s/4  + MINX;
182     y = ((1 + sin(((float)now) / Y_PERIOD * 2. * M_PI))/2.0) 
183       * (sizey - s/2) -s/4  + MINY;
184     
185     /* limit change in x and y to buffer width */
186     if ( x < (oldx - max_x_speed) ) x = oldx - max_x_speed;
187     if ( x > (oldx + max_x_speed) ) x = oldx + max_x_speed;
188     if ( y < (oldy - max_y_speed) ) y = oldy - max_y_speed;
189     if ( y > (oldy + max_y_speed) ) y = oldy + max_y_speed;
190
191     /* copy area of screen image (pm) to buffer
192        Clip to a circle */
193     XSetClipOrigin(dpy, buffer_gc, x,y);
194     XCopyArea(dpy, pm, buffer, buffer_gc, x, y, s, s, x, y);
195     /* copy buffer to screen (window) */
196     XCopyArea(dpy, buffer, window, gc, x , y, s, s, x, y);
197
198 #ifdef DEBUG
199     /* draw a box around the buffer */
200     XDrawLine(dpy, window, white_gc, x, y, x+s, y);
201     XDrawLine(dpy, window, white_gc, x, y, x, y+s);
202     XDrawLine(dpy, window, white_gc, x+s, y, x+s, y+s);
203     XDrawLine(dpy, window, white_gc, x, y+s, x+s, y+s);
204 #endif
205
206 }
207
208 \f
209 char *progclass = "Spotlight";
210
211 char *defaults [] = {
212   "*dontClearRoot:              True",
213
214 #ifdef __sgi    /* really, HAVE_READ_DISPLAY_EXTENSION */
215   "*visualID:                   Best",
216 #endif
217
218   "*delay:                      10000",
219   "*radius:                     125",
220   0
221 };
222
223 XrmOptionDescRec options [] = {
224   { "-delay",           ".delay",               XrmoptionSepArg, 0 },
225   { "-radius",          ".radius",              XrmoptionSepArg, 0 },
226   { 0, 0, 0, 0 }
227 };
228
229 void
230 screenhack (Display *dpy, Window window)
231 {
232   init_hack (dpy, window);
233   while (1) {
234     onestep(dpy, window);
235     XSync(dpy, False);
236     if (delay) usleep (delay);
237     screenhack_handle_events (dpy);
238   }
239 }
240