Krolyxon
Commit 50f9ad1

Branches

Refs →

nsxivmain.c

737 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", mark);
349 if (img.gamma != 0)
350 n += snprintf(rt + n, rlen - n, "G%+d | ", img.gamma);
351 n += snprintf(rt + n, rlen - n, "%3d%% | ", (int) (img.zoom * 100.0));
352 if (img.multi.cnt > 0) {
353 for (fn = 0, i = img.multi.cnt; i > 0; fn++, i /= 10);
354 n += snprintf(rt + n, rlen - n, "%0*d/%d | ",
355 fn, img.multi.sel + 1, img.multi.cnt);
356 }
357 n += snprintf(rt + n, rlen - n, "%0*d/%d", fw, sel + 1, filecnt);
358 ow_info = info.script == NULL;
359 }
360 if (ow_info) {
361 fn = strlen(files[sel].name);
362 if (fn < llen &&
363 win_textwidth(files[sel].name, fn, true) +
364 win_textwidth(rt, n, true) < win.w)
365 {
366 strncpy(lt, files[sel].name, llen);
367 } else {
368 strncpy(lt, files[sel].base, llen);
369 }
370 }
371}
372
373void redraw(void)
374{
375 if (mode == MODE_IMAGE)
376 img_render(&img);
377 else
378 tns_render(&tns);
379 update_info();
380 win_draw(&win);
381 reset_timeout(redraw);
382 reset_cursor();
383}
384
385void reset_cursor(void)
386{
387 int i;
388 cursor_t cursor = CURSOR_NONE;
389
390 if (mode == MODE_IMAGE) {
391 for (i = 0; i < ARRLEN(timeouts); i++) {
392 if (timeouts[i].handler == reset_cursor) {
393 if (timeouts[i].active)
394 cursor = CURSOR_ARROW;
395 break;
396 }
397 }
398 } else {
399 if (tns.cnt != filecnt)
400 cursor = CURSOR_WATCH;
401 else
402 cursor = CURSOR_ARROW;
403 }
404 win_set_cursor(&win, cursor);
405}
406
407void animate(void)
408{
409 if (img_frame_animate(&img, false)) {
410 redraw();
411 set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
412 }
413}
414
415void clear_resize(void)
416{
417 resized = false;
418}
419
420bool keymask(const keymap_t *k, unsigned int state)
421{
422 return (k->ctrl ? ControlMask : 0) == (state & ControlMask);
423}
424
425bool buttonmask(const button_t *b, unsigned int state)
426{
427 return ((b->ctrl ? ControlMask : 0) | (b->shift ? ShiftMask : 0)) ==
428 (state & (ControlMask | ShiftMask));
429}
430
431void on_keypress(XKeyEvent *kev)
432{
433 int i;
434 KeySym ksym;
435 char key;
436
437 if (kev == NULL)
438 return;
439
440 XLookupString(kev, &key, 1, &ksym, NULL);
441
442 if ((ksym == XK_Escape || (key >= '0' && key <= '9')) &&
443 (kev->state & ControlMask) == 0)
444 {
445 /* number prefix for commands */
446 prefix = ksym == XK_Escape ? 0 : prefix * 10 + (int) (key - '0');
447 return;
448 }
449
450 for (i = 0; i < ARRLEN(keys); i++) {
451 if (keys[i].ksym == ksym && keymask(&keys[i], kev->state)) {
452 if (keys[i].cmd != NULL && keys[i].cmd(keys[i].arg))
453 redraw();
454 prefix = 0;
455 return;
456 }
457 }
458}
459
460void on_buttonpress(XButtonEvent *bev)
461{
462 int i, sel;
463
464 if (bev == NULL)
465 return;
466
467 if (mode == MODE_IMAGE) {
468 win_set_cursor(&win, CURSOR_ARROW);
469 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
470
471 for (i = 0; i < ARRLEN(buttons); i++) {
472 if (buttons[i].button == bev->button &&
473 buttonmask(&buttons[i], bev->state))
474 {
475 if (buttons[i].cmd != NULL && buttons[i].cmd(buttons[i].arg))
476 redraw();
477 return;
478 }
479 }
480 } else {
481 /* thumbnail mode (hard-coded) */
482 switch (bev->button) {
483 case Button1:
484 if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
485 if (sel == tns.sel) {
486 mode = MODE_IMAGE;
487 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
488 load_image(tns.sel);
489 } else {
490 tns_highlight(&tns, tns.sel, false);
491 tns_highlight(&tns, sel, true);
492 tns.sel = sel;
493 }
494 redraw();
495 break;
496 }
497 break;
498 case Button3:
499 if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
500 files[sel].marked = !files[sel].marked;
501 tns_mark(&tns, sel, files[sel].marked);
502 redraw();
503 }
504 break;
505 case Button4:
506 case Button5:
507 if (tns_scroll(&tns, bev->button == Button4 ? DIR_UP : DIR_DOWN,
508 (bev->state & ControlMask) != 0))
509 redraw();
510 break;
511 }
512 }
513}
514
515void run(void)
516{
517 int xfd;
518 fd_set fds;
519 struct timeval timeout;
520 bool discard, to_set;
521 XEvent ev, nextev;
522
523 redraw();
524
525 while (true) {
526 while (mode == MODE_THUMB && tns.cnt < filecnt &&
527 XPending(win.env.dpy) == 0)
528 {
529 /* load thumbnails */
530 set_timeout(redraw, TO_REDRAW_THUMBS, false);
531 if (tns_load(&tns, tns.cnt, &files[tns.cnt], false, false)) {
532 tns.cnt++;
533 } else {
534 remove_file(tns.cnt, false);
535 if (tns.sel >= tns.cnt)
536 tns.sel--;
537 }
538 if (tns.cnt == filecnt)
539 redraw();
540 else
541 check_timeouts(NULL);
542 }
543
544 while (XPending(win.env.dpy) == 0
545 && ((to_set = check_timeouts(&timeout)) || info.fd != -1))
546 {
547 /* check for timeouts & input */
548 xfd = ConnectionNumber(win.env.dpy);
549 FD_ZERO(&fds);
550 FD_SET(xfd, &fds);
551 if (info.fd != -1) {
552 FD_SET(info.fd, &fds);
553 xfd = MAX(xfd, info.fd);
554 }
555 select(xfd + 1, &fds, 0, 0, to_set ? &timeout : NULL);
556 if (info.fd != -1 && FD_ISSET(info.fd, &fds))
557 read_info();
558 }
559
560 do {
561 XNextEvent(win.env.dpy, &ev);
562 discard = false;
563 if (XEventsQueued(win.env.dpy, QueuedAlready) > 0) {
564 XPeekEvent(win.env.dpy, &nextev);
565 switch (ev.type) {
566 case ConfigureNotify:
567 discard = ev.type == nextev.type;
568 break;
569 case KeyPress:
570 discard = (nextev.type == KeyPress || nextev.type == KeyRelease)
571 && ev.xkey.keycode == nextev.xkey.keycode;
572 break;
573 }
574 }
575 } while (discard);
576
577 switch (ev.type) {
578 /* handle events */
579 case ButtonPress:
580 on_buttonpress(&ev.xbutton);
581 break;
582 case ClientMessage:
583 if ((Atom) ev.xclient.data.l[0] == wm_delete_win)
584 return;
585 break;
586 case ConfigureNotify:
587 if (win_configure(&win, &ev.xconfigure)) {
588 if (mode == MODE_IMAGE) {
589 img.dirty = true;
590 img.checkpan = true;
591 } else {
592 tns.dirty = true;
593 }
594 if (!resized || win.fullscreen) {
595 redraw();
596 set_timeout(clear_resize, TO_REDRAW_RESIZE, false);
597 resized = true;
598 } else {
599 set_timeout(redraw, TO_REDRAW_RESIZE, false);
600 }
601 }
602 break;
603 case Expose:
604 win_expose(&win, &ev.xexpose);
605 break;
606 case KeyPress:
607 on_keypress(&ev.xkey);
608 break;
609 case MotionNotify:
610 if (mode == MODE_IMAGE) {
611 win_set_cursor(&win, CURSOR_ARROW);
612 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
613 }
614 break;
615 }
616 }
617}
618
619int fncmp(const void *a, const void *b)
620{
621 return strcoll(((fileinfo_t*) a)->name, ((fileinfo_t*) b)->name);
622}
623
624int main(int argc, char **argv)
625{
626 int i, start;
627 size_t n;
628 ssize_t len;
629 char *filename;
630 const char *homedir;
631 struct stat fstats;
632 r_dir_t dir;
633
634 parse_options(argc, argv);
635
636 if (options->clean_cache) {
637 tns_init(&tns, 0, NULL);
638 tns_clean_cache(&tns);
639 exit(EXIT_SUCCESS);
640 }
641
642 if (options->filecnt == 0 && !options->from_stdin) {
643 print_usage();
644 exit(EXIT_FAILURE);
645 }
646
647 if (options->recursive || options->from_stdin)
648 filecnt = FILENAME_CNT;
649 else
650 filecnt = options->filecnt;
651
652 files = (fileinfo_t*) s_malloc(filecnt * sizeof(fileinfo_t));
653 fileidx = 0;
654
655 if (options->from_stdin) {
656 filename = NULL;
657 while ((len = get_line(&filename, &n, stdin)) > 0) {
658 if (filename[len-1] == '\n')
659 filename[len-1] = '\0';
660 check_add_file(filename);
661 }
662 if (filename != NULL)
663 free(filename);
664 }
665
666 for (i = 0; i < options->filecnt; i++) {
667 filename = options->filenames[i];
668
669 if (stat(filename, &fstats) < 0) {
670 warn("could not stat file: %s", filename);
671 continue;
672 }
673 if (!S_ISDIR(fstats.st_mode)) {
674 check_add_file(filename);
675 } else {
676 if (!options->recursive) {
677 warn("ignoring directory: %s", filename);
678 continue;
679 }
680 if (r_opendir(&dir, filename) < 0) {
681 warn("could not open directory: %s", filename);
682 continue;
683 }
684 start = fileidx;
685 while ((filename = r_readdir(&dir)) != NULL) {
686 check_add_file(filename);
687 free((void*) filename);
688 }
689 r_closedir(&dir);
690 if (fileidx - start > 1)
691 qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp);
692 }
693 }
694
695 if (fileidx == 0) {
696 fprintf(stderr, "sxiv: no valid image file given, aborting\n");
697 exit(EXIT_FAILURE);
698 }
699
700 filecnt = fileidx;
701 fileidx = options->startnum < filecnt ? options->startnum : 0;
702
703 win_init(&win);
704 img_init(&img, &win);
705
706 if ((homedir = getenv("HOME")) == NULL) {
707 warn("could not locate home directory");
708 } else {
709 len = strlen(homedir) + strlen(INFO_SCRIPT) + 2;
710 info.script = (char*) s_malloc(len);
711 snprintf(info.script, len, "%s/%s", homedir, INFO_SCRIPT);
712 if (access(info.script, X_OK) != 0) {
713 free(info.script);
714 info.script = NULL;
715 }
716 }
717 info.fd = -1;
718
719 if (options->thumb_mode) {
720 mode = MODE_THUMB;
721 tns_init(&tns, filecnt, &win);
722 while (!tns_load(&tns, 0, &files[0], false, false))
723 remove_file(0, false);
724 tns.cnt = 1;
725 } else {
726 mode = MODE_IMAGE;
727 tns.thumbs = NULL;
728 load_image(fileidx);
729 }
730
731 win_open(&win);
732
733 run();
734 cleanup();
735
736 return 0;
737}