summaryrefslogtreecommitdiff
path: root/lib/timer.c
blob: 66276565f3a24324f3e4c70189f264899c7d27d5 (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
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
 * libuvcgadget timer utils
 *
 * Copyright (C) 2022 Daniel Scally
 *
 * Contact: Daniel Scally <dan.scally@ideasonboard.com>
 */

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/timerfd.h>
#include <unistd.h>

#include "timer.h"

struct timer {
        int fd;
        struct itimerspec settings;
};

struct timer *timer_new(void)
{
        struct timer *timer;

        timer = malloc(sizeof(*timer));
        if (!timer)
                return NULL;

        memset(timer, 0, sizeof(*timer));

        timer->fd = timerfd_create(CLOCK_REALTIME, 0);
        if (timer->fd < 0) {
		fprintf(stderr, "failed to create timer: %s (%d)\n",
			strerror(errno), errno);
		goto err_free_timer;
	}

        return timer;

err_free_timer:
        free(timer);

        return NULL;
}

void timer_set_fps(struct timer *timer, int fps)
{
	int ns_per_frame = 1000000000 / fps;

	timer->settings.it_value.tv_nsec = ns_per_frame;
	timer->settings.it_interval.tv_nsec = ns_per_frame;
}

int timer_arm(struct timer *timer)
{
        int ret;

	ret = timerfd_settime(timer->fd, 0, &timer->settings, NULL);
	if (ret)
		fprintf(stderr, "failed to change timer settings: %s (%d)\n",
			strerror(errno), errno);

        return ret;
}

int timer_disarm(struct timer *timer)
{
	static const struct itimerspec disable_settings = {
		{ 0, 0 },
		{ 0, 0 },
	};
	int ret;

	ret = timerfd_settime(timer->fd, 0, &disable_settings, NULL);
	if (ret)
		fprintf(stderr, "failed to disable timer: %s (%d)\n",
			strerror(errno), errno);

	return ret;
}

void timer_wait(struct timer *timer)
{
        char read_buf[8];

        read(timer->fd, read_buf, 8);
}

void timer_destroy(struct timer *timer)
{
        close(timer->fd);
        free(timer);
}