nsxiv762420c
Close image-info explicitly
+22 −19
2 files changed
- M commands.c +4 −1
- M main.c +18 −18
| @@ -27,6 +27,7 @@ | |||
| 27 | 27 | ||
| 28 | 28 | void remove_file(int, bool); | |
| 29 | 29 | void load_image(int); | |
| 30 | + | void close_info(void); | |
| 30 | 31 | void open_info(void); | |
| 31 | 32 | int ptr_third_x(void); | |
| 32 | 33 | void redraw(void); | |
| @@ -98,9 +99,11 @@ bool cg_toggle_bar(arg_t _) | |||
| 98 | 99 | { | |
| 99 | 100 | win_toggle_bar(&win); | |
| 100 | 101 | if (mode == MODE_IMAGE) { | |
| 101 | − | img.checkpan = img.dirty = true; | |
| 102 | 102 | if (win.bar.h > 0) | |
| 103 | 103 | open_info(); | |
| 104 | + | else | |
| 105 | + | close_info(); | |
| 106 | + | img.checkpan = img.dirty = true; | |
| 104 | 107 | } else { | |
| 105 | 108 | tns.dirty = true; | |
| 106 | 109 | } | |
| @@ -72,7 +72,7 @@ struct { | |||
| 72 | 72 | extcmd_t f; | |
| 73 | 73 | int fd; | |
| 74 | 74 | unsigned int i, lastsep; | |
| 75 | − | bool open; | |
| 75 | + | pid_t pid; | |
| 76 | 76 | } info; | |
| 77 | 77 | ||
| 78 | 78 | struct { | |
| @@ -213,24 +213,27 @@ bool check_timeouts(struct timeval *t) | |||
| 213 | 213 | return tmin > 0; | |
| 214 | 214 | } | |
| 215 | 215 | ||
| 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 | + | ||
| 216 | 226 | void open_info(void) | |
| 217 | 227 | { | |
| 218 | − | static pid_t pid; | |
| 219 | 228 | int pfd[2]; | |
| 220 | 229 | char w[12], h[12]; | |
| 221 | 230 | ||
| 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) | |
| 223 | 232 | return; | |
| 224 | − | if (info.fd != -1) { | |
| 225 | − | close(info.fd); | |
| 226 | − | kill(pid, SIGTERM); | |
| 227 | − | info.fd = -1; | |
| 228 | − | } | |
| 229 | 233 | win.bar.l.buf[0] = '\0'; | |
| 230 | − | ||
| 231 | 234 | if (pipe(pfd) < 0) | |
| 232 | 235 | return; | |
| 233 | − | if ((pid = fork()) == 0) { | |
| 236 | + | if ((info.pid = fork()) == 0) { | |
| 234 | 237 | close(pfd[0]); | |
| 235 | 238 | dup2(pfd[1], 1); | |
| 236 | 239 | snprintf(w, sizeof(w), "%d", img.w); | |
| @@ -239,13 +242,12 @@ void open_info(void) | |||
| 239 | 242 | error(EXIT_FAILURE, errno, "exec: %s", info.f.cmd); | |
| 240 | 243 | } | |
| 241 | 244 | close(pfd[1]); | |
| 242 | − | if (pid < 0) { | |
| 245 | + | if (info.pid < 0) { | |
| 243 | 246 | close(pfd[0]); | |
| 244 | 247 | } else { | |
| 245 | 248 | fcntl(pfd[0], F_SETFL, O_NONBLOCK); | |
| 246 | 249 | info.fd = pfd[0]; | |
| 247 | 250 | info.i = info.lastsep = 0; | |
| 248 | − | info.open = true; | |
| 249 | 251 | } | |
| 250 | 252 | } | |
| 251 | 253 | ||
| @@ -278,9 +280,7 @@ end: | |||
| 278 | 280 | info.i -= info.lastsep; | |
| 279 | 281 | win.bar.l.buf[info.i] = '\0'; | |
| 280 | 282 | win_draw(&win); | |
| 281 | − | close(info.fd); | |
| 282 | − | info.fd = -1; | |
| 283 | − | while (waitpid(-1, NULL, WNOHANG) > 0); | |
| 283 | + | close_info(); | |
| 284 | 284 | } | |
| 285 | 285 | ||
| 286 | 286 | void load_image(int new) | |
| @@ -309,7 +309,7 @@ void load_image(int new) | |||
| 309 | 309 | files[new].flags &= ~FF_WARN; | |
| 310 | 310 | fileidx = current = new; | |
| 311 | 311 | ||
| 312 | − | info.open = false; | |
| 312 | + | close_info(); | |
| 313 | 313 | open_info(); | |
| 314 | 314 | arl_setup(&arl, files[fileidx].path); | |
| 315 | 315 | ||
| @@ -495,6 +495,7 @@ void run_key_handler(const char *key, unsigned int mask) | |||
| 495 | 495 | } | |
| 496 | 496 | oldst = emalloc(fcnt * sizeof(*oldst)); | |
| 497 | 497 | ||
| 498 | + | close_info(); | |
| 498 | 499 | strncpy(win.bar.l.buf, "Running key handler...", win.bar.l.size); | |
| 499 | 500 | win_draw(&win); | |
| 500 | 501 | win_set_cursor(&win, CURSOR_WATCH); | |
| @@ -551,8 +552,7 @@ end: | |||
| 551 | 552 | if (changed) { | |
| 552 | 553 | img_close(&img, true); | |
| 553 | 554 | load_image(fileidx); | |
| 554 | − | } else if (info.f.err == 0) { | |
| 555 | − | info.open = false; | |
| 555 | + | } else { | |
| 556 | 556 | open_info(); | |
| 557 | 557 | } | |
| 558 | 558 | } | |