| 1 | /* (c) 2021 Aaron Taylor <ataylor at subgeniuskitty dot com> */ |
| 2 | /* See LICENSE.txt file for copyright and license details. */ |
| 3 | |
| 4 | #include "screenhack.h" |
| 5 | |
| 6 | /* -------------------------------------------------------------------------- */ |
| 7 | /* Data Structures */ |
| 8 | /* -------------------------------------------------------------------------- */ |
| 9 | |
| 10 | struct state { |
| 11 | /* Various X resources */ |
| 12 | Display * dpy; |
| 13 | Window win; |
| 14 | GC gc; |
| 15 | |
| 16 | /* These hold the pixel value of the foreground and background colors in */ |
| 17 | /* the same format as an XColor struct's "pixel" member. */ |
| 18 | unsigned long fg, bg; |
| 19 | |
| 20 | /* This Pixmap will eventually hold the entire evolution of the CA. The */ |
| 21 | /* displayed portion of the CA's evolution is merely a viewport into this */ |
| 22 | /* Pixmap. */ |
| 23 | Pixmap evolution_history; |
| 24 | |
| 25 | /* Together, these three values define the display viewport into the */ |
| 26 | /* 'evolution_history' Pixmap. The pair 'dpy_width' and 'dpy_height' are */ |
| 27 | /* simply the width and height of the display window. They remain */ |
| 28 | /* unchanged during normal operation. However, 'ypos' tracks the location */ |
| 29 | /* of the viewport in the 'evolution_history'. It must always keep the */ |
| 30 | /* newest generation onscreen and display as much history as possible. */ |
| 31 | int dpy_width, dpy_height, ypos; |
| 32 | |
| 33 | /* In the 'current_generation' array, the value True means a cell is */ |
| 34 | /* alive. We only need to track the current generation since our rulesets */ |
| 35 | /* never consider older generations. Anything older can be rendered to */ |
| 36 | /* the 'evolution_history' Pixmap and subsequently ignored. */ |
| 37 | Bool * current_generation; |
| 38 | |
| 39 | /* When randomizing the seed generation, we can specify a population */ |
| 40 | /* density, or we can restrict to a single living cell. */ |
| 41 | int population_density; |
| 42 | Bool population_single; |
| 43 | |
| 44 | /* For more information on the encoding used for rule_number and on the */ |
| 45 | /* method used to apply it: https://en.wikipedia.org/wiki/Wolfram_code */ |
| 46 | uint8_t rule_number; |
| 47 | |
| 48 | /* At the end of the simulation, the user is given time to admire the */ |
| 49 | /* output. Delay is available to user as CLI option '-admiration-delay'. */ |
| 50 | Bool admiration_in_progress; |
| 51 | size_t admiration_delay; /* ...in seconds. */ |
| 52 | |
| 53 | /* The following values correspond directly to independent CLI options. */ |
| 54 | Bool random_rule; |
| 55 | int requested_rule; |
| 56 | int cell_size; /* If cell_size=N then draw NxN pixels per cell. */ |
| 57 | int delay_microsec; /* ...between calls to WolframAutomata_draw(). */ |
| 58 | int num_generations; /* Reset simulation after this many generations. */ |
| 59 | |
| 60 | /* Not strictly necessary, but makes some code easier to read. */ |
| 61 | size_t number_of_cells; |
| 62 | }; |
| 63 | |
| 64 | enum seed_population { |
| 65 | random_cell, |
| 66 | middle_cell, |
| 67 | edge_cell |
| 68 | }; |
| 69 | |
| 70 | struct curated_ruleset { |
| 71 | uint8_t rule; |
| 72 | enum seed_population seed; |
| 73 | }; |
| 74 | |
| 75 | static const struct curated_ruleset curated_ruleset_list[] = { |
| 76 | { 18, middle_cell}, |
| 77 | { 30, middle_cell}, |
| 78 | { 45, middle_cell}, |
| 79 | { 54, middle_cell}, |
| 80 | { 57, middle_cell}, |
| 81 | { 73, middle_cell}, |
| 82 | {105, middle_cell}, |
| 83 | {109, middle_cell}, |
| 84 | {129, middle_cell}, |
| 85 | {133, middle_cell}, |
| 86 | {135, middle_cell}, |
| 87 | {150, middle_cell}, |
| 88 | { 30, edge_cell}, |
| 89 | { 45, edge_cell}, |
| 90 | { 57, edge_cell}, |
| 91 | { 60, edge_cell}, |
| 92 | { 75, edge_cell}, |
| 93 | {107, edge_cell}, |
| 94 | {110, edge_cell}, |
| 95 | {133, edge_cell}, |
| 96 | {137, edge_cell}, |
| 97 | {169, edge_cell}, |
| 98 | {225, edge_cell}, |
| 99 | { 22, random_cell}, |
| 100 | { 30, random_cell}, |
| 101 | { 54, random_cell}, |
| 102 | { 62, random_cell}, |
| 103 | { 90, random_cell}, |
| 104 | {105, random_cell}, |
| 105 | {108, random_cell}, |
| 106 | {110, random_cell}, |
| 107 | {126, random_cell}, |
| 108 | {146, random_cell}, |
| 109 | {150, random_cell}, |
| 110 | {182, random_cell}, |
| 111 | {184, random_cell}, |
| 112 | {225, random_cell}, |
| 113 | {240, random_cell} |
| 114 | }; |
| 115 | |
| 116 | struct color_pair { |
| 117 | /* The type 'unsigned short' comes from the XColor struct definition, */ |
| 118 | /* reproduced below. */ |
| 119 | /* */ |
| 120 | /* typedef struct { */ |
| 121 | /* unsigned long pixel; */ |
| 122 | /* unsigned short red, green, blue; */ |
| 123 | /* char flags; */ |
| 124 | /* char pad; */ |
| 125 | /* } XColor; */ |
| 126 | /* */ |
| 127 | /* The red, green, and blue values are always in the range 0 to 65535 */ |
| 128 | /* inclusive, independent of the number of bits actually used in the */ |
| 129 | /* display hardware. The server scales these values to the range used */ |
| 130 | /* by the hardware. Black is represented by (0,0,0), and white is */ |
| 131 | /* represented by (65535,65535,65535). */ |
| 132 | unsigned short fg_red, fg_green, fg_blue; |
| 133 | unsigned short bg_red, bg_green, bg_blue; |
| 134 | }; |
| 135 | |
| 136 | static const struct color_pair color_list[] = { |
| 137 | /* For mapping X11 color names to RGB values: */ |
| 138 | /* https://www.ehdp.com/methods/x11-color-names-rgb-values.htm */ |
| 139 | /* Remember that our values range from 0-65535 inclusive, so scale the */ |
| 140 | /* usual 0-255 range accordingly. */ |
| 141 | /* */ |
| 142 | /* +---------------------------------------+ */ |
| 143 | /* | foreground | | background | */ |
| 144 | /* | red,green,blue | | red,green,blue | */ |
| 145 | {65535, 0, 0, 0, 0, 0}, /* {"red", "black"}, */ |
| 146 | {32767,32767, 0, 0, 0, 0}, /* {"olive", "black"}, */ |
| 147 | { 0,32767,32767, 0, 0, 0}, /* {"teal", "black"}, */ |
| 148 | {27524,22937,52428, 0, 0, 0}, /* {"slateblue", "black"}, */ |
| 149 | {60947,33422,60947, 0, 0, 0}, /* {"violet", "black"}, */ |
| 150 | {41287, 8519,61602, 0, 0, 0}, /* {"purple", "black"}, */ |
| 151 | {65535,65535,65535, 0, 0, 0}, /* {"white", "black"}, */ |
| 152 | {65535,65535,65535, 0,25558, 0}, /* {"white", "darkgreen"}, */ |
| 153 | {65535,65535,65535, 36044, 0,36044}, /* {"white", "darkmagenta"}, */ |
| 154 | {65535,65535,65535, 36044, 0, 0}, /* {"white", "darkred"}, */ |
| 155 | {65535,65535,65535, 0, 0,36044}, /* {"white", "darkblue"}, */ |
| 156 | {11796,20315,20315, 36494,65535,65535}, /* {"darkslategray", "darkslategray1"}, */ |
| 157 | {45219,50461,57015, 11796,20315,20315}, /* {"lightsteelblue", "darkslategray"}, */ |
| 158 | {10023,16448,35723, 16383,26869,57670}, /* {"royalblue4", "royalblue"}, */ |
| 159 | {61166,57311,52428, 35723,33667,30840}, /* {"antiquewhite2", "antiquewhite4"}, */ |
| 160 | {51914,65535,28784, 21626,27524,11796}, /* {"darkolivegreen1", "darkolivegreen"}, */ |
| 161 | {49601,65535,49601, 26985,35723,26985}, /* {"darkseagreen1", "darkseagreen4"}, */ |
| 162 | {65535,49151,52428, 36044, 0, 0}, /* {"pink", "darkred"}, */ |
| 163 | {44563,55704,58981, 0,25558, 0}, /* {"lightblue", "darkgreen"}, */ |
| 164 | {65535, 0, 0, 0, 0,65535}, /* {"red", "blue"}, */ |
| 165 | {65535, 0, 0, 0,25558, 0}, /* {"red", "darkgreen"}, */ |
| 166 | { 0,65535,65535, 0,32767,32767}, /* {"aqua", "teal"}, */ |
| 167 | { 0, 0,36044, 0,32767,32767}, /* {"darkblue", "teal"}, */ |
| 168 | {61602,58981,32767, 11796,36044,22281}, /* {"khaki", "seagreen"}, */ |
| 169 | {61602,58981,32767, 21626,27524,11796}, /* {"khaki", "darkolivegreen"}, */ |
| 170 | {30801,34733,39321, 11796,20315,20315}, /* {"lightslategray", "darkslategray"}, */ |
| 171 | {65535,25558,18349, 11796,20315,20315}, /* {"tomato", "darkslategray"}, */ |
| 172 | {65535,25558,18349, 0,36044,36044} /* {"tomato", "darkcyan"} */ |
| 173 | }; |
| 174 | |
| 175 | /* -------------------------------------------------------------------------- */ |
| 176 | /* Helper Functions */ |
| 177 | /* -------------------------------------------------------------------------- */ |
| 178 | |
| 179 | static void |
| 180 | generate_random_seed(struct state * state) |
| 181 | { |
| 182 | int i; |
| 183 | for (i = 0; i < state->number_of_cells; i++) { |
| 184 | state->current_generation[i] = ((random() % 100) < state->population_density) ? True : False; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | /* This function sanitizes the index used to access cells in a generation. */ |
| 189 | /* Specifically, it wraps the index, creating a circular universe for the */ |
| 190 | /* cells and ensuring every cell has two neighbors. */ |
| 191 | static size_t |
| 192 | sindex(struct state * state, int index) |
| 193 | { |
| 194 | while (index < 0) { |
| 195 | index += state->number_of_cells; |
| 196 | } |
| 197 | while (index >= state->number_of_cells) { |
| 198 | index -= state->number_of_cells; |
| 199 | } |
| 200 | return (size_t) index; |
| 201 | } |
| 202 | |
| 203 | /* For more information on the encoding used for state->rule_number and on */ |
| 204 | /* the method used to apply it: https://en.wikipedia.org/wiki/Wolfram_code */ |
| 205 | static Bool |
| 206 | calculate_cell(struct state * state, int cell_id) |
| 207 | { |
| 208 | uint8_t cell_pattern = 0; |
| 209 | int i; |
| 210 | for (i = -1; i < 2; i++) { |
| 211 | cell_pattern = cell_pattern << 1; |
| 212 | if (state->current_generation[sindex(state, cell_id+i)] == True) { |
| 213 | cell_pattern |= 1; |
| 214 | } |
| 215 | } |
| 216 | if ((state->rule_number >> cell_pattern) & 1) { |
| 217 | return True; |
| 218 | } else { |
| 219 | return False; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | static void |
| 224 | render_current_generation(struct state * state) |
| 225 | { |
| 226 | size_t xpos; |
| 227 | for (xpos = 0; xpos < state->number_of_cells; xpos++) { |
| 228 | if (state->current_generation[xpos] == True) { |
| 229 | XFillRectangle(state->dpy, state->evolution_history, state->gc, xpos*state->cell_size, state->ypos, state->cell_size, state->cell_size); |
| 230 | } else { |
| 231 | XSetForeground(state->dpy, state->gc, state->bg); |
| 232 | XFillRectangle(state->dpy, state->evolution_history, state->gc, xpos*state->cell_size, state->ypos, state->cell_size, state->cell_size); |
| 233 | XSetForeground(state->dpy, state->gc, state->fg); |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | /* -------------------------------------------------------------------------- */ |
| 239 | /* Screenhack API Functions */ |
| 240 | /* -------------------------------------------------------------------------- */ |
| 241 | |
| 242 | static Bool |
| 243 | WolframAutomata_event(Display * dpy, Window win, void * closure, XEvent * event) |
| 244 | { |
| 245 | return False; |
| 246 | } |
| 247 | |
| 248 | static void |
| 249 | WolframAutomata_free(Display * dpy, Window win, void * closure) |
| 250 | { |
| 251 | struct state * state = closure; |
| 252 | XFreeGC(state->dpy, state->gc); |
| 253 | XFreePixmap(state->dpy, state->evolution_history); |
| 254 | free(state->current_generation); |
| 255 | free(state); |
| 256 | } |
| 257 | |
| 258 | static void * |
| 259 | WolframAutomata_init(Display * dpy, Window win) |
| 260 | { |
| 261 | struct state * state = calloc(1, sizeof(*state)); |
| 262 | if (!state) { |
| 263 | fprintf(stderr, "ERROR: Failed to calloc() for state struct in WolframAutomata_init().\n"); |
| 264 | exit(EXIT_FAILURE); |
| 265 | } |
| 266 | |
| 267 | XGCValues gcv; |
| 268 | XWindowAttributes xgwa; |
| 269 | const struct curated_ruleset * curated_ruleset = NULL; |
| 270 | |
| 271 | state->dpy = dpy; |
| 272 | state->win = win; |
| 273 | |
| 274 | XGetWindowAttributes(state->dpy, state->win, &xgwa); |
| 275 | state->dpy_width = xgwa.width; |
| 276 | state->dpy_height = xgwa.height; |
| 277 | state->ypos = 0; |
| 278 | |
| 279 | state->admiration_delay = get_integer_resource(state->dpy, "admiration-delay", "Integer"); |
| 280 | state->admiration_in_progress = False; |
| 281 | |
| 282 | /* Set foreground and background colors for active/inactive cells. Either */ |
| 283 | /* the user provided an index into the pre-defined color_list[] or a */ |
| 284 | /* random entry from that same array should be selected. */ |
| 285 | size_t color_index = get_integer_resource(state->dpy, "color-index", "Integer"); |
| 286 | if (color_index == -1) { |
| 287 | color_index = random() % sizeof(color_list)/sizeof(color_list[0]); |
| 288 | } else if (color_index >= sizeof(color_list)/sizeof(color_list[0])) { |
| 289 | fprintf(stderr, "WARNING: Color index out of range.\n"); |
| 290 | color_index = 0; |
| 291 | } |
| 292 | XColor fg, bg; |
| 293 | fg.red = color_list[color_index].fg_red; |
| 294 | fg.green = color_list[color_index].fg_green; |
| 295 | fg.blue = color_list[color_index].fg_blue; |
| 296 | bg.red = color_list[color_index].bg_red; |
| 297 | bg.green = color_list[color_index].bg_green; |
| 298 | bg.blue = color_list[color_index].bg_blue; |
| 299 | /* TODO: Since I 'alloc', presumably I must also 'free' these colors */ |
| 300 | /* at some point. Where/how? I don't want to eventually crash my */ |
| 301 | /* X server after months of use. */ |
| 302 | XAllocColor(state->dpy, xgwa.colormap, &fg); |
| 303 | XAllocColor(state->dpy, xgwa.colormap, &bg); |
| 304 | state->fg = gcv.foreground = fg.pixel; |
| 305 | state->bg = gcv.background = bg.pixel; |
| 306 | |
| 307 | state->gc = XCreateGC(state->dpy, state->win, GCForeground, &gcv); |
| 308 | |
| 309 | /* Set the size of each simulated cell to NxN pixels for cell_size=N. */ |
| 310 | if (get_boolean_resource(state->dpy, "random-cell-size", "Boolean")) { |
| 311 | /* Although we are choosing the pixel size 'randomly', a truly random */ |
| 312 | /* selection would bias toward large numbers since there are more of */ |
| 313 | /* them. To avoid this, we select a random number for a bit shift, */ |
| 314 | /* resulting in a pixel size of 1, 2, 4, 8, 16 or 32, equally likely. */ |
| 315 | state->cell_size = 1 << (random() % 6); |
| 316 | } else { |
| 317 | state->cell_size = get_integer_resource(state->dpy, "cell-size", "Integer"); |
| 318 | } |
| 319 | if (state->cell_size < 1) state->cell_size = 1; |
| 320 | if (state->cell_size > state->dpy_width) state->cell_size = state->dpy_width; |
| 321 | |
| 322 | /* Larger cell sizes won't always evenly divide the number of pixels in */ |
| 323 | /* our window. In order to avoid a black stripe down the edge, '+1' here */ |
| 324 | /* to ensure we are slightly oversize rather than undersize. */ |
| 325 | state->number_of_cells = (state->dpy_width / state->cell_size) + 1; |
| 326 | |
| 327 | /* Set the delay (in microseconds) between simulation of each generation */ |
| 328 | /* of the simulation, also known as the delay between calls to */ |
| 329 | /* WolframAutomata_draw(), which simulates one generation per call. */ |
| 330 | if (get_boolean_resource(state->dpy, "random-delay", "Boolean")) { |
| 331 | /* When randomly setting the delay, the problem is to avoid being too */ |
| 332 | /* fast or too slow, as well as ensuring slower speeds are chosen */ |
| 333 | /* with the same likelihood as faster speeds, as perceived by a */ |
| 334 | /* human. By empirical observation, we note that for 1x1 up to 4x4 */ |
| 335 | /* pixel cell sizes, values for state->delay_microsec between */ |
| 336 | /* 2048 (2^11) and 16556 (2^14) produce pleasant scroll rates. To */ |
| 337 | /* maintain this appearance, we bitshift state->cell_size down until */ |
| 338 | /* it is a maximum of 4x4 pixels in size, record how many bitshifts */ |
| 339 | /* took place, and then shift our valid window for */ |
| 340 | /* state->delay_microsec up by an equal number of bitshifts. For */ |
| 341 | /* example, if state->cell_size=9, then it takes one right shift to */ |
| 342 | /* reach state->cell_size=4. Thus, the valid window for */ |
| 343 | /* state->delay_microsec becomes 4096 (2^12) up to 32768 (2^15). */ |
| 344 | size_t pixel_shift_range = 1; |
| 345 | size_t cell_size_temp = state->cell_size; |
| 346 | while (cell_size_temp > 4) { |
| 347 | cell_size_temp >>= 1; |
| 348 | pixel_shift_range++; |
| 349 | } |
| 350 | /* In the below line, '3' represents the total range, namely '14-11' */ |
| 351 | /* from '2^14' and '2^11' as the endpoints. Similarly, the '11' in */ |
| 352 | /* the below line represents the starting point of this range, from */ |
| 353 | /* the exponent in '2^11'. */ |
| 354 | state->delay_microsec = 1 << ((random() % 3) + 11 + pixel_shift_range); |
| 355 | } else { |
| 356 | state->delay_microsec = get_integer_resource(state->dpy, "delay", "Integer"); |
| 357 | } |
| 358 | if (state->delay_microsec < 0) state->delay_microsec = 0; |
| 359 | |
| 360 | /* Set the number of generations to simulate before wiping the simulation */ |
| 361 | /* and re-running with new settings. */ |
| 362 | if (get_boolean_resource(state->dpy, "random-length", "Boolean")) { |
| 363 | /* By empirical observation, keep the product */ |
| 364 | /* state->num_generations * state->cell_size */ |
| 365 | /* below 10,000 to avoid BadAlloc errors from the X server due to */ |
| 366 | /* requesting an enormous pixmap. This value works on both a 12 core */ |
| 367 | /* Xeon with 108 GiB of RAM and a Sun Ultra 2 with 2 GiB of RAM. */ |
| 368 | state->num_generations = random() % (10000 / state->cell_size); |
| 369 | /* Ensure selected value is large enough to at least fill the screen. */ |
| 370 | /* Cast to avoid overflow. */ |
| 371 | if ((long)state->num_generations * (long)state->cell_size < state->dpy_height) { |
| 372 | state->num_generations = (state->dpy_height / state->cell_size) + 1; |
| 373 | } |
| 374 | } else { |
| 375 | state->num_generations = get_integer_resource(state->dpy, "length", "Integer"); |
| 376 | } |
| 377 | /* The minimum number of generations is 2 since we must allocate enough */ |
| 378 | /* space to hold the seed generation and at least one pass through */ |
| 379 | /* WolframAutomata_draw(), which is where we check whether or not we've */ |
| 380 | /* reached the end of the pixmap. */ |
| 381 | if (state->num_generations < 0) state->num_generations = 2; |
| 382 | /* The maximum number of generations is cell_size dependent. This is a */ |
| 383 | /* soft limit and may be increased if you have plenty of RAM (and a */ |
| 384 | /* cooperative X server). The value 10,000 was determined empirically. */ |
| 385 | if ((long)state->num_generations * (long)state->cell_size > 10000) { |
| 386 | state->num_generations = 10000 / state->cell_size; |
| 387 | } |
| 388 | |
| 389 | /* Time to figure out which rule to use for this simulation. */ |
| 390 | /* We ignore any weirdness resulting from the following casts since every */ |
| 391 | /* bit pattern is also a valid rule; if the user provides weird input, */ |
| 392 | /* then we'll return weird (but well-defined!) output. */ |
| 393 | state->requested_rule = get_integer_resource(state->dpy, "rule", "Integer"); |
| 394 | state->random_rule = get_boolean_resource(state->dpy, "random-rule", "Boolean"); |
| 395 | /* Through the following set of branches, we enforce CLI flag precedence. */ |
| 396 | if (state->random_rule) { |
| 397 | /* If this flag is set, the user wants truly random rules rather than */ |
| 398 | /* random rules from a curated list. */ |
| 399 | state->rule_number = (uint8_t) random(); |
| 400 | } else if (state->requested_rule != -1) { |
| 401 | /* The user requested a specific rule. Use it. */ |
| 402 | state->rule_number = (uint8_t) state->requested_rule; |
| 403 | } else { |
| 404 | /* No command-line options were specified, so select rules randomly */ |
| 405 | /* from a curated list. */ |
| 406 | size_t number_of_array_elements = sizeof(curated_ruleset_list)/sizeof(curated_ruleset_list[0]); |
| 407 | curated_ruleset = &curated_ruleset_list[random() % number_of_array_elements]; |
| 408 | state->rule_number = curated_ruleset->rule; |
| 409 | } |
| 410 | |
| 411 | /* Time to construct the seed generation for this simulation. */ |
| 412 | state->population_single = get_boolean_resource(state->dpy, "population-single", "Boolean"); |
| 413 | state->population_density = get_integer_resource(state->dpy, "population-density", "Integer"); |
| 414 | if (state->population_density < 0 || state->population_density > 100) state->population_density = 50; |
| 415 | state->current_generation = calloc(1, sizeof(*state->current_generation)*state->number_of_cells); |
| 416 | if (!state->current_generation) { |
| 417 | fprintf(stderr, "ERROR: Failed to calloc() for cell generation in WolframAutomata_init().\n"); |
| 418 | exit(EXIT_FAILURE); |
| 419 | } |
| 420 | if (curated_ruleset) { |
| 421 | /* If we're using a curated ruleset, ignore any CLI flags related to */ |
| 422 | /* setting the seed generation, instead drawing that information from */ |
| 423 | /* the curated ruleset. */ |
| 424 | switch (curated_ruleset->seed) { |
| 425 | case random_cell: generate_random_seed(state); break; |
| 426 | case middle_cell: state->current_generation[state->number_of_cells/2] = True; break; |
| 427 | case edge_cell : state->current_generation[0] = True; break; |
| 428 | } |
| 429 | } else { |
| 430 | /* If we're not using a curated ruleset, process any relevant flags */ |
| 431 | /* from the user, falling back to a random seed generation if nothing */ |
| 432 | /* else is specified. */ |
| 433 | if (state->population_single) { |
| 434 | state->current_generation[0] = True; |
| 435 | } else { |
| 436 | generate_random_seed(state); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | state->evolution_history = XCreatePixmap(state->dpy, state->win, state->dpy_width, state->num_generations*state->cell_size, xgwa.depth); |
| 441 | /* Pixmap contents are undefined after creation. Explicitly set a black */ |
| 442 | /* background by drawing a black rectangle over the entire pixmap. */ |
| 443 | XColor blackx, blacks; |
| 444 | XAllocNamedColor(state->dpy, DefaultColormapOfScreen(DefaultScreenOfDisplay(state->dpy)), "black", &blacks, &blackx); |
| 445 | XSetForeground(state->dpy, state->gc, blacks.pixel); |
| 446 | XFillRectangle(state->dpy, state->evolution_history, state->gc, 0, 0, state->dpy_width, state->num_generations*state->cell_size); |
| 447 | XSetForeground(state->dpy, state->gc, state->fg); |
| 448 | render_current_generation(state); |
| 449 | state->ypos += state->cell_size; |
| 450 | |
| 451 | return state; |
| 452 | } |
| 453 | |
| 454 | static unsigned long |
| 455 | WolframAutomata_draw(Display * dpy, Window win, void * closure) |
| 456 | { |
| 457 | struct state * state = closure; |
| 458 | int xpos; |
| 459 | int window_y_offset; |
| 460 | |
| 461 | /* Calculate and record new generation. */ |
| 462 | Bool new_generation[state->dpy_width]; |
| 463 | for (xpos = 0; xpos < state->number_of_cells; xpos++) { |
| 464 | new_generation[xpos] = calculate_cell(state, xpos); |
| 465 | } |
| 466 | for (xpos = 0; xpos < state->number_of_cells; xpos++) { |
| 467 | state->current_generation[xpos] = new_generation[xpos]; |
| 468 | } |
| 469 | render_current_generation(state); |
| 470 | |
| 471 | /* Check for end of simulation. */ |
| 472 | if (state->ypos/state->cell_size < state->num_generations-1) { |
| 473 | /* Life continues. */ |
| 474 | state->ypos += state->cell_size; |
| 475 | } else { |
| 476 | /* We have reached the end of this simulation. Give the user a moment */ |
| 477 | /* to bask in the glory of our output, then reset. */ |
| 478 | if (state->admiration_in_progress) { |
| 479 | WolframAutomata_free(dpy, win, state); |
| 480 | closure = WolframAutomata_init(dpy, win); |
| 481 | } else { |
| 482 | state->admiration_in_progress = True; |
| 483 | return 1000000 * state->admiration_delay; |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | /* Calculate vertical offset of current 'window' into the CA's history. */ |
| 488 | /* After the CA evolution exceeds our display extents, make window track */ |
| 489 | /* current generation, scrolling display to follow newest generation. */ |
| 490 | if (state->ypos < state->dpy_height) { |
| 491 | window_y_offset = 0; |
| 492 | } else { |
| 493 | window_y_offset = state->ypos - (state->dpy_height - 1); |
| 494 | } |
| 495 | |
| 496 | /* Render a window into the CA history. */ |
| 497 | XCopyArea(state->dpy, state->evolution_history, state->win, state->gc, 0, window_y_offset, state->dpy_width, state->dpy_height, 0, 0); |
| 498 | |
| 499 | return state->delay_microsec; |
| 500 | } |
| 501 | |
| 502 | static const char * WolframAutomata_defaults[] = { |
| 503 | "*delay: 25000", |
| 504 | "*admiration-delay: 5", |
| 505 | "*length: 5000", |
| 506 | "*cell-size: 2", |
| 507 | "*color-index: -1", |
| 508 | "*population-density: 50", |
| 509 | "*population-single: False", |
| 510 | "*random-cell-size: False", |
| 511 | "*random-delay: False", |
| 512 | "*random-length: False", |
| 513 | "*random-rule: False", |
| 514 | "*rule: -1", |
| 515 | 0 |
| 516 | }; |
| 517 | |
| 518 | static XrmOptionDescRec WolframAutomata_options[] = { |
| 519 | { "-delay", ".delay", XrmoptionSepArg, 0 }, |
| 520 | { "-admiration-delay", ".admiration-delay", XrmoptionSepArg, 0 }, |
| 521 | { "-length", ".length", XrmoptionSepArg, 0 }, |
| 522 | { "-cell-size", ".cell-size", XrmoptionSepArg, 0 }, |
| 523 | { "-color-index", ".color-index", XrmoptionSepArg, 0 }, |
| 524 | { "-population-density", ".population-density", XrmoptionSepArg, 0 }, |
| 525 | { "-population-single", ".population-single", XrmoptionNoArg, "True" }, |
| 526 | { "-random-cell-size", ".random-cell-size", XrmoptionNoArg, "True" }, |
| 527 | { "-random-delay", ".random-delay", XrmoptionNoArg, "True" }, |
| 528 | { "-random-length", ".random-length", XrmoptionNoArg, "True" }, |
| 529 | { "-random-rule", ".random-rule", XrmoptionNoArg, "True" }, |
| 530 | { "-rule", ".rule", XrmoptionSepArg, 0 }, |
| 531 | { 0, 0, 0, 0 } |
| 532 | }; |
| 533 | |
| 534 | static void |
| 535 | WolframAutomata_reshape(Display * dpy, Window win, void * closure, unsigned int w, unsigned int h) |
| 536 | { |
| 537 | struct state * state = closure; |
| 538 | XWindowAttributes xgwa; |
| 539 | XGetWindowAttributes(state->dpy, state->win, &xgwa); |
| 540 | |
| 541 | /* Only restart the simulation if the window changed size. */ |
| 542 | if (state->dpy_width != xgwa.width || state->dpy_height != xgwa.height) { |
| 543 | WolframAutomata_free(dpy, win, closure); |
| 544 | closure = WolframAutomata_init(dpy, win); |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | XSCREENSAVER_MODULE ("1D Nearest-Neighbor Cellular Automata", WolframAutomata) |
| 549 | |