Krolyxon
Commit 7b49740

Branches

Refs →

nsxivmain.c

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