From 8e2b3ffadcf83dba74d58dbf9ee2c57cef5f1ad5 Mon Sep 17 00:00:00 2001 From: eunhhu Date: Sat, 26 Sep 2026 11:54:31 +0100 Subject: Swap a running app's module: --watch and the swap command vega tui|web MODULE --watch replaces the running app's module when its file is written again (aup watch writes it); states keep their values by name through pure_app_replace and every web page hears of it. The file is compared by the digest of its bytes, at most five times a second, so a module of the same size written within the same second is not missed; a module that does not load leaves the app as it was and is not tried again. Modules alternate between two slots so a failed load never touches the running one. Scripts get swap MODULE; whether a module runs frames is now a property of the screen. --- src/web.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'src/web.c') diff --git a/src/web.c b/src/web.c index 149e888..8202815 100644 --- a/src/web.c +++ b/src/web.c @@ -364,7 +364,7 @@ static void drop(Client *c) { c->fd = -1; } -int vega_web(PureApp *app, const VegaScreen *screen, const char *listen_on, unsigned port) { +int vega_web(PureApp *app, const VegaScreen *screen, const char *listen_on, unsigned port, bool (*reload)(void)) { int listener = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0), one = 1; struct sockaddr_in address = {.sin_family = AF_INET, .sin_port = htons((uint16_t)port)}; socklen_t size = sizeof(address); @@ -382,8 +382,6 @@ 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; - bool ticking = false; - for (uint32_t h = 0; h < app->module->handlers; ++h) ticking = ticking || app->module->handler[h].event == 0; while (!stopping) { struct pollfd fds[CLIENTS + 1]; unsigned slot[CLIENTS + 1], count = 1; @@ -394,10 +392,11 @@ 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, ticking ? 33 : 1000); + int ready = poll(fds, count, screen->ticks ? 33 : reload ? 200 : 1000); if (ready < 0 && errno != EINTR) break; bool changed = false; - if (ready == 0 && ticking) changed = pure_app_frame(app) == PURE_OK; + 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 (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