Krolyxon
Commit cfed65d

Branches

Refs →

nsxivmain.c

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