Krolyxon
Commit f7714ea

Branches

Refs →

nsxivmain.c

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