summaryrefslogtreecommitdiff
path: root/.config/wmii-hg/rbar_clock.c
blob: e5556b0b222686cdffdc1301c464686513f28456 (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
#include "rbar.h"

#include <stdint.h> /* uint64_t */
#include <stdlib.h> /* atexit(3p) */
#include <sys/timerfd.h>
#include <time.h> /* glock_gettime, localtime, strftime */

const char *cleanup_id;

void strfree(char **strp) {
	free(*strp);
}

void cleanup(void) {
	char __attribute__((__cleanup__(strfree))) *glob_id = rbar_globescape(cleanup_id);
	rbar_remove(glob_id);
}

int update(const char *id) {
	cleanup_id = id;
	atexit(cleanup);

	int timer = timerfd_create(CLOCK_REALTIME, 0);
	if (timer < 0)
		error(1, errno, "timerfd_create");

	struct timespec now;
	if (clock_gettime(CLOCK_REALTIME, &now) < 0)
		error(1, errno, "clock_gettime");

	struct itimerspec spec = {
		.it_interval = (struct timespec){.tv_sec = 1, .tv_nsec = 0},
		.it_value = (struct timespec){.tv_sec = now.tv_sec+1, .tv_nsec = 0},
	};

	if (timerfd_settime(timer, TFD_TIMER_ABSTIME, &spec, NULL) < 0)
		error(1, errno, "timerfd_settime");

	for (;;) {
		uint64_t count;
		if (read(timer, &count, sizeof count) != sizeof count)
			error(1, errno, "read");
		if (clock_gettime(CLOCK_REALTIME, &now) < 0)
			error(1, errno, "clock_gettime");
		char str[80];
		if (strftime(str, sizeof str,
			     "label %a %F %T %Z(%z)", /* apparently %:::z" is supported by date(1gnu) but not strftime(3gnu) */
			     localtime(&now.tv_sec)) <= 0)
			error(1, 0, "strftime wanted a really long string");
		rbar_write(id, str);
	}
}

struct impl impl = {
	.update = update,
};