http://www.tienza.es/crux/src/www.jwz.org/xscreensaver/xscreensaver-5.05.tar.gz
[xscreensaver] / hacks / flag.c
index c633cdcd9ea448681345451445468d9f2339b200..c0c33b271456c0870ab5d2296fbdb993ce9a1a71 100644 (file)
@@ -1,7 +1,7 @@
 /* -*- Mode: C; tab-width: 4 -*-
  * flag --- a waving flag
  */
-#if !defined( lint ) && !defined( SABER )
+#if 0
 static const char sccsid[] = "@(#)flag.c       4.02 97/04/01 xlockmore";
 #endif
 
@@ -32,37 +32,27 @@ static const char sccsid[] = "@(#)flag.c    4.02 97/04/01 xlockmore";
  * 01-May-96: written.
  */
 
+#ifdef HAVE_COCOA
+# define DEF_FONT "Monaco 15"
+#else
+# define DEF_FONT "fixed"
+#endif
+
 #ifdef STANDALONE
-# define PROGCLASS                                     "Flag"
-# define HACK_INIT                                     init_flag
-# define HACK_DRAW                                     draw_flag
-# define flag_opts                                     xlockmore_opts
-# define DEFAULTS      "*delay:                50000   \n"                     \
-                                       "*cycles:               1000    \n"                     \
-                                       "*size:                 -7      \n"                     \
-                                       "*ncolors:              200     \n"
+# define DEFAULTS      "*delay:                50000   \n"             \
+                                       "*cycles:               1000    \n"             \
+                                       "*size:                 -7      \n"             \
+                                       "*ncolors:              200     \n"             \
+                                       "*bitmap:                               \n"             \
+                                       "*font:         " DEF_FONT      "\n"    \
+                                       "*text:                                 \n"
 # define BRIGHT_COLORS
 # define UNIFORM_COLORS
-# define DEF_FONT                                      "-*-helvetica-bold-r-*-240-*"
-# define DEF_BITMAP                                    ""
-# define DEF_TEXT                                      ""
+# define reshape_flag 0
+# define flag_handle_event 0
 # include "xlockmore.h"                                /* from the xscreensaver distribution */
 
-# ifdef HAVE_XPM
-#  include <X11/xpm.h>
-#  ifndef PIXEL_ALREADY_TYPEDEFED
-#   define PIXEL_ALREADY_TYPEDEFED /* Sigh, Xmu/Drawing.h needs this... */
-#  endif
-# endif
-
-#ifdef HAVE_XMU
-# ifndef VMS
-#  include <X11/Xmu/Drawing.h>
-# else  /* VMS */
-#  include <Xmu/Drawing.h>
-# endif /* VMS */
-#endif /* HAVE_XMU */
-
+#include "xpm-pixmap.h"
 #include "images/bob.xbm"
 
 #else  /* !STANDALONE */
@@ -71,10 +61,6 @@ static const char sccsid[] = "@(#)flag.c     4.02 97/04/01 xlockmore";
 #endif /* !STANDALONE */
 
 
-#if defined(VMS) && !defined(HAVE_UNAME) && (__VMS_VER >= 70000000)
-# define HAVE_UNAME 1
-#endif
-
 #ifdef HAVE_UNAME
 # include <sys/utsname.h>
 #endif /* HAVE_UNAME */
@@ -88,7 +74,7 @@ static XrmOptionDescRec opts[] =
 
 #endif /* STANDALONE */
 
-ModeSpecOpt flag_opts = {
+ENTRYPOINT ModeSpecOpt flag_opts = {
 #ifdef STANDALONE
   2, opts, 0, NULL, NULL
 #else  /* !STANDALONE */
@@ -96,9 +82,6 @@ ModeSpecOpt flag_opts = {
 #endif /* STANDALONE */
 };
 
-#include <string.h>
-#include <X11/Xutil.h>
-
 #define MINSIZE 1
 #define MAXSCALE 8
 #define MINSCALE 2
@@ -120,6 +103,7 @@ typedef struct {
        int         timer;
        int         initialized;
        int         stab[ANGLES];
+    Bool               dbufp;
        Pixmap      cache;
        int         width, height;
        int         pointsize;
@@ -182,6 +166,11 @@ affiche(ModeInfo * mi)
                                XSetForeground(display, MI_GC(mi),
                                               MI_PIXEL(mi, (y + x + fp->sidx + fp->startcolor) % MI_NPIXELS(mi)));
 
+            if (fp->cache == MI_WINDOW(mi)) {  /* not double-buffering */
+              xp += fp->x_flag;
+              yp += fp->y_flag;
+            }
+
                        if (fp->pointsize <= 1)
                                XDrawPoint(display, fp->cache, MI_GC(mi), xp, yp);
                        else if (fp->pointsize < 6)
@@ -200,8 +189,12 @@ make_flag_bits(ModeInfo *mi)
 {
   Display *dpy = MI_DISPLAY(mi);
   flagstruct *fp = &flags[MI_SCREEN(mi)];
-  char *bitmap_name = get_string_resource ("bitmap", "Bitmap");
-  char *text = get_string_resource ("text", "Text");
+  char *bitmap_name = get_string_resource (dpy, "bitmap", "Bitmap");
+  char *text = get_string_resource (dpy, "text", "Text");
+
+#ifdef HAVE_COCOA
+  bitmap_name = 0;  /* #### always use default */
+#endif
 
   /* If neither a bitmap nor text are specified, randomly select either
         the builtin bitmap or builtin text. */
@@ -223,105 +216,23 @@ make_flag_bits(ModeInfo *mi)
          *bitmap_name &&
          !!strcmp(bitmap_name, "(default)"))
        {
-#ifdef HAVE_XPM
-         Window window = MI_WINDOW(mi);
-         XWindowAttributes xgwa;
-         XpmAttributes xpmattrs;
-         int result;
          Pixmap bitmap = 0;
-         int width = 0, height = 0;
-         xpmattrs.valuemask = 0;
-
-         XGetWindowAttributes (dpy, window, &xgwa);
-
-# ifdef XpmCloseness
-         xpmattrs.valuemask |= XpmCloseness;
-         xpmattrs.closeness = 40000;
-# endif
-# ifdef XpmVisual
-         xpmattrs.valuemask |= XpmVisual;
-         xpmattrs.visual = xgwa.visual;
-# endif
-# ifdef XpmDepth
-         xpmattrs.valuemask |= XpmDepth;
-         xpmattrs.depth = xgwa.depth;
-# endif
-# ifdef XpmColormap
-         xpmattrs.valuemask |= XpmColormap;
-         xpmattrs.colormap = xgwa.colormap;
-# endif
-
-         /* Uh, we don't need these now.  We use the colors from the xpm.
-                It kinda sucks that we already allocated them. */
-         XFreeColors(dpy, xgwa.colormap, mi->pixels, mi->npixels, 0L);
-
-         result = XpmReadFileToPixmap (dpy, window, bitmap_name, &bitmap, 0,
-                                                                       &xpmattrs);
-         switch (result)
-               {
-               case XpmColorError:
-                 fprintf (stderr, "%s: warning: xpm color substitution performed\n",
-                                  progname);
-                 /* fall through */
-               case XpmSuccess:
-                 width = xpmattrs.width;
-                 height = xpmattrs.height;
-                 break;
-               case XpmFileInvalid:
-               case XpmOpenFailed:
-                 bitmap = 0;
-                 break;
-               case XpmColorFailed:
-                 fprintf (stderr, "%s: xpm: color allocation failed\n", progname);
-                 exit (-1);
-               case XpmNoMemory:
-                 fprintf (stderr, "%s: xpm: out of memory\n", progname);
-                 exit (-1);
-               default:
-                 fprintf (stderr, "%s: xpm: unknown error code %d\n", progname,
-                                  result);
-                 exit (-1);
-               }
+      int width = 0;
+      int height = 0;
 
+      bitmap = xpm_file_to_pixmap (dpy, MI_WINDOW (mi), bitmap_name,
+                                   &width, &height, 0);
          if (bitmap)
                {
                  fp->image = XGetImage(dpy, bitmap, 0, 0, width, height, ~0L,
                                                                ZPixmap);
                  XFreePixmap(dpy, bitmap);
                }
-         else
-#endif /* HAVE_XPM */
-
-#ifdef HAVE_XMU
-               {
-                 int width, height, xh, yh;
-                 Pixmap bitmap =
-                       XmuLocateBitmapFile (DefaultScreenOfDisplay (dpy),
-                                                                bitmap_name, 0, 0, &width, &height, &xh, &yh);
-                 if (!bitmap)
-                       {
-                         fprintf(stderr, "%s: unable to load bitmap file %s\n",
-                                         progname, bitmap_name);
-                         exit (1);
-                       }
-                 fp->image = XGetImage(dpy, bitmap, 0, 0, width, height,
-                                                               1L, XYPixmap);
-                 XFreePixmap(dpy, bitmap);
-               }
-
-#else  /* !XMU */
-      fprintf (stderr,
-                          "%s: your vendor doesn't ship the standard Xmu library.\n",
-                          progname);
-      fprintf (stderr, "\tWe can't load XBM files without it.\n");
-      exit (1);
-#endif /* !XMU */
-
        }
   else if (text && *text)
        {
          char *text2;
-         char *fn = get_string_resource ("font", "Font");
+         char *fn = get_string_resource (dpy, "font", "Font");
          char *def_fn = "fixed";
          char *line, *token;
          int width, height;
@@ -350,9 +261,15 @@ make_flag_bits(ModeInfo *mi)
                                *s = 0;
                          text = (char *) malloc(strlen(uts.nodename) +
                                                                         strlen(uts.sysname) +
+                                                                        strlen(uts.version) +
                                                                         strlen(uts.release) + 10);
+# ifdef _AIX
+                         sprintf(text, "%s\n%s %s.%s",
+                                         uts.nodename, uts.sysname, uts.version, uts.release);
+# else  /* !_AIX */
                          sprintf(text, "%s\n%s %s",
                                          uts.nodename, uts.sysname, uts.release);
+# endif /* !_AIX */
                        }
 #else  /* !HAVE_UNAME */
 # ifdef VMS
@@ -475,7 +392,7 @@ make_flag_bits(ModeInfo *mi)
 #endif /* !STANDALONE */
 
 
-void
+ENTRYPOINT void
 init_flag(ModeInfo * mi)
 {
        Display    *display = MI_DISPLAY(mi);
@@ -490,6 +407,7 @@ init_flag(ModeInfo * mi)
        fp = &flags[MI_SCREEN(mi)];
 
        make_flag_bits(mi);
+    if (!fp->image) abort();
 
        fp->width = MI_WIN_WIDTH(mi);
        fp->height = MI_WIN_HEIGHT(mi);
@@ -508,9 +426,17 @@ init_flag(ModeInfo * mi)
        fp->sidx = fp->x_flag = fp->y_flag = 0;
 
        if (!fp->initialized) {
+      fp->dbufp = True;
+# ifdef HAVE_COCOA             /* Don't second-guess Quartz's double-buffering */
+      fp->dbufp = False;
+#endif
                fp->initialized = True;
-               if (!(fp->cache = XCreatePixmap(display, MI_WINDOW(mi),
-               MAXW(fp), MAXH(fp), MI_WIN_DEPTH(mi))))
+               if (!fp->dbufp)
+          fp->cache = MI_WINDOW(mi);  /* not double-buffering */
+        else
+          if (!(fp->cache = XCreatePixmap(display, MI_WINDOW(mi),
+                                          MAXW(fp), MAXH(fp),
+                                          MI_WIN_DEPTH(mi))))
 #ifdef STANDALONE
                  exit(-1);
 #else   /* !STANDALONE */
@@ -541,38 +467,39 @@ init_flag(ModeInfo * mi)
        XClearWindow(display, MI_WINDOW(mi));
 }
 
-void release_flag(ModeInfo * mi);
+ENTRYPOINT void release_flag(ModeInfo * mi);
 
 
-void
+ENTRYPOINT void
 draw_flag(ModeInfo * mi)
 {
        Display    *display = MI_DISPLAY(mi);
        Window      window = MI_WINDOW(mi);
        flagstruct *fp = &flags[MI_SCREEN(mi)];
 
-       if (fp->width <= MAXW(fp) || fp->height <= MAXH(fp)) {
+    if (!fp->image) abort();
+    if (fp->cache == window) {  /* not double-buffering */
+      XClearWindow (display, window);
+    } else if (fp->width <= MAXW(fp) || fp->height <= MAXH(fp)) {
                fp->size = MININITSIZE;
                /* fp->pointsize = MINPOINTSIZE; */
-               XCopyArea(display, fp->cache, window, MI_GC(mi),
-                         0, 0, MINW(fp), MINH(fp), fp->x_flag, fp->y_flag);
+        XCopyArea(display, fp->cache, window, MI_GC(mi),
+                  0, 0, MINW(fp), MINH(fp), fp->x_flag, fp->y_flag);
        } else {
                if ((fp->size + fp->inctaille) > MAXSCALE)
                        fp->inctaille = -fp->inctaille;
                if ((fp->size + fp->inctaille) < MINSCALE)
                        fp->inctaille = -fp->inctaille;
                fp->size += fp->inctaille;
-               XCopyArea(display, fp->cache, window, MI_GC(mi),
-                         0, 0, MAXW(fp), MAXH(fp), fp->x_flag, fp->y_flag);
+        XCopyArea(display, fp->cache, window, MI_GC(mi),
+                  0, 0, MAXW(fp), MAXH(fp), fp->x_flag, fp->y_flag);
        }
        XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_WIN_BLACK_PIXEL(mi));
        XFillRectangle(display, fp->cache, MI_GC(mi),
                       0, 0, MAXW(fp), MAXH(fp));
-       XFlush(display);
        affiche(mi);
        fp->sidx += 2;
        fp->sidx %= (ANGLES * MI_NPIXELS(mi));
-       XFlush(display);
        fp->timer++;
        if ((MI_CYCLES(mi) > 0) && (fp->timer >= MI_CYCLES(mi)))
       {
@@ -581,7 +508,7 @@ draw_flag(ModeInfo * mi)
       }
 }
 
-void
+ENTRYPOINT void
 release_flag(ModeInfo * mi)
 {
        if (flags != NULL) {
@@ -589,7 +516,7 @@ release_flag(ModeInfo * mi)
 
                for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++)
                  {
-                       if (flags[screen].cache)
+                       if (flags[screen].cache && flags[screen].dbufp)
                                XFreePixmap(MI_DISPLAY(mi), flags[screen].cache);
                        if (flags[screen].image)
                          XDestroyImage(flags[screen].image);
@@ -599,8 +526,10 @@ release_flag(ModeInfo * mi)
        }
 }
 
-void
+ENTRYPOINT void
 refresh_flag(ModeInfo * mi)
 {
        /* Do nothing, it will refresh by itself */
 }
+
+XSCREENSAVER_MODULE ("Flag", flag)