Krolyxon
Commit 73a9279

Branches

Refs →

nsxivmain.c

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