diff options
Diffstat (limited to 'src/screen.c')
| -rw-r--r-- | src/screen.c | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/screen.c b/src/screen.c index 56c2cf9..fdecaf4 100644 --- a/src/screen.c +++ b/src/screen.c @@ -1,7 +1,36 @@ +#define _POSIX_C_SOURCE 200809L #include "vega.h" #include "module-text.h" #include <math.h> #include <string.h> +#include <time.h> + +double vega_now(void) { + struct timespec t; + clock_gettime(CLOCK_REALTIME, &t); + return floor((double)t.tv_sec * 1000 + (double)t.tv_nsec / 1e6); +} + +void vega_timers_start(VegaTimers *timers, const PureModule *m, double now) { + for (uint32_t i = 0; i < m->inputs; ++i) + timers->next[i] = m->input[i].kind == PURE_KIND_TIMER ? now + m->input[i].period : 0; +} + +long vega_timers_run(VegaTimers *timers, PureApp *app, double now, bool *ticked) { + const PureModule *m = app->module; + double soonest = -1; + for (uint32_t i = 0; i < m->inputs; ++i) { + if (m->input[i].kind != PURE_KIND_TIMER) continue; + if (timers->next[i] <= now) { + app->now = now; + pure_app_tick(app, i); /* a failed tick changes nothing, as any failed event */ + *ticked = true; + timers->next[i] = now + m->input[i].period; + } + if (soonest < 0 || timers->next[i] - now < soonest) soonest = timers->next[i] - now; + } + return soonest < 0 ? -1 : (long)ceil(soonest); +} static unsigned long evaluations; |
