Krolyxon
Commit f329840

Branches

Refs →

nsxivmain.c

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