Krolyxon
Commit 2afb989

Branches

Refs →

nsxivmain.c

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