Simple Directmedia Layer
at main 156 lines 4.5 kB view raw
1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org> 4 5 This software is provided 'as-is', without any express or implied 6 warranty. In no event will the authors be held liable for any damages 7 arising from the use of this software. 8 9 Permission is granted to anyone to use this software for any purpose, 10 including commercial applications, and to alter it and redistribute it 11 freely, subject to the following restrictions: 12 13 1. The origin of this software must not be misrepresented; you must not 14 claim that you wrote the original software. If you use this software 15 in a product, an acknowledgment in the product documentation would be 16 appreciated but is not required. 17 2. Altered source versions must be plainly marked as such, and must not be 18 misrepresented as being the original software. 19 3. This notice may not be removed or altered from any source distribution. 20*/ 21#include "SDL_internal.h" 22 23#ifdef SDL_FILESYSTEM_HAIKU 24 25/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 26// System dependent filesystem routines 27 28extern "C" { 29#include "../SDL_sysfilesystem.h" 30} 31 32#include <kernel/image.h> 33#include <storage/Directory.h> 34#include <storage/Entry.h> 35#include <storage/FindDirectory.h> 36#include <storage/Path.h> 37 38 39char *SDL_SYS_GetBasePath(void) 40{ 41 char name[MAXPATHLEN]; 42 43 if (find_path(B_APP_IMAGE_SYMBOL, B_FIND_PATH_IMAGE_PATH, NULL, name, sizeof(name)) != B_OK) { 44 return NULL; 45 } 46 47 BEntry entry(name, true); 48 BPath path; 49 status_t rc = entry.GetPath(&path); // (path) now has binary's path. 50 SDL_assert(rc == B_OK); 51 rc = path.GetParent(&path); // chop filename, keep directory. 52 SDL_assert(rc == B_OK); 53 const char *str = path.Path(); 54 SDL_assert(str != NULL); 55 56 const size_t len = SDL_strlen(str); 57 char *result = (char *) SDL_malloc(len + 2); 58 if (result) { 59 SDL_memcpy(result, str, len); 60 result[len] = '/'; 61 result[len+1] = '\0'; 62 } 63 64 return result; 65} 66 67 68char *SDL_SYS_GetPrefPath(const char *org, const char *app) 69{ 70 // !!! FIXME: is there a better way to do this? 71 const char *home = SDL_getenv("HOME"); 72 const char *append = "/config/settings/"; 73 size_t len = SDL_strlen(home); 74 75 if (!app) { 76 SDL_InvalidParamError("app"); 77 return NULL; 78 } 79 if (!org) { 80 org = ""; 81 } 82 83 if (!len || (home[len - 1] == '/')) { 84 ++append; // home empty or ends with separator, skip the one from append 85 } 86 len += SDL_strlen(append) + SDL_strlen(org) + SDL_strlen(app) + 3; 87 char *result = (char *) SDL_malloc(len); 88 if (result) { 89 if (*org) { 90 SDL_snprintf(result, len, "%s%s%s/%s/", home, append, org, app); 91 } else { 92 SDL_snprintf(result, len, "%s%s%s/", home, append, app); 93 } 94 create_directory(result, 0700); // Haiku api: creates missing dirs 95 } 96 97 return result; 98} 99 100char *SDL_SYS_GetUserFolder(SDL_Folder folder) 101{ 102 const char *home = NULL; 103 char *result; 104 105 home = SDL_getenv("HOME"); 106 if (!home) { 107 SDL_SetError("No $HOME environment variable available"); 108 return NULL; 109 } 110 111 switch (folder) { 112 case SDL_FOLDER_HOME: 113 result = (char *) SDL_malloc(SDL_strlen(home) + 2); 114 if (!result) { 115 return NULL; 116 } 117 118 if (SDL_snprintf(result, SDL_strlen(home) + 2, "%s/", home) < 0) { 119 SDL_SetError("Couldn't snprintf home path for Haiku: %s", home); 120 SDL_free(result); 121 return NULL; 122 } 123 124 return result; 125 126 // TODO: Is Haiku's desktop folder always ~/Desktop/ ? 127 case SDL_FOLDER_DESKTOP: 128 result = (char *) SDL_malloc(SDL_strlen(home) + 10); 129 if (!result) { 130 return NULL; 131 } 132 133 if (SDL_snprintf(result, SDL_strlen(home) + 10, "%s/Desktop/", home) < 0) { 134 SDL_SetError("Couldn't snprintf desktop path for Haiku: %s/Desktop/", home); 135 SDL_free(result); 136 return NULL; 137 } 138 139 return result; 140 141 case SDL_FOLDER_DOCUMENTS: 142 case SDL_FOLDER_DOWNLOADS: 143 case SDL_FOLDER_MUSIC: 144 case SDL_FOLDER_PICTURES: 145 case SDL_FOLDER_PUBLICSHARE: 146 case SDL_FOLDER_SAVEDGAMES: 147 case SDL_FOLDER_SCREENSHOTS: 148 case SDL_FOLDER_TEMPLATES: 149 case SDL_FOLDER_VIDEOS: 150 default: 151 SDL_SetError("Only HOME and DESKTOP available on Haiku"); 152 return NULL; 153 } 154} 155 156#endif // SDL_FILESYSTEM_HAIKU