summaryrefslogtreecommitdiff
path: root/src/modules/checkers/checkers.c
blob: be83f9ec1e7ead3a6cfb93bb3ee2c8c4be7997f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
/*
 *  Copyright (C) 2022 - Vito Caputo - <vcaputo@pengaru.com>
 *
 *  This program is free software: you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License version 2 as published
 *  by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <errno.h>
#include <stdlib.h>
#include <unistd.h>

#include "til.h"
#include "til_fb.h"
#include "til_module_context.h"


#define CHECKERS_DEFAULT_SIZE		32
#define CHECKERS_DEFAULT_PATTERN	CHECKERS_PATTERN_CHECKERED
#define CHECKERS_DEFAULT_DYNAMICS	CHECKERS_DYNAMICS_ODD
#define CHECKERS_DEFAULT_DYNAMICS_RATE	1.0
#define CHECKERS_DEFAULT_FILL		CHECKERS_FILL_COLOR
#define CHECKERS_DEFAULT_COLOR		0xffffff
#define CHECKERS_DEFAULT_FILL_MODULE	"none"


typedef enum checkers_pattern_t {
	CHECKERS_PATTERN_CHECKERED,
	CHECKERS_PATTERN_RANDOM,
} checkers_pattern_t;

typedef enum checkers_dynamics_t {
	CHECKERS_DYNAMICS_ODD,
	CHECKERS_DYNAMICS_EVEN,
	CHECKERS_DYNAMICS_ALTERNATING,
	CHECKERS_DYNAMICS_RANDOM,
} checkers_dynamics_t;

typedef enum checkers_fill_t {
	CHECKERS_FILL_COLOR,
	CHECKERS_FILL_SAMPLED,
	CHECKERS_FILL_TEXTURED,
	CHECKERS_FILL_RANDOM,
	CHECKERS_FILL_MIXED,
} checkers_fill_t;

typedef struct checkers_setup_t {
	til_setup_t		til_setup;
	unsigned		size;
	checkers_pattern_t	pattern;
	checkers_dynamics_t	dynamics;
	float			rate;
	checkers_fill_t		fill;
	uint32_t		color;
	const til_module_t	*fill_module;
	til_setup_t		*fill_module_setup;
} checkers_setup_t;

typedef struct checkers_context_t {
	til_module_context_t	til_module_context;
	checkers_setup_t	*setup;
	til_module_context_t	*fill_module_contexts[];
} checkers_context_t;


static til_module_context_t * checkers_create_context(const til_module_t *module, til_stream_t *stream, unsigned seed, unsigned ticks, unsigned n_cpus, char *path, til_setup_t *setup)
{
	size_t			size = sizeof(checkers_context_t);
	checkers_context_t	*ctxt;

	if (((checkers_setup_t *)setup)->fill_module)
		size += sizeof(til_module_context_t *) * n_cpus;

	ctxt = til_module_context_new(module, size, stream, seed, ticks, n_cpus, path, setup);
	if (!ctxt)
		return NULL;

	ctxt->setup = (checkers_setup_t *)setup;

	if (ctxt->setup->fill_module) {
		const til_module_t	*module = ctxt->setup->fill_module;
		size_t			fill_module_path_len = snprintf(NULL, 0, "%s/fill_module", path) + 1; /* FIXME TODO: revisit path construction, see big comment in modules/compose FIXME TODO */
		char			*fill_module_path = calloc(1, fill_module_path_len);

		if (!fill_module_path)
			return til_module_context_free(&ctxt->til_module_context);

		snprintf(fill_module_path, fill_module_path_len, "%s/fill_module", path);

		/* since checkers is already threaded, create an n_cpus=1 context per-cpu */
		for (unsigned i = 0; i < n_cpus; i++) /* TODO: errors */
			(void) til_module_create_context(module, stream, seed, ticks, 1, fill_module_path, ctxt->setup->fill_module_setup, &ctxt->fill_module_contexts[i]);
			 /* FIXME TODO ^^^ sharing the fill_module_path across the per-cpu contexts aliases them _across_threads_ no less, this needs attention/thought FIXME TODO */
			 /* but the problem with just doing something like suffixing the cpu # to give each context a unique path per-cpu is then the taps/pipes would all be
			  * unique on a per-cpu basis.  That's _very_ undesirable in terms of sequencing the pipes by name, we don't want sequencing tracks per-cpu and that's
			  * a non-deterministic quantity depending on the host anyways.  So the path can't encode a cpu number, that's just a hard NOPE.  What may need to be
			  * done here is to stop creating the context per-cpu in checkers and instead be able to simply do a single n_cpus create context at the single path,
			  * then be able to perform a forced cpu-specific render on a given context.  Then whatever the context-specific module does with threaded rendering
			  * on a shared context is its problem, and we already told it the n_cpus value so it could do whatever it needed to.  The problem with this as-is is
			  * that some modules just don't implement threaded rendering at all and can't share their context across threads as a result, so checkers can't make
			  * that assumption.  This hack here of creating a context per-cpu works around that problem entirely, so even simple modules magically become threaded.
			  * Maybe what needs to happen is everything path-bound needs to just have some magic surrounding cpu-local state.  As in, the tap/tap-driving stuff could
			  * just roll with the punches and share the first instance of tap variables then all the others just get left alone. FIXME TODO FIXME TODO
			  */

		free(fill_module_path);
		/* XXX: it would be interesting to support various patterns/layouts by varying the seed, but this will require
		 * more complex context allocation strategies while also maintaining the per-cpu allocation.
		 */
	}

	return &ctxt->til_module_context;
}


static void checkers_destroy_context(til_module_context_t *context)
{
	checkers_context_t	*ctxt = (checkers_context_t *)context;

	if (ctxt->setup->fill_module) {
		for (unsigned i = 0; i < context->n_cpus; i++)
			til_module_context_free(ctxt->fill_module_contexts[i]);
	}

	free(ctxt);
}


/* This is derived from til_fb_fragment_tile_single() with two variations:
 * 1. when the size doesn't align with frame size, the start tiles are offset
 *    to center the checkers letting the edge checkers all clip as needed
 * 2. the incoming frame width isn't propagated down to the tiled fragments,
 *    though for the potentially clipped boundary tiles the frame_{width,height}
 *    won't match the incoming width,height.
 *
 * XXX note this fragmenter in particular really exercises fill_modules' correct handling
 *     of frame vs. fragment dimensions and clipping semantics
 */
int checkers_fragment_tile_single(const til_fb_fragment_t *fragment, unsigned tile_size, unsigned number, til_fb_fragment_t *res_fragment)
{
	unsigned	w = fragment->width / tile_size, h = fragment->height / tile_size;
	unsigned	tiled_w = w * tile_size, tiled_h = h * tile_size;
	unsigned	x, y, xoff, yoff, xshift = 0, yshift = 0;

	assert(fragment);
	assert(res_fragment);

	/* Detect the need for fractional tiles on both axis and shift the fragments
	 * to keep the overall checkered output centered.  This complicates res_fragment.{x,y,width,height}
	 * calculations for the peripheral checker tiles as those must clip when shifted.
	 */
	if (tiled_w < fragment->width) {
		tiled_w += tile_size;
		xshift = (tiled_w - fragment->width) >> 1;
		w++;
	}

	if (tiled_h < fragment->height) {
		tiled_h += tile_size;
		yshift = (tiled_h - fragment->height) >> 1;
		h++;
	}

	y = number / w;
	if (y >= h)
		return 0;

	x = number - (y * w);

	xoff = x * tile_size;
	yoff = y * tile_size;

	if (fragment->texture) {
		assert(res_fragment->texture);
		assert(fragment->frame_width == fragment->texture->frame_width);
		assert(fragment->frame_height == fragment->texture->frame_height);
		assert(fragment->width == fragment->texture->width);
		assert(fragment->height == fragment->texture->height);
		assert(fragment->x == fragment->texture->x);
		assert(fragment->y == fragment->texture->y);

		*(res_fragment->texture) = (til_fb_fragment_t){
					.buf = fragment->texture->buf + (yoff * fragment->texture->pitch) - (y ? (yshift * fragment->texture->pitch) : 0) + (xoff - (x ? xshift : 0)),
					.width = MIN(fragment->width - (xoff - xshift), x ? tile_size : (tile_size - xshift)),
					.height = MIN(fragment->height - (yoff - yshift), y ? tile_size : (tile_size - yshift)),
					.x = x ? 0 : xshift,
					.y = y ? 0 : yshift,
					.frame_width = tile_size,
					.frame_height = tile_size,
					.stride = fragment->texture->stride + (fragment->width - MIN(fragment->width - (xoff - xshift), x ? tile_size : (tile_size - xshift))),
					.pitch = fragment->texture->pitch,
					.cleared = fragment->texture->cleared,
				};
	}

	*res_fragment = (til_fb_fragment_t){
				.texture = fragment->texture ? res_fragment->texture : NULL,
				/* TODO: copy pasta! */
				.buf = fragment->buf + (yoff * fragment->pitch) - (y ? (yshift * fragment->pitch) : 0) + (xoff - (x ? xshift : 0)),
				.width = MIN(fragment->width - (xoff - xshift), x ? tile_size : (tile_size - xshift)),
				.height = MIN(fragment->height - (yoff - yshift), y ? tile_size : (tile_size - yshift)),
				.x = x ? 0 : xshift,
				.y = y ? 0 : yshift,
				// this is a little janky but leave frame_width to be set by render_fragment
				// so it can use the old frame_width for determining checkered state
				.frame_width = fragment->width, // becomes tile_size
				.frame_height = fragment->height, // becomes tile_size
				.stride = fragment->stride + (fragment->width - MIN(fragment->width - (xoff - xshift), x ? tile_size : (tile_size - xshift))),
				.pitch = fragment->pitch,
				.number = number,
				.cleared = fragment->cleared,
			};

	return 1;
}


static int checkers_fragmenter(til_module_context_t *context, const til_fb_fragment_t *fragment, unsigned number, til_fb_fragment_t *res_fragment)
{
	checkers_context_t	*ctxt = (checkers_context_t *)context;

	return checkers_fragment_tile_single(fragment, ctxt->setup->size, number, res_fragment);
}


static void checkers_prepare_frame(til_module_context_t *context, til_stream_t *stream, unsigned ticks, til_fb_fragment_t **fragment_ptr, til_frame_plan_t *res_frame_plan)
{
	checkers_context_t	*ctxt = (checkers_context_t *)context;
	til_fb_fragment_t	*fragment = *fragment_ptr;

	/* XXX: note cpu_affinity is required when fill_module is used, to ensure module_contexts
	 * have a stable relationship to fragnum.  Otherwise the output would be unstable because the
	 * module contexts would be randomly distributed across the filled checkers frame-to-frame.
	 * This is unfortunate since cpu_affinity is likely to be slower than just letting the render
	 * threads render fragments in whatever order (the preferred default).  fill_module here
	 * is actually *the* reason til_frame_plan_t.cpu_affinity got implemented, before this there
	 * wasn't even a til_frame_plan_t container; a bare til_fragmenter_t was returned.
	 */
	*res_frame_plan = (til_frame_plan_t){ .fragmenter = checkers_fragmenter, .cpu_affinity = !!ctxt->setup->fill_module };
}


static inline unsigned hash(unsigned x)
{
	x = ((x >> 16) ^ x) * 0x61C88647;
	x = ((x >> 16) ^ x) * 0x61C88647;
	x = ((x >> 16) ^ x) * 0x61C88647;
	x = (x >> 16) ^ x;

	return x;
}


static void checkers_render_fragment(til_module_context_t *context, til_stream_t *stream, unsigned ticks, unsigned cpu, til_fb_fragment_t **fragment_ptr)
{
	checkers_context_t	*ctxt = (checkers_context_t *)context;
	til_fb_fragment_t	*fragment = *fragment_ptr;

	uint32_t		color = ctxt->setup->color, flags = 0;
	checkers_fill_t		fill = ctxt->setup->fill;
	int			state;

	switch (ctxt->setup->pattern) {
	case CHECKERS_PATTERN_CHECKERED: {
		unsigned	tiles_per_row, row, col;

		tiles_per_row = fragment->frame_width / ctxt->setup->size;
		if (tiles_per_row * ctxt->setup->size < fragment->frame_width)
			tiles_per_row++;

		row = fragment->number / tiles_per_row;
		col = fragment->number % tiles_per_row;
		state = (row ^ col) & 0x1;
		break;
	}
	case CHECKERS_PATTERN_RANDOM:
		state = hash((context->seed + fragment->number) * 0x61C88647) & 0x1;
		break;
	}

	/* now that state has been determined, set the frame size */
	fragment->frame_width = ctxt->setup->size;
	fragment->frame_height = ctxt->setup->size;
	if (fragment->texture) {
		fragment->texture->frame_width = ctxt->setup->size;
		fragment->texture->frame_height = ctxt->setup->size;
	}

	switch (ctxt->setup->dynamics) {
	case CHECKERS_DYNAMICS_ODD:
		break;
	case CHECKERS_DYNAMICS_EVEN:
		state = ~state & 0x1;
		break;
	case CHECKERS_DYNAMICS_ALTERNATING:
		state ^= ((unsigned)((float)ticks * ctxt->setup->rate) & 0x1);
		break;
	case CHECKERS_DYNAMICS_RANDOM: /* note: the big multiply here is just to get up out of the low bits */
		state &= hash((context->seed + fragment->number) * 0x61C88647 + (unsigned)((float)ticks * ctxt->setup->rate)) & 0x1;
		break;
	}

	if (fill == CHECKERS_FILL_RANDOM || fill == CHECKERS_FILL_MIXED)
		fill = rand_r(&ctxt->til_module_context.seed) % CHECKERS_FILL_RANDOM; /* TODO: mixed should have a setting for controlling the ratios */

	switch (ctxt->setup->fill) {
	case CHECKERS_FILL_SAMPLED:
		if (fragment->cleared)
			color = til_fb_fragment_get_pixel_unchecked(fragment, fragment->x + (fragment->width >> 1), fragment->y + (fragment->height >> 1));
		break;
	case CHECKERS_FILL_TEXTURED:
		flags = TIL_FB_DRAW_FLAG_TEXTURABLE;
		break;
	case CHECKERS_FILL_COLOR:
	default:
		break;
	}

	if (!state)
		til_fb_fragment_clear(fragment);
	else {
		if (!ctxt->setup->fill_module)
			til_fb_fragment_fill(fragment, flags, color);
		else {
			/* TODO: we need a way to send down color and flags, and use the module render as a brush of sorts */
			til_module_render(ctxt->fill_module_contexts[cpu], stream, ticks, fragment_ptr);
		}
	}
}


/* TODO: migrate to libtil */
static char * checkers_random_color(unsigned seed)
{
	/* til should probably have a common randomize color helper for this with a large collection of
	 * reasonable colors, and maybe even have themed palettes one can choose from... */
	static const char *	colors[] = {
					"#ffffff",
					"#ff0000",
					"#00ff00",
					"#0000ff",
					"#ffff00",
					"#00ffff",
					"#ff00ff",
				};

	return strdup(colors[seed % nelems(colors)]);
}


/* TODO: migrate to libtil */
static int checkers_rgb_to_uint32(const char *in, uint32_t *out)
{
	uint32_t	color = 0;

	/* this isn't html, but accept #rrggbb syntax */
	if (*in == '#')
		in++;
	else if (in[0] == '0' && in[1] == 'x') /* and 0xrrggbb */
		in += 2;

	if (strlen(in) != 6)
		return -EINVAL;

	for (int i = 0; i < 6;) {
		uint8_t	c = 0;

		color <<= 8;

		for (int j = 0; j < 2; in++, j++, i++) {
			c <<= 4;

			switch (*in) {
			case '0'...'9':
				c |= (*in) - '0';
				break;

			case 'a'...'f':
				c |= (*in) - 'a' + 10;
				break;

			case 'A'...'F':
				c |= (*in) - 'A' + 10;
				break;

			default:
				return -EINVAL;
			}
		}

		color |= c;
	}

	*out = color;

	return 0;
}


static void checkers_setup_free(til_setup_t *setup)
{
	checkers_setup_t	*s = (checkers_setup_t *)setup;

	if (s) {
		til_setup_free(s->fill_module_setup);
		free(setup);
	}
}


static int checkers_setup(const til_settings_t *settings, til_setting_t **res_setting, const til_setting_desc_t **res_desc, til_setup_t **res_setup)
{
	const char		*size;
	const char		*pattern;
	const char		*fill_module;
	const til_settings_t	*fill_module_settings;
	til_setting_t		*fill_module_setting;
	const char		*dynamics;
	const char		*dynamics_rate;
	const char		*fill;
	const char		*color;
	const char		*size_values[] = {
					"4",
					"8",
					"16",
					"32",
					"64",
					"128",
					NULL
				};
	const char		*pattern_values[] = {
					"checkered",
					"random",
					NULL
				};
	const char		*fill_module_values[] = {
					"none",
					"blinds",
					"moire",
					"pixbounce",
					"plato",
					"roto",
					"shapes",
					"snow",
					"spiro",
					"stars",
					NULL
				};
	const char		*dynamics_values[] = {
					"odd",
					"even",
					"alternating",
					"random",
					NULL
				};
	const char		*dynamics_rate_values[] = {
					"1.0",
					".75",
					".5",
					".25",
					".1",
					".01",
					".001",
					".0001",
					NULL
				};
	const char		*fill_values[] = {
					"color",
					"sampled",
					"textured",
					"random",
					"mixed",
					NULL
				};
	int			r;

	r = til_settings_get_and_describe_value(settings,
						&(til_setting_spec_t){
							.name = "Checker size",
							.key = "size",
							.regex = "\\.[0-9]+",
							.preferred = TIL_SETTINGS_STR(CHECKERS_DEFAULT_SIZE),
							.values = size_values,
							.annotations = NULL
						},
						&size,
						res_setting,
						res_desc);
	if (r)
		return r;

	r = til_settings_get_and_describe_value(settings,
						&(til_setting_spec_t){
							.name = "Checkers pattern",
							.key = "pattern",
							.preferred = pattern_values[0],
							.values = pattern_values,
							.annotations = NULL
						},
						&pattern,
						res_setting,
						res_desc);
	if (r)
		return r;

	r = til_settings_get_and_describe_value(settings,
						&(til_setting_spec_t){
							.name = "Filled cell module (\"none\" for plain checkers)",
							.key = "fill_module",
							.preferred = fill_module_values[0],
							.values = fill_module_values,
							.annotations = NULL,
							.as_nested_settings = 1,
						},
						&fill_module, /* XXX: this isn't really of direct use now that it's a potentially full-blown settings string, see fill_module_settings */
						res_setting,
						res_desc);
	if (r)
		return r;

	/* hmm, we're about to assume this is always valid, so let's assert that's actually the case...
	 * looking at til_settings_get_and_describe_value() it looks to be optional there.
	 * TODO: since _we_ require the res_setting, we should provide a temporary place for it
	 * here if it's not guaranteed, then copy it into *res_setting if wanted.
	 */
	assert(res_setting && *res_setting);
	assert((*res_setting)->value_as_nested_settings);

	fill_module_settings = (*res_setting)->value_as_nested_settings;
	fill_module = til_settings_get_value_by_idx(fill_module_settings, 0, &fill_module_setting);
	if (!fill_module)
		return -EINVAL;

	if (!fill_module_setting->desc) {
		r = til_setting_desc_new(fill_module_settings,
					&(til_setting_spec_t){
						.name = "Filled cell module name",
						.preferred = "none",
						.as_label = 1,
					},
					res_desc);
		if (r < 0)
			return r;

		*res_setting = fill_module_setting;

		return 1;
	}

	if (strcasecmp(fill_module, "none")) {
		const til_module_t	*mod = til_lookup_module(fill_module);

		if (!mod)
			return -EINVAL;

		if (mod->setup) {
			r = mod->setup(fill_module_settings, res_setting, res_desc, NULL);
			if (r)
				return r;

			/* XXX: note no res_setup was provided, so while yes the fill_module_settings are
			 * fully populated according to the setup's return value, we don't yet have the baked
			 * setup.  That occurs below while baking the checkers res_setup.
			 */
		}
	}
	/* TODO: here we would do nested setup for fill_module via (*res_setting)->settings until that returned 0.
	 * It seems like we would just leave res_setup NULL for nested setup at this phase, then in our if (res_setup)
	 * branch down below the nested setup would have to recur with res_setup supplied but pointing at a nested
	 * setup pointer hosted within this setup. */

	/* XXX but this is a little more complicated than simply performing exhaustive setup of the fill_module here.
	 * it's kind of like how in scripting languages you have two forms of interpolation while initializing variables;
	 * there's the immediate interpolation at the time of intialization, and there's the delayed interpolation at
	 * time of variable use.  i.e. foo="$a $b" vs. foo:="$a $b", the latter may expand $a and $b once and store the
	 * result in foo, whereas the former may just store the literal "$a $b" in foo and (re)perform the expansion of
	 * $a and $b every time $foo is evaluated.
	 *
	 * should every module have its own separate setting or snowflake syntax to control exhaustive setup up-front
	 * vs. just taking what settings have been provided and randomizing what's not provided at context create time?
	 * in the case of checkers::fill_module it's arguably less relevant (though the current frontends don't have a
	 * randomize settings feature, so if exhaustive setup of fill_module were forced up-front using the existing frontends
	 * would mean loss of fill_module randomized setup)  But that might be O.K. for something like checkers::fill_module
	 *
	 * where it becomes more relevant is something like rtv::channels, where the single rtv instance will repeatedly
	 * create+destroy channel contexts using the supplied (or omitted) settings.  We may want to have the settings
	 * exhaustively applied once when rtv was intantiated, giving the frontend setup opportunity to explicitly configure
	 * the channels exhaustively.  Or we may want to just partially specify some settings for the channels, and for
	 * the settings non-specified use the defaults OR for those unsepcified have them be randomized.  Then there's the
	 * question of should the randomizing recur for every channel switch or just occur once at time of rtv instantiation?
	 * 
	 * it feels like the module settings syntax should probably control this behavior in a general way so the modules don't
	 * start inventing their own approaches.  Perhaps the module name at the start of the settings string could simply have
	 * an optional leading modifier character from a reserved set module names can't start with.  Someting like %modulename
	 * for randomizing unspecified settings, : for evaluating settings just once up-front then reusing them, @modulename for
	 * accepting defaults for anything unspecified, and !modulename to fail if any settings aren't specified.  Maybe also
	 * something like * for making the current qualifiers apply recursively.
	 */

	r = til_settings_get_and_describe_value(settings,
						&(til_setting_spec_t){
							.name = "Checkers dynamics",
							.key = "dynamics",
							.preferred = dynamics_values[0],
							.values = dynamics_values,
							.annotations = NULL
						},
						&dynamics,
						res_setting,
						res_desc);
	if (r)
		return r;

	if (strcasecmp(dynamics, "odd") && strcasecmp(dynamics, "even")) {
		r = til_settings_get_and_describe_value(settings,
							&(til_setting_spec_t){
								.name = "Checkers dynamics rate",
								.key = "dynamics_rate",
								.preferred = dynamics_rate_values[0],
								.values = dynamics_rate_values,
								.annotations = NULL
							},
							&dynamics_rate,
							res_setting,
							res_desc);
		if (r)
			return r;
	}

	r = til_settings_get_and_describe_value(settings,
						&(til_setting_spec_t){
							.name = "Fill mode",
							.key = "fill",
							.preferred = fill_values[CHECKERS_DEFAULT_FILL],
							.values = fill_values,
							.annotations = NULL
						},
						&fill,
						res_setting,
						res_desc);
	if (r)
		return r;

	/* Even though sampled and textured fills don't neceesarily use the color,
	 * if there's no texture or no underlay to sample, we should have a color to fallback on.
	 */
	r = til_settings_get_and_describe_value(settings,
						&(til_setting_spec_t){
							.name = "Fill color",
							.key = "color",
							.preferred = TIL_SETTINGS_STR(CHECKERS_DEFAULT_COLOR),
							.random = checkers_random_color,
							.values = NULL,
							.annotations = NULL
						},
						&color,
						res_setting,
						res_desc);
	if (r)
		return r;

	if (res_setup) {
		checkers_setup_t	*setup;

		setup = til_setup_new(settings, sizeof(*setup), checkers_setup_free);
		if (!setup)
			return -ENOMEM;

		sscanf(size, "%u", &setup->size);

		if (!strcasecmp(pattern, "checkered"))
			setup->pattern = CHECKERS_PATTERN_CHECKERED;
		else if (!strcasecmp(pattern, "random"))
			setup->pattern = CHECKERS_PATTERN_RANDOM;
		else {
			til_setup_free(&setup->til_setup);
			return -EINVAL;
		}

		if (strcasecmp(fill_module, "none")) {
			setup->fill_module = til_lookup_module(fill_module);
			if (!setup->fill_module) {
				til_setup_free(&setup->til_setup);
				return -ENOMEM;
			}

			r = til_module_setup_finalize(setup->fill_module, fill_module_settings, &setup->fill_module_setup);
			if (r < 0) {
				til_setup_free(&setup->til_setup);
				return r;
			}
		}

		if (!strcasecmp(dynamics, "odd"))
			setup->dynamics = CHECKERS_DYNAMICS_ODD;
		else if (!strcasecmp(dynamics, "even"))
			setup->dynamics = CHECKERS_DYNAMICS_EVEN;
		else if (!strcasecmp(dynamics, "alternating"))
			setup->dynamics = CHECKERS_DYNAMICS_ALTERNATING;
		else if (!strcasecmp(dynamics, "random"))
			setup->dynamics = CHECKERS_DYNAMICS_RANDOM;
		else {
			til_setup_free(&setup->til_setup);
			return -EINVAL;
		}

		if (setup->dynamics != CHECKERS_DYNAMICS_ODD && setup->dynamics != CHECKERS_DYNAMICS_EVEN)
			sscanf(dynamics_rate, "%f", &setup->rate);

		if (!strcasecmp(fill, "color"))
			setup->fill = CHECKERS_FILL_COLOR;
		else if (!strcasecmp(fill, "sampled"))
			setup->fill = CHECKERS_FILL_SAMPLED;
		else if (!strcasecmp(fill, "textured"))
			setup->fill = CHECKERS_FILL_TEXTURED;
		else if (!strcasecmp(fill, "random"))
			setup->fill = CHECKERS_FILL_RANDOM;
		else if (!strcasecmp(fill, "mixed"))
			setup->fill = CHECKERS_FILL_MIXED;
		else {
			til_setup_free(&setup->til_setup);
			return -EINVAL;
		}

		r = checkers_rgb_to_uint32(color, &setup->color);
		if (r < 0)
			return r;

		*res_setup = &setup->til_setup;
	}

	return 0;
}


til_module_t	checkers_module = {
	.create_context = checkers_create_context,
	.destroy_context = checkers_destroy_context,
	.prepare_frame = checkers_prepare_frame,
	.render_fragment = checkers_render_fragment,
	.setup = checkers_setup,
	.name = "checkers",
	.description = "Checker-patterned overlay (threaded)",
	.author = "Vito Caputo <vcaputo@pengaru.com>",
	.flags = TIL_MODULE_OVERLAYABLE,
};
© All Rights Reserved