Krolyxon
Commit c937570

Branches

Refs →

nsxivmain.c

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