Krolyxon
Commit 255dd5b

Branches

Refs →

nsxivmain.c

557 lines · 11.8 KB · C

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