@@ -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 */ |
@@ -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) { | |||||
} |
@@ -19,6 +19,8 @@ | |||||
#include <stdlib.h> | #include <stdlib.h> | ||||
#include <stdio.h> | #include <stdio.h> | ||||
#include <Imlib2.h> | |||||
#include "sxiv.h" | #include "sxiv.h" | ||||
#include "image.h" | #include "image.h" | ||||
@@ -32,7 +34,12 @@ void imlib_init(win_t *win) { | |||||
imlib_context_set_drawable(win->xwin); | 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) | if (!img || !filename) | ||||
return; | return; | ||||
@@ -53,7 +60,7 @@ void img_render(img_t *img, win_t *win) { | |||||
unsigned int sx, sy, sw, sh; | unsigned int sx, sy, sw, sh; | ||||
unsigned int dx, dy, dw, dh; | unsigned int dx, dy, dw, dh; | ||||
if (!img || !win) | if (!img || !win || !imlib_context_get_image()) | ||||
return; | return; | ||||
/* set zoom level to fit image into window */ | /* set zoom level to fit image into window */ | ||||
@@ -36,13 +36,13 @@ typedef struct img_s { | |||||
int h; | int h; | ||||
int x; | int x; | ||||
int y; | int y; | ||||
Imlib_Image *im; | Imlib_Image *im; | ||||
} img_t; | } img_t; | ||||
void imlib_init(win_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*); | void img_render(img_t*, win_t*); | ||||
#endif /* IMAGE_H */ | #endif /* IMAGE_H */ |
@@ -18,20 +18,63 @@ | |||||
#include <stdlib.h> | #include <stdlib.h> | ||||
#include <X11/Xlib.h> | |||||
#include <X11/keysym.h> | |||||
#include "sxiv.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) { | 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 | win_open(&win); | ||||
app.filenames = argv + 1; | imlib_init(&win); | ||||
app.filecnt = argc - 1; | |||||
app_init(&app); | img_load(&img, options->filenames[fileidx]); | ||||
app_run(&app); | img_render(&img, &win); | ||||
app_quit(&app); | run(); | ||||
cleanup(); | |||||
return 0; | return 0; | ||||
} | } | ||||
@@ -39,6 +82,36 @@ int main(int argc, char **argv) { | |||||
void cleanup() { | void cleanup() { | ||||
static int in = 0; | static int in = 0; | ||||
if (!in++) | if (!in++) { | ||||
app_quit(&app); | 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) { | |||||
} | } |
@@ -1,4 +1,4 @@ | |||||
/* sxiv: app.c | /* sxiv: options.c | ||||
* Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de> | * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de> | ||||
* | * | ||||
* This program is free software; you can redistribute it and/or modify | * 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. | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||||
*/ | */ | ||||
#include <X11/Xlib.h> | #define _XOPEN_SOURCE | ||||
#include "sxiv.h" | #include <stdlib.h> | ||||
#include "app.h" | #include <stdio.h> | ||||
#include "events.h" | #include <unistd.h> | ||||
void app_init(app_t *app) { | |||||
if (!app) | |||||
return; | |||||
app->fileidx = 0; | |||||
app->img.zoom = 1.0; | |||||
app->img.scalemode = SCALE_MODE; | |||||
app->win.w = WIN_WIDTH; | #include "sxiv.h" | ||||
app->win.h = WIN_HEIGHT; | #include "options.h" | ||||
win_open(&app->win); | options_t _options; | ||||
const options_t *options = (const options_t*) &_options; | |||||
imlib_init(&app->win); | |||||
} | |||||
void app_run(app_t *app) { | void print_usage() { | ||||
app_load_image(app); | printf("usage: sxiv [-hv] FILES...\n"); | ||||
event_loop(app); | |||||
} | } | ||||
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) { | int parse_options(int argc, char **argv) { | ||||
if (!app || app->fileidx >= app->filecnt || !app->filenames) | int opt; | ||||
return; | _options.filenames = (const char**) argv + 1; | ||||
_options.filecnt = argc - 1; | |||||
img_load(&app->img, app->filenames[app->fileidx]); | while ((opt = getopt(argc, argv, "hv")) != -1) { | ||||
switch (opt) { | |||||
img_render(&app->img, &app->win); | case '?': | ||||
return -1; | |||||
case 'h': | |||||
print_usage(); | |||||
exit(0); | |||||
case 'v': | |||||
print_version(); | |||||
exit(0); | |||||
} | |||||
} | |||||
return 0; | |||||
} | } |
@@ -1,4 +1,4 @@ | |||||
/* sxiv: events.h | /* sxiv: options.h | ||||
* Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de> | * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de> | ||||
* | * | ||||
* This program is free software; you can redistribute it and/or modify | * 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. | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||||
*/ | */ | ||||
#ifndef EVENTS_H | #ifndef OPTIONS_H | ||||
#define EVENTS_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 */ |
@@ -21,7 +21,7 @@ | |||||
#include "config.h" | #include "config.h" | ||||
#define VERSION "git-20110117" | #define VERSION "git-20110119" | ||||
#define MIN(a,b) ((a) < (b) ? (a) : (b)) | #define MIN(a,b) ((a) < (b) ? (a) : (b)) | ||||
#define MAX(a,b) ((a) > (b) ? (a) : (b)) | #define MAX(a,b) ((a) > (b) ? (a) : (b)) | ||||