diff --git a/apps/file-browser.c b/apps/file-browser.c index 383b2db9..963206ca 100644 --- a/apps/file-browser.c +++ b/apps/file-browser.c @@ -32,7 +32,6 @@ #include #include #include -#include #define APPLICATION_TITLE "File Browser" #define SCROLL_AMOUNT 120 @@ -1060,7 +1059,7 @@ static void draw_background(int width, int height) { } } - load_sprite_jpg(wallpaper, wallpaper_path); + load_sprite(wallpaper, wallpaper_path); if (free_it) { free(wallpaper_path); diff --git a/apps/glogin-provider.c b/apps/glogin-provider.c index b475290b..dad04933 100644 --- a/apps/glogin-provider.c +++ b/apps/glogin-provider.c @@ -25,7 +25,6 @@ #include #include #include -#include #include #define TRACE_APP_NAME "glogin-provider" @@ -295,7 +294,7 @@ redo_everything: TRACE("Loading wallpaper..."); { sprite_t * wallpaper = malloc(sizeof(sprite_t)); - load_sprite_jpg(wallpaper, WALLPAPER); + load_sprite(wallpaper, WALLPAPER); float x = (float)width / (float)wallpaper->width; float y = (float)height / (float)wallpaper->height; diff --git a/apps/imgviewer.c b/apps/imgviewer.c index 4ac6b0b3..26a803cc 100644 --- a/apps/imgviewer.c +++ b/apps/imgviewer.c @@ -25,8 +25,6 @@ #include #include #include -#include -#include /* Pointer to graphics memory */ static yutani_t * yctx; @@ -151,14 +149,7 @@ int main(int argc, char * argv[]) { decor_width = bounds.width; decor_height = bounds.height; - int status; - if (strstr(argv[optind],".jpg")) { - status = load_sprite_jpg(&img, argv[optind]); - } else if (strstr(argv[optind],".png")) { - status = load_sprite_png(&img, argv[optind]); - } else { - status = load_sprite(&img, argv[optind]); - } + int status = load_sprite(&img, argv[optind]); if (status) { fprintf(stderr, "%s: failed to open image %s\n", argv[0], argv[optind]); return 1; diff --git a/apps/wallpaper-picker.c b/apps/wallpaper-picker.c index a1507bda..abe55c89 100644 --- a/apps/wallpaper-picker.c +++ b/apps/wallpaper-picker.c @@ -13,7 +13,6 @@ #include #include #include -#include #include #include @@ -157,8 +156,8 @@ void set_hilight(struct TTKButton * button, int hilight) { void load_wallpaper(void) { if (wallpaper.bitmap) free(wallpaper.bitmap); wallpaper.bitmap = NULL; - /* load JPG */ - load_sprite_jpg(&wallpaper, wallpaper_path); + /* load wallpaper */ + load_sprite(&wallpaper, wallpaper_path); /* Ensures we render correctly when scaling */ wallpaper.alpha = ALPHA_EMBEDDED; } diff --git a/base/usr/include/toaru/graphics.h b/base/usr/include/toaru/graphics.h index 5855fec3..d9808978 100644 --- a/base/usr/include/toaru/graphics.h +++ b/base/usr/include/toaru/graphics.h @@ -75,6 +75,7 @@ extern void blur_context_box(gfx_context_t * _src, int radius); extern void sprite_free(sprite_t * sprite); extern int load_sprite(sprite_t * sprite, char * filename); +extern int load_sprite_bmp(sprite_t * sprite, char * filename); //extern int load_sprite_png(sprite_t * sprite, char * file); extern void draw_sprite(gfx_context_t * ctx, sprite_t * sprite, int32_t x, int32_t y); extern void draw_line(gfx_context_t * ctx, int32_t x0, int32_t x1, int32_t y0, int32_t y1, uint32_t color); diff --git a/lib/graphics.c b/lib/graphics.c index a4d19a2f..9f361ab4 100644 --- a/lib/graphics.c +++ b/lib/graphics.c @@ -11,6 +11,7 @@ #include #include #include +#include #include @@ -395,7 +396,40 @@ void blur_context_box(gfx_context_t * _src, int radius) { _box_blur_vertical(_src,radius); } +static int (*load_sprite_jpg)(sprite_t *, char *) = NULL; +static int (*load_sprite_png)(sprite_t *, char *) = NULL; + +/** + * TODO: This should probably use some config file or plugin path + * for better discovery; we could rename these libraries and + * not have applications / other libraries depend on them + * directly and instead go through libtoaru_graphics. + */ +__attribute__((constructor)) static void _load_format_libraries() { + void * _lib_jpeg = dlopen("libtoaru_jpeg.so", 0); + if (_lib_jpeg) load_sprite_jpg = dlsym(_lib_jpeg, "load_sprite_jpg"); + void * _lib_png = dlopen("libtoaru_png.so", 0); + if (_lib_png) load_sprite_png = dlsym(_lib_png, "load_sprite_png"); +} + +static char * extension_from_filename(char * filename) { + char * ext = strrchr(filename, '.'); + if (ext && *ext == '.') return ext + 1; + return ""; +} + int load_sprite(sprite_t * sprite, char * filename) { + + char * ext = extension_from_filename(filename); + + if (!strcmp(ext,"png")) return load_sprite_png(sprite, filename); + if (!strcmp(ext,"jpg") || !strcmp(ext,"jpeg")) return load_sprite_jpg(sprite, filename); + + /* Fall back to bitmap */ + return load_sprite_bmp(sprite, filename); +} + +int load_sprite_bmp(sprite_t * sprite, char * filename) { /* Open the requested binary */ FILE * image = fopen(filename, "r"); diff --git a/lib/icon_cache.c b/lib/icon_cache.c index 82286f6d..7f4a723b 100644 --- a/lib/icon_cache.c +++ b/lib/icon_cache.c @@ -37,6 +37,12 @@ static char * icon_directories_48[] = { NULL }; +static char * prefixes[] = { + "png", + "bmp", + NULL +}; + __attribute__((constructor)) static void _init_caches(void) { icon_cache_16 = hashmap_create(10); @@ -73,14 +79,18 @@ static sprite_t * icon_get_int(const char * name, hashmap_t * icon_cache, char * char path[100]; while (icon_directories[i]) { /* Check each path... */ - sprintf(path, "%s/%s.bmp", icon_directories[i], name); - if (access(path, R_OK) == 0) { - /* And if we find one, cache it */ - icon = malloc(sizeof(sprite_t)); - load_sprite(icon, path); - icon->alpha = ALPHA_EMBEDDED; - hashmap_set(icon_cache, (void*)name, icon); - return icon; + char ** prefix = prefixes; + while (*prefix) { + sprintf(path, "%s/%s.%s", icon_directories[i], name, *prefix); + if (access(path, R_OK) == 0) { + /* And if we find one, cache it */ + icon = malloc(sizeof(sprite_t)); + load_sprite(icon, path); + icon->alpha = ALPHA_EMBEDDED; + hashmap_set(icon_cache, (void*)name, icon); + return icon; + } + prefix++; } i++; }