From b466603358f8fa5a74551a9819fdd54ad124ef0c Mon Sep 17 00:00:00 2001 From: eunhhu Date: Sat, 26 Sep 2026 17:58:26 +0100 Subject: Give live surfaces a clock and run timers; add a stopwatch The terminal and the web page set the time events see to the wall clock and tick an app's timers on time: each round ticks what is due, draws, then waits for input, a frame or the next tick, and a tick is news to every open page at once. Scripts have no clock: time MS sets the time the next events see and tick TIMER ticks, so an app that uses time replays exactly. A module swap reschedules the timers. examples/stopwatch.aup runs on every surface; the checks drive it by script, on a pseudo-terminal by the clock, and on the web page. --- src/screen.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'src/screen.c') 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 #include +#include + +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; -- cgit v1.2.3