+/* -------------------------------------------------------------------------- */
+/* Screenhack API Functions */
+/* -------------------------------------------------------------------------- */
+
+static void *
+WolframAutomata_init(Display * dpy, Window win)
+{
+ 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;
+
+ state->dpy = dpy;
+ state->win = win;
+
+ XGetWindowAttributes(state->dpy, state->win, &xgwa);
+ state->xlim = xgwa.width;
+ 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->delay_microsec = get_integer_resource(state->dpy, "delay-usec", "Integer");
+ if (state->delay_microsec < 0) state->delay_microsec = 0;
+
+ state->pixel_size = get_integer_resource(state->dpy, "pixel-size", "Integer");
+ if (state->pixel_size < 1) state->pixel_size = 1;
+ if (state->pixel_size > state->xlim) state->pixel_size = state->xlim;
+
+ state->number_of_cells = state->xlim / state->pixel_size;
+ // TODO: Do we want to enforce that number_of_cells > 0?
+
+ /* The minimum number of generations is 2 since we must allocate enough */
+ /* space to hold the seed generation and at least one pass through */
+ /* WolframAutomata_draw(), which is where we check whether or not we've */
+ /* reached the end of the pixmap. */
+ state->num_generations = get_integer_resource(state->dpy, "num-generations", "Integer");
+ if (state->num_generations < 0) state->num_generations = 2;
+
+ /* Time to figure out which rule to use for this simulation. */
+ /* We ignore any weirdness resulting from the following cast since every */
+ /* 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");
+ /* Through the following set of branches, we enforce CLI flag precedence. */
+ if (state->rule_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 {
+ /* No command-line options were specified, so select rules randomly */
+ /* from a curated list. */
+ size_t number_of_array_elements = sizeof(curated_ruleset_list)/sizeof(curated_ruleset_list[0]);
+ curated_ruleset = &curated_ruleset_list[random() % number_of_array_elements];
+ state->rule_number = curated_ruleset->rule;
+ }
+
+ /* Time to construct the seed generation for this simulation. */
+ state->population_single = get_boolean_resource(state->dpy, "population-single", "Boolean");
+ state->population_density = get_integer_resource(state->dpy, "population-density", "Integer");
+ 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() for cell generation in WolframAutomata_init().\n");
+ exit(EXIT_FAILURE);
+ }
+ if (curated_ruleset) {
+ /* If we're using a curated ruleset, ignore any CLI flags related to */
+ /* setting the seed generation, instead drawing that information from */
+ /* the curated ruleset. */
+ switch (curated_ruleset->seed) {
+ case random_cell: generate_random_seed(state); break;
+ case middle_cell: state->current_generation[state->number_of_cells/2] = True; break;
+ case edge_cell : state->current_generation[0] = True; break;
+ }
+ } else {
+ /* If we're not using a curated ruleset, process any relevant flags */
+ /* from the user, falling back to a random seed generation if nothing */
+ /* else is specified. */
+ if (state->population_single) {
+ state->current_generation[0] = True;
+ } else {
+ generate_random_seed(state);
+ }
+ }
+
+ // TODO: These should be command-line options, but I need to learn how the get_integer_resource() and similar functions work first.
+ state->display_info = True;
+
+ state->evolution_history = XCreatePixmap(state->dpy, state->win, state->xlim, state->num_generations*state->pixel_size, xgwa.depth);
+ // Pixmap contents are undefined after creation. Explicitly set a black
+ // background by drawing a black rectangle over the entire pixmap.
+ XSetForeground(state->dpy, state->gc, state->bg);
+ XFillRectangle(state->dpy, state->evolution_history, state->gc, 0, 0, state->xlim, state->num_generations*state->pixel_size);
+ XSetForeground(state->dpy, state->gc, state->fg);
+ render_current_generation(state);
+ state->ypos += state->pixel_size;
+
+ return state;
+}
+