Krolyxon
Commit e0e9697

Branches

Refs →

nsxivmain.c

919 lines · 20.1 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 == '\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 if (win.xwin != None)
327 win_set_cursor(&win, CURSOR_WATCH);
328 reset_timeout(slideshow);
329
330 if (new != current)
331 alternate = current;
332
333 img_close(&img, false);
334 while (!img_load(&img, &files[new])) {
335 remove_file(new, false);
336 if (new >= filecnt)
337 new = filecnt - 1;
338 else if (new > 0 && new < fileidx)
339 new--;
340 }
341 files[new].flags &= ~FF_WARN;
342 fileidx = current = new;
343
344 info.open = false;
345 open_info();
346
347 if (img.multi.cnt > 0 && img.multi.animate)
348 set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
349 else
350 reset_timeout(animate);
351}
352
353void bar_put(win_bar_t *bar, const char *fmt, ...)
354{
355 size_t len = bar->size - (bar->p - bar->buf), n;
356 va_list ap;
357
358 va_start(ap, fmt);
359 n = vsnprintf(bar->p, len, fmt, ap);
360 bar->p += MIN(len, n);
361 va_end(ap);
362}
363
364void update_info(void)
365{
366 unsigned int i, fn, fw;
367 char title[TITLE_LEN];
368 const char * mark;
369 bool ow_info;
370 win_bar_t *l = &win.bar.l, *r = &win.bar.r;
371
372 /* update window title */
373 if (mode == MODE_THUMB) {
374 win_set_title(&win, "sxiv");
375 } else {
376 snprintf(title, sizeof(title), "sxiv - %s", files[fileidx].name);
377 win_set_title(&win, title);
378 }
379
380 /* update bar contents */
381 if (win.bar.h == 0)
382 return;
383 for (fw = 0, i = filecnt; i > 0; fw++, i /= 10);
384 mark = files[fileidx].flags & FF_MARK ? "* " : "";
385 l->p = l->buf;
386 r->p = r->buf;
387 if (mode == MODE_THUMB) {
388 if (tns.loadnext < tns.end) {
389 bar_put(l, "Loading... %0*d", fw, tns.loadnext + 1);
390 ow_info = false;
391 } else if (tns.initnext < filecnt) {
392 bar_put(l, "Caching... %0*d", fw, tns.initnext + 1);
393 ow_info = false;
394 } else {
395 ow_info = true;
396 }
397 bar_put(r, "%s%0*d/%d", mark, fw, fileidx + 1, filecnt);
398 } else {
399 bar_put(r, "%s", mark);
400 if (img.ss.on)
401 bar_put(r, "%ds | ", img.ss.delay);
402 if (img.gamma != 0)
403 bar_put(r, "G%+d | ", img.gamma);
404 bar_put(r, "%3d%% | ", (int) (img.zoom * 100.0));
405 if (img.multi.cnt > 0) {
406 for (fn = 0, i = img.multi.cnt; i > 0; fn++, i /= 10);
407 bar_put(r, "%0*d/%d | ", fn, img.multi.sel + 1, img.multi.cnt);
408 }
409 bar_put(r, "%0*d/%d", fw, fileidx + 1, filecnt);
410 ow_info = info.cmd == NULL;
411 }
412 if (ow_info) {
413 fn = strlen(files[fileidx].name);
414 if (fn < l->size &&
415 win_textwidth(files[fileidx].name, fn, true) +
416 win_textwidth(r->buf, r->p - r->buf, true) < win.w)
417 {
418 strncpy(l->buf, files[fileidx].name, l->size);
419 } else {
420 strncpy(l->buf, files[fileidx].base, l->size);
421 }
422 }
423}
424
425void redraw(void)
426{
427 int t;
428
429 if (mode == MODE_IMAGE) {
430 img_render(&img);
431 if (img.ss.on) {
432 t = img.ss.delay * 1000;
433 if (img.multi.cnt > 0 && img.multi.animate)
434 t = MAX(t, img.multi.length);
435 set_timeout(slideshow, t, false);
436 }
437 } else {
438 tns_render(&tns);
439 }
440 update_info();
441 win_draw(&win);
442 reset_timeout(redraw);
443 reset_cursor();
444}
445
446void reset_cursor(void)
447{
448 int i;
449 cursor_t cursor = CURSOR_NONE;
450
451 if (mode == MODE_IMAGE) {
452 for (i = 0; i < ARRLEN(timeouts); i++) {
453 if (timeouts[i].handler == reset_cursor) {
454 if (timeouts[i].active)
455 cursor = CURSOR_ARROW;
456 break;
457 }
458 }
459 } else {
460 if (tns.loadnext < tns.end || tns.initnext < filecnt)
461 cursor = CURSOR_WATCH;
462 else
463 cursor = CURSOR_ARROW;
464 }
465 win_set_cursor(&win, cursor);
466}
467
468void animate(void)
469{
470 if (img_frame_animate(&img)) {
471 redraw();
472 set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
473 }
474}
475
476void slideshow(void)
477{
478 load_image(fileidx + 1 < filecnt ? fileidx + 1 : 0);
479 redraw();
480}
481
482void clear_resize(void)
483{
484 resized = false;
485}
486
487void run_key_handler(const char *key, unsigned int mask)
488{
489 pid_t pid;
490 FILE *pfs;
491 bool marked = mode == MODE_THUMB && markcnt > 0;
492 bool changed = false;
493 int f, i, pfd[2], retval, status;
494 int fcnt = marked ? markcnt : 1;
495 char kstr[32];
496 struct stat *oldst, st;
497
498 if (keyhandler.cmd == NULL) {
499 if (!keyhandler.warned) {
500 warn("key handler not installed");
501 keyhandler.warned = true;
502 }
503 return;
504 }
505 if (key == NULL)
506 return;
507
508 if (pipe(pfd) < 0) {
509 warn("could not create pipe for key handler");
510 return;
511 }
512 if ((pfs = fdopen(pfd[1], "w")) == NULL) {
513 close(pfd[0]), close(pfd[1]);
514 warn("could not open pipe for key handler");
515 return;
516 }
517 oldst = s_malloc(fcnt * sizeof(*oldst));
518
519 strncpy(win.bar.l.buf, "Running key handler...", win.bar.l.size);
520 win_draw(&win);
521 win_set_cursor(&win, CURSOR_WATCH);
522
523 snprintf(kstr, sizeof(kstr), "%s%s%s%s",
524 mask & ControlMask ? "C-" : "",
525 mask & Mod1Mask ? "M-" : "",
526 mask & ShiftMask ? "S-" : "", key);
527
528 if ((pid = fork()) == 0) {
529 close(pfd[1]);
530 dup2(pfd[0], 0);
531 execl(keyhandler.cmd, keyhandler.cmd, kstr, NULL);
532 warn("could not exec key handler");
533 exit(EXIT_FAILURE);
534 }
535 close(pfd[0]);
536 if (pid < 0) {
537 fclose(pfs);
538 warn("could not fork key handler");
539 goto end;
540 }
541
542 for (f = i = 0; f < fcnt; i++) {
543 if ((marked && (files[i].flags & FF_MARK)) || (!marked && i == fileidx)) {
544 stat(files[i].path, &oldst[f]);
545 fprintf(pfs, "%s\n", files[i].path);
546 f++;
547 }
548 }
549 fclose(pfs);
550 waitpid(pid, &status, 0);
551 retval = WEXITSTATUS(status);
552 if (WIFEXITED(status) == 0 || retval != 0)
553 warn("key handler exited with non-zero return value: %d", retval);
554
555 for (f = i = 0; f < fcnt; i++) {
556 if ((marked && (files[i].flags & FF_MARK)) || (!marked && i == fileidx)) {
557 if (stat(files[i].path, &st) != 0 ||
558 memcmp(&oldst[f].st_mtime, &st.st_mtime, sizeof(st.st_mtime)) != 0)
559 {
560 if (tns.thumbs != NULL) {
561 tns_unload(&tns, i);
562 tns.loadnext = MIN(tns.loadnext, i);
563 }
564 changed = true;
565 }
566 f++;
567 }
568 }
569end:
570 if (mode == MODE_IMAGE) {
571 if (changed) {
572 img_close(&img, true);
573 load_image(fileidx);
574 } else if (info.cmd != NULL) {
575 info.open = false;
576 open_info();
577 }
578 }
579 free(oldst);
580 reset_cursor();
581 redraw();
582}
583
584#define MODMASK(mask) ((mask) & (ShiftMask|ControlMask|Mod1Mask))
585
586void on_keypress(XKeyEvent *kev)
587{
588 int i;
589 unsigned int sh;
590 KeySym ksym, shksym;
591 char key;
592 bool dirty = false;
593
594 if (kev->state & ShiftMask) {
595 kev->state &= ~ShiftMask;
596 XLookupString(kev, &key, 1, &shksym, NULL);
597 kev->state |= ShiftMask;
598 }
599 XLookupString(kev, &key, 1, &ksym, NULL);
600 sh = (kev->state & ShiftMask) && ksym != shksym ? ShiftMask : 0;
601
602 if (IsModifierKey(ksym))
603 return;
604 if (ksym == XK_Escape && MODMASK(kev->state) == 0) {
605 extprefix = False;
606 } else if (extprefix) {
607 run_key_handler(XKeysymToString(ksym), kev->state & ~sh);
608 extprefix = False;
609 } else if (key >= '0' && key <= '9') {
610 /* number prefix for commands */
611 prefix = prefix * 10 + (int) (key - '0');
612 return;
613 } else for (i = 0; i < ARRLEN(keys); i++) {
614 if (keys[i].ksym == ksym &&
615 MODMASK(keys[i].mask | sh) == MODMASK(kev->state) &&
616 keys[i].cmd >= 0 && keys[i].cmd < CMD_COUNT &&
617 (cmds[keys[i].cmd].mode < 0 || cmds[keys[i].cmd].mode == mode))
618 {
619 if (cmds[keys[i].cmd].func(keys[i].arg))
620 dirty = true;
621 }
622 }
623 if (dirty)
624 redraw();
625 prefix = 0;
626}
627
628void on_buttonpress(XButtonEvent *bev)
629{
630 int i, sel;
631 bool dirty = false;
632 static Time firstclick;
633
634 if (mode == MODE_IMAGE) {
635 win_set_cursor(&win, CURSOR_ARROW);
636 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
637
638 for (i = 0; i < ARRLEN(buttons); i++) {
639 if (buttons[i].button == bev->button &&
640 MODMASK(buttons[i].mask) == MODMASK(bev->state) &&
641 buttons[i].cmd >= 0 && buttons[i].cmd < CMD_COUNT &&
642 (cmds[buttons[i].cmd].mode < 0 || cmds[buttons[i].cmd].mode == mode))
643 {
644 if (cmds[buttons[i].cmd].func(buttons[i].arg))
645 dirty = true;
646 }
647 }
648 if (dirty)
649 redraw();
650 } else {
651 /* thumbnail mode (hard-coded) */
652 switch (bev->button) {
653 case Button1:
654 if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
655 if (sel != fileidx) {
656 tns_highlight(&tns, fileidx, false);
657 tns_highlight(&tns, sel, true);
658 fileidx = sel;
659 firstclick = bev->time;
660 redraw();
661 } else if (bev->time - firstclick <= TO_DOUBLE_CLICK) {
662 mode = MODE_IMAGE;
663 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
664 load_image(fileidx);
665 redraw();
666 } else {
667 firstclick = bev->time;
668 }
669 }
670 break;
671 case Button3:
672 if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
673 files[sel].flags ^= FF_MARK;
674 markcnt += files[sel].flags & FF_MARK ? 1 : -1;
675 tns_mark(&tns, sel, !!(files[sel].flags & FF_MARK));
676 redraw();
677 }
678 break;
679 case Button4:
680 case Button5:
681 if (tns_scroll(&tns, bev->button == Button4 ? DIR_UP : DIR_DOWN,
682 (bev->state & ControlMask) != 0))
683 redraw();
684 break;
685 }
686 }
687 prefix = 0;
688}
689
690void run(void)
691{
692 int xfd;
693 fd_set fds;
694 struct timeval timeout;
695 bool discard, init_thumb, load_thumb, to_set;
696 XEvent ev, nextev;
697
698 while (true) {
699 to_set = check_timeouts(&timeout);
700 init_thumb = mode == MODE_THUMB && tns.initnext < filecnt;
701 load_thumb = mode == MODE_THUMB && tns.loadnext < tns.end;
702
703 if ((init_thumb || load_thumb || to_set || info.fd != -1) &&
704 XPending(win.env.dpy) == 0)
705 {
706 if (load_thumb) {
707 set_timeout(redraw, TO_REDRAW_THUMBS, false);
708 if (!tns_load(&tns, tns.loadnext, false, false)) {
709 remove_file(tns.loadnext, false);
710 tns.dirty = true;
711 }
712 if (tns.loadnext >= tns.end)
713 redraw();
714 } else if (init_thumb) {
715 set_timeout(redraw, TO_REDRAW_THUMBS, false);
716 if (!tns_load(&tns, tns.initnext, false, true))
717 remove_file(tns.initnext, false);
718 } else {
719 xfd = ConnectionNumber(win.env.dpy);
720 FD_ZERO(&fds);
721 FD_SET(xfd, &fds);
722 if (info.fd != -1) {
723 FD_SET(info.fd, &fds);
724 xfd = MAX(xfd, info.fd);
725 }
726 select(xfd + 1, &fds, 0, 0, to_set ? &timeout : NULL);
727 if (info.fd != -1 && FD_ISSET(info.fd, &fds))
728 read_info();
729 }
730 continue;
731 }
732
733 do {
734 XNextEvent(win.env.dpy, &ev);
735 discard = false;
736 if (XEventsQueued(win.env.dpy, QueuedAlready) > 0) {
737 XPeekEvent(win.env.dpy, &nextev);
738 switch (ev.type) {
739 case ConfigureNotify:
740 discard = ev.type == nextev.type;
741 break;
742 case KeyPress:
743 discard = (nextev.type == KeyPress || nextev.type == KeyRelease)
744 && ev.xkey.keycode == nextev.xkey.keycode;
745 break;
746 }
747 }
748 } while (discard);
749
750 switch (ev.type) {
751 /* handle events */
752 case ButtonPress:
753 on_buttonpress(&ev.xbutton);
754 break;
755 case ClientMessage:
756 if ((Atom) ev.xclient.data.l[0] == atoms[ATOM_WM_DELETE_WINDOW])
757 return;
758 break;
759 case ConfigureNotify:
760 if (win_configure(&win, &ev.xconfigure)) {
761 if (mode == MODE_IMAGE) {
762 img.dirty = true;
763 img.checkpan = true;
764 } else {
765 tns.dirty = true;
766 }
767 if (!resized || win.fullscreen) {
768 redraw();
769 set_timeout(clear_resize, TO_REDRAW_RESIZE, false);
770 resized = true;
771 } else {
772 set_timeout(redraw, TO_REDRAW_RESIZE, false);
773 }
774 }
775 break;
776 case KeyPress:
777 on_keypress(&ev.xkey);
778 break;
779 case MotionNotify:
780 if (mode == MODE_IMAGE) {
781 win_set_cursor(&win, CURSOR_ARROW);
782 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
783 }
784 break;
785 }
786 }
787}
788
789int fncmp(const void *a, const void *b)
790{
791 return strcoll(((fileinfo_t*) a)->name, ((fileinfo_t*) b)->name);
792}
793
794int main(int argc, char **argv)
795{
796 int i, start;
797 size_t n;
798 ssize_t len;
799 char *filename;
800 const char *homedir, *dsuffix = "";
801 struct stat fstats;
802 r_dir_t dir;
803
804 signal(SIGPIPE, SIG_IGN);
805
806 parse_options(argc, argv);
807
808 if (options->clean_cache) {
809 tns_init(&tns, NULL, NULL, NULL, NULL);
810 tns_clean_cache(&tns);
811 exit(EXIT_SUCCESS);
812 }
813
814 if (options->filecnt == 0 && !options->from_stdin) {
815 print_usage();
816 exit(EXIT_FAILURE);
817 }
818
819 if (options->recursive || options->from_stdin)
820 filecnt = FILENAME_CNT;
821 else
822 filecnt = options->filecnt;
823
824 files = s_malloc(filecnt * sizeof(*files));
825 memset(files, 0, filecnt * sizeof(*files));
826 fileidx = 0;
827
828 if (options->from_stdin) {
829 filename = NULL;
830 while ((len = get_line(&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 len = strlen(homedir) + strlen(dsuffix) + strlen(name[i]) + 12;
888 *cmd[i] = (char*) s_malloc(len);
889 snprintf(*cmd[i], len, "%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}