Krolyxon
Commit 414fa56

Branches

Refs →

nsxivmain.c

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