Krolyxon
Commit d8114e8

Branches

Refs →

nsxivmain.c

924 lines · 20.2 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, 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], oldbar[BAR_L_LEN];
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 memcpy(oldbar, win.bar.l.buf, sizeof(oldbar));
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 memcpy(win.bar.l.buf, oldbar, win.bar.l.size);
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 == NULL)
594 return;
595
596 if (kev->state & ShiftMask) {
597 kev->state &= ~ShiftMask;
598 XLookupString(kev, &key, 1, &shksym, NULL);
599 kev->state |= ShiftMask;
600 }
601 XLookupString(kev, &key, 1, &ksym, NULL);
602 sh = (kev->state & ShiftMask) && ksym != shksym ? ShiftMask : 0;
603
604 if (IsModifierKey(ksym))
605 return;
606 if (ksym == XK_Escape && MODMASK(kev->state) == 0) {
607 extprefix = False;
608 } else if (extprefix) {
609 run_key_handler(XKeysymToString(ksym), kev->state & ~sh);
610 extprefix = False;
611 } else if (key >= '0' && key <= '9') {
612 /* number prefix for commands */
613 prefix = prefix * 10 + (int) (key - '0');
614 return;
615 } else for (i = 0; i < ARRLEN(keys); i++) {
616 if (keys[i].ksym == ksym &&
617 MODMASK(keys[i].mask | sh) == MODMASK(kev->state) &&
618 keys[i].cmd >= 0 && keys[i].cmd < CMD_COUNT &&
619 (cmds[keys[i].cmd].mode < 0 || cmds[keys[i].cmd].mode == mode))
620 {
621 if (cmds[keys[i].cmd].func(keys[i].arg))
622 dirty = true;
623 }
624 }
625 if (dirty)
626 redraw();
627 prefix = 0;
628}
629
630void on_buttonpress(XButtonEvent *bev)
631{
632 int i, sel;
633 bool dirty = false;
634 static Time firstclick;
635
636 if (bev == NULL)
637 return;
638
639 if (mode == MODE_IMAGE) {
640 win_set_cursor(&win, CURSOR_ARROW);
641 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
642
643 for (i = 0; i < ARRLEN(buttons); i++) {
644 if (buttons[i].button == bev->button &&
645 MODMASK(buttons[i].mask) == MODMASK(bev->state) &&
646 buttons[i].cmd >= 0 && buttons[i].cmd < CMD_COUNT &&
647 (cmds[buttons[i].cmd].mode < 0 || cmds[buttons[i].cmd].mode == mode))
648 {
649 if (cmds[buttons[i].cmd].func(buttons[i].arg))
650 dirty = true;
651 }
652 }
653 if (dirty)
654 redraw();
655 } else {
656 /* thumbnail mode (hard-coded) */
657 switch (bev->button) {
658 case Button1:
659 if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
660 if (sel != fileidx) {
661 tns_highlight(&tns, fileidx, false);
662 tns_highlight(&tns, sel, true);
663 fileidx = sel;
664 firstclick = bev->time;
665 redraw();
666 } else if (bev->time - firstclick <= TO_DOUBLE_CLICK) {
667 mode = MODE_IMAGE;
668 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
669 load_image(fileidx);
670 redraw();
671 } else {
672 firstclick = bev->time;
673 }
674 }
675 break;
676 case Button3:
677 if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
678 files[sel].flags ^= FF_MARK;
679 markcnt += files[sel].flags & FF_MARK ? 1 : -1;
680 tns_mark(&tns, sel, !!(files[sel].flags & FF_MARK));
681 redraw();
682 }
683 break;
684 case Button4:
685 case Button5:
686 if (tns_scroll(&tns, bev->button == Button4 ? DIR_UP : DIR_DOWN,
687 (bev->state & ControlMask) != 0))
688 redraw();
689 break;
690 }
691 }
692 prefix = 0;
693}
694
695void run(void)
696{
697 int xfd;
698 fd_set fds;
699 struct timeval timeout;
700 bool discard, init_thumb, load_thumb, to_set;
701 XEvent ev, nextev;
702
703 while (true) {
704 to_set = check_timeouts(&timeout);
705 init_thumb = mode == MODE_THUMB && tns.initnext < filecnt;
706 load_thumb = mode == MODE_THUMB && tns.loadnext < tns.end;
707
708 if ((init_thumb || load_thumb || to_set || info.fd != -1) &&
709 XPending(win.env.dpy) == 0)
710 {
711 if (load_thumb) {
712 set_timeout(redraw, TO_REDRAW_THUMBS, false);
713 if (!tns_load(&tns, tns.loadnext, false, false)) {
714 remove_file(tns.loadnext, false);
715 tns.dirty = true;
716 }
717 if (tns.loadnext >= tns.end)
718 redraw();
719 } else if (init_thumb) {
720 set_timeout(redraw, TO_REDRAW_THUMBS, false);
721 if (!tns_load(&tns, tns.initnext, false, true))
722 remove_file(tns.initnext, false);
723 } else {
724 xfd = ConnectionNumber(win.env.dpy);
725 FD_ZERO(&fds);
726 FD_SET(xfd, &fds);
727 if (info.fd != -1) {
728 FD_SET(info.fd, &fds);
729 xfd = MAX(xfd, info.fd);
730 }
731 select(xfd + 1, &fds, 0, 0, to_set ? &timeout : NULL);
732 if (info.fd != -1 && FD_ISSET(info.fd, &fds))
733 read_info();
734 }
735 continue;
736 }
737
738 do {
739 XNextEvent(win.env.dpy, &ev);
740 discard = false;
741 if (XEventsQueued(win.env.dpy, QueuedAlready) > 0) {
742 XPeekEvent(win.env.dpy, &nextev);
743 switch (ev.type) {
744 case ConfigureNotify:
745 discard = ev.type == nextev.type;
746 break;
747 case KeyPress:
748 discard = (nextev.type == KeyPress || nextev.type == KeyRelease)
749 && ev.xkey.keycode == nextev.xkey.keycode;
750 break;
751 }
752 }
753 } while (discard);
754
755 switch (ev.type) {
756 /* handle events */
757 case ButtonPress:
758 on_buttonpress(&ev.xbutton);
759 break;
760 case ClientMessage:
761 if ((Atom) ev.xclient.data.l[0] == atoms[ATOM_WM_DELETE_WINDOW])
762 return;
763 break;
764 case ConfigureNotify:
765 if (win_configure(&win, &ev.xconfigure)) {
766 if (mode == MODE_IMAGE) {
767 img.dirty = true;
768 img.checkpan = true;
769 } else {
770 tns.dirty = true;
771 }
772 if (!resized || win.fullscreen) {
773 redraw();
774 set_timeout(clear_resize, TO_REDRAW_RESIZE, false);
775 resized = true;
776 } else {
777 set_timeout(redraw, TO_REDRAW_RESIZE, false);
778 }
779 }
780 break;
781 case KeyPress:
782 on_keypress(&ev.xkey);
783 break;
784 case MotionNotify:
785 if (mode == MODE_IMAGE) {
786 win_set_cursor(&win, CURSOR_ARROW);
787 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
788 }
789 break;
790 }
791 }
792}
793
794int fncmp(const void *a, const void *b)
795{
796 return strcoll(((fileinfo_t*) a)->name, ((fileinfo_t*) b)->name);
797}
798
799int main(int argc, char **argv)
800{
801 int i, start;
802 size_t n;
803 ssize_t len;
804 char *filename;
805 const char *homedir, *dsuffix = "";
806 struct stat fstats;
807 r_dir_t dir;
808
809 signal(SIGPIPE, SIG_IGN);
810
811 parse_options(argc, argv);
812
813 if (options->clean_cache) {
814 tns_init(&tns, NULL, NULL, NULL, NULL);
815 tns_clean_cache(&tns);
816 exit(EXIT_SUCCESS);
817 }
818
819 if (options->filecnt == 0 && !options->from_stdin) {
820 print_usage();
821 exit(EXIT_FAILURE);
822 }
823
824 if (options->recursive || options->from_stdin)
825 filecnt = FILENAME_CNT;
826 else
827 filecnt = options->filecnt;
828
829 files = s_malloc(filecnt * sizeof(*files));
830 memset(files, 0, filecnt * sizeof(*files));
831 fileidx = 0;
832
833 if (options->from_stdin) {
834 filename = NULL;
835 while ((len = get_line(&filename, &n, stdin)) > 0) {
836 if (filename[len-1] == '\n')
837 filename[len-1] = '\0';
838 check_add_file(filename, true);
839 }
840 free(filename);
841 }
842
843 for (i = 0; i < options->filecnt; i++) {
844 filename = options->filenames[i];
845
846 if (stat(filename, &fstats) < 0) {
847 warn("could not stat file: %s", filename);
848 continue;
849 }
850 if (!S_ISDIR(fstats.st_mode)) {
851 check_add_file(filename, true);
852 } else {
853 if (!options->recursive) {
854 warn("ignoring directory: %s", filename);
855 continue;
856 }
857 if (r_opendir(&dir, filename) < 0) {
858 warn("could not open directory: %s", filename);
859 continue;
860 }
861 start = fileidx;
862 while ((filename = r_readdir(&dir)) != NULL) {
863 check_add_file(filename, false);
864 free((void*) filename);
865 }
866 r_closedir(&dir);
867 if (fileidx - start > 1)
868 qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp);
869 }
870 }
871
872 if (fileidx == 0) {
873 fprintf(stderr, "sxiv: no valid image file given, aborting\n");
874 exit(EXIT_FAILURE);
875 }
876
877 filecnt = fileidx;
878 fileidx = options->startnum < filecnt ? options->startnum : 0;
879
880 win_init(&win);
881 img_init(&img, &win);
882
883 if ((homedir = getenv("XDG_CONFIG_HOME")) == NULL || homedir[0] == '\0') {
884 homedir = getenv("HOME");
885 dsuffix = "/.config";
886 }
887 if (homedir != NULL) {
888 char **cmd[] = { &info.cmd, &keyhandler.cmd };
889 const char *name[] = { "image-info", "key-handler" };
890
891 for (i = 0; i < ARRLEN(cmd); i++) {
892 len = strlen(homedir) + strlen(dsuffix) + strlen(name[i]) + 12;
893 *cmd[i] = (char*) s_malloc(len);
894 snprintf(*cmd[i], len, "%s%s/sxiv/exec/%s", homedir, dsuffix, name[i]);
895 if (access(*cmd[i], X_OK) != 0) {
896 free(*cmd[i]);
897 *cmd[i] = NULL;
898 }
899 }
900 } else {
901 warn("could not locate exec directory");
902 }
903 info.fd = -1;
904
905 if (options->thumb_mode) {
906 mode = MODE_THUMB;
907 tns_init(&tns, files, &filecnt, &fileidx, &win);
908 while (!tns_load(&tns, fileidx, false, false))
909 remove_file(fileidx, false);
910 } else {
911 mode = MODE_IMAGE;
912 tns.thumbs = NULL;
913 load_image(fileidx);
914 }
915 win_open(&win);
916 win_set_cursor(&win, CURSOR_WATCH);
917
918 set_timeout(redraw, 25, false);
919
920 run();
921 cleanup();
922
923 return 0;
924}