Krolyxon
Commit 87a1ace

Branches

Refs →

nsxivmain.c

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