Krolyxon
Commit 4db3029

Branches

Refs →

nsxivmain.c

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