Krolyxon
Commit 38bc234

Branches

Refs →

nsxivmain.c

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