Krolyxon
Commit 2d703c3

Branches

Refs →

nsxivmain.c

859 lines · 18.3 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 static bool seen_prefix_key = false;
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
527 if (seen_prefix_key) {
528 seen_prefix_key = false;
529 if (!(MODMASK(kev->state) == 0 && ksym == XK_Escape))
530 run_key_handler(XKeysymToString(ksym), kev->state & ~sh);
531 return;
532 } else if (MODMASK(kev->state) == PREFIX_KEYMASK && ksym == PREFIX_KEYSYM) {
533 seen_prefix_key = true;
534 prefix = 0;
535 return;
536 }
537
538 if ((ksym == XK_Escape && MODMASK(kev->state) == 0) ||
539 (key >= '0' && key <= '9'))
540 {
541 /* number prefix for commands */
542 prefix = ksym == XK_Escape ? 0 : prefix * 10 + (int) (key - '0');
543 return;
544 }
545
546 for (i = 0; i < ARRLEN(keys); i++) {
547 if (keys[i].ksym == ksym &&
548 MODMASK(keys[i].mask | sh) == MODMASK(kev->state) &&
549 keys[i].cmd != NULL)
550 {
551 cmdreturn_t ret = keys[i].cmd(keys[i].arg);
552
553 if (ret == CMD_INVALID)
554 continue;
555 if (ret == CMD_DIRTY)
556 redraw();
557 break;
558 }
559 }
560 prefix = 0;
561}
562
563void on_buttonpress(XButtonEvent *bev)
564{
565 int i, sel;
566 static Time firstclick;
567
568 if (bev == NULL)
569 return;
570
571 if (mode == MODE_IMAGE) {
572 win_set_cursor(&win, CURSOR_ARROW);
573 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
574
575 for (i = 0; i < ARRLEN(buttons); i++) {
576 if (buttons[i].button == bev->button &&
577 MODMASK(buttons[i].mask) == MODMASK(bev->state) &&
578 buttons[i].cmd != NULL)
579 {
580 cmdreturn_t ret = buttons[i].cmd(buttons[i].arg);
581
582 if (ret == CMD_INVALID)
583 continue;
584 if (ret == CMD_DIRTY)
585 redraw();
586 break;
587 }
588 }
589 } else {
590 /* thumbnail mode (hard-coded) */
591 switch (bev->button) {
592 case Button1:
593 if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
594 if (sel != tns.sel) {
595 tns_highlight(&tns, tns.sel, false);
596 tns_highlight(&tns, sel, true);
597 tns.sel = sel;
598 firstclick = bev->time;
599 redraw();
600 } else if (bev->time - firstclick <= TO_DOUBLE_CLICK) {
601 mode = MODE_IMAGE;
602 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
603 load_image(tns.sel);
604 redraw();
605 } else {
606 firstclick = bev->time;
607 }
608 }
609 break;
610 case Button3:
611 if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
612 files[sel].marked = !files[sel].marked;
613 tns_mark(&tns, sel, files[sel].marked);
614 redraw();
615 }
616 break;
617 case Button4:
618 case Button5:
619 if (tns_scroll(&tns, bev->button == Button4 ? DIR_UP : DIR_DOWN,
620 (bev->state & ControlMask) != 0))
621 redraw();
622 break;
623 }
624 }
625 prefix = 0;
626}
627
628void run(void)
629{
630 int xfd;
631 fd_set fds;
632 struct timeval timeout;
633 bool discard, to_set;
634 XEvent ev, nextev;
635
636 redraw();
637
638 while (true) {
639 while (mode == MODE_THUMB && tns.cnt < filecnt &&
640 XPending(win.env.dpy) == 0)
641 {
642 /* load thumbnails */
643 set_timeout(redraw, TO_REDRAW_THUMBS, false);
644 if (tns_load(&tns, tns.cnt, &files[tns.cnt], false, false)) {
645 tns.cnt++;
646 } else {
647 remove_file(tns.cnt, false);
648 if (tns.sel > 0 && tns.sel >= tns.cnt)
649 tns.sel--;
650 }
651 if (tns.cnt == filecnt)
652 redraw();
653 else
654 check_timeouts(NULL);
655 }
656
657 while (XPending(win.env.dpy) == 0
658 && ((to_set = check_timeouts(&timeout)) || info.fd != -1))
659 {
660 /* check for timeouts & input */
661 xfd = ConnectionNumber(win.env.dpy);
662 FD_ZERO(&fds);
663 FD_SET(xfd, &fds);
664 if (info.fd != -1) {
665 FD_SET(info.fd, &fds);
666 xfd = MAX(xfd, info.fd);
667 }
668 select(xfd + 1, &fds, 0, 0, to_set ? &timeout : NULL);
669 if (info.fd != -1 && FD_ISSET(info.fd, &fds))
670 read_info();
671 }
672
673 do {
674 XNextEvent(win.env.dpy, &ev);
675 discard = false;
676 if (XEventsQueued(win.env.dpy, QueuedAlready) > 0) {
677 XPeekEvent(win.env.dpy, &nextev);
678 switch (ev.type) {
679 case ConfigureNotify:
680 discard = ev.type == nextev.type;
681 break;
682 case KeyPress:
683 discard = (nextev.type == KeyPress || nextev.type == KeyRelease)
684 && ev.xkey.keycode == nextev.xkey.keycode;
685 break;
686 }
687 }
688 } while (discard);
689
690 switch (ev.type) {
691 /* handle events */
692 case ButtonPress:
693 on_buttonpress(&ev.xbutton);
694 break;
695 case ClientMessage:
696 if ((Atom) ev.xclient.data.l[0] == wm_delete_win)
697 return;
698 break;
699 case ConfigureNotify:
700 if (win_configure(&win, &ev.xconfigure)) {
701 if (mode == MODE_IMAGE) {
702 img.dirty = true;
703 img.checkpan = true;
704 } else {
705 tns.dirty = true;
706 }
707 if (!resized || win.fullscreen) {
708 redraw();
709 set_timeout(clear_resize, TO_REDRAW_RESIZE, false);
710 resized = true;
711 } else {
712 set_timeout(redraw, TO_REDRAW_RESIZE, false);
713 }
714 }
715 break;
716 case Expose:
717 win_expose(&win, &ev.xexpose);
718 break;
719 case KeyPress:
720 on_keypress(&ev.xkey);
721 break;
722 case MotionNotify:
723 if (mode == MODE_IMAGE) {
724 win_set_cursor(&win, CURSOR_ARROW);
725 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
726 }
727 break;
728 }
729 }
730}
731
732int fncmp(const void *a, const void *b)
733{
734 return strcoll(((fileinfo_t*) a)->name, ((fileinfo_t*) b)->name);
735}
736
737int main(int argc, char **argv)
738{
739 int i, start;
740 size_t n;
741 ssize_t len;
742 char *filename;
743 const char *homedir, *dsuffix = "";
744 struct stat fstats;
745 r_dir_t dir;
746
747 parse_options(argc, argv);
748
749 if (options->clean_cache) {
750 tns_init(&tns, 0, NULL);
751 tns_clean_cache(&tns);
752 exit(EXIT_SUCCESS);
753 }
754
755 if (options->filecnt == 0 && !options->from_stdin) {
756 print_usage();
757 exit(EXIT_FAILURE);
758 }
759
760 if (options->recursive || options->from_stdin)
761 filecnt = FILENAME_CNT;
762 else
763 filecnt = options->filecnt;
764
765 files = (fileinfo_t*) s_malloc(filecnt * sizeof(fileinfo_t));
766 fileidx = 0;
767
768 if (options->from_stdin) {
769 filename = NULL;
770 while ((len = get_line(&filename, &n, stdin)) > 0) {
771 if (filename[len-1] == '\n')
772 filename[len-1] = '\0';
773 check_add_file(filename);
774 }
775 if (filename != NULL)
776 free(filename);
777 }
778
779 for (i = 0; i < options->filecnt; i++) {
780 filename = options->filenames[i];
781
782 if (stat(filename, &fstats) < 0) {
783 warn("could not stat file: %s", filename);
784 continue;
785 }
786 if (!S_ISDIR(fstats.st_mode)) {
787 check_add_file(filename);
788 } else {
789 if (!options->recursive) {
790 warn("ignoring directory: %s", filename);
791 continue;
792 }
793 if (r_opendir(&dir, filename) < 0) {
794 warn("could not open directory: %s", filename);
795 continue;
796 }
797 start = fileidx;
798 while ((filename = r_readdir(&dir)) != NULL) {
799 check_add_file(filename);
800 free((void*) filename);
801 }
802 r_closedir(&dir);
803 if (fileidx - start > 1)
804 qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp);
805 }
806 }
807
808 if (fileidx == 0) {
809 fprintf(stderr, "sxiv: no valid image file given, aborting\n");
810 exit(EXIT_FAILURE);
811 }
812
813 filecnt = fileidx;
814 fileidx = options->startnum < filecnt ? options->startnum : 0;
815
816 win_init(&win);
817 img_init(&img, &win);
818
819 if ((homedir = getenv("XDG_CONFIG_HOME")) == NULL || homedir[0] == '\0') {
820 homedir = getenv("HOME");
821 dsuffix = "/.config";
822 }
823 if (homedir != NULL) {
824 char **cmd[] = { &info.cmd, &keyhandler };
825 const char *name[] = { "image-info", "key-handler" };
826
827 for (i = 0; i < ARRLEN(cmd); i++) {
828 len = strlen(homedir) + strlen(dsuffix) + strlen(name[i]) + 12;
829 *cmd[i] = (char*) s_malloc(len);
830 snprintf(*cmd[i], len, "%s%s/sxiv/exec/%s", homedir, dsuffix, name[i]);
831 if (access(*cmd[i], X_OK) != 0) {
832 free(*cmd[i]);
833 *cmd[i] = NULL;
834 }
835 }
836 } else {
837 warn("could not locate exec directory");
838 }
839 info.fd = -1;
840
841 if (options->thumb_mode) {
842 mode = MODE_THUMB;
843 tns_init(&tns, filecnt, &win);
844 while (!tns_load(&tns, 0, &files[0], false, false))
845 remove_file(0, false);
846 tns.cnt = 1;
847 } else {
848 mode = MODE_IMAGE;
849 tns.thumbs = NULL;
850 load_image(fileidx);
851 }
852
853 win_open(&win);
854
855 run();
856 cleanup();
857
858 return 0;
859}