From c9e09490b729509197dc5fae9aae438462251941 Mon Sep 17 00:00:00 2001 From: Aaron Taylor Date: Thu, 10 Jun 2021 17:42:56 -0700 Subject: [PATCH] Updated WolframAutomata to be C89 compliant. --- hacks/WolframAutomata/Makefile | 6 +++++- hacks/WolframAutomata/WolframAutomata.c | 25 ++++++++++++++++--------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/hacks/WolframAutomata/Makefile b/hacks/WolframAutomata/Makefile index 2b64750..c360e2d 100644 --- a/hacks/WolframAutomata/Makefile +++ b/hacks/WolframAutomata/Makefile @@ -12,11 +12,15 @@ # require minor tweaks (e.g. LIB_PATH on Debian should be /usr/lib/ rather than # /usr/local/lib/ and the compiler is gcc rather than cc). CC = cc -CC_FLAGS = -Wall -pedantic +CC_FLAGS = -Wall -pedantic -std=c89 INC_PATH = -I../../screenhack/ -I/usr/local/include/ LIB_PATH = -L/usr/local/lib/ LIBS = -lm -lX11 -lXt +# Many of the X-related header files contain named variadic macros. Suppress +# that warning so that we can enable -pedantic C89 compliance for our own code. +CC_FLAGS += -Wno-variadic-macros + # These flags are normally set in config.h which is generated by the autoconf # script as the initial step of the xscreensaver build process. The flags # enable/disable code in the various screenhack library files. When compiling diff --git a/hacks/WolframAutomata/WolframAutomata.c b/hacks/WolframAutomata/WolframAutomata.c index edf1dc3..4b5c1ec 100644 --- a/hacks/WolframAutomata/WolframAutomata.c +++ b/hacks/WolframAutomata/WolframAutomata.c @@ -264,16 +264,20 @@ WolframAutomata_free(Display * dpy, Window win, void * closure) static void * WolframAutomata_init(Display * dpy, Window win) { - struct state * state = calloc(1, sizeof(*state)); + struct state * state; + XGCValues gcv; + XWindowAttributes xgwa; + XColor fg, bg; + XColor blackx, blacks; + size_t color_index; + const struct curated_ruleset * curated_ruleset = NULL; + + 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; @@ -288,14 +292,13 @@ WolframAutomata_init(Display * dpy, Window win) /* 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"); + 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; @@ -452,7 +455,6 @@ WolframAutomata_init(Display * dpy, Window win) state->evolution_history = XCreatePixmap(state->dpy, state->win, state->dpy_width, state->num_generations*state->cell_size, xgwa.depth); /* Pixmap contents are undefined after creation. Explicitly set a black */ /* background by drawing a black rectangle over the entire pixmap. */ - XColor blackx, blacks; XAllocNamedColor(state->dpy, DefaultColormapOfScreen(DefaultScreenOfDisplay(state->dpy)), "black", &blacks, &blackx); XSetForeground(state->dpy, state->gc, blacks.pixel); XFillRectangle(state->dpy, state->evolution_history, state->gc, 0, 0, state->dpy_width, state->num_generations*state->cell_size); @@ -471,13 +473,18 @@ WolframAutomata_draw(Display * dpy, Window win, void * closure) int window_y_offset; /* Calculate and record new generation. */ - Bool new_generation[state->dpy_width]; + Bool * new_generation = malloc(state->dpy_width * sizeof(Bool)); + if (new_generation == NULL) { + fprintf(stderr, "ERROR: Failed to malloc() when calculating new generation.\n"); + exit(EXIT_FAILURE); + } for (xpos = 0; xpos < state->number_of_cells; xpos++) { new_generation[xpos] = calculate_cell(state, xpos); } for (xpos = 0; xpos < state->number_of_cells; xpos++) { state->current_generation[xpos] = new_generation[xpos]; } + free(new_generation); render_current_generation(state); /* Check for end of simulation. */ -- 2.20.1