shithub: npe

Download patch

ref: c72d3e3d80352a947f7e2aecfb8768ba4b84c87d
parent: b4adec1f02eda895b9cb30cc96b1ffde8241e75d
author: Sigrid Solveig Haflínudóttir <ftrvxmtrx@gmail.com>
date: Wed Mar 17 05:38:01 EDT 2021

sdl2: SDL_GetPrefPath

--- a/include/npe/SDL2/SDL.h
+++ b/include/npe/SDL2/SDL.h
@@ -127,6 +127,7 @@
 SDL_Cursor *SDL_CreateSystemCursor(SDL_SystemCursor id);
 SDL_Texture *SDL_CreateTextureFromSurface(SDL_Renderer *r, SDL_Surface *s);
 char *SDL_GetBasePath(void);
+char *SDL_GetPrefPath(char *org, char *app);
 int SDL_SetRenderDrawColor(SDL_Renderer *r, Uint8 r, Uint8 g, Uint8 b, Uint8 a);
 int SDL_GetCurrentDisplayMode(int displayIndex, SDL_DisplayMode *mode);
 
--- a/libnpe_sdl2/sdl2.c
+++ b/libnpe_sdl2/sdl2.c
@@ -7,6 +7,7 @@
 #include <cursor.h>
 #include <plumb.h>
 #include <SDL2/SDL.h>
+#include <sys/stat.h>
 
 typedef struct Audiodev Audiodev;
 
@@ -1336,6 +1337,40 @@
 SDL_GetBasePath(void)
 {
 	return strdup(basepath);
+}
+
+static int
+mkdirp(char *s, int perm)
+{
+	char *p;
+	int n;
+
+	for(p = strchr(s+1, '/'); p; p = strchr(p+1, '/')){
+		*p = 0;
+		n = access(s, AEXIST) == 0 || mkdir(s, perm) == 0 ? 0 : -1;
+		*p = '/';
+		if(n != 0)
+			return n;
+	}
+	if(access(s, AEXIST) != 0)
+		return mkdir(s, perm);
+
+	return 0;
+}
+
+char *
+SDL_GetPrefPath(char *org, char *app)
+{
+	char *home, *p;
+
+	p = nil;
+	if((home = getenv("home")) != nil){
+		if((p = smprint("%s/lib/%s/%s", home, org, app)) != nil)
+			mkdirp(cleanname(p), 0755);
+		free(home);
+	}
+
+	return p;
 }
 
 SDL_bool