Krolyxon
Commit 79bcc2b

Branches

Refs →

nsxivmain.c

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