From http://www.jwz.org/xscreensaver/xscreensaver-5.36.tar.gz
[xscreensaver] / hacks / twang.c
index 7a606b129399a1adc38d4248cee83836cd4acee8..7d52a411156bda8047298f13ba6ea15d3acf6514 100644 (file)
@@ -62,7 +62,6 @@ struct state {
   Screen *screen;                 /* the screen to draw on */
   XImage *sourceImage;            /* image source of stuff to draw */
   XImage *workImage;              /* work area image, used when rendering */
-  XImage *backgroundImage;      /* image filled with background pixels */
 
   GC backgroundGC;              /* GC for the background color */
   GC foregroundGC;              /* GC for the foreground color */
@@ -79,6 +78,7 @@ struct state {
 
   time_t start_time;
   async_load_state *img_loader;
+  Pixmap pm;
 
   Bool useShm;         /* whether or not to use xshm */
 #ifdef HAVE_XSHM_EXTENSION
@@ -107,15 +107,16 @@ struct state {
 static void
 grabImage_start (struct state *st, XWindowAttributes *xwa)
 {
-    XFillRectangle (st->dpy, st->window, st->backgroundGC, 0, 0, 
-                   st->windowWidth, st->windowHeight);
-    st->backgroundImage = 
-       XGetImage (st->dpy, st->window, 0, 0, st->windowWidth, st->windowHeight,
-                  ~0L, ZPixmap);
-
-    st->start_time = time ((time_t) 0);
+    /* On MacOS X11, XGetImage on a Window often gets an inexplicable BadMatch,
+       possibly due to the window manager having occluded something?  It seems
+       nondeterministic. Loading the image into a pixmap instead fixes it. */
+    if (st->pm) XFreePixmap (st->dpy, st->pm);
+    st->pm = XCreatePixmap (st->dpy, st->window,
+                            xwa->width, xwa->height, xwa->depth);
+
+    st->start_time = time ((time_t *) 0);
     st->img_loader = load_image_async_simple (0, xwa->screen, st->window,
-                                              st->window, 0, 0);
+                                              st->pm, 0, 0);
 }
 
 static void
@@ -124,9 +125,10 @@ grabImage_done (struct state *st)
     XWindowAttributes xwa;
     XGetWindowAttributes (st->dpy, st->window, &xwa);
 
-    st->start_time = time ((time_t) 0);
+    st->start_time = time ((time_t *) 0);
     if (st->sourceImage) XDestroyImage (st->sourceImage);
-    st->sourceImage = XGetImage (st->dpy, st->window, 0, 0, st->windowWidth, st->windowHeight,
+    st->sourceImage = XGetImage (st->dpy, st->pm, 0, 0,
+                                 st->windowWidth, st->windowHeight,
                             ~0L, ZPixmap);
 
     if (st->workImage) XDestroyImage (st->workImage);
@@ -524,7 +526,8 @@ static void renderFrame (struct state *st)
 {
     int n;
 
-    memcpy (st->workImage->data, st->backgroundImage->data, 
+    /* This assumes black is zero. */
+    memset (st->workImage->data, 0, 
            st->workImage->bytes_per_line * st->workImage->height);
 
     sortTiles (st);
@@ -581,6 +584,7 @@ static void setupModel (struct state *st)
     leftX = (st->windowWidth - (st->columns * st->tileSize) + st->tileSize) / 2;
     topY = (st->windowHeight - (st->rows * st->tileSize) + st->tileSize) / 2;
 
+    if (st->tileCount < 1) st->tileCount = 1;
     st->tiles = calloc (st->tileCount, sizeof (Tile));
     st->sortedTiles = calloc (st->tileCount, sizeof (Tile *));
 
@@ -615,7 +619,7 @@ twang_draw (Display *dpy, Window window, void *closure)
     }
 
   if (!st->img_loader &&
-      st->start_time + st->duration < time ((time_t) 0)) {
+      st->start_time + st->duration < time ((time_t *) 0)) {
     XWindowAttributes xgwa;
     XGetWindowAttributes (st->dpy, st->window, &xgwa);
     grabImage_start (st, &xgwa);
@@ -638,6 +642,12 @@ twang_reshape (Display *dpy, Window window, void *closure,
 static Bool
 twang_event (Display *dpy, Window window, void *closure, XEvent *event)
 {
+  struct state *st = (struct state *) closure;
+  if (screenhack_event_helper (dpy, window, event))
+    {
+      st->start_time = 0;
+      return True;
+    }
   return False;
 }
 
@@ -645,6 +655,7 @@ static void
 twang_free (Display *dpy, Window window, void *closure)
 {
   struct state *st = (struct state *) closure;
+  if (st->pm) XFreePixmap (dpy, st->pm);
   free (st);
 }
 
@@ -766,6 +777,10 @@ static const char *twang_defaults [] = {
     "*useSHM: True",
 #else
     "*useSHM: False",
+#endif
+#ifdef HAVE_MOBILE
+  "*ignoreRotation: True",
+  "*rotateImages:   True",
 #endif
     0
 };