Krolyxon
Commit a732e75

Branches

Refs →

nsxivmain.c

155 lines · 3.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
22#include <X11/Xlib.h>
23#include <X11/keysym.h>
24
25#include "sxiv.h"
26#include "image.h"
27#include "options.h"
28#include "window.h"
29
30void on_keypress(XEvent*);
31void on_configurenotify(XEvent*);
32void on_expose(XEvent*);
33
34static void (*handler[LASTEvent])(XEvent*) = {
35 [Expose] = on_expose,
36 [ConfigureNotify] = on_configurenotify,
37 [KeyPress] = on_keypress
38};
39
40img_t img;
41win_t win;
42
43const char **filenames;
44unsigned int filecnt;
45unsigned int fileidx;
46
47void run() {
48 XEvent ev;
49
50 while (!XNextEvent(win.env.dpy, &ev)) {
51 if (handler[ev.type])
52 handler[ev.type](&ev);
53 }
54}
55
56int main(int argc, char **argv) {
57 int i;
58
59 parse_options(argc, argv);
60
61 if (!options->filecnt) {
62 print_usage();
63 exit(1);
64 }
65
66 if (!(filenames = (const char**) malloc(options->filecnt * sizeof(char*))))
67 DIE("could not allocate memory");
68
69 fileidx = 0;
70 filecnt = 0;
71
72 for (i = 0; i < options->filecnt; ++i) {
73 if (!(img_load(&img, options->filenames[i]) < 0))
74 filenames[filecnt++] = options->filenames[i];
75 }
76
77 if (!filecnt) {
78 fprintf(stderr, "sxiv: no valid image filename given, aborting\n");
79 exit(1);
80 }
81
82 img.zoom = 1.0;
83 img.scalemode = SCALE_MODE;
84
85 win.w = WIN_WIDTH;
86 win.h = WIN_HEIGHT;
87
88 win_open(&win);
89 imlib_init(&win);
90
91 img_load(&img, filenames[fileidx]);
92 img_display(&img, &win);
93
94 run();
95
96 cleanup();
97
98 return 0;
99}
100
101void cleanup() {
102 static int in = 0;
103
104 if (!in++) {
105 imlib_destroy();
106 win_close(&win);
107 }
108}
109
110void on_keypress(XEvent *ev) {
111 KeySym keysym;
112
113 if (!ev)
114 return;
115
116 keysym = XLookupKeysym(&ev->xkey, 0);
117
118 switch (keysym) {
119 case XK_Escape:
120 cleanup();
121 exit(1);
122 case XK_q:
123 cleanup();
124 exit(0);
125 case XK_n:
126 case XK_space:
127 if (fileidx + 1 < filecnt) {
128 img_load(&img, filenames[++fileidx]);
129 img_display(&img, &win);
130 }
131 break;
132 case XK_p:
133 case XK_BackSpace:
134 if (fileidx > 0) {
135 img_load(&img, filenames[--fileidx]);
136 img_display(&img, &win);
137 }
138 break;
139 }
140}
141
142void on_configurenotify(XEvent *ev) {
143 if (!ev)
144 return;
145
146 win_configure(&win, &ev->xconfigure);
147}
148
149void on_expose(XEvent *ev) {
150 if (!ev)
151 return;
152
153 img_render(&img, &win, ev->xexpose.x, ev->xexpose.y,
154 ev->xexpose.width, ev->xexpose.height);
155}