Krolyxon
Commit b2eae52

Branches

Refs →

nsxivmain.c

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