aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app.h39
-rw-r--r--events.c72
-rw-r--r--image.c11
-rw-r--r--image.h4
-rw-r--r--main.c93
-rw-r--r--options.c (renamed from app.c)65
-rw-r--r--options.h (renamed from events.h)20
-rw-r--r--sxiv.h2
8 files changed, 143 insertions, 163 deletions
diff --git a/app.h b/app.h
deleted file mode 100644
index d4b2990..0000000
--- a/app.h
+++ /dev/null
@@ -1,39 +0,0 @@
-/* sxiv: app.h
- * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
-
-#ifndef APP_H
-#define APP_H
-
-#include "image.h"
-#include "window.h"
-
-typedef struct app_s {
- char **filenames;
- unsigned int filecnt;
- unsigned int fileidx;
- img_t img;
- win_t win;
-} app_t;
-
-void app_init(app_t*);
-void app_run(app_t*);
-void app_quit(app_t*);
-
-void app_load_image(app_t*);
-
-#endif /* APP_H */
diff --git a/events.c b/events.c
deleted file mode 100644
index 41a1d0a..0000000
--- a/events.c
+++ /dev/null
@@ -1,72 +0,0 @@
-/* sxiv: events.c
- * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
-
-#include <stdlib.h>
-
-#include <X11/Xlib.h>
-#include <X11/keysym.h>
-
-#include "events.h"
-#include "window.h"
-
-void on_keypress(app_t*, XEvent*);
-void on_configurenotify(app_t*, XEvent*);
-void on_expose(app_t*, XEvent*);
-
-static void (*handler[LASTEvent])(app_t*, XEvent*) = {
- [Expose] = on_expose,
- [ConfigureNotify] = on_configurenotify,
- [KeyPress] = on_keypress
-};
-
-void event_loop(app_t *app) {
- XEvent ev;
-
- while (!XNextEvent(app->win.env.dpy, &ev)) {
- if (handler[ev.type])
- handler[ev.type](app, &ev);
- }
-}
-
-void on_keypress(app_t *app, XEvent *ev) {
- KeySym keysym;
-
- if (!app || !ev)
- return;
-
- keysym = XLookupKeysym(&ev->xkey, 0);
-
- switch (keysym) {
- case XK_Escape:
- app_quit(app);
- exit(1);
- case XK_q:
- app_quit(app);
- exit(0);
- }
-}
-
-void on_configurenotify(app_t *app, XEvent *ev) {
- if (!app || !ev)
- return;
-
- win_configure(&app->win, &ev->xconfigure);
-}
-
-void on_expose(app_t *app, XEvent *ev) {
-}
diff --git a/image.c b/image.c
index 235b2d4..65f79de 100644
--- a/image.c
+++ b/image.c
@@ -19,6 +19,8 @@
#include <stdlib.h>
#include <stdio.h>
+#include <Imlib2.h>
+
#include "sxiv.h"
#include "image.h"
@@ -32,7 +34,12 @@ void imlib_init(win_t *win) {
imlib_context_set_drawable(win->xwin);
}
-void img_load(img_t *img, char *filename) {
+void imlib_destroy() {
+ if (imlib_context_get_image())
+ imlib_free_image();
+}
+
+void img_load(img_t *img, const char *filename) {
if (!img || !filename)
return;
@@ -53,7 +60,7 @@ void img_render(img_t *img, win_t *win) {
unsigned int sx, sy, sw, sh;
unsigned int dx, dy, dw, dh;
- if (!img || !win)
+ if (!img || !win || !imlib_context_get_image())
return;
/* set zoom level to fit image into window */
diff --git a/image.h b/image.h
index 7eaf197..17a913c 100644
--- a/image.h
+++ b/image.h
@@ -36,13 +36,13 @@ typedef struct img_s {
int h;
int x;
int y;
-
Imlib_Image *im;
} img_t;
void imlib_init(win_t*);
+void imlib_destroy();
-void img_load(img_t*, char*);
+void img_load(img_t*, const char*);
void img_render(img_t*, win_t*);
#endif /* IMAGE_H */
diff --git a/main.c b/main.c
index 9959657..6c3c2f3 100644
--- a/main.c
+++ b/main.c
@@ -18,20 +18,63 @@
#include <stdlib.h>
+#include <X11/Xlib.h>
+#include <X11/keysym.h>
+
#include "sxiv.h"
-#include "app.h"
+#include "image.h"
+#include "options.h"
+#include "window.h"
+
+void on_keypress(XEvent*);
+void on_configurenotify(XEvent*);
+void on_expose(XEvent*);
+
+static void (*handler[LASTEvent])(XEvent*) = {
+ [Expose] = on_expose,
+ [ConfigureNotify] = on_configurenotify,
+ [KeyPress] = on_keypress
+};
+
+img_t img;
+win_t win;
+unsigned int fileidx;
+
+void run() {
+ XEvent ev;
-app_t app;
+ while (!XNextEvent(win.env.dpy, &ev)) {
+ if (handler[ev.type])
+ handler[ev.type](&ev);
+ }
+}
int main(int argc, char **argv) {
+ if (parse_options(argc, argv) < 0)
+ return 1;
+
+ if (!options->filecnt) {
+ print_usage();
+ exit(1);
+ }
+
+ fileidx = 0;
+
+ img.zoom = 1.0;
+ img.scalemode = SCALE_MODE;
+
+ win.w = WIN_WIDTH;
+ win.h = WIN_HEIGHT;
- // TODO: parse cmd line arguments properly
- app.filenames = argv + 1;
- app.filecnt = argc - 1;
+ win_open(&win);
+ imlib_init(&win);
- app_init(&app);
- app_run(&app);
- app_quit(&app);
+ img_load(&img, options->filenames[fileidx]);
+ img_render(&img, &win);
+
+ run();
+
+ cleanup();
return 0;
}
@@ -39,6 +82,36 @@ int main(int argc, char **argv) {
void cleanup() {
static int in = 0;
- if (!in++)
- app_quit(&app);
+ if (!in++) {
+ imlib_destroy();
+ win_close(&win);
+ }
+}
+
+void on_keypress(XEvent *ev) {
+ KeySym keysym;
+
+ if (!ev)
+ return;
+
+ keysym = XLookupKeysym(&ev->xkey, 0);
+
+ switch (keysym) {
+ case XK_Escape:
+ cleanup();
+ exit(1);
+ case XK_q:
+ cleanup();
+ exit(0);
+ }
+}
+
+void on_configurenotify(XEvent *ev) {
+ if (!ev)
+ return;
+
+ win_configure(&win, &ev->xconfigure);
+}
+
+void on_expose(XEvent *ev) {
}
diff --git a/app.c b/options.c
index 3c1f88a..4968d2e 100644
--- a/app.c
+++ b/options.c
@@ -1,4 +1,4 @@
-/* sxiv: app.c
+/* sxiv: options.c
* Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de>
*
* This program is free software; you can redistribute it and/or modify
@@ -16,42 +16,45 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
-#include <X11/Xlib.h>
+#define _XOPEN_SOURCE
-#include "sxiv.h"
-#include "app.h"
-#include "events.h"
-
-void app_init(app_t *app) {
- if (!app)
- return;
-
- app->fileidx = 0;
-
- app->img.zoom = 1.0;
- app->img.scalemode = SCALE_MODE;
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
- app->win.w = WIN_WIDTH;
- app->win.h = WIN_HEIGHT;
+#include "sxiv.h"
+#include "options.h"
- win_open(&app->win);
-
- imlib_init(&app->win);
-}
+options_t _options;
+const options_t *options = (const options_t*) &_options;
-void app_run(app_t *app) {
- app_load_image(app);
- event_loop(app);
+void print_usage() {
+ printf("usage: sxiv [-hv] FILES...\n");
}
-void app_quit(app_t *app) {
+void print_version() {
+ printf("sxiv - simple x image viewer\n");
+ printf("Version %s, written by Bert Muennich\n", VERSION);
}
-void app_load_image(app_t *app) {
- if (!app || app->fileidx >= app->filecnt || !app->filenames)
- return;
-
- img_load(&app->img, app->filenames[app->fileidx]);
-
- img_render(&app->img, &app->win);
+int parse_options(int argc, char **argv) {
+ int opt;
+
+ _options.filenames = (const char**) argv + 1;
+ _options.filecnt = argc - 1;
+
+ while ((opt = getopt(argc, argv, "hv")) != -1) {
+ switch (opt) {
+ case '?':
+ return -1;
+ case 'h':
+ print_usage();
+ exit(0);
+ case 'v':
+ print_version();
+ exit(0);
+ }
+ }
+
+ return 0;
}
diff --git a/events.h b/options.h
index 06238d9..3003dff 100644
--- a/events.h
+++ b/options.h
@@ -1,4 +1,4 @@
-/* sxiv: events.h
+/* sxiv: options.h
* Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de>
*
* This program is free software; you can redistribute it and/or modify
@@ -16,11 +16,19 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
-#ifndef EVENTS_H
-#define EVENTS_H
+#ifndef OPTIONS_H
+#define OPTIONS_H
-#include "app.h"
+typedef struct options_s {
+ const char **filenames;
+ unsigned int filecnt;
+} options_t;
-void event_loop(app_t*);
+extern const options_t *options;
-#endif /* EVENTS_H */
+void print_usage();
+void print_version();
+
+int parse_options(int, char**);
+
+#endif /* OPTIONS_H */
diff --git a/sxiv.h b/sxiv.h
index 97252af..d8e329f 100644
--- a/sxiv.h
+++ b/sxiv.h
@@ -21,7 +21,7 @@
#include "config.h"
-#define VERSION "git-20110117"
+#define VERSION "git-20110119"
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define MAX(a,b) ((a) > (b) ? (a) : (b))