Krolyxon
Commit 6adbb38

Branches

Refs →

nsxivmain.c

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