Krolyxon
Commit bbd7b7d

Branches

Refs →

nsxivmain.c

461 lines · 9.1 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
26#include <X11/Xlib.h>
27#include <X11/Xutil.h>
28#include <X11/keysym.h>
29
30#include "sxiv.h"
31#include "image.h"
32#include "window.h"
33
34void* s_malloc(size_t);
35void* s_realloc(void*, size_t);
36
37void on_keypress(XEvent*);
38void on_buttonpress(XEvent*);
39void on_buttonrelease(XEvent*);
40void on_motionnotify(XEvent*);
41void on_configurenotify(XEvent*);
42
43void update_title();
44void check_append(const char*);
45void read_dir_rec(const char*);
46
47static void (*handler[LASTEvent])(XEvent*) = {
48 [KeyPress] = on_keypress,
49 [ButtonPress] = on_buttonpress,
50 [ButtonRelease] = on_buttonrelease,
51 [MotionNotify] = on_motionnotify,
52 [ConfigureNotify] = on_configurenotify
53};
54
55img_t img;
56win_t win;
57
58#define DNAME_CNT 512
59#define FNAME_CNT 4096
60const char **filenames;
61int filecnt, fileidx;
62
63unsigned char timeout;
64
65int mox;
66int moy;
67
68#define TITLE_LEN 256
69char win_title[TITLE_LEN];
70
71void run() {
72 int xfd;
73 fd_set fds;
74 struct timeval t;
75 XEvent ev;
76
77 timeout = 0;
78
79 while (1) {
80 if (timeout) {
81 t.tv_sec = 0;
82 t.tv_usec = 250;
83 xfd = ConnectionNumber(win.env.dpy);
84 FD_ZERO(&fds);
85 FD_SET(xfd, &fds);
86
87 if (!XPending(win.env.dpy) && !select(xfd + 1, &fds, 0, 0, &t)) {
88 img_render(&img, &win);
89 timeout = 0;
90 }
91 }
92
93 if (!XNextEvent(win.env.dpy, &ev) && handler[ev.type])
94 handler[ev.type](&ev);
95 }
96}
97
98int main(int argc, char **argv) {
99 int i;
100 const char *filename;
101 struct stat fstats;
102
103 parse_options(argc, argv);
104
105 if (!options->filecnt) {
106 print_usage();
107 exit(1);
108 }
109
110 if (options->recursive)
111 filecnt = FNAME_CNT;
112 else
113 filecnt = options->filecnt;
114
115 filenames = (const char**) s_malloc(filecnt * sizeof(const char*));
116 fileidx = 0;
117
118 for (i = 0; i < options->filecnt; ++i) {
119 filename = options->filenames[i];
120 if (stat(filename, &fstats)) {
121 WARN("could not stat file: %s", filename);
122 } else if (S_ISDIR(fstats.st_mode)) {
123 if (options->recursive)
124 read_dir_rec(filename);
125 else
126 WARN("ignoring directory: %s", filename);
127 } else {
128 check_append(filename);
129 }
130 }
131
132 filecnt = fileidx;
133 fileidx = 0;
134
135 if (!filecnt) {
136 fprintf(stderr, "sxiv: no valid image filename given, aborting\n");
137 exit(1);
138 }
139
140 win_open(&win);
141 img_init(&img, &win);
142
143 img_load(&img, filenames[fileidx]);
144 img_render(&img, &win);
145 update_title();
146
147 run();
148
149 cleanup();
150
151 return 0;
152}
153
154void cleanup() {
155 static int in = 0;
156
157 if (!in++) {
158 img_free(&img);
159 win_close(&win);
160 }
161}
162
163void on_keypress(XEvent *ev) {
164 char key;
165 KeySym ksym;
166 int changed;
167
168 if (!ev)
169 return;
170
171 XLookupString(&ev->xkey, &key, 1, &ksym, NULL);
172 changed = 0;
173
174 switch (ksym) {
175 case XK_Escape:
176 cleanup();
177 exit(2);
178 case XK_q:
179 cleanup();
180 exit(0);
181
182 /* navigate image list */
183 case XK_n:
184 case XK_space:
185 if (fileidx + 1 < filecnt) {
186 changed = img_load(&img, filenames[++fileidx]);
187 }
188 break;
189 case XK_p:
190 case XK_BackSpace:
191 if (fileidx > 0) {
192 changed = img_load(&img, filenames[--fileidx]);
193 }
194 break;
195 case XK_bracketleft:
196 if (fileidx != 0) {
197 fileidx = MAX(0, fileidx - 10);
198 changed = img_load(&img, filenames[fileidx]);
199 }
200 break;
201 case XK_bracketright:
202 if (fileidx != filecnt - 1) {
203 fileidx = MIN(fileidx + 10, filecnt - 1);
204 changed = img_load(&img, filenames[fileidx]);
205 }
206 break;
207 case XK_g:
208 if (fileidx != 0) {
209 fileidx = 0;
210 changed = img_load(&img, filenames[fileidx]);
211 }
212 break;
213 case XK_G:
214 if (fileidx != filecnt - 1) {
215 fileidx = filecnt - 1;
216 changed = img_load(&img, filenames[fileidx]);
217 }
218 break;
219
220 /* zooming */
221 case XK_plus:
222 case XK_equal:
223 changed = img_zoom_in(&img);
224 break;
225 case XK_minus:
226 changed = img_zoom_out(&img);
227 break;
228
229 /* panning */
230 case XK_h:
231 case XK_Left:
232 changed = img_pan(&img, &win, PAN_LEFT);
233 break;
234 case XK_j:
235 case XK_Down:
236 changed = img_pan(&img, &win, PAN_DOWN);
237 break;
238 case XK_k:
239 case XK_Up:
240 changed = img_pan(&img, &win, PAN_UP);
241 break;
242 case XK_l:
243 case XK_Right:
244 changed = img_pan(&img, &win, PAN_RIGHT);
245 break;
246
247 /* rotation */
248 case XK_less:
249 changed = img_rotate_left(&img, &win);
250 break;
251 case XK_greater:
252 changed = img_rotate_right(&img, &win);
253 break;
254
255 /* control window */
256 case XK_f:
257 win_toggle_fullscreen(&win);
258 break;
259
260 /* miscellaneous */
261 case XK_a:
262 changed = img_toggle_antialias(&img);
263 break;
264 case XK_r:
265 changed = img_load(&img, filenames[fileidx]);
266 break;
267 }
268
269 if (changed) {
270 img_render(&img, &win);
271 update_title();
272 timeout = 0;
273 }
274}
275
276void on_buttonpress(XEvent *ev) {
277 int changed;
278 unsigned int mask;
279
280 if (!ev)
281 return;
282
283 mask = CLEANMASK(ev->xbutton.state);
284 changed = 0;
285
286 switch (ev->xbutton.button) {
287 case Button1:
288 if (fileidx + 1 < filecnt) {
289 img_load(&img, filenames[++fileidx]);
290 changed = 1;
291 }
292 break;
293 case Button2:
294 mox = ev->xbutton.x;
295 moy = ev->xbutton.y;
296 win_set_cursor(&win, CURSOR_HAND);
297 break;
298 case Button3:
299 if (fileidx > 0) {
300 img_load(&img, filenames[--fileidx]);
301 changed = 1;
302 }
303 break;
304 case Button4:
305 if (mask == ControlMask)
306 changed = img_zoom_in(&img);
307 else if (mask == ShiftMask)
308 changed = img_pan(&img, &win, PAN_LEFT);
309 else
310 changed = img_pan(&img, &win, PAN_UP);
311 break;
312 case Button5:
313 if (mask == ControlMask)
314 changed = img_zoom_out(&img);
315 else if (mask == ShiftMask)
316 changed = img_pan(&img, &win, PAN_RIGHT);
317 else
318 changed = img_pan(&img, &win, PAN_DOWN);
319 break;
320 case 6:
321 changed = img_pan(&img, &win, PAN_LEFT);
322 break;
323 case 7:
324 changed = img_pan(&img, &win, PAN_RIGHT);
325 break;
326 }
327
328 if (changed) {
329 img_render(&img, &win);
330 update_title();
331 timeout = 0;
332 }
333}
334
335void on_buttonrelease(XEvent *ev) {
336 if (!ev)
337 return;
338
339 if (ev->xbutton.button == Button2)
340 win_set_cursor(&win, CURSOR_ARROW);
341}
342
343void on_motionnotify(XEvent *ev) {
344 XMotionEvent *m;
345
346 if (!ev)
347 return;
348
349 m = &ev->xmotion;
350
351 if (m->x >= 0 && m->x <= win.w && m->y >= 0 && m->y <= win.h) {
352 if (img_move(&img, &win, m->x - mox, m->y - moy))
353 timeout = 1;
354
355 mox = m->x;
356 moy = m->y;
357 }
358}
359
360void on_configurenotify(XEvent *ev) {
361 if (!ev)
362 return;
363
364 if (win_configure(&win, &ev->xconfigure)) {
365 img.checkpan = 1;
366 timeout = 1;
367 }
368}
369
370void update_title() {
371 int n;
372
373 n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] <%d%%> %s", fileidx + 1,
374 filecnt, (int) (img.zoom * 100.0), filenames[fileidx]);
375
376 if (n >= TITLE_LEN) {
377 win_title[TITLE_LEN - 2] = '.';
378 win_title[TITLE_LEN - 3] = '.';
379 win_title[TITLE_LEN - 4] = '.';
380 }
381
382 win_set_title(&win, win_title);
383}
384
385void check_append(const char *filename) {
386 if (!filename)
387 return;
388
389 if (img_check(filename)) {
390 if (fileidx == filecnt) {
391 filecnt *= 2;
392 filenames = (const char**) s_realloc(filenames,
393 filecnt * sizeof(const char*));
394 }
395 filenames[fileidx++] = filename;
396 }
397}
398
399void read_dir_rec(const char *dirname) {
400 char *filename;
401 const char **dirnames;
402 int dircnt, diridx;
403 unsigned char first;
404 size_t len;
405 DIR *dir;
406 struct dirent *dentry;
407 struct stat fstats;
408
409 if (!dirname)
410 return;
411
412 dircnt = DNAME_CNT;
413 diridx = first = 1;
414 dirnames = (const char**) s_malloc(dircnt * sizeof(const char*));
415 dirnames[0] = dirname;
416
417 while (diridx > 0) {
418 dirname = dirnames[--diridx];
419 if (!(dir = opendir(dirname)))
420 DIE("could not open directory: %s", dirname);
421 while ((dentry = readdir(dir))) {
422 if (!strcmp(dentry->d_name, ".") || !strcmp(dentry->d_name, ".."))
423 continue;
424 len = strlen(dirname) + strlen(dentry->d_name) + 2;
425 filename = (char*) s_malloc(len * sizeof(char));
426 snprintf(filename, len, "%s/%s", dirname, dentry->d_name);
427 if (stat(filename, &fstats)) {
428 WARN("could not stat file: %s", filename);
429 free(filename);
430 } else if (S_ISDIR(fstats.st_mode)) {
431 if (diridx == dircnt) {
432 dircnt *= 2;
433 dirnames = (const char**) s_realloc(dirnames,
434 dircnt * sizeof(const char*));
435 }
436 dirnames[diridx++] = filename;
437 } else {
438 check_append(filename);
439 }
440 }
441 closedir(dir);
442 if (!first)
443 free((void*) dirname);
444 else
445 first = 0;
446 }
447}
448
449void* s_malloc(size_t size) {
450 void *ptr;
451
452 if (!(ptr = malloc(size)))
453 DIE("could not allocate memory");
454 return ptr;
455}
456
457void* s_realloc(void *ptr, size_t size) {
458 if (!(ptr = realloc(ptr, size)))
459 DIE("could not allocate memory");
460 return ptr;
461}