| 1 | |
| 2 | /** |
| 3 | * |
| 4 | * @file xdg.cpp |
| 5 | * |
| 6 | * Part of the OpenJazz project |
| 7 | * |
| 8 | * @par Licence: |
| 9 | * Copyright (c) 2015-2026 Carsten Teibes |
| 10 | * |
| 11 | * OpenJazz is distributed under the terms of |
| 12 | * the GNU General Public License, version 2.0 |
| 13 | * |
| 14 | */ |
| 15 | |
| 16 | #include "xdg.h"cxx . . $ `<file>/src/platforms/xdg.h`/ |
| 17 | |
| 18 | #if __unix__cxx . . $ `<built-in>:446:9`! |
| 19 | |
| 20 | #include <cstring> |
| 21 | #include <sys/stat.h> |
| 22 | #include <cstdlib> |
| 23 | |
| 24 | #include "util.h"cxx . . $ `<file>/src/util.h`/ |
| 25 | #include "io/file.h"cxx . . $ `<file>/src/io/file.h`/ |
| 26 | #include "io/log.h"cxx . . $ `<file>/src/io/log.h`/ |
| 27 | |
| 28 | namespacecxx . . $ `$anonymous_namespace_src/platforms/xdg.cpp`/ { |
| 29 | structcxx . . $ `$anonymous_namespace_src/platforms/xdg.cpp`/$anonymous_type_299ee61523d52587_0# { |
| 30 | const char *envcxx . . $ `$anonymous_namespace_src/platforms/xdg.cpp`/$anonymous_type_299ee61523d52587_0#env.; |
| 31 | const char *relPathcxx . . $ `$anonymous_namespace_src/platforms/xdg.cpp`/$anonymous_type_299ee61523d52587_0#relPath.; |
| 32 | int typecxx . . $ `$anonymous_namespace_src/platforms/xdg.cpp`/$anonymous_type_299ee61523d52587_0#type.; |
| 33 | bool createcxx . . $ `$anonymous_namespace_src/platforms/xdg.cpp`/$anonymous_type_299ee61523d52587_0#create.; |
| 34 | } xdgSpeccxx . . $ `$anonymous_namespace_src/platforms/xdg.cpp`/xdgSpec.[] = { |
| 35 | {"XDG_CONFIG_HOME", "/.config", PATH_TYPE_CONFIGcxx . . $ PATH_TYPE_CONFIG., true}, |
| 36 | {"XDG_CACHE_HOME", "/.cache", PATH_TYPE_TEMPcxx . . $ PATH_TYPE_TEMP., true}, |
| 37 | {"XDG_DATA_HOME", "/.local/share", PATH_TYPE_GAMEcxx . . $ PATH_TYPE_GAME., false} |
| 38 | }; |
| 39 | } |
| 40 | |
| 41 | void XDGPlatformcxx . . $ XDGPlatform/::AddGamePathscxx . . $ XDGPlatform/AddGamePaths(49f6e7a06ebc5aa8).() { |
| 42 | // first get home directory, will be useful later and serves as sanity check |
| 43 | const char *homeDir = getenvcxx . . $ getenv(a8b8e32347182212).("HOME"); |
| 44 | if (!homeDir) { |
| 45 | LOG_WARNcxx . . $ `src/io/log.h:77:9`!("No $HOME found, this might break something."); |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | // define our subdirectory |
| 50 | const char *appDir = "/openjazz"; |
| 51 | |
| 52 | // check all needed XDG directories from spec |
| 53 | for(auto const &entry : xdgSpeccxx . . $ `$anonymous_namespace_src/platforms/xdg.cpp`/xdgSpec.) { |
| 54 | // length of final path |
| 55 | size_t xdgLen = 0; |
| 56 | |
| 57 | // check for user override |
| 58 | const char *env = getenvcxx . . $ getenv(a8b8e32347182212).(entry.envcxx . . $ `$anonymous_namespace_src/platforms/xdg.cpp`/$anonymous_type_299ee61523d52587_0#env.); |
| 59 | if (env) { |
| 60 | xdgLen = strlen(env); |
| 61 | } else { |
| 62 | // use fallback directory |
| 63 | xdgLen = strlen(homeDir) + strlen(entry.relPathcxx . . $ `$anonymous_namespace_src/platforms/xdg.cpp`/$anonymous_type_299ee61523d52587_0#relPath.); |
| 64 | } |
| 65 | |
| 66 | // concatenate path |
| 67 | char *xdgPath = new char[xdgLen + strlen(appDir) + 1]; |
| 68 | if(env) { |
| 69 | strcpy(xdgPath, env); |
| 70 | } else { |
| 71 | strcpy(xdgPath, homeDir); |
| 72 | strcat(xdgPath, entry.relPathcxx . . $ `$anonymous_namespace_src/platforms/xdg.cpp`/$anonymous_type_299ee61523d52587_0#relPath.); |
| 73 | } |
| 74 | strcat(xdgPath, appDir); |
| 75 | |
| 76 | // create if needed |
| 77 | if(entry.createcxx . . $ `$anonymous_namespace_src/platforms/xdg.cpp`/$anonymous_type_299ee61523d52587_0#create.) { |
| 78 | LOG_TRACEcxx . . $ `src/io/log.h:74:9`!("Trying to create '%s' directory", xdgPath); |
| 79 | mkdir(xdgPath, 0777); |
| 80 | } |
| 81 | |
| 82 | gamePathscxx . . $ gamePaths..addcxx . . $ PathMgr#add(37e8d18a243bba77).(xdgPath, entry.typecxx . . $ `$anonymous_namespace_src/platforms/xdg.cpp`/$anonymous_type_299ee61523d52587_0#type.); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | #endif |