Krolyxon
Commit 0353e6e

Branches

Refs →

nsxivmain.c

730 lines · 15.2 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 if (img.gamma != 0)
356 n += snprintf(rt + n, rlen - n, "g%d | ", img.gamma);
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 Button4:
499 case Button5:
500 if (tns_scroll(&tns, bev->button == Button4 ? DIR_UP : DIR_DOWN,
501 (bev->state & ControlMask) != 0))
502 redraw();
503 break;
504 }
505 }
506}
507
508void run(void)
509{
510 int xfd;
511 fd_set fds;
512 struct timeval timeout;
513 bool discard, to_set;
514 XEvent ev, nextev;
515
516 redraw();
517
518 while (true) {
519 while (mode == MODE_THUMB && tns.cnt < filecnt &&
520 XPending(win.env.dpy) == 0)
521 {
522 /* load thumbnails */
523 set_timeout(redraw, TO_REDRAW_THUMBS, false);
524 if (tns_load(&tns, tns.cnt, &files[tns.cnt], false, false)) {
525 tns.cnt++;
526 } else {
527 remove_file(tns.cnt, false);
528 if (tns.sel >= tns.cnt)
529 tns.sel--;
530 }
531 if (tns.cnt == filecnt)
532 redraw();
533 else
534 check_timeouts(NULL);
535 }
536
537 while (XPending(win.env.dpy) == 0
538 && ((to_set = check_timeouts(&timeout)) || info.fd != -1))
539 {
540 /* check for timeouts & input */
541 xfd = ConnectionNumber(win.env.dpy);
542 FD_ZERO(&fds);
543 FD_SET(xfd, &fds);
544 if (info.fd != -1) {
545 FD_SET(info.fd, &fds);
546 xfd = MAX(xfd, info.fd);
547 }
548 select(xfd + 1, &fds, 0, 0, to_set ? &timeout : NULL);
549 if (info.fd != -1 && FD_ISSET(info.fd, &fds))
550 read_info();
551 }
552
553 do {
554 XNextEvent(win.env.dpy, &ev);
555 discard = false;
556 if (XEventsQueued(win.env.dpy, QueuedAlready) > 0) {
557 XPeekEvent(win.env.dpy, &nextev);
558 switch (ev.type) {
559 case ConfigureNotify:
560 discard = ev.type == nextev.type;
561 break;
562 case KeyPress:
563 discard = (nextev.type == KeyPress || nextev.type == KeyRelease)
564 && ev.xkey.keycode == nextev.xkey.keycode;
565 break;
566 }
567 }
568 } while (discard);
569
570 switch (ev.type) {
571 /* handle events */
572 case ButtonPress:
573 on_buttonpress(&ev.xbutton);
574 break;
575 case ClientMessage:
576 if ((Atom) ev.xclient.data.l[0] == wm_delete_win)
577 return;
578 break;
579 case ConfigureNotify:
580 if (win_configure(&win, &ev.xconfigure)) {
581 if (mode == MODE_IMAGE) {
582 img.dirty = true;
583 img.checkpan = true;
584 } else {
585 tns.dirty = true;
586 }
587 if (!resized || win.fullscreen) {
588 redraw();
589 set_timeout(clear_resize, TO_REDRAW_RESIZE, false);
590 resized = true;
591 } else {
592 set_timeout(redraw, TO_REDRAW_RESIZE, false);
593 }
594 }
595 break;
596 case Expose:
597 win_expose(&win, &ev.xexpose);
598 break;
599 case KeyPress:
600 on_keypress(&ev.xkey);
601 break;
602 case MotionNotify:
603 if (mode == MODE_IMAGE) {
604 win_set_cursor(&win, CURSOR_ARROW);
605 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
606 }
607 break;
608 }
609 }
610}
611
612int fncmp(const void *a, const void *b)
613{
614 return strcoll(((fileinfo_t*) a)->name, ((fileinfo_t*) b)->name);
615}
616
617int main(int argc, char **argv)
618{
619 int i, start;
620 size_t n;
621 ssize_t len;
622 char *filename;
623 const char *homedir;
624 struct stat fstats;
625 r_dir_t dir;
626
627 parse_options(argc, argv);
628
629 if (options->clean_cache) {
630 tns_init(&tns, 0, NULL);
631 tns_clean_cache(&tns);
632 exit(EXIT_SUCCESS);
633 }
634
635 if (options->filecnt == 0 && !options->from_stdin) {
636 print_usage();
637 exit(EXIT_FAILURE);
638 }
639
640 if (options->recursive || options->from_stdin)
641 filecnt = FILENAME_CNT;
642 else
643 filecnt = options->filecnt;
644
645 files = (fileinfo_t*) s_malloc(filecnt * sizeof(fileinfo_t));
646 fileidx = 0;
647
648 if (options->from_stdin) {
649 filename = NULL;
650 while ((len = get_line(&filename, &n, stdin)) > 0) {
651 if (filename[len-1] == '\n')
652 filename[len-1] = '\0';
653 check_add_file(filename);
654 }
655 if (filename != NULL)
656 free(filename);
657 }
658
659 for (i = 0; i < options->filecnt; i++) {
660 filename = options->filenames[i];
661
662 if (stat(filename, &fstats) < 0) {
663 warn("could not stat file: %s", filename);
664 continue;
665 }
666 if (!S_ISDIR(fstats.st_mode)) {
667 check_add_file(filename);
668 } else {
669 if (!options->recursive) {
670 warn("ignoring directory: %s", filename);
671 continue;
672 }
673 if (r_opendir(&dir, filename) < 0) {
674 warn("could not open directory: %s", filename);
675 continue;
676 }
677 start = fileidx;
678 while ((filename = r_readdir(&dir)) != NULL) {
679 check_add_file(filename);
680 free((void*) filename);
681 }
682 r_closedir(&dir);
683 if (fileidx - start > 1)
684 qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp);
685 }
686 }
687
688 if (fileidx == 0) {
689 fprintf(stderr, "sxiv: no valid image file given, aborting\n");
690 exit(EXIT_FAILURE);
691 }
692
693 filecnt = fileidx;
694 fileidx = options->startnum < filecnt ? options->startnum : 0;
695
696 win_init(&win);
697 img_init(&img, &win);
698
699 if ((homedir = getenv("HOME")) == NULL) {
700 warn("could not locate home directory");
701 } else {
702 len = strlen(homedir) + strlen(INFO_SCRIPT) + 2;
703 info.script = (char*) s_malloc(len);
704 snprintf(info.script, len, "%s/%s", homedir, INFO_SCRIPT);
705 if (access(info.script, X_OK) != 0) {
706 free(info.script);
707 info.script = NULL;
708 }
709 }
710 info.fd = -1;
711
712 if (options->thumb_mode) {
713 mode = MODE_THUMB;
714 tns_init(&tns, filecnt, &win);
715 while (!tns_load(&tns, 0, &files[0], false, false))
716 remove_file(0, false);
717 tns.cnt = 1;
718 } else {
719 mode = MODE_IMAGE;
720 tns.thumbs = NULL;
721 load_image(fileidx);
722 }
723
724 win_open(&win);
725
726 run();
727 cleanup();
728
729 return 0;
730}