Krolyxon
Commit 72071d5

Branches

Refs →

nsxivmain.c

592 lines · 13.2 KB · C

1/* sxiv: main.c
2 * Copyright (c) 2012 Bert Muennich <be.muennich at googlemail.com>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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/Xutil.h>
30#include <X11/keysym.h>
31
32#include "commands.h"
33#include "image.h"
34#include "options.h"
35#include "thumbs.h"
36#include "types.h"
37#include "util.h"
38#include "window.h"
39#include "config.h"
40
41enum {
42 INFO_STR_LEN = 256,
43 FILENAME_CNT = 1024
44};
45
46typedef struct {
47 struct timeval when;
48 bool active;
49 timeout_f handler;
50} timeout_t;
51
52/* timeout handler functions: */
53void redraw(void);
54void reset_cursor(void);
55void animate(void);
56void clear_resize(void);
57
58appmode_t mode;
59img_t img;
60tns_t tns;
61win_t win;
62
63fileinfo_t *files;
64int filecnt, fileidx;
65size_t filesize;
66
67int prefix;
68
69bool resized = false;
70
71char win_bar_l[INFO_STR_LEN];
72char win_bar_r[INFO_STR_LEN];
73char win_title[INFO_STR_LEN];
74
75timeout_t timeouts[] = {
76 { { 0, 0 }, false, redraw },
77 { { 0, 0 }, false, reset_cursor },
78 { { 0, 0 }, false, animate },
79 { { 0, 0 }, false, clear_resize },
80};
81
82void cleanup(void) {
83 static bool in = false;
84
85 if (!in) {
86 in = true;
87 img_close(&img, false);
88 tns_free(&tns);
89 win_close(&win);
90 }
91}
92
93void check_add_file(char *filename) {
94 const char *bn;
95
96 if (filename == NULL || *filename == '\0')
97 return;
98
99 if (access(filename, R_OK) < 0) {
100 warn("could not open file: %s", filename);
101 return;
102 }
103
104 if (fileidx == filecnt) {
105 filecnt *= 2;
106 files = (fileinfo_t*) s_realloc(files, filecnt * sizeof(fileinfo_t));
107 }
108 if (*filename != '/') {
109 files[fileidx].path = absolute_path(filename);
110 if (files[fileidx].path == NULL) {
111 warn("could not get absolute path of file: %s\n", filename);
112 return;
113 }
114 }
115 files[fileidx].loaded = false;
116 files[fileidx].name = s_strdup(filename);
117 if (*filename == '/')
118 files[fileidx].path = files[fileidx].name;
119 if ((bn = strrchr(files[fileidx].name , '/')) != NULL && bn[1] != '\0')
120 files[fileidx].base = ++bn;
121 else
122 files[fileidx].base = files[fileidx].name;
123 fileidx++;
124}
125
126void remove_file(int n, bool manual) {
127 if (n < 0 || n >= filecnt)
128 return;
129
130 if (filecnt == 1) {
131 if (!manual)
132 fprintf(stderr, "sxiv: no more files to display, aborting\n");
133 cleanup();
134 exit(manual ? EXIT_SUCCESS : EXIT_FAILURE);
135 }
136
137 if (files[n].path != files[n].name)
138 free((void*) files[n].path);
139 free((void*) files[n].name);
140
141 if (n + 1 < filecnt)
142 memmove(files + n, files + n + 1, (filecnt - n - 1) * sizeof(fileinfo_t));
143 if (n + 1 < tns.cnt) {
144 memmove(tns.thumbs + n, tns.thumbs + n + 1, (tns.cnt - n - 1) *
145 sizeof(thumb_t));
146 memset(tns.thumbs + tns.cnt - 1, 0, sizeof(thumb_t));
147 }
148
149 filecnt--;
150 if (n < tns.cnt)
151 tns.cnt--;
152}
153
154void set_timeout(timeout_f handler, int time, bool overwrite) {
155 int i;
156
157 for (i = 0; i < ARRLEN(timeouts); i++) {
158 if (timeouts[i].handler == handler) {
159 if (!timeouts[i].active || overwrite) {
160 gettimeofday(&timeouts[i].when, 0);
161 TV_ADD_MSEC(&timeouts[i].when, time);
162 timeouts[i].active = true;
163 }
164 return;
165 }
166 }
167}
168
169void reset_timeout(timeout_f handler) {
170 int i;
171
172 for (i = 0; i < ARRLEN(timeouts); i++) {
173 if (timeouts[i].handler == handler) {
174 timeouts[i].active = false;
175 return;
176 }
177 }
178}
179
180bool check_timeouts(struct timeval *t) {
181 int i = 0, tdiff, tmin = -1;
182 struct timeval now;
183
184 gettimeofday(&now, 0);
185 while (i < ARRLEN(timeouts)) {
186 if (timeouts[i].active) {
187 tdiff = TV_DIFF(&timeouts[i].when, &now);
188 if (tdiff <= 0) {
189 timeouts[i].active = false;
190 if (timeouts[i].handler != NULL)
191 timeouts[i].handler();
192 i = tmin = -1;
193 } else if (tmin < 0 || tdiff < tmin) {
194 tmin = tdiff;
195 }
196 }
197 i++;
198 }
199 if (tmin > 0 && t != NULL)
200 TV_SET_MSEC(t, tmin);
201 return tmin > 0;
202}
203
204void load_image(int new) {
205 struct stat fstats;
206
207 if (new < 0 || new >= filecnt)
208 return;
209
210 win_set_cursor(&win, CURSOR_WATCH);
211
212 img_close(&img, false);
213 while (!img_load(&img, &files[new])) {
214 remove_file(new, false);
215 if (new >= filecnt)
216 new = filecnt - 1;
217 }
218
219 files[new].loaded = true;
220 fileidx = new;
221 if (stat(files[new].path, &fstats) == 0)
222 filesize = fstats.st_size;
223 else
224 filesize = 0;
225
226 if (img.multi.cnt > 0 && img.multi.animate)
227 set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
228 else
229 reset_timeout(animate);
230}
231
232void update_info(void) {
233 int i, fw, pw, fi, ln, rn;
234 char frame_info[16];
235 const char *size_unit;
236 float size = filesize;
237
238 pw = 0;
239 for (i = filecnt; i > 0; i /= 10)
240 pw++;
241
242 if (mode == MODE_THUMB) {
243 if (tns.cnt != filecnt) {
244 snprintf(win_bar_l, sizeof win_bar_l, "Loading... %0*d/%d",
245 pw, tns.cnt, filecnt);
246 } else {
247 fi = snprintf(win_bar_l, sizeof win_bar_l, "%0*d/%d%s",
248 pw, tns.sel + 1, filecnt, BAR_SEPARATOR);
249 ln = snprintf(win_bar_l + fi, sizeof win_bar_l - fi, "%s",
250 files[tns.sel].name) + fi;
251 if (win_textwidth(win_bar_l, ln, true) > win.w)
252 snprintf(win_bar_l + fi, sizeof win_bar_l - fi, "%s",
253 files[tns.sel].base);
254 }
255 win_set_title(&win, "sxiv");
256 win_set_bar_info(&win, win_bar_l, NULL);
257 } else {
258 size_readable(&size, &size_unit);
259 if (img.multi.cnt > 0) {
260 fw = 0;
261 for (i = img.multi.cnt; i > 0; i /= 10)
262 fw++;
263 snprintf(frame_info, sizeof frame_info, "%s%0*d/%d",
264 BAR_SEPARATOR, fw, img.multi.sel+1, img.multi.cnt);
265 } else {
266 frame_info[0] = '\0';
267 }
268 fi = snprintf(win_bar_l, sizeof win_bar_l, "%0*d/%d%s",
269 pw, fileidx + 1, filecnt, BAR_SEPARATOR);
270 ln = snprintf(win_bar_l + fi, sizeof win_bar_l - fi, "%s",
271 files[fileidx].name) + fi;
272 rn = snprintf(win_bar_r, sizeof win_bar_r, "%.2f%s%s%dx%d%s%3d%%%s",
273 size, size_unit, BAR_SEPARATOR, img.w, img.h, BAR_SEPARATOR,
274 (int) (img.zoom * 100.0), frame_info);
275
276 if (win_textwidth(win_bar_l, ln, true) +
277 win_textwidth(win_bar_r, rn, true) > win.w)
278 {
279 snprintf(win_bar_l + fi, sizeof win_bar_l - fi, "%s",
280 files[fileidx].base);
281 }
282 win_set_bar_info(&win, win_bar_l, win_bar_r);
283
284 snprintf(win_title, sizeof win_title, "sxiv - %s", files[fileidx].name);
285 win_set_title(&win, win_title);
286 }
287}
288
289void redraw(void) {
290 if (mode == MODE_IMAGE)
291 img_render(&img);
292 else
293 tns_render(&tns);
294 update_info();
295 win_draw(&win);
296 reset_timeout(redraw);
297 reset_cursor();
298}
299
300void reset_cursor(void) {
301 int i;
302 cursor_t cursor = CURSOR_NONE;
303
304 if (mode == MODE_IMAGE) {
305 for (i = 0; i < ARRLEN(timeouts); i++) {
306 if (timeouts[i].handler == reset_cursor) {
307 if (timeouts[i].active)
308 cursor = CURSOR_ARROW;
309 break;
310 }
311 }
312 } else {
313 if (tns.cnt != filecnt)
314 cursor = CURSOR_WATCH;
315 else
316 cursor = CURSOR_ARROW;
317 }
318 win_set_cursor(&win, cursor);
319}
320
321void animate(void) {
322 if (img_frame_animate(&img, false)) {
323 redraw();
324 set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
325 }
326}
327
328void clear_resize(void) {
329 resized = false;
330}
331
332bool keymask(const keymap_t *k, unsigned int state) {
333 return (k->ctrl ? ControlMask : 0) == (state & ControlMask);
334}
335
336bool buttonmask(const button_t *b, unsigned int state) {
337 return ((b->ctrl ? ControlMask : 0) | (b->shift ? ShiftMask : 0)) ==
338 (state & (ControlMask | ShiftMask));
339}
340
341void on_keypress(XKeyEvent *kev) {
342 int i;
343 KeySym ksym;
344 char key;
345
346 if (kev == NULL)
347 return;
348
349 XLookupString(kev, &key, 1, &ksym, NULL);
350
351 if ((ksym == XK_Escape || (key >= '0' && key <= '9')) &&
352 (kev->state & ControlMask) == 0)
353 {
354 /* number prefix for commands */
355 prefix = ksym == XK_Escape ? 0 : prefix * 10 + (int) (key - '0');
356 return;
357 }
358
359 for (i = 0; i < ARRLEN(keys); i++) {
360 if (keys[i].ksym == ksym && keymask(&keys[i], kev->state)) {
361 if (keys[i].cmd != NULL && keys[i].cmd(keys[i].arg))
362 redraw();
363 prefix = 0;
364 return;
365 }
366 }
367}
368
369void on_buttonpress(XButtonEvent *bev) {
370 int i, sel;
371
372 if (bev == NULL)
373 return;
374
375 if (mode == MODE_IMAGE) {
376 win_set_cursor(&win, CURSOR_ARROW);
377 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
378
379 for (i = 0; i < ARRLEN(buttons); i++) {
380 if (buttons[i].button == bev->button &&
381 buttonmask(&buttons[i], bev->state))
382 {
383 if (buttons[i].cmd != NULL && buttons[i].cmd(buttons[i].arg))
384 redraw();
385 return;
386 }
387 }
388 } else {
389 /* thumbnail mode (hard-coded) */
390 switch (bev->button) {
391 case Button1:
392 if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
393 if (sel == tns.sel) {
394 mode = MODE_IMAGE;
395 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
396 load_image(tns.sel);
397 } else {
398 tns_highlight(&tns, tns.sel, false);
399 tns_highlight(&tns, sel, true);
400 tns.sel = sel;
401 }
402 redraw();
403 break;
404 }
405 break;
406 case Button4:
407 case Button5:
408 if (tns_scroll(&tns, bev->button == Button4 ? DIR_UP : DIR_DOWN,
409 (bev->state & ControlMask) != 0))
410 redraw();
411 break;
412 }
413 }
414}
415
416void run(void) {
417 int xfd;
418 fd_set fds;
419 struct timeval timeout;
420 XEvent ev;
421
422 redraw();
423
424 while (true) {
425 while (mode == MODE_THUMB && tns.cnt < filecnt &&
426 XPending(win.env.dpy) == 0)
427 {
428 /* load thumbnails */
429 set_timeout(redraw, TO_REDRAW_THUMBS, false);
430 if (tns_load(&tns, tns.cnt, &files[tns.cnt], false, false)) {
431 tns.cnt++;
432 } else {
433 remove_file(tns.cnt, false);
434 if (tns.sel >= tns.cnt)
435 tns.sel--;
436 }
437 if (tns.cnt == filecnt)
438 redraw();
439 else
440 check_timeouts(NULL);
441 }
442
443 while (XPending(win.env.dpy) == 0 && check_timeouts(&timeout)) {
444 /* wait for timeouts */
445 xfd = ConnectionNumber(win.env.dpy);
446 FD_ZERO(&fds);
447 FD_SET(xfd, &fds);
448 select(xfd + 1, &fds, 0, 0, &timeout);
449 }
450
451 XNextEvent(win.env.dpy, &ev);
452 switch (ev.type) {
453 /* handle events */
454 case ButtonPress:
455 on_buttonpress(&ev.xbutton);
456 break;
457 case ClientMessage:
458 if ((Atom) ev.xclient.data.l[0] == wm_delete_win)
459 return;
460 break;
461 case ConfigureNotify:
462 if (win_configure(&win, &ev.xconfigure)) {
463 if (mode == MODE_IMAGE)
464 img.checkpan = true;
465 else
466 tns.dirty = true;
467 if (!resized || win.fullscreen) {
468 redraw();
469 set_timeout(clear_resize, TO_REDRAW_RESIZE, false);
470 resized = true;
471 } else {
472 set_timeout(redraw, TO_REDRAW_RESIZE, false);
473 }
474 }
475 break;
476 case KeyPress:
477 on_keypress(&ev.xkey);
478 break;
479 case MotionNotify:
480 if (mode == MODE_IMAGE) {
481 win_set_cursor(&win, CURSOR_ARROW);
482 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
483 }
484 break;
485 }
486 }
487}
488
489int fncmp(const void *a, const void *b) {
490 return strcoll(((fileinfo_t*) a)->name, ((fileinfo_t*) b)->name);
491}
492
493int main(int argc, char **argv) {
494 int i, start;
495 size_t n;
496 ssize_t len;
497 char *filename;
498 struct stat fstats;
499 r_dir_t dir;
500
501 parse_options(argc, argv);
502
503 if (options->clean_cache) {
504 tns_init(&tns, 0, NULL);
505 tns_clean_cache(&tns);
506 exit(EXIT_SUCCESS);
507 }
508
509 if (options->filecnt == 0) {
510 print_usage();
511 exit(EXIT_FAILURE);
512 }
513
514 if (options->recursive || options->from_stdin)
515 filecnt = FILENAME_CNT;
516 else
517 filecnt = options->filecnt;
518
519 files = (fileinfo_t*) s_malloc(filecnt * sizeof(fileinfo_t));
520 fileidx = 0;
521
522 /* build file list: */
523 if (options->from_stdin) {
524 filename = NULL;
525 while ((len = get_line(&filename, &n, stdin)) > 0) {
526 if (filename[len-1] == '\n')
527 filename[len-1] = '\0';
528 check_add_file(filename);
529 }
530 if (filename != NULL)
531 free(filename);
532 } else {
533 for (i = 0; i < options->filecnt; i++) {
534 filename = options->filenames[i];
535
536 if (stat(filename, &fstats) < 0) {
537 warn("could not stat file: %s", filename);
538 continue;
539 }
540 if (!S_ISDIR(fstats.st_mode)) {
541 check_add_file(filename);
542 } else {
543 if (!options->recursive) {
544 warn("ignoring directory: %s", filename);
545 continue;
546 }
547 if (r_opendir(&dir, filename) < 0) {
548 warn("could not open directory: %s", filename);
549 continue;
550 }
551 start = fileidx;
552 while ((filename = r_readdir(&dir)) != NULL) {
553 check_add_file(filename);
554 free((void*) filename);
555 }
556 r_closedir(&dir);
557 if (fileidx - start > 1)
558 qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp);
559 }
560 }
561 }
562
563 if (fileidx == 0) {
564 fprintf(stderr, "sxiv: no valid image file given, aborting\n");
565 exit(EXIT_FAILURE);
566 }
567
568 filecnt = fileidx;
569 fileidx = options->startnum < filecnt ? options->startnum : 0;
570
571 win_init(&win);
572 img_init(&img, &win);
573
574 if (options->thumb_mode) {
575 mode = MODE_THUMB;
576 tns_init(&tns, filecnt, &win);
577 while (!tns_load(&tns, 0, &files[0], false, false))
578 remove_file(0, false);
579 tns.cnt = 1;
580 } else {
581 mode = MODE_IMAGE;
582 tns.thumbs = NULL;
583 load_image(fileidx);
584 }
585
586 win_open(&win);
587
588 run();
589 cleanup();
590
591 return 0;
592}