Krolyxon
Commit 9a7e97c

Branches

Refs →

nsxivmain.c

904 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#include <stdlib.h>
20#include <stdio.h>
21#include <stdarg.h>
22#include <string.h>
23#include <fcntl.h>
24#include <unistd.h>
25#include <errno.h>
26#include <signal.h>
27#include <sys/select.h>
28#include <sys/stat.h>
29#include <sys/time.h>
30#include <sys/wait.h>
31#include <X11/keysym.h>
32#include <X11/XF86keysym.h>
33
34#include "types.h"
35#include "commands.h"
36#include "image.h"
37#include "options.h"
38#include "thumbs.h"
39#include "util.h"
40#include "window.h"
41
42#define _MAPPINGS_CONFIG
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, bool given)
116{
117 const char *bn;
118
119 if (*filename == '\0')
120 return;
121
122 if (access(filename, R_OK) < 0) {
123 if (given)
124 warn("could not open file: %s", filename);
125 return;
126 }
127
128 if (fileidx == filecnt) {
129 filecnt *= 2;
130 files = s_realloc(files, filecnt * sizeof(*files));
131 memset(&files[filecnt/2], 0, filecnt/2 * sizeof(*files));
132 }
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
139 files[fileidx].name = s_strdup(filename);
140 if ((bn = strrchr(files[fileidx].name , '/')) != NULL && bn[1] != '\0')
141 files[fileidx].base = ++bn;
142 else
143 files[fileidx].base = files[fileidx].name;
144 if (given)
145 files[fileidx].flags |= FF_WARN;
146 fileidx++;
147}
148
149void remove_file(int n, bool manual)
150{
151 if (n < 0 || n >= filecnt)
152 return;
153
154 if (filecnt == 1) {
155 if (!manual)
156 fprintf(stderr, "sxiv: no more files to display, aborting\n");
157 cleanup();
158 exit(manual ? EXIT_SUCCESS : EXIT_FAILURE);
159 }
160 if (files[n].flags & FF_MARK)
161 markcnt--;
162
163 if (files[n].path != files[n].name)
164 free((void*) files[n].path);
165 free((void*) files[n].name);
166
167 if (n + 1 < filecnt) {
168 if (tns.thumbs != NULL) {
169 memmove(tns.thumbs + n, tns.thumbs + n + 1, (filecnt - n - 1) *
170 sizeof(*tns.thumbs));
171 memset(tns.thumbs + filecnt - 1, 0, sizeof(*tns.thumbs));
172 }
173 memmove(files + n, files + n + 1, (filecnt - n - 1) * sizeof(*files));
174 }
175 filecnt--;
176 if (fileidx >= filecnt)
177 fileidx = filecnt - 1;
178 if (n < alternate)
179 alternate--;
180}
181
182void set_timeout(timeout_f handler, int time, bool overwrite)
183{
184 int i;
185
186 for (i = 0; i < ARRLEN(timeouts); i++) {
187 if (timeouts[i].handler == handler) {
188 if (!timeouts[i].active || overwrite) {
189 gettimeofday(&timeouts[i].when, 0);
190 TV_ADD_MSEC(&timeouts[i].when, time);
191 timeouts[i].active = true;
192 }
193 return;
194 }
195 }
196}
197
198void reset_timeout(timeout_f handler)
199{
200 int i;
201
202 for (i = 0; i < ARRLEN(timeouts); i++) {
203 if (timeouts[i].handler == handler) {
204 timeouts[i].active = false;
205 return;
206 }
207 }
208}
209
210bool check_timeouts(struct timeval *t)
211{
212 int i = 0, tdiff, tmin = -1;
213 struct timeval now;
214
215 while (i < ARRLEN(timeouts)) {
216 if (timeouts[i].active) {
217 gettimeofday(&now, 0);
218 tdiff = TV_DIFF(&timeouts[i].when, &now);
219 if (tdiff <= 0) {
220 timeouts[i].active = false;
221 if (timeouts[i].handler != NULL)
222 timeouts[i].handler();
223 i = tmin = -1;
224 } else if (tmin < 0 || tdiff < tmin) {
225 tmin = tdiff;
226 }
227 }
228 i++;
229 }
230 if (tmin > 0 && t != NULL)
231 TV_SET_MSEC(t, tmin);
232 return tmin > 0;
233}
234
235void open_info(void)
236{
237 static pid_t pid;
238 int pfd[2];
239
240 if (info.cmd == NULL || info.open || win.bar.h == 0)
241 return;
242 if (info.fd != -1) {
243 close(info.fd);
244 kill(pid, SIGTERM);
245 info.fd = -1;
246 }
247 win.bar.l.buf[0] = '\0';
248
249 if (pipe(pfd) < 0)
250 return;
251 if ((pid = fork()) == 0) {
252 close(pfd[0]);
253 dup2(pfd[1], 1);
254 execl(info.cmd, info.cmd, files[fileidx].name, NULL);
255 warn("could not exec: %s", info.cmd);
256 exit(EXIT_FAILURE);
257 }
258 close(pfd[1]);
259 if (pid < 0) {
260 close(pfd[0]);
261 } else {
262 fcntl(pfd[0], F_SETFL, O_NONBLOCK);
263 info.fd = pfd[0];
264 info.i = info.lastsep = 0;
265 info.open = true;
266 }
267}
268
269void read_info(void)
270{
271 ssize_t i, n;
272 char buf[BAR_L_LEN];
273
274 while (true) {
275 n = read(info.fd, buf, sizeof(buf));
276 if (n < 0 && errno == EAGAIN)
277 return;
278 else if (n == 0)
279 goto end;
280 for (i = 0; i < n; i++) {
281 if (buf[i] == '\n') {
282 if (info.lastsep == 0) {
283 win.bar.l.buf[info.i++] = ' ';
284 info.lastsep = 1;
285 }
286 } else {
287 win.bar.l.buf[info.i++] = buf[i];
288 info.lastsep = 0;
289 }
290 if (info.i + 1 == win.bar.l.size)
291 goto end;
292 }
293 }
294end:
295 info.i -= info.lastsep;
296 win.bar.l.buf[info.i] = '\0';
297 win_draw(&win);
298 close(info.fd);
299 info.fd = -1;
300 while (waitpid(-1, NULL, WNOHANG) > 0);
301}
302
303void load_image(int new)
304{
305 static int current;
306
307 if (new < 0 || new >= filecnt)
308 return;
309
310 if (win.xwin != None)
311 win_set_cursor(&win, CURSOR_WATCH);
312 reset_timeout(slideshow);
313
314 if (new != current)
315 alternate = current;
316
317 img_close(&img, false);
318 while (!img_load(&img, &files[new])) {
319 remove_file(new, false);
320 if (new >= filecnt)
321 new = filecnt - 1;
322 else if (new > 0 && new < fileidx)
323 new--;
324 }
325 files[new].flags &= ~FF_WARN;
326 fileidx = current = new;
327
328 info.open = false;
329 open_info();
330
331 if (img.multi.cnt > 0 && img.multi.animate)
332 set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
333 else
334 reset_timeout(animate);
335}
336
337void bar_put(win_bar_t *bar, const char *fmt, ...)
338{
339 size_t len = bar->size - (bar->p - bar->buf), n;
340 va_list ap;
341
342 va_start(ap, fmt);
343 n = vsnprintf(bar->p, len, fmt, ap);
344 bar->p += MIN(len, n);
345 va_end(ap);
346}
347
348void update_info(void)
349{
350 unsigned int i, fn, fw;
351 char title[TITLE_LEN];
352 const char * mark;
353 bool ow_info;
354 win_bar_t *l = &win.bar.l, *r = &win.bar.r;
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 for (fw = 0, i = filecnt; i > 0; fw++, i /= 10);
368 mark = files[fileidx].flags & FF_MARK ? "* " : "";
369 l->p = l->buf;
370 r->p = r->buf;
371 if (mode == MODE_THUMB) {
372 if (tns.loadnext < tns.end) {
373 bar_put(l, "Loading... %0*d", fw, tns.loadnext + 1);
374 ow_info = false;
375 } else if (tns.initnext < filecnt) {
376 bar_put(l, "Caching... %0*d", fw, tns.initnext + 1);
377 ow_info = false;
378 } else {
379 ow_info = true;
380 }
381 bar_put(r, "%s%0*d/%d", mark, fw, fileidx + 1, filecnt);
382 } else {
383 bar_put(r, "%s", mark);
384 if (img.ss.on)
385 bar_put(r, "%ds | ", img.ss.delay);
386 if (img.gamma != 0)
387 bar_put(r, "G%+d | ", img.gamma);
388 bar_put(r, "%3d%% | ", (int) (img.zoom * 100.0));
389 if (img.multi.cnt > 0) {
390 for (fn = 0, i = img.multi.cnt; i > 0; fn++, i /= 10);
391 bar_put(r, "%0*d/%d | ", fn, img.multi.sel + 1, img.multi.cnt);
392 }
393 bar_put(r, "%0*d/%d", fw, fileidx + 1, filecnt);
394 ow_info = info.cmd == NULL;
395 }
396 if (ow_info) {
397 fn = strlen(files[fileidx].name);
398 if (fn < l->size &&
399 win_textwidth(files[fileidx].name, fn, true) +
400 win_textwidth(r->buf, r->p - r->buf, true) < win.w)
401 {
402 strncpy(l->buf, files[fileidx].name, l->size);
403 } else {
404 strncpy(l->buf, files[fileidx].base, l->size);
405 }
406 }
407}
408
409void redraw(void)
410{
411 int t;
412
413 if (mode == MODE_IMAGE) {
414 img_render(&img);
415 if (img.ss.on) {
416 t = img.ss.delay * 1000;
417 if (img.multi.cnt > 0 && img.multi.animate)
418 t = MAX(t, img.multi.length);
419 set_timeout(slideshow, t, false);
420 }
421 } else {
422 tns_render(&tns);
423 }
424 update_info();
425 win_draw(&win);
426 reset_timeout(redraw);
427 reset_cursor();
428}
429
430void reset_cursor(void)
431{
432 int i;
433 cursor_t cursor = CURSOR_NONE;
434
435 if (mode == MODE_IMAGE) {
436 for (i = 0; i < ARRLEN(timeouts); i++) {
437 if (timeouts[i].handler == reset_cursor) {
438 if (timeouts[i].active)
439 cursor = CURSOR_ARROW;
440 break;
441 }
442 }
443 } else {
444 if (tns.loadnext < tns.end || tns.initnext < filecnt)
445 cursor = CURSOR_WATCH;
446 else
447 cursor = CURSOR_ARROW;
448 }
449 win_set_cursor(&win, cursor);
450}
451
452void animate(void)
453{
454 if (img_frame_animate(&img)) {
455 redraw();
456 set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
457 }
458}
459
460void slideshow(void)
461{
462 load_image(fileidx + 1 < filecnt ? fileidx + 1 : 0);
463 redraw();
464}
465
466void clear_resize(void)
467{
468 resized = false;
469}
470
471void run_key_handler(const char *key, unsigned int mask)
472{
473 pid_t pid;
474 FILE *pfs;
475 bool marked = mode == MODE_THUMB && markcnt > 0;
476 bool changed = false;
477 int f, i, pfd[2], retval, status;
478 int fcnt = marked ? markcnt : 1;
479 char kstr[32];
480 struct stat *oldst, st;
481
482 if (keyhandler.cmd == NULL) {
483 if (!keyhandler.warned) {
484 warn("key handler not installed");
485 keyhandler.warned = true;
486 }
487 return;
488 }
489 if (key == NULL)
490 return;
491
492 if (pipe(pfd) < 0) {
493 warn("could not create pipe for key handler");
494 return;
495 }
496 if ((pfs = fdopen(pfd[1], "w")) == NULL) {
497 close(pfd[0]), close(pfd[1]);
498 warn("could not open pipe for key handler");
499 return;
500 }
501 oldst = s_malloc(fcnt * sizeof(*oldst));
502
503 strncpy(win.bar.l.buf, "Running key handler...", win.bar.l.size);
504 win_draw(&win);
505 win_set_cursor(&win, CURSOR_WATCH);
506
507 snprintf(kstr, sizeof(kstr), "%s%s%s%s",
508 mask & ControlMask ? "C-" : "",
509 mask & Mod1Mask ? "M-" : "",
510 mask & ShiftMask ? "S-" : "", key);
511
512 if ((pid = fork()) == 0) {
513 close(pfd[1]);
514 dup2(pfd[0], 0);
515 execl(keyhandler.cmd, keyhandler.cmd, kstr, NULL);
516 warn("could not exec key handler");
517 exit(EXIT_FAILURE);
518 }
519 close(pfd[0]);
520 if (pid < 0) {
521 fclose(pfs);
522 warn("could not fork key handler");
523 goto end;
524 }
525
526 for (f = i = 0; f < fcnt; i++) {
527 if ((marked && (files[i].flags & FF_MARK)) || (!marked && i == fileidx)) {
528 stat(files[i].path, &oldst[f]);
529 fprintf(pfs, "%s\n", files[i].path);
530 f++;
531 }
532 }
533 fclose(pfs);
534 waitpid(pid, &status, 0);
535 retval = WEXITSTATUS(status);
536 if (WIFEXITED(status) == 0 || retval != 0)
537 warn("key handler exited with non-zero return value: %d", retval);
538
539 for (f = i = 0; f < fcnt; i++) {
540 if ((marked && (files[i].flags & FF_MARK)) || (!marked && i == fileidx)) {
541 if (stat(files[i].path, &st) != 0 ||
542 memcmp(&oldst[f].st_mtime, &st.st_mtime, sizeof(st.st_mtime)) != 0)
543 {
544 if (tns.thumbs != NULL) {
545 tns_unload(&tns, i);
546 tns.loadnext = MIN(tns.loadnext, i);
547 }
548 changed = true;
549 }
550 f++;
551 }
552 }
553end:
554 if (mode == MODE_IMAGE) {
555 if (changed) {
556 img_close(&img, true);
557 load_image(fileidx);
558 } else if (info.cmd != NULL) {
559 info.open = false;
560 open_info();
561 }
562 }
563 free(oldst);
564 reset_cursor();
565 redraw();
566}
567
568#define MODMASK(mask) ((mask) & (ShiftMask|ControlMask|Mod1Mask))
569
570void on_keypress(XKeyEvent *kev)
571{
572 int i;
573 unsigned int sh;
574 KeySym ksym, shksym;
575 char key;
576 bool dirty = false;
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 (mode == MODE_IMAGE) {
619 win_set_cursor(&win, CURSOR_ARROW);
620 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
621
622 for (i = 0; i < ARRLEN(buttons); i++) {
623 if (buttons[i].button == bev->button &&
624 MODMASK(buttons[i].mask) == MODMASK(bev->state) &&
625 buttons[i].cmd >= 0 && buttons[i].cmd < CMD_COUNT &&
626 (cmds[buttons[i].cmd].mode < 0 || cmds[buttons[i].cmd].mode == mode))
627 {
628 if (cmds[buttons[i].cmd].func(buttons[i].arg))
629 dirty = true;
630 }
631 }
632 if (dirty)
633 redraw();
634 } else {
635 /* thumbnail mode (hard-coded) */
636 switch (bev->button) {
637 case Button1:
638 if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
639 if (sel != fileidx) {
640 tns_highlight(&tns, fileidx, false);
641 tns_highlight(&tns, sel, true);
642 fileidx = sel;
643 firstclick = bev->time;
644 redraw();
645 } else if (bev->time - firstclick <= TO_DOUBLE_CLICK) {
646 mode = MODE_IMAGE;
647 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
648 load_image(fileidx);
649 redraw();
650 } else {
651 firstclick = bev->time;
652 }
653 }
654 break;
655 case Button3:
656 if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
657 files[sel].flags ^= FF_MARK;
658 markcnt += files[sel].flags & FF_MARK ? 1 : -1;
659 tns_mark(&tns, sel, !!(files[sel].flags & FF_MARK));
660 redraw();
661 }
662 break;
663 case Button4:
664 case Button5:
665 if (tns_scroll(&tns, bev->button == Button4 ? DIR_UP : DIR_DOWN,
666 (bev->state & ControlMask) != 0))
667 redraw();
668 break;
669 }
670 }
671 prefix = 0;
672}
673
674void run(void)
675{
676 int xfd;
677 fd_set fds;
678 struct timeval timeout;
679 bool discard, init_thumb, load_thumb, to_set;
680 XEvent ev, nextev;
681
682 while (true) {
683 to_set = check_timeouts(&timeout);
684 init_thumb = mode == MODE_THUMB && tns.initnext < filecnt;
685 load_thumb = mode == MODE_THUMB && tns.loadnext < tns.end;
686
687 if ((init_thumb || load_thumb || to_set || info.fd != -1) &&
688 XPending(win.env.dpy) == 0)
689 {
690 if (load_thumb) {
691 set_timeout(redraw, TO_REDRAW_THUMBS, false);
692 if (!tns_load(&tns, tns.loadnext, false, false)) {
693 remove_file(tns.loadnext, false);
694 tns.dirty = true;
695 }
696 if (tns.loadnext >= tns.end)
697 redraw();
698 } else if (init_thumb) {
699 set_timeout(redraw, TO_REDRAW_THUMBS, false);
700 if (!tns_load(&tns, tns.initnext, false, true))
701 remove_file(tns.initnext, false);
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 signal(SIGPIPE, SIG_IGN);
789
790 parse_options(argc, argv);
791
792 if (options->clean_cache) {
793 tns_init(&tns, NULL, NULL, NULL, NULL);
794 tns_clean_cache(&tns);
795 exit(EXIT_SUCCESS);
796 }
797
798 if (options->filecnt == 0 && !options->from_stdin) {
799 print_usage();
800 exit(EXIT_FAILURE);
801 }
802
803 if (options->recursive || options->from_stdin)
804 filecnt = FILENAME_CNT;
805 else
806 filecnt = options->filecnt;
807
808 files = s_malloc(filecnt * sizeof(*files));
809 memset(files, 0, filecnt * sizeof(*files));
810 fileidx = 0;
811
812 if (options->from_stdin) {
813 n = 0;
814 filename = NULL;
815 while ((len = getline(&filename, &n, stdin)) > 0) {
816 if (filename[len-1] == '\n')
817 filename[len-1] = '\0';
818 check_add_file(filename, true);
819 }
820 free(filename);
821 }
822
823 for (i = 0; i < options->filecnt; i++) {
824 filename = options->filenames[i];
825
826 if (stat(filename, &fstats) < 0) {
827 warn("could not stat file: %s", filename);
828 continue;
829 }
830 if (!S_ISDIR(fstats.st_mode)) {
831 check_add_file(filename, true);
832 } else {
833 if (!options->recursive) {
834 warn("ignoring directory: %s", filename);
835 continue;
836 }
837 if (r_opendir(&dir, filename) < 0) {
838 warn("could not open directory: %s", filename);
839 continue;
840 }
841 start = fileidx;
842 while ((filename = r_readdir(&dir)) != NULL) {
843 check_add_file(filename, false);
844 free((void*) filename);
845 }
846 r_closedir(&dir);
847 if (fileidx - start > 1)
848 qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp);
849 }
850 }
851
852 if (fileidx == 0) {
853 fprintf(stderr, "sxiv: no valid image file given, aborting\n");
854 exit(EXIT_FAILURE);
855 }
856
857 filecnt = fileidx;
858 fileidx = options->startnum < filecnt ? options->startnum : 0;
859
860 win_init(&win);
861 img_init(&img, &win);
862
863 if ((homedir = getenv("XDG_CONFIG_HOME")) == NULL || homedir[0] == '\0') {
864 homedir = getenv("HOME");
865 dsuffix = "/.config";
866 }
867 if (homedir != NULL) {
868 char **cmd[] = { &info.cmd, &keyhandler.cmd };
869 const char *name[] = { "image-info", "key-handler" };
870
871 for (i = 0; i < ARRLEN(cmd); i++) {
872 n = strlen(homedir) + strlen(dsuffix) + strlen(name[i]) + 12;
873 *cmd[i] = (char*) s_malloc(n);
874 snprintf(*cmd[i], n, "%s%s/sxiv/exec/%s", homedir, dsuffix, name[i]);
875 if (access(*cmd[i], X_OK) != 0) {
876 free(*cmd[i]);
877 *cmd[i] = NULL;
878 }
879 }
880 } else {
881 warn("could not locate exec directory");
882 }
883 info.fd = -1;
884
885 if (options->thumb_mode) {
886 mode = MODE_THUMB;
887 tns_init(&tns, files, &filecnt, &fileidx, &win);
888 while (!tns_load(&tns, fileidx, false, false))
889 remove_file(fileidx, false);
890 } else {
891 mode = MODE_IMAGE;
892 tns.thumbs = NULL;
893 load_image(fileidx);
894 }
895 win_open(&win);
896 win_set_cursor(&win, CURSOR_WATCH);
897
898 set_timeout(redraw, 25, false);
899
900 run();
901 cleanup();
902
903 return 0;
904}