Krolyxon
Commit ddd028e

Branches

Refs →

nsxivmain.c

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