Krolyxon
Commit 79bd86d

Branches

Refs →

nsxivmain.c

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