Krolyxon
Commit 004fcd5

Branches

Refs →

nsxivmain.c

922 lines · 20.2 KB · C

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