Krolyxon
Commit cc476be

Branches

Refs →

nsxivmain.c

936 lines · 20.3 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;
60int toggledidx;
61
62int prefix;
63bool extprefix;
64
65bool resized = false;
66
67typedef struct {
68 int err;
69 char *cmd;
70} extcmd_t;
71
72struct {
73 extcmd_t f;
74 int fd;
75 unsigned int i, lastsep;
76 pid_t pid;
77} info;
78
79struct {
80 extcmd_t f;
81 bool warned;
82} keyhandler;
83
84timeout_t timeouts[] = {
85 { { 0, 0 }, false, redraw },
86 { { 0, 0 }, false, reset_cursor },
87 { { 0, 0 }, false, animate },
88 { { 0, 0 }, false, slideshow },
89 { { 0, 0 }, false, clear_resize },
90};
91
92cursor_t imgcursor[3] = {
93 CURSOR_ARROW, CURSOR_ARROW, CURSOR_ARROW
94};
95
96void cleanup(void)
97{
98 img_close(&img, false);
99 arl_cleanup(&arl);
100 tns_free(&tns);
101 win_close(&win);
102}
103
104void check_add_file(char *filename, bool given)
105{
106 char *path;
107
108 if (*filename == '\0')
109 return;
110
111 if (access(filename, R_OK) < 0 ||
112 (path = realpath(filename, NULL)) == NULL)
113 {
114 if (given)
115 error(0, errno, "%s", filename);
116 return;
117 }
118
119 if (fileidx == filecnt) {
120 filecnt *= 2;
121 files = erealloc(files, filecnt * sizeof(*files));
122 memset(&files[filecnt/2], 0, filecnt/2 * sizeof(*files));
123 }
124
125 files[fileidx].name = estrdup(filename);
126 files[fileidx].path = path;
127 if (given)
128 files[fileidx].flags |= FF_WARN;
129 fileidx++;
130}
131
132void remove_file(int n, bool manual)
133{
134 if (n < 0 || n >= filecnt)
135 return;
136
137 if (filecnt == 1) {
138 if (!manual)
139 fprintf(stderr, "sxiv: no more files to display, aborting\n");
140 exit(manual ? EXIT_SUCCESS : EXIT_FAILURE);
141 }
142 if (files[n].flags & FF_MARK)
143 markcnt--;
144
145 if (files[n].path != files[n].name)
146 free((void*) files[n].path);
147 free((void*) files[n].name);
148
149 if (n + 1 < filecnt) {
150 if (tns.thumbs != NULL) {
151 memmove(tns.thumbs + n, tns.thumbs + n + 1, (filecnt - n - 1) *
152 sizeof(*tns.thumbs));
153 memset(tns.thumbs + filecnt - 1, 0, sizeof(*tns.thumbs));
154 }
155 memmove(files + n, files + n + 1, (filecnt - n - 1) * sizeof(*files));
156 }
157 filecnt--;
158 if (n < fileidx)
159 fileidx--;
160 if (n < alternate)
161 alternate--;
162}
163
164void set_timeout(timeout_f handler, int time, bool overwrite)
165{
166 int i;
167
168 for (i = 0; i < ARRLEN(timeouts); i++) {
169 if (timeouts[i].handler == handler) {
170 if (!timeouts[i].active || overwrite) {
171 gettimeofday(&timeouts[i].when, 0);
172 TV_ADD_MSEC(&timeouts[i].when, time);
173 timeouts[i].active = true;
174 }
175 return;
176 }
177 }
178}
179
180void reset_timeout(timeout_f handler)
181{
182 int i;
183
184 for (i = 0; i < ARRLEN(timeouts); i++) {
185 if (timeouts[i].handler == handler) {
186 timeouts[i].active = false;
187 return;
188 }
189 }
190}
191
192bool check_timeouts(struct timeval *t)
193{
194 int i = 0, tdiff, tmin = -1;
195 struct timeval now;
196
197 while (i < ARRLEN(timeouts)) {
198 if (timeouts[i].active) {
199 gettimeofday(&now, 0);
200 tdiff = TV_DIFF(&timeouts[i].when, &now);
201 if (tdiff <= 0) {
202 timeouts[i].active = false;
203 if (timeouts[i].handler != NULL)
204 timeouts[i].handler();
205 i = tmin = -1;
206 } else if (tmin < 0 || tdiff < tmin) {
207 tmin = tdiff;
208 }
209 }
210 i++;
211 }
212 if (tmin > 0 && t != NULL)
213 TV_SET_MSEC(t, tmin);
214 return tmin > 0;
215}
216
217void close_info(void)
218{
219 if (info.fd != -1) {
220 kill(info.pid, SIGTERM);
221 close(info.fd);
222 info.fd = -1;
223 }
224}
225
226void open_info(void)
227{
228 int pfd[2];
229 char w[12], h[12];
230
231 if (info.f.err != 0 || info.fd >= 0 || win.bar.h == 0)
232 return;
233 win.bar.l.buf[0] = '\0';
234 if (pipe(pfd) < 0)
235 return;
236 if ((info.pid = fork()) == 0) {
237 close(pfd[0]);
238 dup2(pfd[1], 1);
239 snprintf(w, sizeof(w), "%d", img.w);
240 snprintf(h, sizeof(h), "%d", img.h);
241 execl(info.f.cmd, info.f.cmd, files[fileidx].name, w, h, NULL);
242 error(EXIT_FAILURE, errno, "exec: %s", info.f.cmd);
243 }
244 close(pfd[1]);
245 if (info.pid < 0) {
246 close(pfd[0]);
247 } else {
248 fcntl(pfd[0], F_SETFL, O_NONBLOCK);
249 info.fd = pfd[0];
250 info.i = info.lastsep = 0;
251 }
252}
253
254void read_info(void)
255{
256 ssize_t i, n;
257 char buf[BAR_L_LEN];
258
259 while (true) {
260 n = read(info.fd, buf, sizeof(buf));
261 if (n < 0 && errno == EAGAIN)
262 return;
263 else if (n == 0)
264 goto end;
265 for (i = 0; i < n; i++) {
266 if (buf[i] == '\n') {
267 if (info.lastsep == 0) {
268 win.bar.l.buf[info.i++] = ' ';
269 info.lastsep = 1;
270 }
271 } else {
272 win.bar.l.buf[info.i++] = buf[i];
273 info.lastsep = 0;
274 }
275 if (info.i + 1 == win.bar.l.size)
276 goto end;
277 }
278 }
279end:
280 info.i -= info.lastsep;
281 win.bar.l.buf[info.i] = '\0';
282 win_draw(&win);
283 close_info();
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 close_info();
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];
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 close_info();
499 strncpy(win.bar.l.buf, "Running key handler...", win.bar.l.size);
500 win_draw(&win);
501 win_set_cursor(&win, CURSOR_WATCH);
502
503 snprintf(kstr, sizeof(kstr), "%s%s%s%s",
504 mask & ControlMask ? "C-" : "",
505 mask & Mod1Mask ? "M-" : "",
506 mask & ShiftMask ? "S-" : "", key);
507
508 if ((pid = fork()) == 0) {
509 close(pfd[1]);
510 dup2(pfd[0], 0);
511 execl(keyhandler.f.cmd, keyhandler.f.cmd, kstr, NULL);
512 error(EXIT_FAILURE, errno, "exec: %s", keyhandler.f.cmd);
513 }
514 close(pfd[0]);
515 if (pid < 0) {
516 error(0, errno, "fork");
517 fclose(pfs);
518 goto end;
519 }
520
521 for (f = i = 0; f < fcnt; i++) {
522 if ((marked && (files[i].flags & FF_MARK)) || (!marked && i == fileidx)) {
523 stat(files[i].path, &oldst[f]);
524 fprintf(pfs, "%s\n", files[i].name);
525 f++;
526 }
527 }
528 fclose(pfs);
529 while (waitpid(pid, NULL, 0) == -1 && errno == EINTR);
530
531 for (f = i = 0; f < fcnt; i++) {
532 if ((marked && (files[i].flags & FF_MARK)) || (!marked && i == fileidx)) {
533 if (stat(files[i].path, &st) != 0 ||
534 memcmp(&oldst[f].st_mtime, &st.st_mtime, sizeof(st.st_mtime)) != 0)
535 {
536 if (tns.thumbs != NULL) {
537 tns_unload(&tns, i);
538 tns.loadnext = MIN(tns.loadnext, i);
539 }
540 changed = true;
541 }
542 f++;
543 }
544 }
545 /* drop user input events that occurred while running the key handler */
546 while (XCheckIfEvent(win.env.dpy, &dump, is_input_ev, NULL));
547
548end:
549 if (mode == MODE_IMAGE) {
550 if (changed) {
551 img_close(&img, true);
552 load_image(fileidx);
553 } else {
554 open_info();
555 }
556 }
557 free(oldst);
558 reset_cursor();
559 redraw();
560}
561
562#define MODMASK(mask) ((mask) & (ShiftMask|ControlMask|Mod1Mask))
563
564void on_keypress(XKeyEvent *kev)
565{
566 int i;
567 unsigned int sh = 0;
568 KeySym ksym, shksym;
569 char dummy, key;
570 bool dirty = false;
571
572 XLookupString(kev, &key, 1, &ksym, NULL);
573
574 if (kev->state & ShiftMask) {
575 kev->state &= ~ShiftMask;
576 XLookupString(kev, &dummy, 1, &shksym, NULL);
577 kev->state |= ShiftMask;
578 if (ksym != shksym)
579 sh = ShiftMask;
580 }
581 if (IsModifierKey(ksym))
582 return;
583 if (ksym == XK_Escape && MODMASK(kev->state) == 0) {
584 extprefix = False;
585 } else if (extprefix) {
586 run_key_handler(XKeysymToString(ksym), kev->state & ~sh);
587 extprefix = False;
588 } else if (key >= '0' && key <= '9') {
589 /* number prefix for commands */
590 prefix = prefix * 10 + (int) (key - '0');
591 return;
592 } else for (i = 0; i < ARRLEN(keys); i++) {
593 if (keys[i].ksym == ksym &&
594 MODMASK(keys[i].mask | sh) == MODMASK(kev->state) &&
595 keys[i].cmd >= 0 && keys[i].cmd < CMD_COUNT &&
596 (cmds[keys[i].cmd].mode < 0 || cmds[keys[i].cmd].mode == mode))
597 {
598 if (cmds[keys[i].cmd].func(keys[i].arg))
599 dirty = true;
600 }
601 }
602 if (dirty)
603 redraw();
604 prefix = 0;
605}
606
607void on_buttonpress(XButtonEvent *bev)
608{
609 int i, sel;
610 bool dirty = false;
611 static Time firstclick;
612
613 if (mode == MODE_IMAGE) {
614 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
615 reset_cursor();
616
617 for (i = 0; i < ARRLEN(buttons); i++) {
618 if (buttons[i].button == bev->button &&
619 MODMASK(buttons[i].mask) == MODMASK(bev->state) &&
620 buttons[i].cmd >= 0 && buttons[i].cmd < CMD_COUNT &&
621 (cmds[buttons[i].cmd].mode < 0 || cmds[buttons[i].cmd].mode == mode))
622 {
623 if (cmds[buttons[i].cmd].func(buttons[i].arg))
624 dirty = true;
625 }
626 }
627 if (dirty)
628 redraw();
629 } else {
630 /* thumbnail mode (hard-coded) */
631 switch (bev->button) {
632 case Button1:
633 if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
634 if (sel != fileidx) {
635 tns_highlight(&tns, fileidx, false);
636 tns_highlight(&tns, sel, true);
637 fileidx = sel;
638 firstclick = bev->time;
639 redraw();
640 } else if (bev->time - firstclick <= TO_DOUBLE_CLICK) {
641 mode = MODE_IMAGE;
642 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
643 load_image(fileidx);
644 redraw();
645 } else {
646 firstclick = bev->time;
647 }
648 }
649 break;
650 case Button3:
651 if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
652 files[sel].flags ^= FF_MARK;
653 markcnt += files[sel].flags & FF_MARK ? 1 : -1;
654 tns_mark(&tns, sel, !!(files[sel].flags & FF_MARK));
655 redraw();
656 }
657 break;
658 case Button4:
659 case Button5:
660 if (tns_scroll(&tns, bev->button == Button4 ? DIR_UP : DIR_DOWN,
661 (bev->state & ControlMask) != 0))
662 redraw();
663 break;
664 }
665 }
666 prefix = 0;
667}
668
669const struct timespec ten_ms = {0, 10000000};
670
671void run(void)
672{
673 int xfd;
674 fd_set fds;
675 struct timeval timeout;
676 bool discard, init_thumb, load_thumb, to_set;
677 XEvent ev, nextev;
678
679 while (true) {
680 to_set = check_timeouts(&timeout);
681 init_thumb = mode == MODE_THUMB && tns.initnext < filecnt;
682 load_thumb = mode == MODE_THUMB && tns.loadnext < tns.end;
683
684 if ((init_thumb || load_thumb || to_set || info.fd != -1 ||
685 arl.fd != -1) && XPending(win.env.dpy) == 0)
686 {
687 if (load_thumb) {
688 set_timeout(redraw, TO_REDRAW_THUMBS, false);
689 if (!tns_load(&tns, tns.loadnext, false, false)) {
690 remove_file(tns.loadnext, false);
691 tns.dirty = true;
692 }
693 if (tns.loadnext >= tns.end)
694 redraw();
695 } else if (init_thumb) {
696 set_timeout(redraw, TO_REDRAW_THUMBS, false);
697 if (!tns_load(&tns, tns.initnext, false, true))
698 remove_file(tns.initnext, false);
699 } else {
700 xfd = ConnectionNumber(win.env.dpy);
701 FD_ZERO(&fds);
702 FD_SET(xfd, &fds);
703 if (info.fd != -1) {
704 FD_SET(info.fd, &fds);
705 xfd = MAX(xfd, info.fd);
706 }
707 if (arl.fd != -1) {
708 FD_SET(arl.fd, &fds);
709 xfd = MAX(xfd, arl.fd);
710 }
711 select(xfd + 1, &fds, 0, 0, to_set ? &timeout : NULL);
712 if (info.fd != -1 && FD_ISSET(info.fd, &fds))
713 read_info();
714 if (arl.fd != -1 && FD_ISSET(arl.fd, &fds)) {
715 if (arl_handle(&arl)) {
716 /* when too fast, imlib2 can't load the image */
717 nanosleep(&ten_ms, NULL);
718 img_close(&img, true);
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
789void sigchld(int sig)
790{
791 while (waitpid(-1, NULL, WNOHANG) > 0);
792}
793
794void setup_signal(int sig, void (*handler)(int sig))
795{
796 struct sigaction sa;
797
798 sa.sa_handler = handler;
799 sigemptyset(&sa.sa_mask);
800 sa.sa_flags = SA_RESTART | SA_NOCLDSTOP;
801 if (sigaction(sig, &sa, 0) == -1)
802 error(EXIT_FAILURE, errno, "signal %d", sig);
803}
804
805int main(int argc, char **argv)
806{
807 int i, start;
808 size_t n;
809 ssize_t len;
810 char *filename;
811 const char *homedir, *dsuffix = "";
812 struct stat fstats;
813 r_dir_t dir;
814
815 setup_signal(SIGCHLD, sigchld);
816 setup_signal(SIGPIPE, SIG_IGN);
817
818 setlocale(LC_COLLATE, "");
819
820 parse_options(argc, argv);
821
822 if (options->clean_cache) {
823 tns_init(&tns, NULL, NULL, NULL, NULL);
824 tns_clean_cache(&tns);
825 exit(EXIT_SUCCESS);
826 }
827
828 if (options->filecnt == 0 && !options->from_stdin) {
829 print_usage();
830 exit(EXIT_FAILURE);
831 }
832
833 if (options->recursive || options->from_stdin)
834 filecnt = 1024;
835 else
836 filecnt = options->filecnt;
837
838 files = emalloc(filecnt * sizeof(*files));
839 memset(files, 0, filecnt * sizeof(*files));
840 fileidx = 0;
841
842 if (options->from_stdin) {
843 n = 0;
844 filename = NULL;
845 while ((len = getline(&filename, &n, stdin)) > 0) {
846 if (filename[len-1] == '\n')
847 filename[len-1] = '\0';
848 check_add_file(filename, true);
849 }
850 free(filename);
851 }
852
853 for (i = 0; i < options->filecnt; i++) {
854 filename = options->filenames[i];
855
856 if (stat(filename, &fstats) < 0) {
857 error(0, errno, "%s", filename);
858 continue;
859 }
860 if (!S_ISDIR(fstats.st_mode)) {
861 check_add_file(filename, true);
862 } else {
863 if (r_opendir(&dir, filename, options->recursive) < 0) {
864 error(0, errno, "%s", filename);
865 continue;
866 }
867 start = fileidx;
868 while ((filename = r_readdir(&dir)) != NULL) {
869 check_add_file(filename, false);
870 free((void*) filename);
871 }
872 r_closedir(&dir);
873 if (fileidx - start > 1)
874 qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp);
875 }
876 }
877
878 if (fileidx == 0)
879 error(EXIT_FAILURE, 0, "No valid image file given, aborting");
880
881 filecnt = fileidx;
882 fileidx = options->startnum < filecnt ? options->startnum : 0;
883
884 for (i = 0; i < ARRLEN(buttons); i++) {
885 if (buttons[i].cmd == i_cursor_navigate) {
886 imgcursor[0] = CURSOR_LEFT;
887 imgcursor[2] = CURSOR_RIGHT;
888 break;
889 }
890 }
891
892 win_init(&win);
893 img_init(&img, &win);
894 arl_init(&arl);
895
896 if ((homedir = getenv("XDG_CONFIG_HOME")) == NULL || homedir[0] == '\0') {
897 homedir = getenv("HOME");
898 dsuffix = "/.config";
899 }
900 if (homedir != NULL) {
901 extcmd_t *cmd[] = { &info.f, &keyhandler.f };
902 const char *name[] = { "image-info", "key-handler" };
903
904 for (i = 0; i < ARRLEN(cmd); i++) {
905 n = strlen(homedir) + strlen(dsuffix) + strlen(name[i]) + 12;
906 cmd[i]->cmd = (char*) emalloc(n);
907 snprintf(cmd[i]->cmd, n, "%s%s/sxiv/exec/%s", homedir, dsuffix, name[i]);
908 if (access(cmd[i]->cmd, X_OK) != 0)
909 cmd[i]->err = errno;
910 }
911 } else {
912 error(0, 0, "Exec directory not found");
913 }
914 info.fd = -1;
915
916 if (options->thumb_mode) {
917 mode = MODE_THUMB;
918 tns_init(&tns, files, &filecnt, &fileidx, &win);
919 while (!tns_load(&tns, fileidx, false, false))
920 remove_file(fileidx, false);
921 } else {
922 mode = MODE_IMAGE;
923 tns.thumbs = NULL;
924 load_image(fileidx);
925 }
926 win_open(&win);
927 win_set_cursor(&win, CURSOR_WATCH);
928
929 atexit(cleanup);
930
931 set_timeout(redraw, 25, false);
932
933 run();
934
935 return 0;
936}