Krolyxon
Commit 12a94cc

Branches

Refs →

nsxivmain.c

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