dwmblocks/src/timer.c

58 lines
1.4 KiB
C
Raw Normal View History

#include "timer.h"
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include "block.h"
#include "util.h"
2023-10-25 20:00:08 +05:30
static unsigned int compute_tick(const block *const blocks,
const unsigned short block_count) {
unsigned int tick = 0;
2023-10-25 20:00:08 +05:30
for (unsigned short i = 0; i < block_count; ++i) {
const block *const block = &blocks[i];
tick = gcd(block->interval, tick);
}
return tick;
}
2023-10-25 20:00:08 +05:30
static unsigned int compute_reset_value(const block *const blocks,
const unsigned short block_count) {
unsigned int reset_value = 1;
2023-10-25 20:00:08 +05:30
for (unsigned short i = 0; i < block_count; ++i) {
const block *const block = &blocks[i];
reset_value = MAX(block->interval, reset_value);
}
return reset_value;
}
2023-10-25 20:00:08 +05:30
timer timer_new(const block *const blocks, const unsigned short block_count) {
timer timer = {
.time = 0,
2023-10-25 20:00:08 +05:30
.tick = compute_tick(blocks, block_count),
.reset_value = compute_reset_value(blocks, block_count),
};
return timer;
}
int timer_arm(timer *const timer) {
errno = 0;
(void)alarm(timer->tick);
if (errno != 0) {
(void)fprintf(stderr, "error: could not arm timer\n");
return 1;
}
// Wrap `time` to the interval [1, reset_value].
timer->time = (timer->time + timer->tick) % timer->reset_value + 1;
return 0;
}