Krolyxon
Commit a841d1c

Branches

Refs →

nsxivmain.c

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