Krolyxon
Commit c3c95ab

Branches

Refs →

nsxivmain.c

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