diff options
author | Harry Jeffery <harry@exec64.co.uk> | 2017-11-24 16:55:53 +0000 |
---|---|---|
committer | Harry Jeffery <harry@exec64.co.uk> | 2017-11-24 16:55:53 +0000 |
commit | fedd2057a22c767792be1d51d3eabac83824a5a0 (patch) | |
tree | ed4c321a278d3e7b490c123f0e90c68f8a3ddaca | |
parent | d5f69f2e830bad9b14a554853daf427b978fee0b (diff) | |
download | imv-fedd2057a22c767792be1d51d3eabac83824a5a0.tar.gz |
loader: Use event to pass image loading error to main thread
-rw-r--r-- | src/imv.c | 38 | ||||
-rw-r--r-- | src/loader.c | 30 | ||||
-rw-r--r-- | src/loader.h | 11 |
3 files changed, 35 insertions, 44 deletions
@@ -95,6 +95,7 @@ struct imv { bool ttf_init; struct { unsigned int NEW_IMAGE; + unsigned int BAD_IMAGE; } events; struct { int width; @@ -495,23 +496,6 @@ int imv_run(struct imv *imv) break; } - /* check if an image failed to load, if so, remove it from our image list */ - char *err_path = imv_loader_get_error(imv->loader); - if(err_path) { - imv_navigator_remove(imv->navigator, err_path); - - /* special case: the image came from stdin */ - if(strcmp(err_path, "-") == 0) { - if(imv->stdin_image_data) { - free(imv->stdin_image_data); - imv->stdin_image_data = NULL; - imv->stdin_image_data_len = 0; - } - fprintf(stderr, "Failed to load image from stdin.\n"); - } - free(err_path); - } - /* Check if navigator wrapped around paths lists */ if(!imv->cycle_input && imv_navigator_wrapped(imv->navigator)) { break; @@ -631,9 +615,12 @@ static bool setup_window(struct imv *imv) /* register custom events */ imv->events.NEW_IMAGE = SDL_RegisterEvents(1); + imv->events.BAD_IMAGE = SDL_RegisterEvents(1); /* tell the loader which event ids it should use */ - imv_loader_set_event_types(imv->loader, imv->events.NEW_IMAGE); + imv_loader_set_event_types(imv->loader, + imv->events.NEW_IMAGE, + imv->events.BAD_IMAGE); imv->sdl_init = true; @@ -707,6 +694,21 @@ static void handle_event(struct imv *imv, SDL_Event *event) imv->need_redraw = true; imv->need_rescale |= event->user.code; return; + } else if(event->type == imv->events.BAD_IMAGE) { + /* an image failed to load, remove it from our image list */ + char *err_path = event->user.data1; + imv_navigator_remove(imv->navigator, err_path); + + /* special case: the image came from stdin */ + if(strcmp(err_path, "-") == 0) { + if(imv->stdin_image_data) { + free(imv->stdin_image_data); + imv->stdin_image_data = NULL; + imv->stdin_image_data_len = 0; + } + fprintf(stderr, "Failed to load image from stdin.\n"); + } + free(err_path); } switch(event->type) { diff --git a/src/loader.c b/src/loader.c index cbebf2c..2a3a08c 100644 --- a/src/loader.c +++ b/src/loader.c @@ -101,23 +101,12 @@ void imv_loader_load(struct imv_loader *ldr, const char *path, pthread_mutex_unlock(&ldr->lock); } -void imv_loader_set_event_types(struct imv_loader *ldr, unsigned int new_image) +void imv_loader_set_event_types(struct imv_loader *ldr, + unsigned int new_image, + unsigned int bad_image) { ldr->new_image_event = new_image; -} - -char *imv_loader_get_error(struct imv_loader *ldr) -{ - char *err = NULL; - pthread_mutex_lock(&ldr->lock); - - if(ldr->out_err) { - err = ldr->out_err; - ldr->out_err = NULL; - } - - pthread_mutex_unlock(&ldr->lock); - return err; + ldr->bad_image_event = bad_image; } void imv_loader_load_next_frame(struct imv_loader *ldr) @@ -418,11 +407,14 @@ static void *bg_next_frame(void *data) static void error_occurred(struct imv_loader *ldr) { pthread_mutex_lock(&ldr->lock); - if(ldr->out_err) { - free(ldr->out_err); - } - ldr->out_err = strdup(ldr->path); + char *err_path = strdup(ldr->path); pthread_mutex_unlock(&ldr->lock); + + SDL_Event event; + SDL_zero(event); + event.type = ldr->bad_image_event; + event.user.data1 = err_path; + SDL_PushEvent(&event); } diff --git a/src/loader.h b/src/loader.h index 07fe068..197b6e2 100644 --- a/src/loader.h +++ b/src/loader.h @@ -31,7 +31,6 @@ struct imv_loader { BYTE *buffer; size_t buffer_size; FIMEMORY *fi_buffer; - char *out_err; FIMULTIBITMAP *mbmp; FIBITMAP *bmp; int width; @@ -41,6 +40,7 @@ struct imv_loader { int num_frames; double frame_time; unsigned int new_image_event; + unsigned int bad_image_event; }; /* Creates an instance of imv_loader */ @@ -54,12 +54,9 @@ void imv_loader_load(struct imv_loader *ldr, const char *path, const void *buffer, const size_t buffer_size); /* Set the custom event types for returning data */ -void imv_loader_set_event_types(struct imv_loader *ldr, unsigned int new_image); - -/* If a file failed to load, return the path to that file. Otherwise returns - * NULL. Only returns the path once. Caller is responsible for cleaning up the - * string returned. */ -char *imv_loader_get_error(struct imv_loader *ldr); +void imv_loader_set_event_types(struct imv_loader *ldr, + unsigned int new_image, + unsigned int bad_image); /* Trigger the next frame of the currently loaded image to be loaded and * returned as soon as possible. */ |