Krolyxon
Commit 64b885b

Branches

Refs →

nsxivmain.c

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