Krolyxon
Commit 5621975

Branches

Refs →

nsxivmain.c

728 lines · 15.1 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
35#include "types.h"
36#include "commands.h"
37#include "image.h"
38#include "options.h"
39#include "thumbs.h"
40#include "util.h"
41#include "window.h"
42#include "config.h"
43
44enum {
45 FILENAME_CNT = 1024,
46 TITLE_LEN = 256
47};
48
49typedef struct {
50 struct timeval when;
51 bool active;
52 timeout_f handler;
53} timeout_t;
54
55/* timeout handler functions: */
56void redraw(void);
57void reset_cursor(void);
58void animate(void);
59void clear_resize(void);
60
61appmode_t mode;
62img_t img;
63tns_t tns;
64win_t win;
65
66fileinfo_t *files;
67int filecnt, fileidx;
68int markcnt;
69int alternate;
70
71int prefix;
72
73bool resized = false;
74
75const char * const INFO_SCRIPT = ".sxiv/exec/image-info";
76struct {
77 char *script;
78 int fd;
79 unsigned int i, lastsep;
80 bool open;
81} info;
82
83timeout_t timeouts[] = {
84 { { 0, 0 }, false, redraw },
85 { { 0, 0 }, false, reset_cursor },
86 { { 0, 0 }, false, animate },
87 { { 0, 0 }, false, clear_resize },
88};
89
90void cleanup(void)
91{
92 static bool in = false;
93
94 if (!in) {
95 in = true;
96 img_close(&img, false);
97 tns_free(&tns);
98 win_close(&win);
99 }
100}
101
102void check_add_file(char *filename)
103{
104 const char *bn;
105
106 if (filename == NULL || *filename == '\0')
107 return;
108
109 if (access(filename, R_OK) < 0) {
110 warn("could not open file: %s", filename);
111 return;
112 }
113
114 if (fileidx == filecnt) {
115 filecnt *= 2;
116 files = (fileinfo_t*) s_realloc(files, filecnt * sizeof(fileinfo_t));
117 }
118 if (*filename != '/') {
119 files[fileidx].path = absolute_path(filename);
120 if (files[fileidx].path == NULL) {
121 warn("could not get absolute path of file: %s\n", filename);
122 return;
123 }
124 }
125 files[fileidx].loaded = false;
126 files[fileidx].name = s_strdup(filename);
127 if (*filename == '/')
128 files[fileidx].path = files[fileidx].name;
129 if ((bn = strrchr(files[fileidx].name , '/')) != NULL && bn[1] != '\0')
130 files[fileidx].base = ++bn;
131 else
132 files[fileidx].base = files[fileidx].name;
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 cleanup();
145 exit(manual ? EXIT_SUCCESS : EXIT_FAILURE);
146 }
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 memmove(files + n, files + n + 1, (filecnt - n - 1) * sizeof(fileinfo_t));
154 if (n + 1 < tns.cnt) {
155 memmove(tns.thumbs + n, tns.thumbs + n + 1, (tns.cnt - n - 1) *
156 sizeof(thumb_t));
157 memset(tns.thumbs + tns.cnt - 1, 0, sizeof(thumb_t));
158 }
159
160 filecnt--;
161 if (n < tns.cnt)
162 tns.cnt--;
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
225 if (info.script == NULL || info.open || win.bar.h == 0)
226 return;
227 if (info.fd != -1) {
228 close(info.fd);
229 kill(pid, SIGTERM);
230 info.fd = -1;
231 }
232 win.bar.l[0] = '\0';
233
234 if (pipe(pfd) < 0)
235 return;
236 pid = fork();
237 if (pid > 0) {
238 close(pfd[1]);
239 fcntl(pfd[0], F_SETFL, O_NONBLOCK);
240 info.fd = pfd[0];
241 info.i = info.lastsep = 0;
242 info.open = true;
243 } else if (pid == 0) {
244 close(pfd[0]);
245 dup2(pfd[1], 1);
246 execl(info.script, info.script, files[fileidx].name, NULL);
247 warn("could not exec: %s", info.script);
248 exit(EXIT_FAILURE);
249 }
250}
251
252void read_info(void)
253{
254 ssize_t i, n;
255 char buf[BAR_L_LEN];
256
257 while (true) {
258 n = read(info.fd, buf, sizeof(buf));
259 if (n < 0 && errno == EAGAIN)
260 return;
261 else if (n == 0)
262 goto end;
263 for (i = 0; i < n; i++) {
264 if (buf[i] == '\n') {
265 if (info.lastsep == 0) {
266 win.bar.l[info.i++] = ' ';
267 info.lastsep = 1;
268 }
269 } else {
270 win.bar.l[info.i++] = buf[i];
271 info.lastsep = 0;
272 }
273 if (info.i + 1 == sizeof(win.bar.l))
274 goto end;
275 }
276 }
277end:
278 info.i -= info.lastsep;
279 win.bar.l[info.i] = '\0';
280 win_update_bar(&win);
281 info.fd = -1;
282 while (waitpid(-1, NULL, WNOHANG) > 0);
283}
284
285void load_image(int new)
286{
287 if (new < 0 || new >= filecnt)
288 return;
289
290 win_set_cursor(&win, CURSOR_WATCH);
291
292 if (new != fileidx)
293 alternate = fileidx;
294
295 img_close(&img, false);
296 while (!img_load(&img, &files[new])) {
297 remove_file(new, false);
298 if (new >= filecnt)
299 new = filecnt - 1;
300 else if (new < fileidx)
301 new--;
302 }
303 files[new].loaded = true;
304 fileidx = new;
305
306 info.open = false;
307 open_info();
308
309 if (img.multi.cnt > 0 && img.multi.animate)
310 set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
311 else
312 reset_timeout(animate);
313}
314
315void update_info(void)
316{
317 int sel;
318 unsigned int i, fn, fw, n;
319 unsigned int llen = sizeof(win.bar.l), rlen = sizeof(win.bar.r);
320 char *lt = win.bar.l, *rt = win.bar.r, title[TITLE_LEN];
321 const char * mark;
322 bool ow_info;
323
324 for (fw = 0, i = filecnt; i > 0; fw++, i /= 10);
325 sel = mode == MODE_IMAGE ? fileidx : tns.sel;
326
327 /* update window title */
328 if (mode == MODE_THUMB) {
329 win_set_title(&win, "sxiv");
330 } else {
331 snprintf(title, sizeof(title), "sxiv - %s", files[sel].name);
332 win_set_title(&win, title);
333 }
334
335 /* update bar contents */
336 if (win.bar.h == 0)
337 return;
338 mark = files[sel].marked ? "* " : "";
339 if (mode == MODE_THUMB) {
340 if (tns.cnt == filecnt) {
341 n = snprintf(rt, rlen, "%s%0*d/%d", mark, fw, sel + 1, filecnt);
342 ow_info = true;
343 } else {
344 snprintf(lt, llen, "Loading... %0*d/%d", fw, tns.cnt, filecnt);
345 rt[0] = '\0';
346 ow_info = false;
347 }
348 } else {
349 n = snprintf(rt, rlen, "%s%3d%% | ", mark, (int) (img.zoom * 100.0));
350 if (img.multi.cnt > 0) {
351 for (fn = 0, i = img.multi.cnt; i > 0; fn++, i /= 10);
352 n += snprintf(rt + n, rlen - n, "%0*d/%d | ",
353 fn, img.multi.sel + 1, img.multi.cnt);
354 }
355 n += snprintf(rt + n, rlen - n, "%0*d/%d", fw, sel + 1, filecnt);
356 ow_info = info.script == NULL;
357 }
358 if (ow_info) {
359 fn = strlen(files[sel].name);
360 if (fn < llen &&
361 win_textwidth(files[sel].name, fn, true) +
362 win_textwidth(rt, n, true) < win.w)
363 {
364 strncpy(lt, files[sel].name, llen);
365 } else {
366 strncpy(lt, files[sel].base, llen);
367 }
368 }
369}
370
371void redraw(void)
372{
373 if (mode == MODE_IMAGE)
374 img_render(&img);
375 else
376 tns_render(&tns);
377 update_info();
378 win_draw(&win);
379 reset_timeout(redraw);
380 reset_cursor();
381}
382
383void reset_cursor(void)
384{
385 int i;
386 cursor_t cursor = CURSOR_NONE;
387
388 if (mode == MODE_IMAGE) {
389 for (i = 0; i < ARRLEN(timeouts); i++) {
390 if (timeouts[i].handler == reset_cursor) {
391 if (timeouts[i].active)
392 cursor = CURSOR_ARROW;
393 break;
394 }
395 }
396 } else {
397 if (tns.cnt != filecnt)
398 cursor = CURSOR_WATCH;
399 else
400 cursor = CURSOR_ARROW;
401 }
402 win_set_cursor(&win, cursor);
403}
404
405void animate(void)
406{
407 if (img_frame_animate(&img, false)) {
408 redraw();
409 set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
410 }
411}
412
413void clear_resize(void)
414{
415 resized = false;
416}
417
418bool keymask(const keymap_t *k, unsigned int state)
419{
420 return (k->ctrl ? ControlMask : 0) == (state & ControlMask);
421}
422
423bool buttonmask(const button_t *b, unsigned int state)
424{
425 return ((b->ctrl ? ControlMask : 0) | (b->shift ? ShiftMask : 0)) ==
426 (state & (ControlMask | ShiftMask));
427}
428
429void on_keypress(XKeyEvent *kev)
430{
431 int i;
432 KeySym ksym;
433 char key;
434
435 if (kev == NULL)
436 return;
437
438 XLookupString(kev, &key, 1, &ksym, NULL);
439
440 if ((ksym == XK_Escape || (key >= '0' && key <= '9')) &&
441 (kev->state & ControlMask) == 0)
442 {
443 /* number prefix for commands */
444 prefix = ksym == XK_Escape ? 0 : prefix * 10 + (int) (key - '0');
445 return;
446 }
447
448 for (i = 0; i < ARRLEN(keys); i++) {
449 if (keys[i].ksym == ksym && keymask(&keys[i], kev->state)) {
450 if (keys[i].cmd != NULL && keys[i].cmd(keys[i].arg))
451 redraw();
452 prefix = 0;
453 return;
454 }
455 }
456}
457
458void on_buttonpress(XButtonEvent *bev)
459{
460 int i, sel;
461
462 if (bev == NULL)
463 return;
464
465 if (mode == MODE_IMAGE) {
466 win_set_cursor(&win, CURSOR_ARROW);
467 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
468
469 for (i = 0; i < ARRLEN(buttons); i++) {
470 if (buttons[i].button == bev->button &&
471 buttonmask(&buttons[i], bev->state))
472 {
473 if (buttons[i].cmd != NULL && buttons[i].cmd(buttons[i].arg))
474 redraw();
475 return;
476 }
477 }
478 } else {
479 /* thumbnail mode (hard-coded) */
480 switch (bev->button) {
481 case Button1:
482 if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
483 if (sel == tns.sel) {
484 mode = MODE_IMAGE;
485 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
486 load_image(tns.sel);
487 } else {
488 tns_highlight(&tns, tns.sel, false);
489 tns_highlight(&tns, sel, true);
490 tns.sel = sel;
491 }
492 redraw();
493 break;
494 }
495 break;
496 case Button4:
497 case Button5:
498 if (tns_scroll(&tns, bev->button == Button4 ? DIR_UP : DIR_DOWN,
499 (bev->state & ControlMask) != 0))
500 redraw();
501 break;
502 }
503 }
504}
505
506void run(void)
507{
508 int xfd;
509 fd_set fds;
510 struct timeval timeout;
511 bool discard, to_set;
512 XEvent ev, nextev;
513
514 redraw();
515
516 while (true) {
517 while (mode == MODE_THUMB && tns.cnt < filecnt &&
518 XPending(win.env.dpy) == 0)
519 {
520 /* load thumbnails */
521 set_timeout(redraw, TO_REDRAW_THUMBS, false);
522 if (tns_load(&tns, tns.cnt, &files[tns.cnt], false, false)) {
523 tns.cnt++;
524 } else {
525 remove_file(tns.cnt, false);
526 if (tns.sel >= tns.cnt)
527 tns.sel--;
528 }
529 if (tns.cnt == filecnt)
530 redraw();
531 else
532 check_timeouts(NULL);
533 }
534
535 while (XPending(win.env.dpy) == 0
536 && ((to_set = check_timeouts(&timeout)) || info.fd != -1))
537 {
538 /* check for timeouts & input */
539 xfd = ConnectionNumber(win.env.dpy);
540 FD_ZERO(&fds);
541 FD_SET(xfd, &fds);
542 if (info.fd != -1) {
543 FD_SET(info.fd, &fds);
544 xfd = MAX(xfd, info.fd);
545 }
546 select(xfd + 1, &fds, 0, 0, to_set ? &timeout : NULL);
547 if (info.fd != -1 && FD_ISSET(info.fd, &fds))
548 read_info();
549 }
550
551 do {
552 XNextEvent(win.env.dpy, &ev);
553 discard = false;
554 if (XEventsQueued(win.env.dpy, QueuedAlready) > 0) {
555 XPeekEvent(win.env.dpy, &nextev);
556 switch (ev.type) {
557 case ConfigureNotify:
558 discard = ev.type == nextev.type;
559 break;
560 case KeyPress:
561 discard = (nextev.type == KeyPress || nextev.type == KeyRelease)
562 && ev.xkey.keycode == nextev.xkey.keycode;
563 break;
564 }
565 }
566 } while (discard);
567
568 switch (ev.type) {
569 /* handle events */
570 case ButtonPress:
571 on_buttonpress(&ev.xbutton);
572 break;
573 case ClientMessage:
574 if ((Atom) ev.xclient.data.l[0] == wm_delete_win)
575 return;
576 break;
577 case ConfigureNotify:
578 if (win_configure(&win, &ev.xconfigure)) {
579 if (mode == MODE_IMAGE) {
580 img.dirty = true;
581 img.checkpan = true;
582 } else {
583 tns.dirty = true;
584 }
585 if (!resized || win.fullscreen) {
586 redraw();
587 set_timeout(clear_resize, TO_REDRAW_RESIZE, false);
588 resized = true;
589 } else {
590 set_timeout(redraw, TO_REDRAW_RESIZE, false);
591 }
592 }
593 break;
594 case Expose:
595 win_expose(&win, &ev.xexpose);
596 break;
597 case KeyPress:
598 on_keypress(&ev.xkey);
599 break;
600 case MotionNotify:
601 if (mode == MODE_IMAGE) {
602 win_set_cursor(&win, CURSOR_ARROW);
603 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
604 }
605 break;
606 }
607 }
608}
609
610int fncmp(const void *a, const void *b)
611{
612 return strcoll(((fileinfo_t*) a)->name, ((fileinfo_t*) b)->name);
613}
614
615int main(int argc, char **argv)
616{
617 int i, start;
618 size_t n;
619 ssize_t len;
620 char *filename;
621 const char *homedir;
622 struct stat fstats;
623 r_dir_t dir;
624
625 parse_options(argc, argv);
626
627 if (options->clean_cache) {
628 tns_init(&tns, 0, NULL);
629 tns_clean_cache(&tns);
630 exit(EXIT_SUCCESS);
631 }
632
633 if (options->filecnt == 0 && !options->from_stdin) {
634 print_usage();
635 exit(EXIT_FAILURE);
636 }
637
638 if (options->recursive || options->from_stdin)
639 filecnt = FILENAME_CNT;
640 else
641 filecnt = options->filecnt;
642
643 files = (fileinfo_t*) s_malloc(filecnt * sizeof(fileinfo_t));
644 fileidx = 0;
645
646 if (options->from_stdin) {
647 filename = NULL;
648 while ((len = get_line(&filename, &n, stdin)) > 0) {
649 if (filename[len-1] == '\n')
650 filename[len-1] = '\0';
651 check_add_file(filename);
652 }
653 if (filename != NULL)
654 free(filename);
655 }
656
657 for (i = 0; i < options->filecnt; i++) {
658 filename = options->filenames[i];
659
660 if (stat(filename, &fstats) < 0) {
661 warn("could not stat file: %s", filename);
662 continue;
663 }
664 if (!S_ISDIR(fstats.st_mode)) {
665 check_add_file(filename);
666 } else {
667 if (!options->recursive) {
668 warn("ignoring directory: %s", filename);
669 continue;
670 }
671 if (r_opendir(&dir, filename) < 0) {
672 warn("could not open directory: %s", filename);
673 continue;
674 }
675 start = fileidx;
676 while ((filename = r_readdir(&dir)) != NULL) {
677 check_add_file(filename);
678 free((void*) filename);
679 }
680 r_closedir(&dir);
681 if (fileidx - start > 1)
682 qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp);
683 }
684 }
685
686 if (fileidx == 0) {
687 fprintf(stderr, "sxiv: no valid image file given, aborting\n");
688 exit(EXIT_FAILURE);
689 }
690
691 filecnt = fileidx;
692 fileidx = options->startnum < filecnt ? options->startnum : 0;
693
694 win_init(&win);
695 img_init(&img, &win);
696
697 if ((homedir = getenv("HOME")) == NULL) {
698 warn("could not locate home directory");
699 } else {
700 len = strlen(homedir) + strlen(INFO_SCRIPT) + 2;
701 info.script = (char*) s_malloc(len);
702 snprintf(info.script, len, "%s/%s", homedir, INFO_SCRIPT);
703 if (access(info.script, X_OK) != 0) {
704 free(info.script);
705 info.script = NULL;
706 }
707 }
708 info.fd = -1;
709
710 if (options->thumb_mode) {
711 mode = MODE_THUMB;
712 tns_init(&tns, filecnt, &win);
713 while (!tns_load(&tns, 0, &files[0], false, false))
714 remove_file(0, false);
715 tns.cnt = 1;
716 } else {
717 mode = MODE_IMAGE;
718 tns.thumbs = NULL;
719 load_image(fileidx);
720 }
721
722 win_open(&win);
723
724 run();
725 cleanup();
726
727 return 0;
728}