Check all given files before open the first

This commit is contained in:
Bert 2011-01-20 16:24:48 +01:00
parent 004f297ebb
commit a732e75d8a
3 changed files with 33 additions and 9 deletions

12
image.c
View File

@ -39,22 +39,26 @@ void imlib_destroy() {
imlib_free_image(); imlib_free_image();
} }
void img_load(img_t *img, const char *filename) { int img_load(img_t *img, const char *filename) {
Imlib_Image *im; Imlib_Image *im;
if (!img || !filename) if (!img || !filename)
return; return -1;
if (imlib_context_get_image()) if (imlib_context_get_image())
imlib_free_image(); imlib_free_image();
if (!(im = imlib_load_image(filename))) if (!(im = imlib_load_image(filename))) {
DIE("could not open image: %s", filename); WARN("could not open image: %s", filename);
return -1;
}
imlib_context_set_image(im); imlib_context_set_image(im);
img->w = imlib_image_get_width(); img->w = imlib_image_get_width();
img->h = imlib_image_get_height(); img->h = imlib_image_get_height();
return 0;
} }
void img_display(img_t *img, win_t *win) { void img_display(img_t *img, win_t *win) {

View File

@ -39,7 +39,7 @@ typedef struct img_s {
void imlib_init(win_t*); void imlib_init(win_t*);
void imlib_destroy(); void imlib_destroy();
void img_load(img_t*, const char*); int img_load(img_t*, const char*);
void img_display(img_t*, win_t*); void img_display(img_t*, win_t*);
void img_render(img_t*, win_t*, int, int, int, int); void img_render(img_t*, win_t*, int, int, int, int);

28
main.c
View File

@ -17,6 +17,7 @@
*/ */
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h>
#include <X11/Xlib.h> #include <X11/Xlib.h>
#include <X11/keysym.h> #include <X11/keysym.h>
@ -38,6 +39,9 @@ static void (*handler[LASTEvent])(XEvent*) = {
img_t img; img_t img;
win_t win; win_t win;
const char **filenames;
unsigned int filecnt;
unsigned int fileidx; unsigned int fileidx;
void run() { void run() {
@ -50,6 +54,8 @@ void run() {
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
int i;
parse_options(argc, argv); parse_options(argc, argv);
if (!options->filecnt) { if (!options->filecnt) {
@ -57,7 +63,21 @@ int main(int argc, char **argv) {
exit(1); exit(1);
} }
if (!(filenames = (const char**) malloc(options->filecnt * sizeof(char*))))
DIE("could not allocate memory");
fileidx = 0; fileidx = 0;
filecnt = 0;
for (i = 0; i < options->filecnt; ++i) {
if (!(img_load(&img, options->filenames[i]) < 0))
filenames[filecnt++] = options->filenames[i];
}
if (!filecnt) {
fprintf(stderr, "sxiv: no valid image filename given, aborting\n");
exit(1);
}
img.zoom = 1.0; img.zoom = 1.0;
img.scalemode = SCALE_MODE; img.scalemode = SCALE_MODE;
@ -68,7 +88,7 @@ int main(int argc, char **argv) {
win_open(&win); win_open(&win);
imlib_init(&win); imlib_init(&win);
img_load(&img, options->filenames[fileidx]); img_load(&img, filenames[fileidx]);
img_display(&img, &win); img_display(&img, &win);
run(); run();
@ -104,15 +124,15 @@ void on_keypress(XEvent *ev) {
exit(0); exit(0);
case XK_n: case XK_n:
case XK_space: case XK_space:
if (fileidx + 1 < options->filecnt) { if (fileidx + 1 < filecnt) {
img_load(&img, options->filenames[++fileidx]); img_load(&img, filenames[++fileidx]);
img_display(&img, &win); img_display(&img, &win);
} }
break; break;
case XK_p: case XK_p:
case XK_BackSpace: case XK_BackSpace:
if (fileidx > 0) { if (fileidx > 0) {
img_load(&img, options->filenames[--fileidx]); img_load(&img, filenames[--fileidx]);
img_display(&img, &win); img_display(&img, &win);
} }
break; break;