Krolyxon
Commit 1449bfc

Branches

Refs →

nsxivmain.c

974 lines · 21.3 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#define _MAPPINGS_CONFIG
21#include "config.h"
22
23#include <stdlib.h>
24#include <string.h>
25#include <fcntl.h>
26#include <unistd.h>
27#include <errno.h>
28#include <locale.h>
29#include <signal.h>
30#include <sys/select.h>
31#include <sys/stat.h>
32#include <sys/wait.h>
33#include <time.h>
34#include <X11/keysym.h>
35#include <X11/XF86keysym.h>
36
37#define MODMASK(mask) ((mask) & ~ignore_mask)
38#define BAR_SEP " "
39
40typedef struct {
41 struct timeval when;
42 bool active;
43 timeout_f handler;
44} timeout_t;
45
46/* timeout handler functions: */
47void redraw(void);
48void reset_cursor(void);
49void animate(void);
50void slideshow(void);
51void clear_resize(void);
52
53appmode_t mode;
54arl_t arl;
55img_t img;
56tns_t tns;
57win_t win;
58
59fileinfo_t *files;
60int filecnt, fileidx;
61int alternate;
62int markcnt;
63int markidx;
64
65int prefix;
66bool extprefix;
67
68bool resized = false;
69
70typedef struct {
71 int err;
72 char *cmd;
73} extcmd_t;
74
75struct {
76 extcmd_t f;
77 int fd;
78 unsigned int i, lastsep;
79 pid_t pid;
80} info;
81
82struct {
83 extcmd_t f;
84 bool warned;
85} keyhandler;
86
87timeout_t timeouts[] = {
88 { { 0, 0 }, false, redraw },
89 { { 0, 0 }, false, reset_cursor },
90 { { 0, 0 }, false, animate },
91 { { 0, 0 }, false, slideshow },
92 { { 0, 0 }, false, clear_resize },
93};
94
95cursor_t imgcursor[3] = {
96 CURSOR_ARROW, CURSOR_ARROW, CURSOR_ARROW
97};
98
99/**************************
100 function implementations
101 **************************/
102void cleanup(void)
103{
104 img_close(&img, false);
105 arl_cleanup(&arl);
106 tns_free(&tns);
107 win_close(&win);
108}
109
110void check_add_file(char *filename, bool given)
111{
112 char *path;
113
114 if (*filename == '\0')
115 return;
116
117 if (access(filename, R_OK) < 0 ||
118 (path = realpath(filename, NULL)) == NULL)
119 {
120 if (given)
121 error(0, errno, "%s", filename);
122 return;
123 }
124
125 if (fileidx == filecnt) {
126 filecnt *= 2;
127 files = erealloc(files, filecnt * sizeof(*files));
128 memset(&files[filecnt/2], 0, filecnt/2 * sizeof(*files));
129 }
130
131 files[fileidx].name = estrdup(filename);
132 files[fileidx].path = path;
133 if (given)
134 files[fileidx].flags |= FF_WARN;
135 fileidx++;
136}
137
138void remove_file(int n, bool manual)
139{
140 if (n < 0 || n >= filecnt)
141 return;
142
143 if (filecnt == 1) {
144 if (!manual)
145 fprintf(stderr, "nsxiv: no more files to display, aborting\n");
146 exit(manual ? EXIT_SUCCESS : EXIT_FAILURE);
147 }
148 if (files[n].flags & FF_MARK)
149 markcnt--;
150
151 if (files[n].path != files[n].name)
152 free((void*) files[n].path);
153 free((void*) files[n].name);
154
155 if (n + 1 < filecnt) {
156 if (tns.thumbs != NULL) {
157 memmove(tns.thumbs + n, tns.thumbs + n + 1, (filecnt - n - 1) *
158 sizeof(*tns.thumbs));
159 memset(tns.thumbs + filecnt - 1, 0, sizeof(*tns.thumbs));
160 }
161 memmove(files + n, files + n + 1, (filecnt - n - 1) * sizeof(*files));
162 }
163 filecnt--;
164 if (fileidx > n || fileidx == filecnt)
165 fileidx--;
166 if (alternate > n || alternate == filecnt)
167 alternate--;
168 if (markidx > n || markidx == filecnt)
169 markidx--;
170}
171
172void set_timeout(timeout_f handler, int time, bool overwrite)
173{
174 int i;
175
176 for (i = 0; i < ARRLEN(timeouts); i++) {
177 if (timeouts[i].handler == handler) {
178 if (!timeouts[i].active || overwrite) {
179 gettimeofday(&timeouts[i].when, 0);
180 TV_ADD_MSEC(&timeouts[i].when, time);
181 timeouts[i].active = true;
182 }
183 return;
184 }
185 }
186}
187
188void reset_timeout(timeout_f handler)
189{
190 int i;
191
192 for (i = 0; i < ARRLEN(timeouts); i++) {
193 if (timeouts[i].handler == handler) {
194 timeouts[i].active = false;
195 return;
196 }
197 }
198}
199
200bool check_timeouts(struct timeval *t)
201{
202 int i = 0, tdiff, tmin = -1;
203 struct timeval now;
204
205 while (i < ARRLEN(timeouts)) {
206 if (timeouts[i].active) {
207 gettimeofday(&now, 0);
208 tdiff = TV_DIFF(&timeouts[i].when, &now);
209 if (tdiff <= 0) {
210 timeouts[i].active = false;
211 if (timeouts[i].handler != NULL)
212 timeouts[i].handler();
213 i = tmin = -1;
214 } else if (tmin < 0 || tdiff < tmin) {
215 tmin = tdiff;
216 }
217 }
218 i++;
219 }
220 if (tmin > 0 && t != NULL)
221 TV_SET_MSEC(t, tmin);
222 return tmin > 0;
223}
224
225void close_info(void)
226{
227 if (info.fd != -1) {
228 kill(info.pid, SIGTERM);
229 close(info.fd);
230 info.fd = -1;
231 }
232}
233
234void open_info(void)
235{
236 int pfd[2];
237 char w[12], h[12];
238
239 if (info.f.err != 0 || info.fd >= 0 || win.bar.h == 0)
240 return;
241 win.bar.l.buf[0] = '\0';
242 if (pipe(pfd) < 0)
243 return;
244 if ((info.pid = fork()) == 0) {
245 close(pfd[0]);
246 dup2(pfd[1], 1);
247 snprintf(w, sizeof(w), "%d", img.w);
248 snprintf(h, sizeof(h), "%d", img.h);
249 execl(info.f.cmd, info.f.cmd, files[fileidx].name, w, h, NULL);
250 error(EXIT_FAILURE, errno, "exec: %s", info.f.cmd);
251 }
252 close(pfd[1]);
253 if (info.pid < 0) {
254 close(pfd[0]);
255 } else {
256 fcntl(pfd[0], F_SETFL, O_NONBLOCK);
257 info.fd = pfd[0];
258 info.i = info.lastsep = 0;
259 }
260}
261
262void read_info(void)
263{
264 ssize_t i, n;
265 char buf[BAR_L_LEN];
266
267 while (true) {
268 n = read(info.fd, buf, sizeof(buf));
269 if (n < 0 && errno == EAGAIN)
270 return;
271 else if (n == 0)
272 goto end;
273 for (i = 0; i < n; i++) {
274 if (buf[i] == '\n') {
275 if (info.lastsep == 0) {
276 win.bar.l.buf[info.i++] = ' ';
277 info.lastsep = 1;
278 }
279 } else {
280 win.bar.l.buf[info.i++] = buf[i];
281 info.lastsep = 0;
282 }
283 if (info.i + 1 == win.bar.l.size)
284 goto end;
285 }
286 }
287end:
288 info.i -= info.lastsep;
289 win.bar.l.buf[info.i] = '\0';
290 win_draw(&win);
291 close_info();
292}
293
294void load_image(int new)
295{
296 bool prev = new < fileidx;
297 static int current;
298
299 if (new < 0 || new >= filecnt)
300 return;
301
302 if (win.xwin != None)
303 win_set_cursor(&win, CURSOR_WATCH);
304 reset_timeout(slideshow);
305
306 if (new != current)
307 alternate = current;
308
309 img_close(&img, false);
310 while (!img_load(&img, &files[new])) {
311 remove_file(new, false);
312 if (new >= filecnt)
313 new = filecnt - 1;
314 else if (new > 0 && prev)
315 new--;
316 }
317 files[new].flags &= ~FF_WARN;
318 fileidx = current = new;
319
320 close_info();
321 open_info();
322 arl_setup(&arl, files[fileidx].path);
323 win_set_title(&win, files[fileidx].path);
324
325 if (img.multi.cnt > 0 && img.multi.animate)
326 set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
327 else
328 reset_timeout(animate);
329}
330
331bool mark_image(int n, bool on)
332{
333 markidx = n;
334 if (!!(files[n].flags & FF_MARK) != on) {
335 files[n].flags ^= FF_MARK;
336 markcnt += on ? 1 : -1;
337 if (mode == MODE_THUMB)
338 tns_mark(&tns, n, on);
339 return true;
340 }
341 return false;
342}
343
344void bar_put(win_bar_t *bar, const char *fmt, ...)
345{
346 size_t len = bar->size - (bar->p - bar->buf), n;
347 va_list ap;
348
349 va_start(ap, fmt);
350 n = vsnprintf(bar->p, len, fmt, ap);
351 bar->p += MIN(len, n);
352 va_end(ap);
353}
354
355void update_info(void)
356{
357 unsigned int i, fn, fw;
358 const char * mark;
359 win_bar_t *l = &win.bar.l, *r = &win.bar.r;
360
361 /* update bar contents */
362 if (win.bar.h == 0)
363 return;
364 for (fw = 0, i = filecnt; i > 0; fw++, i /= 10);
365 mark = files[fileidx].flags & FF_MARK ? "* " : "";
366 l->p = l->buf;
367 r->p = r->buf;
368 if (mode == MODE_THUMB) {
369 if (tns.loadnext < tns.end)
370 bar_put(l, "Loading... %0*d", fw, tns.loadnext + 1);
371 else if (tns.initnext < filecnt)
372 bar_put(l, "Caching... %0*d", fw, tns.initnext + 1);
373 else
374 strncpy(l->buf, files[fileidx].name, l->size);
375 bar_put(r, "%s%0*d/%d", mark, fw, fileidx + 1, filecnt);
376 } else {
377 bar_put(r, "%s", mark);
378 if (img.ss.on) {
379 if (img.ss.delay % 10 != 0)
380 bar_put(r, "%2.1fs" BAR_SEP, (float)img.ss.delay / 10);
381 else
382 bar_put(r, "%ds" BAR_SEP, img.ss.delay / 10);
383 }
384 if (img.gamma != 0)
385 bar_put(r, "G%+d" BAR_SEP, img.gamma);
386 bar_put(r, "%3d%%" BAR_SEP, (int) (img.zoom * 100.0));
387 if (img.multi.cnt > 0) {
388 for (fn = 0, i = img.multi.cnt; i > 0; fn++, i /= 10);
389 bar_put(r, "%0*d/%d" BAR_SEP, fn, img.multi.sel + 1, img.multi.cnt);
390 }
391 bar_put(r, "%0*d/%d", fw, fileidx + 1, filecnt);
392 if (info.f.err)
393 strncpy(l->buf, files[fileidx].name, l->size);
394 }
395}
396
397int ptr_third_x(void)
398{
399 int x, y;
400
401 win_cursor_pos(&win, &x, &y);
402 return MAX(0, MIN(2, (x / (win.w * 0.33))));
403}
404
405void redraw(void)
406{
407 int t;
408
409 if (mode == MODE_IMAGE) {
410 img_render(&img);
411 if (img.ss.on) {
412 t = img.ss.delay * 100;
413 if (img.multi.cnt > 0 && img.multi.animate)
414 t = MAX(t, img.multi.length);
415 set_timeout(slideshow, t, false);
416 }
417 } else {
418 tns_render(&tns);
419 }
420 update_info();
421 win_draw(&win);
422 reset_timeout(redraw);
423 reset_cursor();
424}
425
426void reset_cursor(void)
427{
428 int c, i;
429 cursor_t cursor = CURSOR_NONE;
430
431 if (mode == MODE_IMAGE) {
432 for (i = 0; i < ARRLEN(timeouts); i++) {
433 if (timeouts[i].handler == reset_cursor) {
434 if (timeouts[i].active) {
435 c = ptr_third_x();
436 c = MAX(fileidx > 0 ? 0 : 1, c);
437 c = MIN(fileidx + 1 < filecnt ? 2 : 1, c);
438 cursor = imgcursor[c];
439 }
440 break;
441 }
442 }
443 } else {
444 if (tns.loadnext < tns.end || tns.initnext < filecnt)
445 cursor = CURSOR_WATCH;
446 else
447 cursor = CURSOR_ARROW;
448 }
449 win_set_cursor(&win, cursor);
450}
451
452void animate(void)
453{
454 if (img_frame_animate(&img)) {
455 redraw();
456 set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
457 }
458}
459
460void slideshow(void)
461{
462 load_image(fileidx + 1 < filecnt ? fileidx + 1 : 0);
463 redraw();
464}
465
466void clear_resize(void)
467{
468 resized = false;
469}
470
471Bool is_input_ev(Display *dpy, XEvent *ev, XPointer arg)
472{
473 return ev->type == ButtonPress || ev->type == KeyPress;
474}
475
476void handle_key_handler(bool init)
477{
478 extprefix = init;
479 if (win.bar.h == 0)
480 return;
481 if (init) {
482 close_info();
483 snprintf(win.bar.l.buf, win.bar.l.size, "Getting key handler input "
484 "(%s to abort)...", XKeysymToString(keyhandler_abort));
485 } else { /* abort */
486 open_info();
487 update_info();
488 }
489 win_draw(&win);
490}
491
492void run_key_handler(const char *key, unsigned int mask)
493{
494 pid_t pid;
495 FILE *pfs;
496 bool marked = mode == MODE_THUMB && markcnt > 0;
497 bool changed = false;
498 int f, i, pfd[2];
499 int fcnt = marked ? markcnt : 1;
500 char kstr[32];
501 struct stat *oldst, st;
502 XEvent dump;
503
504 if (keyhandler.f.err != 0) {
505 if (!keyhandler.warned) {
506 error(0, keyhandler.f.err, "%s", keyhandler.f.cmd);
507 keyhandler.warned = true;
508 }
509 return;
510 }
511 if (key == NULL)
512 return;
513
514 if (pipe(pfd) < 0) {
515 error(0, errno, "pipe");
516 return;
517 }
518 if ((pfs = fdopen(pfd[1], "w")) == NULL) {
519 error(0, errno, "open pipe");
520 close(pfd[0]), close(pfd[1]);
521 return;
522 }
523 oldst = emalloc(fcnt * sizeof(*oldst));
524
525 close_info();
526 strncpy(win.bar.l.buf, "Running key handler...", win.bar.l.size);
527 win_draw(&win);
528 win_set_cursor(&win, CURSOR_WATCH);
529
530 snprintf(kstr, sizeof(kstr), "%s%s%s%s",
531 mask & ControlMask ? "C-" : "",
532 mask & Mod1Mask ? "M-" : "",
533 mask & ShiftMask ? "S-" : "", key);
534
535 if ((pid = fork()) == 0) {
536 close(pfd[1]);
537 dup2(pfd[0], 0);
538 execl(keyhandler.f.cmd, keyhandler.f.cmd, kstr, NULL);
539 error(EXIT_FAILURE, errno, "exec: %s", keyhandler.f.cmd);
540 }
541 close(pfd[0]);
542 if (pid < 0) {
543 error(0, errno, "fork");
544 fclose(pfs);
545 goto end;
546 }
547
548 for (f = i = 0; f < fcnt; i++) {
549 if ((marked && (files[i].flags & FF_MARK)) || (!marked && i == fileidx)) {
550 stat(files[i].path, &oldst[f]);
551 fprintf(pfs, "%s\n", files[i].name);
552 f++;
553 }
554 }
555 fclose(pfs);
556 while (waitpid(pid, NULL, 0) == -1 && errno == EINTR);
557
558 for (f = i = 0; f < fcnt; i++) {
559 if ((marked && (files[i].flags & FF_MARK)) || (!marked && i == fileidx)) {
560 if (stat(files[i].path, &st) != 0 ||
561 memcmp(&oldst[f].st_mtime, &st.st_mtime, sizeof(st.st_mtime)) != 0)
562 {
563 if (tns.thumbs != NULL) {
564 tns_unload(&tns, i);
565 tns.loadnext = MIN(tns.loadnext, i);
566 }
567 changed = true;
568 }
569 f++;
570 }
571 }
572 /* drop user input events that occurred while running the key handler */
573 while (XCheckIfEvent(win.env.dpy, &dump, is_input_ev, NULL));
574
575end:
576 if (mode == MODE_IMAGE) {
577 if (changed) {
578 img_close(&img, true);
579 load_image(fileidx);
580 } else {
581 open_info();
582 }
583 }
584 free(oldst);
585 reset_cursor();
586 redraw();
587}
588
589void on_keypress(XKeyEvent *kev)
590{
591 int i;
592 unsigned int sh = 0;
593 KeySym ksym, shksym;
594 char dummy, key;
595 bool dirty = false;
596
597 XLookupString(kev, &key, 1, &ksym, NULL);
598
599 if (kev->state & ShiftMask) {
600 kev->state &= ~ShiftMask;
601 XLookupString(kev, &dummy, 1, &shksym, NULL);
602 kev->state |= ShiftMask;
603 if (ksym != shksym)
604 sh = ShiftMask;
605 }
606 if (IsModifierKey(ksym))
607 return;
608 if (extprefix && ksym == keyhandler_abort && MODMASK(kev->state) == 0) {
609 handle_key_handler(false);
610 } else if (extprefix) {
611 run_key_handler(XKeysymToString(ksym), kev->state & ~sh);
612 extprefix = false;
613 } else if (key >= '0' && key <= '9') {
614 /* number prefix for commands */
615 prefix = prefix * 10 + (int) (key - '0');
616 return;
617 } else for (i = 0; i < ARRLEN(keys); i++) {
618 if (keys[i].ksym == ksym &&
619 MODMASK(keys[i].mask | sh) == MODMASK(kev->state) &&
620 keys[i].cmd >= 0 && keys[i].cmd < CMD_COUNT &&
621 (cmds[keys[i].cmd].mode < 0 || cmds[keys[i].cmd].mode == mode))
622 {
623 if (cmds[keys[i].cmd].func(keys[i].arg))
624 dirty = true;
625 }
626 }
627 if (dirty)
628 redraw();
629 prefix = 0;
630}
631
632void on_buttonpress(XButtonEvent *bev)
633{
634 int i, sel;
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 >= 0 && buttons[i].cmd < CMD_COUNT &&
646 (cmds[buttons[i].cmd].mode < 0 || cmds[buttons[i].cmd].mode == mode))
647 {
648 if (cmds[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 cmds[g_quit].func(0);
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(&tns);
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 for (i = 0; i < ARRLEN(buttons); i++) {
921 if (buttons[i].cmd == i_cursor_navigate) {
922 imgcursor[0] = CURSOR_LEFT;
923 imgcursor[2] = CURSOR_RIGHT;
924 break;
925 }
926 }
927
928 win_init(&win);
929 img_init(&img, &win);
930 arl_init(&arl);
931
932 if ((homedir = getenv("XDG_CONFIG_HOME")) == NULL || homedir[0] == '\0') {
933 homedir = getenv("HOME");
934 dsuffix = "/.config";
935 }
936 if (homedir != NULL) {
937 extcmd_t *cmd[] = { &info.f, &keyhandler.f };
938 const char *name[] = { "image-info", "key-handler" };
939 const char *s = "/nsxiv/exec/";
940
941 for (i = 0; i < ARRLEN(cmd); i++) {
942 n = strlen(homedir) + strlen(dsuffix) + strlen(name[i]) + strlen(s) + 1;
943 cmd[i]->cmd = (char*) emalloc(n);
944 snprintf(cmd[i]->cmd, n, "%s%s%s%s", homedir, dsuffix, s, name[i]);
945 if (access(cmd[i]->cmd, X_OK) != 0)
946 cmd[i]->err = errno;
947 }
948 } else {
949 error(0, 0, "Exec directory not found");
950 }
951 info.fd = -1;
952
953 if (options->thumb_mode) {
954 mode = MODE_THUMB;
955 tns_init(&tns, files, &filecnt, &fileidx, &win);
956 while (!tns_load(&tns, fileidx, false, false))
957 remove_file(fileidx, false);
958 } else {
959 mode = MODE_IMAGE;
960 tns.thumbs = NULL;
961 load_image(fileidx);
962 }
963 win_open(&win);
964 win_set_title(&win, files[fileidx].path);
965 win_set_cursor(&win, CURSOR_WATCH);
966
967 atexit(cleanup);
968
969 set_timeout(redraw, 25, false);
970
971 run();
972
973 return 0;
974}