summaryrefslogtreecommitdiff
path: root/src/modules/blinds/blinds.c
blob: 11e0e302d0962fc168dc91e5bef87fbd701f8b3a (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
#include <errno.h>
#include <inttypes.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

/* Copyright (C) 2017-2022 Vito Caputo <vcaputo@pengaru.com> */

#define	BLINDS_DEFAULT_COUNT		16
#define BLINDS_DEFAULT_ORIENTATION	BLINDS_ORIENTATION_HORIZONTAL


typedef enum blinds_orientation_t {
	BLINDS_ORIENTATION_HORIZONTAL,
	BLINDS_ORIENTATION_VERTICAL,
} blinds_orientation_t;

typedef struct blinds_setup_t {
	til_setup_t		til_setup;
	unsigned		count;
	blinds_orientation_t	orientation;
} blinds_setup_t;

typedef struct blinds_context_t {
	til_module_context_t	til_module_context;
	struct {
		til_tap_t	t, step, count;
	} taps;

	struct {
		float		t, step;
		uint8_t		count;
	} vars;

	float			*t, *step;
	uint8_t			*count;
	blinds_setup_t		*setup;
} blinds_context_t;


static void blinds_update_taps(blinds_context_t *ctxt, til_stream_t *stream, unsigned ticks)
{
	if (!til_stream_tap_context(stream, &ctxt->til_module_context, NULL, &ctxt->taps.t))
		*ctxt->t = til_ticks_to_rads(ticks);
	else
		ctxt->vars.t = *ctxt->t;

	if (!til_stream_tap_context(stream, &ctxt->til_module_context, NULL, &ctxt->taps.step))
		*ctxt->step = .1f;
	else
		ctxt->vars.step = *ctxt->step;

	if (!til_stream_tap_context(stream, &ctxt->til_module_context, NULL, &ctxt->taps.count))
		*ctxt->count = ctxt->setup->count;
	else
		ctxt->vars.count = *ctxt->count;
}


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

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

	ctxt->taps.t = til_tap_init_float(ctxt, &ctxt->t, 1, &ctxt->vars.t, "T");
	ctxt->taps.step = til_tap_init_float(ctxt, &ctxt->step, 1, &ctxt->vars.step, "step");
	ctxt->taps.count = til_tap_init_u8(ctxt, &ctxt->count, 1, &ctxt->vars.count, "count");

	ctxt->setup = (blinds_setup_t *)setup;

	blinds_update_taps(ctxt, stream, ticks);

	return &ctxt->til_module_context;
}


static void blinds_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)
{
	blinds_context_t	*ctxt = (blinds_context_t *)context;

	*res_frame_plan = (til_frame_plan_t){ .fragmenter = til_fragmenter_tile64 };

	blinds_update_taps(ctxt, stream, ticks);
}


/* draw a horizontal blind over fragment */
static void draw_blind_horizontal(til_fb_fragment_t *fragment, unsigned row, unsigned count, float t)
{
	float		row_height = fragment->frame_height / (float)count;
	unsigned	height = roundf(t * row_height);
	unsigned	row_y = row * row_height;

	if (row_y >= fragment->y + fragment->height)
		return;

	if (row_y + height <= fragment->y)
		return;

	{
		unsigned	ystart = MAX(row_y, fragment->y);
		unsigned	yend = MIN(row_y + height, fragment->y + fragment->height);
		unsigned	xstart = fragment->x;
		unsigned	xend = fragment->x + fragment->width;

		for (unsigned y = ystart; y < yend; y++) {
			/* XXX FIXME: use faster block style fill/copy if til_fb gets that */
			for (unsigned x = xstart; x < xend; x++) {
				til_fb_fragment_put_pixel_unchecked(fragment,
								    TIL_FB_DRAW_FLAG_TEXTURABLE,
								    x, y, 0xffffffff);
			}
		}
	}
}


/* draw a vertical blind over fragment */
static void draw_blind_vertical(til_fb_fragment_t *fragment, unsigned column, unsigned count, float t)
{
	float		column_width = fragment->frame_width / (float)count;
	unsigned	width = roundf(t * column_width);
	unsigned	column_x = column * column_width;

	if (column_x >= fragment->x + fragment->width)
		return;

	if (column_x + width <= fragment->x)
		return;

	{
		unsigned	xstart = MAX(column_x, fragment->x);
		unsigned	xend = MIN(column_x + width, fragment->x + fragment->width);
		unsigned	ystart = fragment->y;
		unsigned	yend = fragment->y + fragment->height;

		for (unsigned y = ystart; y < yend; y++) {
			/* XXX FIXME: use faster block style fill/copy if/when til_fb gets that */
			for (unsigned x = xstart; x < xend; x++) {
				til_fb_fragment_put_pixel_unchecked(fragment,
								    TIL_FB_DRAW_FLAG_TEXTURABLE,
								    x, y, 0xffffffff);
			}
		}
	}
}


/* draw blinds over the fragment */
static void blinds_render_fragment(til_module_context_t *context, til_stream_t *stream, unsigned ticks, unsigned cpu, til_fb_fragment_t **fragment_ptr)
{
	blinds_context_t	*ctxt = (blinds_context_t *)context;
	til_fb_fragment_t	*fragment = *fragment_ptr;

	unsigned	blind;
	float		t;

	til_fb_fragment_clear(fragment);

	for (t = *ctxt->t, blind = 0; blind < ctxt->vars.count; blind++, t += *ctxt->step) {
		switch (ctxt->setup->orientation) {
		case BLINDS_ORIENTATION_HORIZONTAL:
			draw_blind_horizontal(fragment, blind, ctxt->vars.count, 1.f - fabsf(cosf(t)));
			break;
		case BLINDS_ORIENTATION_VERTICAL:
			draw_blind_vertical(fragment, blind, ctxt->vars.count, 1.f - fabsf(cosf(t)));
			break;
		}
	}
}


static int blinds_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	blinds_module = {
	.create_context = blinds_create_context,
	.prepare_frame = blinds_prepare_frame,
	.render_fragment = blinds_render_fragment,
	.setup = blinds_setup,
	.name = "blinds",
	.description = "Retro 80s-inspired window blinds (threaded)",
	.author = "Vito Caputo <vcaputo@pengaru.com>",
	.flags = TIL_MODULE_OVERLAYABLE,
};


static int blinds_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	*orientation;
	til_setting_t	*count;
	const char	*orientation_values[] = {
				"horizontal",
				"vertical",
				NULL
			};
	const char	*count_values[] = {
				"2",
				"4",
				"8",
				"12",
				"16",
				"24",
				"32",
				NULL
			};
	int		r;

	r = til_settings_get_and_describe_setting(settings,
						&(til_setting_spec_t){
							.name = "Blinds orientation",
							.key = "orientation",
							.regex = "^(horizontal|vertical)",
							.preferred = orientation_values[BLINDS_DEFAULT_ORIENTATION],
							.values = orientation_values,
							.annotations = NULL
						},
						&orientation,
						res_setting,
						res_desc);
	if (r)
		return r;

	r = til_settings_get_and_describe_setting(settings,
						&(til_setting_spec_t){
							.name = "Blinds count",
							.key = "count",
							.regex = "\\.[0-9]+",
							.preferred = TIL_SETTINGS_STR(BLINDS_DEFAULT_COUNT),
							.values = count_values,
							.annotations = NULL
						},
						&count,
						res_setting,
						res_desc);
	if (r)
		return r;

	if (res_setup) {
		blinds_setup_t	*setup;

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

		if (sscanf(count->value, "%u", &setup->count) != 1)
			return til_setup_free_with_failed_setting_ret_err(&setup->til_setup, count, res_setting, -EINVAL);

		if (setup->count > 255) /* the tap is a u8 */
			return til_setup_free_with_failed_setting_ret_err(&setup->til_setup, count, res_setting, -EINVAL);

		if (!strcasecmp(orientation->value, "horizontal"))
			setup->orientation = BLINDS_ORIENTATION_HORIZONTAL;
		else if (!strcasecmp(orientation->value, "vertical"))
			setup->orientation = BLINDS_ORIENTATION_VERTICAL;
		else
			return til_setup_free_with_failed_setting_ret_err(&setup->til_setup, orientation, res_setting, -EINVAL);

		*res_setup = &setup->til_setup;
	}

	return 0;
}
© All Rights Reserved