Krolyxon
Commit 9b9294b

Branches

Refs →

nsxivmain.c

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