Krolyxon
Commit cbf6aae

Branches

Refs →

nsxivmain.c

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