summaryrefslogtreecommitdiff
path: root/src/til_builtins.c
blob: 4a011cc353a4244c0288f2e802790e9f7351e8aa (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
#include "til.h"
#include "til_fb.h"
#include "til_module_context.h"
#include "til_settings.h"
#include "til_stream.h"

#include "libs/txt/txt.h" /* for rendering some diagnostics (ref built-in)) */


/* "blank" built-in module */
typedef struct _blank_setup_t {
	til_setup_t	til_setup;
	unsigned	force:1;
} _blank_setup_t;

static void _blank_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)
{
	*res_frame_plan = (til_frame_plan_t){ .fragmenter = til_fragmenter_slice_per_cpu };

	if (((_blank_setup_t *)context->setup)->force)
		(*fragment_ptr)->cleared = 0;
}


static void _blank_render_fragment(til_module_context_t *context, til_stream_t *stream, unsigned ticks, unsigned cpu, til_fb_fragment_t **fragment_ptr)
{
	til_fb_fragment_clear(*fragment_ptr);
}


static int blank_setup(const til_settings_t *settings, til_setting_t **res_setting, const til_setting_desc_t **res_desc, til_setup_t **res_setup);


til_module_t	_blank_module = {
	.prepare_frame = _blank_prepare_frame,
	.render_fragment = _blank_render_fragment,
	.setup = blank_setup,
	.name = "blank",
	.description = "Blanker (built-in)",
	.author = "built-in",
	.flags = TIL_MODULE_BUILTIN,
};


static int blank_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	*values[] = {
				"off",
				"on",
				NULL
			};
	til_setting_t	*force;
	int		r;

	r = til_settings_get_and_describe_setting(settings,
						&(til_setting_spec_t){
							.name = "Force clearing",
							.key = "force",
							.regex = NULL,
							.preferred = values[0],
							.values = values,
							.annotations = NULL
						},
						&force,
						res_setting,
						res_desc);
	if (r)
		return r;

	if (res_setup) {
		_blank_setup_t	*setup;

		setup = til_setup_new(settings, sizeof(*setup), NULL, &_blank_module);
		if (!setup)
			return -ENOMEM;

		if (!strcasecmp(force->value, "on"))
			setup->force = 1;

		*res_setup = &setup->til_setup;
	}

	return 0;
}


/* "noop" built-in module */
til_module_t	_noop_module = {
	.name = "noop",
	.description = "Nothing-doer (built-in)",
	.author = "built-in",
	.flags = TIL_MODULE_BUILTIN,
};


/* "ref" built-in module */
typedef struct _ref_setup_t {
	til_setup_t		til_setup;

	char			*path;
} _ref_setup_t;


typedef struct _ref_context_t {
	til_module_context_t	til_module_context;

	til_module_context_t	*ref;
} _ref_context_t;


static til_module_context_t * _ref_create_context(const til_module_t *module, til_stream_t *stream, unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
{
	_ref_context_t	*ctxt;

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

	return &ctxt->til_module_context;
}


static void _ref_destroy_context(til_module_context_t *context)
{
	_ref_context_t	*ctxt = (_ref_context_t *)context;

	ctxt->ref = til_module_context_free(ctxt->ref);
	free(context);
}


static void _ref_render_proxy(til_module_context_t *context, til_stream_t *stream, unsigned ticks, til_fb_fragment_t **fragment_ptr)
{
	_ref_context_t	*ctxt = (_ref_context_t *)context;
	_ref_setup_t	*s = (_ref_setup_t *)context->setup;

	if (!ctxt->ref) {
		int	r;

		/* TODO: switch to til_stream_find_module_context(), this clones concept is DOA. */
		r = til_stream_find_module_contexts(stream, s->path, 1, &ctxt->ref);
		if (r < 0) {
			txt_t	*msg = txt_newf("%s: BAD PATH \"%s\"", context->setup->path, s->path);

			til_fb_fragment_clear(*fragment_ptr);
			txt_render_fragment_aligned(msg, *fragment_ptr, 0xffffffff,
						    0, 0,
						    (txt_align_t){
							.horiz = TXT_HALIGN_LEFT,
							.vert = TXT_VALIGN_TOP,
						    });
			txt_free(msg);
			/* TODO: maybe print all available contexts into the fragment? */
			return;
		}
	}

	til_module_render_limited(ctxt->ref, stream, ticks, context->n_cpus, fragment_ptr);
}


static void _ref_setup_free(til_setup_t *setup)
{
	_ref_setup_t	*s = (_ref_setup_t *)setup;

	free(s->path);
	free(s);
}


static int _ref_setup(const til_settings_t *settings, til_setting_t **res_setting, const til_setting_desc_t **res_desc, til_setup_t **res_setup);


til_module_t	_ref_module = {
	.create_context = _ref_create_context,
	.destroy_context = _ref_destroy_context,
	.render_proxy = _ref_render_proxy,
	.setup = _ref_setup,
	.name = "ref",
	.description = "Context referencer (built-in)",
	.author = "built-in",
	.flags = TIL_MODULE_BUILTIN,
};


static int _ref_setup(const til_settings_t *settings, til_setting_t **res_setting, const til_setting_desc_t **res_desc, til_setup_t **res_setup)
{
	til_setting_t	*path;
	int		r;

	r = til_settings_get_and_describe_setting(settings,
						&(til_setting_spec_t){
							.name = "Context path to reference",
							.key = "path",
							.regex = "[a-zA-Z0-9/_]+",
							.preferred = "",
						},
						&path,
						res_setting,
						res_desc);
	if (r)
		return r;

	if (res_setup) {
		_ref_setup_t    *setup;

		setup = til_setup_new(settings, sizeof(*setup), _ref_setup_free, &_ref_module);
		if (!setup)
			return -ENOMEM;

		setup->path = strdup(path->value);
		if (!setup->path) {
			til_setup_free(&setup->til_setup);

			return -ENOMEM;
	       }

		*res_setup = &setup->til_setup;
	}

	return 0;
}


/* "none" built-in module */
static int _none_setup(const til_settings_t *settings, til_setting_t **res_setting, const til_setting_desc_t **res_desc, til_setup_t **res_setup);


til_module_t	_none_module = {
	.setup = _none_setup,
	.name = "none",
	.description = "Disabled (built-in)",
	.author = "built-in",
	.flags = TIL_MODULE_BUILTIN,
};


static int _none_setup(const til_settings_t *settings, til_setting_t **res_setting, const til_setting_desc_t **res_desc, til_setup_t **res_setup)
{
	if (res_setup)
		*res_setup = NULL;

	return 0;
}


/* "pre" built-in module */
typedef struct _pre_setup_t {
	til_setup_t		til_setup;

	til_setup_t		*module_setup;
} _pre_setup_t;


typedef struct _pre_context_t {
	til_module_context_t	til_module_context;

	til_module_context_t	*module_ctxt;
} _pre_context_t;


#define _PRE_DEFAULT_MODULE	"none"


static til_module_context_t * _pre_create_context(const til_module_t *module, til_stream_t *stream, unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
{
	_pre_setup_t	*s = (_pre_setup_t *)setup;
	_pre_context_t	*ctxt;

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

	if (s->module_setup) {
		const til_module_t	*m = s->module_setup->creator;

		if (til_module_create_context(m, stream, rand_r(&seed), ticks, n_cpus, s->module_setup, &ctxt->module_ctxt) < 0)
			return til_module_context_free(&ctxt->til_module_context);
	}

	if (til_stream_add_pre_module_context(stream, &ctxt->til_module_context) < 0)
		return til_module_context_free(&ctxt->til_module_context);

	return &ctxt->til_module_context;
}


static void _pre_destroy_context(til_module_context_t *context)
{
	_pre_context_t	*ctxt = (_pre_context_t *)context;

	til_stream_del_pre_module_context(context->stream, context);
	til_module_context_free(ctxt->module_ctxt);
	free(context);
}


static void _pre_render_proxy(til_module_context_t *context, til_stream_t *stream, unsigned ticks, til_fb_fragment_t **fragment_ptr)
{
	_pre_context_t	*ctxt = (_pre_context_t *)context;

	/* TODO: introduce taps toggling the render */

	if (ctxt->module_ctxt)
		til_module_render(ctxt->module_ctxt, stream, ticks, fragment_ptr);
}


static void _pre_setup_free(til_setup_t *setup)
{
	_pre_setup_t	*s = (_pre_setup_t *)setup;

	til_setup_free(s->module_setup);
	free(s);
}


static int _pre_setup(const til_settings_t *settings, til_setting_t **res_setting, const til_setting_desc_t **res_desc, til_setup_t **res_setup);


til_module_t	_pre_module = {
	.create_context = _pre_create_context,
	.destroy_context = _pre_destroy_context,
	.render_proxy = _pre_render_proxy,
	.setup = _pre_setup,
	.name = "pre",
	.description = "Pre-render hook registration (built-in)",
	.author = "built-in",
	.flags = TIL_MODULE_BUILTIN,
};


static int _pre_module_setup(const til_settings_t *settings, til_setting_t **res_setting, const til_setting_desc_t **res_desc, til_setup_t **res_setup)
{
	return til_module_setup_full(settings,
				     res_setting,
				     res_desc,
				     res_setup,
				     "Pre-rendering module name",
				     _PRE_DEFAULT_MODULE,
				     (TIL_MODULE_EXPERIMENTAL | TIL_MODULE_HERMETIC),
				     NULL);
}


static int _pre_setup(const til_settings_t *settings, til_setting_t **res_setting, const til_setting_desc_t **res_desc, til_setup_t **res_setup)
{
	const til_settings_t	*module_settings;
	til_setting_t		*module;
	int			r;

	r = til_settings_get_and_describe_setting(settings,
						&(til_setting_spec_t){
							.name = "Module to hook for pre-rendering",
							.key = "module",
							.preferred = _PRE_DEFAULT_MODULE,
							.as_nested_settings = 1,
							.as_label = 1,
						},
						&module,
						res_setting,
						res_desc);
	if (r)
		return r;

	module_settings = module->value_as_nested_settings;
	assert(module_settings);

	r = _pre_module_setup(module_settings,
			      res_setting,
			      res_desc,
			      NULL); /* XXX: note no res_setup, must defer finalize */
	if (r)
		return r;

	if (res_setup) {
		_pre_setup_t    *setup;

		setup = til_setup_new(settings, sizeof(*setup), _pre_setup_free, &_pre_module);
		if (!setup)
			return -ENOMEM;

		r = _pre_module_setup(module_settings,
				      res_setting,
				      res_desc,
				      &setup->module_setup); /* finalize! */
		if (r < 0)
			return til_setup_free_with_ret_err(&setup->til_setup, r);

		assert(r == 0);

		*res_setup = &setup->til_setup;
	}

	return 0;
}
© All Rights Reserved