Krolyxon
Commit bbebd45

Branches

Refs →

nsxivmain.c

963 lines · 21.0 KB · C

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