Krolyxon
Commit de6b58a

Branches

Refs →

nsxivmain.c

518 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 = 0, tdiff, tmin = -1;
164 struct timeval now;
165
166 gettimeofday(&now, 0);
167 while (i < LEN(timeouts)) {
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 i = tmin = -1;
175 } else if (tmin < 0 || tdiff < tmin) {
176 tmin = tdiff;
177 }
178 }
179 i++;
180 }
181 if (tmin > 0 && t)
182 MSEC_TO_TIMEVAL(tmin, t);
183 return tmin > 0;
184}
185
186void load_image(int new) {
187 struct stat fstats;
188
189 if (new < 0 || new >= filecnt)
190 return;
191
192 /* cursor gets reset in redraw() */
193 win_set_cursor(&win, CURSOR_WATCH);
194 img_close(&img, 0);
195
196 while (!img_load(&img, &files[new])) {
197 remove_file(new, 0);
198 if (new >= filecnt)
199 new = filecnt - 1;
200 }
201
202 fileidx = new;
203 if (!stat(files[new].path, &fstats))
204 filesize = fstats.st_size;
205 else
206 filesize = 0;
207
208 if (img.multi.cnt) {
209 if (img.multi.animate)
210 set_timeout(animate, img.multi.frames[img.multi.sel].delay, 1);
211 else
212 reset_timeout(animate);
213 }
214}
215
216void update_title() {
217 int n;
218 float size;
219 const char *unit;
220
221 if (mode == MODE_THUMB) {
222 n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] %s",
223 tns.cnt ? tns.sel + 1 : 0, tns.cnt,
224 tns.cnt ? files[tns.sel].name : "");
225 } else {
226 size = filesize;
227 size_readable(&size, &unit);
228 if (img.multi.cnt)
229 n = snprintf(win_title, TITLE_LEN,
230 "sxiv: [%d/%d] <%d%%> <%dx%d> (%.2f%s) {%d/%d} %s",
231 fileidx + 1, filecnt, (int) (img.zoom * 100.0), img.w,
232 img.h, size, unit, img.multi.sel + 1, img.multi.cnt,
233 files[fileidx].name);
234 else
235 n = snprintf(win_title, TITLE_LEN,
236 "sxiv: [%d/%d] <%d%%> <%dx%d> (%.2f%s) %s",
237 fileidx + 1, filecnt, (int) (img.zoom * 100.0), img.w,
238 img.h, size, unit, files[fileidx].name);
239 }
240
241 if (n >= TITLE_LEN) {
242 for (n = 0; n < 3; n++)
243 win_title[TITLE_LEN - n - 2] = '.';
244 }
245
246 win_set_title(&win, win_title);
247}
248
249void redraw() {
250 if (mode == MODE_IMAGE) {
251 img_render(&img, &win);
252 if (img.multi.animate) {
253 win_set_cursor(&win, CURSOR_NONE);
254 } else {
255 win_set_cursor(&win, CURSOR_ARROW);
256 set_timeout(hide_cursor, TO_CURSOR_HIDE, 1);
257 }
258 } else {
259 tns_render(&tns, &win);
260 }
261 update_title();
262 reset_timeout(redraw);
263}
264
265void hide_cursor() {
266 win_set_cursor(&win, CURSOR_NONE);
267}
268
269void animate() {
270 int delay;
271
272 delay = img_frame_animate(&img, 0);
273 if (delay) {
274 set_timeout(animate, delay, 1);
275 redraw();
276 }
277}
278
279Bool keymask(const keymap_t *k, unsigned int state) {
280 return (k->ctrl ? ControlMask : 0) == (state & ControlMask);
281}
282
283Bool buttonmask(const button_t *b, unsigned int state) {
284 return ((b->ctrl ? ControlMask : 0) | (b->shift ? ShiftMask : 0)) ==
285 (state & (ControlMask | ShiftMask));
286}
287
288void on_keypress(XKeyEvent *kev) {
289 int i;
290 KeySym ksym;
291 char key;
292
293 if (!kev)
294 return;
295
296 XLookupString(kev, &key, 1, &ksym, NULL);
297
298 for (i = 0; i < LEN(keys); i++) {
299 if (keys[i].ksym == ksym && keymask(&keys[i], kev->state)) {
300 if (keys[i].cmd && keys[i].cmd(keys[i].arg))
301 redraw();
302 return;
303 }
304 }
305}
306
307void on_buttonpress(XButtonEvent *bev) {
308 int i, sel;
309
310 if (!bev)
311 return;
312
313 if (mode == MODE_IMAGE) {
314 win_set_cursor(&win, CURSOR_ARROW);
315 set_timeout(hide_cursor, TO_CURSOR_HIDE, 1);
316
317 for (i = 0; i < LEN(buttons); i++) {
318 if (buttons[i].button == bev->button &&
319 buttonmask(&buttons[i], bev->state))
320 {
321 if (buttons[i].cmd && buttons[i].cmd(buttons[i].arg))
322 redraw();
323 return;
324 }
325 }
326 } else {
327 /* thumbnail mode (hard-coded) */
328 switch (bev->button) {
329 case Button1:
330 if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
331 if (sel == tns.sel) {
332 load_image(tns.sel);
333 mode = MODE_IMAGE;
334 set_timeout(hide_cursor, TO_CURSOR_HIDE, 1);
335 } else {
336 tns_highlight(&tns, &win, tns.sel, False);
337 tns_highlight(&tns, &win, sel, True);
338 tns.sel = sel;
339 }
340 redraw();
341 break;
342 }
343 break;
344 case Button4:
345 case Button5:
346 if (tns_scroll(&tns, bev->button == Button4 ? DIR_UP : DIR_DOWN))
347 redraw();
348 break;
349 }
350 }
351}
352
353void run() {
354 int xfd;
355 fd_set fds;
356 struct timeval timeout;
357 XEvent ev;
358
359 redraw();
360
361 while (1) {
362 while (mode == MODE_THUMB && tns.cnt < filecnt &&
363 !XPending(win.env.dpy))
364 {
365 /* load thumbnails */
366 win_set_cursor(&win, CURSOR_WATCH);
367 set_timeout(redraw, TO_REDRAW_THUMBS, 0);
368 if (tns_load(&tns, tns.cnt, &files[tns.cnt], False, False))
369 tns.cnt++;
370 else
371 remove_file(tns.cnt, 0);
372 if (tns.cnt == filecnt) {
373 redraw();
374 win_set_cursor(&win, CURSOR_ARROW);
375 } else {
376 check_timeouts(NULL);
377 }
378 }
379
380 while (!XPending(win.env.dpy) && check_timeouts(&timeout)) {
381 /* wait for timeouts */
382 xfd = ConnectionNumber(win.env.dpy);
383 FD_ZERO(&fds);
384 FD_SET(xfd, &fds);
385 select(xfd + 1, &fds, 0, 0, &timeout);
386 }
387
388 if (!XNextEvent(win.env.dpy, &ev)) {
389 /* handle events */
390 switch (ev.type) {
391 case ButtonPress:
392 on_buttonpress(&ev.xbutton);
393 break;
394 case ClientMessage:
395 if ((Atom) ev.xclient.data.l[0] == wm_delete_win)
396 return;
397 break;
398 case ConfigureNotify:
399 if (win_configure(&win, &ev.xconfigure)) {
400 set_timeout(redraw, TO_REDRAW_RESIZE, 0);
401 if (mode == MODE_IMAGE)
402 img.checkpan = 1;
403 else
404 tns.dirty = 1;
405 }
406 break;
407 case KeyPress:
408 on_keypress(&ev.xkey);
409 break;
410 case MotionNotify:
411 if (mode == MODE_IMAGE) {
412 win_set_cursor(&win, CURSOR_ARROW);
413 set_timeout(hide_cursor, TO_CURSOR_HIDE, 1);
414 }
415 break;
416 }
417 }
418 }
419}
420
421int fncmp(const void *a, const void *b) {
422 return strcoll(((fileinfo_t*) a)->name, ((fileinfo_t*) b)->name);
423}
424
425int main(int argc, char **argv) {
426 int i, len, start;
427 size_t n;
428 char *filename;
429 struct stat fstats;
430 r_dir_t dir;
431
432 parse_options(argc, argv);
433
434 if (options->clean_cache) {
435 tns_init(&tns, 0);
436 tns_clean_cache(&tns);
437 exit(0);
438 }
439
440 if (!options->filecnt) {
441 print_usage();
442 exit(1);
443 }
444
445 if (options->recursive || options->from_stdin)
446 filecnt = FNAME_CNT;
447 else
448 filecnt = options->filecnt;
449
450 files = (fileinfo_t*) s_malloc(filecnt * sizeof(fileinfo_t));
451 fileidx = 0;
452
453 /* build file list: */
454 if (options->from_stdin) {
455 filename = NULL;
456 while ((len = getline(&filename, &n, stdin)) > 0) {
457 if (filename[len-1] == '\n')
458 filename[len-1] = '\0';
459 check_add_file(filename);
460 }
461 } else {
462 for (i = 0; i < options->filecnt; i++) {
463 filename = options->filenames[i];
464
465 if (stat(filename, &fstats) || !S_ISDIR(fstats.st_mode)) {
466 check_add_file(filename);
467 } else {
468 if (!options->recursive) {
469 warn("ignoring directory: %s", filename);
470 continue;
471 }
472 if (r_opendir(&dir, filename)) {
473 warn("could not open directory: %s", filename);
474 continue;
475 }
476 start = fileidx;
477 printf("reading dir: %s\n", filename);
478 while ((filename = r_readdir(&dir))) {
479 check_add_file(filename);
480 free((void*) filename);
481 }
482 r_closedir(&dir);
483 if (fileidx - start > 1)
484 qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp);
485 }
486 }
487 }
488
489 if (!fileidx) {
490 fprintf(stderr, "sxiv: no valid image file given, aborting\n");
491 exit(1);
492 }
493
494 filecnt = fileidx;
495 fileidx = options->startnum < filecnt ? options->startnum : 0;
496
497 win_init(&win);
498 img_init(&img, &win);
499
500 if (options->thumbnails) {
501 mode = MODE_THUMB;
502 tns_init(&tns, filecnt);
503 while (!tns_load(&tns, 0, &files[0], False, False))
504 remove_file(0, 0);
505 tns.cnt = 1;
506 } else {
507 mode = MODE_IMAGE;
508 tns.thumbs = NULL;
509 load_image(fileidx);
510 }
511
512 win_open(&win);
513
514 run();
515 cleanup();
516
517 return 0;
518}