Krolyxon
Commit bad9a70

Branches

Refs →

nsxivmain.c

458 lines · 8.8 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
288 /* panning */
289 case XK_h:
290 case XK_Left:
291 changed = img_pan(&img, &win, PAN_LEFT);
292 break;
293 case XK_j:
294 case XK_Down:
295 changed = img_pan(&img, &win, PAN_DOWN);
296 break;
297 case XK_k:
298 case XK_Up:
299 changed = img_pan(&img, &win, PAN_UP);
300 break;
301 case XK_l:
302 case XK_Right:
303 changed = img_pan(&img, &win, PAN_RIGHT);
304 break;
305
306 /* rotation */
307 case XK_less:
308 img_rotate_left(&img, &win);
309 changed = 1;
310 break;
311 case XK_greater:
312 img_rotate_right(&img, &win);
313 changed = 1;
314 break;
315
316 /* control window */
317 case XK_f:
318 win_toggle_fullscreen(&win);
319 /* render on next configurenotify */
320 break;
321
322 /* miscellaneous */
323 case XK_a:
324 img_toggle_antialias(&img);
325 changed = 1;
326 break;
327 case XK_r:
328 changed = load_image();
329 break;
330;
331 }
332
333 if (changed) {
334 img_render(&img, &win);
335 update_title();
336 timeout = 0;
337 }
338}
339
340void on_buttonpress(XButtonEvent *bev) {
341 int changed;
342 unsigned int mask;
343
344 if (!bev)
345 return;
346
347 mask = CLEANMASK(bev->state);
348 changed = 0;
349
350 switch (bev->button) {
351 case Button1:
352 if (fileidx + 1 < filecnt) {
353 ++fileidx;
354 changed = load_image();
355 }
356 break;
357 case Button2:
358 mox = bev->x;
359 moy = bev->y;
360 win_set_cursor(&win, CURSOR_HAND);
361 break;
362 case Button3:
363 if (fileidx > 0) {
364 --fileidx;
365 changed = load_image();
366 }
367 break;
368 case Button4:
369 if (mask == ControlMask)
370 changed = img_zoom_in(&img);
371 else if (mask == ShiftMask)
372 changed = img_pan(&img, &win, PAN_LEFT);
373 else
374 changed = img_pan(&img, &win, PAN_UP);
375 break;
376 case Button5:
377 if (mask == ControlMask)
378 changed = img_zoom_out(&img);
379 else if (mask == ShiftMask)
380 changed = img_pan(&img, &win, PAN_RIGHT);
381 else
382 changed = img_pan(&img, &win, PAN_DOWN);
383 break;
384 case 6:
385 changed = img_pan(&img, &win, PAN_LEFT);
386 break;
387 case 7:
388 changed = img_pan(&img, &win, PAN_RIGHT);
389 break;
390 }
391
392 if (changed) {
393 img_render(&img, &win);
394 update_title();
395 timeout = 0;
396 }
397}
398
399void on_motionnotify(XMotionEvent *mev) {
400 if (!mev)
401 return;
402
403 if (mev->x >= 0 && mev->x <= win.w && mev->y >= 0 && mev->y <= win.h) {
404 if (img_move(&img, &win, mev->x - mox, mev->y - moy))
405 timeout = 1;
406
407 mox = mev->x;
408 moy = mev->y;
409 }
410}
411
412void run() {
413 int xfd;
414 fd_set fds;
415 struct timeval t;
416 XEvent ev;
417
418 timeout = 0;
419
420 while (1) {
421 if (timeout) {
422 t.tv_sec = 0;
423 t.tv_usec = 250;
424 xfd = ConnectionNumber(win.env.dpy);
425 FD_ZERO(&fds);
426 FD_SET(xfd, &fds);
427
428 if (!XPending(win.env.dpy) && !select(xfd + 1, &fds, 0, 0, &t)) {
429 img_render(&img, &win);
430 timeout = 0;
431 }
432 }
433
434 if (!XNextEvent(win.env.dpy, &ev)) {
435 switch (ev.type) {
436 case KeyPress:
437 on_keypress(&ev.xkey);
438 break;
439 case ButtonPress:
440 on_buttonpress(&ev.xbutton);
441 break;
442 case ButtonRelease:
443 if (ev.xbutton.button == Button2)
444 win_set_cursor(&win, CURSOR_ARROW);
445 break;
446 case MotionNotify:
447 on_motionnotify(&ev.xmotion);
448 break;
449 case ConfigureNotify:
450 if (win_configure(&win, &ev.xconfigure)) {
451 img.checkpan = 1;
452 timeout = 1;
453 }
454 break;
455 }
456 }
457 }
458}