Krolyxon
Commit 029e120

Branches

Refs →

nsxivmain.c

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