Krolyxon
Commit 8a9e3ea

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