Added ability to set fg/bg color in WolframAutomata.
authorAaron Taylor <ataylor@subgeniuskitty.com>
Mon, 15 Mar 2021 15:33:11 +0000 (08:33 -0700)
committerAaron Taylor <ataylor@subgeniuskitty.com>
Mon, 15 Mar 2021 15:33:11 +0000 (08:33 -0700)
hacks/WolframAutomata/WolframAutomata.c

index 71e51bb..faab86b 100644 (file)
 /* 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
 //        delay time (speed of simulation)
 //              -delay-usec N
 //        foreground and background color
 //        delay time (speed of simulation)
 //              -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?
@@ -135,6 +152,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                                                           */
 /* -------------------------------------------------------------------------- */
@@ -215,6 +244,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);
@@ -360,6 +396,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 +410,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 +446,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)