Krolyxon
Commit a7e30bb

Branches

Refs →

nsxiva7e30bb

Option handling, merged app.c & events.c into main.c

Author
Bert
Date
2011-01-19 14:07 +0100
Parent
79c7e61
Commit
a7e30bb081ab0a27147f97b8851d7bb76c39c51b

+143 −163

8 files changed

  1. D app.h +0 −39
  2. D events.c +0 −72
  3. M image.c +9 −2
  4. M image.h +2 −2
  5. M main.c +83 −10
  6. R app.c → options.c +34 −31
  7. R events.h → options.h +14 −6
  8. M sxiv.h +1 −1
D app.h View
@@ -1,39 +0,0 @@
1−/* sxiv: app.h
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−#ifndef APP_H
20−#define APP_H
21−
22−#include "image.h"
23−#include "window.h"
24−
25−typedef struct app_s {
26− char **filenames;
27− unsigned int filecnt;
28− unsigned int fileidx;
29− img_t img;
30− win_t win;
31−} app_t;
32−
33−void app_init(app_t*);
34−void app_run(app_t*);
35−void app_quit(app_t*);
36−
37−void app_load_image(app_t*);
38−
39−#endif /* APP_H */
D events.c View
@@ -1,72 +0,0 @@
1−/* sxiv: events.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 "events.h"
25−#include "window.h"
26−
27−void on_keypress(app_t*, XEvent*);
28−void on_configurenotify(app_t*, XEvent*);
29−void on_expose(app_t*, XEvent*);
30−
31−static void (*handler[LASTEvent])(app_t*, XEvent*) = {
32− [Expose] = on_expose,
33− [ConfigureNotify] = on_configurenotify,
34− [KeyPress] = on_keypress
35−};
36−
37−void event_loop(app_t *app) {
38− XEvent ev;
39−
40− while (!XNextEvent(app->win.env.dpy, &ev)) {
41− if (handler[ev.type])
42− handler[ev.type](app, &ev);
43− }
44−}
45−
46−void on_keypress(app_t *app, XEvent *ev) {
47− KeySym keysym;
48−
49− if (!app || !ev)
50− return;
51−
52− keysym = XLookupKeysym(&ev->xkey, 0);
53−
54− switch (keysym) {
55− case XK_Escape:
56− app_quit(app);
57− exit(1);
58− case XK_q:
59− app_quit(app);
60− exit(0);
61− }
62−}
63−
64−void on_configurenotify(app_t *app, XEvent *ev) {
65− if (!app || !ev)
66− return;
67−
68− win_configure(&app->win, &ev->xconfigure);
69−}
70−
71−void on_expose(app_t *app, XEvent *ev) {
72−}
M image.c View
@@ -19,6 +19,8 @@
1919 #include <stdlib.h>
2020 #include <stdio.h>
2121
22+#include <Imlib2.h>
23+
2224 #include "sxiv.h"
2325 #include "image.h"
2426
@@ -32,7 +34,12 @@ void imlib_init(win_t *win) {
3234 imlib_context_set_drawable(win->xwin);
3335 }
3436
35−void img_load(img_t *img, char *filename) {
37+void imlib_destroy() {
38+ if (imlib_context_get_image())
39+ imlib_free_image();
40+}
41+
42+void img_load(img_t *img, const char *filename) {
3643 if (!img || !filename)
3744 return;
3845
@@ -53,7 +60,7 @@ void img_render(img_t *img, win_t *win) {
5360 unsigned int sx, sy, sw, sh;
5461 unsigned int dx, dy, dw, dh;
5562
56− if (!img || !win)
63+ if (!img || !win || !imlib_context_get_image())
5764 return;
5865
5966 /* set zoom level to fit image into window */
M image.h View
@@ -36,13 +36,13 @@ typedef struct img_s {
3636 int h;
3737 int x;
3838 int y;
39−
4039 Imlib_Image *im;
4140 } img_t;
4241
4342 void imlib_init(win_t*);
43+void imlib_destroy();
4444
45−void img_load(img_t*, char*);
45+void img_load(img_t*, const char*);
4646 void img_render(img_t*, win_t*);
4747
4848 #endif /* IMAGE_H */
M main.c View
@@ -18,20 +18,63 @@
1818
1919 #include <stdlib.h>
2020
21+#include <X11/Xlib.h>
22+#include <X11/keysym.h>
23+
2124 #include "sxiv.h"
22−#include "app.h"
25+#include "image.h"
26+#include "options.h"
27+#include "window.h"
28+
29+void on_keypress(XEvent*);
30+void on_configurenotify(XEvent*);
31+void on_expose(XEvent*);
32+
33+static void (*handler[LASTEvent])(XEvent*) = {
34+ [Expose] = on_expose,
35+ [ConfigureNotify] = on_configurenotify,
36+ [KeyPress] = on_keypress
37+};
38+
39+img_t img;
40+win_t win;
41+unsigned int fileidx;
42+
43+void run() {
44+ XEvent ev;
2345
24−app_t app;
46+ while (!XNextEvent(win.env.dpy, &ev)) {
47+ if (handler[ev.type])
48+ handler[ev.type](&ev);
49+ }
50+}
2551
2652 int 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;
2768
28− // TODO: parse cmd line arguments properly
29− app.filenames = argv + 1;
30− app.filecnt = argc - 1;
69+ win_open(&win);
70+ imlib_init(&win);
3171
32− app_init(&app);
33− app_run(&app);
34− app_quit(&app);
72+ img_load(&img, options->filenames[fileidx]);
73+ img_render(&img, &win);
74+
75+ run();
76+
77+ cleanup();
3578
3679 return 0;
3780 }
@@ -39,6 +82,36 @@ int main(int argc, char **argv) {
3982 void cleanup() {
4083 static int in = 0;
4184
42− if (!in++)
43− app_quit(&app);
85+ if (!in++) {
86+ imlib_destroy();
87+ win_close(&win);
88+ }
89+}
90+
91+void 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+
109+void on_configurenotify(XEvent *ev) {
110+ if (!ev)
111+ return;
112+
113+ win_configure(&win, &ev->xconfigure);
114+}
115+
116+void on_expose(XEvent *ev) {
44117 }
R app.c → options.c View
@@ -1,4 +1,4 @@
1−/* sxiv: app.c
1+/* sxiv: options.c
22 * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de>
33 *
44 * This program is free software; you can redistribute it and/or modify
@@ -16,42 +16,45 @@
1616 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1717 */
1818
19−#include <X11/Xlib.h>
19+#define _XOPEN_SOURCE
2020
21−#include "sxiv.h"
22−#include "app.h"
23−#include "events.h"
24−
25−void app_init(app_t *app) {
26− if (!app)
27− return;
28−
29− app->fileidx = 0;
30−
31− app->img.zoom = 1.0;
32− app->img.scalemode = SCALE_MODE;
21+#include <stdlib.h>
22+#include <stdio.h>
23+#include <unistd.h>
3324
34− app->win.w = WIN_WIDTH;
35− app->win.h = WIN_HEIGHT;
25+#include "sxiv.h"
26+#include "options.h"
3627
37− win_open(&app->win);
38−
39− imlib_init(&app->win);
40−}
28+options_t _options;
29+const options_t *options = (const options_t*) &_options;
4130
42−void app_run(app_t *app) {
43− app_load_image(app);
44− event_loop(app);
31+void print_usage() {
32+ printf("usage: sxiv [-hv] FILES...\n");
4533 }
4634
47−void app_quit(app_t *app) {
35+void print_version() {
36+ printf("sxiv - simple x image viewer\n");
37+ printf("Version %s, written by Bert Muennich\n", VERSION);
4838 }
4939
50−void app_load_image(app_t *app) {
51− if (!app || app->fileidx >= app->filecnt || !app->filenames)
52− return;
53−
54− img_load(&app->img, app->filenames[app->fileidx]);
55−
56− img_render(&app->img, &app->win);
40+int parse_options(int argc, char **argv) {
41+ int opt;
42+
43+ _options.filenames = (const char**) argv + 1;
44+ _options.filecnt = argc - 1;
45+
46+ while ((opt = getopt(argc, argv, "hv")) != -1) {
47+ switch (opt) {
48+ case '?':
49+ return -1;
50+ case 'h':
51+ print_usage();
52+ exit(0);
53+ case 'v':
54+ print_version();
55+ exit(0);
56+ }
57+ }
58+
59+ return 0;
5760 }
R events.h → options.h View
@@ -1,4 +1,4 @@
1−/* sxiv: events.h
1+/* sxiv: options.h
22 * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de>
33 *
44 * This program is free software; you can redistribute it and/or modify
@@ -16,11 +16,19 @@
1616 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1717 */
1818
19−#ifndef EVENTS_H
20−#define EVENTS_H
19+#ifndef OPTIONS_H
20+#define OPTIONS_H
2121
22−#include "app.h"
22+typedef struct options_s {
23+ const char **filenames;
24+ unsigned int filecnt;
25+} options_t;
2326
24−void event_loop(app_t*);
27+extern const options_t *options;
2528
26−#endif /* EVENTS_H */
29+void print_usage();
30+void print_version();
31+
32+int parse_options(int, char**);
33+
34+#endif /* OPTIONS_H */
M sxiv.h View
@@ -21,7 +21,7 @@
2121
2222 #include "config.h"
2323
24−#define VERSION "git-20110117"
24+#define VERSION "git-20110119"
2525
2626 #define MIN(a,b) ((a) < (b) ? (a) : (b))
2727 #define MAX(a,b) ((a) > (b) ? (a) : (b))