Simplify speedrun code a bit

master
Kenny Levinsen 2020-10-17 13:37:22 +02:00
parent e14e8efcf3
commit 75b0a71d99
1 changed files with 40 additions and 32 deletions

72
main.c
View File

@ -17,49 +17,49 @@
#include "color_math.h" #include "color_math.h"
#if defined(SPEEDRUN) #if defined(SPEEDRUN)
static time_t start = 0; static time_t start = 0, offset = 0, multiplier = 1000;
static void init_time(time_t *tloc) { static void init_time(void) {
(void)tloc;
tzset(); tzset();
struct timespec realtime; struct timespec realtime;
clock_gettime(CLOCK_REALTIME, &realtime); clock_gettime(CLOCK_REALTIME, &realtime);
start = realtime.tv_sec; offset = realtime.tv_sec;
char *startstr = getenv("SPEEDRUN_START");
if (startstr != NULL) {
start = atol(startstr);
} else {
start = offset;
}
char *multistr = getenv("SPEEDRUN_MULTIPLIER");
if (multistr != NULL) {
multiplier = atol(multistr);
}
} }
static time_t get_time_sec(time_t *tloc) { static time_t get_time_sec(void) {
(void)tloc;
struct timespec realtime; struct timespec realtime;
clock_gettime(CLOCK_REALTIME, &realtime); clock_gettime(CLOCK_REALTIME, &realtime);
time_t now = start + ((realtime.tv_sec - start) * 1000 + realtime.tv_nsec / 1000000); time_t now = start + ((realtime.tv_sec - offset) * multiplier + realtime.tv_nsec / (1000000000 / multiplier));
struct tm tm; struct tm tm;
localtime_r(&now, &tm); localtime_r(&now, &tm);
fprintf(stderr, "time in termina: %02d:%02d:%02d\n", tm.tm_hour, tm.tm_min, tm.tm_sec); fprintf(stderr, "time in termina: %02d:%02d:%02d, %d/%d/%d\n", tm.tm_hour, tm.tm_min, tm.tm_sec,
tm.tm_mday, tm.tm_mon+1, tm.tm_year + 1900);
return now; return now;
} }
static int set_timer(timer_t timer, time_t deadline) { static void adjust_timerspec(struct itimerspec *timerspec) {
time_t diff = deadline - start; int diff = timerspec->it_value.tv_sec - offset;
struct itimerspec timerspec = {{0}, {0}}; timerspec->it_value.tv_sec = offset + diff / multiplier;
timerspec.it_value.tv_sec = start + diff / 1000; timerspec->it_value.tv_nsec = (diff % multiplier) * (1000000000 / multiplier);
timerspec.it_value.tv_nsec = (diff % 1000) * 1000000;
struct tm tm;
localtime_r(&deadline, &tm);
fprintf(stderr, "sleeping until %02d:%02d:%02d\n", tm.tm_hour, tm.tm_min, tm.tm_sec);
return timer_settime(timer, TIMER_ABSTIME, &timerspec, NULL);
} }
#else #else
static inline void init_time(time_t *tloc) { static inline void init_time(void) {
(void)tloc;
tzset(); tzset();
} }
static inline time_t get_time_sec(time_t *tloc) { static inline time_t get_time_sec(void) {
return time(tloc); return time(NULL);
} }
static int set_timer(timer_t timer, time_t deadline) { static inline void adjust_timerspec(struct itimerspec *timerspec) {
struct itimerspec timerspec = {{0}, {0}}; (void)timerspec;
timerspec.it_value.tv_sec = deadline;
return timer_settime(timer, TIMER_ABSTIME, &timerspec, NULL);
} }
#endif #endif
@ -486,7 +486,15 @@ static void update_timer(const struct context *ctx, timer_t timer, time_t now) {
} }
assert(deadline > now); assert(deadline > now);
set_timer(timer, deadline); struct itimerspec timerspec = {
.it_interval = {0},
.it_value = {
.tv_sec = deadline,
.tv_nsec = 0,
}
};
adjust_timerspec(&timerspec);
timer_settime(timer, TIMER_ABSTIME, &timerspec, NULL);
} }
static int display_poll(struct wl_display *display, short int events, int timeout) { static int display_poll(struct wl_display *display, short int events, int timeout) {
@ -552,7 +560,7 @@ static void timer_signal(int signal) {
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
init_time(NULL); init_time();
// Initialize defaults // Initialize defaults
struct context ctx = { struct context ctx = {
@ -646,7 +654,7 @@ int main(int argc, char *argv[]) {
wl_display_roundtrip(display); wl_display_roundtrip(display);
time_t now = get_time_sec(NULL); time_t now = get_time_sec();
recalc_stops(&ctx, now); recalc_stops(&ctx, now);
update_timer(&ctx, ctx.timer, now); update_timer(&ctx, ctx.timer, now);
@ -658,7 +666,7 @@ int main(int argc, char *argv[]) {
if (timer_fired) { if (timer_fired) {
timer_fired = false; timer_fired = false;
now = get_time_sec(NULL); now = get_time_sec();
recalc_stops(&ctx, now); recalc_stops(&ctx, now);
update_timer(&ctx, ctx.timer, now); update_timer(&ctx, ctx.timer, now);