Krolyxon
Commit 03b1d7c

Branches

Refs →

nsxivmain.c

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