Krolyxon
Commit 2e758f7

Branches

Refs →

nsxivmain.c

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