Krolyxon
Commit b92ebf6

Branches

Refs →

nsxivmain.c

201 lines · 3.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
22#include <X11/Xlib.h>
23#include <X11/Xutil.h>
24#include <X11/keysym.h>
25
26#include "sxiv.h"
27#include "image.h"
28#include "options.h"
29#include "window.h"
30
31void on_keypress(XEvent*);
32void on_configurenotify(XEvent*);
33
34void update_title();
35
36static void (*handler[LASTEvent])(XEvent*) = {
37 [KeyPress] = on_keypress,
38 [ConfigureNotify] = on_configurenotify
39};
40
41img_t img;
42win_t win;
43
44const char **filenames;
45unsigned int filecnt;
46unsigned int fileidx;
47
48#define TITLE_LEN 256
49char win_title[TITLE_LEN];
50
51void run() {
52 XEvent ev;
53
54 while (!XNextEvent(win.env.dpy, &ev)) {
55 if (handler[ev.type])
56 handler[ev.type](&ev);
57 }
58}
59
60int main(int argc, char **argv) {
61 int i;
62
63 parse_options(argc, argv);
64
65 if (!options->filecnt) {
66 print_usage();
67 exit(1);
68 }
69
70 if (!(filenames = (const char**) malloc(options->filecnt * sizeof(char*))))
71 DIE("could not allocate memory");
72
73 fileidx = 0;
74 filecnt = 0;
75
76 for (i = 0; i < options->filecnt; ++i) {
77 if (!(img_load(&img, options->filenames[i]) < 0))
78 filenames[filecnt++] = options->filenames[i];
79 }
80
81 if (!filecnt) {
82 fprintf(stderr, "sxiv: no valid image filename given, aborting\n");
83 exit(1);
84 }
85
86 win_open(&win);
87 img_init(&img, &win);
88
89 img_load(&img, filenames[fileidx]);
90 img_render(&img, &win);
91 update_title();
92
93 run();
94
95 cleanup();
96
97 return 0;
98}
99
100void cleanup() {
101 static int in = 0;
102
103 if (!in++) {
104 img_free(&img);
105 win_close(&win);
106 }
107}
108
109void on_keypress(XEvent *ev) {
110 char key;
111 KeySym keysym;
112 int changed;
113
114 if (!ev)
115 return;
116
117 XLookupString(&ev->xkey, &key, 1, &keysym, NULL);
118 changed = 0;
119
120 switch (keysym) {
121 case XK_Escape:
122 cleanup();
123 exit(2);
124 case XK_space:
125 key = 'n';
126 break;
127 case XK_BackSpace:
128 key = 'p';
129 break;
130 }
131
132 switch (key) {
133 case 'q':
134 cleanup();
135 exit(0);
136
137 /* navigate through image list */
138 case 'n':
139 if (fileidx + 1 < filecnt) {
140 img_load(&img, filenames[++fileidx]);
141 changed = 1;
142 }
143 break;
144 case 'p':
145 if (fileidx > 0) {
146 img_load(&img, filenames[--fileidx]);
147 changed = 1;
148 }
149 break;
150
151 /* zooming */
152 case '+':
153 case '=':
154 changed = img_zoom_in(&img);
155 break;
156 case '-':
157 changed = img_zoom_out(&img);
158 break;
159
160 /* panning */
161 case 'h':
162 changed = img_pan(&img, &win, PAN_LEFT);
163 break;
164 case 'j':
165 changed = img_pan(&img, &win, PAN_DOWN);
166 break;
167 case 'k':
168 changed = img_pan(&img, &win, PAN_UP);
169 break;
170 case 'l':
171 changed = img_pan(&img, &win, PAN_RIGHT);
172 break;
173 }
174
175 if (changed) {
176 img_render(&img, &win);
177 update_title();
178 }
179}
180
181void on_configurenotify(XEvent *ev) {
182 if (!ev)
183 return;
184
185 win_configure(&win, &ev->xconfigure);
186}
187
188void update_title() {
189 int n;
190
191 n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] <%d%%> %s", fileidx + 1,
192 filecnt, (int) (img.zoom * 100.0), filenames[fileidx]);
193
194 if (n >= TITLE_LEN) {
195 win_title[TITLE_LEN - 2] = '.';
196 win_title[TITLE_LEN - 3] = '.';
197 win_title[TITLE_LEN - 4] = '.';
198 }
199
200 win_set_title(&win, win_title);
201}