aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFederico Angelilli <code@fedang.net>2024-11-11 23:04:59 +0100
committerFederico Angelilli <code@fedang.net>2024-11-11 23:04:59 +0100
commitb492e69682945fdf0ada95a66edd1e2cb637eabd (patch)
tree78ee37d02764dcc1511b7f7ea6b820058d4f9c9a /src
parentb1bde5b6fe35f9ff94ae480d37c365410b94a43b (diff)
Add ring effectrewrite
Diffstat (limited to 'src')
-rw-r--r--src/config.c6
-rw-r--r--src/effect.c6
-rw-r--r--src/effect.h2
-rw-r--r--src/effects/rainbow.c99
-rw-r--r--src/effects/ring.c83
-rw-r--r--src/effects/scheme.c2
-rw-r--r--src/effects/shine.c1
-rw-r--r--src/event.c2
-rw-r--r--src/layout.c4
-rw-r--r--src/util.h5
10 files changed, 198 insertions, 12 deletions
diff --git a/src/config.c b/src/config.c
index 8955359..54c3c7b 100644
--- a/src/config.c
+++ b/src/config.c
@@ -727,10 +727,14 @@ block_t *config_resolve(config_t *config)
void config_free(config_t *config)
{
- for (int i = 0; i < config->n_blocks; i++)
+ for (size_t i = 0; i < config->n_blocks; i++)
block_free(&config->blocks[i]);
+ for (size_t i = 0; i < config->n_infos; i++)
+ free(config->infos[i].state);
+
free(config->blocks);
+ free(config->infos);
free(config->font);
free(config->monitor);
}
diff --git a/src/effect.c b/src/effect.c
index 65daad1..5c43f3a 100644
--- a/src/effect.c
+++ b/src/effect.c
@@ -12,12 +12,6 @@ void effect_init(effect_t *effect, const effect_info_t *info)
effect->next = NULL;
}
-void effect_info_free(effect_info_t *info)
-{
- free(info->state);
- free(info);
-}
-
void effect_free(effect_t *effect)
{
assert(effect->info->finalize != NULL);
diff --git a/src/effect.h b/src/effect.h
index b5f4b32..a598690 100644
--- a/src/effect.h
+++ b/src/effect.h
@@ -41,8 +41,6 @@ struct effect {
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);
#endif
diff --git a/src/effects/rainbow.c b/src/effects/rainbow.c
new file mode 100644
index 0000000..06b671c
--- /dev/null
+++ b/src/effects/rainbow.c
@@ -0,0 +1,99 @@
+#include <string.h>
+#include <math.h>
+#include <stdio.h>
+#include <assert.h>
+#include <stdlib.h>
+
+#include "scheme.h"
+
+#include "../any_log.h"
+
+typedef struct {
+ effect_t effect;
+ unsigned int speed;
+ cairo_pattern_t *gradient;
+} effect_rainbow_t;
+
+extern double cubic_bezier(double x, double a, double b, double c, double d);
+
+static void effect_rainbow_pre(effect_t *effect, layout_t *layout, cairo_t *cr)
+{
+ struct timespec now;
+ timespec_get(&now, TIME_UTC);
+
+ struct timespec diff = timespec_diff(now, effect->start);
+ double t = (double)timespec_to_ms(diff) / timespec_to_ms(effect->info->duration);
+ double s = cubic_bezier(t, 0.19, 1.0, 0.22, 1.0);
+ (void ) s;;
+
+ effect_rainbow_t *rainbow = (effect_rainbow_t *)effect;
+ double degree = M_PI / 180.0;
+ int radius = layout->height / 2 - layout->y_padding;
+
+ cairo_matrix_t matrix;
+ cairo_matrix_init_translate(&matrix, s, 0);
+ //cairo_matrix_scale(&matrix, layout->x, layout->y);
+ cairo_pattern_set_matrix(rainbow->gradient, &matrix);
+ cairo_set_source(cr, rainbow->gradient);
+
+ cairo_new_sub_path(cr);
+ cairo_arc(cr, layout->x + layout->x_padding + radius, layout->y + layout->y_padding + radius, radius, 90 * degree, 270 * degree);
+ cairo_arc(cr, layout->x + layout->width - layout->x_padding - radius, layout->y + layout->y_padding + radius, radius, 270 * degree, 450 * degree);
+ cairo_close_path(cr);
+ cairo_fill(cr);
+}
+
+static effect_t *effect_rainbow_allocate(const effect_info_t *info)
+{
+ effect_rainbow_t *effect = malloc(sizeof(effect_rainbow_t));
+ effect_init((effect_t *)effect, info);
+
+ effect->speed = 1;
+ effect->gradient = cairo_pattern_create_linear(0, 0, 1, 0);
+ cairo_pattern_set_extend(effect->gradient, CAIRO_EXTEND_REPEAT);
+
+ unsigned int colors[] = {
+ 0xff0000, 0xff7300, 0xfffb00,
+ 0x48ff00, 0x00ffd5, 0x002bff,
+ 0x7a00ff, 0xff00c8, 0xff0000,
+ };
+
+ for (int i = 0; i < 9; i++) {
+ color_t color = color_hex(colors[i]);
+ cairo_pattern_add_color_stop_rgb(effect->gradient, i * (1.0 / 9.0),
+ color.r, color.g, color.b);
+ }
+
+ return (effect_t *)effect;
+}
+
+static void effect_rainbow_finalize(effect_rainbow_t *effect)
+{
+ cairo_pattern_destroy(effect->gradient);
+ free(effect);
+}
+
+static bool effect_rainbow_validate(effect_info_t *info, const effect_scheme_t *scheme)
+{
+ return true;
+}
+
+static const config_entry_t effect_rainbow_entries[] = {
+ { 0 },
+};
+
+const effect_scheme_t effect_rainbow_scheme = {
+ .name = "rainbow",
+ .info = {
+ .duration = {
+ .tv_sec = 0,
+ .tv_nsec = 200000000,
+ },
+ .allocate = effect_rainbow_allocate,
+ .finalize = (effect_finalize_t)effect_rainbow_finalize,
+ .pre = effect_rainbow_pre,
+ },
+ .size = sizeof(unsigned int),
+ .entries = effect_rainbow_entries,
+ .validate = effect_rainbow_validate,
+};
diff --git a/src/effects/ring.c b/src/effects/ring.c
new file mode 100644
index 0000000..5de5489
--- /dev/null
+++ b/src/effects/ring.c
@@ -0,0 +1,83 @@
+#include <string.h>
+#include <assert.h>
+#include <math.h>
+
+#include "scheme.h"
+
+#include "../any_log.h"
+
+typedef struct {
+ effect_t effect;
+ unsigned int width;
+ unsigned int padding;
+ color_t color;
+} effect_ring_t;
+
+extern double cubic_bezier(double x, double a, double b, double c, double d);
+
+static void effect_ring_post(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->info->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);
+
+ double s = cubic_bezier(t, 0.19, 1.0, 0.22, 1.0);
+ double angle = 2 * M_PI * s;
+ (void)angle;
+
+ effect_ring_t *ring = (effect_ring_t *)effect;
+ double radius = layout->width / 2 - ring->padding - ring->width / 2;
+
+ cairo_set_line_width(cr, ring->width);
+ cairo_set_source_rgba(cr, ring->color.r, ring->color.g, ring->color.b, ring->color.a);
+
+ cairo_arc(cr, layout->x + layout->width / 2, layout->y + layout->height / 2, radius, M_PI/2, 0);
+ cairo_stroke(cr);
+}
+
+static effect_t *effect_ring_allocate(const effect_info_t *info)
+{
+ effect_ring_t *effect = malloc(sizeof(effect_ring_t));
+ effect_init((effect_t *)effect, info);
+
+ effect->width = *(unsigned int *)info->state;
+ effect->padding = *(unsigned int *)(info->state + sizeof(unsigned int));
+ effect->color = *(color_t*)(info->state + 2 * sizeof(unsigned int));
+
+ return (effect_t *)effect;
+}
+
+static void effect_ring_finalize(effect_t *effect)
+{
+ free(effect);
+}
+
+static const config_entry_t effect_ring_entries[] = {
+ { "width", CONFIG_UINT, NULL, 0 },
+ { "padding", CONFIG_UINT, NULL, sizeof(unsigned int) },
+ { "color", CONFIG_COLOR, NULL, sizeof(unsigned int) * 2},
+ { 0 },
+};
+
+const effect_scheme_t effect_ring_scheme = {
+ .name = "ring",
+ .info = {
+ .duration = {
+ .tv_sec = 0,
+ .tv_nsec = 200000000,
+ },
+ .allocate = effect_ring_allocate,
+ .finalize = effect_ring_finalize,
+ .post = effect_ring_post,
+ },
+ .size = sizeof(unsigned int) * 2 + sizeof(color_t),
+ .entries = effect_ring_entries,
+ .validate = NULL,
+};
diff --git a/src/effects/scheme.c b/src/effects/scheme.c
index 9de96a1..ef0711b 100644
--- a/src/effects/scheme.c
+++ b/src/effects/scheme.c
@@ -2,9 +2,11 @@
extern const effect_scheme_t effect_pulse_scheme;
extern const effect_scheme_t effect_shine_scheme;
+extern const effect_scheme_t effect_rainbow_scheme;
const effect_scheme_t *effect_schemes[] = {
&effect_pulse_scheme,
&effect_shine_scheme,
+ &effect_rainbow_scheme,
NULL
};
diff --git a/src/effects/shine.c b/src/effects/shine.c
index 3a873fe..31f03f0 100644
--- a/src/effects/shine.c
+++ b/src/effects/shine.c
@@ -80,4 +80,3 @@ const effect_scheme_t effect_shine_scheme = {
.entries = effect_shine_entries,
.validate = effect_shine_validate,
};
-
diff --git a/src/event.c b/src/event.c
index 8db76a5..20467f6 100644
--- a/src/event.c
+++ b/src/event.c
@@ -33,7 +33,7 @@ static bool event_click(layout_t *layout, event_t event)
}
block_t *block = layout->block;
- log_value_debug("Block was clicked",
+ log_value_trace("Block was clicked",
"s:type", even_type_to_string(event.type),
"i:x", event.x,
"i:y", event.y,
diff --git a/src/layout.c b/src/layout.c
index ae476b4..392da24 100644
--- a/src/layout.c
+++ b/src/layout.c
@@ -140,8 +140,10 @@ void layout_render(layout_t *layout, cairo_t *cr)
effect->info->post(effect, layout, cr);
}
- if (layout->block->effect == NULL && effect != NULL)
+ if (layout->block->effect == NULL && effect != NULL) {
+ layout->block->effect = effect->next;
effect_free(effect);
+ }
}
void layout_free(layout_t *layout)
diff --git a/src/util.h b/src/util.h
index 563e17d..42047ea 100644
--- a/src/util.h
+++ b/src/util.h
@@ -25,6 +25,11 @@ static inline color_t color_rgb(int r, int g, int b)
return color_rgba(r, g, b, 255);
}
+static inline color_t color_hex(unsigned int h)
+{
+ return color_rgb((h >> 16) & 0xff, (h >> 8) & 0xff, h & 0xff);
+}
+
char *color_to_string(color_t *color);
void color_print(FILE *stream, color_t *color);