Krolyxon
Commit bf41012

Branches

Refs →

nsxivmain.c

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