summaryrefslogtreecommitdiff
path: root/src/timer.c
blob: c0759ccb27343b96ff43ec7c69bffd8c41a429e2 (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
#include "timer.h"

static void timer_callback(struct ev_loop *loop, ev_timer *w, s32 revents)
{
    (void)loop;
    struct nn_timer *timer = (struct nn_timer *)w->data;
    (void)revents;
    if (!timer->disabled) timer->callback(timer->userdata, timer);
}

void nn_timer_init(struct nn_timer *timer, struct nn_event_loop *loop,
    void (*callback)(void *, struct nn_timer *), void *userdata)
{
    timer->loop = loop;
    timer->callback = callback;
    timer->userdata = userdata;
    timer->timer.data = timer;
    timer->disabled = false;
    ev_init_n(&timer->timer, timer_callback);
}

void nn_timer_set_repeat(struct nn_timer *timer, nn_os_tstamp repeat)
{
    timer->timer.repeat = repeat;
}

void nn_timer_again(struct nn_timer *timer)
{
    if (!timer->disabled) ev_timer_again(timer->loop->ev, &timer->timer);
}

void nn_timer_disable(struct nn_timer *timer)
{
    timer->disabled = true;
}

void nn_timer_enable(struct nn_timer *timer)
{
    timer->disabled = false;
}

void nn_timer_stop(struct nn_timer *timer)
{
    ev_timer_stop(timer->loop->ev, &timer->timer);
}