Krolyxon
Commit ed2c9f7

Branches

Refs →

nsxivmain.c

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