Krolyxon
Commit dcd04e5

Branches

Refs →

nsxivmain.c

517 lines · 11.0 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
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301, USA.
17 */
18
19#include <stdlib.h>
20#include <stdio.h>
21#include <string.h>
22#include <unistd.h>
23#include <sys/stat.h>
24#include <sys/time.h>
25#include <X11/Xutil.h>
26#include <X11/keysym.h>
27
28#include "commands.h"
29#include "image.h"
30#include "options.h"
31#include "thumbs.h"
32#include "types.h"
33#include "util.h"
34#include "window.h"
35
36#define _MAPPINGS_CONFIG
37#include "config.h"
38
39enum {
40 TITLE_LEN = 256,
41 FNAME_CNT = 1024
42};
43
44typedef struct {
45 struct timeval when;
46 Bool active;
47 timeout_f handler;
48} timeout_t;
49
50/* timeout handler functions: */
51void redraw();
52void hide_cursor();
53void animate();
54
55appmode_t mode;
56img_t img;
57tns_t tns;
58win_t win;
59
60fileinfo_t *files;
61int filecnt, fileidx;
62size_t filesize;
63
64char win_title[TITLE_LEN];
65
66timeout_t timeouts[] = {
67 { { 0, 0 }, False, redraw },
68 { { 0, 0 }, False, hide_cursor },
69 { { 0, 0 }, False, animate }
70};
71
72void cleanup() {
73 static int in = 0;
74
75 if (!in++) {
76 img_close(&img, 0);
77 tns_free(&tns);
78 win_close(&win);
79 }
80}
81
82void check_add_file(char *filename) {
83 if (!filename || !*filename)
84 return;
85
86 if (access(filename, R_OK)) {
87 warn("could not open file: %s", filename);
88 return;
89 }
90
91 if (fileidx == filecnt) {
92 filecnt *= 2;
93 files = (fileinfo_t*) s_realloc(files, filecnt * sizeof(fileinfo_t));
94 }
95 if (*filename != '/') {
96 files[fileidx].path = absolute_path(filename);
97 if (!files[fileidx].path) {
98 warn("could not get absolute path of file: %s\n", filename);
99 return;
100 }
101 }
102 files[fileidx].name = s_strdup(filename);
103 if (*filename == '/')
104 files[fileidx].path = files[fileidx].name;
105 fileidx++;
106}
107
108void remove_file(int n, unsigned char silent) {
109 if (n < 0 || n >= filecnt)
110 return;
111
112 if (filecnt == 1) {
113 if (!silent)
114 fprintf(stderr, "sxiv: no more files to display, aborting\n");
115 cleanup();
116 exit(!silent);
117 }
118
119 if (n + 1 < filecnt) {
120 if (files[n].path != files[n].name)
121 free((void*) files[n].path);
122 free((void*) files[n].name);
123 memmove(files + n, files + n + 1, (filecnt - n - 1) * sizeof(fileinfo_t));
124 }
125 if (n + 1 < tns.cnt) {
126 memmove(tns.thumbs + n, tns.thumbs + n + 1, (tns.cnt - n - 1) *
127 sizeof(thumb_t));
128 memset(tns.thumbs + tns.cnt - 1, 0, sizeof(thumb_t));
129 }
130
131 filecnt--;
132 if (n < tns.cnt)
133 tns.cnt--;
134}
135
136void set_timeout(timeout_f handler, int time, int overwrite) {
137 int i;
138
139 for (i = 0; i < LEN(timeouts); i++) {
140 if (timeouts[i].handler == handler) {
141 if (!timeouts[i].active || overwrite) {
142 gettimeofday(&timeouts[i].when, 0);
143 MSEC_ADD_TO_TIMEVAL(time, &timeouts[i].when);
144 timeouts[i].active = True;
145 }
146 return;
147 }
148 }
149}
150
151void reset_timeout(timeout_f handler) {
152 int i;
153
154 for (i = 0; i < LEN(timeouts); i++) {
155 if (timeouts[i].handler == handler) {
156 timeouts[i].active = False;
157 return;
158 }
159 }
160}
161
162int check_timeouts(struct timeval *t) {
163 int i, tdiff, tmin = -1;
164 struct timeval now;
165
166 gettimeofday(&now, 0);
167 for (i = 0; i < LEN(timeouts); i++) {
168 if (timeouts[i].active) {
169 tdiff = TIMEDIFF(&timeouts[i].when, &now);
170 if (tdiff <= 0) {
171 timeouts[i].active = False;
172 if (timeouts[i].handler)
173 timeouts[i].handler();
174 } else if (tmin < 0 || tdiff < tmin) {
175 tmin = tdiff;
176 }
177 }
178 }
179 if (tmin > 0 && t)
180 MSEC_TO_TIMEVAL(tmin, t);
181 return tmin > 0;
182}
183
184void load_image(int new) {
185 struct stat fstats;
186
187 if (new < 0 || new >= filecnt)
188 return;
189
190 /* cursor gets reset in redraw() */
191 win_set_cursor(&win, CURSOR_WATCH);
192 img_close(&img, 0);
193
194 while (!img_load(&img, &files[new])) {
195 remove_file(new, 0);
196 if (new >= filecnt)
197 new = filecnt - 1;
198 }
199
200 fileidx = new;
201 if (!stat(files[new].path, &fstats))
202 filesize = fstats.st_size;
203 else
204 filesize = 0;
205
206 if (img.multi.cnt) {
207 if (img.multi.animate)
208 set_timeout(animate, img.multi.frames[img.multi.sel].delay, 1);
209 else
210 reset_timeout(animate);
211 }
212}
213
214void update_title() {
215 int n;
216 float size;
217 const char *unit;
218
219 if (mode == MODE_THUMB) {
220 n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] %s",
221 tns.cnt ? tns.sel + 1 : 0, tns.cnt,
222 tns.cnt ? files[tns.sel].name : "");
223 } else {
224 size = filesize;
225 size_readable(&size, &unit);
226 if (img.multi.cnt)
227 n = snprintf(win_title, TITLE_LEN,
228 "sxiv: [%d/%d] <%d%%> <%dx%d> (%.2f%s) {%d/%d} %s",
229 fileidx + 1, filecnt, (int) (img.zoom * 100.0), img.w,
230 img.h, size, unit, img.multi.sel + 1, img.multi.cnt,
231 files[fileidx].name);
232 else
233 n = snprintf(win_title, TITLE_LEN,
234 "sxiv: [%d/%d] <%d%%> <%dx%d> (%.2f%s) %s",
235 fileidx + 1, filecnt, (int) (img.zoom * 100.0), img.w,
236 img.h, size, unit, files[fileidx].name);
237 }
238
239 if (n >= TITLE_LEN) {
240 for (n = 0; n < 3; n++)
241 win_title[TITLE_LEN - n - 2] = '.';
242 }
243
244 win_set_title(&win, win_title);
245}
246
247void redraw() {
248 if (mode == MODE_IMAGE) {
249 img_render(&img, &win);
250 if (img.multi.animate) {
251 win_set_cursor(&win, CURSOR_NONE);
252 } else {
253 win_set_cursor(&win, CURSOR_ARROW);
254 set_timeout(hide_cursor, TO_CURSOR_HIDE, 1);
255 }
256 } else {
257 tns_render(&tns, &win);
258 }
259 update_title();
260 reset_timeout(redraw);
261}
262
263void hide_cursor() {
264 win_set_cursor(&win, CURSOR_NONE);
265}
266
267void animate() {
268 int delay;
269
270 delay = img_frame_animate(&img, 0);
271 if (delay) {
272 set_timeout(animate, delay, 1);
273 redraw();
274 }
275}
276
277Bool keymask(const keymap_t *k, unsigned int state) {
278 return (k->ctrl ? ControlMask : 0) == (state & ControlMask);
279}
280
281Bool buttonmask(const button_t *b, unsigned int state) {
282 return ((b->ctrl ? ControlMask : 0) | (b->shift ? ShiftMask : 0)) ==
283 (state & (ControlMask | ShiftMask));
284}
285
286void on_keypress(XKeyEvent *kev) {
287 int i;
288 KeySym ksym;
289 char key;
290
291 if (!kev)
292 return;
293
294 XLookupString(kev, &key, 1, &ksym, NULL);
295
296 for (i = 0; i < LEN(keys); i++) {
297 if (keys[i].ksym == ksym && keymask(&keys[i], kev->state)) {
298 if (keys[i].cmd && keys[i].cmd(keys[i].arg))
299 redraw();
300 return;
301 }
302 }
303}
304
305void on_buttonpress(XButtonEvent *bev) {
306 int i, sel;
307
308 if (!bev)
309 return;
310
311 if (mode == MODE_IMAGE) {
312 win_set_cursor(&win, CURSOR_ARROW);
313 set_timeout(hide_cursor, TO_CURSOR_HIDE, 1);
314
315 for (i = 0; i < LEN(buttons); i++) {
316 if (buttons[i].button == bev->button &&
317 buttonmask(&buttons[i], bev->state))
318 {
319 if (buttons[i].cmd && buttons[i].cmd(buttons[i].arg))
320 redraw();
321 return;
322 }
323 }
324 } else {
325 /* thumbnail mode (hard-coded) */
326 switch (bev->button) {
327 case Button1:
328 if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
329 if (sel == tns.sel) {
330 load_image(tns.sel);
331 mode = MODE_IMAGE;
332 set_timeout(hide_cursor, TO_CURSOR_HIDE, 1);
333 } else {
334 tns_highlight(&tns, &win, tns.sel, False);
335 tns_highlight(&tns, &win, sel, True);
336 tns.sel = sel;
337 }
338 redraw();
339 break;
340 }
341 break;
342 case Button4:
343 case Button5:
344 if (tns_scroll(&tns, bev->button == Button4 ? DIR_UP : DIR_DOWN))
345 redraw();
346 break;
347 }
348 }
349}
350
351void run() {
352 int xfd;
353 fd_set fds;
354 struct timeval timeout;
355 XEvent ev;
356
357 redraw();
358
359 while (1) {
360 while (mode == MODE_THUMB && tns.cnt < filecnt &&
361 !XPending(win.env.dpy))
362 {
363 /* load thumbnails */
364 win_set_cursor(&win, CURSOR_WATCH);
365 set_timeout(redraw, TO_REDRAW_THUMBS, 0);
366 if (tns_load(&tns, tns.cnt, &files[tns.cnt], False, False))
367 tns.cnt++;
368 else
369 remove_file(tns.cnt, 0);
370 if (tns.cnt == filecnt) {
371 redraw();
372 win_set_cursor(&win, CURSOR_ARROW);
373 } else {
374 check_timeouts(NULL);
375 }
376 }
377
378 if (!XPending(win.env.dpy) && check_timeouts(&timeout)) {
379 /* handle timeouts */
380 xfd = ConnectionNumber(win.env.dpy);
381 FD_ZERO(&fds);
382 FD_SET(xfd, &fds);
383 if (!select(xfd + 1, &fds, 0, 0, &timeout))
384 check_timeouts(NULL);
385 }
386
387 if (!XNextEvent(win.env.dpy, &ev)) {
388 /* handle events */
389 switch (ev.type) {
390 case ButtonPress:
391 on_buttonpress(&ev.xbutton);
392 break;
393 case ClientMessage:
394 if ((Atom) ev.xclient.data.l[0] == wm_delete_win)
395 return;
396 break;
397 case ConfigureNotify:
398 if (win_configure(&win, &ev.xconfigure)) {
399 set_timeout(redraw, TO_REDRAW_RESIZE, 0);
400 if (mode == MODE_IMAGE)
401 img.checkpan = 1;
402 else
403 tns.dirty = 1;
404 }
405 break;
406 case KeyPress:
407 on_keypress(&ev.xkey);
408 break;
409 case MotionNotify:
410 if (mode == MODE_IMAGE) {
411 win_set_cursor(&win, CURSOR_ARROW);
412 set_timeout(hide_cursor, TO_CURSOR_HIDE, 1);
413 }
414 break;
415 }
416 }
417 }
418}
419
420int fncmp(const void *a, const void *b) {
421 return strcoll(((fileinfo_t*) a)->name, ((fileinfo_t*) b)->name);
422}
423
424int main(int argc, char **argv) {
425 int i, len, start;
426 size_t n;
427 char *filename;
428 struct stat fstats;
429 r_dir_t dir;
430
431 parse_options(argc, argv);
432
433 if (options->clean_cache) {
434 tns_init(&tns, 0);
435 tns_clean_cache(&tns);
436 exit(0);
437 }
438
439 if (!options->filecnt) {
440 print_usage();
441 exit(1);
442 }
443
444 if (options->recursive || options->from_stdin)
445 filecnt = FNAME_CNT;
446 else
447 filecnt = options->filecnt;
448
449 files = (fileinfo_t*) s_malloc(filecnt * sizeof(fileinfo_t));
450 fileidx = 0;
451
452 /* build file list: */
453 if (options->from_stdin) {
454 filename = NULL;
455 while ((len = getline(&filename, &n, stdin)) > 0) {
456 if (filename[len-1] == '\n')
457 filename[len-1] = '\0';
458 check_add_file(filename);
459 }
460 } else {
461 for (i = 0; i < options->filecnt; i++) {
462 filename = options->filenames[i];
463
464 if (stat(filename, &fstats) || !S_ISDIR(fstats.st_mode)) {
465 check_add_file(filename);
466 } else {
467 if (!options->recursive) {
468 warn("ignoring directory: %s", filename);
469 continue;
470 }
471 if (r_opendir(&dir, filename)) {
472 warn("could not open directory: %s", filename);
473 continue;
474 }
475 start = fileidx;
476 printf("reading dir: %s\n", filename);
477 while ((filename = r_readdir(&dir))) {
478 check_add_file(filename);
479 free((void*) filename);
480 }
481 r_closedir(&dir);
482 if (fileidx - start > 1)
483 qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp);
484 }
485 }
486 }
487
488 if (!fileidx) {
489 fprintf(stderr, "sxiv: no valid image file given, aborting\n");
490 exit(1);
491 }
492
493 filecnt = fileidx;
494 fileidx = options->startnum < filecnt ? options->startnum : 0;
495
496 win_init(&win);
497 img_init(&img, &win);
498
499 if (options->thumbnails) {
500 mode = MODE_THUMB;
501 tns_init(&tns, filecnt);
502 while (!tns_load(&tns, 0, &files[0], False, False))
503 remove_file(0, 0);
504 tns.cnt = 1;
505 } else {
506 mode = MODE_IMAGE;
507 tns.thumbs = NULL;
508 load_image(fileidx);
509 }
510
511 win_open(&win);
512
513 run();
514 cleanup();
515
516 return 0;
517}