Krolyxon
Commit 89ec183

Branches

Refs →

nsxivmain.c

516 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 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 "util.h"
33#include "window.h"
34
35typedef enum appmode_e {
36 MODE_NORMAL = 0,
37 MODE_THUMBS
38} appmode_t;
39
40void update_title();
41int check_append(const char*);
42void read_dir_rec(const char*);
43void run();
44
45appmode_t mode;
46img_t img;
47win_t win;
48
49#define DNAME_CNT 512
50#define FNAME_CNT 1024
51const char **filenames;
52int filecnt, fileidx;
53size_t filesize;
54
55thumb_t *thumbs;
56
57#define TITLE_LEN 256
58char win_title[TITLE_LEN];
59
60void cleanup() {
61 static int in = 0;
62
63 if (!in++) {
64 img_free(&img);
65 win_close(&win);
66 }
67}
68
69int load_image() {
70 struct stat fstats;
71
72 if (!stat(filenames[fileidx], &fstats))
73 filesize = fstats.st_size;
74 else
75 filesize = 0;
76
77 return img_load(&img, filenames[fileidx]);
78}
79
80int main(int argc, char **argv) {
81 int i;
82 const char *filename;
83 struct stat fstats;
84
85 parse_options(argc, argv);
86
87 if (!options->filecnt) {
88 print_usage();
89 exit(1);
90 }
91
92 if (options->recursive || options->from_stdin)
93 filecnt = FNAME_CNT;
94 else
95 filecnt = options->filecnt;
96
97 filenames = (const char**) s_malloc(filecnt * sizeof(const char*));
98 fileidx = 0;
99
100 if (options->from_stdin) {
101 while ((filename = readline(stdin))) {
102 if (!*filename || !check_append(filename))
103 free((void*) filename);
104 }
105 } else {
106 for (i = 0; i < options->filecnt; ++i) {
107 filename = options->filenames[i];
108 if (!stat(filename, &fstats) && S_ISDIR(fstats.st_mode)) {
109 if (options->recursive)
110 read_dir_rec(filename);
111 else
112 warn("ignoring directory: %s", filename);
113 } else {
114 check_append(filename);
115 }
116 }
117 }
118
119 filecnt = fileidx;
120 fileidx = 0;
121
122 if (!filecnt) {
123 fprintf(stderr, "sxiv: no valid image filename given, aborting\n");
124 exit(1);
125 }
126
127 win_open(&win);
128 img_init(&img, &win);
129
130 if (options->thumbnails) {
131 thumbs = (thumb_t*) s_malloc(filecnt * sizeof(thumb_t));
132 for (i = 0; i < filecnt; ++i) {
133 thumbs[i].pm = win_create_pixmap(&win);
134 img_load_thumb(&thumbs[i], filenames[i]);
135 }
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 img_render(&img, &win);
258 update_title();
259 timeout = 0;
260}
261
262void on_keypress(XKeyEvent *kev) {
263 int x, y;
264 unsigned int w, h;
265 char key;
266 KeySym ksym;
267 int changed;
268
269 if (!kev)
270 return;
271
272 XLookupString(kev, &key, 1, &ksym, NULL);
273 changed = 0;
274
275 switch (ksym) {
276 case XK_Escape:
277 cleanup();
278 exit(2);
279 case XK_q:
280 cleanup();
281 exit(0);
282
283 /* navigate image list */
284 case XK_n:
285 case XK_space:
286 if (fileidx + 1 < filecnt) {
287 ++fileidx;
288 changed = load_image();
289 }
290 break;
291 case XK_p:
292 case XK_BackSpace:
293 if (fileidx > 0) {
294 --fileidx;
295 changed = load_image();
296 }
297 break;
298 case XK_bracketleft:
299 if (fileidx != 0) {
300 fileidx = MAX(0, fileidx - 10);
301 changed = load_image();
302 }
303 break;
304 case XK_bracketright:
305 if (fileidx != filecnt - 1) {
306 fileidx = MIN(fileidx + 10, filecnt - 1);
307 changed = load_image();
308 }
309 break;
310 case XK_g:
311 if (fileidx != 0) {
312 fileidx = 0;
313 changed = load_image();
314 }
315 break;
316 case XK_G:
317 if (fileidx != filecnt - 1) {
318 fileidx = filecnt - 1;
319 changed = load_image();
320 }
321 break;
322
323 /* zooming */
324 case XK_plus:
325 case XK_equal:
326 changed = img_zoom_in(&img);
327 break;
328 case XK_minus:
329 changed = img_zoom_out(&img);
330 break;
331 case XK_0:
332 changed = img_zoom(&img, 1.0);
333 break;
334 case XK_w:
335 if ((changed = img_fit_win(&img, &win)))
336 img_center(&img, &win);
337 break;
338
339 /* panning */
340 case XK_h:
341 case XK_Left:
342 changed = img_pan(&img, &win, PAN_LEFT);
343 break;
344 case XK_j:
345 case XK_Down:
346 changed = img_pan(&img, &win, PAN_DOWN);
347 break;
348 case XK_k:
349 case XK_Up:
350 changed = img_pan(&img, &win, PAN_UP);
351 break;
352 case XK_l:
353 case XK_Right:
354 changed = img_pan(&img, &win, PAN_RIGHT);
355 break;
356
357 /* rotation */
358 case XK_less:
359 img_rotate_left(&img, &win);
360 changed = 1;
361 break;
362 case XK_greater:
363 img_rotate_right(&img, &win);
364 changed = 1;
365 break;
366
367 /* control window */
368 case XK_f:
369 win_toggle_fullscreen(&win);
370 /* render on next configurenotify */
371 break;
372 case XK_W:
373 x = win.x + img.x;
374 y = win.y + img.y;
375 w = img.w * img.zoom;
376 h = img.h * img.zoom;
377 if ((changed = win_moveresize(&win, x, y, w, h))) {
378 img.x = x - win.x;
379 img.y = y - win.y;
380 }
381 break;
382
383 /* miscellaneous */
384 case XK_a:
385 img_toggle_antialias(&img);
386 changed = 1;
387 break;
388 case XK_r:
389 changed = load_image();
390 break;
391 }
392
393 if (changed)
394 redraw();
395}
396
397void on_buttonpress(XButtonEvent *bev) {
398 int changed;
399 unsigned int mask;
400
401 if (!bev)
402 return;
403
404 mask = CLEANMASK(bev->state);
405 changed = 0;
406
407 switch (bev->button) {
408 case Button1:
409 if (fileidx + 1 < filecnt) {
410 ++fileidx;
411 changed = load_image();
412 }
413 break;
414 case Button2:
415 mox = bev->x;
416 moy = bev->y;
417 win_set_cursor(&win, CURSOR_HAND);
418 break;
419 case Button3:
420 if (fileidx > 0) {
421 --fileidx;
422 changed = load_image();
423 }
424 break;
425 case Button4:
426 if (mask == ControlMask)
427 changed = img_zoom_in(&img);
428 else if (mask == ShiftMask)
429 changed = img_pan(&img, &win, PAN_LEFT);
430 else
431 changed = img_pan(&img, &win, PAN_UP);
432 break;
433 case Button5:
434 if (mask == ControlMask)
435 changed = img_zoom_out(&img);
436 else if (mask == ShiftMask)
437 changed = img_pan(&img, &win, PAN_RIGHT);
438 else
439 changed = img_pan(&img, &win, PAN_DOWN);
440 break;
441 case 6:
442 changed = img_pan(&img, &win, PAN_LEFT);
443 break;
444 case 7:
445 changed = img_pan(&img, &win, PAN_RIGHT);
446 break;
447 }
448
449 if (changed)
450 redraw();
451}
452
453void on_motionnotify(XMotionEvent *mev) {
454 if (!mev)
455 return;
456
457 if (mev->x >= 0 && mev->x <= win.w && mev->y >= 0 && mev->y <= win.h) {
458 if (img_move(&img, &win, mev->x - mox, mev->y - moy))
459 timeout = 1;
460
461 mox = mev->x;
462 moy = mev->y;
463 }
464}
465
466void run() {
467 int xfd;
468 fd_set fds;
469 struct timeval t;
470 XEvent ev;
471
472 timeout = 0;
473
474 while (1) {
475 if (timeout) {
476 t.tv_sec = 0;
477 t.tv_usec = 250;
478 xfd = ConnectionNumber(win.env.dpy);
479 FD_ZERO(&fds);
480 FD_SET(xfd, &fds);
481
482 if (!XPending(win.env.dpy) && !select(xfd + 1, &fds, 0, 0, &t)) {
483 img_render(&img, &win);
484 timeout = 0;
485 }
486 }
487
488 if (!XNextEvent(win.env.dpy, &ev)) {
489 switch (ev.type) {
490 case KeyPress:
491 on_keypress(&ev.xkey);
492 break;
493 case ButtonPress:
494 on_buttonpress(&ev.xbutton);
495 break;
496 case ButtonRelease:
497 if (ev.xbutton.button == Button2)
498 win_set_cursor(&win, CURSOR_ARROW);
499 break;
500 case MotionNotify:
501 on_motionnotify(&ev.xmotion);
502 break;
503 case ConfigureNotify:
504 if (win_configure(&win, &ev.xconfigure)) {
505 img.checkpan = 1;
506 timeout = 1;
507 }
508 break;
509 case ClientMessage:
510 if ((Atom) ev.xclient.data.l[0] == wm_delete_win)
511 return;
512 break;
513 }
514 }
515 }
516}