Krolyxon
Commit 7a74fae

Branches

Refs →

nsxivmain.c

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