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
110147SDL_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 ;
0 commit comments