Krolyxon
Commit dc04bdc

Branches

Refs →

nsxivmain.c

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