Krolyxon
Commit 629d373

Branches

Refs →

nsxivmain.c

196 lines · 3.6 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 img.zoom = 1.0;
87 img.scalemode = SCALE_MODE;
88
89 win.w = WIN_WIDTH;
90 win.h = WIN_HEIGHT;
91
92 win_open(&win);
93 imlib_init(&win);
94
95 img_load(&img, filenames[fileidx]);
96 img_render(&img, &win);
97 update_title();
98
99 run();
100
101 cleanup();
102
103 return 0;
104}
105
106void cleanup() {
107 static int in = 0;
108
109 if (!in++) {
110 imlib_destroy();
111 win_close(&win);
112 }
113}
114
115void on_keypress(XEvent *ev) {
116 char key;
117 int len;
118 KeySym keysym;
119
120 if (!ev)
121 return;
122
123 len = XLookupString(&ev->xkey, &key, 1, &keysym, NULL);
124
125 switch (keysym) {
126 case XK_Escape:
127 cleanup();
128 exit(2);
129 case XK_space:
130 key = 'n';
131 len = 1;
132 break;
133 case XK_BackSpace:
134 key = 'p';
135 len = 1;
136 break;
137 }
138
139 if (!len)
140 return;
141
142 switch (key) {
143 case 'q':
144 cleanup();
145 exit(0);
146 case 'n':
147 if (fileidx + 1 < filecnt) {
148 img_load(&img, filenames[++fileidx]);
149 img_render(&img, &win);
150 update_title();
151 }
152 break;
153 case 'p':
154 if (fileidx > 0) {
155 img_load(&img, filenames[--fileidx]);
156 img_render(&img, &win);
157 update_title();
158 }
159 break;
160 case '+':
161 case '=':
162 if (img_zoom_in(&img)) {
163 img_render(&img, &win);
164 update_title();
165 }
166 break;
167 case '-':
168 if (img_zoom_out(&img)) {
169 img_render(&img, &win);
170 update_title();
171 }
172 break;
173 }
174}
175
176void on_configurenotify(XEvent *ev) {
177 if (!ev)
178 return;
179
180 win_configure(&win, &ev->xconfigure);
181}
182
183void update_title() {
184 int n;
185
186 n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] <%d%%> %s", fileidx + 1,
187 filecnt, (int) (img.zoom * 100.0), filenames[fileidx]);
188
189 if (n >= TITLE_LEN) {
190 win_title[TITLE_LEN - 2] = '.';
191 win_title[TITLE_LEN - 3] = '.';
192 win_title[TITLE_LEN - 4] = '.';
193 }
194
195 win_set_title(&win, win_title);
196}