Krolyxon
Commit 26bb005

Branches

Refs →

nsxivmain.c

601 lines · 12.0 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 ? fileidx + 1 : 0, tns.cnt,
160 tns.cnt ? filenames[fileidx] : "");
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
258int 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;
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 = MAX(0, win.x + img.x);
372 y = MAX(0, 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 /* switch to thumnail mode */
382 case XK_Return:
383 if (options->thumbnails) {
384 mode = MODE_THUMBS;
385 changed = tns.dirty = 1;
386 }
387 break;
388
389 /* miscellaneous */
390 case XK_a:
391 img_toggle_antialias(&img);
392 changed = 1;
393 break;
394 case XK_r:
395 changed = load_image();
396 break;
397 }
398 } else {
399 /* thumbnail mode */
400 switch (ksym) {
401 /* open selected image */
402 case XK_Return:
403 load_image();
404 mode = MODE_NORMAL;
405 win_set_cursor(&win, CURSOR_ARROW);
406 changed = 1;
407 break;
408
409 /* move selection */
410 case XK_h:
411 case XK_Left:
412 changed = tns_move_selection(&tns, &win, MOVE_LEFT);
413 break;
414 case XK_j:
415 case XK_Down:
416 changed = tns_move_selection(&tns, &win, MOVE_DOWN);
417 break;
418 case XK_k:
419 case XK_Up:
420 changed = tns_move_selection(&tns, &win, MOVE_UP);
421 break;
422 case XK_l:
423 case XK_Right:
424 changed = tns_move_selection(&tns, &win, MOVE_RIGHT);
425 break;
426 }
427 }
428
429 /* common key mappings */
430 switch (ksym) {
431 case XK_Escape:
432 cleanup();
433 exit(2);
434 case XK_q:
435 cleanup();
436 exit(0);
437 case XK_f:
438 win_toggle_fullscreen(&win);
439 /* render on next configurenotify */
440 break;
441 }
442
443 if (changed)
444 redraw();
445}
446
447void on_buttonpress(XButtonEvent *bev) {
448 int changed, sel;
449 unsigned int mask;
450
451 if (!bev)
452 return;
453
454 mask = CLEANMASK(bev->state);
455 changed = 0;
456
457 if (mode == MODE_NORMAL) {
458 switch (bev->button) {
459 case Button1:
460 if (fileidx + 1 < filecnt) {
461 ++fileidx;
462 changed = load_image();
463 }
464 break;
465 case Button2:
466 mox = bev->x;
467 moy = bev->y;
468 win_set_cursor(&win, CURSOR_HAND);
469 break;
470 case Button3:
471 if (fileidx > 0) {
472 --fileidx;
473 changed = load_image();
474 }
475 break;
476 case Button4:
477 if (mask == ControlMask)
478 changed = img_zoom_in(&img);
479 else if (mask == ShiftMask)
480 changed = img_pan(&img, &win, PAN_LEFT);
481 else
482 changed = img_pan(&img, &win, PAN_UP);
483 break;
484 case Button5:
485 if (mask == ControlMask)
486 changed = img_zoom_out(&img);
487 else if (mask == ShiftMask)
488 changed = img_pan(&img, &win, PAN_RIGHT);
489 else
490 changed = img_pan(&img, &win, PAN_DOWN);
491 break;
492 case 6:
493 changed = img_pan(&img, &win, PAN_LEFT);
494 break;
495 case 7:
496 changed = img_pan(&img, &win, PAN_RIGHT);
497 break;
498 }
499 } else {
500 /* thumbnail mode */
501 switch (bev->button) {
502 case Button1:
503 if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
504 fileidx = sel;
505 load_image();
506 mode = MODE_NORMAL;
507 win_set_cursor(&win, CURSOR_ARROW);
508 changed = 1;
509 break;
510 }
511 break;
512 }
513 }
514
515 if (changed)
516 redraw();
517}
518
519void on_motionnotify(XMotionEvent *mev) {
520 if (!mev)
521 return;
522
523 if (mev->x >= 0 && mev->x <= win.w && mev->y >= 0 && mev->y <= win.h) {
524 if (img_move(&img, &win, mev->x - mox, mev->y - moy))
525 timeout = 1000;
526
527 mox = mev->x;
528 moy = mev->y;
529 }
530}
531
532void run() {
533 int xfd;
534 fd_set fds;
535 struct timeval t, t0;
536 XEvent ev;
537
538 timeout = 0;
539
540 while (1) {
541 if (mode == MODE_THUMBS && tns.cnt < filecnt) {
542 win_set_cursor(&win, CURSOR_WATCH);
543 gettimeofday(&t0, 0);
544
545 while (!XPending(win.env.dpy) && tns.cnt < filecnt) {
546 tns_load(&tns, &win, filenames[tns.cnt]);
547 gettimeofday(&t, 0);
548 if (TV_TO_DOUBLE(t) - TV_TO_DOUBLE(t0) >= 0.25)
549 break;
550 }
551 if (tns.cnt == filecnt)
552 win_set_cursor(&win, CURSOR_ARROW);
553 if (!XPending(win.env.dpy)) {
554 redraw();
555 continue;
556 } else {
557 timeout = 75000;
558 }
559 } else if (timeout) {
560 t.tv_sec = 0;
561 t.tv_usec = timeout;
562 xfd = ConnectionNumber(win.env.dpy);
563 FD_ZERO(&fds);
564 FD_SET(xfd, &fds);
565
566 if (!XPending(win.env.dpy) && !select(xfd + 1, &fds, 0, 0, &t))
567 redraw();
568 }
569
570 if (!XNextEvent(win.env.dpy, &ev)) {
571 switch (ev.type) {
572 case KeyPress:
573 on_keypress(&ev.xkey);
574 break;
575 case ButtonPress:
576 on_buttonpress(&ev.xbutton);
577 break;
578 case ButtonRelease:
579 if (ev.xbutton.button == Button2)
580 win_set_cursor(&win, CURSOR_ARROW);
581 break;
582 case MotionNotify:
583 on_motionnotify(&ev.xmotion);
584 break;
585 case ConfigureNotify:
586 if (win_configure(&win, &ev.xconfigure)) {
587 timeout = 75000;
588 if (mode == MODE_NORMAL)
589 img.checkpan = 1;
590 else
591 tns.dirty = 1;
592 }
593 break;
594 case ClientMessage:
595 if ((Atom) ev.xclient.data.l[0] == wm_delete_win)
596 return;
597 break;
598 }
599 }
600 }
601}