Krolyxon
Commit 34d4295

Branches

Refs →

nsxiv34d4295

read win-title in non-blocking manner (#314)

Author
NRK
Date
2023-01-09 08:12 +0000
Parent
16b966b
Commit
34d42951032c6bad6057d9c82893add1c94ade38

+42 −33

1 file changed

  1. M main.c +42 −33
M main.c View
@@ -78,17 +78,13 @@ static struct {
7878 extcmd_t f, ft;
7979 int fd;
8080 pid_t pid;
81−} info;
81+} info, wintitle;
8282
8383 static struct {
8484 extcmd_t f;
8585 bool warned;
8686 } keyhandler;
8787
88−static struct {
89− extcmd_t f;
90−} wintitle;
91−
9288 static struct {
9389 timeout_f handler;
9490 struct timeval when;
@@ -274,16 +270,41 @@ static bool check_timeouts(int *t)
274270 return tmin > 0;
275271 }
276272
277−static size_t get_win_title(char *buf, size_t len)
273+static void kill_close(pid_t pid, int *fd)
274+{
275+ if (fd != NULL && *fd != -1) {
276+ kill(pid, SIGTERM);
277+ close(*fd);
278+ *fd = -1;
279+ }
280+}
281+
282+static void close_title(void)
283+{
284+ kill_close(wintitle.pid, &wintitle.fd);
285+}
286+
287+static void read_title(void)
288+{
289+ ssize_t n;
290+ char buf[512];
291+
292+ if ((n = read(wintitle.fd, buf, sizeof(buf)-1)) > 0) {
293+ buf[n] = '\0';
294+ win_set_title(&win, buf, n);
295+ }
296+ close_title();
297+}
298+
299+static void open_title(void)
278300 {
279301 char *argv[8];
280302 char w[12] = "", h[12] = "", z[12] = "", fidx[12], fcnt[12];
281− ssize_t n = -1;
282− int readfd;
283303
284− if (wintitle.f.err || buf == NULL || len == 0)
285− return 0;
304+ if (wintitle.f.err || !title_dirty)
305+ return;
286306
307+ close_title();
287308 if (mode == MODE_IMAGE) {
288309 snprintf(w, ARRLEN(w), "%d", img.w);
289310 snprintf(h, ARRLEN(h), "%d", img.h);
@@ -293,22 +314,14 @@ static size_t get_win_title(char *buf, size_t len)
293314 snprintf(fcnt, ARRLEN(fcnt), "%d", filecnt);
294315 construct_argv(argv, ARRLEN(argv), wintitle.f.cmd, files[fileidx].path,
295316 fidx, fcnt, w, h, z, NULL);
296− if (spawn(&readfd, NULL, argv) > 0) {
297− if ((n = read(readfd, buf, len-1)) > 0)
298− buf[n] = '\0';
299− close(readfd);
300− }
301−
302− return MAX(0, n);
317+ if ((wintitle.pid = spawn(&wintitle.fd, NULL, argv)) > 0)
318+ fcntl(wintitle.fd, F_SETFL, O_NONBLOCK);
319+ title_dirty = false;
303320 }
304321
305322 void close_info(void)
306323 {
307− if (info.fd != -1) {
308− kill(info.pid, SIGTERM);
309− close(info.fd);
310− info.fd = -1;
311− }
324+ kill_close(info.pid, &info.fd);
312325 }
313326
314327 void open_info(void)
@@ -487,14 +500,7 @@ void redraw(void)
487500 tns_render(&tns);
488501 }
489502 update_info();
490− if (title_dirty) {
491− size_t n;
492− char buf[512];
493−
494− if ((n = get_win_title(buf, sizeof(buf))) > 0)
495− win_set_title(&win, buf, n);
496− title_dirty = false;
497− }
503+ open_title();
498504 win_draw(&win);
499505 reset_timeout(redraw);
500506 reset_cursor();
@@ -722,7 +728,7 @@ static void on_buttonpress(const XButtonEvent *bev)
722728
723729 static void run(void)
724730 {
725− enum { FD_X, FD_INFO, FD_ARL, FD_CNT };
731+ enum { FD_X, FD_INFO, FD_TITLE, FD_ARL, FD_CNT };
726732 struct pollfd pfd[FD_CNT];
727733 int timeout = 0;
728734 const struct timespec ten_ms = {0, 10000000};
@@ -755,13 +761,16 @@ static void run(void)
755761 } else {
756762 pfd[FD_X].fd = ConnectionNumber(win.env.dpy);
757763 pfd[FD_INFO].fd = info.fd;
764+ pfd[FD_TITLE].fd = wintitle.fd;
758765 pfd[FD_ARL].fd = arl.fd;
759− pfd[FD_X].events = pfd[FD_INFO].events = pfd[FD_ARL].events = POLLIN;
766+ pfd[FD_X].events = pfd[FD_INFO].events = pfd[FD_TITLE].events = pfd[FD_ARL].events = POLLIN;
760767
761768 if (poll(pfd, ARRLEN(pfd), to_set ? timeout : -1) < 0)
762769 continue;
763770 if (pfd[FD_INFO].revents & POLLIN)
764771 read_info();
772+ if (pfd[FD_TITLE].revents & POLLIN)
773+ read_title();
765774 if (pfd[FD_ARL].revents & POLLIN) {
766775 if (arl_handle(&arl)) {
767776 /* when too fast, imlib2 can't load the image */
@@ -922,7 +931,7 @@ int main(int argc, char *argv[])
922931 } else {
923932 error(0, 0, "Exec directory not found");
924933 }
925− info.fd = -1;
934+ wintitle.fd = info.fd = -1;
926935
927936 if (options->thumb_mode) {
928937 mode = MODE_THUMB;