Krolyxon
Commit 851e428

Branches

Refs →

nsxivmain.c

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