Krolyxon
Commit 751ebb3

Branches

Refs →

nsxivmain.c

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