From http://www.jwz.org/xscreensaver/xscreensaver-5.37.tar.gz
[xscreensaver] / hacks / rd-bomb.c
index dfcc2897cab5cacf51cb1c4c0d2c1cec183b4e31..33cb602a1658a0d189d3a1382f8c5b38158d0699 100644 (file)
 #include <math.h>
 
 #include "screenhack.h"
-
-#ifdef HAVE_XSHM_EXTENSION
-# include "xshm.h"
-#endif /* HAVE_XSHM_EXTENSION */
+#include "xshm.h"
 
 /* costs ~6% speed */
 #define dither_when_mapped 1
@@ -52,10 +49,7 @@ struct state {
   char *pd;
   int array_width, array_height;
 
-#ifdef HAVE_XSHM_EXTENSION
-  Bool use_shm;
   XShmSegmentInfo shm_info;
-#endif
 
   GC gc;
   XImage *image;
@@ -95,10 +89,10 @@ pixack_init(struct state *st, int *size_h, int *size_v)
   st->height = get_integer_resource (st->dpy, "height", "Integer");
 
   if (st->width <= 0 && st->height <= 0 && (R & 1))
-    st->width = st->height = 48 + BELLRAND(256);
+    st->width = st->height = 64 + BELLRAND(512);
 
-  if (st->width  <= 0) st->width  = 48 + BELLRAND(256);
-  if (st->height <= 0) st->height = 48 + BELLRAND(256);
+  if (st->width  <= 0) st->width  = 64 + BELLRAND(512);
+  if (st->height <= 0) st->height = 64 + BELLRAND(512);
 
   if (st->width  > st->xgwa.width)  st->width  = st->xgwa.width;
   if (st->height > st->xgwa.height) st->height = st->xgwa.height;
@@ -145,12 +139,6 @@ pixack_frame(struct state *st, char *pix_buf)
 
   if (!(st->frame%st->epoch_time)) {
     int s;
-    if (0 != st->frame) {
-      int tt = st->epoch_time / 500;
-      if (tt > 15)
-       tt = 15;
-      /*sleep(tt);*/
-    }
          
     for (i = 0; i < st->npix; i++) {
       /* equilibrium */
@@ -236,7 +224,9 @@ pixack_frame(struct state *st, char *pix_buf)
       /* John E. Pearson "Complex Patterns in a Simple System"
         Science, July 1993 */
 
-      uvv = (((r1 * r2) >> bps) * r2) >> bps;
+      /* uvv = (((r1 * r2) >> bps) * r2) >> bps; */
+      /* avoid signed integer overflow */
+      uvv = ((((r1 >> 1)* r2) >> bps) * r2) >> (bps - 1);
       switch (st->reaction) {  /* costs 4% */
       case 0:
        r1 += 4 * (((28 * (mx-r1)) >> 10) - uvv);
@@ -312,7 +302,7 @@ static const char *rd_defaults [] = {
 #else
   "*useSHM:    False",
 #endif
-#ifdef USE_IPHONE
+#ifdef HAVE_MOBILE
   "*ignoreRotation: True",
 #endif
   0
@@ -376,7 +366,6 @@ rd_init (Display *dpy, Window win)
 {
   struct state *st = (struct state *) calloc (1, sizeof(*st));
   XGCValues gcv;
-  int w2;
   int vdepth;
 
   st->dpy = dpy;
@@ -384,10 +373,6 @@ rd_init (Display *dpy, Window win)
 
   st->delay = get_integer_resource (st->dpy, "delay", "Float");
 
-#ifdef HAVE_XSHM_EXTENSION
-  st->use_shm = get_boolean_resource(st->dpy, "useSHM", "Boolean");
-#endif
-
   XGetWindowAttributes (st->dpy, win, &st->xgwa);
   st->visual = st->xgwa.visual;
   pixack_init(st, &st->width, &st->height);
@@ -416,7 +401,6 @@ rd_init (Display *dpy, Window win)
 
   }
   st->npix = (st->width + 2) * (st->height + 2);
-  w2 = st->width + 2;
 /*  gcv.function = GXcopy;*/
   st->gc = XCreateGC(st->dpy, win, 0 /*GCFunction*/, &gcv);
   vdepth = visual_depth(DefaultScreenOfDisplay(st->dpy), st->xgwa.visual);
@@ -491,35 +475,9 @@ rd_init (Display *dpy, Window win)
     }
   }
 
-  st->pd = malloc(st->npix * (st->pdepth == 1 ? 1 : (st->pdepth / 8)));
-  if (!st->pd) {
-    fprintf(stderr, "not enough memory for %d pixels.\n", st->npix);
-    exit(1);
-  }
-
-  st->image = 0;
-
-#ifdef HAVE_XSHM_EXTENSION
-  if (st->use_shm)
-    {
-      st->image = create_xshm_image(st->dpy, st->xgwa.visual, vdepth,
-                               ZPixmap, 0, &st->shm_info, st->width, st->height);
-      if (!st->image)
-       st->use_shm = False;
-      else
-       {
-         free(st->pd);
-         st->pd = st->image->data;
-       }
-    }
-#endif /* HAVE_XSHM_EXTENSION */
-
-  if (!st->image)
-    {
-      st->image = XCreateImage(st->dpy, st->xgwa.visual, vdepth,
-                          ZPixmap, 0, st->pd,
-                          st->width, st->height, 8, 0);
-    }
+  st->image = create_xshm_image(st->dpy, st->xgwa.visual, vdepth,
+                                ZPixmap, &st->shm_info, st->width, st->height);
+  st->pd = st->image->data;
 
   return st;
 }
@@ -534,14 +492,8 @@ rd_draw (Display *dpy, Window win, void *closure)
   pixack_frame(st, st->pd);
   for (i = 0; i < st->array_width; i += st->width)
     for (j = 0; j < st->array_height; j += st->height)
-#ifdef HAVE_XSHM_EXTENSION
-      if (st->use_shm)
-        XShmPutImage(st->dpy, st->window, st->gc, st->image, 0, 0, i+st->array_x, j+st->array_y,
-                     st->width, st->height, False);
-      else
-#endif
-        XPutImage(st->dpy, win, st->gc, st->image, 0, 0, i+st->array_x, j+st->array_y,
-                  st->width, st->height);
+      put_xshm_image(st->dpy, st->window, st->gc, st->image, 0, 0, i+st->array_x, j+st->array_y,
+                     st->width, st->height, &st->shm_info);
 
   st->array_x += st->array_dx;
   st->array_y += st->array_dy;