Krolyxon
Commit c52c4fa

Branches

Refs →

nsxivmain.c

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