Krolyxon
Commit e09a6b5

Branches

Refs →

nsxivmain.c

545 lines · 10.7 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 switch (ksym) {
288 case XK_Escape:
289 cleanup();
290 exit(2);
291 case XK_q:
292 cleanup();
293 exit(0);
294
295 /* navigate image list */
296 case XK_n:
297 case XK_space:
298 if (fileidx + 1 < filecnt) {
299 ++fileidx;
300 changed = load_image();
301 }
302 break;
303 case XK_p:
304 case XK_BackSpace:
305 if (fileidx > 0) {
306 --fileidx;
307 changed = load_image();
308 }
309 break;
310 case XK_bracketleft:
311 if (fileidx != 0) {
312 fileidx = MAX(0, fileidx - 10);
313 changed = load_image();
314 }
315 break;
316 case XK_bracketright:
317 if (fileidx != filecnt - 1) {
318 fileidx = MIN(fileidx + 10, filecnt - 1);
319 changed = load_image();
320 }
321 break;
322 case XK_g:
323 if (fileidx != 0) {
324 fileidx = 0;
325 changed = load_image();
326 }
327 break;
328 case XK_G:
329 if (fileidx != filecnt - 1) {
330 fileidx = filecnt - 1;
331 changed = load_image();
332 }
333 break;
334
335 /* zooming */
336 case XK_plus:
337 case XK_equal:
338 changed = img_zoom_in(&img);
339 break;
340 case XK_minus:
341 changed = img_zoom_out(&img);
342 break;
343 case XK_0:
344 changed = img_zoom(&img, 1.0);
345 break;
346 case XK_w:
347 if ((changed = img_fit_win(&img, &win)))
348 img_center(&img, &win);
349 break;
350
351 /* panning */
352 case XK_h:
353 case XK_Left:
354 changed = img_pan(&img, &win, PAN_LEFT);
355 break;
356 case XK_j:
357 case XK_Down:
358 changed = img_pan(&img, &win, PAN_DOWN);
359 break;
360 case XK_k:
361 case XK_Up:
362 changed = img_pan(&img, &win, PAN_UP);
363 break;
364 case XK_l:
365 case XK_Right:
366 changed = img_pan(&img, &win, PAN_RIGHT);
367 break;
368
369 /* rotation */
370 case XK_less:
371 img_rotate_left(&img, &win);
372 changed = 1;
373 break;
374 case XK_greater:
375 img_rotate_right(&img, &win);
376 changed = 1;
377 break;
378
379 /* control window */
380 case XK_f:
381 win_toggle_fullscreen(&win);
382 /* render on next configurenotify */
383 break;
384 case XK_W:
385 x = win.x + img.x;
386 y = win.y + img.y;
387 w = img.w * img.zoom;
388 h = img.h * img.zoom;
389 if ((changed = win_moveresize(&win, x, y, w, h))) {
390 img.x = x - win.x;
391 img.y = y - win.y;
392 }
393 break;
394
395 /* miscellaneous */
396 case XK_a:
397 img_toggle_antialias(&img);
398 changed = 1;
399 break;
400 case XK_r:
401 changed = load_image();
402 break;
403 }
404
405 if (changed)
406 redraw();
407}
408
409void on_buttonpress(XButtonEvent *bev) {
410 int changed;
411 unsigned int mask;
412
413 if (!bev)
414 return;
415
416 mask = CLEANMASK(bev->state);
417 changed = 0;
418
419 switch (bev->button) {
420 case Button1:
421 if (fileidx + 1 < filecnt) {
422 ++fileidx;
423 changed = load_image();
424 }
425 break;
426 case Button2:
427 mox = bev->x;
428 moy = bev->y;
429 win_set_cursor(&win, CURSOR_HAND);
430 break;
431 case Button3:
432 if (fileidx > 0) {
433 --fileidx;
434 changed = load_image();
435 }
436 break;
437 case Button4:
438 if (mask == ControlMask)
439 changed = img_zoom_in(&img);
440 else if (mask == ShiftMask)
441 changed = img_pan(&img, &win, PAN_LEFT);
442 else
443 changed = img_pan(&img, &win, PAN_UP);
444 break;
445 case Button5:
446 if (mask == ControlMask)
447 changed = img_zoom_out(&img);
448 else if (mask == ShiftMask)
449 changed = img_pan(&img, &win, PAN_RIGHT);
450 else
451 changed = img_pan(&img, &win, PAN_DOWN);
452 break;
453 case 6:
454 changed = img_pan(&img, &win, PAN_LEFT);
455 break;
456 case 7:
457 changed = img_pan(&img, &win, PAN_RIGHT);
458 break;
459 }
460
461 if (changed)
462 redraw();
463}
464
465void on_motionnotify(XMotionEvent *mev) {
466 if (!mev)
467 return;
468
469 if (mev->x >= 0 && mev->x <= win.w && mev->y >= 0 && mev->y <= win.h) {
470 if (img_move(&img, &win, mev->x - mox, mev->y - moy))
471 timeout = 1;
472
473 mox = mev->x;
474 moy = mev->y;
475 }
476}
477
478void run() {
479 int xfd;
480 fd_set fds;
481 struct timeval t, t0;
482 XEvent ev;
483
484 timeout = 0;
485
486 while (1) {
487 if (mode == MODE_THUMBS && tns_loaded < filecnt) {
488 win_set_cursor(&win, CURSOR_WATCH);
489 gettimeofday(&t0, 0);
490
491 while (!XPending(win.env.dpy) && tns_loaded < filecnt) {
492 tns_load(&tns, &win, filenames[tns_loaded++]);
493 gettimeofday(&t, 0);
494 if (TV_TO_DOUBLE(t) - TV_TO_DOUBLE(t0) >= 0.25)
495 break;
496 }
497 if (tns_loaded == filecnt)
498 win_set_cursor(&win, CURSOR_ARROW);
499 if (!XPending(win.env.dpy)) {
500 redraw();
501 continue;
502 } else {
503 timeout = 1;
504 }
505 } else if (timeout) {
506 t.tv_sec = 0;
507 t.tv_usec = 75000;
508 xfd = ConnectionNumber(win.env.dpy);
509 FD_ZERO(&fds);
510 FD_SET(xfd, &fds);
511
512 if (!XPending(win.env.dpy) && !select(xfd + 1, &fds, 0, 0, &t))
513 redraw();
514 }
515
516 if (!XNextEvent(win.env.dpy, &ev)) {
517 switch (ev.type) {
518 case KeyPress:
519 on_keypress(&ev.xkey);
520 break;
521 case ButtonPress:
522 on_buttonpress(&ev.xbutton);
523 break;
524 case ButtonRelease:
525 if (ev.xbutton.button == Button2)
526 win_set_cursor(&win, CURSOR_ARROW);
527 break;
528 case MotionNotify:
529 on_motionnotify(&ev.xmotion);
530 break;
531 case ConfigureNotify:
532 if (win_configure(&win, &ev.xconfigure)) {
533 timeout = 1;
534 if (mode == MODE_NORMAL)
535 img.checkpan = 1;
536 }
537 break;
538 case ClientMessage:
539 if ((Atom) ev.xclient.data.l[0] == wm_delete_win)
540 return;
541 break;
542 }
543 }
544 }
545}