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