Krolyxon
Commit 8123d63

Branches

Refs →

nsxivmain.c

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