commit 9bfbbe3

wf  ·  2026-06-06 17:23:31 +0000 UTC
parent ac24492
Fix module rerunning and optimize run loop
1 files changed,  +21, -12
+21, -12
  1@@ -186,6 +186,7 @@ module_run(struct module *m) {
  2 
  3 	int f = fcntl(m->fd, F_GETFL, 0);
  4 	if (f < 0 || fcntl(m->fd, F_SETFL, f | O_NONBLOCK) < 0) {
  5+		LOG("while running module `%s`: couldn't fcntl: %s\n", m->name, strerror(errno));
  6 		close(m->fd);
  7 		m->fd = -1;
  8 		kill(m->pid, SIGTERM);
  9@@ -219,9 +220,17 @@ module_out(struct module *m) {
 10 		out[total] = '\0';
 11 	}
 12 
 13-	if (m->fd >= 0) {
 14+	if (r <= 0) {
 15+		if (errno == EAGAIN || errno == EWOULDBLOCK)
 16+			; /* non-problem */
 17+		else
 18+			LOG("while running module `%s`: couldn't read: %s\n", m->name, strerror(errno));
 19+		int ret;
 20+		waitpid(m->pid, &ret, WNOHANG);
 21 		close(m->fd);
 22+		m->pid = 0;
 23 		m->fd = -1;
 24+		m->next = m->interval ? now_ms() + m->interval * 1000 : UINT64_MAX;
 25 	}
 26 
 27 	if (out) {
 28@@ -645,24 +654,21 @@ draw(struct app *app) {
 29 	wl_surface_damage(app->surface, 0, 0, app->config.width, app->config.height);
 30 	wld_flush(app->wld_ren);
 31 	wld_swap(app->wld_surface);
 32+	wl_display_roundtrip(app->dpy);
 33 }
 34 
 35 static void
 36 run(struct app *app) {
 37 	while (running) {
 38 		uint64_t now = now_ms();
 39-		int timeout = -1, count = 0;
 40+		int timeout = 50, count = 0;
 41 
 42 		for (size_t i = 0; i < app->panel.plen; i++) {
 43 			struct module *m = app->panel.parsed[i];
 44 			if (m->next != UINT64_MAX) {
 45-				if ((int64_t)(m->next - now) <= 0)
 46-					timeout = 100;
 47-				else {
 48-					int64_t t = (int64_t)(m->next - now);
 49-					if (timeout < 0 || t < timeout)
 50-						timeout = (int)t;
 51-				}
 52+				int64_t t = (int64_t)(m->next - now);
 53+				if (timeout < 0 || t < timeout)
 54+					timeout = (int)t;
 55 			}
 56 			if (m->fd >= 0)
 57 				count++;
 58@@ -693,7 +699,6 @@ run(struct app *app) {
 59 			break;
 60 		}
 61 
 62-		// int ret = poll(pfds, fds, timeout);
 63 		int ret = poll(pfds, n, timeout);
 64 		now = now_ms();
 65 
 66@@ -709,7 +714,7 @@ run(struct app *app) {
 67 		if (ret == 0) {
 68 			for (size_t i = 0; i < app->panel.plen; i++) {
 69 				struct module *m = app->panel.parsed[i];
 70-				if (m->fd < 0 && m->pid == 0 && m->interval && now_ms() >= m->next)
 71+				if (m->fd < 0 && m->pid == 0 && m->interval && now >= m->next)
 72 					module_run(m);
 73 			}
 74 			free(pfds);
 75@@ -719,13 +724,14 @@ run(struct app *app) {
 76 		if (pfds[0].revents & POLLIN)
 77 			wl_display_dispatch(app->dpy);
 78 
 79+		bool redraw = false;
 80 		n = 1;
 81 		for (size_t i = 0; i < app->panel.plen; i++) {
 82 			struct module *m = app->panel.parsed[i];
 83 			if (m->fd >= 0) {
 84 				if (pfds[n].revents & (POLLIN | POLLHUP | POLLRDHUP)) {
 85 					module_out(m);
 86-					draw(app);
 87+					redraw = true;
 88 				}
 89 				n++;
 90 			}
 91@@ -734,6 +740,9 @@ run(struct app *app) {
 92 				module_run(m);
 93 		}
 94 
 95+		if (redraw)
 96+			draw(app);
 97+
 98 		free(pfds);
 99 	}
100 }