Krolyxon
Commit 72e56e5

Branches

Refs →

nsxivmain.c

190 lines · 3.5 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 int len;
112 KeySym keysym;
113
114 if (!ev)
115 return;
116
117 len = XLookupString(&ev->xkey, &key, 1, &keysym, NULL);
118
119 switch (keysym) {
120 case XK_Escape:
121 cleanup();
122 exit(2);
123 case XK_space:
124 key = 'n';
125 len = 1;
126 break;
127 case XK_BackSpace:
128 key = 'p';
129 len = 1;
130 break;
131 }
132
133 if (!len)
134 return;
135
136 switch (key) {
137 case 'q':
138 cleanup();
139 exit(0);
140 case 'n':
141 if (fileidx + 1 < filecnt) {
142 img_load(&img, filenames[++fileidx]);
143 img_render(&img, &win);
144 update_title();
145 }
146 break;
147 case 'p':
148 if (fileidx > 0) {
149 img_load(&img, filenames[--fileidx]);
150 img_render(&img, &win);
151 update_title();
152 }
153 break;
154 case '+':
155 case '=':
156 if (img_zoom_in(&img)) {
157 img_render(&img, &win);
158 update_title();
159 }
160 break;
161 case '-':
162 if (img_zoom_out(&img)) {
163 img_render(&img, &win);
164 update_title();
165 }
166 break;
167 }
168}
169
170void on_configurenotify(XEvent *ev) {
171 if (!ev)
172 return;
173
174 win_configure(&win, &ev->xconfigure);
175}
176
177void update_title() {
178 int n;
179
180 n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] <%d%%> %s", fileidx + 1,
181 filecnt, (int) (img.zoom * 100.0), filenames[fileidx]);
182
183 if (n >= TITLE_LEN) {
184 win_title[TITLE_LEN - 2] = '.';
185 win_title[TITLE_LEN - 3] = '.';
186 win_title[TITLE_LEN - 4] = '.';
187 }
188
189 win_set_title(&win, win_title);
190}