1 /* -*- Mode: C; tab-width: 4 -*-
2 * flag --- a waving flag
4 #if !defined( lint ) && !defined( SABER )
5 static const char sccsid[] = "@(#)flag.c 4.02 97/04/01 xlockmore";
8 /* Copyright (c) 1996 Charles Vidal <vidalc@univ-mlv.fr>.
9 * PEtite demo X11 de charles vidal 15 05 96
10 * tourne sous Linux et SOLARIS
11 * thank's to Bas van Gaalen, Holland, PD, for his sources
12 * in pascal vous devez rajouter une ligne dans mode.c
14 * Permission to use, copy, modify, and distribute this software and its
15 * documentation for any purpose and without fee is hereby granted,
16 * provided that the above copyright notice appear in all copies and that
17 * both that copyright notice and this permission notice appear in
18 * supporting documentation.
20 * This file is provided AS IS with no warranties of any kind. The author
21 * shall have no liability with respect to the infringement of copyrights,
22 * trade secrets or any patents by this file or any part thereof. In no
23 * event will the author be liable for any lost revenue or profits or
24 * other special, indirect and consequential damages.
27 * 13-May-97: jwz@netscape.com: turned into a standalone program.
28 * Made it able to animate arbitrary (runtime) text or bitmaps.
33 # define PROGCLASS "Flag"
34 # define HACK_INIT init_flag
35 # define HACK_DRAW draw_flag
36 # define flag_opts xlockmore_opts
37 # define DEFAULTS "*delay: 50000 \n" \
41 # define BRIGHT_COLORS
42 # define UNIFORM_COLORS
43 # define DEF_FONT "-*-helvetica-bold-r-*-240-*"
44 # define DEF_BITMAP ""
46 # include "xlockmore.h" /* from the xscreensaver distribution */
50 # include <X11/Xmu/Drawing.h>
52 # include <Xmu/Drawing.h>
58 #else /* !STANDALONE */
59 # include "xlock.h" /* from the xlockmore distribution */
61 #endif /* !STANDALONE */
64 #if defined(VMS) && !defined(HAVE_UNAME) && (__VMS_VER >= 70000000)
69 # include <sys/utsname.h>
70 #endif /* HAVE_UNAME */
72 ModeSpecOpt flag_opts = {
73 0, NULL, 0, NULL, NULL };
76 #include <X11/Xutil.h>
85 #define MAXW(fp) (MAXSCALE * (fp)->image->width + 2 * MAXAMP + (fp)->pointsize)
86 #define MAXH(fp) (MAXSCALE * (fp)->image->height+ 2 * MAXAMP + (fp)->pointsize)
87 #define MINW(fp) (MINSCALE * (fp)->image->width + 2 * MINAMP + (fp)->pointsize)
88 #define MINH(fp) (MINSCALE * (fp)->image->height+ 2 * MINAMP + (fp)->pointsize)
108 static flagstruct *flags = NULL;
113 return ((int) (((float) LRAND() / MAXRAND) * (n + 1.0)));
117 initSintab(ModeInfo * mi)
119 flagstruct *fp = &flags[MI_SCREEN(mi)];
122 for (i = 0; i < ANGLES; i++)
123 fp->stab[i] = (int) (SINF(i * 4 * M_PI / ANGLES) * fp->samp) + fp->sofs;
127 affiche(ModeInfo * mi)
129 Display *display = MI_DISPLAY(mi);
131 flagstruct *fp = &flags[MI_SCREEN(mi)];
133 for (x = 0; x < fp->image->width; x++)
134 for (y = fp->image->height-1; y >= 0; y--) {
135 xp = (int) (fp->size * (float) x) +
136 fp->stab[(fp->sidx + x + y) % ANGLES];
137 yp = (int) (fp->size * (float) y) +
138 fp->stab[(fp->sidx + 4 * x + y + y) % ANGLES];
139 if (XGetPixel(fp->image, x, y))
140 XSetForeground(display, MI_GC(mi), MI_WIN_BLACK_PIXEL(mi));
141 else if (MI_NPIXELS(mi) <= 2)
142 XSetForeground(display, MI_GC(mi), MI_WIN_WHITE_PIXEL(mi));
144 XSetForeground(display, MI_GC(mi),
145 MI_PIXEL(mi, (y + x + fp->sidx + fp->startcolor) % MI_NPIXELS(mi)));
146 if (fp->pointsize <= 1)
147 XDrawPoint(display, fp->cache, MI_GC(mi), xp, yp);
148 else if (fp->pointsize < 6)
149 XFillRectangle(display, fp->cache, MI_GC(mi), xp, yp,
150 fp->pointsize, fp->pointsize);
152 XFillArc(display, fp->cache, MI_GC(mi), xp, yp,
153 fp->pointsize, fp->pointsize, 0, 360*64);
160 make_flag_bits(ModeInfo *mi)
162 Display *dpy = MI_DISPLAY(mi);
163 flagstruct *fp = &flags[MI_SCREEN(mi)];
164 char *bitmap_name = get_string_resource ("bitmap", "Bitmap");
165 char *text = get_string_resource ("text", "Text");
167 /* If neither a bitmap nor text are specified, randomly select either
168 the builtin bitmap or builtin text. */
169 if ((!bitmap_name || !*bitmap_name) && (!text || !*text))
174 bitmap_name = strdup("(default)");
179 text = strdup("(default)");
185 !!strcmp(bitmap_name, "(default)"))
188 int width, height, xh, yh;
190 XmuLocateBitmapFile (DefaultScreenOfDisplay (dpy),
191 bitmap_name, 0, 0, &width, &height, &xh, &yh);
194 fprintf(stderr, "%s: unable to load bitmap file %s\n",
195 progname, bitmap_name);
198 fp->image = XGetImage(dpy, bitmap, 0, 0, width, height,
200 XFreePixmap(dpy, bitmap);
204 "%s: your vendor doesn't ship the standard Xmu library.\n",
206 fprintf (stderr, "\tWe can't load XBM files without it.\n");
211 else if (text && *text)
214 char *fn = get_string_resource ("font", "Font");
215 char *def_fn = "fixed";
228 if (!strcmp(text, "(default)"))
232 if (uname (&uts) < 0)
234 text = strdup("uname() failed");
239 if ((s = strchr(uts.nodename, '.')))
241 text = (char *) malloc(strlen(uts.nodename) +
242 strlen(uts.sysname) +
243 strlen(uts.release) + 10);
244 sprintf(text, "%s\n%s %s",
245 uts.nodename, uts.sysname, uts.release);
247 #else /* !HAVE_UNAME */
249 text = strdup(getenv("SYS$NODE"));
251 text = strdup("X\nScreen\nSaver");
253 #endif /* !HAVE_UNAME */
257 (text[strlen(text)-1] == '\r' ||
258 text[strlen(text)-1] == '\n'))
259 text[strlen(text)-1] = 0;
261 text2 = strdup(text);
263 if (!fn) fn = def_fn;
264 font = XLoadQueryFont (dpy, fn);
267 fprintf(stderr, "%s: unable to load font %s; using %s\n",
268 progname, fn, def_fn);
269 font = XLoadQueryFont (dpy, def_fn);
272 memset(&overall, 0, sizeof(overall));
275 while ((line = strtok(token, "\r\n")))
278 int ascent, descent, direction;
280 XTextExtents(font, line, strlen(line),
281 &direction, &ascent, &descent, &o2);
282 overall.lbearing = MAX(overall.lbearing, o2.lbearing);
283 overall.rbearing = MAX(overall.rbearing, o2.rbearing);
287 width = overall.lbearing + overall.rbearing + margin + margin + 1;
288 height = ((font->ascent + font->descent) * lines) + margin + margin;
290 bitmap = XCreatePixmap(dpy, MI_WINDOW(mi), width, height, 1);
292 gcv.font = font->fid;
294 gc = XCreateGC (dpy, bitmap, (GCFont | GCForeground), &gcv);
295 XFillRectangle(dpy, bitmap, gc, 0, 0, width, height);
296 XSetForeground(dpy, gc, fg);
300 while ((line = strtok(token, "\r\n")))
303 int ascent, descent, direction, xoff;
306 XTextExtents(font, line, strlen(line),
307 &direction, &ascent, &descent, &o2);
308 xoff = ((overall.lbearing + overall.rbearing) -
309 (o2.lbearing + o2.rbearing)) / 2;
311 XDrawString(dpy, bitmap, gc,
312 overall.lbearing + margin + xoff,
313 ((font->ascent * (lines + 1)) +
314 (font->descent * lines) +
320 XUnloadFont(dpy, font->fid);
321 XFree((XPointer) font);
324 fp->image = XGetImage(dpy, bitmap, 0, 0, width, height, 1L, XYPixmap);
325 XFreePixmap(dpy, bitmap);
329 fp->image = XCreateImage (dpy, MI_VISUAL(mi), 1, XYBitmap, 0,
330 (char *) bob_bits, bob_width, bob_height,
332 fp->image->byte_order = LSBFirst;
333 fp->image->bitmap_bit_order = LSBFirst;
342 #else /* !STANDALONE */
345 make_flag_bits(ModeInfo *mi)
347 flagstruct *fp = &flags[MI_SCREEN(mi)];
353 XCreateImage(MI_DISPLAY(mi), MI_VISUAL(mi),
354 1, XYBitmap, 0, /* dpth, fmt, offset */
355 (char *) calloc ((w+8) / 8, h), /* data */
356 w, h, 8, 0); /* w, h, pad, bpl */
357 /* Geez, what kinda goofy bit order is this?? */
358 for (x = 0; x < w; x++)
359 for (y = h-1; y >= 0; y--)
360 XPutPixel (fp->image, x, y, flag_bits[i++]);
363 #endif /* !STANDALONE */
367 init_flag(ModeInfo * mi)
369 Display *display = MI_DISPLAY(mi);
370 int size = MI_SIZE(mi);
374 if ((flags = (flagstruct *) calloc(MI_NUM_SCREENS(mi),
375 sizeof (flagstruct))) == NULL)
378 fp = &flags[MI_SCREEN(mi)];
382 fp->width = MI_WIN_WIDTH(mi);
383 fp->height = MI_WIN_HEIGHT(mi);
385 fp->samp = MAXAMP; /* Amplitude */
386 fp->sofs = 20; /* ???????? */
387 fp->pointsize = size;
389 fp->pointsize = NRAND(-size - MINSIZE + 1) + MINSIZE;
390 if (fp->pointsize < MINSIZE ||
391 fp->width <= MAXW(fp) || fp->height <= MAXH(fp))
392 fp->pointsize = MINSIZE;
393 fp->size = MAXINITSIZE; /* Initial distance between pts */
394 fp->inctaille = 0.05;
396 fp->sidx = fp->x_flag = fp->y_flag = 0;
398 if (!fp->initialized) {
399 fp->initialized = True;
400 if (!(fp->cache = XCreatePixmap(display, MI_WINDOW(mi),
401 MAXW(fp), MAXH(fp), MI_WIN_DEPTH(mi))))
404 #else /* !STANDALONE */
405 error("%s: catastrophe memoire\n");
406 #endif /* !STANDALONE */
408 XSetForeground(display, MI_GC(mi), MI_WIN_BLACK_PIXEL(mi));
409 XFillRectangle(display, fp->cache, MI_GC(mi),
410 0, 0, MAXW(fp), MAXH(fp));
411 /* don't want any exposure events from XCopyArea */
412 XSetGraphicsExposures(display, MI_GC(mi), False);
413 if (MI_NPIXELS(mi) > 2)
414 fp->startcolor = NRAND(MI_NPIXELS(mi));
415 if (fp->width <= MAXW(fp) || fp->height <= MAXH(fp)) {
418 fp->x_flag = random_num(fp->width - MINW(fp));
419 fp->y_flag = random_num(fp->height - MINH(fp));
423 fp->x_flag = random_num(fp->width - MAXW(fp));
424 fp->y_flag = random_num(fp->height - MAXH(fp));
429 XClearWindow(display, MI_WINDOW(mi));
433 draw_flag(ModeInfo * mi)
435 Display *display = MI_DISPLAY(mi);
436 Window window = MI_WINDOW(mi);
437 flagstruct *fp = &flags[MI_SCREEN(mi)];
439 if (fp->width <= MAXW(fp) || fp->height <= MAXH(fp)) {
440 fp->size = MININITSIZE;
441 /* fp->pointsize = MINPOINTSIZE; */
442 XCopyArea(display, fp->cache, window, MI_GC(mi),
443 0, 0, MINW(fp), MINH(fp), fp->x_flag, fp->y_flag);
445 if ((fp->size + fp->inctaille) > MAXSCALE)
446 fp->inctaille = -fp->inctaille;
447 if ((fp->size + fp->inctaille) < MINSCALE)
448 fp->inctaille = -fp->inctaille;
449 fp->size += fp->inctaille;
450 XCopyArea(display, fp->cache, window, MI_GC(mi),
451 0, 0, MAXW(fp), MAXH(fp), fp->x_flag, fp->y_flag);
453 XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_WIN_BLACK_PIXEL(mi));
454 XFillRectangle(display, fp->cache, MI_GC(mi),
455 0, 0, MAXW(fp), MAXH(fp));
459 fp->sidx %= (ANGLES * MI_NPIXELS(mi));
462 if ((MI_CYCLES(mi) > 0) && (fp->timer >= MI_CYCLES(mi)))
467 release_flag(ModeInfo * mi)
472 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++)
474 if (flags[screen].cache)
475 XFreePixmap(MI_DISPLAY(mi), flags[screen].cache);
476 if (flags[screen].image)
477 XDestroyImage(flags[screen].image);
479 (void) free((void *) flags);
485 refresh_flag(ModeInfo * mi)
487 /* Do nothing, it will refresh by itself */