Krolyxon
Commit 5c6947c

Branches

Refs →

nsxivmain.c

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