Krolyxon
Commit 9c92de8

Branches

Refs →

nsxivmain.c

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