From http://www.jwz.org/xscreensaver/xscreensaver-5.35.tar.gz
[xscreensaver] / hacks / xlockmore.c
index 58d6acd9b3c2379ff338a431c8b5e743519e53e5..00994ac878d93763e22f2a28f69e00e473afda34 100644 (file)
@@ -1,5 +1,5 @@
 /* xlockmore.c --- xscreensaver compatibility layer for xlockmore modules.
- * xscreensaver, Copyright (c) 1997-2008 Jamie Zawinski <jwz@jwz.org>
+ * xscreensaver, Copyright (c) 1997-2014 Jamie Zawinski <jwz@jwz.org>
  *
  * Permission to use, copy, modify, distribute, and sell this software and its
  * documentation for any purpose is hereby granted without fee, provided that
@@ -19,9 +19,9 @@
 #include "xlockmoreI.h"
 #include "screenhack.h"
 
-#ifndef HAVE_COCOA
+#ifndef HAVE_JWXYZ
 # include <X11/Intrinsic.h>
-#endif /* !HAVE_COCOA */
+#endif /* !HAVE_JWXYZ */
 
 #define countof(x) (sizeof((x))/sizeof(*(x)))
 
@@ -97,8 +97,7 @@ xlockmore_setup (struct xscreensaver_function_table *xsft, void *arg)
   /* Add extra args, if they're mentioned in the defaults... */
   {
     char *args[] = { "-count", "-cycles", "-delay", "-ncolors",
-                    "-size", "-font", "-wireframe", "-use3d", "-useSHM",
-                     "-showFPS" };
+                    "-size", "-font", "-wireframe", "-use3d", "-useSHM" };
     for (j = 0; j < countof(args); j++)
       if (strstr(xlockmore_defaults, args[j]+1))
        {
@@ -138,17 +137,6 @@ xlockmore_setup (struct xscreensaver_function_table *xsft, void *arg)
              new->argKind = XrmoptionNoArg;
              new->value = "False";
            }
-         else if (!strcmp(new->option, "-showFPS"))
-           {
-             new->option = "-fps";
-             new->argKind = XrmoptionNoArg;
-             new->value = "True";
-             new = &new_options[i++];
-             new->option = "-no-fps";
-             new->specifier = new_options[i-2].specifier;
-             new->argKind = XrmoptionNoArg;
-             new->value = "False";
-           }
          else
            {
              new->argKind = XrmoptionSepArg;
@@ -164,12 +152,12 @@ xlockmore_setup (struct xscreensaver_function_table *xsft, void *arg)
    */
   i = 0;
 
-  new_defaults = (char **) calloc (1, xlockmore_opts->numvarsdesc * 10 + 100);
+  new_defaults = (char **) calloc (1, xlockmore_opts->numvarsdesc * 10 + 1000);
 
   /* Put on the PROGCLASS.background/foreground resources. */
   s = (char *) malloc(50);
   *s = 0;
-# ifndef HAVE_COCOA
+# ifndef HAVE_JWXYZ
   strcpy (s, progclass);
 # endif
   strcat (s, ".background: black");
@@ -177,7 +165,7 @@ xlockmore_setup (struct xscreensaver_function_table *xsft, void *arg)
 
   s = (char *) malloc(50);
   *s = 0;
-# ifndef HAVE_COCOA
+# ifndef HAVE_JWXYZ
   strcpy (s, progclass);
 # endif
   strcat (s, ".foreground: white");
@@ -294,7 +282,6 @@ xlockmore_init (Display *dpy, Window window,
   XGCValues gcv;
   XColor color;
   int i;
-  int orig_pause;
   Bool root_p;
 
   if (! xlmft)
@@ -305,29 +292,31 @@ xlockmore_init (Display *dpy, Window window,
   mi->window = window;
   XGetWindowAttributes (dpy, window, &mi->xgwa);
   
-#ifdef HAVE_COCOA
+#ifdef HAVE_JWXYZ
   
-  /* In Cocoa-based xscreensaver, all hacks run in the same address space,
-     so each one needs to get its own screen number.  We just use a global
-     counter for that, instead of actually trying to figure out which
-     monitor each window is on.  Also, the embedded "preview" view counts
-     as a screen as well.
-    
-     Note that the screen number will increase each time the saver is
-     restarted (e.g., each time preferences are changed!)  So we just
-     keep pushing the num_screens number up as needed, and assume that
-     no more than 10 simultanious copies will be running at once...
+# if 0
+  /* In Cocoa and Android-based xscreensaver, all hacks run in the
+     same address space, so each one needs to get its own screen
+     number.  Believe what jwxyz says about screen counts and numbers.
    */
-  {
-    static int screen_tick = 0;
-    mi->num_screens = 10;
-    mi->screen_number = screen_tick++;
-    if (screen_tick >= mi->num_screens)
-      mi->num_screens += 10;
-  }
-  
+  mi->num_screens = ScreenCount (dpy);
+  mi->screen_number = XScreenNumberOfScreen (mi->xgwa.screen);
+# else
+  /* No, that doesn't work.
+     The xlockmore docs/HACKERS.GUIDE says that xlock modes are supposed to
+     support repeated calls to init_*() for the same screen in the same
+     session, but in practice, a number of them blow up if you do that.
+     So instead we're stuck with a world where on OSX/iOS, we have to
+     increment the screen number every time the hack is run. Arrgh.
+   */
+  mi->num_screens = 40;
+  mi->screen_number = xlmft->screen_count;
+  if (mi->screen_number >= mi->num_screens) abort();
+  xlmft->screen_count++;
+# endif
   root_p = True;
-#else /* !HAVE_COCOA -- real Xlib */
+
+#else /* real Xlib */
   
   /* In Xlib-based xscreensaver, each hack runs in its own address space,
      so each one only needs to be aware of one screen.
@@ -348,7 +337,7 @@ xlockmore_init (Display *dpy, Window window,
   /* Everybody gets motion events, just in case. */
   XSelectInput (dpy, window, (mi->xgwa.your_event_mask | PointerMotionMask));
 
-#endif /* !HAVE_COCOA */
+#endif /* !HAVE_JWXYZ */
   
   color.flags = DoRed|DoGreen|DoBlue;
   color.red = color.green = color.blue = 0;
@@ -362,7 +351,6 @@ xlockmore_init (Display *dpy, Window window,
 
   if (mono_p)
     {
-      static unsigned long pixels[2];
       static XColor colors[2];
     MONO:
       mi->npixels = 2;
@@ -372,8 +360,6 @@ xlockmore_init (Display *dpy, Window window,
       if (!mi->colors)
         mi->colors = (XColor *) 
           calloc (mi->npixels, sizeof (*mi->colors));
-      pixels[0] = mi->black;
-      pixels[1] = mi->white;
       colors[0].flags = DoRed|DoGreen|DoBlue;
       colors[1].flags = DoRed|DoGreen|DoBlue;
       colors[0].red = colors[0].green = colors[0].blue = 0;
@@ -395,18 +381,21 @@ xlockmore_init (Display *dpy, Window window,
       switch (mi->xlmft->desired_color_scheme)
         {
         case color_scheme_uniform:
-          make_uniform_colormap (dpy, mi->xgwa.visual, mi->xgwa.colormap,
+          make_uniform_colormap (mi->xgwa.screen, mi->xgwa.visual,
+                                 mi->xgwa.colormap,
                                  mi->colors, &mi->npixels,
                                  True, &mi->writable_p, True);
           break;
         case color_scheme_smooth:
-          make_smooth_colormap (dpy, mi->xgwa.visual, mi->xgwa.colormap,
+          make_smooth_colormap (mi->xgwa.screen, mi->xgwa.visual,
+                                mi->xgwa.colormap,
                                 mi->colors, &mi->npixels,
                                 True, &mi->writable_p, True);
           break;
         case color_scheme_bright:
         case color_scheme_default:
-          make_random_colormap (dpy, mi->xgwa.visual, mi->xgwa.colormap,
+          make_random_colormap (mi->xgwa.screen, mi->xgwa.visual,
+                                mi->xgwa.colormap,
                                 mi->colors, &mi->npixels,
                                 (mi->xlmft->desired_color_scheme ==
                                  color_scheme_bright),
@@ -452,16 +441,16 @@ xlockmore_init (Display *dpy, Window window,
 
   mi->wireframe_p = get_boolean_resource (dpy, "wireframe", "Boolean");
   mi->root_p = root_p;
-  mi->fps_p = get_boolean_resource (dpy, "showFPS", "Boolean");
 #ifdef HAVE_XSHM_EXTENSION
   mi->use_shm = get_boolean_resource (dpy, "useSHM", "Boolean");
 #endif /* !HAVE_XSHM_EXTENSION */
+  mi->fps_p = get_boolean_resource (dpy, "doFPS", "DoFPS");
+  mi->recursion_depth = -1;  /* see fps.c */
 
   if (mi->pause < 0)
     mi->pause = 0;
   else if (mi->pause > 100000000)
     mi->pause = 100000000;
-  orig_pause = mi->pause;
 
   /* If this hack uses fonts (meaning, mentioned "font" in DEFAULTS)
      then load it. */
@@ -470,7 +459,7 @@ xlockmore_init (Display *dpy, Window window,
     if (name)
       {
         XFontStruct *f = XLoadQueryFont (dpy, name);
-        const char *def1 = "-*-times-bold-r-normal-*-180-*";
+        const char *def1 = "-*-helvetica-bold-r-normal-*-180-*";
         const char *def2 = "fixed";
         if (!f)
           {
@@ -532,30 +521,32 @@ xlockmore_event (Display *dpy, Window window, void *closure, XEvent *event)
 {
   ModeInfo *mi = (ModeInfo *) closure;
   if (mi && mi->xlmft->hack_handle_events)
-    {
-      mi->xlmft->hack_handle_events (mi, event);
-      /* Since xlockmore hacks don't tell us whether they handled the
-         event, assume they handled buttons (so we don't beep) but that
-         they didn't handle anything else (so that "q" still quits).
-       */
-      return (event->xany.type == ButtonPress);
-    }
+    return mi->xlmft->hack_handle_events (mi, event);
   else
     return False;
 }
 
+void
+xlockmore_do_fps (Display *dpy, Window w, fps_state *fpst, void *closure)
+{
+  ModeInfo *mi = (ModeInfo *) closure;
+  fps_compute (fpst, 0, mi ? mi->recursion_depth : -1);
+  fps_draw (fpst);
+}
+
+
 static void
 xlockmore_free (Display *dpy, Window window, void *closure)
 {
+  /* Most of the xlockmore/GL hacks don't have `free' functions, and of
+     those that do have them, they're incomplete or buggy.  So, fuck it.
+     Under X11, we're about to exit anyway, and it doesn't matter.
+     On OSX, we'll leak a little.  Beats crashing.
+   */
+#if 0
   ModeInfo *mi = (ModeInfo *) closure;
   if (mi->xlmft->hack_free)
     mi->xlmft->hack_free (mi);
-#ifdef USE_GL
-/* ####
-  if (mi->fps_state)
-    fps_free (mi);
- */
-#endif /* USE_GL */
 
   XFreeGC (dpy, mi->gc);
   free_colors (dpy, mi->xgwa.colormap, mi->colors, mi->npixels);
@@ -563,5 +554,6 @@ xlockmore_free (Display *dpy, Window window, void *closure)
   free (mi->pixels);
 
   free (mi);
+#endif
 }