Krolyxon
Commit c33f2ad

Branches

Refs →

nsxivmain.c

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