Updated WolframAutomata's rule related CLI flags to match README.
[screensavers] / hacks / WolframAutomata / WolframAutomata.c
index 5e0b725..01246f2 100644 (file)
@@ -46,13 +46,13 @@ struct state {
     uint8_t rule_number;
 
     /* At the end of the simulation, the user is given time to admire the     */
     uint8_t rule_number;
 
     /* At the end of the simulation, the user is given time to admire the     */
-    /* output. Delay is available to user as CLI option.                      */
+    /* output. Delay is available to user as CLI option '-admiration-delay'.  */
     Bool admiration_in_progress;
     Bool admiration_in_progress;
-    size_t admiration_delay; /* ...in microseconds.                           */
+    size_t admiration_delay; /* ...in seconds.                                */
 
     /* The following values correspond directly to independent CLI options.   */
 
     /* The following values correspond directly to independent CLI options.   */
-    Bool    rule_random;
-    uint8_t rule_requested;  /* Note: Repurposing Rule 0 as null value.       */
+    Bool    random_rule;
+    int     requested_rule;
     int     cell_size; /* If cell_size=N then draw NxN pixels per cell.       */
     int     delay_microsec; /* ...between calls to WolframAutomata_draw().    */
     int     num_generations; /* Reset simulation after this many generations. */
     int     cell_size; /* If cell_size=N then draw NxN pixels per cell.       */
     int     delay_microsec; /* ...between calls to WolframAutomata_draw().    */
     int     num_generations; /* Reset simulation after this many generations. */
@@ -276,41 +276,45 @@ WolframAutomata_init(Display * dpy, Window win)
     state->dpy_height = xgwa.height;
     state->ypos = 0;
 
     state->dpy_height = xgwa.height;
     state->ypos = 0;
 
-    state->admiration_delay = 5000000;
+    state->admiration_delay = get_integer_resource(state->dpy, "admiration-delay", "Integer");
     state->admiration_in_progress = False;
 
     state->admiration_in_progress = False;
 
-    if (get_boolean_resource(state->dpy, "random-colors", "Boolean")) {
-        XColor fg, bg;
-        size_t rand_i = random() % sizeof(color_list)/sizeof(color_list[0]);
-        fg.red   = color_list[rand_i].fg_red;
-        fg.green = color_list[rand_i].fg_green;
-        fg.blue  = color_list[rand_i].fg_blue;
-        bg.red   = color_list[rand_i].bg_red;
-        bg.green = color_list[rand_i].bg_green;
-        bg.blue  = color_list[rand_i].bg_blue;
-        /* TODO: Since I 'alloc', presumably I must also 'free' these colors  */
-        /* at some point. Where/how? I don't want to eventually crash my      */
-        /* X server after months of use.                                      */
-        XAllocColor(state->dpy, xgwa.colormap, &fg);
-        XAllocColor(state->dpy, xgwa.colormap, &bg);
-        state->fg = gcv.foreground = fg.pixel;
-        state->bg = gcv.background = bg.pixel;
-    } else {
-        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");
+    /* Set foreground and background colors for active/inactive cells. Either */
+    /* the user provided an index into the pre-defined color_list[] or a      */
+    /* random entry from that same array should be selected.                  */
+    size_t color_index = get_integer_resource(state->dpy, "color-index", "Integer");
+    if (color_index == -1) {
+        color_index = random() % sizeof(color_list)/sizeof(color_list[0]);
+    } else if (color_index >= sizeof(color_list)/sizeof(color_list[0])) {
+        fprintf(stderr, "WARNING: Color index out of range.\n");
+        color_index = 0;
     }
     }
+    XColor fg, bg;
+    fg.red   = color_list[color_index].fg_red;
+    fg.green = color_list[color_index].fg_green;
+    fg.blue  = color_list[color_index].fg_blue;
+    bg.red   = color_list[color_index].bg_red;
+    bg.green = color_list[color_index].bg_green;
+    bg.blue  = color_list[color_index].bg_blue;
+    /* TODO: Since I 'alloc', presumably I must also 'free' these colors  */
+    /* at some point. Where/how? I don't want to eventually crash my      */
+    /* X server after months of use.                                      */
+    XAllocColor(state->dpy, xgwa.colormap, &fg);
+    XAllocColor(state->dpy, xgwa.colormap, &bg);
+    state->fg = gcv.foreground = fg.pixel;
+    state->bg = gcv.background = bg.pixel;
 
     state->gc = XCreateGC(state->dpy, state->win, GCForeground, &gcv);
 
     /* Set the size of each simulated cell to NxN pixels for cell_size=N.     */
 
     state->gc = XCreateGC(state->dpy, state->win, GCForeground, &gcv);
 
     /* Set the size of each simulated cell to NxN pixels for cell_size=N.     */
-    if (get_boolean_resource(state->dpy, "random-pixel-size", "Boolean")) {
+    if (get_boolean_resource(state->dpy, "random-cell-size", "Boolean")) {
         /* Although we are choosing the pixel size 'randomly', a truly random */
         /* selection would bias toward large numbers since there are more of  */
         /* them. To avoid this, we select a random number for a bit shift,    */
         /* resulting in a pixel size of 1, 2, 4, 8, 16 or 32, equally likely. */
         state->cell_size = 1 << (random() % 6);
     } else {
         /* Although we are choosing the pixel size 'randomly', a truly random */
         /* selection would bias toward large numbers since there are more of  */
         /* them. To avoid this, we select a random number for a bit shift,    */
         /* resulting in a pixel size of 1, 2, 4, 8, 16 or 32, equally likely. */
         state->cell_size = 1 << (random() % 6);
     } else {
-        state->cell_size = get_integer_resource(state->dpy, "pixel-size", "Integer");
+        state->cell_size = get_integer_resource(state->dpy, "cell-size", "Integer");
     }
     if (state->cell_size < 1) state->cell_size = 1;
     if (state->cell_size > state->dpy_width) state->cell_size = state->dpy_width;
     }
     if (state->cell_size < 1) state->cell_size = 1;
     if (state->cell_size > state->dpy_width) state->cell_size = state->dpy_width;
@@ -349,13 +353,13 @@ WolframAutomata_init(Display * dpy, Window win)
         /* the exponent in '2^11'.                                            */
         state->delay_microsec = 1 << ((random() % 3) + 11 + pixel_shift_range);
     } else {
         /* the exponent in '2^11'.                                            */
         state->delay_microsec = 1 << ((random() % 3) + 11 + pixel_shift_range);
     } else {
-        state->delay_microsec = get_integer_resource(state->dpy, "delay-usec", "Integer");
+        state->delay_microsec = get_integer_resource(state->dpy, "delay", "Integer");
     }
     if (state->delay_microsec < 0) state->delay_microsec = 0;
 
     /* Set the number of generations to simulate before wiping the simulation */
     /* and re-running with new settings.                                      */
     }
     if (state->delay_microsec < 0) state->delay_microsec = 0;
 
     /* Set the number of generations to simulate before wiping the simulation */
     /* and re-running with new settings.                                      */
-    if (get_boolean_resource(state->dpy, "random-num-generations", "Boolean")) {
+    if (get_boolean_resource(state->dpy, "random-length", "Boolean")) {
         /* By empirical observation, keep the product                         */
         /*      state->num_generations * state->cell_size                     */
         /* below 10,000 to avoid BadAlloc errors from the X server due to     */
         /* By empirical observation, keep the product                         */
         /*      state->num_generations * state->cell_size                     */
         /* below 10,000 to avoid BadAlloc errors from the X server due to     */
@@ -368,7 +372,7 @@ WolframAutomata_init(Display * dpy, Window win)
             state->num_generations = (state->dpy_height / state->cell_size) + 1;
         }
     } else {
             state->num_generations = (state->dpy_height / state->cell_size) + 1;
         }
     } else {
-        state->num_generations = get_integer_resource(state->dpy, "num-generations", "Integer");
+        state->num_generations = get_integer_resource(state->dpy, "length", "Integer");
     }
     /* The minimum number of generations is 2 since we must allocate enough   */
     /* space to hold the seed generation and at least one pass through        */
     }
     /* The minimum number of generations is 2 since we must allocate enough   */
     /* space to hold the seed generation and at least one pass through        */
@@ -383,21 +387,19 @@ WolframAutomata_init(Display * dpy, Window win)
     }
 
     /* Time to figure out which rule to use for this simulation.              */
     }
 
     /* Time to figure out which rule to use for this simulation.              */
-    /* We ignore any weirdness resulting from the following cast since every  */
+    /* We ignore any weirdness resulting from the following casts since every */
     /* bit pattern is also a valid rule; if the user provides weird input,    */
     /* then we'll return weird (but well-defined!) output.                    */
     /* bit pattern is also a valid rule; if the user provides weird input,    */
     /* then we'll return weird (but well-defined!) output.                    */
-    state->rule_requested = (uint8_t) get_integer_resource(state->dpy, "rule-requested", "Integer");
-    state->rule_random = get_boolean_resource(state->dpy, "rule-random", "Boolean");
+    state->requested_rule = get_integer_resource(state->dpy, "rule", "Integer");
+    state->random_rule = get_boolean_resource(state->dpy, "random-rule", "Boolean");
     /* Through the following set of branches, we enforce CLI flag precedence. */
     /* Through the following set of branches, we enforce CLI flag precedence. */
-    if (state->rule_random) {
+    if (state->random_rule) {
         /* If this flag is set, the user wants truly random rules rather than */
         /* random rules from a curated list.                                  */
         state->rule_number = (uint8_t) random();
         /* If this flag is set, the user wants truly random rules rather than */
         /* random rules from a curated list.                                  */
         state->rule_number = (uint8_t) random();
-    } else if (state->rule_requested != 0) {
-        /* Rule 0 is terribly uninteresting, so we are reusing it as a 'null' */
-        /* value and hoping nobody notices. Finding a non-zero value means    */
-        /* the user requested a specific rule. Use it.                        */
-        state->rule_number = state->rule_requested;
+    } else if (state->requested_rule != -1) {
+        /* The user requested a specific rule. Use it.                        */
+        state->rule_number = (uint8_t) state->requested_rule;
     } else {
         /* No command-line options were specified, so select rules randomly   */
         /* from a curated list.                                               */
     } else {
         /* No command-line options were specified, so select rules randomly   */
         /* from a curated list.                                               */
@@ -478,7 +480,7 @@ WolframAutomata_draw(Display * dpy, Window win, void * closure)
             closure = WolframAutomata_init(dpy, win);
         } else {
             state->admiration_in_progress = True;
             closure = WolframAutomata_init(dpy, win);
         } else {
             state->admiration_in_progress = True;
-            return state->admiration_delay;
+            return 1000000 * state->admiration_delay;
         }
     }
 
         }
     }
 
@@ -498,32 +500,34 @@ WolframAutomata_draw(Display * dpy, Window win, void * closure)
 }
 
 static const char * WolframAutomata_defaults[] = {
 }
 
 static const char * WolframAutomata_defaults[] = {
-    "*delay-usec:         25000",
-    "*num-generations:    5000",
-    "*pixel-size:         2",
+    "*delay:              25000",
+    "*admiration-delay:   5",
+    "*length:             5000",
+    "*cell-size:          2",
+    "*color-index:        -1",
     "*population-density: 50",
     "*population-single:  False",
     "*population-density: 50",
     "*population-single:  False",
-    "*random-cellsize:    False",
-    "*random-color:       False",
+    "*random-cell-size:   False",
     "*random-delay:       False",
     "*random-length:      False",
     "*random-rule:        False",
     "*random-delay:       False",
     "*random-length:      False",
     "*random-rule:        False",
-    "*rule-requested:     0",
+    "*rule:               -1",
     0
 };
 
 static XrmOptionDescRec WolframAutomata_options[] = {
     0
 };
 
 static XrmOptionDescRec WolframAutomata_options[] = {
-    { "-delay-usec",         ".delay-usec",             XrmoptionSepArg, 0      },
-    { "-num-generations",    ".num-generations",        XrmoptionSepArg, 0      },
-    { "-pixel-size",         ".pixel-size",             XrmoptionSepArg, 0      },
+    { "-delay",              ".delay",                  XrmoptionSepArg, 0      },
+    { "-admiration-delay",   ".admiration-delay",       XrmoptionSepArg, 0      },
+    { "-length",             ".length",                 XrmoptionSepArg, 0      },
+    { "-cell-size",          ".cell-size",              XrmoptionSepArg, 0      },
+    { "-color-index",        ".color-index",            XrmoptionSepArg, 0      },
     { "-population-density", ".population-density",     XrmoptionSepArg, 0      },
     { "-population-single",  ".population-single",      XrmoptionNoArg,  "True" },
     { "-population-density", ".population-density",     XrmoptionSepArg, 0      },
     { "-population-single",  ".population-single",      XrmoptionNoArg,  "True" },
-    { "-random-cellsize",    ".random-pixel-size",      XrmoptionNoArg,  "True" },
-    { "-random-color",       ".random-colors",          XrmoptionNoArg,  "True" },
+    { "-random-cell-size",   ".random-cell-size",       XrmoptionNoArg,  "True" },
     { "-random-delay",       ".random-delay",           XrmoptionNoArg,  "True" },
     { "-random-delay",       ".random-delay",           XrmoptionNoArg,  "True" },
-    { "-random-length",      ".random-num-generations", XrmoptionNoArg,  "True" },
-    { "-random-rule",        ".rule-random",            XrmoptionNoArg,  "True" },
-    { "-rule",               ".rule-requested",         XrmoptionSepArg, 0      },
+    { "-random-length",      ".random-length",          XrmoptionNoArg,  "True" },
+    { "-random-rule",        ".random-rule",            XrmoptionNoArg,  "True" },
+    { "-rule",               ".rule",                   XrmoptionSepArg, 0      },
     { 0, 0, 0, 0 }
 };
 
     { 0, 0, 0, 0 }
 };