Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
docs/Gemfile.lock
docs/_site
docs/.jekyll-cache
build/
57 changes: 56 additions & 1 deletion src/image.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "util.h"
#include "debug.h"
#include <ini.h>
#include <string.h>
#define NANOSVG_IMPLEMENTATION
#include <nanosvg.h>
#define NANOSVGRAST_IMPLEMENTATION
Expand Down Expand Up @@ -106,6 +107,55 @@ int load_next_slideshow_background_async(void *data)
return 0;
}

#ifdef __unix__
SDL_Surface *load_surface_from_xdg(const char *path){
// TODO: allow the user to specify a specific theme in the config.ini
// then prefer that to `hicolor`.
// Spec for this is here: https://specifications.freedesktop.org/icon-theme/latest/#icon_lookup
SDL_Surface *surface = NULL;
const char* xdg_dirs = getenv("XDG_DATA_DIRS");
const char* sizes[] = {
"scalable",
"512x512",
"256x256",
"128x128",
"64x64",
"32x32"
};

for(int i = 0; i < sizeof(sizes) / sizeof(void*); i++){
char icon_path[128] = {};
snprintf(icon_path, 127, "/icons/hicolor/%s/apps/", sizes[i]);

if(xdg_dirs != NULL){
ssize_t last_delim = -1;
ssize_t dirs_len = strlen(xdg_dirs);
while(surface == NULL && last_delim < dirs_len){
char xdg_path[4096] = {0};
ssize_t start_ind = last_delim;
if(start_ind < 0)
start_ind = 0;

ssize_t end = strcspn(&xdg_dirs[start_ind], ":");
memcpy(&xdg_path, &xdg_dirs[start_ind], end);
memcpy(&xdg_path[end], icon_path, strlen(icon_path));
memcpy(&xdg_path[end + strlen(icon_path)], path, strlen(path));
surface = IMG_Load(xdg_path);
if (surface != NULL)
return surface;

last_delim += end + 1;
}
}
}

log_error(
"Could not load %s from XDG_DATA_DIRS",
path
);
}
#endif

// A function to load a texture from a file
SDL_Texture *load_texture_from_file(const char *path)
{
Expand All @@ -119,8 +169,13 @@ SDL_Texture *load_texture_from_file(const char *path)
path,
IMG_GetError()
);
#ifdef __unix__
// we failed to find the image normally, lets try xdg on unix
surface = load_surface_from_xdg(path);
#endif
}
else

if(surface != NULL)
texture = load_texture(surface);
}
return texture;
Expand Down
3 changes: 3 additions & 0 deletions src/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ void render_scroll_indicators(Scroll *scroll, int height, Geometry *geo);
SDL_Surface *load_next_slideshow_background(Slideshow *slideshow, bool transition);
int load_next_slideshow_background_async(void *data);
SDL_Texture *load_texture(SDL_Surface *surface);
#ifdef __unix__
SDL_Surface *load_surface_from_xdg(const char *path);
#endif
SDL_Texture *load_texture_from_file(const char *path);
SDL_Texture *rasterize_svg(char *buffer, int w, int h, SDL_Rect *rect);
SDL_Texture *rasterize_svg_from_file(const char *path, int w, int h, SDL_Rect *rect);
Expand Down