Krolyxon
Commit f08c24b

Branches

Refs →

nsxivmain.c

577 lines · 11.4 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 sel, 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 = 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;
443 unsigned int mask;
444
445 if (!bev)
446 return;
447
448 mask = CLEANMASK(bev->state);
449 changed = 0;
450
451 switch (bev->button) {
452 case Button1:
453 if (fileidx + 1 < filecnt) {
454 ++fileidx;
455 changed = load_image();
456 }
457 break;
458 case Button2:
459 mox = bev->x;
460 moy = bev->y;
461 win_set_cursor(&win, CURSOR_HAND);
462 break;
463 case Button3:
464 if (fileidx > 0) {
465 --fileidx;
466 changed = load_image();
467 }
468 break;
469 case Button4:
470 if (mask == ControlMask)
471 changed = img_zoom_in(&img);
472 else if (mask == ShiftMask)
473 changed = img_pan(&img, &win, PAN_LEFT);
474 else
475 changed = img_pan(&img, &win, PAN_UP);
476 break;
477 case Button5:
478 if (mask == ControlMask)
479 changed = img_zoom_out(&img);
480 else if (mask == ShiftMask)
481 changed = img_pan(&img, &win, PAN_RIGHT);
482 else
483 changed = img_pan(&img, &win, PAN_DOWN);
484 break;
485 case 6:
486 changed = img_pan(&img, &win, PAN_LEFT);
487 break;
488 case 7:
489 changed = img_pan(&img, &win, PAN_RIGHT);
490 break;
491 }
492
493 if (changed)
494 redraw();
495}
496
497void on_motionnotify(XMotionEvent *mev) {
498 if (!mev)
499 return;
500
501 if (mev->x >= 0 && mev->x <= win.w && mev->y >= 0 && mev->y <= win.h) {
502 if (img_move(&img, &win, mev->x - mox, mev->y - moy))
503 timeout = 1;
504
505 mox = mev->x;
506 moy = mev->y;
507 }
508}
509
510void run() {
511 int xfd;
512 fd_set fds;
513 struct timeval t, t0;
514 XEvent ev;
515
516 timeout = 0;
517
518 while (1) {
519 if (mode == MODE_THUMBS && tns.cnt < filecnt) {
520 win_set_cursor(&win, CURSOR_WATCH);
521 gettimeofday(&t0, 0);
522
523 while (!XPending(win.env.dpy) && tns.cnt < filecnt) {
524 tns_load(&tns, &win, filenames[tns.cnt]);
525 gettimeofday(&t, 0);
526 if (TV_TO_DOUBLE(t) - TV_TO_DOUBLE(t0) >= 0.25)
527 break;
528 }
529 if (tns.cnt == filecnt)
530 win_set_cursor(&win, CURSOR_ARROW);
531 if (!XPending(win.env.dpy)) {
532 redraw();
533 continue;
534 } else {
535 timeout = 1;
536 }
537 } else if (timeout) {
538 t.tv_sec = 0;
539 t.tv_usec = 75000;
540 xfd = ConnectionNumber(win.env.dpy);
541 FD_ZERO(&fds);
542 FD_SET(xfd, &fds);
543
544 if (!XPending(win.env.dpy) && !select(xfd + 1, &fds, 0, 0, &t))
545 redraw();
546 }
547
548 if (!XNextEvent(win.env.dpy, &ev)) {
549 switch (ev.type) {
550 case KeyPress:
551 on_keypress(&ev.xkey);
552 break;
553 case ButtonPress:
554 on_buttonpress(&ev.xbutton);
555 break;
556 case ButtonRelease:
557 if (ev.xbutton.button == Button2)
558 win_set_cursor(&win, CURSOR_ARROW);
559 break;
560 case MotionNotify:
561 on_motionnotify(&ev.xmotion);
562 break;
563 case ConfigureNotify:
564 if (win_configure(&win, &ev.xconfigure)) {
565 timeout = 1;
566 if (mode == MODE_NORMAL)
567 img.checkpan = 1;
568 }
569 break;
570 case ClientMessage:
571 if ((Atom) ev.xclient.data.l[0] == wm_delete_win)
572 return;
573 break;
574 }
575 }
576 }
577}