Krolyxon
Commit e8cf8da

Branches

Refs →

nsxivmain.c

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