Krolyxon
Commit 8db3191

Branches

Refs →

nsxivmain.c

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