X-Git-Url: http://git.hungrycats.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=hacks%2Fgoop.c;h=62b704ecce82af34fa4348a94d3c4494ebff9eba;hb=39809ded547bdbb08207d3e514950425215b4410;hp=ace23527eaab766296f7f5c1acd74f0a46ed31d3;hpb=df7adbee81405e2849728a24b498ad2117784b1f;p=xscreensaver diff --git a/hacks/goop.c b/hacks/goop.c index ace23527..62b704ec 100644 --- a/hacks/goop.c +++ b/hacks/goop.c @@ -1,4 +1,4 @@ -/* xscreensaver, Copyright (c) 1997 Jamie Zawinski +/* xscreensaver, Copyright (c) 1997-2008 Jamie Zawinski * * Permission to use, copy, modify, distribute, and sell this software and its * documentation for any purpose is hereby granted without fee, provided that @@ -44,11 +44,9 @@ blobs could have thickness, and curved edges with specular reflections... */ - #define SCALE 10000 /* fixed-point math, for sub-pixel motion */ #define DEF_COUNT 12 /* When planes and count are 0, how many blobs. */ - #define RAND(n) ((long) ((random() & 0x7fffffff) % ((long) (n)))) #define RANDSIGN() ((random() & 1) ? 1 : -1) @@ -91,11 +89,12 @@ struct goop { GC window_gc; Bool additive_p; Bool cmap_p; + int delay; }; static struct blob * -make_blob (int maxx, int maxy, int size) +make_blob (Display *dpy, int maxx, int maxy, int size) { struct blob *b = (struct blob *) calloc(1, sizeof(*b)); int i; @@ -111,9 +110,9 @@ make_blob (int maxx, int maxy, int size) if (b->min_r < (5*SCALE)) b->min_r = (5*SCALE); mid = ((b->min_r + b->max_r) / 2); - b->torque = get_float_resource ("torque", "Torque"); - b->elasticity = SCALE * get_float_resource ("elasticity", "Elasticity"); - b->max_velocity = SCALE * get_float_resource ("maxVelocity", "MaxVelocity"); + b->torque = get_float_resource (dpy, "torque", "Torque"); + b->elasticity = SCALE * get_float_resource (dpy, "elasticity", "Elasticity"); + b->max_velocity = SCALE * get_float_resource (dpy, "maxVelocity", "MaxVelocity"); b->x = RAND(maxx); b->y = RAND(maxy); @@ -126,10 +125,18 @@ make_blob (int maxx, int maxy, int size) b->spline = make_spline (b->npoints); b->r = (long *) malloc (sizeof(*b->r) * b->npoints); for (i = 0; i < b->npoints; i++) - b->r[i] = ((random() % mid) + (mid/2)) * RANDSIGN(); + b->r[i] = (long) ((random() % mid) + (mid/2)) * RANDSIGN(); return b; } +static void +free_blob(struct blob *blob) +{ + free_spline(blob->spline); + free(blob->r); + free(blob); +} + static void throb_blob (struct blob *b) { @@ -257,17 +264,41 @@ make_layer (Display *dpy, Window window, int width, int height, int nblobs) layer->blobs = (struct blob **) malloc(sizeof(*layer->blobs)*layer->nblobs); blob_max = (width < height ? width : height) / 2; + + if (width < 100 || height < 100) /* tiny window */ + blob_max *= 10; + blob_min = (blob_max * 2) / 3; - for (i = 0; i < layer->nblobs; i++) - layer->blobs[i] = make_blob (width, height, - (random() % (blob_max-blob_min)) + blob_min); + for (i = 0; i < layer->nblobs; i++){ + int j = blob_max - blob_min; + layer->blobs[i] = make_blob (dpy, width, height, + (j ? random() % j : 0) + blob_min); + } layer->pixmap = XCreatePixmap (dpy, window, width, height, 1); layer->gc = XCreateGC (dpy, layer->pixmap, 0, &gcv); +# ifdef HAVE_JWXYZ + jwxyz_XSetAlphaAllowed (dpy, layer->gc, True); +# endif /* HAVE_JWXYZ */ + return layer; } +static void +free_layer(struct layer *layer, Display *dpy) +{ + int i; + for (i = 0; i < layer->nblobs; i++){ + free_blob(layer->blobs[i]); + } + free(layer->blobs); + XFreeGC(dpy, layer->gc); + free(layer); +} + + +#ifndef HAVE_JWXYZ static void draw_layer_plane (Display *dpy, struct layer *layer, int width, int height) { @@ -279,6 +310,7 @@ draw_layer_plane (Display *dpy, struct layer *layer, int width, int height) draw_blob (dpy, layer->pixmap, layer->gc, layer->blobs[i], True); } } +#endif /* !HAVE_JWXYZ */ static void @@ -304,32 +336,45 @@ make_goop (Screen *screen, Visual *visual, Window window, Colormap cmap, int i; struct goop *goop = (struct goop *) calloc(1, sizeof(*goop)); XGCValues gcv; - int nblobs = get_integer_resource ("count", "Count"); + int nblobs = get_integer_resource (dpy, "count", "Count"); unsigned long *plane_masks = 0; +# ifndef HAVE_JWXYZ unsigned long base_pixel = 0; +# endif + char *s; + + s = get_string_resource (dpy, "mode", "Mode"); + goop->mode = transparent; + if (!s || !*s || !strcasecmp (s, "transparent")) + ; + else if (!strcasecmp (s, "opaque")) + goop->mode = opaque; + else if (!strcasecmp (s, "xor")) + goop->mode = xor; + else + fprintf (stderr, "%s: bogus mode: \"%s\"\n", progname, s); + free(s); - goop->mode = (get_boolean_resource("xor", "Xor") - ? xor - : (get_boolean_resource("transparent", "Transparent") - ? transparent - : opaque)); + goop->delay = get_integer_resource (dpy, "delay", "Integer"); goop->width = width; goop->height = height; - - goop->nlayers = get_integer_resource ("planes", "Planes"); + goop->nlayers = get_integer_resource (dpy, "planes", "Planes"); if (goop->nlayers <= 0) goop->nlayers = (random() % (depth-2)) + 2; - goop->layers = (struct layer **) malloc(sizeof(*goop->layers)*goop->nlayers); + if (! goop->layers) + goop->layers = (struct layer **) + malloc(sizeof(*goop->layers)*goop->nlayers); - goop->additive_p = get_boolean_resource ("additive", "Additive"); + goop->additive_p = get_boolean_resource (dpy, "additive", "Additive"); goop->cmap_p = has_writable_cells (screen, visual); if (mono_p && goop->mode == transparent) goop->mode = opaque; +# ifndef HAVE_JWXYZ /* Try to allocate some color planes before committing to nlayers. */ if (goop->mode == transparent) @@ -348,6 +393,7 @@ make_goop (Screen *screen, Visual *visual, Window window, Colormap cmap, goop->mode = opaque; } } +# endif /* !HAVE_JWXYZ */ { int lblobs[32]; @@ -362,22 +408,27 @@ make_goop (Screen *screen, Visual *visual, Window window, Colormap cmap, (nblobs > 0 ? nblobs : lblobs[i])); } +# ifndef HAVE_JWXYZ if (goop->mode == transparent && plane_masks) { for (i = 0; i < goop->nlayers; i++) goop->layers[i]->pixel = base_pixel | plane_masks[i]; goop->background = base_pixel; } +# endif /* !HAVE_JWXYZ */ + if (plane_masks) free (plane_masks); +# ifndef HAVE_JWXYZ if (goop->mode != transparent) +# endif /* !HAVE_JWXYZ */ { XColor color; color.flags = DoRed|DoGreen|DoBlue; goop->background = - get_pixel_resource ("background", "Background", dpy,cmap); + get_pixel_resource (dpy,cmap, "background", "Background"); for (i = 0; i < goop->nlayers; i++) { @@ -390,6 +441,17 @@ make_goop (Screen *screen, Visual *visual, Window window, Colormap cmap, else goop->layers[i]->pixel = WhitePixelOfScreen(DefaultScreenOfDisplay(dpy)); +# ifdef HAVE_JWXYZ + if (goop->mode == transparent) + { + /* give a non-opaque alpha to the color */ + unsigned long pixel = goop->layers[i]->pixel; + unsigned long amask = BlackPixelOfScreen (screen); + unsigned long a = (0xBBBBBBBB & amask); + pixel = (pixel & (~amask)) | a; + goop->layers[i]->pixel = pixel; + } +# endif /* HAVE_JWXYZ */ } } @@ -397,31 +459,51 @@ make_goop (Screen *screen, Visual *visual, Window window, Colormap cmap, (goop->mode == xor ? 1L : depth)); gcv.background = goop->background; - gcv.foreground = get_pixel_resource ("foreground", "Foreground", dpy, cmap); - gcv.line_width = get_integer_resource ("thickness","Thickness"); + gcv.foreground = get_pixel_resource (dpy, cmap, "foreground", "Foreground"); + gcv.line_width = get_integer_resource (dpy, "thickness","Thickness"); goop->pixmap_gc = XCreateGC (dpy, goop->pixmap, GCLineWidth, &gcv); goop->window_gc = XCreateGC (dpy, window, GCForeground|GCBackground, &gcv); +# ifdef HAVE_JWXYZ + jwxyz_XSetAlphaAllowed (dpy, goop->pixmap_gc, True); +# endif /* HAVE_JWXYZ */ + return goop; } -static struct goop * -init_goop (Display *dpy, Window window) +/* Well, the naming of this function is + confusing with goop_free()... */ +static void +free_goop (struct goop *goop, Display *dpy) +{ + int i; + for (i = 0; i < goop->nlayers; i++){ + struct layer * layer = goop->layers[i]; + free_layer(layer, dpy); + } + free(goop->layers); + XFreeGC(dpy, goop->pixmap_gc); + XFreeGC(dpy, goop->window_gc); +} + +static void * +goop_init (Display *dpy, Window window) { XWindowAttributes xgwa; XGetWindowAttributes (dpy, window, &xgwa); - return make_goop (xgwa.screen, xgwa.visual, window, xgwa.colormap, xgwa.width, xgwa.height, xgwa.depth); } -static void -run_goop (Display *dpy, Window window, struct goop *goop) +static unsigned long +goop_draw (Display *dpy, Window window, void *closure) { - int i, j; + struct goop *goop = (struct goop *) closure; + int i; switch (goop->mode) { +# ifndef HAVE_JWXYZ case transparent: for (i = 0; i < goop->nlayers; i++) @@ -437,6 +519,7 @@ run_goop (Display *dpy, Window window, struct goop *goop) if (!goop->cmap_p && !goop->additive_p) { + int j; for (i = 0; i < goop->nlayers; i++) for (j = 0; j < goop->layers[i]->nblobs; j++) draw_blob (dpy, goop->pixmap, goop->pixmap_gc, @@ -454,6 +537,7 @@ run_goop (Display *dpy, Window window, struct goop *goop) XCopyArea (dpy, goop->pixmap, window, goop->window_gc, 0, 0, goop->width, goop->height, 0, 0); break; +#endif /* !HAVE_JWXYZ */ case xor: XSetFunction (dpy, goop->pixmap_gc, GXcopy); @@ -470,6 +554,9 @@ run_goop (Display *dpy, Window window, struct goop *goop) goop->width, goop->height, 0, 0, 1L); break; +# ifdef HAVE_JWXYZ + case transparent: +# endif case opaque: case outline: XSetForeground (dpy, goop->pixmap_gc, goop->background); @@ -490,37 +577,69 @@ run_goop (Display *dpy, Window window, struct goop *goop) abort (); break; } + return goop->delay; +} + +static void +goop_reshape (Display *dpy, Window window, void *closure, + unsigned int w, unsigned int h) +{ + struct goop *goop = (struct goop *) closure; + + if (w != goop->width || h != goop->height) + { + struct goop *goop2 = goop_init (dpy, window); + free_goop(goop, dpy); + memcpy (goop, goop2, sizeof(*goop)); + free(goop2); + } } +static Bool +goop_event (Display *dpy, Window window, void *closure, XEvent *event) +{ + return False; +} + +static void +goop_free (Display *dpy, Window window, void *closure) +{ + struct goop *goop = (struct goop *) closure; + free_goop(goop, dpy); + free(goop); +} + + -char *progclass = "Goop"; -char *defaults [] = { +static const char *goop_defaults [] = { ".background: black", - ".foreground: white", + ".foreground: yellow", "*delay: 12000", - "*transparent: true", "*additive: true", - "*xor: false", - "*count: 0", - "*planes: 0", + "*mode: transparent", + "*count: 1", + "*planes: 12", "*thickness: 5", "*torque: 0.0075", - "*elasticity: 1.8", - "*maxVelocity: 1.2", + "*elasticity: 0.9", + "*maxVelocity: 0.5", +#ifdef HAVE_MOBILE + "*ignoreRotation: True", +#endif 0 }; -XrmOptionDescRec options [] = { +static XrmOptionDescRec goop_options [] = { { "-delay", ".delay", XrmoptionSepArg, 0 }, { "-count", ".count", XrmoptionSepArg, 0 }, { "-planes", ".planes", XrmoptionSepArg, 0 }, - { "-transparent", ".transparent", XrmoptionNoArg, "True" }, - { "-non-transparent", ".transparent", XrmoptionNoArg, "False" }, + { "-mode", ".mode", XrmoptionSepArg, 0 }, + { "-xor", ".mode", XrmoptionNoArg, "xor" }, + { "-transparent", ".mode", XrmoptionNoArg, "transparent" }, + { "-opaque", ".mode", XrmoptionNoArg, "opaque" }, { "-additive", ".additive", XrmoptionNoArg, "True" }, { "-subtractive", ".additive", XrmoptionNoArg, "false" }, - { "-xor", ".xor", XrmoptionNoArg, "true" }, - { "-no-xor", ".xor", XrmoptionNoArg, "false" }, { "-thickness", ".thickness", XrmoptionSepArg, 0 }, { "-torque", ".torque", XrmoptionSepArg, 0 }, { "-elasticity", ".elasticity", XrmoptionSepArg, 0 }, @@ -528,16 +647,4 @@ XrmOptionDescRec options [] = { { 0, 0, 0, 0 } }; -void -screenhack (Display *dpy, Window window) -{ - struct goop *g = init_goop (dpy, window); - int delay = get_integer_resource ("delay", "Integer"); - while (1) - { - run_goop (dpy, window, g); - XSync (dpy, False); - screenhack_handle_events (dpy); - if (delay) usleep (delay); - } -} +XSCREENSAVER_MODULE ("Goop", goop)