Krolyxon
Commit a7e30bb

Branches

Refs →

nsxivmain.c

117 lines · 2.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
21#include <X11/Xlib.h>
22#include <X11/keysym.h>
23
24#include "sxiv.h"
25#include "image.h"
26#include "options.h"
27#include "window.h"
28
29void on_keypress(XEvent*);
30void on_configurenotify(XEvent*);
31void on_expose(XEvent*);
32
33static void (*handler[LASTEvent])(XEvent*) = {
34 [Expose] = on_expose,
35 [ConfigureNotify] = on_configurenotify,
36 [KeyPress] = on_keypress
37};
38
39img_t img;
40win_t win;
41unsigned int fileidx;
42
43void run() {
44 XEvent ev;
45
46 while (!XNextEvent(win.env.dpy, &ev)) {
47 if (handler[ev.type])
48 handler[ev.type](&ev);
49 }
50}
51
52int main(int argc, char **argv) {
53 if (parse_options(argc, argv) < 0)
54 return 1;
55
56 if (!options->filecnt) {
57 print_usage();
58 exit(1);
59 }
60
61 fileidx = 0;
62
63 img.zoom = 1.0;
64 img.scalemode = SCALE_MODE;
65
66 win.w = WIN_WIDTH;
67 win.h = WIN_HEIGHT;
68
69 win_open(&win);
70 imlib_init(&win);
71
72 img_load(&img, options->filenames[fileidx]);
73 img_render(&img, &win);
74
75 run();
76
77 cleanup();
78
79 return 0;
80}
81
82void cleanup() {
83 static int in = 0;
84
85 if (!in++) {
86 imlib_destroy();
87 win_close(&win);
88 }
89}
90
91void on_keypress(XEvent *ev) {
92 KeySym keysym;
93
94 if (!ev)
95 return;
96
97 keysym = XLookupKeysym(&ev->xkey, 0);
98
99 switch (keysym) {
100 case XK_Escape:
101 cleanup();
102 exit(1);
103 case XK_q:
104 cleanup();
105 exit(0);
106 }
107}
108
109void on_configurenotify(XEvent *ev) {
110 if (!ev)
111 return;
112
113 win_configure(&win, &ev->xconfigure);
114}
115
116void on_expose(XEvent *ev) {
117}