Krolyxon
Commit 5dea695

Branches

Refs →

nsxivmain.c

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