Krolyxon
Commit 9632dd8

Branches

Refs →

nsxivmain.c

834 lines · 17.6 KB · C

1/* Copyright 2011-2013 Bert Muennich
2 *
3 * This file is part of sxiv.
4 *
5 * sxiv is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published
7 * by the Free Software Foundation; either version 2 of the License,
8 * or (at your option) any later version.
9 *
10 * sxiv is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with sxiv. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#define _POSIX_C_SOURCE 200112L
20#define _MAPPINGS_CONFIG
21
22#include <stdlib.h>
23#include <stdio.h>
24#include <string.h>
25#include <fcntl.h>
26#include <unistd.h>
27#include <errno.h>
28#include <signal.h>
29#include <sys/select.h>
30#include <sys/stat.h>
31#include <sys/time.h>
32#include <sys/wait.h>
33#include <X11/keysym.h>
34
35#include "types.h"
36#include "commands.h"
37#include "image.h"
38#include "options.h"
39#include "thumbs.h"
40#include "util.h"
41#include "window.h"
42#include "config.h"
43
44enum {
45 FILENAME_CNT = 1024,
46 TITLE_LEN = 256
47};
48
49#define EXEC_REL_DIR ".sxiv/exec"
50
51enum {
52 EXEC_INFO,
53 EXEC_KEY
54};
55
56typedef struct {
57 const char *name;
58 char *cmd;
59} exec_t;
60
61typedef struct {
62 struct timeval when;
63 bool active;
64 timeout_f handler;
65} timeout_t;
66
67/* timeout handler functions: */
68void redraw(void);
69void reset_cursor(void);
70void animate(void);
71void slideshow(void);
72void clear_resize(void);
73
74appmode_t mode;
75img_t img;
76tns_t tns;
77win_t win;
78
79fileinfo_t *files;
80int filecnt, fileidx;
81int alternate;
82
83int prefix;
84
85bool resized = false;
86
87exec_t exec[] = {
88 { "image-info", NULL },
89 { "key-handler", NULL }
90};
91
92struct {
93 char *cmd;
94 int fd;
95 unsigned int i, lastsep;
96 bool open;
97} info;
98
99timeout_t timeouts[] = {
100 { { 0, 0 }, false, redraw },
101 { { 0, 0 }, false, reset_cursor },
102 { { 0, 0 }, false, animate },
103 { { 0, 0 }, false, slideshow },
104 { { 0, 0 }, false, clear_resize },
105};
106
107void cleanup(void)
108{
109 static bool in = false;
110
111 if (!in) {
112 in = true;
113 img_close(&img, false);
114 tns_free(&tns);
115 win_close(&win);
116 }
117}
118
119void check_add_file(char *filename)
120{
121 const char *bn;
122
123 if (filename == NULL || *filename == '\0')
124 return;
125
126 if (access(filename, R_OK) < 0) {
127 warn("could not open file: %s", filename);
128 return;
129 }
130
131 if (fileidx == filecnt) {
132 filecnt *= 2;
133 files = (fileinfo_t*) s_realloc(files, filecnt * sizeof(fileinfo_t));
134 }
135 if (*filename != '/') {
136 files[fileidx].path = absolute_path(filename);
137 if (files[fileidx].path == NULL) {
138 warn("could not get absolute path of file: %s\n", filename);
139 return;
140 }
141 }
142 files[fileidx].loaded = false;
143 files[fileidx].name = s_strdup(filename);
144 if (*filename == '/')
145 files[fileidx].path = files[fileidx].name;
146 if ((bn = strrchr(files[fileidx].name , '/')) != NULL && bn[1] != '\0')
147 files[fileidx].base = ++bn;
148 else
149 files[fileidx].base = files[fileidx].name;
150 fileidx++;
151}
152
153void remove_file(int n, bool manual)
154{
155 if (n < 0 || n >= filecnt)
156 return;
157
158 if (filecnt == 1) {
159 if (!manual)
160 fprintf(stderr, "sxiv: no more files to display, aborting\n");
161 cleanup();
162 exit(manual ? EXIT_SUCCESS : EXIT_FAILURE);
163 }
164
165 if (files[n].path != files[n].name)
166 free((void*) files[n].path);
167 free((void*) files[n].name);
168
169 if (n + 1 < filecnt)
170 memmove(files + n, files + n + 1, (filecnt - n - 1) * sizeof(fileinfo_t));
171 if (n + 1 < tns.cnt) {
172 memmove(tns.thumbs + n, tns.thumbs + n + 1, (tns.cnt - n - 1) *
173 sizeof(thumb_t));
174 memset(tns.thumbs + tns.cnt - 1, 0, sizeof(thumb_t));
175 }
176
177 filecnt--;
178 if (n < tns.cnt)
179 tns.cnt--;
180 if (n < alternate)
181 alternate--;
182}
183
184void set_timeout(timeout_f handler, int time, bool overwrite)
185{
186 int i;
187
188 for (i = 0; i < ARRLEN(timeouts); i++) {
189 if (timeouts[i].handler == handler) {
190 if (!timeouts[i].active || overwrite) {
191 gettimeofday(&timeouts[i].when, 0);
192 TV_ADD_MSEC(&timeouts[i].when, time);
193 timeouts[i].active = true;
194 }
195 return;
196 }
197 }
198}
199
200void reset_timeout(timeout_f handler)
201{
202 int i;
203
204 for (i = 0; i < ARRLEN(timeouts); i++) {
205 if (timeouts[i].handler == handler) {
206 timeouts[i].active = false;
207 return;
208 }
209 }
210}
211
212bool check_timeouts(struct timeval *t)
213{
214 int i = 0, tdiff, tmin = -1;
215 struct timeval now;
216
217 while (i < ARRLEN(timeouts)) {
218 if (timeouts[i].active) {
219 gettimeofday(&now, 0);
220 tdiff = TV_DIFF(&timeouts[i].when, &now);
221 if (tdiff <= 0) {
222 timeouts[i].active = false;
223 if (timeouts[i].handler != NULL)
224 timeouts[i].handler();
225 i = tmin = -1;
226 } else if (tmin < 0 || tdiff < tmin) {
227 tmin = tdiff;
228 }
229 }
230 i++;
231 }
232 if (tmin > 0 && t != NULL)
233 TV_SET_MSEC(t, tmin);
234 return tmin > 0;
235}
236
237void open_info(void)
238{
239 static pid_t pid;
240 int pfd[2];
241
242 if (info.cmd == NULL || info.open || win.bar.h == 0)
243 return;
244 if (info.fd != -1) {
245 close(info.fd);
246 kill(pid, SIGTERM);
247 info.fd = -1;
248 }
249 win.bar.l[0] = '\0';
250
251 if (pipe(pfd) < 0)
252 return;
253 pid = fork();
254 if (pid > 0) {
255 close(pfd[1]);
256 fcntl(pfd[0], F_SETFL, O_NONBLOCK);
257 info.fd = pfd[0];
258 info.i = info.lastsep = 0;
259 info.open = true;
260 } else if (pid == 0) {
261 close(pfd[0]);
262 dup2(pfd[1], 1);
263 execl(info.cmd, info.cmd, files[fileidx].name, NULL);
264 warn("could not exec: %s", info.cmd);
265 exit(EXIT_FAILURE);
266 }
267}
268
269void read_info(void)
270{
271 ssize_t i, n;
272 char buf[BAR_L_LEN];
273
274 while (true) {
275 n = read(info.fd, buf, sizeof(buf));
276 if (n < 0 && errno == EAGAIN)
277 return;
278 else if (n == 0)
279 goto end;
280 for (i = 0; i < n; i++) {
281 if (buf[i] == '\n') {
282 if (info.lastsep == 0) {
283 win.bar.l[info.i++] = ' ';
284 info.lastsep = 1;
285 }
286 } else {
287 win.bar.l[info.i++] = buf[i];
288 info.lastsep = 0;
289 }
290 if (info.i + 1 == sizeof(win.bar.l))
291 goto end;
292 }
293 }
294end:
295 info.i -= info.lastsep;
296 win.bar.l[info.i] = '\0';
297 win_update_bar(&win);
298 close(info.fd);
299 info.fd = -1;
300 while (waitpid(-1, NULL, WNOHANG) > 0);
301}
302
303void load_image(int new)
304{
305 if (new < 0 || new >= filecnt)
306 return;
307
308 win_set_cursor(&win, CURSOR_WATCH);
309 reset_timeout(slideshow);
310
311 if (new != fileidx)
312 alternate = fileidx;
313
314 img_close(&img, false);
315 while (!img_load(&img, &files[new])) {
316 remove_file(new, false);
317 if (new >= filecnt)
318 new = filecnt - 1;
319 else if (new > 0 && new < fileidx)
320 new--;
321 }
322 files[new].loaded = true;
323 fileidx = new;
324
325 info.open = false;
326 open_info();
327
328 if (img.multi.cnt > 0 && img.multi.animate)
329 set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
330 else
331 reset_timeout(animate);
332}
333
334void update_info(void)
335{
336 int sel;
337 unsigned int i, fn, fw, n;
338 unsigned int llen = sizeof(win.bar.l), rlen = sizeof(win.bar.r);
339 char *lt = win.bar.l, *rt = win.bar.r, title[TITLE_LEN];
340 const char * mark;
341 bool ow_info;
342
343 for (fw = 0, i = filecnt; i > 0; fw++, i /= 10);
344 sel = mode == MODE_IMAGE ? fileidx : tns.sel;
345
346 /* update window title */
347 if (mode == MODE_THUMB) {
348 win_set_title(&win, "sxiv");
349 } else {
350 snprintf(title, sizeof(title), "sxiv - %s", files[sel].name);
351 win_set_title(&win, title);
352 }
353
354 /* update bar contents */
355 if (win.bar.h == 0)
356 return;
357 mark = files[sel].marked ? "* " : "";
358 if (mode == MODE_THUMB) {
359 if (tns.cnt == filecnt) {
360 n = snprintf(rt, rlen, "%s%0*d/%d", mark, fw, sel + 1, filecnt);
361 ow_info = true;
362 } else {
363 snprintf(lt, llen, "Loading... %0*d/%d", fw, tns.cnt, filecnt);
364 rt[0] = '\0';
365 ow_info = false;
366 }
367 } else {
368 n = snprintf(rt, rlen, "%s", mark);
369 if (img.ss.on)
370 n += snprintf(rt + n, rlen - n, "%ds | ", img.ss.delay);
371 if (img.gamma != 0)
372 n += snprintf(rt + n, rlen - n, "G%+d | ", img.gamma);
373 n += snprintf(rt + n, rlen - n, "%3d%% | ", (int) (img.zoom * 100.0));
374 if (img.multi.cnt > 0) {
375 for (fn = 0, i = img.multi.cnt; i > 0; fn++, i /= 10);
376 n += snprintf(rt + n, rlen - n, "%0*d/%d | ",
377 fn, img.multi.sel + 1, img.multi.cnt);
378 }
379 n += snprintf(rt + n, rlen - n, "%0*d/%d", fw, sel + 1, filecnt);
380 ow_info = info.cmd == NULL;
381 }
382 if (ow_info) {
383 fn = strlen(files[sel].name);
384 if (fn < llen &&
385 win_textwidth(files[sel].name, fn, true) +
386 win_textwidth(rt, n, true) < win.w)
387 {
388 strncpy(lt, files[sel].name, llen);
389 } else {
390 strncpy(lt, files[sel].base, llen);
391 }
392 }
393}
394
395void redraw(void)
396{
397 int t;
398
399 if (mode == MODE_IMAGE) {
400 img_render(&img);
401 if (img.ss.on) {
402 t = img.ss.delay * 1000;
403 if (img.multi.cnt > 0 && img.multi.animate)
404 t = MAX(t, img.multi.length);
405 set_timeout(slideshow, t, false);
406 }
407 } else {
408 tns_render(&tns);
409 }
410 update_info();
411 win_draw(&win);
412 reset_timeout(redraw);
413 reset_cursor();
414}
415
416void reset_cursor(void)
417{
418 int i;
419 cursor_t cursor = CURSOR_NONE;
420
421 if (mode == MODE_IMAGE) {
422 for (i = 0; i < ARRLEN(timeouts); i++) {
423 if (timeouts[i].handler == reset_cursor) {
424 if (timeouts[i].active)
425 cursor = CURSOR_ARROW;
426 break;
427 }
428 }
429 } else {
430 if (tns.cnt != filecnt)
431 cursor = CURSOR_WATCH;
432 else
433 cursor = CURSOR_ARROW;
434 }
435 win_set_cursor(&win, cursor);
436}
437
438void animate(void)
439{
440 if (img_frame_animate(&img, false)) {
441 redraw();
442 set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
443 }
444}
445
446void slideshow(void)
447{
448 load_image(fileidx + 1 < filecnt ? fileidx + 1 : 0);
449 redraw();
450}
451
452void clear_resize(void)
453{
454 resized = false;
455}
456
457void key_handler(const char *key, unsigned int mask)
458{
459 pid_t pid;
460 int retval, status, n = mode == MODE_IMAGE ? fileidx : tns.sel;
461 char *cmd = exec[EXEC_KEY].cmd, kstr[32];
462 struct stat oldst, newst;
463
464 if (cmd == NULL || key == NULL)
465 return;
466
467 snprintf(kstr, sizeof(kstr), "%s%s%s%s",
468 mask & ControlMask ? "C-" : "",
469 mask & Mod1Mask ? "M-" : "",
470 mask & ShiftMask ? "S-" : "", key);
471
472 stat(files[n].path, &oldst);
473
474 if ((pid = fork()) == 0) {
475 execl(cmd, cmd, kstr, files[n].path, NULL);
476 warn("could not exec key handler");
477 exit(EXIT_FAILURE);
478 } else if (pid < 0) {
479 warn("could not fork key handler");
480 return;
481 }
482 win_set_cursor(&win, CURSOR_WATCH);
483
484 waitpid(pid, &status, 0);
485 retval = WEXITSTATUS(status);
486 if (WIFEXITED(status) == 0 || retval != 0)
487 warn("key handler exited with non-zero return value: %d", retval);
488
489 if (stat(files[n].path, &newst) == 0 &&
490 memcmp(&oldst.st_mtime, &newst.st_mtime, sizeof(oldst.st_mtime)) == 0)
491 {
492 /* file has not changed */
493 win_set_cursor(&win, CURSOR_ARROW);
494 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
495 return;
496 }
497 if (mode == MODE_IMAGE) {
498 img_close(&img, true);
499 load_image(fileidx);
500 }
501 if (!tns_load(&tns, n, &files[n], true, mode == MODE_IMAGE) &&
502 mode == MODE_THUMB)
503 {
504 remove_file(tns.sel, false);
505 tns.dirty = true;
506 if (tns.sel >= tns.cnt)
507 tns.sel = tns.cnt - 1;
508 }
509 redraw();
510}
511
512#define MODMASK(mask) ((mask) & (ShiftMask|ControlMask|Mod1Mask))
513
514void on_keypress(XKeyEvent *kev)
515{
516 int i;
517 unsigned int sh;
518 KeySym ksym, shksym;
519 char key;
520
521 if (kev == NULL)
522 return;
523
524 if (kev->state & ShiftMask) {
525 kev->state &= ~ShiftMask;
526 XLookupString(kev, &key, 1, &shksym, NULL);
527 kev->state |= ShiftMask;
528 }
529 XLookupString(kev, &key, 1, &ksym, NULL);
530 sh = (kev->state & ShiftMask) && ksym != shksym ? ShiftMask : 0;
531
532 if (IsModifierKey(ksym))
533 return;
534
535 if ((ksym == XK_Escape && MODMASK(kev->state) == 0) ||
536 (key >= '0' && key <= '9'))
537 {
538 /* number prefix for commands */
539 prefix = ksym == XK_Escape ? 0 : prefix * 10 + (int) (key - '0');
540 return;
541 }
542
543 for (i = 0; i < ARRLEN(keys); i++) {
544 if (keys[i].ksym == ksym &&
545 MODMASK(keys[i].mask | sh) == MODMASK(kev->state))
546 {
547 if (keys[i].cmd != NULL && keys[i].cmd(keys[i].arg))
548 redraw();
549 prefix = 0;
550 return;
551 }
552 }
553 key_handler(XKeysymToString(ksym), kev->state & ~sh);
554}
555
556void on_buttonpress(XButtonEvent *bev)
557{
558 int i, sel;
559
560 if (bev == NULL)
561 return;
562
563 if (mode == MODE_IMAGE) {
564 win_set_cursor(&win, CURSOR_ARROW);
565 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
566
567 for (i = 0; i < ARRLEN(buttons); i++) {
568 if (buttons[i].button == bev->button &&
569 MODMASK(buttons[i].mask) == MODMASK(bev->state))
570 {
571 if (buttons[i].cmd != NULL && buttons[i].cmd(buttons[i].arg))
572 redraw();
573 return;
574 }
575 }
576 } else {
577 /* thumbnail mode (hard-coded) */
578 switch (bev->button) {
579 case Button1:
580 if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
581 if (sel == tns.sel) {
582 mode = MODE_IMAGE;
583 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
584 load_image(tns.sel);
585 } else {
586 tns_highlight(&tns, tns.sel, false);
587 tns_highlight(&tns, sel, true);
588 tns.sel = sel;
589 }
590 redraw();
591 break;
592 }
593 break;
594 case Button3:
595 if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
596 files[sel].marked = !files[sel].marked;
597 tns_mark(&tns, sel, files[sel].marked);
598 redraw();
599 }
600 break;
601 case Button4:
602 case Button5:
603 if (tns_scroll(&tns, bev->button == Button4 ? DIR_UP : DIR_DOWN,
604 (bev->state & ControlMask) != 0))
605 redraw();
606 break;
607 }
608 }
609}
610
611void run(void)
612{
613 int xfd;
614 fd_set fds;
615 struct timeval timeout;
616 bool discard, to_set;
617 XEvent ev, nextev;
618
619 redraw();
620
621 while (true) {
622 while (mode == MODE_THUMB && tns.cnt < filecnt &&
623 XPending(win.env.dpy) == 0)
624 {
625 /* load thumbnails */
626 set_timeout(redraw, TO_REDRAW_THUMBS, false);
627 if (tns_load(&tns, tns.cnt, &files[tns.cnt], false, false)) {
628 tns.cnt++;
629 } else {
630 remove_file(tns.cnt, false);
631 if (tns.sel > 0 && tns.sel >= tns.cnt)
632 tns.sel--;
633 }
634 if (tns.cnt == filecnt)
635 redraw();
636 else
637 check_timeouts(NULL);
638 }
639
640 while (XPending(win.env.dpy) == 0
641 && ((to_set = check_timeouts(&timeout)) || info.fd != -1))
642 {
643 /* check for timeouts & input */
644 xfd = ConnectionNumber(win.env.dpy);
645 FD_ZERO(&fds);
646 FD_SET(xfd, &fds);
647 if (info.fd != -1) {
648 FD_SET(info.fd, &fds);
649 xfd = MAX(xfd, info.fd);
650 }
651 select(xfd + 1, &fds, 0, 0, to_set ? &timeout : NULL);
652 if (info.fd != -1 && FD_ISSET(info.fd, &fds))
653 read_info();
654 }
655
656 do {
657 XNextEvent(win.env.dpy, &ev);
658 discard = false;
659 if (XEventsQueued(win.env.dpy, QueuedAlready) > 0) {
660 XPeekEvent(win.env.dpy, &nextev);
661 switch (ev.type) {
662 case ConfigureNotify:
663 discard = ev.type == nextev.type;
664 break;
665 case KeyPress:
666 discard = (nextev.type == KeyPress || nextev.type == KeyRelease)
667 && ev.xkey.keycode == nextev.xkey.keycode;
668 break;
669 }
670 }
671 } while (discard);
672
673 switch (ev.type) {
674 /* handle events */
675 case ButtonPress:
676 on_buttonpress(&ev.xbutton);
677 break;
678 case ClientMessage:
679 if ((Atom) ev.xclient.data.l[0] == wm_delete_win)
680 return;
681 break;
682 case ConfigureNotify:
683 if (win_configure(&win, &ev.xconfigure)) {
684 if (mode == MODE_IMAGE) {
685 img.dirty = true;
686 img.checkpan = true;
687 } else {
688 tns.dirty = true;
689 }
690 if (!resized || win.fullscreen) {
691 redraw();
692 set_timeout(clear_resize, TO_REDRAW_RESIZE, false);
693 resized = true;
694 } else {
695 set_timeout(redraw, TO_REDRAW_RESIZE, false);
696 }
697 }
698 break;
699 case Expose:
700 win_expose(&win, &ev.xexpose);
701 break;
702 case KeyPress:
703 on_keypress(&ev.xkey);
704 break;
705 case MotionNotify:
706 if (mode == MODE_IMAGE) {
707 win_set_cursor(&win, CURSOR_ARROW);
708 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
709 }
710 break;
711 }
712 }
713}
714
715int fncmp(const void *a, const void *b)
716{
717 return strcoll(((fileinfo_t*) a)->name, ((fileinfo_t*) b)->name);
718}
719
720int main(int argc, char **argv)
721{
722 int i, start;
723 size_t n;
724 ssize_t len;
725 char *filename;
726 const char *homedir;
727 struct stat fstats;
728 r_dir_t dir;
729
730 parse_options(argc, argv);
731
732 if (options->clean_cache) {
733 tns_init(&tns, 0, NULL);
734 tns_clean_cache(&tns);
735 exit(EXIT_SUCCESS);
736 }
737
738 if (options->filecnt == 0 && !options->from_stdin) {
739 print_usage();
740 exit(EXIT_FAILURE);
741 }
742
743 if (options->recursive || options->from_stdin)
744 filecnt = FILENAME_CNT;
745 else
746 filecnt = options->filecnt;
747
748 files = (fileinfo_t*) s_malloc(filecnt * sizeof(fileinfo_t));
749 fileidx = 0;
750
751 if (options->from_stdin) {
752 filename = NULL;
753 while ((len = get_line(&filename, &n, stdin)) > 0) {
754 if (filename[len-1] == '\n')
755 filename[len-1] = '\0';
756 check_add_file(filename);
757 }
758 if (filename != NULL)
759 free(filename);
760 }
761
762 for (i = 0; i < options->filecnt; i++) {
763 filename = options->filenames[i];
764
765 if (stat(filename, &fstats) < 0) {
766 warn("could not stat file: %s", filename);
767 continue;
768 }
769 if (!S_ISDIR(fstats.st_mode)) {
770 check_add_file(filename);
771 } else {
772 if (!options->recursive) {
773 warn("ignoring directory: %s", filename);
774 continue;
775 }
776 if (r_opendir(&dir, filename) < 0) {
777 warn("could not open directory: %s", filename);
778 continue;
779 }
780 start = fileidx;
781 while ((filename = r_readdir(&dir)) != NULL) {
782 check_add_file(filename);
783 free((void*) filename);
784 }
785 r_closedir(&dir);
786 if (fileidx - start > 1)
787 qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp);
788 }
789 }
790
791 if (fileidx == 0) {
792 fprintf(stderr, "sxiv: no valid image file given, aborting\n");
793 exit(EXIT_FAILURE);
794 }
795
796 filecnt = fileidx;
797 fileidx = options->startnum < filecnt ? options->startnum : 0;
798
799 win_init(&win);
800 img_init(&img, &win);
801
802 if ((homedir = getenv("HOME")) == NULL) {
803 warn("could not locate home directory");
804 } else for (i = 0; i < ARRLEN(exec); i++) {
805 len = strlen(homedir) + strlen(EXEC_REL_DIR) + strlen(exec[i].name) + 3;
806 exec[i].cmd = (char*) s_malloc(len);
807 snprintf(exec[i].cmd, len, "%s/%s/%s", homedir, EXEC_REL_DIR, exec[i].name);
808 if (access(exec[i].cmd, X_OK) != 0) {
809 free(exec[i].cmd);
810 exec[i].cmd = NULL;
811 }
812 }
813 info.fd = -1;
814 info.cmd = exec[EXEC_INFO].cmd;
815
816 if (options->thumb_mode) {
817 mode = MODE_THUMB;
818 tns_init(&tns, filecnt, &win);
819 while (!tns_load(&tns, 0, &files[0], false, false))
820 remove_file(0, false);
821 tns.cnt = 1;
822 } else {
823 mode = MODE_IMAGE;
824 tns.thumbs = NULL;
825 load_image(fileidx);
826 }
827
828 win_open(&win);
829
830 run();
831 cleanup();
832
833 return 0;
834}