1 /* -*- Mode: C; tab-width: 4 -*-
2 * flag --- a waving flag
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 * 22-Jan-98: jwz: made the flag wigglier; added xpm support.
28 * (I tried to do this by re-porting from xlockmore, but the
29 * current xlockmore version is completely inscrutable.)
30 * 13-May-97: jwz@jwz.org: turned into a standalone program.
31 * Made it able to animate arbitrary (runtime) text or bitmaps.
36 # define DEF_FONT "Monaco 15"
38 # define DEF_FONT "fixed"
42 # define DEFAULTS "*delay: 50000 \n" \
47 "*font: " DEF_FONT "\n" \
49 # define BRIGHT_COLORS
50 # define UNIFORM_COLORS
51 # define reshape_flag 0
52 # define flag_handle_event 0
53 # include "xlockmore.h" /* from the xscreensaver distribution */
55 #include "xpm-pixmap.h"
56 #include "images/bob.xbm"
58 #else /* !STANDALONE */
59 # include "xlock.h" /* from the xlockmore distribution */
61 #endif /* !STANDALONE */
65 # include <sys/utsname.h>
66 #endif /* HAVE_UNAME */
69 static XrmOptionDescRec opts[] =
71 { "-bitmap", ".flag.bitmap", XrmoptionSepArg, 0 },
72 { "-text", ".flag.text", XrmoptionSepArg, 0 }
75 #endif /* STANDALONE */
77 ENTRYPOINT ModeSpecOpt flag_opts = {
79 2, opts, 0, NULL, NULL
80 #else /* !STANDALONE */
81 0, NULL, 0, NULL, NULL
82 #endif /* STANDALONE */
92 #define MAXW(fp) (MAXSCALE * (fp)->image->width + 2 * MAXAMP + (fp)->pointsize)
93 #define MAXH(fp) (MAXSCALE * (fp)->image->height+ 2 * MAXAMP + (fp)->pointsize)
94 #define MINW(fp) (MINSCALE * (fp)->image->width + 2 * MINAMP + (fp)->pointsize)
95 #define MINH(fp) (MINSCALE * (fp)->image->height+ 2 * MINAMP + (fp)->pointsize)
116 static flagstruct *flags = NULL;
121 return ((int) (((float) LRAND() / MAXRAND) * (n + 1.0)));
125 initSintab(ModeInfo * mi)
127 flagstruct *fp = &flags[MI_SCREEN(mi)];
131 * change the periodicity of the sin formula : the maximum of the
132 * periocity seem to be 16 ( 2^4 ), after the drawing isn't good looking
134 int periodicity = random_num(4);
137 /* for (i=0;i<periodicity;i++) puissance*=2; */
138 puissance <<= periodicity;
139 for (i = 0; i < ANGLES; i++)
140 fp->stab[i] = (int) (SINF(i * puissance * M_PI / ANGLES) * fp->samp) +
145 affiche(ModeInfo * mi)
147 Display *display = MI_DISPLAY(mi);
149 flagstruct *fp = &flags[MI_SCREEN(mi)];
151 for (x = 0; x < fp->image->width; x++)
152 for (y = fp->image->height-1; y >= 0; y--) {
153 xp = (int) (fp->size * (float) x) +
154 fp->stab[(fp->sidx + x + y) % ANGLES];
155 yp = (int) (fp->size * (float) y) +
156 fp->stab[(fp->sidx + 4 * x + y + y) % ANGLES];
158 if (fp->image->depth > 1)
159 XSetForeground(display, MI_GC(mi),
160 XGetPixel(fp->image, x, y));
161 else if (XGetPixel(fp->image, x, y))
162 XSetForeground(display, MI_GC(mi), MI_WIN_BLACK_PIXEL(mi));
163 else if (MI_NPIXELS(mi) <= 2)
164 XSetForeground(display, MI_GC(mi), MI_WIN_WHITE_PIXEL(mi));
166 XSetForeground(display, MI_GC(mi),
167 MI_PIXEL(mi, (y + x + fp->sidx + fp->startcolor) % MI_NPIXELS(mi)));
169 if (fp->cache == MI_WINDOW(mi)) { /* not double-buffering */
174 if (fp->pointsize <= 1)
175 XDrawPoint(display, fp->cache, MI_GC(mi), xp, yp);
176 else if (fp->pointsize < 6)
177 XFillRectangle(display, fp->cache, MI_GC(mi), xp, yp,
178 fp->pointsize, fp->pointsize);
180 XFillArc(display, fp->cache, MI_GC(mi), xp, yp,
181 fp->pointsize, fp->pointsize, 0, 360*64);
188 make_flag_bits(ModeInfo *mi)
190 Display *dpy = MI_DISPLAY(mi);
191 flagstruct *fp = &flags[MI_SCREEN(mi)];
192 char *bitmap_name = get_string_resource (dpy, "bitmap", "Bitmap");
193 char *text = get_string_resource (dpy, "text", "Text");
195 /* If neither a bitmap nor text are specified, randomly select either
196 the builtin bitmap or builtin text. */
197 if ((!bitmap_name || !*bitmap_name) && (!text || !*text))
202 bitmap_name = strdup("(default)");
207 text = strdup("(default)");
213 !!strcmp(bitmap_name, "(default)"))
219 bitmap = xpm_file_to_pixmap (dpy, MI_WINDOW (mi), bitmap_name,
223 fp->image = XGetImage(dpy, bitmap, 0, 0, width, height, ~0L,
225 XFreePixmap(dpy, bitmap);
228 else if (text && *text)
231 char *fn = get_string_resource (dpy, "font", "Font");
232 char *def_fn = "fixed";
245 if (!strcmp(text, "(default)"))
249 if (uname (&uts) < 0)
251 text = strdup("uname() failed");
256 if ((s = strchr(uts.nodename, '.')))
258 text = (char *) malloc(strlen(uts.nodename) +
259 strlen(uts.sysname) +
260 strlen(uts.version) +
261 strlen(uts.release) + 10);
263 sprintf(text, "%s\n%s %s.%s",
264 uts.nodename, uts.sysname, uts.version, uts.release);
266 sprintf(text, "%s\n%s %s",
267 uts.nodename, uts.sysname, uts.release);
270 #else /* !HAVE_UNAME */
272 text = strdup(getenv("SYS$NODE"));
274 text = strdup("X\nScreen\nSaver");
276 #endif /* !HAVE_UNAME */
280 (text[strlen(text)-1] == '\r' ||
281 text[strlen(text)-1] == '\n'))
282 text[strlen(text)-1] = 0;
284 text2 = strdup(text);
286 if (!fn) fn = def_fn;
287 font = XLoadQueryFont (dpy, fn);
290 fprintf(stderr, "%s: unable to load font %s; using %s\n",
291 progname, fn, def_fn);
292 font = XLoadQueryFont (dpy, def_fn);
295 memset(&overall, 0, sizeof(overall));
298 while ((line = strtok(token, "\r\n")))
301 int ascent, descent, direction;
303 XTextExtents(font, line, strlen(line),
304 &direction, &ascent, &descent, &o2);
305 overall.lbearing = MAX(overall.lbearing, o2.lbearing);
306 overall.rbearing = MAX(overall.rbearing, o2.rbearing);
310 width = overall.lbearing + overall.rbearing + margin + margin + 1;
311 height = ((font->ascent + font->descent) * lines) + margin + margin;
313 bitmap = XCreatePixmap(dpy, MI_WINDOW(mi), width, height, 1);
315 gcv.font = font->fid;
317 gc = XCreateGC (dpy, bitmap, (GCFont | GCForeground), &gcv);
318 XFillRectangle(dpy, bitmap, gc, 0, 0, width, height);
319 XSetForeground(dpy, gc, fg);
323 while ((line = strtok(token, "\r\n")))
326 int ascent, descent, direction, xoff;
329 XTextExtents(font, line, strlen(line),
330 &direction, &ascent, &descent, &o2);
331 xoff = ((overall.lbearing + overall.rbearing) -
332 (o2.lbearing + o2.rbearing)) / 2;
334 XDrawString(dpy, bitmap, gc,
335 overall.lbearing + margin + xoff,
336 ((font->ascent * (lines + 1)) +
337 (font->descent * lines) +
343 XUnloadFont(dpy, font->fid);
344 XFree((XPointer) font);
347 fp->image = XGetImage(dpy, bitmap, 0, 0, width, height, 1L, XYPixmap);
348 XFreePixmap(dpy, bitmap);
352 char *bits = (char *) malloc (sizeof(bob_bits));
353 memcpy (bits, bob_bits, sizeof(bob_bits));
354 fp->image = XCreateImage (dpy, MI_VISUAL(mi), 1, XYBitmap, 0,
355 bits, bob_width, bob_height,
357 fp->image->byte_order = LSBFirst;
358 fp->image->bitmap_bit_order = LSBFirst;
367 #else /* !STANDALONE */
370 make_flag_bits(ModeInfo *mi)
372 flagstruct *fp = &flags[MI_SCREEN(mi)];
378 XCreateImage(MI_DISPLAY(mi), MI_VISUAL(mi),
379 1, XYBitmap, 0, /* dpth, fmt, offset */
380 (char *) calloc ((w+8) / 8, h), /* data */
381 w, h, 8, 0); /* w, h, pad, bpl */
382 /* Geez, what kinda goofy bit order is this?? */
383 for (x = 0; x < w; x++)
384 for (y = h-1; y >= 0; y--)
385 XPutPixel (fp->image, x, y, flag_bits[i++]);
388 #endif /* !STANDALONE */
392 init_flag(ModeInfo * mi)
394 Display *display = MI_DISPLAY(mi);
395 int size = MI_SIZE(mi);
399 if ((flags = (flagstruct *) calloc(MI_NUM_SCREENS(mi),
400 sizeof (flagstruct))) == NULL)
403 fp = &flags[MI_SCREEN(mi)];
407 fp->width = MI_WIN_WIDTH(mi);
408 fp->height = MI_WIN_HEIGHT(mi);
410 fp->samp = MAXAMP; /* Amplitude */
411 fp->sofs = 20; /* ???????? */
412 fp->pointsize = size;
414 fp->pointsize = NRAND(-size - MINSIZE + 1) + MINSIZE;
415 if (fp->pointsize < MINSIZE ||
416 fp->width <= MAXW(fp) || fp->height <= MAXH(fp))
417 fp->pointsize = MINSIZE;
418 fp->size = MAXINITSIZE; /* Initial distance between pts */
419 fp->inctaille = 0.05;
421 fp->sidx = fp->x_flag = fp->y_flag = 0;
423 if (!fp->initialized) {
425 # ifdef HAVE_COCOA /* Don't second-guess Quartz's double-buffering */
428 fp->initialized = True;
430 fp->cache = MI_WINDOW(mi); /* not double-buffering */
432 if (!(fp->cache = XCreatePixmap(display, MI_WINDOW(mi),
437 #else /* !STANDALONE */
438 error("%s: catastrophe memoire\n");
439 #endif /* !STANDALONE */
441 XSetForeground(display, MI_GC(mi), MI_WIN_BLACK_PIXEL(mi));
442 XFillRectangle(display, fp->cache, MI_GC(mi),
443 0, 0, MAXW(fp), MAXH(fp));
444 /* don't want any exposure events from XCopyArea */
445 XSetGraphicsExposures(display, MI_GC(mi), False);
446 if (MI_NPIXELS(mi) > 2)
447 fp->startcolor = NRAND(MI_NPIXELS(mi));
448 if (fp->width <= MAXW(fp) || fp->height <= MAXH(fp)) {
451 fp->x_flag = random_num(fp->width - MINW(fp));
452 fp->y_flag = random_num(fp->height - MINH(fp));
456 fp->x_flag = random_num(fp->width - MAXW(fp));
457 fp->y_flag = random_num(fp->height - MAXH(fp));
462 XClearWindow(display, MI_WINDOW(mi));
465 ENTRYPOINT void release_flag(ModeInfo * mi);
469 draw_flag(ModeInfo * mi)
471 Display *display = MI_DISPLAY(mi);
472 Window window = MI_WINDOW(mi);
473 flagstruct *fp = &flags[MI_SCREEN(mi)];
475 if (fp->cache == window) { /* not double-buffering */
476 XClearWindow (display, window);
477 } else if (fp->width <= MAXW(fp) || fp->height <= MAXH(fp)) {
478 fp->size = MININITSIZE;
479 /* fp->pointsize = MINPOINTSIZE; */
480 XCopyArea(display, fp->cache, window, MI_GC(mi),
481 0, 0, MINW(fp), MINH(fp), fp->x_flag, fp->y_flag);
483 if ((fp->size + fp->inctaille) > MAXSCALE)
484 fp->inctaille = -fp->inctaille;
485 if ((fp->size + fp->inctaille) < MINSCALE)
486 fp->inctaille = -fp->inctaille;
487 fp->size += fp->inctaille;
488 XCopyArea(display, fp->cache, window, MI_GC(mi),
489 0, 0, MAXW(fp), MAXH(fp), fp->x_flag, fp->y_flag);
491 XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_WIN_BLACK_PIXEL(mi));
492 XFillRectangle(display, fp->cache, MI_GC(mi),
493 0, 0, MAXW(fp), MAXH(fp));
496 fp->sidx %= (ANGLES * MI_NPIXELS(mi));
498 if ((MI_CYCLES(mi) > 0) && (fp->timer >= MI_CYCLES(mi)))
506 release_flag(ModeInfo * mi)
511 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++)
513 if (flags[screen].cache && flags[screen].dbufp)
514 XFreePixmap(MI_DISPLAY(mi), flags[screen].cache);
515 if (flags[screen].image)
516 XDestroyImage(flags[screen].image);
518 (void) free((void *) flags);
524 refresh_flag(ModeInfo * mi)
526 /* Do nothing, it will refresh by itself */
529 XSCREENSAVER_MODULE ("Flag", flag)