Krolyxon
Commit 0497a7f

Branches

Refs →

nsxivmain.c

298 lines · 5.4 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_configurenotify(XEvent*);
34void on_buttonpress(XEvent*);
35
36void update_title();
37
38static void (*handler[LASTEvent])(XEvent*) = {
39 [ButtonPress] = on_buttonpress,
40 [KeyPress] = on_keypress,
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_buttonpress(XEvent *ev) {
132 int changed;
133 XButtonEvent *buttonevent;
134
135 changed = 0;
136 buttonevent = &ev->xbutton;
137
138 switch (buttonevent->button) {
139 case Button4:
140 changed = img_zoom_in(&img);
141 break;
142 case Button5:
143 changed = img_zoom_out(&img);
144 break;
145 default:
146 return;
147 }
148
149 if (changed) {
150 img_render(&img, &win);
151 update_title();
152 timeout = 0;
153 }
154}
155
156void on_keypress(XEvent *ev) {
157 char key;
158 KeySym keysym;
159 int changed;
160
161 if (!ev)
162 return;
163
164 XLookupString(&ev->xkey, &key, 1, &keysym, NULL);
165 changed = 0;
166
167 switch (keysym) {
168 case XK_Escape:
169 cleanup();
170 exit(2);
171 case XK_space:
172 key = 'n';
173 break;
174 case XK_BackSpace:
175 key = 'p';
176 break;
177 }
178
179 switch (key) {
180 case 'q':
181 cleanup();
182 exit(0);
183
184 /* navigate image list */
185 case 'n':
186 if (fileidx + 1 < filecnt) {
187 img_load(&img, filenames[++fileidx]);
188 changed = 1;
189 }
190 break;
191 case 'p':
192 if (fileidx > 0) {
193 img_load(&img, filenames[--fileidx]);
194 changed = 1;
195 }
196 break;
197 case '[':
198 if (fileidx != 0) {
199 fileidx = MAX(0, fileidx - 10);
200 img_load(&img, filenames[fileidx]);
201 changed = 1;
202 }
203 break;
204 case ']':
205 if (fileidx != filecnt - 1) {
206 fileidx = MIN(fileidx + 10, filecnt - 1);
207 img_load(&img, filenames[fileidx]);
208 changed = 1;
209 }
210 break;
211 case 'g':
212 if (fileidx != 0) {
213 fileidx = 0;
214 img_load(&img, filenames[fileidx]);
215 changed = 1;
216 }
217 break;
218 case 'G':
219 if (fileidx != filecnt - 1) {
220 fileidx = filecnt - 1;
221 img_load(&img, filenames[fileidx]);
222 changed = 1;
223 }
224 break;
225
226 /* zooming */
227 case '+':
228 case '=':
229 changed = img_zoom_in(&img);
230 break;
231 case '-':
232 changed = img_zoom_out(&img);
233 break;
234
235 /* panning */
236 case 'h':
237 changed = img_pan(&img, &win, PAN_LEFT);
238 break;
239 case 'j':
240 changed = img_pan(&img, &win, PAN_DOWN);
241 break;
242 case 'k':
243 changed = img_pan(&img, &win, PAN_UP);
244 break;
245 case 'l':
246 changed = img_pan(&img, &win, PAN_RIGHT);
247 break;
248
249 /* rotation */
250 case '<':
251 changed = img_rotate_left(&img, &win);
252 break;
253 case '>':
254 changed = img_rotate_right(&img, &win);
255 break;
256
257 /* control window */
258 case 'f':
259 win_toggle_fullscreen(&win);
260 break;
261
262 /* miscellaneous */
263 case 'a':
264 changed = img_toggle_antialias(&img);
265 break;
266 }
267
268 if (changed) {
269 img_render(&img, &win);
270 update_title();
271 timeout = 0;
272 }
273}
274
275void on_configurenotify(XEvent *ev) {
276 if (!ev)
277 return;
278
279 if (win_configure(&win, &ev->xconfigure)) {
280 img.checkpan = 1;
281 timeout = 1;
282 }
283}
284
285void update_title() {
286 int n;
287
288 n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] <%d%%> %s", fileidx + 1,
289 filecnt, (int) (img.zoom * 100.0), filenames[fileidx]);
290
291 if (n >= TITLE_LEN) {
292 win_title[TITLE_LEN - 2] = '.';
293 win_title[TITLE_LEN - 3] = '.';
294 win_title[TITLE_LEN - 4] = '.';
295 }
296
297 win_set_title(&win, win_title);
298}