Krolyxon
Commit e2d4b9c

Branches

Refs →

nsxivmain.c

560 lines · 11.7 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
21#include <stdlib.h>
22#include <stdio.h>
23#include <string.h>
24#include <unistd.h>
25#include <sys/stat.h>
26#include <sys/time.h>
27#include <X11/Xutil.h>
28#include <X11/keysym.h>
29
30#include "commands.h"
31#include "image.h"
32#include "options.h"
33#include "thumbs.h"
34#include "types.h"
35#include "util.h"
36#include "window.h"
37
38#define _MAPPINGS_CONFIG
39#include "config.h"
40
41enum {
42 TITLE_LEN = 256,
43 FNAME_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();
54void reset_cursor();
55void animate();
56void slideshow();
57
58appmode_t mode;
59img_t img;
60tns_t tns;
61win_t win;
62
63fileinfo_t *files;
64int filecnt, fileidx;
65size_t filesize;
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 { { 0, 0 }, False, slideshow },
74};
75
76void cleanup() {
77 static int in = 0;
78
79 if (!in++) {
80 img_close(&img, 0);
81 tns_free(&tns);
82 win_close(&win);
83 }
84}
85
86void check_add_file(char *filename) {
87 if (!filename || !*filename)
88 return;
89
90 if (access(filename, R_OK)) {
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) {
102 warn("could not get absolute path of file: %s\n", filename);
103 return;
104 }
105 }
106 files[fileidx].loaded = 0;
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, unsigned char silent) {
114 if (n < 0 || n >= filecnt)
115 return;
116
117 if (filecnt == 1) {
118 if (!silent)
119 fprintf(stderr, "sxiv: no more files to display, aborting\n");
120 cleanup();
121 exit(!silent);
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, int 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 MSEC_ADD_TO_TIMEVAL(time, &timeouts[i].when);
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
167int 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 = TIMEDIFF(&timeouts[i].when, &now);
175 if (tdiff <= 0) {
176 timeouts[i].active = False;
177 if (timeouts[i].handler)
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)
187 MSEC_TO_TIMEVAL(tmin, t);
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, 0);
200 while (!img_load(&img, &files[new])) {
201 remove_file(new, 0);
202 if (new >= filecnt)
203 new = filecnt - 1;
204 }
205
206 files[new].loaded = 1;
207 fileidx = new;
208 if (!stat(files[new].path, &fstats))
209 filesize = fstats.st_size;
210 else
211 filesize = 0;
212
213 if (img.multi.cnt && img.multi.animate)
214 set_timeout(animate, img.multi.frames[img.multi.sel].delay, 1);
215 else
216 reset_timeout(animate);
217}
218
219void update_title() {
220 int n;
221 char sshow_info[16];
222 char frame_info[16];
223 float size, time;
224 const char *size_unit, *time_unit;
225
226 if (mode == MODE_THUMB) {
227 n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] %s",
228 tns.cnt ? tns.sel + 1 : 0, tns.cnt,
229 tns.cnt ? files[tns.sel].name : "");
230 } else {
231 size = filesize;
232 size_readable(&size, &size_unit);
233
234 if (img.slideshow) {
235 time = img.ss_delay / 1000.0;
236 time_readable(&time, &time_unit);
237 snprintf(sshow_info, sizeof(sshow_info), "*%d%s* ",
238 (int) time, time_unit);
239 } else {
240 sshow_info[0] = '\0';
241 }
242 if (img.multi.cnt)
243 snprintf(frame_info, sizeof(frame_info), "{%d/%d} ",
244 img.multi.sel + 1, img.multi.cnt);
245 else
246 frame_info[0] = '\0';
247
248 n = snprintf(win_title, TITLE_LEN,
249 "sxiv: [%d/%d] <%dx%d:%d%%> (%.2f%s) %s%s%s",
250 fileidx + 1, filecnt, img.w, img.h,
251 (int) (img.zoom * 100.0), size, size_unit,
252 sshow_info, frame_info, files[fileidx].name);
253 }
254
255 if (n >= TITLE_LEN) {
256 for (n = 0; n < 3; n++)
257 win_title[TITLE_LEN - n - 2] = '.';
258 }
259
260 win_set_title(&win, win_title);
261}
262
263void redraw() {
264 if (mode == MODE_IMAGE) {
265 img_render(&img, &win);
266 if (img.slideshow && !img.multi.animate) {
267 if (fileidx + 1 < filecnt)
268 set_timeout(slideshow, img.ss_delay, 1);
269 else
270 img.slideshow = 0;
271 }
272 } else {
273 tns_render(&tns, &win);
274 }
275 update_title();
276 reset_timeout(redraw);
277 reset_cursor();
278}
279
280void reset_cursor() {
281 int i;
282 cursor_t cursor = CURSOR_NONE;
283
284 if (mode == MODE_IMAGE) {
285 for (i = 0; i < ARRLEN(timeouts); i++) {
286 if (timeouts[i].handler == reset_cursor) {
287 if (timeouts[i].active)
288 cursor = CURSOR_ARROW;
289 break;
290 }
291 }
292 } else {
293 if (tns.cnt != filecnt)
294 cursor = CURSOR_WATCH;
295 else
296 cursor = CURSOR_ARROW;
297 }
298 win_set_cursor(&win, cursor);
299}
300
301void animate() {
302 int delay;
303
304 delay = img_frame_animate(&img, 0);
305 redraw();
306 if (delay)
307 set_timeout(animate, delay, 1);
308}
309
310void slideshow() {
311 if (mode == MODE_IMAGE && !img.multi.animate) {
312 if (fileidx + 1 < filecnt) {
313 load_image(fileidx + 1);
314 redraw();
315 } else {
316 img.slideshow = 0;
317 }
318 }
319}
320
321Bool keymask(const keymap_t *k, unsigned int state) {
322 return (k->ctrl ? ControlMask : 0) == (state & ControlMask);
323}
324
325Bool buttonmask(const button_t *b, unsigned int state) {
326 return ((b->ctrl ? ControlMask : 0) | (b->shift ? ShiftMask : 0)) ==
327 (state & (ControlMask | ShiftMask));
328}
329
330void on_keypress(XKeyEvent *kev) {
331 int i;
332 KeySym ksym;
333 char key;
334
335 if (!kev)
336 return;
337
338 XLookupString(kev, &key, 1, &ksym, NULL);
339
340 for (i = 0; i < ARRLEN(keys); i++) {
341 if (keys[i].ksym == ksym && keymask(&keys[i], kev->state)) {
342 if (keys[i].cmd && keys[i].cmd(keys[i].arg))
343 redraw();
344 return;
345 }
346 }
347}
348
349void on_buttonpress(XButtonEvent *bev) {
350 int i, sel;
351
352 if (!bev)
353 return;
354
355 if (mode == MODE_IMAGE) {
356 win_set_cursor(&win, CURSOR_ARROW);
357 set_timeout(reset_cursor, TO_CURSOR_HIDE, 1);
358
359 for (i = 0; i < ARRLEN(buttons); i++) {
360 if (buttons[i].button == bev->button &&
361 buttonmask(&buttons[i], bev->state))
362 {
363 if (buttons[i].cmd && buttons[i].cmd(buttons[i].arg))
364 redraw();
365 return;
366 }
367 }
368 } else {
369 /* thumbnail mode (hard-coded) */
370 switch (bev->button) {
371 case Button1:
372 if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
373 if (sel == tns.sel) {
374 mode = MODE_IMAGE;
375 set_timeout(reset_cursor, TO_CURSOR_HIDE, 1);
376 load_image(tns.sel);
377 } else {
378 tns_highlight(&tns, &win, tns.sel, False);
379 tns_highlight(&tns, &win, sel, True);
380 tns.sel = sel;
381 }
382 redraw();
383 break;
384 }
385 break;
386 case Button4:
387 case Button5:
388 if (tns_scroll(&tns, bev->button == Button4 ? DIR_UP : DIR_DOWN))
389 redraw();
390 break;
391 }
392 }
393}
394
395void run() {
396 int xfd;
397 fd_set fds;
398 struct timeval timeout;
399 XEvent ev;
400
401 redraw();
402
403 while (1) {
404 while (mode == MODE_THUMB && tns.cnt < filecnt &&
405 !XPending(win.env.dpy))
406 {
407 /* load thumbnails */
408 set_timeout(redraw, TO_REDRAW_THUMBS, 0);
409 if (tns_load(&tns, tns.cnt, &files[tns.cnt], False, False))
410 tns.cnt++;
411 else
412 remove_file(tns.cnt, 0);
413 if (tns.cnt == filecnt)
414 redraw();
415 else
416 check_timeouts(NULL);
417 }
418
419 while (!XPending(win.env.dpy) && check_timeouts(&timeout)) {
420 /* wait for timeouts */
421 xfd = ConnectionNumber(win.env.dpy);
422 FD_ZERO(&fds);
423 FD_SET(xfd, &fds);
424 select(xfd + 1, &fds, 0, 0, &timeout);
425 }
426
427 if (!XNextEvent(win.env.dpy, &ev)) {
428 /* handle events */
429 switch (ev.type) {
430 case ButtonPress:
431 on_buttonpress(&ev.xbutton);
432 break;
433 case ClientMessage:
434 if ((Atom) ev.xclient.data.l[0] == wm_delete_win)
435 return;
436 break;
437 case ConfigureNotify:
438 if (win_configure(&win, &ev.xconfigure)) {
439 set_timeout(redraw, TO_REDRAW_RESIZE, 0);
440 if (mode == MODE_IMAGE)
441 img.checkpan = 1;
442 else
443 tns.dirty = 1;
444 }
445 break;
446 case KeyPress:
447 on_keypress(&ev.xkey);
448 break;
449 case MotionNotify:
450 if (mode == MODE_IMAGE) {
451 win_set_cursor(&win, CURSOR_ARROW);
452 set_timeout(reset_cursor, TO_CURSOR_HIDE, 1);
453 }
454 break;
455 }
456 }
457 }
458}
459
460int fncmp(const void *a, const void *b) {
461 return strcoll(((fileinfo_t*) a)->name, ((fileinfo_t*) b)->name);
462}
463
464int main(int argc, char **argv) {
465 int i, start;
466 size_t n;
467 ssize_t len;
468 char *filename;
469 struct stat fstats;
470 r_dir_t dir;
471
472 parse_options(argc, argv);
473
474 if (options->clean_cache) {
475 tns_init(&tns, 0);
476 tns_clean_cache(&tns);
477 exit(0);
478 }
479
480 if (!options->filecnt) {
481 print_usage();
482 exit(1);
483 }
484
485 if (options->recursive || options->from_stdin)
486 filecnt = FNAME_CNT;
487 else
488 filecnt = options->filecnt;
489
490 files = (fileinfo_t*) s_malloc(filecnt * sizeof(fileinfo_t));
491 fileidx = 0;
492
493 /* build file list: */
494 if (options->from_stdin) {
495 filename = NULL;
496 while ((len = get_line(&filename, &n, stdin)) > 0) {
497 if (filename[len-1] == '\n')
498 filename[len-1] = '\0';
499 check_add_file(filename);
500 }
501 if (filename)
502 free(filename);
503 } else {
504 for (i = 0; i < options->filecnt; i++) {
505 filename = options->filenames[i];
506
507 if (stat(filename, &fstats) || !S_ISDIR(fstats.st_mode)) {
508 check_add_file(filename);
509 } else {
510 if (!options->recursive) {
511 warn("ignoring directory: %s", filename);
512 continue;
513 }
514 if (r_opendir(&dir, filename)) {
515 warn("could not open directory: %s", filename);
516 continue;
517 }
518 start = fileidx;
519 printf("reading dir: %s\n", filename);
520 while ((filename = r_readdir(&dir))) {
521 check_add_file(filename);
522 free((void*) filename);
523 }
524 r_closedir(&dir);
525 if (fileidx - start > 1)
526 qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp);
527 }
528 }
529 }
530
531 if (!fileidx) {
532 fprintf(stderr, "sxiv: no valid image file given, aborting\n");
533 exit(1);
534 }
535
536 filecnt = fileidx;
537 fileidx = options->startnum < filecnt ? options->startnum : 0;
538
539 win_init(&win);
540 img_init(&img, &win);
541
542 if (options->thumbnails) {
543 mode = MODE_THUMB;
544 tns_init(&tns, filecnt);
545 while (!tns_load(&tns, 0, &files[0], False, False))
546 remove_file(0, 0);
547 tns.cnt = 1;
548 } else {
549 mode = MODE_IMAGE;
550 tns.thumbs = NULL;
551 load_image(fileidx);
552 }
553
554 win_open(&win);
555
556 run();
557 cleanup();
558
559 return 0;
560}