aboutsummaryrefslogtreecommitdiff
path: root/src/effects/ring.c
blob: 5de5489fbff4340019a5b84bf1b7c07875a9ad01 (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
#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,
};