From http://www.jwz.org/xscreensaver/xscreensaver-5.38.tar.gz
[xscreensaver] / hacks / flag.c
1 /* -*- Mode: C; tab-width: 4 -*-
2  * flag --- a waving flag
3  */
4 #if 0
5 static const char sccsid[] = "@(#)flag.c        4.02 97/04/01 xlockmore";
6 #endif
7
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
13  *
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.
19  *
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.
25  *
26  * Revision History: 
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.
32  * 01-May-96: written.
33  */
34
35 #ifdef HAVE_COCOA
36 # define DEF_FONT "Monaco 15"
37 #else
38 # define DEF_FONT "fixed"
39 #endif
40
41 #ifdef STANDALONE
42 # define DEFAULTS       "*delay:                50000   \n"             \
43                                         "*cycles:               1000    \n"             \
44                                         "*size:                 -7      \n"             \
45                                         "*ncolors:              200     \n"             \
46                                         "*bitmap:                               \n"             \
47                                         "*font:         " DEF_FONT      "\n"    \
48                                         "*text:                                 \n" \
49                                         "*fpsSolid:             true    \n" \
50                                     "*lowrez:       true    \n" \
51
52 # define BRIGHT_COLORS
53 # define UNIFORM_COLORS
54 # define release_flag 0
55 # define reshape_flag 0
56 # define flag_handle_event 0
57 # include "xlockmore.h"                         /* from the xscreensaver distribution */
58
59 #include "xpm-pixmap.h"
60 #include "images/bob.xbm"
61
62 #else  /* !STANDALONE */
63 # include "xlock.h"                                     /* from the xlockmore distribution */
64 # include "flag.h"
65 #endif /* !STANDALONE */
66
67
68 #ifdef HAVE_UNAME
69 # include <sys/utsname.h>
70 #endif /* HAVE_UNAME */
71
72 #ifdef STANDALONE
73 static XrmOptionDescRec opts[] =
74 {
75   { "-bitmap", ".flag.bitmap", XrmoptionSepArg, 0 },
76   { "-text",   ".flag.text",   XrmoptionSepArg, 0 }
77 };
78
79 #endif /* STANDALONE */
80
81 ENTRYPOINT ModeSpecOpt flag_opts = {
82 #ifdef STANDALONE
83   2, opts, 0, NULL, NULL
84 #else  /* !STANDALONE */
85   0, NULL, 0, NULL, NULL
86 #endif /* STANDALONE */
87 };
88
89 #define MINSIZE 1
90 #define MAXSCALE 8
91 #define MINSCALE 2
92 #define MAXINITSIZE 6
93 #define MININITSIZE 2
94 #define MINAMP 5
95 #define MAXAMP 20
96 #define MAXW(fp) (MAXSCALE * (fp)->image->width + 2 * MAXAMP + (fp)->pointsize)
97 #define MAXH(fp) (MAXSCALE * (fp)->image->height+ 2 * MAXAMP + (fp)->pointsize)
98 #define MINW(fp) (MINSCALE * (fp)->image->width + 2 * MINAMP + (fp)->pointsize)
99 #define MINH(fp) (MINSCALE * (fp)->image->height+ 2 * MINAMP + (fp)->pointsize)
100 #define ANGLES          360
101
102 typedef struct {
103         int         samp;
104         int         sofs;
105         int         sidx;
106         int         x_flag, y_flag;
107         int         timer;
108         int         initialized;
109         int         stab[ANGLES];
110     Bool                dbufp;
111         Pixmap      cache;
112         int         width, height;
113         int         pointsize;
114         float      size;
115         float      inctaille;
116         int         startcolor;
117     XImage     *image;
118 } flagstruct;
119
120 static flagstruct *flags = NULL;
121
122 static int
123 random_num(int n)
124 {
125         return ((int) (((float) LRAND() / MAXRAND) * (n + 1.0)));
126 }
127
128 static void
129 initSintab(ModeInfo * mi)
130 {
131         flagstruct *fp = &flags[MI_SCREEN(mi)];
132         int         i;
133
134   /*-
135    * change the periodicity of the sin formula : the maximum of the
136    * periocity seem to be 16 ( 2^4 ), after the drawing isn't good looking
137    */
138         int         periodicity = random_num(4);
139         int         puissance = 1;
140
141         /* for (i=0;i<periodicity;i++) puissance*=2; */
142         puissance <<= periodicity;
143         for (i = 0; i < ANGLES; i++)
144                 fp->stab[i] = (int) (SINF(i * puissance * M_PI / ANGLES) * fp->samp) +
145                         fp->sofs;
146 }
147
148 static void
149 affiche(ModeInfo * mi)
150 {
151         Display    *display = MI_DISPLAY(mi);
152         int         x, y, xp, yp;
153         flagstruct *fp = &flags[MI_SCREEN(mi)];
154
155         for (x = 0; x < fp->image->width; x++)
156                 for (y = fp->image->height-1; y >= 0; y--) {
157                         xp = (int) (fp->size * (float) x) +
158                                 fp->stab[(fp->sidx + x + y) % ANGLES];
159                         yp = (int) (fp->size * (float) y) +
160                                 fp->stab[(fp->sidx + 4 * x + y + y) % ANGLES];
161
162                         if (fp->image->depth > 1)
163                           XSetForeground(display, MI_GC(mi),
164                                                          XGetPixel(fp->image, x, y));
165                         else if (XGetPixel(fp->image, x, y))
166                                 XSetForeground(display, MI_GC(mi), MI_WIN_BLACK_PIXEL(mi));
167                         else if (MI_NPIXELS(mi) <= 2)
168                                 XSetForeground(display, MI_GC(mi), MI_WIN_WHITE_PIXEL(mi));
169                         else
170                                 XSetForeground(display, MI_GC(mi),
171                                                MI_PIXEL(mi, (y + x + fp->sidx + fp->startcolor) % MI_NPIXELS(mi)));
172
173             if (fp->cache == MI_WINDOW(mi)) {  /* not double-buffering */
174               xp += fp->x_flag;
175               yp += fp->y_flag;
176             }
177
178                         if (fp->pointsize <= 1)
179                                 XDrawPoint(display, fp->cache, MI_GC(mi), xp, yp);
180                         else if (fp->pointsize < 6)
181                                 XFillRectangle(display, fp->cache, MI_GC(mi), xp, yp,
182                                                            fp->pointsize, fp->pointsize);
183                         else
184                                 XFillArc(display, fp->cache, MI_GC(mi), xp, yp,
185                                                  fp->pointsize, fp->pointsize, 0, 360*64);
186                 }
187 }
188
189 #ifdef STANDALONE
190
191 static void
192 make_flag_bits(ModeInfo *mi)
193 {
194   Display *dpy = MI_DISPLAY(mi);
195   flagstruct *fp = &flags[MI_SCREEN(mi)];
196   char *bitmap_name = get_string_resource (dpy, "bitmap", "Bitmap");
197   char *text = get_string_resource (dpy, "text", "Text");
198
199 #ifdef HAVE_JWXYZ
200   bitmap_name = 0;  /* #### always use default */
201 #endif
202
203   /* If neither a bitmap nor text are specified, randomly select either
204          the builtin bitmap or builtin text. */
205   if ((!bitmap_name || !*bitmap_name) && (!text || !*text))
206         {
207           if (random() & 1)
208                 {
209                   free(bitmap_name);
210                   bitmap_name = strdup("(default)");
211                 }
212           else
213                 {
214                   free(text);
215                   text = strdup("(default)");
216                 }
217         }
218
219   if (bitmap_name &&
220           *bitmap_name &&
221           !!strcmp(bitmap_name, "(default)"))
222         {
223           Pixmap bitmap = 0;
224       int width = 0;
225       int height = 0;
226
227       bitmap = xpm_file_to_pixmap (dpy, MI_WINDOW (mi), bitmap_name,
228                                    &width, &height, 0);
229           if (bitmap)
230                 {
231                   fp->image = XGetImage(dpy, bitmap, 0, 0, width, height, ~0L,
232                                                                 ZPixmap);
233                   XFreePixmap(dpy, bitmap);
234                 }
235         }
236   else if (text && *text)
237         {
238           char *text2;
239           char *fn = get_string_resource (dpy, "font", "Font");
240           char *def_fn = "fixed";
241           char *line, *token;
242           int width, height;
243           int lines;
244           int margin = 2;
245           int fg = 1;
246           int bg = 0;
247           Pixmap bitmap;
248           XFontStruct *font;
249           XCharStruct overall;
250       XGCValues gcv;
251           GC gc;
252
253           if (!strcmp(text, "(default)"))
254                 {
255 # ifdef HAVE_UNAME
256                   struct utsname uts;
257                   if (uname (&uts) < 0)
258                         {
259                           text = strdup("uname() failed");
260                         }
261                   else
262                         {
263                           char *s;
264                           if ((s = strchr(uts.nodename, '.')))
265                                 *s = 0;
266                           text = (char *) malloc(strlen(uts.nodename) +
267                                                                          strlen(uts.sysname) +
268                                                                          strlen(uts.version) +
269                                                                          strlen(uts.release) + 10);
270 # if defined(_AIX)
271                           sprintf(text, "%s\n%s %s.%s",
272                                           uts.nodename, uts.sysname, uts.version, uts.release);
273 #  elif defined(__APPLE__) && !defined(USE_IPHONE)  /* MacOS X + XDarwin */
274               {
275                 const char *file = 
276                   "/System/Library/CoreServices/SystemVersion.plist";
277                 FILE *f = fopen (file, "r");
278                 char *pbv = 0, *pn = 0, *puvv = 0;
279                 if (f) {
280                   char *s, buf[255];
281
282                   while (fgets (buf, sizeof(buf)-1, f)) {
283 #                   define GRAB(S,V)                                    \
284                     if (strstr(buf, S)) {                                       \
285                       fgets (buf, sizeof(buf)-1, f);                    \
286                       if ((s = strchr (buf, '>'))) V = strdup(s+1);     \
287                       if ((s = strchr (V, '<'))) *s = 0;                        \
288                     }
289                     GRAB ("ProductName", pn)
290                     GRAB ("ProductBuildVersion", pbv)
291                     GRAB ("ProductUserVisibleVersion", puvv)
292 #                   undef GRAB
293                   }
294                 }
295                 if (pbv)
296                   sprintf (text, "%s\n%s\n%s", 
297                            uts.nodename, pn, puvv /*, uts.machine*/);
298                 else
299                   sprintf(text, "%s\n%s %s",
300                           uts.nodename, uts.sysname, uts.release);
301               }
302 # else
303                           sprintf(text, "%s\n%s %s",
304                                           uts.nodename, uts.sysname, uts.release);
305 # endif /* special system types */
306                         }
307 #else   /* !HAVE_UNAME */
308 # ifdef VMS
309                   text = strdup(getenv("SYS$NODE"));
310 # else
311                   text = strdup("X\nScreen\nSaver");
312 # endif
313 #endif  /* !HAVE_UNAME */
314                 }
315
316           while (*text &&
317                          (text[strlen(text)-1] == '\r' ||
318                           text[strlen(text)-1] == '\n'))
319                 text[strlen(text)-1] = 0;
320
321           text2 = strdup(text);
322
323           if (!fn) fn = def_fn;
324       font = XLoadQueryFont (dpy, fn);
325       if (! font)
326                 {
327                   fprintf(stderr, "%s: unable to load font %s; using %s\n",
328                                   progname, fn, def_fn);
329                   font = XLoadQueryFont (dpy, def_fn);
330                 }
331
332           memset(&overall, 0, sizeof(overall));
333           token = text;
334           lines = 0;
335           while ((line = strtok(token, "\r\n")))
336                 {
337                   XCharStruct o2;
338                   int ascent, descent, direction;
339                   token = 0;
340                   XTextExtents(font, line, strlen(line),
341                                            &direction, &ascent, &descent, &o2);
342                   overall.lbearing = MAX(overall.lbearing, o2.lbearing);
343                   overall.rbearing = MAX(overall.rbearing, o2.rbearing);
344                   lines++;
345                 }
346
347           width = overall.lbearing + overall.rbearing + margin + margin + 1;
348           height = ((font->ascent + font->descent) * lines) + margin + margin;
349
350           bitmap = XCreatePixmap(dpy, MI_WINDOW(mi), width, height, 1);
351
352       gcv.font = font->fid;
353       gcv.foreground = bg;
354       gc = XCreateGC (dpy, bitmap, (GCFont | GCForeground), &gcv);
355           XFillRectangle(dpy, bitmap, gc, 0, 0, width, height);
356           XSetForeground(dpy, gc, fg);
357
358           token = text2;
359           lines = 0;
360           while ((line = strtok(token, "\r\n")))
361                 {
362                   XCharStruct o2;
363                   int ascent, descent, direction, xoff;
364                   token = 0;
365
366                   XTextExtents(font, line, strlen(line),
367                                            &direction, &ascent, &descent, &o2);
368                   xoff = ((overall.lbearing + overall.rbearing) -
369                                   (o2.lbearing + o2.rbearing)) / 2;
370
371                   XDrawString(dpy, bitmap, gc,
372                                           overall.lbearing + margin + xoff,
373                                           ((font->ascent * (lines + 1)) +
374                                            (font->descent * lines) +
375                                            margin),
376                                           line, strlen(line));
377                   lines++;
378                 }
379           free(text2);
380           XUnloadFont(dpy, font->fid);
381           XFree((XPointer) font);
382           XFreeGC(dpy, gc);
383
384           fp->image = XGetImage(dpy, bitmap, 0, 0, width, height, 1L, XYPixmap);
385           XFreePixmap(dpy, bitmap);
386         }
387
388
389   if (! fp->image)
390         {
391       char *bits = (char *) malloc (sizeof(bob_bits));
392       memcpy (bits, bob_bits, sizeof(bob_bits));
393           fp->image = XCreateImage (dpy, MI_VISUAL(mi), 1, XYBitmap, 0,
394                                                                 bits, bob_width, bob_height,
395                                                                 8, 0);
396           fp->image->byte_order = LSBFirst;
397           fp->image->bitmap_bit_order = LSBFirst;
398         }
399
400   if (bitmap_name)
401         free (bitmap_name);
402   if (text)
403         free (text);
404 }
405
406 #else  /* !STANDALONE */
407
408 static void
409 make_flag_bits(ModeInfo *mi)
410 {
411   flagstruct *fp = &flags[MI_SCREEN(mi)];
412   int x, y;
413   int w = flag_width;
414   int h = flag_height;
415   int i = 0;
416   fp->image =
417         XCreateImage(MI_DISPLAY(mi), MI_VISUAL(mi),
418                                  1, XYBitmap, 0,                                        /* dpth, fmt, offset */
419                                  (char *) calloc ((w+8) / 8, h),        /* data */
420                                  w, h, 8, 0);                                           /* w, h, pad, bpl */
421   /* Geez, what kinda goofy bit order is this?? */
422   for (x = 0; x < w; x++)
423         for (y = h-1; y >= 0; y--)
424           XPutPixel (fp->image, x, y, flag_bits[i++]);
425 }
426
427 #endif /* !STANDALONE */
428
429
430 ENTRYPOINT void
431 init_flag(ModeInfo * mi)
432 {
433         Display    *display = MI_DISPLAY(mi);
434         int         size = MI_SIZE(mi);
435         flagstruct *fp;
436
437         MI_INIT (mi, flags);
438         fp = &flags[MI_SCREEN(mi)];
439
440         make_flag_bits(mi);
441     if (!fp->image) abort();
442
443         fp->width = MI_WIN_WIDTH(mi);
444         fp->height = MI_WIN_HEIGHT(mi);
445
446         fp->samp = MAXAMP;      /* Amplitude */
447         fp->sofs = 20;          /* ???????? */
448         fp->pointsize = size;
449         if (size < -MINSIZE)
450                 fp->pointsize = NRAND(-size - MINSIZE + 1) + MINSIZE;
451         if (fp->pointsize < MINSIZE ||
452         fp->width <= MAXW(fp) || fp->height <= MAXH(fp))
453                 fp->pointsize = MINSIZE;
454         fp->size = MAXINITSIZE; /* Initial distance between pts */
455         fp->inctaille = 0.05;
456         fp->timer = 0;
457         fp->sidx = fp->x_flag = fp->y_flag = 0;
458
459         if (!fp->initialized) {
460       fp->dbufp = True;
461 # ifdef HAVE_JWXYZ              /* Don't second-guess Quartz's double-buffering */
462       fp->dbufp = False;
463 #endif
464                 fp->initialized = True;
465                 if (!fp->dbufp)
466           fp->cache = MI_WINDOW(mi);  /* not double-buffering */
467         else
468           if (!(fp->cache = XCreatePixmap(display, MI_WINDOW(mi),
469                                           MAXW(fp), MAXH(fp),
470                                           MI_WIN_DEPTH(mi))))
471 #ifdef STANDALONE
472                   exit(-1);
473 #else   /* !STANDALONE */
474                         error("%s: catastrophe memoire\n");
475 #endif /* !STANDALONE */
476         }
477         XSetForeground(display, MI_GC(mi), MI_WIN_BLACK_PIXEL(mi));
478         XFillRectangle(display, fp->cache, MI_GC(mi),
479                        0, 0, MAXW(fp), MAXH(fp));
480         /* don't want any exposure events from XCopyArea */
481         XSetGraphicsExposures(display, MI_GC(mi), False);
482         if (MI_NPIXELS(mi) > 2)
483                 fp->startcolor = NRAND(MI_NPIXELS(mi));
484         if (fp->width <= MAXW(fp) || fp->height <= MAXH(fp)) {
485                 fp->samp = MINAMP;
486                 fp->sofs = 0;
487                 fp->x_flag = random_num(fp->width - MINW(fp));
488                 fp->y_flag = random_num(fp->height - MINH(fp));
489         } else {
490                 fp->samp = MAXAMP;
491                 fp->sofs = 20;
492                 fp->x_flag = random_num(fp->width - MAXW(fp));
493                 fp->y_flag = random_num(fp->height - MAXH(fp));
494         }
495
496         initSintab(mi);
497
498         XClearWindow(display, MI_WINDOW(mi));
499 }
500
501 ENTRYPOINT void
502 draw_flag(ModeInfo * mi)
503 {
504         Display    *display = MI_DISPLAY(mi);
505         Window      window = MI_WINDOW(mi);
506         flagstruct *fp = &flags[MI_SCREEN(mi)];
507
508     if (!fp->image) abort();
509     if (fp->cache == window) {  /* not double-buffering */
510       XClearWindow (display, window);
511     } else if (fp->width <= MAXW(fp) || fp->height <= MAXH(fp)) {
512                 fp->size = MININITSIZE;
513                 /* fp->pointsize = MINPOINTSIZE; */
514         XCopyArea(display, fp->cache, window, MI_GC(mi),
515                   0, 0, MINW(fp), MINH(fp), fp->x_flag, fp->y_flag);
516         } else {
517                 if ((fp->size + fp->inctaille) > MAXSCALE)
518                         fp->inctaille = -fp->inctaille;
519                 if ((fp->size + fp->inctaille) < MINSCALE)
520                         fp->inctaille = -fp->inctaille;
521                 fp->size += fp->inctaille;
522         XCopyArea(display, fp->cache, window, MI_GC(mi),
523                   0, 0, MAXW(fp), MAXH(fp), fp->x_flag, fp->y_flag);
524         }
525         XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_WIN_BLACK_PIXEL(mi));
526         XFillRectangle(display, fp->cache, MI_GC(mi),
527                        0, 0, MAXW(fp), MAXH(fp));
528         affiche(mi);
529         fp->sidx += 2;
530         fp->sidx %= (ANGLES * MI_NPIXELS(mi));
531         fp->timer++;
532         if ((MI_CYCLES(mi) > 0) && (fp->timer >= MI_CYCLES(mi)))
533                 init_flag(mi);
534 }
535
536 ENTRYPOINT void
537 free_flag(ModeInfo * mi)
538 {
539         int         screen = MI_SCREEN(mi);
540
541         if (flags == NULL)
542                 return;
543
544         if (flags[screen].cache && flags[screen].dbufp)
545                 XFreePixmap(MI_DISPLAY(mi), flags[screen].cache);
546         if (flags[screen].image)
547           XDestroyImage(flags[screen].image);
548 }
549
550 #ifndef STANDALONE
551 ENTRYPOINT void
552 refresh_flag(ModeInfo * mi)
553 {
554         /* Do nothing, it will refresh by itself */
555 }
556 #endif
557
558 XSCREENSAVER_MODULE ("Flag", flag)