Krolyxon
Commit e98266b

Branches

Refs →

nsxivmain.c

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