Krolyxon
Commit edb117e

Branches

Refs →

nsxivmain.c

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