summaryrefslogtreecommitdiff
path: root/app/beforelight/b4light.c
blob: 9a125f9b7559f672262e923c529bf77675930e0f (plain)
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*
 * $XConsortium: b4light.c,v 1.3 94/04/17 20:59:38 rws Exp $
 *
Copyright (c) 1992  X Consortium

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of the X Consortium shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from the X Consortium.
 *
 * Author:  Keith Packard, MIT X Consortium
 */
/* $XFree86: xc/programs/beforelight/b4light.c,v 3.6tsi Exp $ */

#include <X11/Xatom.h>
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Shell.h>
#include <X11/Xaw/Label.h>
#include <X11/Xaw/Cardinals.h>
#include <X11/extensions/scrnsaver.h>
#include <X11/Xcms.h>
#include <stdlib.h>
#include <time.h>

#ifdef NOTDEF
static void quit (Widget w, XEvent *event, 
		  String *params, Cardinal *num_params);

static XtActionsRec beforedark_actions[] = {
    { "quit",	quit },
};

static Atom wm_delete_window;
#endif

static int  ss_event, ss_error;

static Display *display;
static Window  root, saver;
static int screen;
static int  scr_wid, scr_hei;
static Colormap	cmap;
static GC gc, black_gc, erase_gc;
static int  screen_saved;
static XtAppContext app_con;
static GC   bit_1_gc, bit_0_gc;
static Bool	filled = False;

#define MAX_POINTS  16

typedef  struct _moving {
    int  x, y, dx, dy;
} Moving;

static Moving	p[MAX_POINTS];

#define NUM_HISTORY 32

static XPoint	history[NUM_HISTORY][MAX_POINTS];
static Pixmap	old_pixmaps[NUM_HISTORY];
static unsigned long	old_pixels[NUM_HISTORY];
static int	num_points = 3;
static int	history_head, history_tail;
#define hist_bump(h)	((++(h) == NUM_HISTORY) ? ((h) = 0) : 0)

#define NUM_COLORS  64

static unsigned long	black_pixel;
static unsigned long	pixels[NUM_COLORS];
static int  cur_pen = 0;

static void
AllocateColors (void)
{
    double	angle;
    double	step;
    XcmsColor	cms_color;
    int		i;
    XColor	hard, exact;

    XAllocNamedColor (display, cmap, "black", &hard, &exact);
    black_pixel = hard.pixel;
    step = 360.0 / NUM_COLORS;
    for (i = 0; i < NUM_COLORS; i++) {
	angle = i * step;
	cms_color.spec.TekHVC.H = angle;
	cms_color.spec.TekHVC.V = 75.0;
	cms_color.spec.TekHVC.C = 75.0;
	cms_color.format = XcmsTekHVCFormat;
	XcmsAllocColor (display, cmap, &cms_color, XcmsRGBFormat);
	pixels[i] = cms_color.pixel;
    }
}


static void
StepPen (void)
{
    XSetForeground (display, gc, pixels[cur_pen]);
    cur_pen++;
    if (cur_pen == NUM_COLORS)
	cur_pen = 0;
}

static void
DrawPoints (Drawable draw, GC gc, XPoint *p, int n)
{
    XPoint  xp[MAX_POINTS + 1];
    int	    i;

    switch (n) {
    case 1:
	XDrawPoint (display, draw, gc, p->x, p->y);
	break;
    case 2:
	XDrawLine (display, draw, gc, p[0].x, p[0].y, p[1].x, p[1].y);
	break;
    default:
	for (i = 0; i < n; i++) {
	    xp[i].x = p[i].x; xp[i].y = p[i].y;
	}
	xp[i].x = p[0].x; xp[i].y = p[0].y;
	if (filled)
	    XFillPolygon (display, draw, gc, xp, i+1, Complex, CoordModeOrigin);
	else
	    XDrawLines (display, draw, gc, xp, i + 1, CoordModeOrigin);
    }
}

static void
Draw (Moving *p, int n)
{
    XPoint  xp[MAX_POINTS];
    int	    i;
    for (i = 0; i < n; i++) 
    {
	xp[i].x = p[i].x; xp[i].y = p[i].y;
    }
    old_pixels[history_head] = pixels[cur_pen];
    StepPen ();
    DrawPoints (saver, gc, xp, n);
    if (filled)
    {
	XFillRectangle (display, old_pixmaps[history_head], bit_0_gc,
			0, 0, scr_wid, scr_hei);
	DrawPoints (old_pixmaps[history_head], bit_1_gc, xp, n);
	for (i = history_tail; i != history_head; hist_bump(i))
	    DrawPoints (old_pixmaps[i], bit_0_gc, xp, n);
    }
}

static void
Erase (XPoint *p, int n)
{
    if (filled) {
	XSetForeground (display, erase_gc, black_pixel ^ old_pixels[history_tail]);
	XCopyPlane (display, old_pixmaps[history_tail], saver, erase_gc,
		    0, 0, scr_wid, scr_hei, 0, 0, 1);
    }
    else
	DrawPoints (saver, black_gc, p, n);
}

#define STEP_MAX    32

static int
RandomStep (void)
{
    return (rand () % STEP_MAX) + 1;
}

static void
StepMoving (Moving *m)
{
    int	maxx, maxy;

    maxx = DisplayWidth (display, screen);
    maxy = DisplayHeight (display, screen);
    m->x += m->dx;
    if (m->x <= 0) {
	m->x = 0;
	m->dx = RandomStep ();
    }
    if (m->x >= maxx) {
	m->x = maxx - 1;
	m->dx = -RandomStep ();
    }
    m->y += m->dy;
    if (m->y <= 0) {
	m->y = 0;
	m->dy = RandomStep ();
    }
    if (m->y >= maxy) {
	m->y = maxy - 1;
	m->dy = -RandomStep ();
    }
}

static void
StepPoints (void)
{
    int	i;

    for (i = 0; i < num_points; i++)
	StepMoving (&p[i]);
    hist_bump(history_head);
    if (history_tail == history_head)
    {
	Erase (history[history_tail], num_points);
	hist_bump(history_tail);
    }
    Draw (p, num_points);
    for (i = 0; i < num_points; i++)
    {
    	history[history_head][i].x = p[i].x;
    	history[history_head][i].y = p[i].y;
    }
}

static void
StartPoints (void)
{
    history_head = history_tail = 0;
}

static void
Timeout (XtPointer closure, XtIntervalId *id)
{
    if (screen_saved)
    {
	StepPoints ();
	(void) XtAppAddTimeOut (app_con, 50, Timeout, NULL);
    }
}

static void 
StartSaver (void)
{
    if (screen_saved)
	return;
    screen_saved = True;
    StartPoints ();
    StepPoints ();
    (void) XtAppAddTimeOut (app_con, 50, Timeout, NULL);
}

static void
StopSaver (void)
{
    if (!screen_saved)
	return;
    screen_saved = False;
}

static int 
ignoreError (Display *display, XErrorEvent *error)
{
    return 0;
}

int 
main(int argc, char *argv[])
{
    Widget toplevel;
    XEvent  event;
    XScreenSaverNotifyEvent *sevent;
    XSetWindowAttributes    attr;
    XScreenSaverInfo	    *info;
    unsigned long	    mask;
    Pixmap		    blank_pix;
    XColor		    dummyColor;
    XID			    kill_id;
    Atom		    kill_type;
    int			    i;
    int			    (*oldHandler)();
    Window 		    r;
    int			    x, y;
    unsigned int	    w, h, b, d;
    Status		    s;

#if !defined(X_NOT_POSIX)
    srand((int)time((time_t *)NULL));
#else
    srand((int)time((int *)NULL));
#endif

    toplevel = XtAppInitialize (&app_con, "Beforelight", NULL, ZERO,
				&argc, argv, NULL, NULL, ZERO);
    display = XtDisplay (toplevel);
    root = DefaultRootWindow (display);
    screen = DefaultScreen (display);
    scr_wid = DisplayWidth (display, screen);
    scr_hei = DisplayHeight (display, screen);
    if (!XScreenSaverQueryExtension (display, &ss_event, &ss_error))
	exit (1);
#ifdef NOTDEF
    XtAppAddActions (app_con, beforedark_actions, XtNumber(beforedark_actions));

    /*
     * This is a hack so that f.delete will do something useful in this
     * single-window application.
     */
    XtOverrideTranslations(toplevel, 
		    XtParseTranslationTable ("<Message>WM_PROTOCOLS: quit()"));

    XtCreateManagedWidget ("label", labelWidgetClass, toplevel, NULL, ZERO);
    XtRealizeWidget (toplevel);
    wm_delete_window = XInternAtom (XtDisplay(toplevel), "WM_DELETE_WINDOW",
				    False);
    (void) XSetWMProtocols (XtDisplay(toplevel), XtWindow(toplevel),
			    &wm_delete_window, 1);
    
#endif
    oldHandler = XSetErrorHandler (ignoreError);
    if (XScreenSaverGetRegistered (display, screen, &kill_id, &kill_type)) {
	s = XGetGeometry(display, kill_id, &r, &x, &y, &w, &h, &b, &d);
	if (s == True && r == root && w == 1 && h == 1 && d == 1) {
	    /* Try to clean up existing saver & resources */
	    XKillClient (display, kill_id);
	    XScreenSaverUnregister(display, screen);
	}
    }
    XSync(display, FALSE);
    XSetErrorHandler(oldHandler);
    XScreenSaverSelectInput (display, root, ScreenSaverNotifyMask);
#ifdef NOTDEF
    cmap = XCreateColormap (display, root, DefaultVisual (display, screen), AllocNone);
#else
    cmap = DefaultColormap (display, screen);
#endif
    AllocateColors();
    blank_pix = XCreatePixmap (display, root, 1, 1, 1);
    XScreenSaverRegister (display, screen, (XID) blank_pix, XA_PIXMAP);
    bit_0_gc = XCreateGC (display, blank_pix, 0, 0);
    XSetForeground (display, bit_0_gc, 0);
    bit_1_gc = XCreateGC (display, blank_pix, 0, 0);
    XSetForeground (display, bit_1_gc, ~0);
    XFillRectangle (display, blank_pix, bit_0_gc, 0, 0, 1, 1);
    info = XScreenSaverAllocInfo ();
    XScreenSaverQueryInfo (display, root, info);
    mask = 0;
    attr.colormap = cmap;
    mask |= CWColormap;
    attr.background_pixel = black_pixel;
    mask |= CWBackPixel;
    attr.cursor = XCreatePixmapCursor (display, blank_pix, blank_pix, &dummyColor, &dummyColor, 0, 0);
    mask |= CWCursor;
    XScreenSaverSetAttributes (display, root, 0, 0,
	DisplayWidth (display, screen), DisplayHeight(display, screen), 0,
	CopyFromParent, CopyFromParent, CopyFromParent, mask, &attr);
    XSync (display, False);
    gc = XCreateGC (display, root, 0, 0);
    black_gc = XCreateGC (display, root, 0, 0);
    XSetForeground (display, black_gc, black_pixel);
    if (filled)
    {
    	erase_gc = XCreateGC (display, root, 0, 0);
    	XSetBackground (display, erase_gc, 0);
    	XSetFunction (display, erase_gc, GXxor);
    	XSetGraphicsExposures (display, erase_gc, False);
    	for (i = 0; i < NUM_HISTORY; i++)
	    old_pixmaps[i] = XCreatePixmap (display, root, scr_wid, scr_hei, 1);
    }
    XSetErrorHandler (ignoreError);
    saver = info->window;
    if (info->state == ScreenSaverOn)
    {
	if (info->kind != ScreenSaverExternal) 
	{
	    XResetScreenSaver (display);
	    XActivateScreenSaver (display);
	}
	StartSaver ();
    }
    for (;;) 
    {
	XtAppNextEvent (app_con, &event);
	if (event.type == ss_event) {
	    sevent = (XScreenSaverNotifyEvent *) &event;
	    if (sevent->state == ScreenSaverOn) {
		if (sevent->kind != ScreenSaverExternal) {
	    	    XResetScreenSaver (display);
	    	    XActivateScreenSaver (display);
		} else {
		    StartSaver ();
		}
	    } else if (sevent->state == ScreenSaverOff) {
		StopSaver ();
	    }
	} else {
	    XtDispatchEvent(&event);
	}
    }
}

#ifdef NOTDEF
static void 
quit (Widget w, XEvent *event, String *params, Cardinal *num_params)
{
    if (event->type == ClientMessage &&
	event->xclient.data.l[0] != wm_delete_window) {
	XBell (XtDisplay(w), 0);
	return;
    }
    XCloseDisplay (XtDisplay(w));
    exit (0);
}
#endif