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 * 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 PROGCLASS "Flag"
37 # define HACK_INIT init_flag
38 # define HACK_DRAW draw_flag
39 # define flag_opts xlockmore_opts
40 # define DEFAULTS "*delay: 50000 \n" \
44 # define BRIGHT_COLORS
45 # define UNIFORM_COLORS
46 # define DEF_FONT "-*-helvetica-bold-r-*-240-*"
47 # define DEF_BITMAP ""
49 # include "xlockmore.h" /* from the xscreensaver distribution */
53 # ifndef PIXEL_ALREADY_TYPEDEFED
54 # define PIXEL_ALREADY_TYPEDEFED /* Sigh, Xmu/Drawing.h needs this... */
60 # include <X11/Xmu/Drawing.h>
62 # include <Xmu/Drawing.h>
66 #include "images/bob.xbm"
68 #else /* !STANDALONE */
69 # include "xlock.h" /* from the xlockmore distribution */
71 #endif /* !STANDALONE */
75 # include <sys/utsname.h>
76 #endif /* HAVE_UNAME */
79 static XrmOptionDescRec opts[] =
81 { "-bitmap", ".flag.bitmap", XrmoptionSepArg, 0 },
82 { "-text", ".flag.text", XrmoptionSepArg, 0 }
85 #endif /* STANDALONE */
87 ModeSpecOpt flag_opts = {
89 2, opts, 0, NULL, NULL
90 #else /* !STANDALONE */
91 0, NULL, 0, NULL, NULL
92 #endif /* STANDALONE */
96 #include <X11/Xutil.h>
101 #define MAXINITSIZE 6
102 #define MININITSIZE 2
105 #define MAXW(fp) (MAXSCALE * (fp)->image->width + 2 * MAXAMP + (fp)->pointsize)
106 #define MAXH(fp) (MAXSCALE * (fp)->image->height+ 2 * MAXAMP + (fp)->pointsize)
107 #define MINW(fp) (MINSCALE * (fp)->image->width + 2 * MINAMP + (fp)->pointsize)
108 #define MINH(fp) (MINSCALE * (fp)->image->height+ 2 * MINAMP + (fp)->pointsize)
128 static flagstruct *flags = NULL;
133 return ((int) (((float) LRAND() / MAXRAND) * (n + 1.0)));
137 initSintab(ModeInfo * mi)
139 flagstruct *fp = &flags[MI_SCREEN(mi)];
143 * change the periodicity of the sin formula : the maximum of the
144 * periocity seem to be 16 ( 2^4 ), after the drawing isn't good looking
146 int periodicity = random_num(4);
149 /* for (i=0;i<periodicity;i++) puissance*=2; */
150 puissance <<= periodicity;
151 for (i = 0; i < ANGLES; i++)
152 fp->stab[i] = (int) (SINF(i * puissance * M_PI / ANGLES) * fp->samp) +
157 affiche(ModeInfo * mi)
159 Display *display = MI_DISPLAY(mi);
161 flagstruct *fp = &flags[MI_SCREEN(mi)];
163 for (x = 0; x < fp->image->width; x++)
164 for (y = fp->image->height-1; y >= 0; y--) {
165 xp = (int) (fp->size * (float) x) +
166 fp->stab[(fp->sidx + x + y) % ANGLES];
167 yp = (int) (fp->size * (float) y) +
168 fp->stab[(fp->sidx + 4 * x + y + y) % ANGLES];
170 if (fp->image->depth > 1)
171 XSetForeground(display, MI_GC(mi),
172 XGetPixel(fp->image, x, y));
173 else if (XGetPixel(fp->image, x, y))
174 XSetForeground(display, MI_GC(mi), MI_WIN_BLACK_PIXEL(mi));
175 else if (MI_NPIXELS(mi) <= 2)
176 XSetForeground(display, MI_GC(mi), MI_WIN_WHITE_PIXEL(mi));
178 XSetForeground(display, MI_GC(mi),
179 MI_PIXEL(mi, (y + x + fp->sidx + fp->startcolor) % MI_NPIXELS(mi)));
181 if (fp->pointsize <= 1)
182 XDrawPoint(display, fp->cache, MI_GC(mi), xp, yp);
183 else if (fp->pointsize < 6)
184 XFillRectangle(display, fp->cache, MI_GC(mi), xp, yp,
185 fp->pointsize, fp->pointsize);
187 XFillArc(display, fp->cache, MI_GC(mi), xp, yp,
188 fp->pointsize, fp->pointsize, 0, 360*64);
195 make_flag_bits(ModeInfo *mi)
197 Display *dpy = MI_DISPLAY(mi);
198 flagstruct *fp = &flags[MI_SCREEN(mi)];
199 char *bitmap_name = get_string_resource ("bitmap", "Bitmap");
200 char *text = get_string_resource ("text", "Text");
202 /* If neither a bitmap nor text are specified, randomly select either
203 the builtin bitmap or builtin text. */
204 if ((!bitmap_name || !*bitmap_name) && (!text || !*text))
209 bitmap_name = strdup("(default)");
214 text = strdup("(default)");
220 !!strcmp(bitmap_name, "(default)"))
223 Window window = MI_WINDOW(mi);
224 XWindowAttributes xgwa;
225 XpmAttributes xpmattrs;
228 int width = 0, height = 0;
229 xpmattrs.valuemask = 0;
231 XGetWindowAttributes (dpy, window, &xgwa);
234 xpmattrs.valuemask |= XpmCloseness;
235 xpmattrs.closeness = 40000;
238 xpmattrs.valuemask |= XpmVisual;
239 xpmattrs.visual = xgwa.visual;
242 xpmattrs.valuemask |= XpmDepth;
243 xpmattrs.depth = xgwa.depth;
246 xpmattrs.valuemask |= XpmColormap;
247 xpmattrs.colormap = xgwa.colormap;
250 /* Uh, we don't need these now. We use the colors from the xpm.
251 It kinda sucks that we already allocated them. */
252 XFreeColors(dpy, xgwa.colormap, mi->pixels, mi->npixels, 0L);
254 result = XpmReadFileToPixmap (dpy, window, bitmap_name, &bitmap, 0,
259 fprintf (stderr, "%s: warning: xpm color substitution performed\n",
263 width = xpmattrs.width;
264 height = xpmattrs.height;
271 fprintf (stderr, "%s: xpm: color allocation failed\n", progname);
274 fprintf (stderr, "%s: xpm: out of memory\n", progname);
277 fprintf (stderr, "%s: xpm: unknown error code %d\n", progname,
284 fp->image = XGetImage(dpy, bitmap, 0, 0, width, height, ~0L,
286 XFreePixmap(dpy, bitmap);
289 #endif /* HAVE_XPM */
293 int width, height, xh, yh;
295 XmuLocateBitmapFile (DefaultScreenOfDisplay (dpy),
296 bitmap_name, 0, 0, &width, &height, &xh, &yh);
299 fprintf(stderr, "%s: unable to load bitmap file %s\n",
300 progname, bitmap_name);
303 fp->image = XGetImage(dpy, bitmap, 0, 0, width, height,
305 XFreePixmap(dpy, bitmap);
310 "%s: your vendor doesn't ship the standard Xmu library.\n",
312 fprintf (stderr, "\tWe can't load XBM files without it.\n");
317 else if (text && *text)
320 char *fn = get_string_resource ("font", "Font");
321 char *def_fn = "fixed";
334 if (!strcmp(text, "(default)"))
338 if (uname (&uts) < 0)
340 text = strdup("uname() failed");
345 if ((s = strchr(uts.nodename, '.')))
347 text = (char *) malloc(strlen(uts.nodename) +
348 strlen(uts.sysname) +
349 strlen(uts.version) +
350 strlen(uts.release) + 10);
352 sprintf(text, "%s\n%s %s.%s",
353 uts.nodename, uts.sysname, uts.version, uts.release);
355 sprintf(text, "%s\n%s %s",
356 uts.nodename, uts.sysname, uts.release);
359 #else /* !HAVE_UNAME */
361 text = strdup(getenv("SYS$NODE"));
363 text = strdup("X\nScreen\nSaver");
365 #endif /* !HAVE_UNAME */
369 (text[strlen(text)-1] == '\r' ||
370 text[strlen(text)-1] == '\n'))
371 text[strlen(text)-1] = 0;
373 text2 = strdup(text);
375 if (!fn) fn = def_fn;
376 font = XLoadQueryFont (dpy, fn);
379 fprintf(stderr, "%s: unable to load font %s; using %s\n",
380 progname, fn, def_fn);
381 font = XLoadQueryFont (dpy, def_fn);
384 memset(&overall, 0, sizeof(overall));
387 while ((line = strtok(token, "\r\n")))
390 int ascent, descent, direction;
392 XTextExtents(font, line, strlen(line),
393 &direction, &ascent, &descent, &o2);
394 overall.lbearing = MAX(overall.lbearing, o2.lbearing);
395 overall.rbearing = MAX(overall.rbearing, o2.rbearing);
399 width = overall.lbearing + overall.rbearing + margin + margin + 1;
400 height = ((font->ascent + font->descent) * lines) + margin + margin;
402 bitmap = XCreatePixmap(dpy, MI_WINDOW(mi), width, height, 1);
404 gcv.font = font->fid;
406 gc = XCreateGC (dpy, bitmap, (GCFont | GCForeground), &gcv);
407 XFillRectangle(dpy, bitmap, gc, 0, 0, width, height);
408 XSetForeground(dpy, gc, fg);
412 while ((line = strtok(token, "\r\n")))
415 int ascent, descent, direction, xoff;
418 XTextExtents(font, line, strlen(line),
419 &direction, &ascent, &descent, &o2);
420 xoff = ((overall.lbearing + overall.rbearing) -
421 (o2.lbearing + o2.rbearing)) / 2;
423 XDrawString(dpy, bitmap, gc,
424 overall.lbearing + margin + xoff,
425 ((font->ascent * (lines + 1)) +
426 (font->descent * lines) +
432 XUnloadFont(dpy, font->fid);
433 XFree((XPointer) font);
436 fp->image = XGetImage(dpy, bitmap, 0, 0, width, height, 1L, XYPixmap);
437 XFreePixmap(dpy, bitmap);
441 char *bits = (char *) malloc (sizeof(bob_bits));
442 memcpy (bits, bob_bits, sizeof(bob_bits));
443 fp->image = XCreateImage (dpy, MI_VISUAL(mi), 1, XYBitmap, 0,
444 bits, bob_width, bob_height,
446 fp->image->byte_order = LSBFirst;
447 fp->image->bitmap_bit_order = LSBFirst;
456 #else /* !STANDALONE */
459 make_flag_bits(ModeInfo *mi)
461 flagstruct *fp = &flags[MI_SCREEN(mi)];
467 XCreateImage(MI_DISPLAY(mi), MI_VISUAL(mi),
468 1, XYBitmap, 0, /* dpth, fmt, offset */
469 (char *) calloc ((w+8) / 8, h), /* data */
470 w, h, 8, 0); /* w, h, pad, bpl */
471 /* Geez, what kinda goofy bit order is this?? */
472 for (x = 0; x < w; x++)
473 for (y = h-1; y >= 0; y--)
474 XPutPixel (fp->image, x, y, flag_bits[i++]);
477 #endif /* !STANDALONE */
481 init_flag(ModeInfo * mi)
483 Display *display = MI_DISPLAY(mi);
484 int size = MI_SIZE(mi);
488 if ((flags = (flagstruct *) calloc(MI_NUM_SCREENS(mi),
489 sizeof (flagstruct))) == NULL)
492 fp = &flags[MI_SCREEN(mi)];
496 fp->width = MI_WIN_WIDTH(mi);
497 fp->height = MI_WIN_HEIGHT(mi);
499 fp->samp = MAXAMP; /* Amplitude */
500 fp->sofs = 20; /* ???????? */
501 fp->pointsize = size;
503 fp->pointsize = NRAND(-size - MINSIZE + 1) + MINSIZE;
504 if (fp->pointsize < MINSIZE ||
505 fp->width <= MAXW(fp) || fp->height <= MAXH(fp))
506 fp->pointsize = MINSIZE;
507 fp->size = MAXINITSIZE; /* Initial distance between pts */
508 fp->inctaille = 0.05;
510 fp->sidx = fp->x_flag = fp->y_flag = 0;
512 if (!fp->initialized) {
513 fp->initialized = True;
514 if (!(fp->cache = XCreatePixmap(display, MI_WINDOW(mi),
515 MAXW(fp), MAXH(fp), MI_WIN_DEPTH(mi))))
518 #else /* !STANDALONE */
519 error("%s: catastrophe memoire\n");
520 #endif /* !STANDALONE */
522 XSetForeground(display, MI_GC(mi), MI_WIN_BLACK_PIXEL(mi));
523 XFillRectangle(display, fp->cache, MI_GC(mi),
524 0, 0, MAXW(fp), MAXH(fp));
525 /* don't want any exposure events from XCopyArea */
526 XSetGraphicsExposures(display, MI_GC(mi), False);
527 if (MI_NPIXELS(mi) > 2)
528 fp->startcolor = NRAND(MI_NPIXELS(mi));
529 if (fp->width <= MAXW(fp) || fp->height <= MAXH(fp)) {
532 fp->x_flag = random_num(fp->width - MINW(fp));
533 fp->y_flag = random_num(fp->height - MINH(fp));
537 fp->x_flag = random_num(fp->width - MAXW(fp));
538 fp->y_flag = random_num(fp->height - MAXH(fp));
543 XClearWindow(display, MI_WINDOW(mi));
546 void release_flag(ModeInfo * mi);
550 draw_flag(ModeInfo * mi)
552 Display *display = MI_DISPLAY(mi);
553 Window window = MI_WINDOW(mi);
554 flagstruct *fp = &flags[MI_SCREEN(mi)];
556 if (fp->width <= MAXW(fp) || fp->height <= MAXH(fp)) {
557 fp->size = MININITSIZE;
558 /* fp->pointsize = MINPOINTSIZE; */
559 XCopyArea(display, fp->cache, window, MI_GC(mi),
560 0, 0, MINW(fp), MINH(fp), fp->x_flag, fp->y_flag);
562 if ((fp->size + fp->inctaille) > MAXSCALE)
563 fp->inctaille = -fp->inctaille;
564 if ((fp->size + fp->inctaille) < MINSCALE)
565 fp->inctaille = -fp->inctaille;
566 fp->size += fp->inctaille;
567 XCopyArea(display, fp->cache, window, MI_GC(mi),
568 0, 0, MAXW(fp), MAXH(fp), fp->x_flag, fp->y_flag);
570 XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_WIN_BLACK_PIXEL(mi));
571 XFillRectangle(display, fp->cache, MI_GC(mi),
572 0, 0, MAXW(fp), MAXH(fp));
576 fp->sidx %= (ANGLES * MI_NPIXELS(mi));
579 if ((MI_CYCLES(mi) > 0) && (fp->timer >= MI_CYCLES(mi)))
587 release_flag(ModeInfo * mi)
592 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++)
594 if (flags[screen].cache)
595 XFreePixmap(MI_DISPLAY(mi), flags[screen].cache);
596 if (flags[screen].image)
597 XDestroyImage(flags[screen].image);
599 (void) free((void *) flags);
605 refresh_flag(ModeInfo * mi)
607 /* Do nothing, it will refresh by itself */