Krolyxon
Commit 762420c

Branches

Refs →

nsxiv762420c

Close image-info explicitly

Author
Bert Münnich
Date
2018-02-18 13:16 +0100
Committer
Bert Münnich · 2018-02-18 14:12 +0100
Parent
4124857
Commit
762420cdf135a9a8381d43b074aa8f286bc4902e

+22 −19

2 files changed

  1. M commands.c +4 −1
  2. M main.c +18 −18
M commands.c View
@@ -27,6 +27,7 @@
2727
2828 void remove_file(int, bool);
2929 void load_image(int);
30+void close_info(void);
3031 void open_info(void);
3132 int ptr_third_x(void);
3233 void redraw(void);
@@ -98,9 +99,11 @@ bool cg_toggle_bar(arg_t _)
9899 {
99100 win_toggle_bar(&win);
100101 if (mode == MODE_IMAGE) {
101− img.checkpan = img.dirty = true;
102102 if (win.bar.h > 0)
103103 open_info();
104+ else
105+ close_info();
106+ img.checkpan = img.dirty = true;
104107 } else {
105108 tns.dirty = true;
106109 }
M main.c View
@@ -72,7 +72,7 @@ struct {
7272 extcmd_t f;
7373 int fd;
7474 unsigned int i, lastsep;
75− bool open;
75+ pid_t pid;
7676 } info;
7777
7878 struct {
@@ -213,24 +213,27 @@ bool check_timeouts(struct timeval *t)
213213 return tmin > 0;
214214 }
215215
216+void close_info(void)
217+{
218+ if (info.fd != -1) {
219+ kill(info.pid, SIGTERM);
220+ close(info.fd);
221+ info.fd = -1;
222+ waitpid(info.pid, NULL, WNOHANG);
223+ }
224+}
225+
216226 void open_info(void)
217227 {
218− static pid_t pid;
219228 int pfd[2];
220229 char w[12], h[12];
221230
222− if (info.f.err != 0 || info.open || win.bar.h == 0)
231+ if (info.f.err != 0 || info.fd >= 0 || win.bar.h == 0)
223232 return;
224− if (info.fd != -1) {
225− close(info.fd);
226− kill(pid, SIGTERM);
227− info.fd = -1;
228− }
229233 win.bar.l.buf[0] = '\0';
230−
231234 if (pipe(pfd) < 0)
232235 return;
233− if ((pid = fork()) == 0) {
236+ if ((info.pid = fork()) == 0) {
234237 close(pfd[0]);
235238 dup2(pfd[1], 1);
236239 snprintf(w, sizeof(w), "%d", img.w);
@@ -239,13 +242,12 @@ void open_info(void)
239242 error(EXIT_FAILURE, errno, "exec: %s", info.f.cmd);
240243 }
241244 close(pfd[1]);
242− if (pid < 0) {
245+ if (info.pid < 0) {
243246 close(pfd[0]);
244247 } else {
245248 fcntl(pfd[0], F_SETFL, O_NONBLOCK);
246249 info.fd = pfd[0];
247250 info.i = info.lastsep = 0;
248− info.open = true;
249251 }
250252 }
251253
@@ -278,9 +280,7 @@ end:
278280 info.i -= info.lastsep;
279281 win.bar.l.buf[info.i] = '\0';
280282 win_draw(&win);
281− close(info.fd);
282− info.fd = -1;
283− while (waitpid(-1, NULL, WNOHANG) > 0);
283+ close_info();
284284 }
285285
286286 void load_image(int new)
@@ -309,7 +309,7 @@ void load_image(int new)
309309 files[new].flags &= ~FF_WARN;
310310 fileidx = current = new;
311311
312− info.open = false;
312+ close_info();
313313 open_info();
314314 arl_setup(&arl, files[fileidx].path);
315315
@@ -495,6 +495,7 @@ void run_key_handler(const char *key, unsigned int mask)
495495 }
496496 oldst = emalloc(fcnt * sizeof(*oldst));
497497
498+ close_info();
498499 strncpy(win.bar.l.buf, "Running key handler...", win.bar.l.size);
499500 win_draw(&win);
500501 win_set_cursor(&win, CURSOR_WATCH);
@@ -551,8 +552,7 @@ end:
551552 if (changed) {
552553 img_close(&img, true);
553554 load_image(fileidx);
554− } else if (info.f.err == 0) {
555− info.open = false;
555+ } else {
556556 open_info();
557557 }
558558 }