Krolyxon
Commit 09b04d7

Branches

Refs →

nsxivmain.c

935 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;
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 pid_t pid;
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 close_info(void)
217{
218 if (info.fd != -1) {
219 kill(info.pid, SIGTERM);
220 close(info.fd);
221 info.fd = -1;
222 }
223}
224
225void open_info(void)
226{
227 int pfd[2];
228 char w[12], h[12];
229
230 if (info.f.err != 0 || info.fd >= 0 || win.bar.h == 0)
231 return;
232 win.bar.l.buf[0] = '\0';
233 if (pipe(pfd) < 0)
234 return;
235 if ((info.pid = fork()) == 0) {
236 close(pfd[0]);
237 dup2(pfd[1], 1);
238 snprintf(w, sizeof(w), "%d", img.w);
239 snprintf(h, sizeof(h), "%d", img.h);
240 execl(info.f.cmd, info.f.cmd, files[fileidx].name, w, h, NULL);
241 error(EXIT_FAILURE, errno, "exec: %s", info.f.cmd);
242 }
243 close(pfd[1]);
244 if (info.pid < 0) {
245 close(pfd[0]);
246 } else {
247 fcntl(pfd[0], F_SETFL, O_NONBLOCK);
248 info.fd = pfd[0];
249 info.i = info.lastsep = 0;
250 }
251}
252
253void read_info(void)
254{
255 ssize_t i, n;
256 char buf[BAR_L_LEN];
257
258 while (true) {
259 n = read(info.fd, buf, sizeof(buf));
260 if (n < 0 && errno == EAGAIN)
261 return;
262 else if (n == 0)
263 goto end;
264 for (i = 0; i < n; i++) {
265 if (buf[i] == '\n') {
266 if (info.lastsep == 0) {
267 win.bar.l.buf[info.i++] = ' ';
268 info.lastsep = 1;
269 }
270 } else {
271 win.bar.l.buf[info.i++] = buf[i];
272 info.lastsep = 0;
273 }
274 if (info.i + 1 == win.bar.l.size)
275 goto end;
276 }
277 }
278end:
279 info.i -= info.lastsep;
280 win.bar.l.buf[info.i] = '\0';
281 win_draw(&win);
282 close_info();
283}
284
285void load_image(int new)
286{
287 bool prev = new < fileidx;
288 static int current;
289
290 if (new < 0 || new >= filecnt)
291 return;
292
293 if (win.xwin != None)
294 win_set_cursor(&win, CURSOR_WATCH);
295 reset_timeout(slideshow);
296
297 if (new != current)
298 alternate = current;
299
300 img_close(&img, false);
301 while (!img_load(&img, &files[new])) {
302 remove_file(new, false);
303 if (new >= filecnt)
304 new = filecnt - 1;
305 else if (new > 0 && prev)
306 new--;
307 }
308 files[new].flags &= ~FF_WARN;
309 fileidx = current = new;
310
311 close_info();
312 open_info();
313 arl_setup(&arl, files[fileidx].path);
314
315 if (img.multi.cnt > 0 && img.multi.animate)
316 set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
317 else
318 reset_timeout(animate);
319}
320
321void bar_put(win_bar_t *bar, const char *fmt, ...)
322{
323 size_t len = bar->size - (bar->p - bar->buf), n;
324 va_list ap;
325
326 va_start(ap, fmt);
327 n = vsnprintf(bar->p, len, fmt, ap);
328 bar->p += MIN(len, n);
329 va_end(ap);
330}
331
332#define BAR_SEP " "
333
334void update_info(void)
335{
336 unsigned int i, fn, fw;
337 char title[256];
338 const char * mark;
339 win_bar_t *l = &win.bar.l, *r = &win.bar.r;
340
341 /* update window title */
342 if (mode == MODE_THUMB) {
343 win_set_title(&win, "sxiv");
344 } else {
345 snprintf(title, sizeof(title), "sxiv - %s", files[fileidx].name);
346 win_set_title(&win, title);
347 }
348
349 /* update bar contents */
350 if (win.bar.h == 0)
351 return;
352 for (fw = 0, i = filecnt; i > 0; fw++, i /= 10);
353 mark = files[fileidx].flags & FF_MARK ? "* " : "";
354 l->p = l->buf;
355 r->p = r->buf;
356 if (mode == MODE_THUMB) {
357 if (tns.loadnext < tns.end)
358 bar_put(l, "Loading... %0*d", fw, tns.loadnext + 1);
359 else if (tns.initnext < filecnt)
360 bar_put(l, "Caching... %0*d", fw, tns.initnext + 1);
361 else
362 strncpy(l->buf, files[fileidx].name, l->size);
363 bar_put(r, "%s%0*d/%d", mark, fw, fileidx + 1, filecnt);
364 } else {
365 bar_put(r, "%s", mark);
366 if (img.ss.on) {
367 if (img.ss.delay % 10 != 0)
368 bar_put(r, "%2.1fs" BAR_SEP, (float)img.ss.delay / 10);
369 else
370 bar_put(r, "%ds" BAR_SEP, img.ss.delay / 10);
371 }
372 if (img.gamma != 0)
373 bar_put(r, "G%+d" BAR_SEP, img.gamma);
374 bar_put(r, "%3d%%" BAR_SEP, (int) (img.zoom * 100.0));
375 if (img.multi.cnt > 0) {
376 for (fn = 0, i = img.multi.cnt; i > 0; fn++, i /= 10);
377 bar_put(r, "%0*d/%d" BAR_SEP, fn, img.multi.sel + 1, img.multi.cnt);
378 }
379 bar_put(r, "%0*d/%d", fw, fileidx + 1, filecnt);
380 if (info.f.err)
381 strncpy(l->buf, files[fileidx].name, l->size);
382 }
383}
384
385int ptr_third_x(void)
386{
387 int x, y;
388
389 win_cursor_pos(&win, &x, &y);
390 return MAX(0, MIN(2, (x / (win.w * 0.33))));
391}
392
393void redraw(void)
394{
395 int t;
396
397 if (mode == MODE_IMAGE) {
398 img_render(&img);
399 if (img.ss.on) {
400 t = img.ss.delay * 100;
401 if (img.multi.cnt > 0 && img.multi.animate)
402 t = MAX(t, img.multi.length);
403 set_timeout(slideshow, t, false);
404 }
405 } else {
406 tns_render(&tns);
407 }
408 update_info();
409 win_draw(&win);
410 reset_timeout(redraw);
411 reset_cursor();
412}
413
414void reset_cursor(void)
415{
416 int c, i;
417 cursor_t cursor = CURSOR_NONE;
418
419 if (mode == MODE_IMAGE) {
420 for (i = 0; i < ARRLEN(timeouts); i++) {
421 if (timeouts[i].handler == reset_cursor) {
422 if (timeouts[i].active) {
423 c = ptr_third_x();
424 c = MAX(fileidx > 0 ? 0 : 1, c);
425 c = MIN(fileidx + 1 < filecnt ? 2 : 1, c);
426 cursor = imgcursor[c];
427 }
428 break;
429 }
430 }
431 } else {
432 if (tns.loadnext < tns.end || tns.initnext < filecnt)
433 cursor = CURSOR_WATCH;
434 else
435 cursor = CURSOR_ARROW;
436 }
437 win_set_cursor(&win, cursor);
438}
439
440void animate(void)
441{
442 if (img_frame_animate(&img)) {
443 redraw();
444 set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
445 }
446}
447
448void slideshow(void)
449{
450 load_image(fileidx + 1 < filecnt ? fileidx + 1 : 0);
451 redraw();
452}
453
454void clear_resize(void)
455{
456 resized = false;
457}
458
459Bool is_input_ev(Display *dpy, XEvent *ev, XPointer arg)
460{
461 return ev->type == ButtonPress || ev->type == KeyPress;
462}
463
464void run_key_handler(const char *key, unsigned int mask)
465{
466 pid_t pid;
467 FILE *pfs;
468 bool marked = mode == MODE_THUMB && markcnt > 0;
469 bool changed = false;
470 int f, i, pfd[2];
471 int fcnt = marked ? markcnt : 1;
472 char kstr[32];
473 struct stat *oldst, st;
474 XEvent dump;
475
476 if (keyhandler.f.err != 0) {
477 if (!keyhandler.warned) {
478 error(0, keyhandler.f.err, "%s", keyhandler.f.cmd);
479 keyhandler.warned = true;
480 }
481 return;
482 }
483 if (key == NULL)
484 return;
485
486 if (pipe(pfd) < 0) {
487 error(0, errno, "pipe");
488 return;
489 }
490 if ((pfs = fdopen(pfd[1], "w")) == NULL) {
491 error(0, errno, "open pipe");
492 close(pfd[0]), close(pfd[1]);
493 return;
494 }
495 oldst = emalloc(fcnt * sizeof(*oldst));
496
497 close_info();
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 while (waitpid(pid, NULL, 0) == -1 && errno == EINTR);
529
530 for (f = i = 0; f < fcnt; i++) {
531 if ((marked && (files[i].flags & FF_MARK)) || (!marked && i == fileidx)) {
532 if (stat(files[i].path, &st) != 0 ||
533 memcmp(&oldst[f].st_mtime, &st.st_mtime, sizeof(st.st_mtime)) != 0)
534 {
535 if (tns.thumbs != NULL) {
536 tns_unload(&tns, i);
537 tns.loadnext = MIN(tns.loadnext, i);
538 }
539 changed = true;
540 }
541 f++;
542 }
543 }
544 /* drop user input events that occurred while running the key handler */
545 while (XCheckIfEvent(win.env.dpy, &dump, is_input_ev, NULL));
546
547end:
548 if (mode == MODE_IMAGE) {
549 if (changed) {
550 img_close(&img, true);
551 load_image(fileidx);
552 } else {
553 open_info();
554 }
555 }
556 free(oldst);
557 reset_cursor();
558 redraw();
559}
560
561#define MODMASK(mask) ((mask) & (ShiftMask|ControlMask|Mod1Mask))
562
563void on_keypress(XKeyEvent *kev)
564{
565 int i;
566 unsigned int sh = 0;
567 KeySym ksym, shksym;
568 char dummy, key;
569 bool dirty = false;
570
571 XLookupString(kev, &key, 1, &ksym, NULL);
572
573 if (kev->state & ShiftMask) {
574 kev->state &= ~ShiftMask;
575 XLookupString(kev, &dummy, 1, &shksym, NULL);
576 kev->state |= ShiftMask;
577 if (ksym != shksym)
578 sh = ShiftMask;
579 }
580 if (IsModifierKey(ksym))
581 return;
582 if (ksym == XK_Escape && MODMASK(kev->state) == 0) {
583 extprefix = False;
584 } else if (extprefix) {
585 run_key_handler(XKeysymToString(ksym), kev->state & ~sh);
586 extprefix = False;
587 } else if (key >= '0' && key <= '9') {
588 /* number prefix for commands */
589 prefix = prefix * 10 + (int) (key - '0');
590 return;
591 } else for (i = 0; i < ARRLEN(keys); i++) {
592 if (keys[i].ksym == ksym &&
593 MODMASK(keys[i].mask | sh) == MODMASK(kev->state) &&
594 keys[i].cmd >= 0 && keys[i].cmd < CMD_COUNT &&
595 (cmds[keys[i].cmd].mode < 0 || cmds[keys[i].cmd].mode == mode))
596 {
597 if (cmds[keys[i].cmd].func(keys[i].arg))
598 dirty = true;
599 }
600 }
601 if (dirty)
602 redraw();
603 prefix = 0;
604}
605
606void on_buttonpress(XButtonEvent *bev)
607{
608 int i, sel;
609 bool dirty = false;
610 static Time firstclick;
611
612 if (mode == MODE_IMAGE) {
613 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
614 reset_cursor();
615
616 for (i = 0; i < ARRLEN(buttons); i++) {
617 if (buttons[i].button == bev->button &&
618 MODMASK(buttons[i].mask) == MODMASK(bev->state) &&
619 buttons[i].cmd >= 0 && buttons[i].cmd < CMD_COUNT &&
620 (cmds[buttons[i].cmd].mode < 0 || cmds[buttons[i].cmd].mode == mode))
621 {
622 if (cmds[buttons[i].cmd].func(buttons[i].arg))
623 dirty = true;
624 }
625 }
626 if (dirty)
627 redraw();
628 } else {
629 /* thumbnail mode (hard-coded) */
630 switch (bev->button) {
631 case Button1:
632 if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
633 if (sel != fileidx) {
634 tns_highlight(&tns, fileidx, false);
635 tns_highlight(&tns, sel, true);
636 fileidx = sel;
637 firstclick = bev->time;
638 redraw();
639 } else if (bev->time - firstclick <= TO_DOUBLE_CLICK) {
640 mode = MODE_IMAGE;
641 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
642 load_image(fileidx);
643 redraw();
644 } else {
645 firstclick = bev->time;
646 }
647 }
648 break;
649 case Button3:
650 if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
651 files[sel].flags ^= FF_MARK;
652 markcnt += files[sel].flags & FF_MARK ? 1 : -1;
653 tns_mark(&tns, sel, !!(files[sel].flags & FF_MARK));
654 redraw();
655 }
656 break;
657 case Button4:
658 case Button5:
659 if (tns_scroll(&tns, bev->button == Button4 ? DIR_UP : DIR_DOWN,
660 (bev->state & ControlMask) != 0))
661 redraw();
662 break;
663 }
664 }
665 prefix = 0;
666}
667
668const struct timespec ten_ms = {0, 10000000};
669
670void run(void)
671{
672 int xfd;
673 fd_set fds;
674 struct timeval timeout;
675 bool discard, init_thumb, load_thumb, to_set;
676 XEvent ev, nextev;
677
678 while (true) {
679 to_set = check_timeouts(&timeout);
680 init_thumb = mode == MODE_THUMB && tns.initnext < filecnt;
681 load_thumb = mode == MODE_THUMB && tns.loadnext < tns.end;
682
683 if ((init_thumb || load_thumb || to_set || info.fd != -1 ||
684 arl.fd != -1) && XPending(win.env.dpy) == 0)
685 {
686 if (load_thumb) {
687 set_timeout(redraw, TO_REDRAW_THUMBS, false);
688 if (!tns_load(&tns, tns.loadnext, false, false)) {
689 remove_file(tns.loadnext, false);
690 tns.dirty = true;
691 }
692 if (tns.loadnext >= tns.end)
693 redraw();
694 } else if (init_thumb) {
695 set_timeout(redraw, TO_REDRAW_THUMBS, false);
696 if (!tns_load(&tns, tns.initnext, false, true))
697 remove_file(tns.initnext, false);
698 } else {
699 xfd = ConnectionNumber(win.env.dpy);
700 FD_ZERO(&fds);
701 FD_SET(xfd, &fds);
702 if (info.fd != -1) {
703 FD_SET(info.fd, &fds);
704 xfd = MAX(xfd, info.fd);
705 }
706 if (arl.fd != -1) {
707 FD_SET(arl.fd, &fds);
708 xfd = MAX(xfd, arl.fd);
709 }
710 select(xfd + 1, &fds, 0, 0, to_set ? &timeout : NULL);
711 if (info.fd != -1 && FD_ISSET(info.fd, &fds))
712 read_info();
713 if (arl.fd != -1 && FD_ISSET(arl.fd, &fds)) {
714 if (arl_handle(&arl)) {
715 /* when too fast, imlib2 can't load the image */
716 nanosleep(&ten_ms, NULL);
717 img_close(&img, true);
718 load_image(fileidx);
719 redraw();
720 }
721 }
722 }
723 continue;
724 }
725
726 do {
727 XNextEvent(win.env.dpy, &ev);
728 discard = false;
729 if (XEventsQueued(win.env.dpy, QueuedAlready) > 0) {
730 XPeekEvent(win.env.dpy, &nextev);
731 switch (ev.type) {
732 case ConfigureNotify:
733 case MotionNotify:
734 discard = ev.type == nextev.type;
735 break;
736 case KeyPress:
737 discard = (nextev.type == KeyPress || nextev.type == KeyRelease)
738 && ev.xkey.keycode == nextev.xkey.keycode;
739 break;
740 }
741 }
742 } while (discard);
743
744 switch (ev.type) {
745 /* handle events */
746 case ButtonPress:
747 on_buttonpress(&ev.xbutton);
748 break;
749 case ClientMessage:
750 if ((Atom) ev.xclient.data.l[0] == atoms[ATOM_WM_DELETE_WINDOW])
751 cmds[g_quit].func(0);
752 break;
753 case ConfigureNotify:
754 if (win_configure(&win, &ev.xconfigure)) {
755 if (mode == MODE_IMAGE) {
756 img.dirty = true;
757 img.checkpan = true;
758 } else {
759 tns.dirty = true;
760 }
761 if (!resized || win.fullscreen) {
762 redraw();
763 set_timeout(clear_resize, TO_REDRAW_RESIZE, false);
764 resized = true;
765 } else {
766 set_timeout(redraw, TO_REDRAW_RESIZE, false);
767 }
768 }
769 break;
770 case KeyPress:
771 on_keypress(&ev.xkey);
772 break;
773 case MotionNotify:
774 if (mode == MODE_IMAGE) {
775 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
776 reset_cursor();
777 }
778 break;
779 }
780 }
781}
782
783int fncmp(const void *a, const void *b)
784{
785 return strcoll(((fileinfo_t*) a)->name, ((fileinfo_t*) b)->name);
786}
787
788void sigchld(int sig)
789{
790 while (waitpid(-1, NULL, WNOHANG) > 0);
791}
792
793void setup_signal(int sig, void (*handler)(int sig))
794{
795 struct sigaction sa;
796
797 sa.sa_handler = handler;
798 sigemptyset(&sa.sa_mask);
799 sa.sa_flags = SA_RESTART | SA_NOCLDSTOP;
800 if (sigaction(sig, &sa, 0) == -1)
801 error(EXIT_FAILURE, errno, "signal %d", sig);
802}
803
804int main(int argc, char **argv)
805{
806 int i, start;
807 size_t n;
808 ssize_t len;
809 char *filename;
810 const char *homedir, *dsuffix = "";
811 struct stat fstats;
812 r_dir_t dir;
813
814 setup_signal(SIGCHLD, sigchld);
815 setup_signal(SIGPIPE, SIG_IGN);
816
817 setlocale(LC_COLLATE, "");
818
819 parse_options(argc, argv);
820
821 if (options->clean_cache) {
822 tns_init(&tns, NULL, NULL, NULL, NULL);
823 tns_clean_cache(&tns);
824 exit(EXIT_SUCCESS);
825 }
826
827 if (options->filecnt == 0 && !options->from_stdin) {
828 print_usage();
829 exit(EXIT_FAILURE);
830 }
831
832 if (options->recursive || options->from_stdin)
833 filecnt = 1024;
834 else
835 filecnt = options->filecnt;
836
837 files = emalloc(filecnt * sizeof(*files));
838 memset(files, 0, filecnt * sizeof(*files));
839 fileidx = 0;
840
841 if (options->from_stdin) {
842 n = 0;
843 filename = NULL;
844 while ((len = getline(&filename, &n, stdin)) > 0) {
845 if (filename[len-1] == '\n')
846 filename[len-1] = '\0';
847 check_add_file(filename, true);
848 }
849 free(filename);
850 }
851
852 for (i = 0; i < options->filecnt; i++) {
853 filename = options->filenames[i];
854
855 if (stat(filename, &fstats) < 0) {
856 error(0, errno, "%s", filename);
857 continue;
858 }
859 if (!S_ISDIR(fstats.st_mode)) {
860 check_add_file(filename, true);
861 } else {
862 if (r_opendir(&dir, filename, options->recursive) < 0) {
863 error(0, errno, "%s", filename);
864 continue;
865 }
866 start = fileidx;
867 while ((filename = r_readdir(&dir)) != NULL) {
868 check_add_file(filename, false);
869 free((void*) filename);
870 }
871 r_closedir(&dir);
872 if (fileidx - start > 1)
873 qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp);
874 }
875 }
876
877 if (fileidx == 0)
878 error(EXIT_FAILURE, 0, "No valid image file given, aborting");
879
880 filecnt = fileidx;
881 fileidx = options->startnum < filecnt ? options->startnum : 0;
882
883 for (i = 0; i < ARRLEN(buttons); i++) {
884 if (buttons[i].cmd == i_cursor_navigate) {
885 imgcursor[0] = CURSOR_LEFT;
886 imgcursor[2] = CURSOR_RIGHT;
887 break;
888 }
889 }
890
891 win_init(&win);
892 img_init(&img, &win);
893 arl_init(&arl);
894
895 if ((homedir = getenv("XDG_CONFIG_HOME")) == NULL || homedir[0] == '\0') {
896 homedir = getenv("HOME");
897 dsuffix = "/.config";
898 }
899 if (homedir != NULL) {
900 extcmd_t *cmd[] = { &info.f, &keyhandler.f };
901 const char *name[] = { "image-info", "key-handler" };
902
903 for (i = 0; i < ARRLEN(cmd); i++) {
904 n = strlen(homedir) + strlen(dsuffix) + strlen(name[i]) + 12;
905 cmd[i]->cmd = (char*) emalloc(n);
906 snprintf(cmd[i]->cmd, n, "%s%s/sxiv/exec/%s", homedir, dsuffix, name[i]);
907 if (access(cmd[i]->cmd, X_OK) != 0)
908 cmd[i]->err = errno;
909 }
910 } else {
911 error(0, 0, "Exec directory not found");
912 }
913 info.fd = -1;
914
915 if (options->thumb_mode) {
916 mode = MODE_THUMB;
917 tns_init(&tns, files, &filecnt, &fileidx, &win);
918 while (!tns_load(&tns, fileidx, false, false))
919 remove_file(fileidx, false);
920 } else {
921 mode = MODE_IMAGE;
922 tns.thumbs = NULL;
923 load_image(fileidx);
924 }
925 win_open(&win);
926 win_set_cursor(&win, CURSOR_WATCH);
927
928 atexit(cleanup);
929
930 set_timeout(redraw, 25, false);
931
932 run();
933
934 return 0;
935}