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/web.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'src/web.c') diff --git a/src/web.c b/src/web.c index 8202815..535d92f 100644 --- a/src/web.c +++ b/src/web.c @@ -382,7 +382,12 @@ int vega_web(PureApp *app, const VegaScreen *screen, const char *listen_on, unsi signal(SIGPIPE, SIG_IGN); static Client clients[CLIENTS]; for (unsigned i = 0; i < CLIENTS; ++i) clients[i].fd = -1; + VegaTimers timers; + vega_timers_start(&timers, app->module, vega_now()); while (!stopping) { + /* Tick what is due; every page hears of it this round, without waiting. */ + bool ticked = false; + long due = vega_timers_run(&timers, app, vega_now(), &ticked); struct pollfd fds[CLIENTS + 1]; unsigned slot[CLIENTS + 1], count = 1; fds[0] = (struct pollfd){listener, POLLIN, 0}; @@ -392,11 +397,17 @@ int vega_web(PureApp *app, const VegaScreen *screen, const char *listen_on, unsi fds[count] = (struct pollfd){c->fd, (short)(POLLIN | (c->out_at < c->out.used ? POLLOUT : 0)), 0}; slot[count++] = i; } - int ready = poll(fds, count, screen->ticks ? 33 : reload ? 200 : 1000); + int timeout = ticked ? 0 : screen->ticks ? 33 : reload ? 200 : 1000; + if (due >= 0 && due < timeout) timeout = (int)due; + int ready = poll(fds, count, timeout); if (ready < 0 && errno != EINTR) break; - bool changed = false; + bool changed = ticked; + app->now = vega_now(); /* what the events of this round see */ if (ready == 0 && screen->ticks) changed = pure_app_frame(app) == PURE_OK; - if (reload && reload()) changed = true; /* a new module: every page draws it */ + if (reload && reload()) { /* a new module: every page draws it */ + changed = true; + vega_timers_start(&timers, app->module, app->now); + } if (fds[0].revents & POLLIN) for (int fd; (fd = accept4(listener, NULL, NULL, SOCK_NONBLOCK | SOCK_CLOEXEC)) >= 0;) { unsigned free_slot = CLIENTS; -- cgit v1.2.3