Krolyxon
Commit 2d34c4b

Branches

Refs →

nsxivmain.c

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