Krolyxon
Commit 2d4264a

Branches

Refs →

nsxivmain.c

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