summaryrefslogtreecommitdiff
path: root/src/modules/blinds/blinds.c
blob: aa71ddd5daa579673f5b5edcabc1a3da7a48163c (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
#include <errno.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"

/* 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;
	blinds_setup_t		setup;
} blinds_context_t;


static blinds_setup_t blinds_default_setup = {
	.count = BLINDS_DEFAULT_COUNT,
	.orientation = BLINDS_DEFAULT_ORIENTATION,
};


static til_module_context_t * blinds_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
{
	blinds_context_t	*ctxt;

	if (!setup)
		setup = &blinds_default_setup.til_setup;

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

	ctxt->setup = *(blinds_setup_t *)setup;

	return &ctxt->til_module_context;
}


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

/* XXX FIXME: use faster block style fill/copy if til_fb gets that */
	for (unsigned y = 0; y < height; y++) {
		for (unsigned x = 0; x < fragment->width; x++)
			til_fb_fragment_put_pixel_checked(fragment, TIL_FB_DRAW_FLAG_TEXTURABLE, fragment->x + x, fragment->y + y + row * row_height, 0xffffffff); /* FIXME: use _unchecked() variant, but must not assume frame_height == height */
	}
}


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

/* XXX FIXME: use faster block style fill/copy if til_fb gets that */
	for (unsigned y = 0; y < fragment->height; y++) {
		for (unsigned x = 0; x < width; x++)
			til_fb_fragment_put_pixel_checked(fragment, TIL_FB_DRAW_FLAG_TEXTURABLE, fragment->x + x + column * column_width, fragment->y + y, 0xffffffff); /* FIXME: use _unchecked() variant, but must not assume frame_width == width */
	}
}


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

	static float rr;

	unsigned	blind;
	float		r;

	til_fb_fragment_clear(fragment);

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

	rr += .01;
}


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)
{
	const char	*orientation;
	const char	*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_value(settings,
						&(til_setting_desc_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_value(settings,
						&(til_setting_desc_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(sizeof(*setup), (void(*)(til_setup_t *))free);
		if (!setup)
			return -ENOMEM;

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

		if (!strcasecmp(orientation, "horizontal")) {
			setup->orientation = BLINDS_ORIENTATION_HORIZONTAL;
		} else if (!strcasecmp(orientation, "vertical")) {
			setup->orientation = BLINDS_ORIENTATION_VERTICAL;
		} else {
			til_setup_free(&setup->til_setup);

			return -EINVAL;
		}

		*res_setup = &setup->til_setup;
	}

	return 0;
}


til_module_t	blinds_module = {
	.create_context = blinds_create_context,
	.render_fragment = blinds_render_fragment,
	.setup = blinds_setup,
	.name = "blinds",
	.description = "Retro 80s-inspired window blinds",
	.author = "Vito Caputo <vcaputo@pengaru.com>",
	.flags = TIL_MODULE_OVERLAYABLE,
};
© All Rights Reserved