Krolyxon
Commit 8919204

Branches

Refs →

nsxivmain.c

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