Krolyxon
Commit 284be74

Branches

Refs →

nsxivmain.c

879 lines · 19.2 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;
76int markcnt;
77
78int prefix;
79bool extprefix;
80
81bool resized = false;
82
83struct {
84 char *cmd;
85 int fd;
86 unsigned int i, lastsep;
87 bool open;
88} info;
89
90struct {
91 char *cmd;
92 bool warned;
93} keyhandler;
94
95timeout_t timeouts[] = {
96 { { 0, 0 }, false, redraw },
97 { { 0, 0 }, false, reset_cursor },
98 { { 0, 0 }, false, animate },
99 { { 0, 0 }, false, slideshow },
100 { { 0, 0 }, false, clear_resize },
101};
102
103void cleanup(void)
104{
105 static bool in = false;
106
107 if (!in) {
108 in = true;
109 img_close(&img, false);
110 tns_free(&tns);
111 win_close(&win);
112 }
113}
114
115void check_add_file(char *filename)
116{
117 const char *bn;
118
119 if (filename == NULL || *filename == '\0')
120 return;
121
122 if (access(filename, R_OK) < 0) {
123 warn("could not open file: %s", filename);
124 return;
125 }
126
127 if (fileidx == filecnt) {
128 filecnt *= 2;
129 files = (fileinfo_t*) s_realloc(files, filecnt * sizeof(fileinfo_t));
130 }
131
132#if defined _BSD_SOURCE || defined _XOPEN_SOURCE && \
133 ((_XOPEN_SOURCE - 0) >= 500 || defined _XOPEN_SOURCE_EXTENDED)
134
135 if ((files[fileidx].path = realpath(filename, NULL)) == NULL) {
136 warn("could not get real path of file: %s\n", filename);
137 return;
138 }
139#else
140 if (*filename != '/') {
141 if ((files[fileidx].path = absolute_path(filename)) == NULL) {
142 warn("could not get absolute path of file: %s\n", filename);
143 return;
144 }
145 } else {
146 files[fileidx].path = NULL;
147 }
148#endif
149
150 files[fileidx].loaded = false;
151 files[fileidx].name = s_strdup(filename);
152 if (files[fileidx].path == NULL)
153 files[fileidx].path = files[fileidx].name;
154 if ((bn = strrchr(files[fileidx].name , '/')) != NULL && bn[1] != '\0')
155 files[fileidx].base = ++bn;
156 else
157 files[fileidx].base = files[fileidx].name;
158 fileidx++;
159}
160
161void remove_file(int n, bool manual)
162{
163 if (n < 0 || n >= filecnt)
164 return;
165
166 if (filecnt == 1) {
167 if (!manual)
168 fprintf(stderr, "sxiv: no more files to display, aborting\n");
169 cleanup();
170 exit(manual ? EXIT_SUCCESS : EXIT_FAILURE);
171 }
172 if (files[n].marked)
173 markcnt--;
174
175 if (files[n].path != files[n].name)
176 free((void*) files[n].path);
177 free((void*) files[n].name);
178
179 if (n + 1 < filecnt)
180 memmove(files + n, files + n + 1, (filecnt - n - 1) * sizeof(fileinfo_t));
181 if (n + 1 < tns.cnt) {
182 memmove(tns.thumbs + n, tns.thumbs + n + 1, (tns.cnt - n - 1) *
183 sizeof(thumb_t));
184 memset(tns.thumbs + tns.cnt - 1, 0, sizeof(thumb_t));
185 }
186
187 filecnt--;
188 if (n < tns.cnt)
189 tns.cnt--;
190 if (mode == MODE_THUMB && fileidx >= tns.cnt)
191 fileidx = tns.cnt - 1;
192 if (n < alternate)
193 alternate--;
194}
195
196void set_timeout(timeout_f handler, int time, bool overwrite)
197{
198 int i;
199
200 for (i = 0; i < ARRLEN(timeouts); i++) {
201 if (timeouts[i].handler == handler) {
202 if (!timeouts[i].active || overwrite) {
203 gettimeofday(&timeouts[i].when, 0);
204 TV_ADD_MSEC(&timeouts[i].when, time);
205 timeouts[i].active = true;
206 }
207 return;
208 }
209 }
210}
211
212void reset_timeout(timeout_f handler)
213{
214 int i;
215
216 for (i = 0; i < ARRLEN(timeouts); i++) {
217 if (timeouts[i].handler == handler) {
218 timeouts[i].active = false;
219 return;
220 }
221 }
222}
223
224bool check_timeouts(struct timeval *t)
225{
226 int i = 0, tdiff, tmin = -1;
227 struct timeval now;
228
229 while (i < ARRLEN(timeouts)) {
230 if (timeouts[i].active) {
231 gettimeofday(&now, 0);
232 tdiff = TV_DIFF(&timeouts[i].when, &now);
233 if (tdiff <= 0) {
234 timeouts[i].active = false;
235 if (timeouts[i].handler != NULL)
236 timeouts[i].handler();
237 i = tmin = -1;
238 } else if (tmin < 0 || tdiff < tmin) {
239 tmin = tdiff;
240 }
241 }
242 i++;
243 }
244 if (tmin > 0 && t != NULL)
245 TV_SET_MSEC(t, tmin);
246 return tmin > 0;
247}
248
249void open_info(void)
250{
251 static pid_t pid;
252 int pfd[2];
253
254 if (info.cmd == NULL || info.open || win.bar.h == 0)
255 return;
256 if (info.fd != -1) {
257 close(info.fd);
258 kill(pid, SIGTERM);
259 info.fd = -1;
260 }
261 win.bar.l[0] = '\0';
262
263 if (pipe(pfd) < 0)
264 return;
265 pid = fork();
266 if (pid > 0) {
267 close(pfd[1]);
268 fcntl(pfd[0], F_SETFL, O_NONBLOCK);
269 info.fd = pfd[0];
270 info.i = info.lastsep = 0;
271 info.open = true;
272 } else if (pid == 0) {
273 close(pfd[0]);
274 dup2(pfd[1], 1);
275 execl(info.cmd, info.cmd, files[fileidx].name, NULL);
276 warn("could not exec: %s", info.cmd);
277 exit(EXIT_FAILURE);
278 }
279}
280
281void read_info(void)
282{
283 ssize_t i, n;
284 char buf[BAR_L_LEN];
285
286 while (true) {
287 n = read(info.fd, buf, sizeof(buf));
288 if (n < 0 && errno == EAGAIN)
289 return;
290 else if (n == 0)
291 goto end;
292 for (i = 0; i < n; i++) {
293 if (buf[i] == '\n') {
294 if (info.lastsep == 0) {
295 win.bar.l[info.i++] = ' ';
296 info.lastsep = 1;
297 }
298 } else {
299 win.bar.l[info.i++] = buf[i];
300 info.lastsep = 0;
301 }
302 if (info.i + 1 == sizeof(win.bar.l))
303 goto end;
304 }
305 }
306end:
307 info.i -= info.lastsep;
308 win.bar.l[info.i] = '\0';
309 win_draw(&win);
310 close(info.fd);
311 info.fd = -1;
312 while (waitpid(-1, NULL, WNOHANG) > 0);
313}
314
315void load_image(int new)
316{
317 if (new < 0 || new >= filecnt)
318 return;
319
320 win_set_cursor(&win, CURSOR_WATCH);
321 reset_timeout(slideshow);
322
323 if (new != fileidx)
324 alternate = fileidx;
325
326 img_close(&img, false);
327 while (!img_load(&img, &files[new])) {
328 remove_file(new, false);
329 if (new >= filecnt)
330 new = filecnt - 1;
331 else if (new > 0 && new < fileidx)
332 new--;
333 }
334 files[new].loaded = true;
335 fileidx = new;
336
337 info.open = false;
338 open_info();
339
340 if (img.multi.cnt > 0 && img.multi.animate)
341 set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
342 else
343 reset_timeout(animate);
344}
345
346void update_info(void)
347{
348 unsigned int i, fn, fw, n;
349 unsigned int llen = sizeof(win.bar.l), rlen = sizeof(win.bar.r);
350 char *lt = win.bar.l, *rt = win.bar.r, title[TITLE_LEN];
351 const char * mark;
352 bool ow_info;
353
354 for (fw = 0, i = filecnt; i > 0; fw++, i /= 10);
355
356 /* update window title */
357 if (mode == MODE_THUMB) {
358 win_set_title(&win, "sxiv");
359 } else {
360 snprintf(title, sizeof(title), "sxiv - %s", files[fileidx].name);
361 win_set_title(&win, title);
362 }
363
364 /* update bar contents */
365 if (win.bar.h == 0)
366 return;
367 mark = files[fileidx].marked ? "* " : "";
368 if (mode == MODE_THUMB) {
369 if (tns.loadnext >= filecnt) {
370 n = snprintf(rt, rlen, "%s%0*d/%d", mark, fw, fileidx + 1, filecnt);
371 ow_info = true;
372 } else {
373 snprintf(lt, llen, "Loading... %0*d/%d", fw, tns.loadnext, filecnt);
374 rt[0] = '\0';
375 ow_info = false;
376 }
377 } else {
378 n = snprintf(rt, rlen, "%s", mark);
379 if (img.ss.on)
380 n += snprintf(rt + n, rlen - n, "%ds | ", img.ss.delay);
381 if (img.gamma != 0)
382 n += snprintf(rt + n, rlen - n, "G%+d | ", img.gamma);
383 n += snprintf(rt + n, rlen - n, "%3d%% | ", (int) (img.zoom * 100.0));
384 if (img.multi.cnt > 0) {
385 for (fn = 0, i = img.multi.cnt; i > 0; fn++, i /= 10);
386 n += snprintf(rt + n, rlen - n, "%0*d/%d | ",
387 fn, img.multi.sel + 1, img.multi.cnt);
388 }
389 n += snprintf(rt + n, rlen - n, "%0*d/%d", fw, fileidx + 1, filecnt);
390 ow_info = info.cmd == NULL;
391 }
392 if (ow_info) {
393 fn = strlen(files[fileidx].name);
394 if (fn < llen &&
395 win_textwidth(files[fileidx].name, fn, true) +
396 win_textwidth(rt, n, true) < win.w)
397 {
398 strncpy(lt, files[fileidx].name, llen);
399 } else {
400 strncpy(lt, files[fileidx].base, llen);
401 }
402 }
403}
404
405void redraw(void)
406{
407 int t;
408
409 if (mode == MODE_IMAGE) {
410 img_render(&img);
411 if (img.ss.on) {
412 t = img.ss.delay * 1000;
413 if (img.multi.cnt > 0 && img.multi.animate)
414 t = MAX(t, img.multi.length);
415 set_timeout(slideshow, t, false);
416 }
417 } else {
418 tns_render(&tns);
419 }
420 update_info();
421 win_draw(&win);
422 reset_timeout(redraw);
423 reset_cursor();
424}
425
426void reset_cursor(void)
427{
428 int i;
429 cursor_t cursor = CURSOR_NONE;
430
431 if (mode == MODE_IMAGE) {
432 for (i = 0; i < ARRLEN(timeouts); i++) {
433 if (timeouts[i].handler == reset_cursor) {
434 if (timeouts[i].active)
435 cursor = CURSOR_ARROW;
436 break;
437 }
438 }
439 } else {
440 if (tns.loadnext < filecnt)
441 cursor = CURSOR_WATCH;
442 else
443 cursor = CURSOR_ARROW;
444 }
445 win_set_cursor(&win, cursor);
446}
447
448void animate(void)
449{
450 if (img_frame_animate(&img, false)) {
451 redraw();
452 set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
453 }
454}
455
456void slideshow(void)
457{
458 load_image(fileidx + 1 < filecnt ? fileidx + 1 : 0);
459 redraw();
460}
461
462void clear_resize(void)
463{
464 resized = false;
465}
466
467void run_key_handler(const char *key, unsigned int mask)
468{
469 pid_t pid;
470 int retval, status;
471 char kstr[32], oldbar[sizeof(win.bar.l)];
472 bool restore_bar = mode == MODE_IMAGE && info.cmd != NULL;
473 struct stat oldst, newst;
474
475 if (keyhandler.cmd == NULL) {
476 if (!keyhandler.warned) {
477 warn("key handler not installed");
478 keyhandler.warned = true;
479 }
480 return;
481 }
482 if (key == NULL)
483 return;
484
485 snprintf(kstr, sizeof(kstr), "%s%s%s%s",
486 mask & ControlMask ? "C-" : "",
487 mask & Mod1Mask ? "M-" : "",
488 mask & ShiftMask ? "S-" : "", key);
489
490 if (restore_bar)
491 memcpy(oldbar, win.bar.l, sizeof(win.bar.l));
492 strncpy(win.bar.l, "Running key handler...", sizeof(win.bar.l));
493 win_draw(&win);
494 win_set_cursor(&win, CURSOR_WATCH);
495 stat(files[fileidx].path, &oldst);
496
497 if ((pid = fork()) == 0) {
498 execl(keyhandler.cmd, keyhandler.cmd, kstr, files[fileidx].path, NULL);
499 warn("could not exec key handler");
500 exit(EXIT_FAILURE);
501 } else if (pid < 0) {
502 warn("could not fork key handler");
503 goto end;
504 }
505 waitpid(pid, &status, 0);
506 retval = WEXITSTATUS(status);
507 if (WIFEXITED(status) == 0 || retval != 0)
508 warn("key handler exited with non-zero return value: %d", retval);
509
510 if (stat(files[fileidx].path, &newst) == 0 &&
511 memcmp(&oldst.st_mtime, &newst.st_mtime, sizeof(oldst.st_mtime)) == 0)
512 {
513 /* file has not changed */
514 goto end;
515 }
516 restore_bar = false;
517 strncpy(win.bar.l, "Reloading image...", sizeof(win.bar.l));
518 win_draw(&win);
519
520 if (mode == MODE_IMAGE) {
521 img_close(&img, true);
522 load_image(fileidx);
523 }
524 if (!tns_load(&tns, fileidx, &files[fileidx], true, mode == MODE_IMAGE) &&
525 mode == MODE_THUMB)
526 {
527 remove_file(fileidx, false);
528 tns.dirty = true;
529 }
530end:
531 if (restore_bar)
532 memcpy(win.bar.l, oldbar, sizeof(win.bar.l));
533 reset_cursor();
534 redraw();
535}
536
537#define MODMASK(mask) ((mask) & (ShiftMask|ControlMask|Mod1Mask))
538
539void on_keypress(XKeyEvent *kev)
540{
541 int i;
542 unsigned int sh;
543 KeySym ksym, shksym;
544 char key;
545 bool dirty = false;
546
547 if (kev == NULL)
548 return;
549
550 if (kev->state & ShiftMask) {
551 kev->state &= ~ShiftMask;
552 XLookupString(kev, &key, 1, &shksym, NULL);
553 kev->state |= ShiftMask;
554 }
555 XLookupString(kev, &key, 1, &ksym, NULL);
556 sh = (kev->state & ShiftMask) && ksym != shksym ? ShiftMask : 0;
557
558 if (IsModifierKey(ksym))
559 return;
560 if (ksym == XK_Escape && MODMASK(kev->state) == 0) {
561 extprefix = False;
562 } else if (extprefix) {
563 run_key_handler(XKeysymToString(ksym), kev->state & ~sh);
564 extprefix = False;
565 } else if (key >= '0' && key <= '9') {
566 /* number prefix for commands */
567 prefix = prefix * 10 + (int) (key - '0');
568 return;
569 } else for (i = 0; i < ARRLEN(keys); i++) {
570 if (keys[i].ksym == ksym &&
571 MODMASK(keys[i].mask | sh) == MODMASK(kev->state) &&
572 keys[i].cmd >= 0 && keys[i].cmd < CMD_COUNT &&
573 (cmds[keys[i].cmd].mode < 0 || cmds[keys[i].cmd].mode == mode))
574 {
575 if (cmds[keys[i].cmd].func(keys[i].arg))
576 dirty = true;
577 }
578 }
579 if (dirty)
580 redraw();
581 prefix = 0;
582}
583
584void on_buttonpress(XButtonEvent *bev)
585{
586 int i, sel;
587 bool dirty = false;
588 static Time firstclick;
589
590 if (bev == NULL)
591 return;
592
593 if (mode == MODE_IMAGE) {
594 win_set_cursor(&win, CURSOR_ARROW);
595 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
596
597 for (i = 0; i < ARRLEN(buttons); i++) {
598 if (buttons[i].button == bev->button &&
599 MODMASK(buttons[i].mask) == MODMASK(bev->state) &&
600 buttons[i].cmd >= 0 && buttons[i].cmd < CMD_COUNT &&
601 (cmds[buttons[i].cmd].mode < 0 || cmds[buttons[i].cmd].mode == mode))
602 {
603 if (cmds[buttons[i].cmd].func(buttons[i].arg))
604 dirty = true;
605 }
606 }
607 if (dirty)
608 redraw();
609 } else {
610 /* thumbnail mode (hard-coded) */
611 switch (bev->button) {
612 case Button1:
613 if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
614 if (sel != fileidx) {
615 tns_highlight(&tns, fileidx, false);
616 tns_highlight(&tns, sel, true);
617 fileidx = sel;
618 firstclick = bev->time;
619 redraw();
620 } else if (bev->time - firstclick <= TO_DOUBLE_CLICK) {
621 mode = MODE_IMAGE;
622 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
623 load_image(fileidx);
624 redraw();
625 } else {
626 firstclick = bev->time;
627 }
628 }
629 break;
630 case Button3:
631 if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
632 files[sel].marked = !files[sel].marked;
633 markcnt += files[sel].marked ? 1 : -1;
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.loadnext < 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.loadnext, &files[tns.loadnext], false, false)) {
666 if (tns.cnt == tns.loadnext)
667 tns.cnt++;
668 } else {
669 remove_file(tns.loadnext, false);
670 }
671 while (tns.loadnext < filecnt && tns.thumbs[tns.loadnext].loaded)
672 tns.loadnext++;
673 if (tns.loadnext >= filecnt)
674 redraw();
675 else
676 check_timeouts(NULL);
677 }
678
679 while (XPending(win.env.dpy) == 0
680 && ((to_set = check_timeouts(&timeout)) || info.fd != -1))
681 {
682 /* check for timeouts & input */
683 xfd = ConnectionNumber(win.env.dpy);
684 FD_ZERO(&fds);
685 FD_SET(xfd, &fds);
686 if (info.fd != -1) {
687 FD_SET(info.fd, &fds);
688 xfd = MAX(xfd, info.fd);
689 }
690 select(xfd + 1, &fds, 0, 0, to_set ? &timeout : NULL);
691 if (info.fd != -1 && FD_ISSET(info.fd, &fds))
692 read_info();
693 }
694
695 do {
696 XNextEvent(win.env.dpy, &ev);
697 discard = false;
698 if (XEventsQueued(win.env.dpy, QueuedAlready) > 0) {
699 XPeekEvent(win.env.dpy, &nextev);
700 switch (ev.type) {
701 case ConfigureNotify:
702 discard = ev.type == nextev.type;
703 break;
704 case KeyPress:
705 discard = (nextev.type == KeyPress || nextev.type == KeyRelease)
706 && ev.xkey.keycode == nextev.xkey.keycode;
707 break;
708 }
709 }
710 } while (discard);
711
712 switch (ev.type) {
713 /* handle events */
714 case ButtonPress:
715 on_buttonpress(&ev.xbutton);
716 break;
717 case ClientMessage:
718 if ((Atom) ev.xclient.data.l[0] == atoms[ATOM_WM_DELETE_WINDOW])
719 return;
720 break;
721 case ConfigureNotify:
722 if (win_configure(&win, &ev.xconfigure)) {
723 if (mode == MODE_IMAGE) {
724 img.dirty = true;
725 img.checkpan = true;
726 } else {
727 tns.dirty = true;
728 }
729 if (!resized || win.fullscreen) {
730 redraw();
731 set_timeout(clear_resize, TO_REDRAW_RESIZE, false);
732 resized = true;
733 } else {
734 set_timeout(redraw, TO_REDRAW_RESIZE, false);
735 }
736 }
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, 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, &fileidx);
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 win_set_cursor(&win, CURSOR_WATCH);
874
875 run();
876 cleanup();
877
878 return 0;
879}