Krolyxon
Commit 7e2c85b

Branches

Refs →

nsxivmain.c

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