Krolyxon
Commit 25077ac

Branches

Refs →

nsxivmain.c

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