Krolyxon
Commit bd6b05c

Branches

Refs →

nsxivmain.c

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