Krolyxon
Commit 2737fc8

Branches

Refs →

nsxivmain.c

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