Misc 'catchup' commit on WolframAutomata.c.
[screensavers] / hacks / WolframAutomata / WolframAutomata.c
index 71e51bb..59562b3 100644 (file)
 /* TODO: I suppose a lot of this stuff goes in the README instead. */
 /* TODO: Explain the data structures in detail. */
 /* TODO: Explain all the options, like the various starting conditions. */
 /* TODO: I suppose a lot of this stuff goes in the README instead. */
 /* TODO: Explain the data structures in detail. */
 /* TODO: Explain all the options, like the various starting conditions. */
+/* TODO: Explain all the dependencies like libXpm. */
 
 
 
 
+/* TODO: Add a #define for the hack version. */
 /* TODO: Check manpage for all functions I use and ensure my includes are correct. I don't want to depend on picking up includes via screenhack.h. */
 /* TODO: Verify everything in this file is C89. Get rid of things like '//' comments, pack all my declarations upfront, no stdint, etc. */
 
 /* TODO: Check manpage for all functions I use and ensure my includes are correct. I don't want to depend on picking up includes via screenhack.h. */
 /* TODO: Verify everything in this file is C89. Get rid of things like '//' comments, pack all my declarations upfront, no stdint, etc. */
 
+#include <X11/Intrinsic.h>
 #include "screenhack.h"
 
 #include "screenhack.h"
 
+/*
+ * We do a few manual manipulations of X resources in this hack, like picking
+ * random colors. In order to ensure our manual manipulations always use the
+ * same X resource specification as Xscreensaver, we pass HACKNAME to
+ * Xscreensaver via the XSCREENSAVER_MODULE() line at the bottom of this file,
+ * and then always use HACKNAME or MAKE_STRING(HACKNAME) as the base of the
+ * resource specification when making manual manipulations.
+ */
+#define HACKNAME WolframAutomata
+#define MAKE_STRING_X(s) #s
+#define MAKE_STRING(s) MAKE_STRING_X(s)
+
 // Command line options
 //        directory to output XBM files of each run (and call an external command to convert to PNGs?)
 //              -save-dir STRING
 // Command line options
 //        directory to output XBM files of each run (and call an external command to convert to PNGs?)
 //              -save-dir STRING
+//              (could use libXpm to save an XPM and then convert to PNG with ImageMagick) (this is a single function call to go from pixmap -> file)
+//              (since it depends on an external library, make this whole feature optional at build-time?)
 //        number of generations to simulate
 //        number of generations to simulate
+//              -random-generations
 //              -num-generations N
 //        delay time (speed of simulation)
 //              -num-generations N
 //        delay time (speed of simulation)
+//              -random-delay
 //              -delay-usec N
 //        foreground and background color
 //              -delay-usec N
 //        foreground and background color
-//              ??? (strings of some sort, but I need to look up what X resources to interact with)
+//              -random-colors (highest precedence)
+//              -foreground "COLORNAME"
+//              -background "COLORNAME"
+//              (default is black and white)
+//              (mention sample color combinations in manpage, and link to: https://en.wikipedia.org/wiki/X11_color_names)
 //        display info overlay with CA number and start conditions?
 //              -overlay
 //        which ruleset number to use? Or random? Or random from small set of hand-selected interesting examples?
 //        display info overlay with CA number and start conditions?
 //              -overlay
 //        which ruleset number to use? Or random? Or random from small set of hand-selected interesting examples?
@@ -40,6 +63,7 @@
 //                  (the two options above only apply to the simulation under the -rule-random or -rule N options. in curated mode, starting population is defined in the curation array)
 //                  TODO: In the future, add the option for user to pass list of cell IDs to turn ON.
 //        size of pixel square (e.g. 1x1, 2x2, 3x3, etc)
 //                  (the two options above only apply to the simulation under the -rule-random or -rule N options. in curated mode, starting population is defined in the curation array)
 //                  TODO: In the future, add the option for user to pass list of cell IDs to turn ON.
 //        size of pixel square (e.g. 1x1, 2x2, 3x3, etc)
+//              -random-pixel-size
 //              -pixel-size N
 
 /* -------------------------------------------------------------------------- */
 //              -pixel-size N
 
 /* -------------------------------------------------------------------------- */
@@ -135,6 +159,18 @@ static const struct curated_ruleset curated_ruleset_list[] = {
     {240, random_cell}
 };
 
     {240, random_cell}
 };
 
+// TODO: Decorations
+struct color_pair {
+    char * fg;
+    char * bg;
+};
+
+// TODO: Decorations
+// TODO: Populate this table with more examples.
+static const struct color_pair color_list[] = {
+    {"white", "black"},
+};
+
 /* -------------------------------------------------------------------------- */
 /* Helper Functions                                                           */
 /* -------------------------------------------------------------------------- */
 /* -------------------------------------------------------------------------- */
 /* Helper Functions                                                           */
 /* -------------------------------------------------------------------------- */
@@ -202,7 +238,12 @@ render_current_generation(struct state * state)
 static void *
 WolframAutomata_init(Display * dpy, Window win)
 {
 static void *
 WolframAutomata_init(Display * dpy, Window win)
 {
-    struct state * state = calloc(1, sizeof(*state)); // TODO: Check calloc() call
+    struct state * state = calloc(1, sizeof(*state));
+    if (!state) {
+        fprintf(stderr, "ERROR: Failed to calloc() for state struct in WolframAutomata_init().\n");
+        exit(EXIT_FAILURE);
+    }
+
     XGCValues gcv;
     XWindowAttributes xgwa;
     const struct curated_ruleset * curated_ruleset = NULL;
     XGCValues gcv;
     XWindowAttributes xgwa;
     const struct curated_ruleset * curated_ruleset = NULL;
@@ -215,6 +256,13 @@ WolframAutomata_init(Display * dpy, Window win)
     state->ylim = xgwa.height;
     state->ypos = 0; // TODO: Explain why.
 
     state->ylim = xgwa.height;
     state->ypos = 0; // TODO: Explain why.
 
+    if (get_boolean_resource(state->dpy, "random-colors", "Boolean")) {
+        XrmDatabase db = XtDatabase(state->dpy);
+        size_t rand_i = random() % sizeof(color_list)/sizeof(color_list[0]);
+        XrmPutStringResource(&db, MAKE_STRING(HACKNAME) ".background", color_list[rand_i].bg);
+        XrmPutStringResource(&db, MAKE_STRING(HACKNAME) ".foreground", color_list[rand_i].fg);
+    }
+
     state->fg = gcv.foreground = get_pixel_resource(state->dpy, xgwa.colormap, "foreground", "Foreground");
     state->bg = gcv.background = get_pixel_resource(state->dpy, xgwa.colormap, "background", "Background");
     state->gc = XCreateGC(state->dpy, state->win, GCForeground, &gcv);
     state->fg = gcv.foreground = get_pixel_resource(state->dpy, xgwa.colormap, "foreground", "Foreground");
     state->bg = gcv.background = get_pixel_resource(state->dpy, xgwa.colormap, "background", "Background");
     state->gc = XCreateGC(state->dpy, state->win, GCForeground, &gcv);
@@ -266,7 +314,7 @@ WolframAutomata_init(Display * dpy, Window win)
     if (state->population_density < 0 || state->population_density > 100) state->population_density = 50;
     state->current_generation = calloc(1, sizeof(*state->current_generation)*state->number_of_cells);
     if (!state->current_generation) {
     if (state->population_density < 0 || state->population_density > 100) state->population_density = 50;
     state->current_generation = calloc(1, sizeof(*state->current_generation)*state->number_of_cells);
     if (!state->current_generation) {
-        fprintf(stderr, "ERROR: Failed to calloc() in WolframAutomata_init().\n");
+        fprintf(stderr, "ERROR: Failed to calloc() for cell generation in WolframAutomata_init().\n");
         exit(EXIT_FAILURE);
     }
     if (curated_ruleset) {
         exit(EXIT_FAILURE);
     }
     if (curated_ruleset) {
@@ -360,6 +408,7 @@ WolframAutomata_draw(Display * dpy, Window win, void * closure)
 static const char * WolframAutomata_defaults[] = {
     ".background:    black",
     ".foreground:    white",
 static const char * WolframAutomata_defaults[] = {
     ".background:    black",
     ".foreground:    white",
+    "*random-colors: False",
     "*delay-usec:    25000",
     // TODO: Difference between dot and asterisk? Presumably the asterisk matches all resouces of attribute "pixelsize"? Apply answer to all new options.
     "*pixel-size:    2",
     "*delay-usec:    25000",
     // TODO: Difference between dot and asterisk? Presumably the asterisk matches all resouces of attribute "pixelsize"? Apply answer to all new options.
     "*pixel-size:    2",
@@ -373,6 +422,9 @@ static const char * WolframAutomata_defaults[] = {
 
 // TODO: Fix formatting
 static XrmOptionDescRec WolframAutomata_options[] = {
 
 // TODO: Fix formatting
 static XrmOptionDescRec WolframAutomata_options[] = {
+    { "-background",        ".background",    XrmoptionSepArg, 0},
+    { "-foreground",        ".foreground",    XrmoptionSepArg, 0},
+    { "-random-colors",        ".random-colors",    XrmoptionNoArg, "True"},
     { "-delay-usec",        ".delay-usec",    XrmoptionSepArg, 0 },
     { "-pixel-size",    ".pixel-size",    XrmoptionSepArg, 0 },
     { "-num-generations",    ".num-generations",    XrmoptionSepArg, 0 },
     { "-delay-usec",        ".delay-usec",    XrmoptionSepArg, 0 },
     { "-pixel-size",    ".pixel-size",    XrmoptionSepArg, 0 },
     { "-num-generations",    ".num-generations",    XrmoptionSepArg, 0 },
@@ -406,5 +458,5 @@ WolframAutomata_reshape(Display * dpy, Window win, void * closure, unsigned int
     closure = WolframAutomata_init(dpy, win);
 }
 
     closure = WolframAutomata_init(dpy, win);
 }
 
-XSCREENSAVER_MODULE ("1D Nearest-Neighbor Cellular Automata", WolframAutomata)
+XSCREENSAVER_MODULE ("1D Nearest-Neighbor Cellular Automata", HACKNAME)