Krolyxon
Commit 1d74938

Branches

Refs →

nsxivmain.c

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