Krolyxon
Commit 653a6ee

Branches

Refs →

nsxivmain.c

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