Krolyxon
Commit 5d0679b

Branches

Refs →

nsxivmain.c

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