Krolyxon
Commit 783213c

Branches

Refs →

nsxivmain.c

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