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 - * - * 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 - * - * 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 - -#include -#include - -#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 #include +#include + #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 -#include "sxiv.h" -#include "app.h" +#include +#include -app_t app; +#include "sxiv.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; + + 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; - // TODO: parse cmd line arguments properly - app.filenames = argv + 1; - app.filecnt = argc - 1; + if (!options->filecnt) { + print_usage(); + exit(1); + } - app_init(&app); - app_run(&app); - app_quit(&app); + fileidx = 0; + + img.zoom = 1.0; + img.scalemode = SCALE_MODE; + + win.w = WIN_WIDTH; + win.h = WIN_HEIGHT; + + win_open(&win); + imlib_init(&win); + + 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 similarity index 53% rename from app.c rename to 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 * * 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 +#define _XOPEN_SOURCE + +#include +#include +#include #include "sxiv.h" -#include "app.h" -#include "events.h" +#include "options.h" -void app_init(app_t *app) { - if (!app) - return; +options_t _options; +const options_t *options = (const options_t*) &_options; - app->fileidx = 0; - - app->img.zoom = 1.0; - app->img.scalemode = SCALE_MODE; - - app->win.w = WIN_WIDTH; - app->win.h = WIN_HEIGHT; - - win_open(&app->win); - - imlib_init(&app->win); +void print_usage() { + printf("usage: sxiv [-hv] FILES...\n"); } -void app_run(app_t *app) { - app_load_image(app); - event_loop(app); +void print_version() { + printf("sxiv - simple x image viewer\n"); + printf("Version %s, written by Bert Muennich\n", VERSION); } -void app_quit(app_t *app) { -} - -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 similarity index 74% rename from events.h rename to 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 * * 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))