Krolyxon
Commit 1576eb4

Branches

Refs →

nsxivmain.c

357 lines · 6.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 <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_check(options->filenames[i]))
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 ksym;
140 int changed;
141
142 if (!ev)
143 return;
144
145 XLookupString(&ev->xkey, &key, 1, &ksym, NULL);
146 changed = 0;
147
148 switch (ksym) {
149 case XK_Escape:
150 cleanup();
151 exit(2);
152 case XK_q:
153 cleanup();
154 exit(0);
155
156 /* navigate image list */
157 case XK_n:
158 case XK_space:
159 if (fileidx + 1 < filecnt) {
160 changed = img_load(&img, filenames[++fileidx]);
161 }
162 break;
163 case XK_p:
164 case XK_BackSpace:
165 if (fileidx > 0) {
166 changed = img_load(&img, filenames[--fileidx]);
167 }
168 break;
169 case XK_bracketleft:
170 if (fileidx != 0) {
171 fileidx = MAX(0, fileidx - 10);
172 changed = img_load(&img, filenames[fileidx]);
173 }
174 break;
175 case XK_bracketright:
176 if (fileidx != filecnt - 1) {
177 fileidx = MIN(fileidx + 10, filecnt - 1);
178 changed = img_load(&img, filenames[fileidx]);
179 }
180 break;
181 case XK_g:
182 if (fileidx != 0) {
183 fileidx = 0;
184 changed = img_load(&img, filenames[fileidx]);
185 }
186 break;
187 case XK_G:
188 if (fileidx != filecnt - 1) {
189 fileidx = filecnt - 1;
190 changed = img_load(&img, filenames[fileidx]);
191 }
192 break;
193
194 /* zooming */
195 case XK_plus:
196 case XK_equal:
197 changed = img_zoom_in(&img);
198 break;
199 case XK_minus:
200 changed = img_zoom_out(&img);
201 break;
202
203 /* panning */
204 case XK_h:
205 case XK_Left:
206 changed = img_pan(&img, &win, PAN_LEFT);
207 break;
208 case XK_j:
209 case XK_Down:
210 changed = img_pan(&img, &win, PAN_DOWN);
211 break;
212 case XK_k:
213 case XK_Up:
214 changed = img_pan(&img, &win, PAN_UP);
215 break;
216 case XK_l:
217 case XK_Right:
218 changed = img_pan(&img, &win, PAN_RIGHT);
219 break;
220
221 /* rotation */
222 case XK_less:
223 changed = img_rotate_left(&img, &win);
224 break;
225 case XK_greater:
226 changed = img_rotate_right(&img, &win);
227 break;
228
229 /* control window */
230 case XK_f:
231 win_toggle_fullscreen(&win);
232 break;
233
234 /* miscellaneous */
235 case XK_a:
236 changed = img_toggle_antialias(&img);
237 break;
238 case XK_r:
239 changed = img_load(&img, filenames[fileidx]);
240 break;
241 }
242
243 if (changed) {
244 img_render(&img, &win);
245 update_title();
246 timeout = 0;
247 }
248}
249
250void on_buttonpress(XEvent *ev) {
251 int changed;
252 unsigned int mask;
253
254 if (!ev)
255 return;
256
257 mask = CLEANMASK(ev->xbutton.state);
258 changed = 0;
259
260 switch (ev->xbutton.button) {
261 case Button1:
262 if (fileidx + 1 < filecnt) {
263 img_load(&img, filenames[++fileidx]);
264 changed = 1;
265 }
266 break;
267 case Button2:
268 mox = ev->xbutton.x;
269 moy = ev->xbutton.y;
270 win_set_cursor(&win, CURSOR_HAND);
271 break;
272 case Button3:
273 if (fileidx > 0) {
274 img_load(&img, filenames[--fileidx]);
275 changed = 1;
276 }
277 break;
278 case Button4:
279 if (mask == ControlMask)
280 changed = img_zoom_in(&img);
281 else if (mask == ShiftMask)
282 changed = img_pan(&img, &win, PAN_LEFT);
283 else
284 changed = img_pan(&img, &win, PAN_UP);
285 break;
286 case Button5:
287 if (mask == ControlMask)
288 changed = img_zoom_out(&img);
289 else if (mask == ShiftMask)
290 changed = img_pan(&img, &win, PAN_RIGHT);
291 else
292 changed = img_pan(&img, &win, PAN_DOWN);
293 break;
294 case 6:
295 changed = img_pan(&img, &win, PAN_LEFT);
296 break;
297 case 7:
298 changed = img_pan(&img, &win, PAN_RIGHT);
299 break;
300 }
301
302 if (changed) {
303 img_render(&img, &win);
304 update_title();
305 timeout = 0;
306 }
307}
308
309void on_buttonrelease(XEvent *ev) {
310 if (!ev)
311 return;
312
313 if (ev->xbutton.button == Button2)
314 win_set_cursor(&win, CURSOR_ARROW);
315}
316
317void on_motionnotify(XEvent *ev) {
318 XMotionEvent *m;
319
320 if (!ev)
321 return;
322
323 m = &ev->xmotion;
324
325 if (m->x >= 0 && m->x <= win.w && m->y >= 0 && m->y <= win.h) {
326 if (img_move(&img, &win, m->x - mox, m->y - moy))
327 timeout = 1;
328
329 mox = m->x;
330 moy = m->y;
331 }
332}
333
334void on_configurenotify(XEvent *ev) {
335 if (!ev)
336 return;
337
338 if (win_configure(&win, &ev->xconfigure)) {
339 img.checkpan = 1;
340 timeout = 1;
341 }
342}
343
344void update_title() {
345 int n;
346
347 n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] <%d%%> %s", fileidx + 1,
348 filecnt, (int) (img.zoom * 100.0), filenames[fileidx]);
349
350 if (n >= TITLE_LEN) {
351 win_title[TITLE_LEN - 2] = '.';
352 win_title[TITLE_LEN - 3] = '.';
353 win_title[TITLE_LEN - 4] = '.';
354 }
355
356 win_set_title(&win, win_title);
357}