Krolyxon
Commit 8b3ae50

Branches

Refs →

nsxivmain.c

471 lines · 10.4 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
44appmode_t mode;
45img_t img;
46tns_t tns;
47win_t win;
48
49fileinfo_t *files;
50int filecnt, fileidx;
51size_t filesize;
52
53char win_title[TITLE_LEN];
54
55int timo_cursor;
56int timo_redraw;
57int timo_adelay; /* multi-frame animation delay time */
58
59void cleanup() {
60 static int in = 0;
61
62 if (!in++) {
63 img_close(&img, 0);
64 tns_free(&tns);
65 win_close(&win);
66 }
67}
68
69void check_add_file(char *filename) {
70 if (!filename || !*filename)
71 return;
72
73 if (access(filename, R_OK)) {
74 warn("could not open file: %s", filename);
75 return;
76 }
77
78 if (fileidx == filecnt) {
79 filecnt *= 2;
80 files = (fileinfo_t*) s_realloc(files, filecnt * sizeof(fileinfo_t));
81 }
82 if (*filename != '/') {
83 files[fileidx].path = absolute_path(filename);
84 if (!files[fileidx].path) {
85 warn("could not get absolute path of file: %s\n", filename);
86 return;
87 }
88 }
89 files[fileidx].name = s_strdup(filename);
90 if (*filename == '/')
91 files[fileidx].path = files[fileidx].name;
92 fileidx++;
93}
94
95void remove_file(int n, unsigned char silent) {
96 if (n < 0 || n >= filecnt)
97 return;
98
99 if (filecnt == 1) {
100 if (!silent)
101 fprintf(stderr, "sxiv: no more files to display, aborting\n");
102 cleanup();
103 exit(!silent);
104 }
105
106 if (n + 1 < filecnt) {
107 if (files[n].path != files[n].name)
108 free((void*) files[n].path);
109 free((void*) files[n].name);
110 memmove(files + n, files + n + 1, (filecnt - n - 1) * sizeof(fileinfo_t));
111 }
112 if (n + 1 < tns.cnt) {
113 memmove(tns.thumbs + n, tns.thumbs + n + 1, (tns.cnt - n - 1) *
114 sizeof(thumb_t));
115 memset(tns.thumbs + tns.cnt - 1, 0, sizeof(thumb_t));
116 }
117
118 filecnt--;
119 if (n < tns.cnt)
120 tns.cnt--;
121}
122
123void load_image(int new) {
124 struct stat fstats;
125
126 if (new < 0 || new >= filecnt)
127 return;
128
129 /* cursor gets reset in redraw() */
130 win_set_cursor(&win, CURSOR_WATCH);
131 img_close(&img, 0);
132
133 while (!img_load(&img, &files[new])) {
134 remove_file(new, 0);
135 if (new >= filecnt)
136 new = filecnt - 1;
137 }
138
139 fileidx = new;
140 if (!stat(files[new].path, &fstats))
141 filesize = fstats.st_size;
142 else
143 filesize = 0;
144
145 if (img.multi.cnt && img.multi.animate)
146 timo_adelay = img.multi.frames[img.multi.sel].delay;
147}
148
149void update_title() {
150 int n;
151 float size;
152 const char *unit;
153
154 if (mode == MODE_THUMB) {
155 n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] %s",
156 tns.cnt ? tns.sel + 1 : 0, tns.cnt,
157 tns.cnt ? files[tns.sel].name : "");
158 } else {
159 size = filesize;
160 size_readable(&size, &unit);
161 if (img.multi.cnt)
162 n = snprintf(win_title, TITLE_LEN,
163 "sxiv: [%d/%d] <%d%%> <%dx%d> (%.2f%s) {%d/%d} %s",
164 fileidx + 1, filecnt, (int) (img.zoom * 100.0), img.w,
165 img.h, size, unit, img.multi.sel + 1, img.multi.cnt,
166 files[fileidx].name);
167 else
168 n = snprintf(win_title, TITLE_LEN,
169 "sxiv: [%d/%d] <%d%%> <%dx%d> (%.2f%s) %s",
170 fileidx + 1, filecnt, (int) (img.zoom * 100.0), img.w,
171 img.h, size, unit, files[fileidx].name);
172 }
173
174 if (n >= TITLE_LEN) {
175 for (n = 0; n < 3; n++)
176 win_title[TITLE_LEN - n - 2] = '.';
177 }
178
179 win_set_title(&win, win_title);
180}
181
182void redraw() {
183 if (mode == MODE_IMAGE) {
184 img_render(&img, &win);
185 if (timo_cursor)
186 win_set_cursor(&win, CURSOR_ARROW);
187 else
188 win_set_cursor(&win, CURSOR_NONE);
189 } else {
190 tns_render(&tns, &win);
191 }
192 update_title();
193 timo_redraw = 0;
194}
195
196Bool keymask(const keymap_t *k, unsigned int state) {
197 return (k->ctrl ? ControlMask : 0) == (state & ControlMask);
198}
199
200Bool buttonmask(const button_t *b, unsigned int state) {
201 return ((b->ctrl ? ControlMask : 0) | (b->shift ? ShiftMask : 0)) ==
202 (state & (ControlMask | ShiftMask));
203}
204
205void on_keypress(XKeyEvent *kev) {
206 int i;
207 KeySym ksym;
208 char key;
209
210 if (!kev)
211 return;
212
213 XLookupString(kev, &key, 1, &ksym, NULL);
214
215 for (i = 0; i < LEN(keys); i++) {
216 if (keys[i].ksym == ksym && keymask(&keys[i], kev->state)) {
217 if (keys[i].cmd && keys[i].cmd(keys[i].arg))
218 redraw();
219 return;
220 }
221 }
222}
223
224void on_buttonpress(XButtonEvent *bev) {
225 int i, sel;
226
227 if (!bev)
228 return;
229
230 if (mode == MODE_IMAGE) {
231 win_set_cursor(&win, CURSOR_ARROW);
232 timo_cursor = TO_CURSOR_HIDE;
233
234 for (i = 0; i < LEN(buttons); i++) {
235 if (buttons[i].button == bev->button &&
236 buttonmask(&buttons[i], bev->state))
237 {
238 if (buttons[i].cmd && buttons[i].cmd(buttons[i].arg))
239 redraw();
240 return;
241 }
242 }
243 } else {
244 /* thumbnail mode (hard-coded) */
245 switch (bev->button) {
246 case Button1:
247 if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
248 if (sel == tns.sel) {
249 load_image(tns.sel);
250 mode = MODE_IMAGE;
251 timo_cursor = TO_CURSOR_HIDE;
252 } else {
253 tns_highlight(&tns, &win, tns.sel, False);
254 tns_highlight(&tns, &win, sel, True);
255 tns.sel = sel;
256 }
257 redraw();
258 break;
259 }
260 break;
261 case Button4:
262 case Button5:
263 if (tns_scroll(&tns, bev->button == Button4 ? DIR_UP : DIR_DOWN))
264 redraw();
265 break;
266 }
267 }
268}
269
270void run() {
271 int xfd, timeout;
272 fd_set fds;
273 struct timeval tt, t0, t1;
274 XEvent ev;
275
276 timo_cursor = mode == MODE_IMAGE ? TO_CURSOR_HIDE : 0;
277
278 redraw();
279
280 while (1) {
281 if (mode == MODE_THUMB && tns.cnt < filecnt) {
282 /* load thumbnails */
283 win_set_cursor(&win, CURSOR_WATCH);
284 gettimeofday(&t0, 0);
285
286 while (tns.cnt < filecnt && !XPending(win.env.dpy)) {
287 if (tns_load(&tns, tns.cnt, &files[tns.cnt], False, False))
288 tns.cnt++;
289 else
290 remove_file(tns.cnt, 0);
291 gettimeofday(&t1, 0);
292 if (TIMEDIFF(&t1, &t0) >= TO_THUMBS_LOAD)
293 break;
294 }
295 if (tns.cnt == filecnt)
296 win_set_cursor(&win, CURSOR_ARROW);
297 if (!XPending(win.env.dpy)) {
298 redraw();
299 continue;
300 } else {
301 timo_redraw = TO_THUMBS_LOAD;
302 }
303 }
304
305 if (timo_cursor || timo_redraw || timo_adelay) {
306 /* check active timeouts */
307 gettimeofday(&t0, 0);
308 timeout = min_int_nz(3, timo_cursor, timo_redraw, timo_adelay);
309 MSEC_TO_TIMEVAL(timeout, &tt);
310 xfd = ConnectionNumber(win.env.dpy);
311 FD_ZERO(&fds);
312 FD_SET(xfd, &fds);
313
314 if (!XPending(win.env.dpy))
315 select(xfd + 1, &fds, 0, 0, &tt);
316 gettimeofday(&t1, 0);
317 timeout = MIN(TIMEDIFF(&t1, &t0), timeout);
318
319 /* timeouts fired? */
320 if (timo_cursor) {
321 timo_cursor = MAX(0, timo_cursor - timeout);
322 if (!timo_cursor)
323 win_set_cursor(&win, CURSOR_NONE);
324 }
325 if (timo_redraw) {
326 timo_redraw = MAX(0, timo_redraw - timeout);
327 if (!timo_redraw)
328 redraw();
329 }
330 if (timo_adelay) {
331 timo_adelay = MAX(0, timo_adelay - timeout);
332 if (!timo_adelay) {
333 if ((timo_adelay = img_frame_animate(&img, 0)))
334 redraw();
335 }
336 }
337 if ((timo_cursor || timo_redraw || timo_adelay) &&
338 !XPending(win.env.dpy))
339 continue;
340 }
341
342 if (!XNextEvent(win.env.dpy, &ev)) {
343 /* handle events */
344 switch (ev.type) {
345 case ButtonPress:
346 on_buttonpress(&ev.xbutton);
347 break;
348 case ClientMessage:
349 if ((Atom) ev.xclient.data.l[0] == wm_delete_win)
350 return;
351 break;
352 case ConfigureNotify:
353 if (win_configure(&win, &ev.xconfigure)) {
354 timo_redraw = TO_WIN_RESIZE;
355 if (mode == MODE_IMAGE)
356 img.checkpan = 1;
357 else
358 tns.dirty = 1;
359 }
360 break;
361 case KeyPress:
362 on_keypress(&ev.xkey);
363 break;
364 case MotionNotify:
365 if (!timo_cursor)
366 win_set_cursor(&win, CURSOR_ARROW);
367 timo_cursor = TO_CURSOR_HIDE;
368 break;
369 }
370 }
371 }
372}
373
374int fncmp(const void *a, const void *b) {
375 return strcoll(((fileinfo_t*) a)->name, ((fileinfo_t*) b)->name);
376}
377
378int main(int argc, char **argv) {
379 int i, len, start;
380 size_t n;
381 char *filename;
382 struct stat fstats;
383 r_dir_t dir;
384
385 parse_options(argc, argv);
386
387 if (options->clean_cache) {
388 tns_init(&tns, 0);
389 tns_clean_cache(&tns);
390 exit(0);
391 }
392
393 if (!options->filecnt) {
394 print_usage();
395 exit(1);
396 }
397
398 if (options->recursive || options->from_stdin)
399 filecnt = FNAME_CNT;
400 else
401 filecnt = options->filecnt;
402
403 files = (fileinfo_t*) s_malloc(filecnt * sizeof(fileinfo_t));
404 fileidx = 0;
405
406 /* build file list: */
407 if (options->from_stdin) {
408 filename = NULL;
409 while ((len = getline(&filename, &n, stdin)) > 0) {
410 if (filename[len-1] == '\n')
411 filename[len-1] = '\0';
412 check_add_file(filename);
413 }
414 } else {
415 for (i = 0; i < options->filecnt; i++) {
416 filename = options->filenames[i];
417
418 if (stat(filename, &fstats) || !S_ISDIR(fstats.st_mode)) {
419 check_add_file(filename);
420 } else {
421 if (!options->recursive) {
422 warn("ignoring directory: %s", filename);
423 continue;
424 }
425 if (r_opendir(&dir, filename)) {
426 warn("could not open directory: %s", filename);
427 continue;
428 }
429 start = fileidx;
430 printf("reading dir: %s\n", filename);
431 while ((filename = r_readdir(&dir))) {
432 check_add_file(filename);
433 free((void*) filename);
434 }
435 r_closedir(&dir);
436 if (fileidx - start > 1)
437 qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp);
438 }
439 }
440 }
441
442 if (!fileidx) {
443 fprintf(stderr, "sxiv: no valid image file given, aborting\n");
444 exit(1);
445 }
446
447 filecnt = fileidx;
448 fileidx = options->startnum < filecnt ? options->startnum : 0;
449
450 win_init(&win);
451 img_init(&img, &win);
452
453 if (options->thumbnails) {
454 mode = MODE_THUMB;
455 tns_init(&tns, filecnt);
456 while (!tns_load(&tns, 0, &files[0], False, False))
457 remove_file(0, 0);
458 tns.cnt = 1;
459 } else {
460 mode = MODE_IMAGE;
461 tns.thumbs = NULL;
462 load_image(fileidx);
463 }
464
465 win_open(&win);
466
467 run();
468 cleanup();
469
470 return 0;
471}