Krolyxon
Commit ea65610

Branches

Refs →

nsxivmain.c

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