diff options
Diffstat (limited to 'surfaces')
| -rw-r--r-- | surfaces/main.c | 10 | ||||
| -rw-r--r-- | surfaces/screen.c | 20 | ||||
| -rw-r--r-- | surfaces/vega.h | 14 | ||||
| -rw-r--r-- | surfaces/web.c | 6 |
4 files changed, 30 insertions, 20 deletions
diff --git a/surfaces/main.c b/surfaces/main.c index 6c7e368..70958e1 100644 --- a/surfaces/main.c +++ b/surfaces/main.c @@ -483,7 +483,7 @@ static int interactive(void) { VegaGrid grid = {0}; drawn = &grid; VegaTimers timers; - vega_timers_start(&timers, app.module, vega_now()); + vega_timers_start(&timers, app.module, vega_clock()); for (;;) { if (resized) { struct winsize size; @@ -494,7 +494,7 @@ static int interactive(void) { } /* Tick what is due, draw, then wait for input, a frame or the next tick. */ bool ticked = false; - long due = vega_timers_run(&timers, &app, vega_now(), &ticked); + long due = vega_timers_run(&timers, &app, vega_clock(), &ticked); char line[VEGA_TEXT + 4]; vega_draw(&grid, &app, &screen, line, shown_entry(line), typing); paint_terminal(&grid); @@ -505,7 +505,7 @@ static int interactive(void) { app.now = vega_now(); if (ready == 0) { if (screen.ticks) pure_app_frame(&app); - if (module_changed()) vega_timers_start(&timers, app.module, app.now); + if (module_changed()) vega_timers_start(&timers, app.module, vega_clock()); continue; } if (ready < 0) continue; /* a signal, such as a resize */ @@ -647,7 +647,7 @@ static int live(VegaInput *input, unsigned columns, unsigned rows, bool recorded signal(SIGTERM, on_stop); } VegaTimers timers; - vega_timers_start(&timers, app.module, vega_now()); + vega_timers_start(&timers, app.module, vega_clock()); char path[512] = ""; unsigned long differing = 0; bool open_input = true, printed = false; @@ -664,7 +664,7 @@ static int live(VegaInput *input, unsigned columns, unsigned rows, bool recorded bool dirty = true; while (!stopping && !(node && node->stopping(node->host))) { bool ticked = false; - long due = node ? -1 : vega_timers_run(&timers, &app, vega_now(), &ticked); + long due = node ? -1 : vega_timers_run(&timers, &app, vega_clock(), &ticked); PureSnapshot *snapshot = node ? node->read(node->host, &app) : NULL; dirty = dirty || ticked || snapshot || access_acted; access_acted = false; diff --git a/surfaces/screen.c b/surfaces/screen.c index fdecaf4..f079bd5 100644 --- a/surfaces/screen.c +++ b/surfaces/screen.c @@ -11,23 +11,29 @@ double vega_now(void) { return floor((double)t.tv_sec * 1000 + (double)t.tv_nsec / 1e6); } -void vega_timers_start(VegaTimers *timers, const PureModule *m, double now) { +double vega_clock(void) { + struct timespec t; + clock_gettime(CLOCK_MONOTONIC, &t); + return floor((double)t.tv_sec * 1000 + (double)t.tv_nsec / 1e6); +} + +void vega_timers_start(VegaTimers *timers, const PureModule *m, double clock) { 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; + timers->next[i] = m->input[i].kind == PURE_KIND_TIMER ? clock + m->input[i].period : 0; } -long vega_timers_run(VegaTimers *timers, PureApp *app, double now, bool *ticked) { +long vega_timers_run(VegaTimers *timers, PureApp *app, double clock, 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; + if (timers->next[i] <= clock) { + app->now = vega_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; + timers->next[i] = clock + m->input[i].period; } - if (soonest < 0 || timers->next[i] - now < soonest) soonest = timers->next[i] - now; + if (soonest < 0 || timers->next[i] - clock < soonest) soonest = timers->next[i] - clock; } return soonest < 0 ? -1 : (long)ceil(soonest); } diff --git a/surfaces/vega.h b/surfaces/vega.h index ff880c5..0ab87dd 100644 --- a/surfaces/vega.h +++ b/surfaces/vega.h @@ -43,12 +43,16 @@ void vega_screen(VegaScreen *, const PureModule *); /* The wall-clock time events see on a live surface: milliseconds since 1970 * (UTC). Scripts set their own (time MS). */ double vega_now(void); -/* When each timer input ticks next. run ticks every one due at now (a tick - * sees now) and gives the milliseconds until the next, or -1 without timers; - * ticked says whether any did. Missed ticks are not made up. */ +/* A clock that only goes on (milliseconds, from boot): when timers are due, + * which setting the wall clock back must not hold up. */ +double vega_clock(void); +/* When each timer input ticks next, on vega_clock. run ticks every one due + * at clock (a tick sees the wall clock, vega_now) and gives the + * milliseconds until the next, or -1 without timers; ticked says whether + * any did. Missed ticks are not made up. */ typedef struct { double next[PURE_MODULE_INPUTS]; } VegaTimers; -void vega_timers_start(VegaTimers *, const PureModule *, double now); -long vega_timers_run(VegaTimers *, PureApp *, double now, bool *ticked); +void vega_timers_start(VegaTimers *, const PureModule *, double clock); +long vega_timers_run(VegaTimers *, PureApp *, double clock, bool *ticked); /* What a module seems to mean but does not meet: a list label that is not * text, a _focus view without its table, a column a list never shows, a * second text input. Each finding names the view or input it is about; diff --git a/surfaces/web.c b/surfaces/web.c index 7103c8f..a4759c4 100644 --- a/surfaces/web.c +++ b/surfaces/web.c @@ -418,11 +418,11 @@ int vega_web(PureApp *app, const VegaScreen *screen, const char *listen_on, unsi 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()); + vega_timers_start(&timers, app->module, vega_clock()); 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); + long due = vega_timers_run(&timers, app, vega_clock(), &ticked); struct pollfd fds[CLIENTS + 1]; unsigned slot[CLIENTS + 1], count = 1; fds[0] = (struct pollfd){listener, POLLIN, 0}; @@ -441,7 +441,7 @@ int vega_web(PureApp *app, const VegaScreen *screen, const char *listen_on, unsi if (ready == 0 && screen->ticks) changed = pure_app_frame(app) == PURE_OK; if (reload && reload()) { /* a new module: every page draws it */ changed = true; - vega_timers_start(&timers, app->module, app->now); + vega_timers_start(&timers, app->module, vega_clock()); } if (fds[0].revents & POLLIN) for (int fd; (fd = accept4(listener, NULL, NULL, SOCK_NONBLOCK | SOCK_CLOEXEC)) >= 0;) { |
