Krolyxon
Commit 8b3b283

Branches

Refs →

nsxivmain.c

116 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 parse_options(argc, argv);
54
55 if (!options->filecnt) {
56 print_usage();
57 exit(1);
58 }
59
60 fileidx = 0;
61
62 img.zoom = 1.0;
63 img.scalemode = SCALE_MODE;
64
65 win.w = WIN_WIDTH;
66 win.h = WIN_HEIGHT;
67
68 win_open(&win);
69 imlib_init(&win);
70
71 img_load(&img, options->filenames[fileidx]);
72 img_render(&img, &win);
73
74 run();
75
76 cleanup();
77
78 return 0;
79}
80
81void cleanup() {
82 static int in = 0;
83
84 if (!in++) {
85 imlib_destroy();
86 win_close(&win);
87 }
88}
89
90void on_keypress(XEvent *ev) {
91 KeySym keysym;
92
93 if (!ev)
94 return;
95
96 keysym = XLookupKeysym(&ev->xkey, 0);
97
98 switch (keysym) {
99 case XK_Escape:
100 cleanup();
101 exit(1);
102 case XK_q:
103 cleanup();
104 exit(0);
105 }
106}
107
108void on_configurenotify(XEvent *ev) {
109 if (!ev)
110 return;
111
112 win_configure(&win, &ev->xconfigure);
113}
114
115void on_expose(XEvent *ev) {
116}