Krolyxon
Commit 5b01c15

Branches

Refs →

nsxivmain.c

875 lines · 19.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
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 (mode == MODE_THUMB && fileidx >= tns.cnt)
188 fileidx = tns.cnt - 1;
189 if (n < alternate)
190 alternate--;
191}
192
193void set_timeout(timeout_f handler, int time, bool overwrite)
194{
195 int i;
196
197 for (i = 0; i < ARRLEN(timeouts); i++) {
198 if (timeouts[i].handler == handler) {
199 if (!timeouts[i].active || overwrite) {
200 gettimeofday(&timeouts[i].when, 0);
201 TV_ADD_MSEC(&timeouts[i].when, time);
202 timeouts[i].active = true;
203 }
204 return;
205 }
206 }
207}
208
209void reset_timeout(timeout_f handler)
210{
211 int i;
212
213 for (i = 0; i < ARRLEN(timeouts); i++) {
214 if (timeouts[i].handler == handler) {
215 timeouts[i].active = false;
216 return;
217 }
218 }
219}
220
221bool check_timeouts(struct timeval *t)
222{
223 int i = 0, tdiff, tmin = -1;
224 struct timeval now;
225
226 while (i < ARRLEN(timeouts)) {
227 if (timeouts[i].active) {
228 gettimeofday(&now, 0);
229 tdiff = TV_DIFF(&timeouts[i].when, &now);
230 if (tdiff <= 0) {
231 timeouts[i].active = false;
232 if (timeouts[i].handler != NULL)
233 timeouts[i].handler();
234 i = tmin = -1;
235 } else if (tmin < 0 || tdiff < tmin) {
236 tmin = tdiff;
237 }
238 }
239 i++;
240 }
241 if (tmin > 0 && t != NULL)
242 TV_SET_MSEC(t, tmin);
243 return tmin > 0;
244}
245
246void open_info(void)
247{
248 static pid_t pid;
249 int pfd[2];
250
251 if (info.cmd == NULL || info.open || win.bar.h == 0)
252 return;
253 if (info.fd != -1) {
254 close(info.fd);
255 kill(pid, SIGTERM);
256 info.fd = -1;
257 }
258 win.bar.l[0] = '\0';
259
260 if (pipe(pfd) < 0)
261 return;
262 pid = fork();
263 if (pid > 0) {
264 close(pfd[1]);
265 fcntl(pfd[0], F_SETFL, O_NONBLOCK);
266 info.fd = pfd[0];
267 info.i = info.lastsep = 0;
268 info.open = true;
269 } else if (pid == 0) {
270 close(pfd[0]);
271 dup2(pfd[1], 1);
272 execl(info.cmd, info.cmd, files[fileidx].name, NULL);
273 warn("could not exec: %s", info.cmd);
274 exit(EXIT_FAILURE);
275 }
276}
277
278void read_info(void)
279{
280 ssize_t i, n;
281 char buf[BAR_L_LEN];
282
283 while (true) {
284 n = read(info.fd, buf, sizeof(buf));
285 if (n < 0 && errno == EAGAIN)
286 return;
287 else if (n == 0)
288 goto end;
289 for (i = 0; i < n; i++) {
290 if (buf[i] == '\n') {
291 if (info.lastsep == 0) {
292 win.bar.l[info.i++] = ' ';
293 info.lastsep = 1;
294 }
295 } else {
296 win.bar.l[info.i++] = buf[i];
297 info.lastsep = 0;
298 }
299 if (info.i + 1 == sizeof(win.bar.l))
300 goto end;
301 }
302 }
303end:
304 info.i -= info.lastsep;
305 win.bar.l[info.i] = '\0';
306 win_draw(&win);
307 close(info.fd);
308 info.fd = -1;
309 while (waitpid(-1, NULL, WNOHANG) > 0);
310}
311
312void load_image(int new)
313{
314 if (new < 0 || new >= filecnt)
315 return;
316
317 win_set_cursor(&win, CURSOR_WATCH);
318 reset_timeout(slideshow);
319
320 if (new != fileidx)
321 alternate = fileidx;
322
323 img_close(&img, false);
324 while (!img_load(&img, &files[new])) {
325 remove_file(new, false);
326 if (new >= filecnt)
327 new = filecnt - 1;
328 else if (new > 0 && new < fileidx)
329 new--;
330 }
331 files[new].loaded = true;
332 fileidx = new;
333
334 info.open = false;
335 open_info();
336
337 if (img.multi.cnt > 0 && img.multi.animate)
338 set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
339 else
340 reset_timeout(animate);
341}
342
343void update_info(void)
344{
345 unsigned int i, fn, fw, n;
346 unsigned int llen = sizeof(win.bar.l), rlen = sizeof(win.bar.r);
347 char *lt = win.bar.l, *rt = win.bar.r, title[TITLE_LEN];
348 const char * mark;
349 bool ow_info;
350
351 for (fw = 0, i = filecnt; i > 0; fw++, i /= 10);
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[fileidx].name);
358 win_set_title(&win, title);
359 }
360
361 /* update bar contents */
362 if (win.bar.h == 0)
363 return;
364 mark = files[fileidx].marked ? "* " : "";
365 if (mode == MODE_THUMB) {
366 if (tns.loadnext >= filecnt) {
367 n = snprintf(rt, rlen, "%s%0*d/%d", mark, fw, fileidx + 1, filecnt);
368 ow_info = true;
369 } else {
370 snprintf(lt, llen, "Loading... %0*d/%d", fw, tns.loadnext, 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, fileidx + 1, filecnt);
387 ow_info = info.cmd == NULL;
388 }
389 if (ow_info) {
390 fn = strlen(files[fileidx].name);
391 if (fn < llen &&
392 win_textwidth(files[fileidx].name, fn, true) +
393 win_textwidth(rt, n, true) < win.w)
394 {
395 strncpy(lt, files[fileidx].name, llen);
396 } else {
397 strncpy(lt, files[fileidx].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.loadnext < 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;
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_draw(&win);
491 win_set_cursor(&win, CURSOR_WATCH);
492 stat(files[fileidx].path, &oldst);
493
494 if ((pid = fork()) == 0) {
495 execl(keyhandler.cmd, keyhandler.cmd, kstr, files[fileidx].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[fileidx].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_draw(&win);
516
517 if (mode == MODE_IMAGE) {
518 img_close(&img, true);
519 load_image(fileidx);
520 }
521 if (!tns_load(&tns, fileidx, &files[fileidx], true, mode == MODE_IMAGE) &&
522 mode == MODE_THUMB)
523 {
524 remove_file(fileidx, false);
525 tns.dirty = true;
526 }
527end:
528 if (restore_bar)
529 memcpy(win.bar.l, oldbar, sizeof(win.bar.l));
530 reset_cursor();
531 redraw();
532}
533
534#define MODMASK(mask) ((mask) & (ShiftMask|ControlMask|Mod1Mask))
535
536void on_keypress(XKeyEvent *kev)
537{
538 int i;
539 unsigned int sh;
540 KeySym ksym, shksym;
541 char key;
542 bool dirty = false;
543
544 if (kev == NULL)
545 return;
546
547 if (kev->state & ShiftMask) {
548 kev->state &= ~ShiftMask;
549 XLookupString(kev, &key, 1, &shksym, NULL);
550 kev->state |= ShiftMask;
551 }
552 XLookupString(kev, &key, 1, &ksym, NULL);
553 sh = (kev->state & ShiftMask) && ksym != shksym ? ShiftMask : 0;
554
555 if (IsModifierKey(ksym))
556 return;
557 if (ksym == XK_Escape && MODMASK(kev->state) == 0) {
558 extprefix = False;
559 } else if (extprefix) {
560 run_key_handler(XKeysymToString(ksym), kev->state & ~sh);
561 extprefix = False;
562 } else if (key >= '0' && key <= '9') {
563 /* number prefix for commands */
564 prefix = prefix * 10 + (int) (key - '0');
565 return;
566 } else for (i = 0; i < ARRLEN(keys); i++) {
567 if (keys[i].ksym == ksym &&
568 MODMASK(keys[i].mask | sh) == MODMASK(kev->state) &&
569 keys[i].cmd >= 0 && keys[i].cmd < CMD_COUNT &&
570 (cmds[keys[i].cmd].mode < 0 || cmds[keys[i].cmd].mode == mode))
571 {
572 if (cmds[keys[i].cmd].func(keys[i].arg))
573 dirty = true;
574 }
575 }
576 if (dirty)
577 redraw();
578 prefix = 0;
579}
580
581void on_buttonpress(XButtonEvent *bev)
582{
583 int i, sel;
584 bool dirty = false;
585 static Time firstclick;
586
587 if (bev == NULL)
588 return;
589
590 if (mode == MODE_IMAGE) {
591 win_set_cursor(&win, CURSOR_ARROW);
592 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
593
594 for (i = 0; i < ARRLEN(buttons); i++) {
595 if (buttons[i].button == bev->button &&
596 MODMASK(buttons[i].mask) == MODMASK(bev->state) &&
597 buttons[i].cmd >= 0 && buttons[i].cmd < CMD_COUNT &&
598 (cmds[buttons[i].cmd].mode < 0 || cmds[buttons[i].cmd].mode == mode))
599 {
600 if (cmds[buttons[i].cmd].func(buttons[i].arg))
601 dirty = true;
602 }
603 }
604 if (dirty)
605 redraw();
606 } else {
607 /* thumbnail mode (hard-coded) */
608 switch (bev->button) {
609 case Button1:
610 if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
611 if (sel != fileidx) {
612 tns_highlight(&tns, fileidx, false);
613 tns_highlight(&tns, sel, true);
614 fileidx = sel;
615 firstclick = bev->time;
616 redraw();
617 } else if (bev->time - firstclick <= TO_DOUBLE_CLICK) {
618 mode = MODE_IMAGE;
619 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
620 load_image(fileidx);
621 redraw();
622 } else {
623 firstclick = bev->time;
624 }
625 }
626 break;
627 case Button3:
628 if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
629 files[sel].marked = !files[sel].marked;
630 tns_mark(&tns, sel, files[sel].marked);
631 redraw();
632 }
633 break;
634 case Button4:
635 case Button5:
636 if (tns_scroll(&tns, bev->button == Button4 ? DIR_UP : DIR_DOWN,
637 (bev->state & ControlMask) != 0))
638 redraw();
639 break;
640 }
641 }
642 prefix = 0;
643}
644
645void run(void)
646{
647 int xfd;
648 fd_set fds;
649 struct timeval timeout;
650 bool discard, to_set;
651 XEvent ev, nextev;
652
653 set_timeout(redraw, 25, false);
654
655 while (true) {
656 while (mode == MODE_THUMB && tns.loadnext < filecnt &&
657 XPending(win.env.dpy) == 0)
658 {
659 /* load thumbnails */
660 set_timeout(redraw, TO_REDRAW_THUMBS, false);
661 if (tns_load(&tns, tns.loadnext, &files[tns.loadnext], false, false)) {
662 if (tns.cnt == tns.loadnext)
663 tns.cnt++;
664 } else {
665 remove_file(tns.loadnext, false);
666 }
667 while (tns.loadnext < filecnt && tns.thumbs[tns.loadnext].loaded)
668 tns.loadnext++;
669 if (tns.loadnext >= filecnt)
670 redraw();
671 else
672 check_timeouts(NULL);
673 }
674
675 while (XPending(win.env.dpy) == 0
676 && ((to_set = check_timeouts(&timeout)) || info.fd != -1))
677 {
678 /* check for timeouts & input */
679 xfd = ConnectionNumber(win.env.dpy);
680 FD_ZERO(&fds);
681 FD_SET(xfd, &fds);
682 if (info.fd != -1) {
683 FD_SET(info.fd, &fds);
684 xfd = MAX(xfd, info.fd);
685 }
686 select(xfd + 1, &fds, 0, 0, to_set ? &timeout : NULL);
687 if (info.fd != -1 && FD_ISSET(info.fd, &fds))
688 read_info();
689 }
690
691 do {
692 XNextEvent(win.env.dpy, &ev);
693 discard = false;
694 if (XEventsQueued(win.env.dpy, QueuedAlready) > 0) {
695 XPeekEvent(win.env.dpy, &nextev);
696 switch (ev.type) {
697 case ConfigureNotify:
698 discard = ev.type == nextev.type;
699 break;
700 case KeyPress:
701 discard = (nextev.type == KeyPress || nextev.type == KeyRelease)
702 && ev.xkey.keycode == nextev.xkey.keycode;
703 break;
704 }
705 }
706 } while (discard);
707
708 switch (ev.type) {
709 /* handle events */
710 case ButtonPress:
711 on_buttonpress(&ev.xbutton);
712 break;
713 case ClientMessage:
714 if ((Atom) ev.xclient.data.l[0] == atoms[ATOM_WM_DELETE_WINDOW])
715 return;
716 break;
717 case ConfigureNotify:
718 if (win_configure(&win, &ev.xconfigure)) {
719 if (mode == MODE_IMAGE) {
720 img.dirty = true;
721 img.checkpan = true;
722 } else {
723 tns.dirty = true;
724 }
725 if (!resized || win.fullscreen) {
726 redraw();
727 set_timeout(clear_resize, TO_REDRAW_RESIZE, false);
728 resized = true;
729 } else {
730 set_timeout(redraw, TO_REDRAW_RESIZE, false);
731 }
732 }
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, 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, &fileidx);
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 win_set_cursor(&win, CURSOR_WATCH);
870
871 run();
872 cleanup();
873
874 return 0;
875}