Krolyxon
Commit 7834dee

Branches

Refs →

nsxivmain.c

271 lines · 5.0 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*);
34
35void update_title();
36
37static void (*handler[LASTEvent])(XEvent*) = {
38 [KeyPress] = on_keypress,
39 [ConfigureNotify] = on_configurenotify
40};
41
42img_t img;
43win_t win;
44
45const char **filenames;
46int filecnt, fileidx;
47
48unsigned char timeout;
49
50#define TITLE_LEN 256
51char win_title[TITLE_LEN];
52
53void run() {
54 int xfd;
55 fd_set fds;
56 struct timeval t;
57 XEvent ev;
58
59 timeout = 0;
60
61 while (1) {
62 if (timeout) {
63 t.tv_sec = 0;
64 t.tv_usec = 250;
65 xfd = ConnectionNumber(win.env.dpy);
66 FD_ZERO(&fds);
67 FD_SET(xfd, &fds);
68
69 if (!XPending(win.env.dpy) && !select(xfd + 1, &fds, 0, 0, &t)) {
70 img_render(&img, &win);
71 timeout = 0;
72 }
73 }
74
75 if (!XNextEvent(win.env.dpy, &ev) && handler[ev.type])
76 handler[ev.type](&ev);
77 }
78}
79
80int main(int argc, char **argv) {
81 int i;
82
83 parse_options(argc, argv);
84
85 if (!options->filecnt) {
86 print_usage();
87 exit(1);
88 }
89
90 if (!(filenames = (const char**) malloc(options->filecnt * sizeof(char*))))
91 DIE("could not allocate memory");
92
93 fileidx = 0;
94 filecnt = 0;
95
96 for (i = 0; i < options->filecnt; ++i) {
97 if (!(img_load(&img, options->filenames[i]) < 0))
98 filenames[filecnt++] = options->filenames[i];
99 }
100
101 if (!filecnt) {
102 fprintf(stderr, "sxiv: no valid image filename given, aborting\n");
103 exit(1);
104 }
105
106 win_open(&win);
107 img_init(&img, &win);
108
109 img_load(&img, filenames[fileidx]);
110 img_render(&img, &win);
111 update_title();
112
113 run();
114
115 cleanup();
116
117 return 0;
118}
119
120void cleanup() {
121 static int in = 0;
122
123 if (!in++) {
124 img_free(&img);
125 win_close(&win);
126 }
127}
128
129void on_keypress(XEvent *ev) {
130 char key;
131 KeySym keysym;
132 int changed;
133
134 if (!ev)
135 return;
136
137 XLookupString(&ev->xkey, &key, 1, &keysym, NULL);
138 changed = 0;
139
140 switch (keysym) {
141 case XK_Escape:
142 cleanup();
143 exit(2);
144 case XK_space:
145 key = 'n';
146 break;
147 case XK_BackSpace:
148 key = 'p';
149 break;
150 }
151
152 switch (key) {
153 case 'q':
154 cleanup();
155 exit(0);
156
157 /* navigate image list */
158 case 'n':
159 if (fileidx + 1 < filecnt) {
160 img_load(&img, filenames[++fileidx]);
161 changed = 1;
162 }
163 break;
164 case 'p':
165 if (fileidx > 0) {
166 img_load(&img, filenames[--fileidx]);
167 changed = 1;
168 }
169 break;
170 case '[':
171 if (fileidx != 0) {
172 fileidx = MAX(0, fileidx - 10);
173 img_load(&img, filenames[fileidx]);
174 changed = 1;
175 }
176 break;
177 case ']':
178 if (fileidx != filecnt - 1) {
179 fileidx = MIN(fileidx + 10, filecnt - 1);
180 img_load(&img, filenames[fileidx]);
181 changed = 1;
182 }
183 break;
184 case 'g':
185 if (fileidx != 0) {
186 fileidx = 0;
187 img_load(&img, filenames[fileidx]);
188 changed = 1;
189 }
190 break;
191 case 'G':
192 if (fileidx != filecnt - 1) {
193 fileidx = filecnt - 1;
194 img_load(&img, filenames[fileidx]);
195 changed = 1;
196 }
197 break;
198
199 /* zooming */
200 case '+':
201 case '=':
202 changed = img_zoom_in(&img);
203 break;
204 case '-':
205 changed = img_zoom_out(&img);
206 break;
207
208 /* panning */
209 case 'h':
210 changed = img_pan(&img, &win, PAN_LEFT);
211 break;
212 case 'j':
213 changed = img_pan(&img, &win, PAN_DOWN);
214 break;
215 case 'k':
216 changed = img_pan(&img, &win, PAN_UP);
217 break;
218 case 'l':
219 changed = img_pan(&img, &win, PAN_RIGHT);
220 break;
221
222 /* rotation */
223 case '<':
224 changed = img_rotate_left(&img, &win);
225 break;
226 case '>':
227 changed = img_rotate_right(&img, &win);
228 break;
229
230 /* control window */
231 case 'f':
232 win_toggle_fullscreen(&win);
233 break;
234
235 /* miscellaneous */
236 case 'a':
237 changed = img_toggle_antialias(&img);
238 break;
239 }
240
241 if (changed) {
242 img_render(&img, &win);
243 update_title();
244 timeout = 0;
245 }
246}
247
248void on_configurenotify(XEvent *ev) {
249 if (!ev)
250 return;
251
252 if (win_configure(&win, &ev->xconfigure)) {
253 img.checkpan = 1;
254 timeout = 1;
255 }
256}
257
258void update_title() {
259 int n;
260
261 n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] <%d%%> %s", fileidx + 1,
262 filecnt, (int) (img.zoom * 100.0), filenames[fileidx]);
263
264 if (n >= TITLE_LEN) {
265 win_title[TITLE_LEN - 2] = '.';
266 win_title[TITLE_LEN - 3] = '.';
267 win_title[TITLE_LEN - 4] = '.';
268 }
269
270 win_set_title(&win, win_title);
271}