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 "*fpsSolid: true \n" \
51 # define BRIGHT_COLORS
52 # define UNIFORM_COLORS
53 # define reshape_flag 0
54 # define flag_handle_event 0
55 # include "xlockmore.h" /* from the xscreensaver distribution */
57 #include "xpm-pixmap.h"
58 #include "images/bob.xbm"
60 #else /* !STANDALONE */
61 # include "xlock.h" /* from the xlockmore distribution */
63 #endif /* !STANDALONE */
67 # include <sys/utsname.h>
68 #endif /* HAVE_UNAME */
71 static XrmOptionDescRec opts[] =
73 { "-bitmap", ".flag.bitmap", XrmoptionSepArg, 0 },
74 { "-text", ".flag.text", XrmoptionSepArg, 0 }
77 #endif /* STANDALONE */
79 ENTRYPOINT ModeSpecOpt flag_opts = {
81 2, opts, 0, NULL, NULL
82 #else /* !STANDALONE */
83 0, NULL, 0, NULL, NULL
84 #endif /* STANDALONE */
94 #define MAXW(fp) (MAXSCALE * (fp)->image->width + 2 * MAXAMP + (fp)->pointsize)
95 #define MAXH(fp) (MAXSCALE * (fp)->image->height+ 2 * MAXAMP + (fp)->pointsize)
96 #define MINW(fp) (MINSCALE * (fp)->image->width + 2 * MINAMP + (fp)->pointsize)
97 #define MINH(fp) (MINSCALE * (fp)->image->height+ 2 * MINAMP + (fp)->pointsize)
118 static flagstruct *flags = NULL;
123 return ((int) (((float) LRAND() / MAXRAND) * (n + 1.0)));
127 initSintab(ModeInfo * mi)
129 flagstruct *fp = &flags[MI_SCREEN(mi)];
133 * change the periodicity of the sin formula : the maximum of the
134 * periocity seem to be 16 ( 2^4 ), after the drawing isn't good looking
136 int periodicity = random_num(4);
139 /* for (i=0;i<periodicity;i++) puissance*=2; */
140 puissance <<= periodicity;
141 for (i = 0; i < ANGLES; i++)
142 fp->stab[i] = (int) (SINF(i * puissance * M_PI / ANGLES) * fp->samp) +
147 affiche(ModeInfo * mi)
149 Display *display = MI_DISPLAY(mi);
151 flagstruct *fp = &flags[MI_SCREEN(mi)];
153 for (x = 0; x < fp->image->width; x++)
154 for (y = fp->image->height-1; y >= 0; y--) {
155 xp = (int) (fp->size * (float) x) +
156 fp->stab[(fp->sidx + x + y) % ANGLES];
157 yp = (int) (fp->size * (float) y) +
158 fp->stab[(fp->sidx + 4 * x + y + y) % ANGLES];
160 if (fp->image->depth > 1)
161 XSetForeground(display, MI_GC(mi),
162 XGetPixel(fp->image, x, y));
163 else if (XGetPixel(fp->image, x, y))
164 XSetForeground(display, MI_GC(mi), MI_WIN_BLACK_PIXEL(mi));
165 else if (MI_NPIXELS(mi) <= 2)
166 XSetForeground(display, MI_GC(mi), MI_WIN_WHITE_PIXEL(mi));
168 XSetForeground(display, MI_GC(mi),
169 MI_PIXEL(mi, (y + x + fp->sidx + fp->startcolor) % MI_NPIXELS(mi)));
171 if (fp->cache == MI_WINDOW(mi)) { /* not double-buffering */
176 if (fp->pointsize <= 1)
177 XDrawPoint(display, fp->cache, MI_GC(mi), xp, yp);
178 else if (fp->pointsize < 6)
179 XFillRectangle(display, fp->cache, MI_GC(mi), xp, yp,
180 fp->pointsize, fp->pointsize);
182 XFillArc(display, fp->cache, MI_GC(mi), xp, yp,
183 fp->pointsize, fp->pointsize, 0, 360*64);
190 make_flag_bits(ModeInfo *mi)
192 Display *dpy = MI_DISPLAY(mi);
193 flagstruct *fp = &flags[MI_SCREEN(mi)];
194 char *bitmap_name = get_string_resource (dpy, "bitmap", "Bitmap");
195 char *text = get_string_resource (dpy, "text", "Text");
198 bitmap_name = 0; /* #### always use default */
201 /* If neither a bitmap nor text are specified, randomly select either
202 the builtin bitmap or builtin text. */
203 if ((!bitmap_name || !*bitmap_name) && (!text || !*text))
208 bitmap_name = strdup("(default)");
213 text = strdup("(default)");
219 !!strcmp(bitmap_name, "(default)"))
225 bitmap = xpm_file_to_pixmap (dpy, MI_WINDOW (mi), bitmap_name,
229 fp->image = XGetImage(dpy, bitmap, 0, 0, width, height, ~0L,
231 XFreePixmap(dpy, bitmap);
234 else if (text && *text)
237 char *fn = get_string_resource (dpy, "font", "Font");
238 char *def_fn = "fixed";
251 if (!strcmp(text, "(default)"))
255 if (uname (&uts) < 0)
257 text = strdup("uname() failed");
262 if ((s = strchr(uts.nodename, '.')))
264 text = (char *) malloc(strlen(uts.nodename) +
265 strlen(uts.sysname) +
266 strlen(uts.version) +
267 strlen(uts.release) + 10);
269 sprintf(text, "%s\n%s %s.%s",
270 uts.nodename, uts.sysname, uts.version, uts.release);
271 # elif defined(__APPLE__) /* MacOS X + XDarwin */
274 "/System/Library/CoreServices/SystemVersion.plist";
275 FILE *f = fopen (file, "r");
276 char *pbv = 0, *pn = 0, *puvv = 0;
280 while (fgets (buf, sizeof(buf)-1, f)) {
282 if (strstr(buf, S)) { \
283 fgets (buf, sizeof(buf)-1, f); \
284 if ((s = strchr (buf, '>'))) V = strdup(s+1); \
285 if ((s = strchr (V, '<'))) *s = 0; \
287 GRAB ("ProductName", pn)
288 GRAB ("ProductBuildVersion", pbv)
289 GRAB ("ProductUserVisibleVersion", puvv)
294 sprintf (text, "%s\n%s\n%s",
295 uts.nodename, pn, puvv /*, uts.machine*/);
297 sprintf(text, "%s\n%s %s",
298 uts.nodename, uts.sysname, uts.release);
301 sprintf(text, "%s\n%s %s",
302 uts.nodename, uts.sysname, uts.release);
303 # endif /* special system types */
305 #else /* !HAVE_UNAME */
307 text = strdup(getenv("SYS$NODE"));
309 text = strdup("X\nScreen\nSaver");
311 #endif /* !HAVE_UNAME */
315 (text[strlen(text)-1] == '\r' ||
316 text[strlen(text)-1] == '\n'))
317 text[strlen(text)-1] = 0;
319 text2 = strdup(text);
321 if (!fn) fn = def_fn;
322 font = XLoadQueryFont (dpy, fn);
325 fprintf(stderr, "%s: unable to load font %s; using %s\n",
326 progname, fn, def_fn);
327 font = XLoadQueryFont (dpy, def_fn);
330 memset(&overall, 0, sizeof(overall));
333 while ((line = strtok(token, "\r\n")))
336 int ascent, descent, direction;
338 XTextExtents(font, line, strlen(line),
339 &direction, &ascent, &descent, &o2);
340 overall.lbearing = MAX(overall.lbearing, o2.lbearing);
341 overall.rbearing = MAX(overall.rbearing, o2.rbearing);
345 width = overall.lbearing + overall.rbearing + margin + margin + 1;
346 height = ((font->ascent + font->descent) * lines) + margin + margin;
348 bitmap = XCreatePixmap(dpy, MI_WINDOW(mi), width, height, 1);
350 gcv.font = font->fid;
352 gc = XCreateGC (dpy, bitmap, (GCFont | GCForeground), &gcv);
353 XFillRectangle(dpy, bitmap, gc, 0, 0, width, height);
354 XSetForeground(dpy, gc, fg);
358 while ((line = strtok(token, "\r\n")))
361 int ascent, descent, direction, xoff;
364 XTextExtents(font, line, strlen(line),
365 &direction, &ascent, &descent, &o2);
366 xoff = ((overall.lbearing + overall.rbearing) -
367 (o2.lbearing + o2.rbearing)) / 2;
369 XDrawString(dpy, bitmap, gc,
370 overall.lbearing + margin + xoff,
371 ((font->ascent * (lines + 1)) +
372 (font->descent * lines) +
378 XUnloadFont(dpy, font->fid);
379 XFree((XPointer) font);
382 fp->image = XGetImage(dpy, bitmap, 0, 0, width, height, 1L, XYPixmap);
383 XFreePixmap(dpy, bitmap);
389 char *bits = (char *) malloc (sizeof(bob_bits));
390 memcpy (bits, bob_bits, sizeof(bob_bits));
391 fp->image = XCreateImage (dpy, MI_VISUAL(mi), 1, XYBitmap, 0,
392 bits, bob_width, bob_height,
394 fp->image->byte_order = LSBFirst;
395 fp->image->bitmap_bit_order = LSBFirst;
404 #else /* !STANDALONE */
407 make_flag_bits(ModeInfo *mi)
409 flagstruct *fp = &flags[MI_SCREEN(mi)];
415 XCreateImage(MI_DISPLAY(mi), MI_VISUAL(mi),
416 1, XYBitmap, 0, /* dpth, fmt, offset */
417 (char *) calloc ((w+8) / 8, h), /* data */
418 w, h, 8, 0); /* w, h, pad, bpl */
419 /* Geez, what kinda goofy bit order is this?? */
420 for (x = 0; x < w; x++)
421 for (y = h-1; y >= 0; y--)
422 XPutPixel (fp->image, x, y, flag_bits[i++]);
425 #endif /* !STANDALONE */
429 init_flag(ModeInfo * mi)
431 Display *display = MI_DISPLAY(mi);
432 int size = MI_SIZE(mi);
436 if ((flags = (flagstruct *) calloc(MI_NUM_SCREENS(mi),
437 sizeof (flagstruct))) == NULL)
440 fp = &flags[MI_SCREEN(mi)];
443 if (!fp->image) abort();
445 fp->width = MI_WIN_WIDTH(mi);
446 fp->height = MI_WIN_HEIGHT(mi);
448 fp->samp = MAXAMP; /* Amplitude */
449 fp->sofs = 20; /* ???????? */
450 fp->pointsize = size;
452 fp->pointsize = NRAND(-size - MINSIZE + 1) + MINSIZE;
453 if (fp->pointsize < MINSIZE ||
454 fp->width <= MAXW(fp) || fp->height <= MAXH(fp))
455 fp->pointsize = MINSIZE;
456 fp->size = MAXINITSIZE; /* Initial distance between pts */
457 fp->inctaille = 0.05;
459 fp->sidx = fp->x_flag = fp->y_flag = 0;
461 if (!fp->initialized) {
463 # ifdef HAVE_COCOA /* Don't second-guess Quartz's double-buffering */
466 fp->initialized = True;
468 fp->cache = MI_WINDOW(mi); /* not double-buffering */
470 if (!(fp->cache = XCreatePixmap(display, MI_WINDOW(mi),
475 #else /* !STANDALONE */
476 error("%s: catastrophe memoire\n");
477 #endif /* !STANDALONE */
479 XSetForeground(display, MI_GC(mi), MI_WIN_BLACK_PIXEL(mi));
480 XFillRectangle(display, fp->cache, MI_GC(mi),
481 0, 0, MAXW(fp), MAXH(fp));
482 /* don't want any exposure events from XCopyArea */
483 XSetGraphicsExposures(display, MI_GC(mi), False);
484 if (MI_NPIXELS(mi) > 2)
485 fp->startcolor = NRAND(MI_NPIXELS(mi));
486 if (fp->width <= MAXW(fp) || fp->height <= MAXH(fp)) {
489 fp->x_flag = random_num(fp->width - MINW(fp));
490 fp->y_flag = random_num(fp->height - MINH(fp));
494 fp->x_flag = random_num(fp->width - MAXW(fp));
495 fp->y_flag = random_num(fp->height - MAXH(fp));
500 XClearWindow(display, MI_WINDOW(mi));
503 ENTRYPOINT void release_flag(ModeInfo * mi);
507 draw_flag(ModeInfo * mi)
509 Display *display = MI_DISPLAY(mi);
510 Window window = MI_WINDOW(mi);
511 flagstruct *fp = &flags[MI_SCREEN(mi)];
513 if (!fp->image) abort();
514 if (fp->cache == window) { /* not double-buffering */
515 XClearWindow (display, window);
516 } else if (fp->width <= MAXW(fp) || fp->height <= MAXH(fp)) {
517 fp->size = MININITSIZE;
518 /* fp->pointsize = MINPOINTSIZE; */
519 XCopyArea(display, fp->cache, window, MI_GC(mi),
520 0, 0, MINW(fp), MINH(fp), fp->x_flag, fp->y_flag);
522 if ((fp->size + fp->inctaille) > MAXSCALE)
523 fp->inctaille = -fp->inctaille;
524 if ((fp->size + fp->inctaille) < MINSCALE)
525 fp->inctaille = -fp->inctaille;
526 fp->size += fp->inctaille;
527 XCopyArea(display, fp->cache, window, MI_GC(mi),
528 0, 0, MAXW(fp), MAXH(fp), fp->x_flag, fp->y_flag);
530 XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_WIN_BLACK_PIXEL(mi));
531 XFillRectangle(display, fp->cache, MI_GC(mi),
532 0, 0, MAXW(fp), MAXH(fp));
535 fp->sidx %= (ANGLES * MI_NPIXELS(mi));
537 if ((MI_CYCLES(mi) > 0) && (fp->timer >= MI_CYCLES(mi)))
545 release_flag(ModeInfo * mi)
550 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++)
552 if (flags[screen].cache && flags[screen].dbufp)
553 XFreePixmap(MI_DISPLAY(mi), flags[screen].cache);
554 if (flags[screen].image)
555 XDestroyImage(flags[screen].image);
557 (void) free((void *) flags);
563 refresh_flag(ModeInfo * mi)
565 /* Do nothing, it will refresh by itself */
568 XSCREENSAVER_MODULE ("Flag", flag)