Krolyxon
Commit 0b91328

Branches

Refs →

nsxivmain.c

783 lines · 16.3 KB · C

1/* sxiv: main.c
2 * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19#include <stdlib.h>
20#include <stdio.h>
21#include <string.h>
22#include <sys/select.h>
23#include <sys/stat.h>
24#include <sys/time.h>
25#include <sys/wait.h>
26#include <unistd.h>
27
28#include <X11/Xlib.h>
29#include <X11/Xutil.h>
30#include <X11/keysym.h>
31
32#include "image.h"
33#include "options.h"
34#include "thumbs.h"
35#include "util.h"
36#include "window.h"
37
38#define FNAME_CNT 1024
39#define TITLE_LEN 256
40
41#define TO_WIN_RESIZE 75000
42#define TO_IMAGE_DRAG 1000
43#define TO_CURSOR_HIDE 1500000
44#define TO_THUMBS_LOAD 75000
45
46typedef struct {
47 KeySym ksym;
48 Bool reload;
49 const char *cmdline;
50} command_t;
51
52typedef enum {
53 MODE_NORMAL = 0,
54 MODE_THUMBS
55} appmode_t;
56
57#define MAIN_C
58#include "config.h"
59
60void run();
61
62appmode_t mode;
63img_t img;
64tns_t tns;
65win_t win;
66
67const char **filenames;
68int filecnt, fileidx;
69size_t filesize;
70
71char win_title[TITLE_LEN];
72
73int timo_cursor;
74int timo_redraw;
75unsigned char drag;
76int mox, moy;
77
78void cleanup() {
79 static int in = 0;
80
81 if (!in++) {
82 img_close(&img, 0);
83 tns_free(&tns);
84 win_close(&win);
85 }
86}
87
88void remove_file(int n, unsigned char silent) {
89 if (n < 0 || n >= filecnt)
90 return;
91
92 if (filecnt == 1) {
93 if (!silent)
94 fprintf(stderr, "sxiv: no more files to display, aborting\n");
95 cleanup();
96 exit(!silent);
97 }
98
99 if (n + 1 < filecnt)
100 memmove(filenames + n, filenames + n + 1, (filecnt - n - 1) *
101 sizeof(const char*));
102 if (n + 1 < tns.cnt) {
103 memmove(tns.thumbs + n, tns.thumbs + n + 1, (tns.cnt - n - 1) *
104 sizeof(thumb_t));
105 memset(tns.thumbs + tns.cnt - 1, 0, sizeof(thumb_t));
106 }
107
108 --filecnt;
109 if (n < tns.cnt)
110 --tns.cnt;
111}
112
113int load_image(int new) {
114 struct stat fstats;
115
116 if (new >= 0 && new < filecnt) {
117 win_set_cursor(&win, CURSOR_WATCH);
118 img_close(&img, 0);
119
120 while (!img_load(&img, filenames[new])) {
121 remove_file(new, 0);
122 if (new >= filecnt)
123 new = filecnt - 1;
124 }
125 fileidx = new;
126 if (!stat(filenames[new], &fstats))
127 filesize = fstats.st_size;
128 else
129 filesize = 0;
130
131 if (!timo_cursor)
132 win_set_cursor(&win, CURSOR_NONE);
133 }
134 return 1;
135}
136
137void update_title() {
138 int n;
139 float size;
140 const char *unit;
141
142 if (mode == MODE_THUMBS) {
143 n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] %s",
144 tns.cnt ? tns.sel + 1 : 0, tns.cnt,
145 tns.cnt ? filenames[tns.sel] : "");
146 } else {
147 size = filesize;
148 size_readable(&size, &unit);
149 n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] <%d%%> (%.2f%s) %s",
150 fileidx + 1, filecnt, (int) (img.zoom * 100.0), size, unit,
151 filenames[fileidx]);
152 }
153
154 if (n >= TITLE_LEN) {
155 win_title[TITLE_LEN - 2] = '.';
156 win_title[TITLE_LEN - 3] = '.';
157 win_title[TITLE_LEN - 4] = '.';
158 }
159
160 win_set_title(&win, win_title);
161}
162
163int check_append(const char *filename) {
164 if (!filename)
165 return 0;
166
167 if (access(filename, R_OK)) {
168 warn("could not open file: %s", filename);
169 return 0;
170 } else {
171 if (fileidx == filecnt) {
172 filecnt *= 2;
173 filenames = (const char**) s_realloc(filenames,
174 filecnt * sizeof(const char*));
175 }
176 filenames[fileidx++] = filename;
177 return 1;
178 }
179}
180
181int fncmp(const void *a, const void *b) {
182 return strcoll(*((char* const*) a), *((char* const*) b));
183}
184
185int main(int argc, char **argv) {
186 int i, start;
187 const char *filename;
188 struct stat fstats;
189 r_dir_t dir;
190
191 parse_options(argc, argv);
192
193 if (options->clean_cache) {
194 tns_init(&tns, 0);
195 tns_clean_cache(&tns);
196 exit(0);
197 }
198
199 if (!options->filecnt) {
200 print_usage();
201 exit(1);
202 }
203
204 if (options->recursive || options->from_stdin)
205 filecnt = FNAME_CNT;
206 else
207 filecnt = options->filecnt;
208
209 filenames = (const char**) s_malloc(filecnt * sizeof(const char*));
210 fileidx = 0;
211
212 if (options->from_stdin) {
213 while ((filename = readline(stdin))) {
214 if (!*filename || !check_append(filename))
215 free((void*) filename);
216 }
217 } else {
218 for (i = 0; i < options->filecnt; ++i) {
219 filename = options->filenames[i];
220
221 if (stat(filename, &fstats) || !S_ISDIR(fstats.st_mode)) {
222 check_append(filename);
223 } else {
224 if (!options->recursive) {
225 warn("ignoring directory: %s", filename);
226 continue;
227 }
228 if (r_opendir(&dir, filename)) {
229 warn("could not open directory: %s", filename);
230 continue;
231 }
232 start = fileidx;
233 while ((filename = r_readdir(&dir))) {
234 if (!check_append(filename))
235 free((void*) filename);
236 }
237 r_closedir(&dir);
238 if (fileidx - start > 1)
239 qsort(filenames + start, fileidx - start, sizeof(char*), fncmp);
240 }
241 }
242 }
243
244 filecnt = fileidx;
245 fileidx = 0;
246
247 if (!filecnt) {
248 fprintf(stderr, "sxiv: no valid image file given, aborting\n");
249 exit(1);
250 }
251
252 win_init(&win);
253 img_init(&img, &win);
254
255 if (options->thumbnails) {
256 mode = MODE_THUMBS;
257 tns_init(&tns, filecnt);
258 while (!tns_load(&tns, 0, filenames[0], 0))
259 remove_file(0, 0);
260 tns.cnt = 1;
261 } else {
262 mode = MODE_NORMAL;
263 tns.thumbs = NULL;
264 load_image(fileidx);
265 }
266
267 win_open(&win);
268
269 run();
270 cleanup();
271
272 return 0;
273}
274
275#if EXT_COMMANDS
276int run_command(const char *cline, Bool reload) {
277 int fncnt, fnlen;
278 char *cn, *cmdline;
279 const char *co, *fname;
280 pid_t pid;
281 int ret, status;
282
283 if (!cline || !*cline)
284 return 0;
285
286 fncnt = 0;
287 co = cline - 1;
288 while ((co = strchr(co + 1, '#')))
289 ++fncnt;
290
291 if (!fncnt)
292 return 0;
293
294 ret = 0;
295 fname = filenames[mode == MODE_NORMAL ? fileidx : tns.sel];
296 fnlen = strlen(fname);
297 cn = cmdline = (char*) s_malloc((strlen(cline) + fncnt * (fnlen + 2)) *
298 sizeof(char));
299
300 /* replace all '#' with filename */
301 for (co = cline; *co; ++co) {
302 if (*co == '#') {
303 *cn++ = '"';
304 strcpy(cn, fname);
305 cn += fnlen;
306 *cn++ = '"';
307 } else {
308 *cn++ = *co;
309 }
310 }
311 *cn = '\0';
312
313 if ((pid = fork()) == 0) {
314 execlp("/bin/sh", "/bin/sh", "-c", cmdline, NULL);
315 warn("could not exec: /bin/sh");
316 exit(1);
317 } else if (pid < 0) {
318 warn("could not fork. command line was: %s", cmdline);
319 } else if (reload) {
320 waitpid(pid, &status, 0);
321 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
322 ret = 1;
323 else
324 warn("child exited with non-zero return value: %d. command line was: %s",
325 WEXITSTATUS(status), cmdline);
326 }
327
328 free(cmdline);
329 return ret;
330}
331#endif /* EXT_COMMANDS */
332
333
334/* event handling */
335
336void redraw() {
337 if (mode == MODE_NORMAL) {
338 img_render(&img, &win);
339 if (timo_cursor)
340 win_set_cursor(&win, CURSOR_ARROW);
341 else if (!drag)
342 win_set_cursor(&win, CURSOR_NONE);
343 } else {
344 tns_render(&tns, &win);
345 }
346 update_title();
347 timo_redraw = 0;
348}
349
350void on_keypress(XKeyEvent *kev) {
351 int x, y;
352 unsigned int w, h;
353 char key;
354 KeySym ksym;
355 int changed;
356
357 if (!kev)
358 return;
359
360 XLookupString(kev, &key, 1, &ksym, NULL);
361 changed = 0;
362
363#if EXT_COMMANDS
364 /* external commands from commands.h */
365 if (CLEANMASK(kev->state) & ControlMask) {
366 for (x = 0; x < LEN(commands); ++x) {
367 if (commands[x].ksym == ksym) {
368 win_set_cursor(&win, CURSOR_WATCH);
369 if (run_command(commands[x].cmdline, commands[x].reload)) {
370 if (mode == MODE_NORMAL) {
371 if (fileidx < tns.cnt)
372 tns_load(&tns, fileidx, filenames[fileidx], 1);
373 img_close(&img, 1);
374 load_image(fileidx);
375 } else {
376 if (!tns_load(&tns, tns.sel, filenames[tns.sel], 0)) {
377 remove_file(tns.sel, 0);
378 tns.dirty = 1;
379 if (tns.sel >= tns.cnt)
380 tns.sel = tns.cnt - 1;
381 }
382 }
383 redraw();
384 }
385 if (mode == MODE_THUMBS)
386 win_set_cursor(&win, CURSOR_ARROW);
387 else if (!timo_cursor)
388 win_set_cursor(&win, CURSOR_NONE);
389 return;
390 }
391 }
392 }
393#endif
394
395 if (mode == MODE_NORMAL) {
396 switch (ksym) {
397 /* navigate image list */
398 case XK_n:
399 case XK_space:
400 if (fileidx + 1 < filecnt)
401 changed = load_image(fileidx + 1);
402 break;
403 case XK_p:
404 case XK_BackSpace:
405 if (fileidx > 0)
406 changed = load_image(fileidx - 1);
407 break;
408 case XK_bracketleft:
409 if (fileidx != 0)
410 changed = load_image(MAX(0, fileidx - 10));
411 break;
412 case XK_bracketright:
413 if (fileidx != filecnt - 1)
414 changed = load_image(MIN(fileidx + 10, filecnt - 1));
415 break;
416 case XK_g:
417 if (fileidx != 0)
418 changed = load_image(0);
419 break;
420 case XK_G:
421 if (fileidx != filecnt - 1)
422 changed = load_image(filecnt - 1);
423 break;
424
425 /* zooming */
426 case XK_plus:
427 case XK_equal:
428 changed = img_zoom_in(&img);
429 break;
430 case XK_minus:
431 changed = img_zoom_out(&img);
432 break;
433 case XK_0:
434 changed = img_zoom(&img, 1.0);
435 break;
436 case XK_w:
437 if ((changed = img_fit_win(&img, &win)))
438 img_center(&img, &win);
439 break;
440
441 /* panning */
442 case XK_h:
443 case XK_Left:
444 changed = img_pan(&img, &win, PAN_LEFT);
445 break;
446 case XK_j:
447 case XK_Down:
448 changed = img_pan(&img, &win, PAN_DOWN);
449 break;
450 case XK_k:
451 case XK_Up:
452 changed = img_pan(&img, &win, PAN_UP);
453 break;
454 case XK_l:
455 case XK_Right:
456 changed = img_pan(&img, &win, PAN_RIGHT);
457 break;
458
459 /* rotation */
460 case XK_less:
461 img_rotate_left(&img, &win);
462 changed = 1;
463 break;
464 case XK_greater:
465 img_rotate_right(&img, &win);
466 changed = 1;
467 break;
468
469 /* control window */
470 case XK_W:
471 x = MAX(0, win.x + img.x);
472 y = MAX(0, win.y + img.y);
473 w = img.w * img.zoom;
474 h = img.h * img.zoom;
475 if ((changed = win_moveresize(&win, x, y, w, h))) {
476 img.x = x - win.x;
477 img.y = y - win.y;
478 }
479 break;
480
481 /* switch to thumbnail mode */
482 case XK_Return:
483 if (!tns.thumbs)
484 tns_init(&tns, filecnt);
485 img_close(&img, 0);
486 mode = MODE_THUMBS;
487 win_set_cursor(&win, CURSOR_ARROW);
488 timo_cursor = 0;
489 tns.sel = fileidx;
490 changed = tns.dirty = 1;
491 break;
492
493 /* miscellaneous */
494 case XK_a:
495 img_toggle_antialias(&img);
496 changed = 1;
497 break;
498 case XK_A:
499 img.alpha ^= 1;
500 changed = 1;
501 break;
502 case XK_D:
503 remove_file(fileidx, 1);
504 changed = load_image(fileidx >= filecnt ? filecnt - 1 : fileidx);
505 break;
506 case XK_r:
507 changed = load_image(fileidx);
508 break;
509 }
510 } else {
511 /* thumbnail mode */
512 switch (ksym) {
513 /* open selected image */
514 case XK_Return:
515 load_image(tns.sel);
516 mode = MODE_NORMAL;
517 changed = 1;
518 break;
519
520 /* move selection */
521 case XK_h:
522 case XK_Left:
523 changed = tns_move_selection(&tns, &win, TNS_LEFT);
524 break;
525 case XK_j:
526 case XK_Down:
527 changed = tns_move_selection(&tns, &win, TNS_DOWN);
528 break;
529 case XK_k:
530 case XK_Up:
531 changed = tns_move_selection(&tns, &win, TNS_UP);
532 break;
533 case XK_l:
534 case XK_Right:
535 changed = tns_move_selection(&tns, &win, TNS_RIGHT);
536 break;
537 case XK_g:
538 if (tns.sel != 0) {
539 tns.sel = 0;
540 changed = tns.dirty = 1;
541 }
542 break;
543 case XK_G:
544 if (tns.sel != tns.cnt - 1) {
545 tns.sel = tns.cnt - 1;
546 changed = tns.dirty = 1;
547 }
548 break;
549
550 /* miscellaneous */
551 case XK_D:
552 if (tns.sel < tns.cnt) {
553 remove_file(tns.sel, 1);
554 changed = tns.dirty = 1;
555 if (tns.sel >= tns.cnt)
556 tns.sel = tns.cnt - 1;
557 }
558 break;
559 }
560 }
561
562 /* common key mappings */
563 switch (ksym) {
564 case XK_q:
565 cleanup();
566 exit(0);
567 case XK_f:
568 win_toggle_fullscreen(&win);
569 /* render on next configurenotify */
570 break;
571 }
572
573 if (changed)
574 redraw();
575}
576
577void on_buttonpress(XButtonEvent *bev) {
578 int changed, sel;
579 unsigned int mask;
580
581 if (!bev)
582 return;
583
584 mask = CLEANMASK(bev->state);
585 changed = 0;
586
587 if (mode == MODE_NORMAL) {
588 win_set_cursor(&win, CURSOR_ARROW);
589 timo_cursor = TO_CURSOR_HIDE;
590
591 switch (bev->button) {
592 case Button1:
593 if (fileidx + 1 < filecnt)
594 changed = load_image(fileidx + 1);
595 break;
596 case Button2:
597 mox = bev->x;
598 moy = bev->y;
599 win_set_cursor(&win, CURSOR_HAND);
600 timo_cursor = 0;
601 drag = 1;
602 break;
603 case Button3:
604 if (fileidx > 0)
605 changed = load_image(fileidx - 1);
606 break;
607 case Button4:
608 if (mask == ControlMask)
609 changed = img_zoom_in(&img);
610 else if (mask == ShiftMask)
611 changed = img_pan(&img, &win, PAN_LEFT);
612 else
613 changed = img_pan(&img, &win, PAN_UP);
614 break;
615 case Button5:
616 if (mask == ControlMask)
617 changed = img_zoom_out(&img);
618 else if (mask == ShiftMask)
619 changed = img_pan(&img, &win, PAN_RIGHT);
620 else
621 changed = img_pan(&img, &win, PAN_DOWN);
622 break;
623 case 6:
624 changed = img_pan(&img, &win, PAN_LEFT);
625 break;
626 case 7:
627 changed = img_pan(&img, &win, PAN_RIGHT);
628 break;
629 }
630 } else {
631 /* thumbnail mode */
632 switch (bev->button) {
633 case Button1:
634 if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
635 if (sel == tns.sel) {
636 load_image(tns.sel);
637 mode = MODE_NORMAL;
638 timo_cursor = TO_CURSOR_HIDE;
639 } else {
640 tns_highlight(&tns, &win, tns.sel, False);
641 tns_highlight(&tns, &win, sel, True);
642 tns.sel = sel;
643 }
644 changed = 1;
645 break;
646 }
647 break;
648 case Button4:
649 changed = tns_scroll(&tns, TNS_UP);
650 break;
651 case Button5:
652 changed = tns_scroll(&tns, TNS_DOWN);
653 break;
654 }
655 }
656
657 if (changed)
658 redraw();
659}
660
661void on_motionnotify(XMotionEvent *mev) {
662 if (!mev)
663 return;
664
665 if (mev->x >= 0 && mev->x <= win.w && mev->y >= 0 && mev->y <= win.h) {
666 if (img_move(&img, &win, mev->x - mox, mev->y - moy))
667 timo_redraw = TO_IMAGE_DRAG;
668
669 mox = mev->x;
670 moy = mev->y;
671 }
672}
673
674void run() {
675 int xfd, timeout;
676 fd_set fds;
677 struct timeval tt, t0, t1;
678 XEvent ev;
679
680 drag = 0;
681 timo_cursor = mode == MODE_NORMAL ? TO_CURSOR_HIDE : 0;
682
683 redraw();
684
685 while (1) {
686 if (mode == MODE_THUMBS && tns.cnt < filecnt) {
687 win_set_cursor(&win, CURSOR_WATCH);
688 gettimeofday(&t0, 0);
689
690 while (tns.cnt < filecnt && !XPending(win.env.dpy)) {
691 if (tns_load(&tns, tns.cnt, filenames[tns.cnt], 0))
692 ++tns.cnt;
693 else
694 remove_file(tns.cnt, 0);
695 gettimeofday(&t1, 0);
696 if (TV_TO_DOUBLE(t1) - TV_TO_DOUBLE(t0) >= 0.25)
697 break;
698 }
699 if (tns.cnt == filecnt)
700 win_set_cursor(&win, CURSOR_ARROW);
701 if (!XPending(win.env.dpy)) {
702 redraw();
703 continue;
704 } else {
705 timo_redraw = TO_THUMBS_LOAD;
706 }
707 } else if (timo_cursor || timo_redraw) {
708 gettimeofday(&t0, 0);
709 if (timo_cursor && timo_redraw)
710 timeout = MIN(timo_cursor, timo_redraw);
711 else if (timo_cursor)
712 timeout = timo_cursor;
713 else
714 timeout = timo_redraw;
715 tt.tv_sec = timeout / 1000000;
716 tt.tv_usec = timeout % 1000000;
717 xfd = ConnectionNumber(win.env.dpy);
718 FD_ZERO(&fds);
719 FD_SET(xfd, &fds);
720
721 if (!XPending(win.env.dpy))
722 select(xfd + 1, &fds, 0, 0, &tt);
723 gettimeofday(&t1, 0);
724 timeout = MIN((TV_TO_DOUBLE(t1) - TV_TO_DOUBLE(t0)) * 1000000, timeout);
725
726 /* timeouts fired? */
727 if (timo_cursor) {
728 timo_cursor = MAX(0, timo_cursor - timeout);
729 if (!timo_cursor)
730 win_set_cursor(&win, CURSOR_NONE);
731 }
732 if (timo_redraw) {
733 timo_redraw = MAX(0, timo_redraw - timeout);
734 if (!timo_redraw)
735 redraw();
736 }
737 if (!XPending(win.env.dpy) && (timo_cursor || timo_redraw))
738 continue;
739 }
740
741 if (!XNextEvent(win.env.dpy, &ev)) {
742 switch (ev.type) {
743 case KeyPress:
744 on_keypress(&ev.xkey);
745 break;
746 case ButtonPress:
747 on_buttonpress(&ev.xbutton);
748 break;
749 case ButtonRelease:
750 if (ev.xbutton.button == Button2) {
751 drag = 0;
752 if (mode == MODE_NORMAL) {
753 win_set_cursor(&win, CURSOR_ARROW);
754 timo_cursor = TO_CURSOR_HIDE;
755 }
756 }
757 break;
758 case MotionNotify:
759 if (drag) {
760 on_motionnotify(&ev.xmotion);
761 } else if (mode == MODE_NORMAL) {
762 if (!timo_cursor)
763 win_set_cursor(&win, CURSOR_ARROW);
764 timo_cursor = TO_CURSOR_HIDE;
765 }
766 break;
767 case ConfigureNotify:
768 if (win_configure(&win, &ev.xconfigure)) {
769 timo_redraw = TO_WIN_RESIZE;
770 if (mode == MODE_NORMAL)
771 img.checkpan = 1;
772 else
773 tns.dirty = 1;
774 }
775 break;
776 case ClientMessage:
777 if ((Atom) ev.xclient.data.l[0] == wm_delete_win)
778 return;
779 break;
780 }
781 }
782 }
783}