Krolyxon
Commit ef24ded

Branches

Refs →

nsxivmain.c

547 lines · 10.8 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;
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 }
392
393 /* common key mappings */
394 switch (ksym) {
395 case XK_Escape:
396 cleanup();
397 exit(2);
398 case XK_q:
399 cleanup();
400 exit(0);
401 case XK_f:
402 win_toggle_fullscreen(&win);
403 /* render on next configurenotify */
404 break;
405 }
406
407 if (changed)
408 redraw();
409}
410
411void on_buttonpress(XButtonEvent *bev) {
412 int changed;
413 unsigned int mask;
414
415 if (!bev)
416 return;
417
418 mask = CLEANMASK(bev->state);
419 changed = 0;
420
421 switch (bev->button) {
422 case Button1:
423 if (fileidx + 1 < filecnt) {
424 ++fileidx;
425 changed = load_image();
426 }
427 break;
428 case Button2:
429 mox = bev->x;
430 moy = bev->y;
431 win_set_cursor(&win, CURSOR_HAND);
432 break;
433 case Button3:
434 if (fileidx > 0) {
435 --fileidx;
436 changed = load_image();
437 }
438 break;
439 case Button4:
440 if (mask == ControlMask)
441 changed = img_zoom_in(&img);
442 else if (mask == ShiftMask)
443 changed = img_pan(&img, &win, PAN_LEFT);
444 else
445 changed = img_pan(&img, &win, PAN_UP);
446 break;
447 case Button5:
448 if (mask == ControlMask)
449 changed = img_zoom_out(&img);
450 else if (mask == ShiftMask)
451 changed = img_pan(&img, &win, PAN_RIGHT);
452 else
453 changed = img_pan(&img, &win, PAN_DOWN);
454 break;
455 case 6:
456 changed = img_pan(&img, &win, PAN_LEFT);
457 break;
458 case 7:
459 changed = img_pan(&img, &win, PAN_RIGHT);
460 break;
461 }
462
463 if (changed)
464 redraw();
465}
466
467void on_motionnotify(XMotionEvent *mev) {
468 if (!mev)
469 return;
470
471 if (mev->x >= 0 && mev->x <= win.w && mev->y >= 0 && mev->y <= win.h) {
472 if (img_move(&img, &win, mev->x - mox, mev->y - moy))
473 timeout = 1;
474
475 mox = mev->x;
476 moy = mev->y;
477 }
478}
479
480void run() {
481 int xfd;
482 fd_set fds;
483 struct timeval t, t0;
484 XEvent ev;
485
486 timeout = 0;
487
488 while (1) {
489 if (mode == MODE_THUMBS && tns.cnt < filecnt) {
490 win_set_cursor(&win, CURSOR_WATCH);
491 gettimeofday(&t0, 0);
492
493 while (!XPending(win.env.dpy) && tns.cnt < filecnt) {
494 tns_load(&tns, &win, filenames[tns.cnt]);
495 gettimeofday(&t, 0);
496 if (TV_TO_DOUBLE(t) - TV_TO_DOUBLE(t0) >= 0.25)
497 break;
498 }
499 if (tns.cnt == filecnt)
500 win_set_cursor(&win, CURSOR_ARROW);
501 if (!XPending(win.env.dpy)) {
502 redraw();
503 continue;
504 } else {
505 timeout = 1;
506 }
507 } else if (timeout) {
508 t.tv_sec = 0;
509 t.tv_usec = 75000;
510 xfd = ConnectionNumber(win.env.dpy);
511 FD_ZERO(&fds);
512 FD_SET(xfd, &fds);
513
514 if (!XPending(win.env.dpy) && !select(xfd + 1, &fds, 0, 0, &t))
515 redraw();
516 }
517
518 if (!XNextEvent(win.env.dpy, &ev)) {
519 switch (ev.type) {
520 case KeyPress:
521 on_keypress(&ev.xkey);
522 break;
523 case ButtonPress:
524 on_buttonpress(&ev.xbutton);
525 break;
526 case ButtonRelease:
527 if (ev.xbutton.button == Button2)
528 win_set_cursor(&win, CURSOR_ARROW);
529 break;
530 case MotionNotify:
531 on_motionnotify(&ev.xmotion);
532 break;
533 case ConfigureNotify:
534 if (win_configure(&win, &ev.xconfigure)) {
535 timeout = 1;
536 if (mode == MODE_NORMAL)
537 img.checkpan = 1;
538 }
539 break;
540 case ClientMessage:
541 if ((Atom) ev.xclient.data.l[0] == wm_delete_win)
542 return;
543 break;
544 }
545 }
546 }
547}