Krolyxon
Commit 8934744

Branches

Refs →

nsxivmain.c

966 lines · 21.0 KB · C

1/* Copyright 2011-2020 Bert Muennich
2 * Copyright 2021 nsxiv contributors
3 *
4 * This file is a part of nsxiv.
5 *
6 * nsxiv is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published
8 * by the Free Software Foundation; either version 2 of the License,
9 * or (at your option) any later version.
10 *
11 * nsxiv is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with nsxiv. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "nsxiv.h"
21#include "commands.h"
22#define _MAPPINGS_CONFIG
23#include "config.h"
24
25#include <stdlib.h>
26#include <string.h>
27#include <fcntl.h>
28#include <unistd.h>
29#include <errno.h>
30#include <locale.h>
31#include <signal.h>
32#include <sys/select.h>
33#include <sys/stat.h>
34#include <sys/wait.h>
35#include <time.h>
36#include <X11/keysym.h>
37#include <X11/XF86keysym.h>
38
39#define MODMASK(mask) ((mask) & ~ignore_mask)
40#define BAR_SEP " "
41
42typedef struct {
43 struct timeval when;
44 bool active;
45 timeout_f handler;
46} timeout_t;
47
48/* timeout handler functions: */
49void redraw(void);
50void reset_cursor(void);
51void animate(void);
52void slideshow(void);
53void clear_resize(void);
54
55appmode_t mode;
56arl_t arl;
57img_t img;
58tns_t tns;
59win_t win;
60
61fileinfo_t *files;
62int filecnt, fileidx;
63int alternate;
64int markcnt;
65int markidx;
66
67int prefix;
68bool extprefix;
69
70bool resized = false;
71
72typedef struct {
73 int err;
74 char *cmd;
75} extcmd_t;
76
77struct {
78 extcmd_t f;
79 int fd;
80 unsigned int i, lastsep;
81 pid_t pid;
82} info;
83
84struct {
85 extcmd_t f;
86 bool warned;
87} keyhandler;
88
89timeout_t timeouts[] = {
90 { { 0, 0 }, false, redraw },
91 { { 0, 0 }, false, reset_cursor },
92 { { 0, 0 }, false, animate },
93 { { 0, 0 }, false, slideshow },
94 { { 0, 0 }, false, clear_resize },
95};
96
97/**************************
98 function implementations
99 **************************/
100void cleanup(void)
101{
102 img_close(&img, false);
103 arl_cleanup(&arl);
104 tns_free(&tns);
105 win_close(&win);
106}
107
108void check_add_file(char *filename, bool given)
109{
110 char *path;
111
112 if (*filename == '\0')
113 return;
114
115 if (access(filename, R_OK) < 0 ||
116 (path = realpath(filename, NULL)) == NULL)
117 {
118 if (given)
119 error(0, errno, "%s", filename);
120 return;
121 }
122
123 if (fileidx == filecnt) {
124 filecnt *= 2;
125 files = erealloc(files, filecnt * sizeof(*files));
126 memset(&files[filecnt/2], 0, filecnt/2 * sizeof(*files));
127 }
128
129 files[fileidx].name = estrdup(filename);
130 files[fileidx].path = path;
131 if (given)
132 files[fileidx].flags |= FF_WARN;
133 fileidx++;
134}
135
136void remove_file(int n, bool manual)
137{
138 if (n < 0 || n >= filecnt)
139 return;
140
141 if (filecnt == 1) {
142 if (!manual)
143 fprintf(stderr, "nsxiv: no more files to display, aborting\n");
144 exit(manual ? EXIT_SUCCESS : EXIT_FAILURE);
145 }
146 if (files[n].flags & FF_MARK)
147 markcnt--;
148
149 if (files[n].path != files[n].name)
150 free((void*) files[n].path);
151 free((void*) files[n].name);
152
153 if (n + 1 < filecnt) {
154 if (tns.thumbs != NULL) {
155 memmove(tns.thumbs + n, tns.thumbs + n + 1, (filecnt - n - 1) *
156 sizeof(*tns.thumbs));
157 memset(tns.thumbs + filecnt - 1, 0, sizeof(*tns.thumbs));
158 }
159 memmove(files + n, files + n + 1, (filecnt - n - 1) * sizeof(*files));
160 }
161 filecnt--;
162 if (fileidx > n || fileidx == filecnt)
163 fileidx--;
164 if (alternate > n || alternate == filecnt)
165 alternate--;
166 if (markidx > n || markidx == filecnt)
167 markidx--;
168}
169
170void set_timeout(timeout_f handler, int time, bool overwrite)
171{
172 unsigned int i;
173
174 for (i = 0; i < ARRLEN(timeouts); i++) {
175 if (timeouts[i].handler == handler) {
176 if (!timeouts[i].active || overwrite) {
177 gettimeofday(&timeouts[i].when, 0);
178 TV_ADD_MSEC(&timeouts[i].when, time);
179 timeouts[i].active = true;
180 }
181 return;
182 }
183 }
184}
185
186void reset_timeout(timeout_f handler)
187{
188 unsigned int i;
189
190 for (i = 0; i < ARRLEN(timeouts); i++) {
191 if (timeouts[i].handler == handler) {
192 timeouts[i].active = false;
193 return;
194 }
195 }
196}
197
198bool check_timeouts(struct timeval *t)
199{
200 int i = 0, tdiff, tmin = -1;
201 struct timeval now;
202
203 while (i < ARRLEN(timeouts)) {
204 if (timeouts[i].active) {
205 gettimeofday(&now, 0);
206 tdiff = TV_DIFF(&timeouts[i].when, &now);
207 if (tdiff <= 0) {
208 timeouts[i].active = false;
209 if (timeouts[i].handler != NULL)
210 timeouts[i].handler();
211 i = tmin = -1;
212 } else if (tmin < 0 || tdiff < tmin) {
213 tmin = tdiff;
214 }
215 }
216 i++;
217 }
218 if (tmin > 0 && t != NULL)
219 TV_SET_MSEC(t, tmin);
220 return tmin > 0;
221}
222
223void close_info(void)
224{
225 if (info.fd != -1) {
226 kill(info.pid, SIGTERM);
227 close(info.fd);
228 info.fd = -1;
229 }
230}
231
232void open_info(void)
233{
234 int pfd[2];
235 char w[12], h[12];
236
237 if (info.f.err || info.fd >= 0 || win.bar.h == 0)
238 return;
239 win.bar.l.buf[0] = '\0';
240 if (pipe(pfd) < 0)
241 return;
242 if ((info.pid = fork()) == 0) {
243 close(pfd[0]);
244 dup2(pfd[1], 1);
245 snprintf(w, sizeof(w), "%d", img.w);
246 snprintf(h, sizeof(h), "%d", img.h);
247 execl(info.f.cmd, info.f.cmd, files[fileidx].name, w, h, NULL);
248 error(EXIT_FAILURE, errno, "exec: %s", info.f.cmd);
249 }
250 close(pfd[1]);
251 if (info.pid < 0) {
252 close(pfd[0]);
253 } else {
254 fcntl(pfd[0], F_SETFL, O_NONBLOCK);
255 info.fd = pfd[0];
256 info.i = info.lastsep = 0;
257 }
258}
259
260void read_info(void)
261{
262 ssize_t i, n;
263 char buf[BAR_L_LEN];
264
265 while (true) {
266 n = read(info.fd, buf, sizeof(buf));
267 if (n < 0 && errno == EAGAIN)
268 return;
269 else if (n == 0)
270 goto end;
271 for (i = 0; i < n; i++) {
272 if (buf[i] == '\n') {
273 if (info.lastsep == 0) {
274 win.bar.l.buf[info.i++] = ' ';
275 info.lastsep = 1;
276 }
277 } else {
278 win.bar.l.buf[info.i++] = buf[i];
279 info.lastsep = 0;
280 }
281 if (info.i + 1 == win.bar.l.size)
282 goto end;
283 }
284 }
285end:
286 info.i -= info.lastsep;
287 win.bar.l.buf[info.i] = '\0';
288 win_draw(&win);
289 close_info();
290}
291
292void load_image(int new)
293{
294 bool prev = new < fileidx;
295 static int current;
296
297 if (new < 0 || new >= filecnt)
298 return;
299
300 if (win.xwin != None)
301 win_set_cursor(&win, CURSOR_WATCH);
302 reset_timeout(slideshow);
303
304 if (new != current)
305 alternate = current;
306
307 img_close(&img, false);
308 while (!img_load(&img, &files[new])) {
309 remove_file(new, false);
310 if (new >= filecnt)
311 new = filecnt - 1;
312 else if (new > 0 && prev)
313 new--;
314 }
315 files[new].flags &= ~FF_WARN;
316 fileidx = current = new;
317
318 close_info();
319 open_info();
320 arl_setup(&arl, files[fileidx].path);
321 win_set_title(&win, files[fileidx].path);
322
323 if (img.multi.cnt > 0 && img.multi.animate)
324 set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
325 else
326 reset_timeout(animate);
327}
328
329bool mark_image(int n, bool on)
330{
331 markidx = n;
332 if (!!(files[n].flags & FF_MARK) != on) {
333 files[n].flags ^= FF_MARK;
334 markcnt += on ? 1 : -1;
335 if (mode == MODE_THUMB)
336 tns_mark(&tns, n, on);
337 return true;
338 }
339 return false;
340}
341
342void bar_put(win_bar_t *bar, const char *fmt, ...)
343{
344 size_t len = bar->size - (bar->p - bar->buf), n;
345 va_list ap;
346
347 va_start(ap, fmt);
348 n = vsnprintf(bar->p, len, fmt, ap);
349 bar->p += MIN(len, n);
350 va_end(ap);
351}
352
353void update_info(void)
354{
355 unsigned int i, fn, fw;
356 const char * mark;
357 win_bar_t *l = &win.bar.l, *r = &win.bar.r;
358
359 /* update bar contents */
360 if (win.bar.h == 0)
361 return;
362 for (fw = 0, i = filecnt; i > 0; fw++, i /= 10);
363 mark = files[fileidx].flags & FF_MARK ? "* " : "";
364 l->p = l->buf;
365 r->p = r->buf;
366 if (mode == MODE_THUMB) {
367 if (tns.loadnext < tns.end)
368 bar_put(l, "Loading... %0*d", fw, tns.loadnext + 1);
369 else if (tns.initnext < filecnt)
370 bar_put(l, "Caching... %0*d", fw, tns.initnext + 1);
371 else
372 strncpy(l->buf, files[fileidx].name, l->size);
373 bar_put(r, "%s%0*d/%d", mark, fw, fileidx + 1, filecnt);
374 } else {
375 bar_put(r, "%s", mark);
376 if (img.ss.on) {
377 if (img.ss.delay % 10 != 0)
378 bar_put(r, "%2.1fs" BAR_SEP, (float)img.ss.delay / 10);
379 else
380 bar_put(r, "%ds" BAR_SEP, img.ss.delay / 10);
381 }
382 if (img.gamma)
383 bar_put(r, "G%+d" BAR_SEP, img.gamma);
384 bar_put(r, "%3d%%" BAR_SEP, (int) (img.zoom * 100.0));
385 if (img.multi.cnt > 0) {
386 for (fn = 0, i = img.multi.cnt; i > 0; fn++, i /= 10);
387 bar_put(r, "%0*d/%d" BAR_SEP, fn, img.multi.sel + 1, img.multi.cnt);
388 }
389 bar_put(r, "%0*d/%d", fw, fileidx + 1, filecnt);
390 if (info.f.err)
391 strncpy(l->buf, files[fileidx].name, l->size);
392 }
393}
394
395int ptr_third_x(void)
396{
397 int x, y;
398
399 win_cursor_pos(&win, &x, &y);
400 return MAX(0, MIN(2, (x / (win.w * 0.33))));
401}
402
403void redraw(void)
404{
405 int t;
406
407 if (mode == MODE_IMAGE) {
408 img_render(&img);
409 if (img.ss.on) {
410 t = img.ss.delay * 100;
411 if (img.multi.cnt > 0 && img.multi.animate)
412 t = MAX(t, img.multi.length);
413 set_timeout(slideshow, t, false);
414 }
415 } else {
416 tns_render(&tns);
417 }
418 update_info();
419 win_draw(&win);
420 reset_timeout(redraw);
421 reset_cursor();
422}
423
424void reset_cursor(void)
425{
426 int c;
427 unsigned int i;
428 cursor_t cursor = CURSOR_NONE;
429
430 if (mode == MODE_IMAGE) {
431 for (i = 0; i < ARRLEN(timeouts); i++) {
432 if (timeouts[i].handler == reset_cursor) {
433 if (timeouts[i].active) {
434 c = ptr_third_x();
435 c = MAX(fileidx > 0 ? 0 : 1, c);
436 c = MIN(fileidx + 1 < filecnt ? 2 : 1, c);
437 cursor = imgcursor[c];
438 }
439 break;
440 }
441 }
442 } else {
443 if (tns.loadnext < tns.end || tns.initnext < filecnt)
444 cursor = CURSOR_WATCH;
445 else
446 cursor = CURSOR_ARROW;
447 }
448 win_set_cursor(&win, cursor);
449}
450
451void animate(void)
452{
453 if (img_frame_animate(&img)) {
454 redraw();
455 set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
456 }
457}
458
459void slideshow(void)
460{
461 load_image(fileidx + 1 < filecnt ? fileidx + 1 : 0);
462 redraw();
463}
464
465void clear_resize(void)
466{
467 resized = false;
468}
469
470Bool is_input_ev(Display *dpy, XEvent *ev, XPointer arg)
471{
472 return ev->type == ButtonPress || ev->type == KeyPress;
473}
474
475void handle_key_handler(bool init)
476{
477 extprefix = init;
478 if (win.bar.h == 0)
479 return;
480 if (init) {
481 close_info();
482 snprintf(win.bar.l.buf, win.bar.l.size, "Getting key handler input "
483 "(%s to abort)...", XKeysymToString(keyhandler_abort));
484 } else { /* abort */
485 open_info();
486 update_info();
487 }
488 win_draw(&win);
489}
490
491void run_key_handler(const char *key, unsigned int mask)
492{
493 pid_t pid;
494 FILE *pfs;
495 bool marked = mode == MODE_THUMB && markcnt > 0;
496 bool changed = false;
497 int f, i, pfd[2];
498 int fcnt = marked ? markcnt : 1;
499 char kstr[32];
500 struct stat *oldst, st;
501 XEvent dump;
502
503 if (keyhandler.f.err) {
504 if (!keyhandler.warned) {
505 error(0, keyhandler.f.err, "%s", keyhandler.f.cmd);
506 keyhandler.warned = true;
507 }
508 return;
509 }
510 if (key == NULL)
511 return;
512
513 if (pipe(pfd) < 0) {
514 error(0, errno, "pipe");
515 return;
516 }
517 if ((pfs = fdopen(pfd[1], "w")) == NULL) {
518 error(0, errno, "open pipe");
519 close(pfd[0]), close(pfd[1]);
520 return;
521 }
522 oldst = emalloc(fcnt * sizeof(*oldst));
523
524 close_info();
525 strncpy(win.bar.l.buf, "Running key handler...", win.bar.l.size);
526 win_draw(&win);
527 win_set_cursor(&win, CURSOR_WATCH);
528
529 snprintf(kstr, sizeof(kstr), "%s%s%s%s",
530 mask & ControlMask ? "C-" : "",
531 mask & Mod1Mask ? "M-" : "",
532 mask & ShiftMask ? "S-" : "", key);
533
534 if ((pid = fork()) == 0) {
535 close(pfd[1]);
536 dup2(pfd[0], 0);
537 execl(keyhandler.f.cmd, keyhandler.f.cmd, kstr, NULL);
538 error(EXIT_FAILURE, errno, "exec: %s", keyhandler.f.cmd);
539 }
540 close(pfd[0]);
541 if (pid < 0) {
542 error(0, errno, "fork");
543 fclose(pfs);
544 goto end;
545 }
546
547 for (f = i = 0; f < fcnt; i++) {
548 if ((marked && (files[i].flags & FF_MARK)) || (!marked && i == fileidx)) {
549 stat(files[i].path, &oldst[f]);
550 fprintf(pfs, "%s\n", files[i].name);
551 f++;
552 }
553 }
554 fclose(pfs);
555 while (waitpid(pid, NULL, 0) == -1 && errno == EINTR);
556
557 for (f = i = 0; f < fcnt; i++) {
558 if ((marked && (files[i].flags & FF_MARK)) || (!marked && i == fileidx)) {
559 if (stat(files[i].path, &st) != 0 ||
560 memcmp(&oldst[f].st_mtime, &st.st_mtime, sizeof(st.st_mtime)) != 0)
561 {
562 if (tns.thumbs != NULL) {
563 tns_unload(&tns, i);
564 tns.loadnext = MIN(tns.loadnext, i);
565 }
566 changed = true;
567 }
568 f++;
569 }
570 }
571 /* drop user input events that occurred while running the key handler */
572 while (XCheckIfEvent(win.env.dpy, &dump, is_input_ev, NULL));
573
574end:
575 if (mode == MODE_IMAGE) {
576 if (changed) {
577 img_close(&img, true);
578 load_image(fileidx);
579 } else {
580 open_info();
581 }
582 }
583 free(oldst);
584 reset_cursor();
585 redraw();
586}
587
588void on_keypress(XKeyEvent *kev)
589{
590 unsigned int i;
591 unsigned int sh = 0;
592 KeySym ksym, shksym;
593 char dummy, key;
594 bool dirty = false;
595
596 XLookupString(kev, &key, 1, &ksym, NULL);
597
598 if (kev->state & ShiftMask) {
599 kev->state &= ~ShiftMask;
600 XLookupString(kev, &dummy, 1, &shksym, NULL);
601 kev->state |= ShiftMask;
602 if (ksym != shksym)
603 sh = ShiftMask;
604 }
605 if (IsModifierKey(ksym))
606 return;
607 if (extprefix && ksym == keyhandler_abort && MODMASK(kev->state) == 0) {
608 handle_key_handler(false);
609 } else if (extprefix) {
610 run_key_handler(XKeysymToString(ksym), kev->state & ~sh);
611 extprefix = false;
612 } else if (key >= '0' && key <= '9') {
613 /* number prefix for commands */
614 prefix = prefix * 10 + (int) (key - '0');
615 return;
616 } else for (i = 0; i < ARRLEN(keys); i++) {
617 if (keys[i].ksym == ksym &&
618 MODMASK(keys[i].mask | sh) == MODMASK(kev->state) &&
619 keys[i].cmd.func &&
620 (keys[i].cmd.mode == MODE_ALL || keys[i].cmd.mode == mode))
621 {
622 if (keys[i].cmd.func(keys[i].arg))
623 dirty = true;
624 }
625 }
626 if (dirty)
627 redraw();
628 prefix = 0;
629}
630
631void on_buttonpress(XButtonEvent *bev)
632{
633 int sel;
634 unsigned int i;
635 bool dirty = false;
636 static Time firstclick;
637
638 if (mode == MODE_IMAGE) {
639 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
640 reset_cursor();
641
642 for (i = 0; i < ARRLEN(buttons); i++) {
643 if (buttons[i].button == bev->button &&
644 MODMASK(buttons[i].mask) == MODMASK(bev->state) &&
645 buttons[i].cmd.func &&
646 (buttons[i].cmd.mode == MODE_ALL || buttons[i].cmd.mode == mode))
647 {
648 if (buttons[i].cmd.func(buttons[i].arg))
649 dirty = true;
650 }
651 }
652 if (dirty)
653 redraw();
654 } else {
655 /* thumbnail mode (hard-coded) */
656 switch (bev->button) {
657 case Button1:
658 if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
659 if (sel != fileidx) {
660 tns_highlight(&tns, fileidx, false);
661 tns_highlight(&tns, sel, true);
662 fileidx = sel;
663 firstclick = bev->time;
664 redraw();
665 } else if (bev->time - firstclick <= TO_DOUBLE_CLICK) {
666 mode = MODE_IMAGE;
667 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
668 load_image(fileidx);
669 redraw();
670 } else {
671 firstclick = bev->time;
672 }
673 }
674 break;
675 case Button3:
676 if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
677 bool on = !(files[sel].flags & FF_MARK);
678 XEvent e;
679
680 while (true) {
681 if (sel >= 0 && mark_image(sel, on))
682 redraw();
683 XMaskEvent(win.env.dpy,
684 ButtonPressMask | ButtonReleaseMask | PointerMotionMask, &e);
685 if (e.type == ButtonPress || e.type == ButtonRelease)
686 break;
687 while (XCheckTypedEvent(win.env.dpy, MotionNotify, &e));
688 sel = tns_translate(&tns, e.xbutton.x, e.xbutton.y);
689 }
690 }
691 break;
692 case Button4:
693 case Button5:
694 if (tns_scroll(&tns, bev->button == Button4 ? DIR_UP : DIR_DOWN,
695 (bev->state & ControlMask) != 0))
696 redraw();
697 break;
698 }
699 }
700 prefix = 0;
701}
702
703void run(void)
704{
705 int xfd;
706 fd_set fds;
707 struct timeval timeout;
708 const struct timespec ten_ms = {0, 10000000};
709 bool discard, init_thumb, load_thumb, to_set;
710 XEvent ev, nextev;
711
712 while (true) {
713 to_set = check_timeouts(&timeout);
714 init_thumb = mode == MODE_THUMB && tns.initnext < filecnt;
715 load_thumb = mode == MODE_THUMB && tns.loadnext < tns.end;
716
717 if ((init_thumb || load_thumb || to_set || info.fd != -1 ||
718 arl.fd != -1) && XPending(win.env.dpy) == 0)
719 {
720 if (load_thumb) {
721 set_timeout(redraw, TO_REDRAW_THUMBS, false);
722 if (!tns_load(&tns, tns.loadnext, false, false)) {
723 remove_file(tns.loadnext, false);
724 tns.dirty = true;
725 }
726 if (tns.loadnext >= tns.end)
727 redraw();
728 } else if (init_thumb) {
729 set_timeout(redraw, TO_REDRAW_THUMBS, false);
730 if (!tns_load(&tns, tns.initnext, false, true))
731 remove_file(tns.initnext, false);
732 } else {
733 xfd = ConnectionNumber(win.env.dpy);
734 FD_ZERO(&fds);
735 FD_SET(xfd, &fds);
736 if (info.fd != -1) {
737 FD_SET(info.fd, &fds);
738 xfd = MAX(xfd, info.fd);
739 }
740 if (arl.fd != -1) {
741 FD_SET(arl.fd, &fds);
742 xfd = MAX(xfd, arl.fd);
743 }
744 select(xfd + 1, &fds, 0, 0, to_set ? &timeout : NULL);
745 if (info.fd != -1 && FD_ISSET(info.fd, &fds))
746 read_info();
747 if (arl.fd != -1 && FD_ISSET(arl.fd, &fds)) {
748 if (arl_handle(&arl)) {
749 /* when too fast, imlib2 can't load the image */
750 nanosleep(&ten_ms, NULL);
751 img_close(&img, true);
752 load_image(fileidx);
753 redraw();
754 }
755 }
756 }
757 continue;
758 }
759
760 do {
761 XNextEvent(win.env.dpy, &ev);
762 discard = false;
763 if (XEventsQueued(win.env.dpy, QueuedAlready) > 0) {
764 XPeekEvent(win.env.dpy, &nextev);
765 switch (ev.type) {
766 case ConfigureNotify:
767 case MotionNotify:
768 discard = ev.type == nextev.type;
769 break;
770 case KeyPress:
771 discard = (nextev.type == KeyPress || nextev.type == KeyRelease)
772 && ev.xkey.keycode == nextev.xkey.keycode;
773 break;
774 }
775 }
776 } while (discard);
777
778 switch (ev.type) {
779 /* handle events */
780 case ButtonPress:
781 on_buttonpress(&ev.xbutton);
782 break;
783 case ClientMessage:
784 if ((Atom) ev.xclient.data.l[0] == atoms[ATOM_WM_DELETE_WINDOW])
785 cg_quit();
786 break;
787 case DestroyNotify:
788 exit(EXIT_FAILURE);
789 break;
790 case ConfigureNotify:
791 if (win_configure(&win, &ev.xconfigure)) {
792 if (mode == MODE_IMAGE) {
793 img.dirty = true;
794 img.checkpan = true;
795 } else {
796 tns.dirty = true;
797 }
798 if (!resized) {
799 redraw();
800 set_timeout(clear_resize, TO_REDRAW_RESIZE, false);
801 resized = true;
802 } else {
803 set_timeout(redraw, TO_REDRAW_RESIZE, false);
804 }
805 }
806 break;
807 case KeyPress:
808 on_keypress(&ev.xkey);
809 break;
810 case MotionNotify:
811 if (mode == MODE_IMAGE) {
812 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
813 reset_cursor();
814 }
815 break;
816 }
817 }
818}
819
820int fncmp(const void *a, const void *b)
821{
822 return strcoll(((fileinfo_t*) a)->name, ((fileinfo_t*) b)->name);
823}
824
825void sigchld(int sig)
826{
827 while (waitpid(-1, NULL, WNOHANG) > 0);
828}
829
830void setup_signal(int sig, void (*handler)(int sig))
831{
832 struct sigaction sa;
833
834 sa.sa_handler = handler;
835 sigemptyset(&sa.sa_mask);
836 sa.sa_flags = SA_RESTART | SA_NOCLDSTOP;
837 if (sigaction(sig, &sa, 0) == -1)
838 error(EXIT_FAILURE, errno, "signal %d", sig);
839}
840
841int main(int argc, char *argv[])
842{
843 int i, start;
844 size_t n;
845 ssize_t len;
846 char *filename;
847 const char *homedir, *dsuffix = "";
848 struct stat fstats;
849 r_dir_t dir;
850
851 setup_signal(SIGCHLD, sigchld);
852 setup_signal(SIGPIPE, SIG_IGN);
853
854 setlocale(LC_COLLATE, "");
855
856 parse_options(argc, argv);
857
858 if (options->clean_cache) {
859 tns_init(&tns, NULL, NULL, NULL, NULL);
860 tns_clean_cache();
861 exit(EXIT_SUCCESS);
862 }
863
864 if (options->filecnt == 0 && !options->from_stdin) {
865 print_usage();
866 exit(EXIT_FAILURE);
867 }
868
869 if (options->recursive || options->from_stdin)
870 filecnt = 1024;
871 else
872 filecnt = options->filecnt;
873
874 files = emalloc(filecnt * sizeof(*files));
875 memset(files, 0, filecnt * sizeof(*files));
876 fileidx = 0;
877
878 if (options->from_stdin) {
879 n = 0;
880 filename = NULL;
881 while ((len = getline(&filename, &n, stdin)) > 0) {
882 if (filename[len-1] == '\n')
883 filename[len-1] = '\0';
884 check_add_file(filename, true);
885 }
886 free(filename);
887 }
888
889 for (i = 0; i < options->filecnt; i++) {
890 filename = options->filenames[i];
891
892 if (stat(filename, &fstats) < 0) {
893 error(0, errno, "%s", filename);
894 continue;
895 }
896 if (!S_ISDIR(fstats.st_mode)) {
897 check_add_file(filename, true);
898 } else {
899 if (r_opendir(&dir, filename, options->recursive) < 0) {
900 error(0, errno, "%s", filename);
901 continue;
902 }
903 start = fileidx;
904 while ((filename = r_readdir(&dir, true)) != NULL) {
905 check_add_file(filename, false);
906 free((void*) filename);
907 }
908 r_closedir(&dir);
909 if (fileidx - start > 1)
910 qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp);
911 }
912 }
913
914 if (fileidx == 0)
915 error(EXIT_FAILURE, 0, "No valid image file given, aborting");
916
917 filecnt = fileidx;
918 fileidx = options->startnum < filecnt ? options->startnum : 0;
919
920 win_init(&win);
921 img_init(&img, &win);
922 arl_init(&arl);
923
924 if ((homedir = getenv("XDG_CONFIG_HOME")) == NULL || homedir[0] == '\0') {
925 homedir = getenv("HOME");
926 dsuffix = "/.config";
927 }
928 if (homedir != NULL) {
929 extcmd_t *cmd[] = { &info.f, &keyhandler.f };
930 const char *name[] = { "image-info", "key-handler" };
931 const char *s = "/nsxiv/exec/";
932
933 for (i = 0; i < ARRLEN(cmd); i++) {
934 n = strlen(homedir) + strlen(dsuffix) + strlen(s) + strlen(name[i]) + 1;
935 cmd[i]->cmd = emalloc(n);
936 snprintf(cmd[i]->cmd, n, "%s%s%s%s", homedir, dsuffix, s, name[i]);
937 if (access(cmd[i]->cmd, X_OK) != 0)
938 cmd[i]->err = errno;
939 }
940 } else {
941 error(0, 0, "Exec directory not found");
942 }
943 info.fd = -1;
944
945 if (options->thumb_mode) {
946 mode = MODE_THUMB;
947 tns_init(&tns, files, &filecnt, &fileidx, &win);
948 while (!tns_load(&tns, fileidx, false, false))
949 remove_file(fileidx, false);
950 } else {
951 mode = MODE_IMAGE;
952 tns.thumbs = NULL;
953 load_image(fileidx);
954 }
955 win_open(&win);
956 win_set_title(&win, files[fileidx].path);
957 win_set_cursor(&win, CURSOR_WATCH);
958
959 atexit(cleanup);
960
961 set_timeout(redraw, 25, false);
962
963 run();
964
965 return 0;
966}