Krolyxon
Commit 06164c2

Branches

Refs →

nsxivmain.c

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