Krolyxon
Commit 86dc686

Branches

Refs →

nsxivmain.c

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