Krolyxon
Commit 66c3c55

Branches

Refs →

nsxivmain.c

919 lines · 20.0 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
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, bool given)
116{
117 const char *bn;
118
119 if (*filename == '\0')
120 return;
121
122 if (access(filename, R_OK) < 0) {
123 if (given)
124 warn("could not open file: %s", filename);
125 return;
126 }
127
128 if (fileidx == filecnt) {
129 filecnt *= 2;
130 files = s_realloc(files, filecnt * sizeof(*files));
131 memset(&files[filecnt/2], 0, filecnt/2 * sizeof(*files));
132 }
133
134#if defined _BSD_SOURCE || defined _XOPEN_SOURCE && \
135 ((_XOPEN_SOURCE - 0) >= 500 || defined _XOPEN_SOURCE_EXTENDED)
136
137 if ((files[fileidx].path = realpath(filename, NULL)) == NULL) {
138 warn("could not get real path of file: %s\n", filename);
139 return;
140 }
141#else
142 if (*filename != '/') {
143 if ((files[fileidx].path = absolute_path(filename)) == NULL) {
144 warn("could not get absolute path of file: %s\n", filename);
145 return;
146 }
147 } else {
148 files[fileidx].path = NULL;
149 }
150#endif
151
152 files[fileidx].name = s_strdup(filename);
153 if (files[fileidx].path == NULL)
154 files[fileidx].path = files[fileidx].name;
155 if ((bn = strrchr(files[fileidx].name , '/')) != NULL && bn[1] != '\0')
156 files[fileidx].base = ++bn;
157 else
158 files[fileidx].base = files[fileidx].name;
159 if (given)
160 files[fileidx].flags |= FF_WARN;
161 fileidx++;
162}
163
164void remove_file(int n, bool manual)
165{
166 if (n < 0 || n >= filecnt)
167 return;
168
169 if (filecnt == 1) {
170 if (!manual)
171 fprintf(stderr, "sxiv: no more files to display, aborting\n");
172 cleanup();
173 exit(manual ? EXIT_SUCCESS : EXIT_FAILURE);
174 }
175 if (files[n].flags & FF_MARK)
176 markcnt--;
177
178 if (files[n].path != files[n].name)
179 free((void*) files[n].path);
180 free((void*) files[n].name);
181
182 if (n + 1 < filecnt) {
183 if (tns.thumbs != NULL) {
184 memmove(tns.thumbs + n, tns.thumbs + n + 1, (filecnt - n - 1) *
185 sizeof(*tns.thumbs));
186 memset(tns.thumbs + filecnt - 1, 0, sizeof(*tns.thumbs));
187 }
188 memmove(files + n, files + n + 1, (filecnt - n - 1) * sizeof(*files));
189 }
190 filecnt--;
191 if (fileidx >= filecnt)
192 fileidx = filecnt - 1;
193 if (n < alternate)
194 alternate--;
195}
196
197void set_timeout(timeout_f handler, int time, bool overwrite)
198{
199 int i;
200
201 for (i = 0; i < ARRLEN(timeouts); i++) {
202 if (timeouts[i].handler == handler) {
203 if (!timeouts[i].active || overwrite) {
204 gettimeofday(&timeouts[i].when, 0);
205 TV_ADD_MSEC(&timeouts[i].when, time);
206 timeouts[i].active = true;
207 }
208 return;
209 }
210 }
211}
212
213void reset_timeout(timeout_f handler)
214{
215 int i;
216
217 for (i = 0; i < ARRLEN(timeouts); i++) {
218 if (timeouts[i].handler == handler) {
219 timeouts[i].active = false;
220 return;
221 }
222 }
223}
224
225bool check_timeouts(struct timeval *t)
226{
227 int i = 0, tdiff, tmin = -1;
228 struct timeval now;
229
230 while (i < ARRLEN(timeouts)) {
231 if (timeouts[i].active) {
232 gettimeofday(&now, 0);
233 tdiff = TV_DIFF(&timeouts[i].when, &now);
234 if (tdiff <= 0) {
235 timeouts[i].active = false;
236 if (timeouts[i].handler != NULL)
237 timeouts[i].handler();
238 i = tmin = -1;
239 } else if (tmin < 0 || tdiff < tmin) {
240 tmin = tdiff;
241 }
242 }
243 i++;
244 }
245 if (tmin > 0 && t != NULL)
246 TV_SET_MSEC(t, tmin);
247 return tmin > 0;
248}
249
250void open_info(void)
251{
252 static pid_t pid;
253 int pfd[2];
254
255 if (info.cmd == NULL || info.open || win.bar.h == 0)
256 return;
257 if (info.fd != -1) {
258 close(info.fd);
259 kill(pid, SIGTERM);
260 info.fd = -1;
261 }
262 win.bar.l.buf[0] = '\0';
263
264 if (pipe(pfd) < 0)
265 return;
266 if ((pid = fork()) == 0) {
267 close(pfd[0]);
268 dup2(pfd[1], 1);
269 execl(info.cmd, info.cmd, files[fileidx].name, NULL);
270 warn("could not exec: %s", info.cmd);
271 exit(EXIT_FAILURE);
272 }
273 close(pfd[1]);
274 if (pid < 0) {
275 close(pfd[0]);
276 } else {
277 fcntl(pfd[0], F_SETFL, O_NONBLOCK);
278 info.fd = pfd[0];
279 info.i = info.lastsep = 0;
280 info.open = true;
281 }
282}
283
284void read_info(void)
285{
286 ssize_t i, n;
287 char buf[BAR_L_LEN];
288
289 while (true) {
290 n = read(info.fd, buf, sizeof(buf));
291 if (n < 0 && errno == EAGAIN)
292 return;
293 else if (n == 0)
294 goto end;
295 for (i = 0; i < n; i++) {
296 if (buf[i] == '\n') {
297 if (info.lastsep == 0) {
298 win.bar.l.buf[info.i++] = ' ';
299 info.lastsep = 1;
300 }
301 } else {
302 win.bar.l.buf[info.i++] = buf[i];
303 info.lastsep = 0;
304 }
305 if (info.i + 1 == win.bar.l.size)
306 goto end;
307 }
308 }
309end:
310 info.i -= info.lastsep;
311 win.bar.l.buf[info.i] = '\0';
312 win_draw(&win);
313 close(info.fd);
314 info.fd = -1;
315 while (waitpid(-1, NULL, WNOHANG) > 0);
316}
317
318void load_image(int new)
319{
320 static int current;
321
322 if (new < 0 || new >= filecnt)
323 return;
324
325 if (win.xwin != None)
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, tns.loadnext + 1);
389 ow_info = false;
390 } else if (tns.initnext < filecnt) {
391 bar_put(l, "Caching... %0*d", fw, tns.initnext + 1);
392 ow_info = false;
393 } else {
394 ow_info = true;
395 }
396 bar_put(r, "%s%0*d/%d", mark, fw, fileidx + 1, filecnt);
397 } else {
398 bar_put(r, "%s", mark);
399 if (img.ss.on)
400 bar_put(r, "%ds | ", img.ss.delay);
401 if (img.gamma != 0)
402 bar_put(r, "G%+d | ", img.gamma);
403 bar_put(r, "%3d%% | ", (int) (img.zoom * 100.0));
404 if (img.multi.cnt > 0) {
405 for (fn = 0, i = img.multi.cnt; i > 0; fn++, i /= 10);
406 bar_put(r, "%0*d/%d | ", fn, img.multi.sel + 1, img.multi.cnt);
407 }
408 bar_put(r, "%0*d/%d", fw, fileidx + 1, filecnt);
409 ow_info = info.cmd == NULL;
410 }
411 if (ow_info) {
412 fn = strlen(files[fileidx].name);
413 if (fn < l->size &&
414 win_textwidth(files[fileidx].name, fn, true) +
415 win_textwidth(r->buf, r->p - r->buf, true) < win.w)
416 {
417 strncpy(l->buf, files[fileidx].name, l->size);
418 } else {
419 strncpy(l->buf, files[fileidx].base, l->size);
420 }
421 }
422}
423
424void redraw(void)
425{
426 int t;
427
428 if (mode == MODE_IMAGE) {
429 img_render(&img);
430 if (img.ss.on) {
431 t = img.ss.delay * 1000;
432 if (img.multi.cnt > 0 && img.multi.animate)
433 t = MAX(t, img.multi.length);
434 set_timeout(slideshow, t, false);
435 }
436 } else {
437 tns_render(&tns);
438 }
439 update_info();
440 win_draw(&win);
441 reset_timeout(redraw);
442 reset_cursor();
443}
444
445void reset_cursor(void)
446{
447 int i;
448 cursor_t cursor = CURSOR_NONE;
449
450 if (mode == MODE_IMAGE) {
451 for (i = 0; i < ARRLEN(timeouts); i++) {
452 if (timeouts[i].handler == reset_cursor) {
453 if (timeouts[i].active)
454 cursor = CURSOR_ARROW;
455 break;
456 }
457 }
458 } else {
459 if (tns.loadnext < tns.end || tns.initnext < filecnt)
460 cursor = CURSOR_WATCH;
461 else
462 cursor = CURSOR_ARROW;
463 }
464 win_set_cursor(&win, cursor);
465}
466
467void animate(void)
468{
469 if (img_frame_animate(&img)) {
470 redraw();
471 set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
472 }
473}
474
475void slideshow(void)
476{
477 load_image(fileidx + 1 < filecnt ? fileidx + 1 : 0);
478 redraw();
479}
480
481void clear_resize(void)
482{
483 resized = false;
484}
485
486void run_key_handler(const char *key, unsigned int mask)
487{
488 pid_t pid;
489 FILE *pfs;
490 bool marked = mode == MODE_THUMB && markcnt > 0;
491 bool changed = false;
492 int f, i, pfd[2], retval, status;
493 int fcnt = marked ? markcnt : 1;
494 char kstr[32];
495 struct stat *oldst, st;
496
497 if (keyhandler.cmd == NULL) {
498 if (!keyhandler.warned) {
499 warn("key handler not installed");
500 keyhandler.warned = true;
501 }
502 return;
503 }
504 if (key == NULL)
505 return;
506
507 if (pipe(pfd) < 0) {
508 warn("could not create pipe for key handler");
509 return;
510 }
511 if ((pfs = fdopen(pfd[1], "w")) == NULL) {
512 close(pfd[0]), close(pfd[1]);
513 warn("could not open pipe for key handler");
514 return;
515 }
516 oldst = s_malloc(fcnt * sizeof(*oldst));
517
518 strncpy(win.bar.l.buf, "Running key handler...", win.bar.l.size);
519 win_draw(&win);
520 win_set_cursor(&win, CURSOR_WATCH);
521
522 snprintf(kstr, sizeof(kstr), "%s%s%s%s",
523 mask & ControlMask ? "C-" : "",
524 mask & Mod1Mask ? "M-" : "",
525 mask & ShiftMask ? "S-" : "", key);
526
527 if ((pid = fork()) == 0) {
528 close(pfd[1]);
529 dup2(pfd[0], 0);
530 execl(keyhandler.cmd, keyhandler.cmd, kstr, NULL);
531 warn("could not exec key handler");
532 exit(EXIT_FAILURE);
533 }
534 close(pfd[0]);
535 if (pid < 0) {
536 fclose(pfs);
537 warn("could not fork key handler");
538 goto end;
539 }
540
541 for (f = i = 0; f < fcnt; i++) {
542 if ((marked && (files[i].flags & FF_MARK)) || (!marked && i == fileidx)) {
543 stat(files[i].path, &oldst[f]);
544 fprintf(pfs, "%s\n", files[i].path);
545 f++;
546 }
547 }
548 fclose(pfs);
549 waitpid(pid, &status, 0);
550 retval = WEXITSTATUS(status);
551 if (WIFEXITED(status) == 0 || retval != 0)
552 warn("key handler exited with non-zero return value: %d", retval);
553
554 for (f = i = 0; f < fcnt; i++) {
555 if ((marked && (files[i].flags & FF_MARK)) || (!marked && i == fileidx)) {
556 if (stat(files[i].path, &st) != 0 ||
557 memcmp(&oldst[f].st_mtime, &st.st_mtime, sizeof(st.st_mtime)) != 0)
558 {
559 if (tns.thumbs != NULL) {
560 tns_unload(&tns, i);
561 tns.loadnext = MIN(tns.loadnext, i);
562 }
563 changed = true;
564 }
565 f++;
566 }
567 }
568end:
569 if (mode == MODE_IMAGE) {
570 if (changed) {
571 img_close(&img, true);
572 load_image(fileidx);
573 } else if (info.cmd != NULL) {
574 info.open = false;
575 open_info();
576 }
577 }
578 free(oldst);
579 reset_cursor();
580 redraw();
581}
582
583#define MODMASK(mask) ((mask) & (ShiftMask|ControlMask|Mod1Mask))
584
585void on_keypress(XKeyEvent *kev)
586{
587 int i;
588 unsigned int sh;
589 KeySym ksym, shksym;
590 char key;
591 bool dirty = false;
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 (mode == MODE_IMAGE) {
634 win_set_cursor(&win, CURSOR_ARROW);
635 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
636
637 for (i = 0; i < ARRLEN(buttons); i++) {
638 if (buttons[i].button == bev->button &&
639 MODMASK(buttons[i].mask) == MODMASK(bev->state) &&
640 buttons[i].cmd >= 0 && buttons[i].cmd < CMD_COUNT &&
641 (cmds[buttons[i].cmd].mode < 0 || cmds[buttons[i].cmd].mode == mode))
642 {
643 if (cmds[buttons[i].cmd].func(buttons[i].arg))
644 dirty = true;
645 }
646 }
647 if (dirty)
648 redraw();
649 } else {
650 /* thumbnail mode (hard-coded) */
651 switch (bev->button) {
652 case Button1:
653 if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
654 if (sel != fileidx) {
655 tns_highlight(&tns, fileidx, false);
656 tns_highlight(&tns, sel, true);
657 fileidx = sel;
658 firstclick = bev->time;
659 redraw();
660 } else if (bev->time - firstclick <= TO_DOUBLE_CLICK) {
661 mode = MODE_IMAGE;
662 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
663 load_image(fileidx);
664 redraw();
665 } else {
666 firstclick = bev->time;
667 }
668 }
669 break;
670 case Button3:
671 if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
672 files[sel].flags ^= FF_MARK;
673 markcnt += files[sel].flags & FF_MARK ? 1 : -1;
674 tns_mark(&tns, sel, !!(files[sel].flags & FF_MARK));
675 redraw();
676 }
677 break;
678 case Button4:
679 case Button5:
680 if (tns_scroll(&tns, bev->button == Button4 ? DIR_UP : DIR_DOWN,
681 (bev->state & ControlMask) != 0))
682 redraw();
683 break;
684 }
685 }
686 prefix = 0;
687}
688
689void run(void)
690{
691 int xfd;
692 fd_set fds;
693 struct timeval timeout;
694 bool discard, init_thumb, load_thumb, to_set;
695 XEvent ev, nextev;
696
697 while (true) {
698 to_set = check_timeouts(&timeout);
699 init_thumb = mode == MODE_THUMB && tns.initnext < filecnt;
700 load_thumb = mode == MODE_THUMB && tns.loadnext < tns.end;
701
702 if ((init_thumb || load_thumb || to_set || info.fd != -1) &&
703 XPending(win.env.dpy) == 0)
704 {
705 if (load_thumb) {
706 set_timeout(redraw, TO_REDRAW_THUMBS, false);
707 if (!tns_load(&tns, tns.loadnext, false, false)) {
708 remove_file(tns.loadnext, false);
709 tns.dirty = true;
710 }
711 if (tns.loadnext >= tns.end)
712 redraw();
713 } else if (init_thumb) {
714 set_timeout(redraw, TO_REDRAW_THUMBS, false);
715 if (!tns_load(&tns, tns.initnext, false, true))
716 remove_file(tns.initnext, false);
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 n = 0;
829 filename = NULL;
830 while ((len = getline(&filename, &n, stdin)) > 0) {
831 if (filename[len-1] == '\n')
832 filename[len-1] = '\0';
833 check_add_file(filename, true);
834 }
835 free(filename);
836 }
837
838 for (i = 0; i < options->filecnt; i++) {
839 filename = options->filenames[i];
840
841 if (stat(filename, &fstats) < 0) {
842 warn("could not stat file: %s", filename);
843 continue;
844 }
845 if (!S_ISDIR(fstats.st_mode)) {
846 check_add_file(filename, true);
847 } else {
848 if (!options->recursive) {
849 warn("ignoring directory: %s", filename);
850 continue;
851 }
852 if (r_opendir(&dir, filename) < 0) {
853 warn("could not open directory: %s", filename);
854 continue;
855 }
856 start = fileidx;
857 while ((filename = r_readdir(&dir)) != NULL) {
858 check_add_file(filename, false);
859 free((void*) filename);
860 }
861 r_closedir(&dir);
862 if (fileidx - start > 1)
863 qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp);
864 }
865 }
866
867 if (fileidx == 0) {
868 fprintf(stderr, "sxiv: no valid image file given, aborting\n");
869 exit(EXIT_FAILURE);
870 }
871
872 filecnt = fileidx;
873 fileidx = options->startnum < filecnt ? options->startnum : 0;
874
875 win_init(&win);
876 img_init(&img, &win);
877
878 if ((homedir = getenv("XDG_CONFIG_HOME")) == NULL || homedir[0] == '\0') {
879 homedir = getenv("HOME");
880 dsuffix = "/.config";
881 }
882 if (homedir != NULL) {
883 char **cmd[] = { &info.cmd, &keyhandler.cmd };
884 const char *name[] = { "image-info", "key-handler" };
885
886 for (i = 0; i < ARRLEN(cmd); i++) {
887 n = strlen(homedir) + strlen(dsuffix) + strlen(name[i]) + 12;
888 *cmd[i] = (char*) s_malloc(n);
889 snprintf(*cmd[i], n, "%s%s/sxiv/exec/%s", homedir, dsuffix, name[i]);
890 if (access(*cmd[i], X_OK) != 0) {
891 free(*cmd[i]);
892 *cmd[i] = NULL;
893 }
894 }
895 } else {
896 warn("could not locate exec directory");
897 }
898 info.fd = -1;
899
900 if (options->thumb_mode) {
901 mode = MODE_THUMB;
902 tns_init(&tns, files, &filecnt, &fileidx, &win);
903 while (!tns_load(&tns, fileidx, false, false))
904 remove_file(fileidx, false);
905 } else {
906 mode = MODE_IMAGE;
907 tns.thumbs = NULL;
908 load_image(fileidx);
909 }
910 win_open(&win);
911 win_set_cursor(&win, CURSOR_WATCH);
912
913 set_timeout(redraw, 25, false);
914
915 run();
916 cleanup();
917
918 return 0;
919}