From d0f3b852e00df82f39ff80eaf5ede2bb82450487 Mon Sep 17 00:00:00 2001 From: Aaron Taylor Date: Mon, 15 Mar 2021 08:33:11 -0700 Subject: [PATCH] Added ability to set fg/bg color in WolframAutomata. --- hacks/WolframAutomata/WolframAutomata.c | 44 +++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/hacks/WolframAutomata/WolframAutomata.c b/hacks/WolframAutomata/WolframAutomata.c index 71e51bb..faab86b 100644 --- a/hacks/WolframAutomata/WolframAutomata.c +++ b/hacks/WolframAutomata/WolframAutomata.c @@ -15,8 +15,21 @@ /* 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 #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 @@ -25,7 +38,11 @@ // 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? @@ -135,6 +152,18 @@ static const struct curated_ruleset curated_ruleset_list[] = { {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 */ /* -------------------------------------------------------------------------- */ @@ -215,6 +244,13 @@ WolframAutomata_init(Display * dpy, Window win) 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); @@ -360,6 +396,7 @@ WolframAutomata_draw(Display * dpy, Window win, void * closure) 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", @@ -373,6 +410,9 @@ static const char * WolframAutomata_defaults[] = { // 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 }, @@ -406,5 +446,5 @@ WolframAutomata_reshape(Display * dpy, Window win, void * closure, unsigned int closure = WolframAutomata_init(dpy, win); } -XSCREENSAVER_MODULE ("1D Nearest-Neighbor Cellular Automata", WolframAutomata) +XSCREENSAVER_MODULE ("1D Nearest-Neighbor Cellular Automata", HACKNAME) -- 2.20.1