Krolyxon
Commit 691c6d7

Branches

Refs →

nsxivmain.c

555 lines · 12.1 KB · C

1/* sxiv: main.c
2 * Copyright (c) 2011 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 snprintf(win_bar_l, sizeof win_bar_l, "%0*d/%d%s%s",
235 pw, tns.cnt > 0 ? tns.sel + 1 : 0, filecnt, BAR_SEPARATOR,
236 tns.cnt > 0 ? files[tns.sel].base : "");
237 win_bar_r[0] = '\0';
238 snprintf(win_title, sizeof win_title, "sxiv");
239 } else {
240 size_readable(&size, &size_unit);
241 if (img.multi.cnt > 0) {
242 fw = 0;
243 for (i = img.multi.cnt; i > 0; i /= 10)
244 fw++;
245 snprintf(frame_info, sizeof frame_info, "%s%0*d/%d",
246 BAR_SEPARATOR, fw, img.multi.sel + 1, img.multi.cnt);
247 } else {
248 frame_info[0] = '\0';
249 }
250 snprintf(win_bar_l, sizeof win_bar_l, "%0*d/%d%s%s",
251 pw, fileidx + 1, filecnt, BAR_SEPARATOR, files[fileidx].base);
252 snprintf(win_bar_r, sizeof win_bar_r, "%.2f%s%s%dx%d%s%3d%%%s",
253 size, size_unit, BAR_SEPARATOR, img.w, img.h, BAR_SEPARATOR,
254 (int) (img.zoom * 100.0), frame_info);
255 snprintf(win_title, sizeof win_title, "sxiv - %s",
256 files[fileidx].name);
257 }
258 win_set_title(&win, win_title);
259 win_set_bar_info(&win, win_bar_l, win_bar_r);
260}
261
262void redraw(void) {
263 if (mode == MODE_IMAGE)
264 img_render(&img);
265 else
266 tns_render(&tns);
267 update_info();
268 win_draw(&win);
269 reset_timeout(redraw);
270 reset_cursor();
271}
272
273void reset_cursor(void) {
274 int i;
275 cursor_t cursor = CURSOR_NONE;
276
277 if (mode == MODE_IMAGE) {
278 for (i = 0; i < ARRLEN(timeouts); i++) {
279 if (timeouts[i].handler == reset_cursor) {
280 if (timeouts[i].active)
281 cursor = CURSOR_ARROW;
282 break;
283 }
284 }
285 } else {
286 if (tns.cnt != filecnt)
287 cursor = CURSOR_WATCH;
288 else
289 cursor = CURSOR_ARROW;
290 }
291 win_set_cursor(&win, cursor);
292}
293
294void animate(void) {
295 if (img_frame_animate(&img, false)) {
296 redraw();
297 set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
298 }
299}
300
301bool keymask(const keymap_t *k, unsigned int state) {
302 return (k->ctrl ? ControlMask : 0) == (state & ControlMask);
303}
304
305bool buttonmask(const button_t *b, unsigned int state) {
306 return ((b->ctrl ? ControlMask : 0) | (b->shift ? ShiftMask : 0)) ==
307 (state & (ControlMask | ShiftMask));
308}
309
310void on_keypress(XKeyEvent *kev) {
311 int i;
312 KeySym ksym;
313 char key;
314
315 if (kev == NULL)
316 return;
317
318 XLookupString(kev, &key, 1, &ksym, NULL);
319
320 if ((ksym == XK_Escape || (key >= '0' && key <= '9')) &&
321 (kev->state & ControlMask) == 0)
322 {
323 /* number prefix for commands */
324 prefix = ksym == XK_Escape ? 0 : prefix * 10 + (int) (key - '0');
325 return;
326 }
327
328 for (i = 0; i < ARRLEN(keys); i++) {
329 if (keys[i].ksym == ksym && keymask(&keys[i], kev->state)) {
330 if (keys[i].cmd != NULL && keys[i].cmd(keys[i].arg))
331 redraw();
332 prefix = 0;
333 return;
334 }
335 }
336}
337
338void on_buttonpress(XButtonEvent *bev) {
339 int i, sel;
340
341 if (bev == NULL)
342 return;
343
344 if (mode == MODE_IMAGE) {
345 win_set_cursor(&win, CURSOR_ARROW);
346 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
347
348 for (i = 0; i < ARRLEN(buttons); i++) {
349 if (buttons[i].button == bev->button &&
350 buttonmask(&buttons[i], bev->state))
351 {
352 if (buttons[i].cmd != NULL && buttons[i].cmd(buttons[i].arg))
353 redraw();
354 return;
355 }
356 }
357 } else {
358 /* thumbnail mode (hard-coded) */
359 switch (bev->button) {
360 case Button1:
361 if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
362 if (sel == tns.sel) {
363 mode = MODE_IMAGE;
364 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
365 load_image(tns.sel);
366 } else {
367 tns_highlight(&tns, tns.sel, false);
368 tns_highlight(&tns, sel, true);
369 tns.sel = sel;
370 }
371 redraw();
372 break;
373 }
374 break;
375 case Button4:
376 case Button5:
377 if (tns_scroll(&tns, bev->button == Button4 ? DIR_UP : DIR_DOWN,
378 (bev->state & ControlMask) != 0))
379 redraw();
380 break;
381 }
382 }
383}
384
385void run(void) {
386 int xfd;
387 fd_set fds;
388 struct timeval timeout;
389 XEvent ev;
390
391 redraw();
392
393 while (true) {
394 while (mode == MODE_THUMB && tns.cnt < filecnt &&
395 XPending(win.env.dpy) == 0)
396 {
397 /* load thumbnails */
398 set_timeout(redraw, TO_REDRAW_THUMBS, false);
399 if (tns_load(&tns, tns.cnt, &files[tns.cnt], false, false)) {
400 tns.cnt++;
401 } else {
402 remove_file(tns.cnt, false);
403 if (tns.sel >= tns.cnt)
404 tns.sel--;
405 }
406 if (tns.cnt == filecnt)
407 redraw();
408 else
409 check_timeouts(NULL);
410 }
411
412 while (XPending(win.env.dpy) == 0 && check_timeouts(&timeout)) {
413 /* wait for timeouts */
414 xfd = ConnectionNumber(win.env.dpy);
415 FD_ZERO(&fds);
416 FD_SET(xfd, &fds);
417 select(xfd + 1, &fds, 0, 0, &timeout);
418 }
419
420 XNextEvent(win.env.dpy, &ev);
421 switch (ev.type) {
422 /* handle events */
423 case ButtonPress:
424 on_buttonpress(&ev.xbutton);
425 break;
426 case ClientMessage:
427 if ((Atom) ev.xclient.data.l[0] == wm_delete_win)
428 return;
429 break;
430 case ConfigureNotify:
431 if (win_configure(&win, &ev.xconfigure)) {
432 set_timeout(redraw, TO_REDRAW_RESIZE, false);
433 if (mode == MODE_IMAGE)
434 img.checkpan = true;
435 else
436 tns.dirty = true;
437 }
438 break;
439 case KeyPress:
440 on_keypress(&ev.xkey);
441 break;
442 case MotionNotify:
443 if (mode == MODE_IMAGE) {
444 win_set_cursor(&win, CURSOR_ARROW);
445 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
446 }
447 break;
448 }
449 }
450}
451
452int fncmp(const void *a, const void *b) {
453 return strcoll(((fileinfo_t*) a)->name, ((fileinfo_t*) b)->name);
454}
455
456int main(int argc, char **argv) {
457 int i, start;
458 size_t n;
459 ssize_t len;
460 char *filename;
461 struct stat fstats;
462 r_dir_t dir;
463
464 parse_options(argc, argv);
465
466 if (options->clean_cache) {
467 tns_init(&tns, 0, NULL);
468 tns_clean_cache(&tns);
469 exit(EXIT_SUCCESS);
470 }
471
472 if (options->filecnt == 0) {
473 print_usage();
474 exit(EXIT_FAILURE);
475 }
476
477 if (options->recursive || options->from_stdin)
478 filecnt = FILENAME_CNT;
479 else
480 filecnt = options->filecnt;
481
482 files = (fileinfo_t*) s_malloc(filecnt * sizeof(fileinfo_t));
483 fileidx = 0;
484
485 /* build file list: */
486 if (options->from_stdin) {
487 filename = NULL;
488 while ((len = get_line(&filename, &n, stdin)) > 0) {
489 if (filename[len-1] == '\n')
490 filename[len-1] = '\0';
491 check_add_file(filename);
492 }
493 if (filename != NULL)
494 free(filename);
495 } else {
496 for (i = 0; i < options->filecnt; i++) {
497 filename = options->filenames[i];
498
499 if (stat(filename, &fstats) < 0) {
500 warn("could not stat file: %s", filename);
501 continue;
502 }
503 if (!S_ISDIR(fstats.st_mode)) {
504 check_add_file(filename);
505 } else {
506 if (!options->recursive) {
507 warn("ignoring directory: %s", filename);
508 continue;
509 }
510 if (r_opendir(&dir, filename) < 0) {
511 warn("could not open directory: %s", filename);
512 continue;
513 }
514 start = fileidx;
515 while ((filename = r_readdir(&dir)) != NULL) {
516 check_add_file(filename);
517 free((void*) filename);
518 }
519 r_closedir(&dir);
520 if (fileidx - start > 1)
521 qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp);
522 }
523 }
524 }
525
526 if (fileidx == 0) {
527 fprintf(stderr, "sxiv: no valid image file given, aborting\n");
528 exit(EXIT_FAILURE);
529 }
530
531 filecnt = fileidx;
532 fileidx = options->startnum < filecnt ? options->startnum : 0;
533
534 win_init(&win);
535 img_init(&img, &win);
536
537 if (options->thumb_mode) {
538 mode = MODE_THUMB;
539 tns_init(&tns, filecnt, &win);
540 while (!tns_load(&tns, 0, &files[0], false, false))
541 remove_file(0, false);
542 tns.cnt = 1;
543 } else {
544 mode = MODE_IMAGE;
545 tns.thumbs = NULL;
546 load_image(fileidx);
547 }
548
549 win_open(&win);
550
551 run();
552 cleanup();
553
554 return 0;
555}