Skip to content

Commit 728ec54

Browse files
committed
Unix now uses XDG paths to find icons, making it easier to provide images. Defaulting to hicolor for icons
1 parent dde3bc4 commit 728ec54

3 files changed

Lines changed: 47 additions & 1 deletion

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
docs/Gemfile.lock
22
docs/_site
33
docs/.jekyll-cache
4+
build/

src/image.c

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "util.h"
1212
#include "debug.h"
1313
#include <ini.h>
14+
#include <string.h>
1415
#define NANOSVG_IMPLEMENTATION
1516
#include <nanosvg.h>
1617
#define NANOSVGRAST_IMPLEMENTATION
@@ -106,6 +107,42 @@ int load_next_slideshow_background_async(void *data)
106107
return 0;
107108
}
108109

110+
#ifdef __unix__
111+
SDL_Surface *load_surface_from_xdg(const char *path){
112+
// TODO: allow the user to specify a specific theme in the config.ini
113+
// then prefer that to `hicolor`.
114+
// Spec for this is here: https://specifications.freedesktop.org/icon-theme/latest/#icon_lookup
115+
SDL_Surface *surface = NULL;
116+
const char* xdg_dirs = getenv("XDG_DATA_DIRS");
117+
const char* icon_path = "/icons/hicolor/scalable/apps/";
118+
if(xdg_dirs != NULL){
119+
ssize_t last_delim = -1;
120+
ssize_t dirs_len = strlen(xdg_dirs);
121+
while(surface == NULL && last_delim < dirs_len){
122+
char xdg_path[4096] = {0};
123+
ssize_t start_ind = last_delim;
124+
if(start_ind < 0)
125+
start_ind = 0;
126+
127+
ssize_t end = strcspn(&xdg_dirs[start_ind], ":");
128+
memcpy(&xdg_path, &xdg_dirs[start_ind], end);
129+
memcpy(&xdg_path[end], icon_path, strlen(icon_path));
130+
memcpy(&xdg_path[end + strlen(icon_path)], path, strlen(path));
131+
surface = IMG_Load(xdg_path);
132+
if (surface == NULL) {
133+
log_error(
134+
"Could not load image from xdg %s\n%s",
135+
xdg_path,
136+
IMG_GetError()
137+
);
138+
}
139+
last_delim += end + 1;
140+
}
141+
}
142+
return surface;
143+
}
144+
#endif
145+
109146
// A function to load a texture from a file
110147
SDL_Texture *load_texture_from_file(const char *path)
111148
{
@@ -119,8 +156,13 @@ SDL_Texture *load_texture_from_file(const char *path)
119156
path,
120157
IMG_GetError()
121158
);
159+
#ifdef __unix__
160+
// we failed to find the image normally, lets try xdg on unix
161+
surface = load_surface_from_xdg(path);
162+
#endif
122163
}
123-
else
164+
165+
if(surface != NULL)
124166
texture = load_texture(surface);
125167
}
126168
return texture;

src/image.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ void render_scroll_indicators(Scroll *scroll, int height, Geometry *geo);
2828
SDL_Surface *load_next_slideshow_background(Slideshow *slideshow, bool transition);
2929
int load_next_slideshow_background_async(void *data);
3030
SDL_Texture *load_texture(SDL_Surface *surface);
31+
#ifdef __unix__
32+
SDL_Surface *load_surface_from_xdg(const char *path);
33+
#endif
3134
SDL_Texture *load_texture_from_file(const char *path);
3235
SDL_Texture *rasterize_svg(char *buffer, int w, int h, SDL_Rect *rect);
3336
SDL_Texture *rasterize_svg_from_file(const char *path, int w, int h, SDL_Rect *rect);

0 commit comments

Comments
 (0)