From a7e30bb081ab0a27147f97b8851d7bb76c39c51b Mon Sep 17 00:00:00 2001 From: Bert Date: Wed, 19 Jan 2011 14:07:45 +0100 Subject: Option handling, merged app.c & events.c into main.c --- main.c | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 83 insertions(+), 10 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index 9959657..6c3c2f3 100644 --- a/main.c +++ b/main.c @@ -18,20 +18,63 @@ #include +#include +#include + #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) { } -- cgit v1.2.3