Krolyxon
Commit 03eb664

Branches

Refs →

nsxivmain.c

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