aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFederico Angelilli <code@fedang.net>2024-09-21 00:23:25 +0200
committerFederico Angelilli <code@fedang.net>2024-09-21 00:23:25 +0200
commit09190056721872069a82db51d97de6ab60f73a40 (patch)
tree39afa0488d680cfda870a109e5d3d5dbdfa3d684 /src
parentae59c294bfd4b73f6e751a3103c2ee7501068492 (diff)
Allow user to specify effect
Diffstat (limited to 'src')
-rw-r--r--src/block.h1
-rw-r--r--src/config.c56
-rw-r--r--src/effect.c50
-rw-r--r--src/effect.h4
-rw-r--r--src/effects/pulse.c4
-rw-r--r--src/event.c8
-rw-r--r--src/util.c7
-rw-r--r--src/util.h2
8 files changed, 73 insertions, 59 deletions
diff --git a/src/block.h b/src/block.h
index d38a79d..1ec62b4 100644
--- a/src/block.h
+++ b/src/block.h
@@ -50,6 +50,7 @@ struct block {
block_update_t update;
block_finalize_t finalize;
effect_t *effect;
+ effect_info_t *info;
action_t *right_click_action;
action_t *middle_click_action;
action_t *left_click_action;
diff --git a/src/config.c b/src/config.c
index 54d32eb..8955359 100644
--- a/src/config.c
+++ b/src/config.c
@@ -37,15 +37,16 @@ static const config_entry_t effect_entries[] = {
};
static const config_entry_t block_entries[] = {
- { "hidden", CONFIG_BOOL, NULL, offsetof(block_t, hidden) },
- { "color", CONFIG_COLOR, NULL, offsetof(block_t, color) },
- { "line-color", CONFIG_COLOR, NULL, offsetof(block_t, line_color) },
- { "line-width", CONFIG_UINT, NULL, offsetof(block_t, line_width) },
- { "x-padding", CONFIG_UINT, NULL, offsetof(block_t, x_padding) },
- { "y-padding", CONFIG_UINT, NULL, offsetof(block_t, y_padding) },
- { "min-width", CONFIG_UINT, NULL, offsetof(block_t, min_width) },
- { "max-width", CONFIG_UINT, NULL, offsetof(block_t, max_width) },
- { "interval", CONFIG_TIME, NULL, offsetof(block_t, update_interval) },
+ { "hidden", CONFIG_BOOL, NULL, offsetof(block_t, hidden) },
+ { "color", CONFIG_COLOR, NULL, offsetof(block_t, color) },
+ { "line-color", CONFIG_COLOR, NULL, offsetof(block_t, line_color) },
+ { "line-width", CONFIG_UINT, NULL, offsetof(block_t, line_width) },
+ { "x-padding", CONFIG_UINT, NULL, offsetof(block_t, x_padding) },
+ { "y-padding", CONFIG_UINT, NULL, offsetof(block_t, y_padding) },
+ { "min-width", CONFIG_UINT, NULL, offsetof(block_t, min_width) },
+ { "max-width", CONFIG_UINT, NULL, offsetof(block_t, max_width) },
+ { "interval", CONFIG_TIME, NULL, offsetof(block_t, update_interval) },
+ { "effect", CONFIG_STRING, NULL, offsetof(block_t, info) },
{ 0 },
};
@@ -479,7 +480,7 @@ void config_read(config_t *config, FILE *file)
assert(config->infos != NULL);
info = &config->infos[config->n_infos - 1];
- char *label = strcopy(section + 6);
+ char *label = strcopy(section + 7);
if (label == NULL || *label == '\0') {
++errors;
log_value_error("Effect section must have a valid label",
@@ -628,9 +629,35 @@ skip_pair:
log_panic("Config file contained %d errors", n_errors);
}
+static bool config_resolve_effect(config_t *config, block_t *block)
+{
+ if (block->info == NULL)
+ return true;
+
+ char *effect = (void *)block->info;
+ block->info = NULL;
+
+ for (size_t i = 0; i < config->n_infos; i++) {
+ if (!strcmp(effect, config->infos[i].label)) {
+ block->info = &config->infos[i];
+ free(effect);
+ return true;
+ }
+ }
+
+
+ log_error("Effect '%s' not found", effect);
+ free(effect);
+ return false;
+}
+
static bool config_resolve_children(config_t *config, block_t *block)
{
block->resolved = true;
+
+ if (!config_resolve_effect(config, block))
+ return false;
+
if (block->type == BLOCK_TEXT)
return true;
@@ -655,23 +682,28 @@ static bool config_resolve_children(config_t *config, block_t *block)
if (!strcmp(children[i], config->blocks[j].label)) {
if (config->blocks[j].resolved) {
log_error("Block '%s' can only be referenced by one block", config->blocks[j].label);
+ strvfree(children);
return false;
}
- log_debug("Block '%s' is parent of '%s'", block->label, config->blocks[j].label);
block->group.children[i] = &config->blocks[j];
+ log_debug("Block '%s' is parent of '%s'", block->label, config->blocks[j].label);
- if (!config_resolve_children(config, &config->blocks[j]))
+ if (!config_resolve_children(config, &config->blocks[j])) {
+ strvfree(children);
return false;
+ }
goto next;
}
}
+ strvfree(children);
log_error("Block '%s' not found", children[i]);
return false;
next:
}
+ strvfree(children);
return true;
}
diff --git a/src/effect.c b/src/effect.c
index 9266979..65daad1 100644
--- a/src/effect.c
+++ b/src/effect.c
@@ -4,49 +4,13 @@
#include "effect.h"
#include "any_log.h"
-
-//double cubic_bezier(double x, double a, double b, double c, double d)
-//{
-// const double t = 1 - x;
-// return a * (t * t * t) + 3 * b * (t * t * x) + 3 * c * (t * x * x) + d * (x * x * x);
-//}
-//
-//static void effect_pulse_pre(effect_t *effect, layout_t *layout, cairo_t *cr)
-//{
-// struct timespec now;
-// timespec_get(&now, TIME_UTC);
-//
-// // After half the duration we invert direction
-// struct timespec midpoint = timespec_div(effect->duration, 2);
-// struct timespec diff = timespec_diff(now, effect->start);
-//
-// double t = timespec_greater(midpoint, diff)
-// ? (double)timespec_to_ms(diff) / timespec_to_ms(midpoint)
-// : 1.0 - (double)timespec_to_ms(timespec_diff(diff, midpoint)) / timespec_to_ms(midpoint);
-//
-// // Make it customizable
-// double s = cubic_bezier(t, 0.19, 1.0, 0.22, 1.0);
-//
-// // Make it a parameter
-// const double amplitude = 0.12;
-//
-// // FIXME: The intent was to scale the animation for long blocks, but it needs more love
-// //
-// int x_max = amplitude * (layout->height + (layout->width / (double)layout->height) - 1);
-// int y_max = amplitude * layout->height;
-//
-// layout->x_padding = layout->block->x_padding + x_max * s;
-// layout->y_padding = layout->block->y_padding + y_max * s;
-//}
-//
-//effect_t *effect_pulse(struct timespec duration)
-//{
-// effect_t *effect = calloc(1, sizeof(effect_t));
-// effect->type = EFFECT_PULSE;
-// effect->duration = duration;
-// effect->pre = effect_pulse_pre;
-// return effect;
-//}
+void effect_init(effect_t *effect, const effect_info_t *info)
+{
+ assert(effect != NULL);
+ effect->info = info;
+ effect->start = timespec_from_ms(0);
+ effect->next = NULL;
+}
void effect_info_free(effect_info_t *info)
{
diff --git a/src/effect.h b/src/effect.h
index 980d339..b5f4b32 100644
--- a/src/effect.h
+++ b/src/effect.h
@@ -34,11 +34,13 @@ struct effect_info {
};
struct effect {
- effect_info_t *info;
+ const effect_info_t *info;
struct timespec start;
struct effect *next;
};
+void effect_init(effect_t *effect, const effect_info_t *info);
+
void effect_info_free(effect_info_t *info);
void effect_free(effect_t *effect);
diff --git a/src/effects/pulse.c b/src/effects/pulse.c
index c2a324a..a6983ba 100644
--- a/src/effects/pulse.c
+++ b/src/effects/pulse.c
@@ -47,8 +47,8 @@ static void effect_pulse_pre(effect_t *effect, layout_t *layout, cairo_t *cr)
static effect_t *effect_pulse_allocate(const effect_info_t *info)
{
- effect_pulse_t *effect = calloc(1, sizeof(effect_pulse_t));
- assert(effect != NULL);
+ effect_pulse_t *effect = malloc(sizeof(effect_pulse_t));
+ effect_init((effect_t *)effect, info);
effect->amplitude = *(unsigned int *)info->state / 100.0;
return (effect_t *)effect;
}
diff --git a/src/event.c b/src/event.c
index 5792a34..8db76a5 100644
--- a/src/event.c
+++ b/src/event.c
@@ -32,16 +32,22 @@ static bool event_click(layout_t *layout, event_t event)
return true;
}
+ block_t *block = layout->block;
log_value_debug("Block was clicked",
"s:type", even_type_to_string(event.type),
"i:x", event.x,
"i:y", event.y,
- "s:block_label", layout->block->label,
+ "s:block_label", block->label,
"i:block_x", layout->x,
"i:block_y", layout->y,
"i:block_width", layout->width,
"i:block_height", layout->height);
+ if (block->effect == NULL && block->info != NULL) {
+ block->effect = block->info->allocate(block->info);
+ log_debug("Started effect '%s'", block->info->label);
+ }
+
return true;
}
diff --git a/src/util.c b/src/util.c
index 669e031..42caa8a 100644
--- a/src/util.c
+++ b/src/util.c
@@ -205,3 +205,10 @@ next:
buffer[n] = '\0';
return buffer;
}
+
+void strvfree(char **strv)
+{
+ for (int i = 0; strv[i] != NULL; i++)
+ free(strv[i]);
+ free(strv);
+}
diff --git a/src/util.h b/src/util.h
index 30acf34..563e17d 100644
--- a/src/util.h
+++ b/src/util.h
@@ -64,4 +64,6 @@ size_t strprefix(const char *string, const char *prefix);
char *strformat(const char *string, char delim, const char *keys[], const char *values[]);
+void strvfree(char **strv);
+
#endif