Krolyxon
Commit 9a85dd4

Branches

Refs →

nsxivmain.c

894 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
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
461Bool is_input_ev(Display *dpy, XEvent *ev, XPointer arg)
462{
463 return ev->type == ButtonPress || ev->type == KeyPress;
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], status;
473 int fcnt = marked ? markcnt : 1;
474 char kstr[32];
475 struct stat *oldst, st;
476 XEvent dump;
477
478 if (keyhandler.f.err != 0) {
479 if (!keyhandler.warned) {
480 error(0, keyhandler.f.err, "%s", keyhandler.f.cmd);
481 keyhandler.warned = true;
482 }
483 return;
484 }
485 if (key == NULL)
486 return;
487
488 if (pipe(pfd) < 0) {
489 error(0, errno, "pipe");
490 return;
491 }
492 if ((pfs = fdopen(pfd[1], "w")) == NULL) {
493 error(0, errno, "open pipe");
494 close(pfd[0]), close(pfd[1]);
495 return;
496 }
497 oldst = emalloc(fcnt * sizeof(*oldst));
498
499 strncpy(win.bar.l.buf, "Running key handler...", win.bar.l.size);
500 win_draw(&win);
501 win_set_cursor(&win, CURSOR_WATCH);
502
503 snprintf(kstr, sizeof(kstr), "%s%s%s%s",
504 mask & ControlMask ? "C-" : "",
505 mask & Mod1Mask ? "M-" : "",
506 mask & ShiftMask ? "S-" : "", key);
507
508 if ((pid = fork()) == 0) {
509 close(pfd[1]);
510 dup2(pfd[0], 0);
511 execl(keyhandler.f.cmd, keyhandler.f.cmd, kstr, NULL);
512 error(EXIT_FAILURE, errno, "exec: %s", keyhandler.f.cmd);
513 }
514 close(pfd[0]);
515 if (pid < 0) {
516 error(0, errno, "fork");
517 fclose(pfs);
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 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
531 error(0, 0, "%s: Exited abnormally", keyhandler.f.cmd);
532
533 for (f = i = 0; f < fcnt; i++) {
534 if ((marked && (files[i].flags & FF_MARK)) || (!marked && i == fileidx)) {
535 if (stat(files[i].path, &st) != 0 ||
536 memcmp(&oldst[f].st_mtime, &st.st_mtime, sizeof(st.st_mtime)) != 0)
537 {
538 if (tns.thumbs != NULL) {
539 tns_unload(&tns, i);
540 tns.loadnext = MIN(tns.loadnext, i);
541 }
542 changed = true;
543 }
544 f++;
545 }
546 }
547 /* drop user input events that occured while running the key handler */
548 while (XCheckIfEvent(win.env.dpy, &dump, is_input_ev, NULL));
549
550end:
551 if (mode == MODE_IMAGE) {
552 if (changed) {
553 img_close(&img, true);
554 load_image(fileidx);
555 } else if (info.f.err == 0) {
556 info.open = false;
557 open_info();
558 }
559 }
560 free(oldst);
561 reset_cursor();
562 redraw();
563}
564
565#define MODMASK(mask) ((mask) & (ShiftMask|ControlMask|Mod1Mask))
566
567void on_keypress(XKeyEvent *kev)
568{
569 int i;
570 unsigned int sh;
571 KeySym ksym, shksym;
572 char key;
573 bool dirty = false;
574
575 if (kev->state & ShiftMask) {
576 kev->state &= ~ShiftMask;
577 XLookupString(kev, &key, 1, &shksym, NULL);
578 kev->state |= ShiftMask;
579 }
580 XLookupString(kev, &key, 1, &ksym, NULL);
581 sh = (kev->state & ShiftMask) && ksym != shksym ? ShiftMask : 0;
582
583 if (IsModifierKey(ksym))
584 return;
585 if (ksym == XK_Escape && MODMASK(kev->state) == 0) {
586 extprefix = False;
587 } else if (extprefix) {
588 run_key_handler(XKeysymToString(ksym), kev->state & ~sh);
589 extprefix = False;
590 } else if (key >= '0' && key <= '9') {
591 /* number prefix for commands */
592 prefix = prefix * 10 + (int) (key - '0');
593 return;
594 } else for (i = 0; i < ARRLEN(keys); i++) {
595 if (keys[i].ksym == ksym &&
596 MODMASK(keys[i].mask | sh) == MODMASK(kev->state) &&
597 keys[i].cmd >= 0 && keys[i].cmd < CMD_COUNT &&
598 (cmds[keys[i].cmd].mode < 0 || cmds[keys[i].cmd].mode == mode))
599 {
600 if (cmds[keys[i].cmd].func(keys[i].arg))
601 dirty = true;
602 }
603 }
604 if (dirty)
605 redraw();
606 prefix = 0;
607}
608
609void on_buttonpress(XButtonEvent *bev)
610{
611 int i, sel;
612 bool dirty = false;
613 static Time firstclick;
614
615 if (mode == MODE_IMAGE) {
616 win_set_cursor(&win, CURSOR_ARROW);
617 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
618
619 for (i = 0; i < ARRLEN(buttons); i++) {
620 if (buttons[i].button == bev->button &&
621 MODMASK(buttons[i].mask) == MODMASK(bev->state) &&
622 buttons[i].cmd >= 0 && buttons[i].cmd < CMD_COUNT &&
623 (cmds[buttons[i].cmd].mode < 0 || cmds[buttons[i].cmd].mode == mode))
624 {
625 if (cmds[buttons[i].cmd].func(buttons[i].arg))
626 dirty = true;
627 }
628 }
629 if (dirty)
630 redraw();
631 } else {
632 /* thumbnail mode (hard-coded) */
633 switch (bev->button) {
634 case Button1:
635 if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
636 if (sel != fileidx) {
637 tns_highlight(&tns, fileidx, false);
638 tns_highlight(&tns, sel, true);
639 fileidx = sel;
640 firstclick = bev->time;
641 redraw();
642 } else if (bev->time - firstclick <= TO_DOUBLE_CLICK) {
643 mode = MODE_IMAGE;
644 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
645 load_image(fileidx);
646 redraw();
647 } else {
648 firstclick = bev->time;
649 }
650 }
651 break;
652 case Button3:
653 if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
654 files[sel].flags ^= FF_MARK;
655 markcnt += files[sel].flags & FF_MARK ? 1 : -1;
656 tns_mark(&tns, sel, !!(files[sel].flags & FF_MARK));
657 redraw();
658 }
659 break;
660 case Button4:
661 case Button5:
662 if (tns_scroll(&tns, bev->button == Button4 ? DIR_UP : DIR_DOWN,
663 (bev->state & ControlMask) != 0))
664 redraw();
665 break;
666 }
667 }
668 prefix = 0;
669}
670
671void run(void)
672{
673 int xfd;
674 fd_set fds;
675 struct timeval timeout;
676 bool discard, init_thumb, load_thumb, to_set;
677 XEvent ev, nextev;
678
679 while (true) {
680 to_set = check_timeouts(&timeout);
681 init_thumb = mode == MODE_THUMB && tns.initnext < filecnt;
682 load_thumb = mode == MODE_THUMB && tns.loadnext < tns.end;
683
684 if ((init_thumb || load_thumb || to_set || info.fd != -1) &&
685 XPending(win.env.dpy) == 0)
686 {
687 if (load_thumb) {
688 set_timeout(redraw, TO_REDRAW_THUMBS, false);
689 if (!tns_load(&tns, tns.loadnext, false, false)) {
690 remove_file(tns.loadnext, false);
691 tns.dirty = true;
692 }
693 if (tns.loadnext >= tns.end)
694 redraw();
695 } else if (init_thumb) {
696 set_timeout(redraw, TO_REDRAW_THUMBS, false);
697 if (!tns_load(&tns, tns.initnext, false, true))
698 remove_file(tns.initnext, false);
699 } else {
700 xfd = ConnectionNumber(win.env.dpy);
701 FD_ZERO(&fds);
702 FD_SET(xfd, &fds);
703 if (info.fd != -1) {
704 FD_SET(info.fd, &fds);
705 xfd = MAX(xfd, info.fd);
706 }
707 select(xfd + 1, &fds, 0, 0, to_set ? &timeout : NULL);
708 if (info.fd != -1 && FD_ISSET(info.fd, &fds))
709 read_info();
710 }
711 continue;
712 }
713
714 do {
715 XNextEvent(win.env.dpy, &ev);
716 discard = false;
717 if (XEventsQueued(win.env.dpy, QueuedAlready) > 0) {
718 XPeekEvent(win.env.dpy, &nextev);
719 switch (ev.type) {
720 case ConfigureNotify:
721 discard = ev.type == nextev.type;
722 break;
723 case KeyPress:
724 discard = (nextev.type == KeyPress || nextev.type == KeyRelease)
725 && ev.xkey.keycode == nextev.xkey.keycode;
726 break;
727 }
728 }
729 } while (discard);
730
731 switch (ev.type) {
732 /* handle events */
733 case ButtonPress:
734 on_buttonpress(&ev.xbutton);
735 break;
736 case ClientMessage:
737 if ((Atom) ev.xclient.data.l[0] == atoms[ATOM_WM_DELETE_WINDOW])
738 return;
739 break;
740 case ConfigureNotify:
741 if (win_configure(&win, &ev.xconfigure)) {
742 if (mode == MODE_IMAGE) {
743 img.dirty = true;
744 img.checkpan = true;
745 } else {
746 tns.dirty = true;
747 }
748 if (!resized || win.fullscreen) {
749 redraw();
750 set_timeout(clear_resize, TO_REDRAW_RESIZE, false);
751 resized = true;
752 } else {
753 set_timeout(redraw, TO_REDRAW_RESIZE, false);
754 }
755 }
756 break;
757 case KeyPress:
758 on_keypress(&ev.xkey);
759 break;
760 case MotionNotify:
761 if (mode == MODE_IMAGE) {
762 win_set_cursor(&win, CURSOR_ARROW);
763 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
764 }
765 break;
766 }
767 }
768}
769
770int fncmp(const void *a, const void *b)
771{
772 return strcoll(((fileinfo_t*) a)->name, ((fileinfo_t*) b)->name);
773}
774
775int main(int argc, char **argv)
776{
777 int i, start;
778 size_t n;
779 ssize_t len;
780 char *filename;
781 const char *homedir, *dsuffix = "";
782 struct stat fstats;
783 r_dir_t dir;
784
785 signal(SIGPIPE, SIG_IGN);
786
787 parse_options(argc, argv);
788
789 if (options->clean_cache) {
790 tns_init(&tns, NULL, NULL, NULL, NULL);
791 tns_clean_cache(&tns);
792 exit(EXIT_SUCCESS);
793 }
794
795 if (options->filecnt == 0 && !options->from_stdin) {
796 print_usage();
797 exit(EXIT_FAILURE);
798 }
799
800 if (options->recursive || options->from_stdin)
801 filecnt = 1024;
802 else
803 filecnt = options->filecnt;
804
805 files = emalloc(filecnt * sizeof(*files));
806 memset(files, 0, filecnt * sizeof(*files));
807 fileidx = 0;
808
809 if (options->from_stdin) {
810 n = 0;
811 filename = NULL;
812 while ((len = getline(&filename, &n, stdin)) > 0) {
813 if (filename[len-1] == '\n')
814 filename[len-1] = '\0';
815 check_add_file(filename, true);
816 }
817 free(filename);
818 }
819
820 for (i = 0; i < options->filecnt; i++) {
821 filename = options->filenames[i];
822
823 if (stat(filename, &fstats) < 0) {
824 error(0, errno, "%s", filename);
825 continue;
826 }
827 if (!S_ISDIR(fstats.st_mode)) {
828 check_add_file(filename, true);
829 } else {
830 if (r_opendir(&dir, filename, options->recursive) < 0) {
831 error(0, errno, "%s", filename);
832 continue;
833 }
834 start = fileidx;
835 while ((filename = r_readdir(&dir)) != NULL) {
836 check_add_file(filename, false);
837 free((void*) filename);
838 }
839 r_closedir(&dir);
840 if (fileidx - start > 1)
841 qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp);
842 }
843 }
844
845 if (fileidx == 0)
846 error(EXIT_FAILURE, 0, "No valid image file given, aborting");
847
848 filecnt = fileidx;
849 fileidx = options->startnum < filecnt ? options->startnum : 0;
850
851 win_init(&win);
852 img_init(&img, &win);
853
854 if ((homedir = getenv("XDG_CONFIG_HOME")) == NULL || homedir[0] == '\0') {
855 homedir = getenv("HOME");
856 dsuffix = "/.config";
857 }
858 if (homedir != NULL) {
859 extcmd_t *cmd[] = { &info.f, &keyhandler.f };
860 const char *name[] = { "image-info", "key-handler" };
861
862 for (i = 0; i < ARRLEN(cmd); i++) {
863 n = strlen(homedir) + strlen(dsuffix) + strlen(name[i]) + 12;
864 cmd[i]->cmd = (char*) emalloc(n);
865 snprintf(cmd[i]->cmd, n, "%s%s/sxiv/exec/%s", homedir, dsuffix, name[i]);
866 if (access(cmd[i]->cmd, X_OK) != 0)
867 cmd[i]->err = errno;
868 }
869 } else {
870 error(0, 0, "Exec directory not found");
871 }
872 info.fd = -1;
873
874 if (options->thumb_mode) {
875 mode = MODE_THUMB;
876 tns_init(&tns, files, &filecnt, &fileidx, &win);
877 while (!tns_load(&tns, fileidx, false, false))
878 remove_file(fileidx, false);
879 } else {
880 mode = MODE_IMAGE;
881 tns.thumbs = NULL;
882 load_image(fileidx);
883 }
884 win_open(&win);
885 win_set_cursor(&win, CURSOR_WATCH);
886
887 atexit(cleanup);
888
889 set_timeout(redraw, 25, false);
890
891 run();
892
893 return 0;
894}