Krolyxon
Commit b828b55

Branches

Refs →

nsxivmain.c

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