Krolyxon
Commit 9fcf2c8

Branches

Refs →

nsxivmain.c

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