Krolyxon
Commit 1c072cc

Branches

Refs →

nsxivmain.c

544 lines · 10.6 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 modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19#include <stdlib.h>
20#include <stdio.h>
21#include <string.h>
22#include <dirent.h>
23#include <sys/select.h>
24#include <sys/stat.h>
25#include <sys/time.h>
26
27#include <X11/Xlib.h>
28#include <X11/Xutil.h>
29#include <X11/keysym.h>
30
31#include "image.h"
32#include "options.h"
33#include "thumbs.h"
34#include "util.h"
35#include "window.h"
36
37typedef enum appmode_e {
38 MODE_NORMAL = 0,
39 MODE_THUMBS
40} appmode_t;
41
42void update_title();
43int check_append(const char*);
44void read_dir_rec(const char*);
45void run();
46
47appmode_t mode;
48img_t img;
49tns_t tns;
50win_t win;
51
52#define DNAME_CNT 512
53#define FNAME_CNT 1024
54const char **filenames;
55int filecnt, fileidx;
56size_t filesize;
57
58int tns_loaded;
59
60#define TITLE_LEN 256
61char win_title[TITLE_LEN];
62
63void cleanup() {
64 static int in = 0;
65
66 if (!in++) {
67 img_free(&img);
68 tns_free(&tns, &win);
69 win_close(&win);
70 }
71}
72
73int load_image() {
74 struct stat fstats;
75
76 if (!stat(filenames[fileidx], &fstats))
77 filesize = fstats.st_size;
78 else
79 filesize = 0;
80
81 return img_load(&img, filenames[fileidx]);
82}
83
84int main(int argc, char **argv) {
85 int i;
86 const char *filename;
87 struct stat fstats;
88
89 parse_options(argc, argv);
90
91 if (!options->filecnt) {
92 print_usage();
93 exit(1);
94 }
95
96 if (options->recursive || options->from_stdin)
97 filecnt = FNAME_CNT;
98 else
99 filecnt = options->filecnt;
100
101 filenames = (const char**) s_malloc(filecnt * sizeof(const char*));
102 fileidx = 0;
103
104 if (options->from_stdin) {
105 while ((filename = readline(stdin))) {
106 if (!*filename || !check_append(filename))
107 free((void*) filename);
108 }
109 } else {
110 for (i = 0; i < options->filecnt; ++i) {
111 filename = options->filenames[i];
112 if (!stat(filename, &fstats) && S_ISDIR(fstats.st_mode)) {
113 if (options->recursive)
114 read_dir_rec(filename);
115 else
116 warn("ignoring directory: %s", filename);
117 } else {
118 check_append(filename);
119 }
120 }
121 }
122
123 filecnt = fileidx;
124 fileidx = 0;
125
126 if (!filecnt) {
127 fprintf(stderr, "sxiv: no valid image filename given, aborting\n");
128 exit(1);
129 }
130
131 win_open(&win);
132 img_init(&img, &win);
133
134 if (options->thumbnails) {
135 tns_loaded = 0;
136 tns_init(&tns, filecnt);
137 }
138
139 if (options->thumbnails == 2) {
140 mode = MODE_THUMBS;
141 win_clear(&win);
142 win_draw(&win);
143 } else {
144 mode = MODE_NORMAL;
145 load_image();
146 img_render(&img, &win);
147 }
148
149 update_title();
150 run();
151 cleanup();
152
153 return 0;
154}
155
156void update_title() {
157 int n;
158 float size;
159 const char *unit;
160
161 if (img.valid) {
162 size = filesize;
163 size_readable(&size, &unit);
164 n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] <%d%%> (%.2f%s) %s",
165 fileidx + 1, filecnt, (int) (img.zoom * 100.0), size, unit,
166 filenames[fileidx]);
167 } else {
168 n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] broken: %s",
169 fileidx + 1, filecnt, filenames[fileidx]);
170 }
171
172 if (n >= TITLE_LEN) {
173 win_title[TITLE_LEN - 2] = '.';
174 win_title[TITLE_LEN - 3] = '.';
175 win_title[TITLE_LEN - 4] = '.';
176 }
177
178 win_set_title(&win, win_title);
179}
180
181int check_append(const char *filename) {
182 if (!filename)
183 return 0;
184
185 if (img_check(filename)) {
186 if (fileidx == filecnt) {
187 filecnt *= 2;
188 filenames = (const char**) s_realloc(filenames,
189 filecnt * sizeof(const char*));
190 }
191 filenames[fileidx++] = filename;
192 return 1;
193 } else {
194 return 0;
195 }
196}
197
198void read_dir_rec(const char *dirname) {
199 char *filename;
200 const char **dirnames;
201 int dircnt, diridx;
202 unsigned char first;
203 size_t len;
204 DIR *dir;
205 struct dirent *dentry;
206 struct stat fstats;
207
208 if (!dirname)
209 return;
210
211 dircnt = DNAME_CNT;
212 diridx = first = 1;
213 dirnames = (const char**) s_malloc(dircnt * sizeof(const char*));
214 dirnames[0] = dirname;
215
216 while (diridx > 0) {
217 dirname = dirnames[--diridx];
218 if (!(dir = opendir(dirname))) {
219 warn("could not open directory: %s", dirname);
220 } else {
221 while ((dentry = readdir(dir))) {
222 if (!strcmp(dentry->d_name, ".") || !strcmp(dentry->d_name, ".."))
223 continue;
224
225 len = strlen(dirname) + strlen(dentry->d_name) + 2;
226 filename = (char*) s_malloc(len * sizeof(char));
227 snprintf(filename, len, "%s/%s", dirname, dentry->d_name);
228
229 if (!stat(filename, &fstats) && S_ISDIR(fstats.st_mode)) {
230 if (diridx == dircnt) {
231 dircnt *= 2;
232 dirnames = (const char**) s_realloc(dirnames,
233 dircnt * sizeof(const char*));
234 }
235 dirnames[diridx++] = filename;
236 } else {
237 if (!check_append(filename))
238 free(filename);
239 }
240 }
241 closedir(dir);
242 }
243
244 if (!first)
245 free((void*) dirname);
246 else
247 first = 0;
248 }
249
250 free(dirnames);
251}
252
253
254/* event handling */
255
256unsigned char timeout;
257int mox, moy;
258
259void redraw() {
260 if (mode == MODE_NORMAL)
261 img_render(&img, &win);
262 else
263 tns_render(&tns, &win);
264 update_title();
265 timeout = 0;
266}
267
268void on_keypress(XKeyEvent *kev) {
269 int x, y;
270 unsigned int w, h;
271 char key;
272 KeySym ksym;
273 int changed;
274
275 if (!kev)
276 return;
277
278 XLookupString(kev, &key, 1, &ksym, NULL);
279 changed = 0;
280
281 switch (ksym) {
282 case XK_Escape:
283 cleanup();
284 exit(2);
285 case XK_q:
286 cleanup();
287 exit(0);
288
289 /* navigate image list */
290 case XK_n:
291 case XK_space:
292 if (fileidx + 1 < filecnt) {
293 ++fileidx;
294 changed = load_image();
295 }
296 break;
297 case XK_p:
298 case XK_BackSpace:
299 if (fileidx > 0) {
300 --fileidx;
301 changed = load_image();
302 }
303 break;
304 case XK_bracketleft:
305 if (fileidx != 0) {
306 fileidx = MAX(0, fileidx - 10);
307 changed = load_image();
308 }
309 break;
310 case XK_bracketright:
311 if (fileidx != filecnt - 1) {
312 fileidx = MIN(fileidx + 10, filecnt - 1);
313 changed = load_image();
314 }
315 break;
316 case XK_g:
317 if (fileidx != 0) {
318 fileidx = 0;
319 changed = load_image();
320 }
321 break;
322 case XK_G:
323 if (fileidx != filecnt - 1) {
324 fileidx = filecnt - 1;
325 changed = load_image();
326 }
327 break;
328
329 /* zooming */
330 case XK_plus:
331 case XK_equal:
332 changed = img_zoom_in(&img);
333 break;
334 case XK_minus:
335 changed = img_zoom_out(&img);
336 break;
337 case XK_0:
338 changed = img_zoom(&img, 1.0);
339 break;
340 case XK_w:
341 if ((changed = img_fit_win(&img, &win)))
342 img_center(&img, &win);
343 break;
344
345 /* panning */
346 case XK_h:
347 case XK_Left:
348 changed = img_pan(&img, &win, PAN_LEFT);
349 break;
350 case XK_j:
351 case XK_Down:
352 changed = img_pan(&img, &win, PAN_DOWN);
353 break;
354 case XK_k:
355 case XK_Up:
356 changed = img_pan(&img, &win, PAN_UP);
357 break;
358 case XK_l:
359 case XK_Right:
360 changed = img_pan(&img, &win, PAN_RIGHT);
361 break;
362
363 /* rotation */
364 case XK_less:
365 img_rotate_left(&img, &win);
366 changed = 1;
367 break;
368 case XK_greater:
369 img_rotate_right(&img, &win);
370 changed = 1;
371 break;
372
373 /* control window */
374 case XK_f:
375 win_toggle_fullscreen(&win);
376 /* render on next configurenotify */
377 break;
378 case XK_W:
379 x = win.x + img.x;
380 y = win.y + img.y;
381 w = img.w * img.zoom;
382 h = img.h * img.zoom;
383 if ((changed = win_moveresize(&win, x, y, w, h))) {
384 img.x = x - win.x;
385 img.y = y - win.y;
386 }
387 break;
388
389 /* miscellaneous */
390 case XK_a:
391 img_toggle_antialias(&img);
392 changed = 1;
393 break;
394 case XK_r:
395 changed = load_image();
396 break;
397 }
398
399 if (changed)
400 redraw();
401}
402
403void on_buttonpress(XButtonEvent *bev) {
404 int changed;
405 unsigned int mask;
406
407 if (!bev)
408 return;
409
410 mask = CLEANMASK(bev->state);
411 changed = 0;
412
413 switch (bev->button) {
414 case Button1:
415 if (fileidx + 1 < filecnt) {
416 ++fileidx;
417 changed = load_image();
418 }
419 break;
420 case Button2:
421 mox = bev->x;
422 moy = bev->y;
423 win_set_cursor(&win, CURSOR_HAND);
424 break;
425 case Button3:
426 if (fileidx > 0) {
427 --fileidx;
428 changed = load_image();
429 }
430 break;
431 case Button4:
432 if (mask == ControlMask)
433 changed = img_zoom_in(&img);
434 else if (mask == ShiftMask)
435 changed = img_pan(&img, &win, PAN_LEFT);
436 else
437 changed = img_pan(&img, &win, PAN_UP);
438 break;
439 case Button5:
440 if (mask == ControlMask)
441 changed = img_zoom_out(&img);
442 else if (mask == ShiftMask)
443 changed = img_pan(&img, &win, PAN_RIGHT);
444 else
445 changed = img_pan(&img, &win, PAN_DOWN);
446 break;
447 case 6:
448 changed = img_pan(&img, &win, PAN_LEFT);
449 break;
450 case 7:
451 changed = img_pan(&img, &win, PAN_RIGHT);
452 break;
453 }
454
455 if (changed)
456 redraw();
457}
458
459void on_motionnotify(XMotionEvent *mev) {
460 if (!mev)
461 return;
462
463 if (mev->x >= 0 && mev->x <= win.w && mev->y >= 0 && mev->y <= win.h) {
464 if (img_move(&img, &win, mev->x - mox, mev->y - moy))
465 timeout = 1;
466
467 mox = mev->x;
468 moy = mev->y;
469 }
470}
471
472void run() {
473 int xfd;
474 fd_set fds;
475 struct timeval t, t0;
476 XEvent ev;
477
478 timeout = 0;
479
480 while (1) {
481 if (mode == MODE_THUMBS && tns_loaded < filecnt) {
482 win_set_cursor(&win, CURSOR_WATCH);
483 gettimeofday(&t0, 0);
484
485 while (!XPending(win.env.dpy) && tns_loaded < filecnt) {
486 tns_load(&tns, &win, filenames[tns_loaded++]);
487 gettimeofday(&t, 0);
488 if (TV_TO_DOUBLE(t) - TV_TO_DOUBLE(t0) >= 0.25)
489 break;
490 }
491 if (tns_loaded == filecnt)
492 win_set_cursor(&win, CURSOR_ARROW);
493 if (!XPending(win.env.dpy)) {
494 tns_render(&tns, &win);
495 continue;
496 } else {
497 timeout = 1;
498 }
499 } else if (timeout) {
500 t.tv_sec = 0;
501 t.tv_usec = 75000;
502 xfd = ConnectionNumber(win.env.dpy);
503 FD_ZERO(&fds);
504 FD_SET(xfd, &fds);
505
506 if (!XPending(win.env.dpy) && !select(xfd + 1, &fds, 0, 0, &t)) {
507 timeout = 0;
508 if (mode == MODE_NORMAL)
509 img_render(&img, &win);
510 else
511 tns_render(&tns, &win);
512 }
513 }
514
515 if (!XNextEvent(win.env.dpy, &ev)) {
516 switch (ev.type) {
517 case KeyPress:
518 on_keypress(&ev.xkey);
519 break;
520 case ButtonPress:
521 on_buttonpress(&ev.xbutton);
522 break;
523 case ButtonRelease:
524 if (ev.xbutton.button == Button2)
525 win_set_cursor(&win, CURSOR_ARROW);
526 break;
527 case MotionNotify:
528 on_motionnotify(&ev.xmotion);
529 break;
530 case ConfigureNotify:
531 if (win_configure(&win, &ev.xconfigure)) {
532 timeout = 1;
533 if (mode == MODE_NORMAL)
534 img.checkpan = 1;
535 }
536 break;
537 case ClientMessage:
538 if ((Atom) ev.xclient.data.l[0] == wm_delete_win)
539 return;
540 break;
541 }
542 }
543 }
544}