Krolyxon
Commit 03bfe10

Branches

Refs →

nsxivmain.c

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