game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
paths_linux.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2#include "platform/paths.h"
3#include "platform/platform.h"
4
5#ifdef PLATFORM_LINUX
6
7#include <cstdlib>
8#include <cstring>
9#include <pwd.h>
10#include <sys/stat.h>
11#include <sys/types.h>
12#include <unistd.h>
13
14namespace platform
15{
16
17class LinuxPaths : public Paths
18{
19 public:
20 std::filesystem::path getDataDir() const override
21 {
22 // XDG Base Directory spec: $XDG_DATA_HOME or ~/.local/share
23 const char* xdgDataHome = std::getenv("XDG_DATA_HOME");
24 if (xdgDataHome && xdgDataHome[0] != '\0')
25 {
26 return std::filesystem::path(xdgDataHome) / "gamelauncher";
27 }
28 return getHomeDir() / ".local" / "share" / "gamelauncher";
29 }
30
31 std::filesystem::path getCacheDir() const override
32 {
33 // XDG Base Directory spec: $XDG_CACHE_HOME or ~/.cache
34 const char* xdgCacheHome = std::getenv("XDG_CACHE_HOME");
35 if (xdgCacheHome && xdgCacheHome[0] != '\0')
36 {
37 return std::filesystem::path(xdgCacheHome) / "gamelauncher";
38 }
39 return getHomeDir() / ".cache" / "gamelauncher";
40 }
41
42 std::filesystem::path getLogDir() const override
43 {
44 // XDG Base Directory spec: $XDG_STATE_HOME or ~/.local/state
45 // Logs are the canonical example of state data
46 const char* xdgStateHome = std::getenv("XDG_STATE_HOME");
47 if (xdgStateHome && xdgStateHome[0] != '\0')
48 {
49 return std::filesystem::path(xdgStateHome) / "gamelauncher" / "logs";
50 }
51 return getHomeDir() / ".local" / "state" / "gamelauncher" / "logs";
52 }
53
54 std::filesystem::path getHomeDir() const override
55 {
56 // Try $HOME first
57 const char* home = std::getenv("HOME");
58 if (home && home[0] != '\0')
59 {
60 return std::filesystem::path(home);
61 }
62
63 // Fall back to passwd entry
64 struct passwd* pw = getpwuid(getuid());
65 if (pw && pw->pw_dir)
66 {
67 return std::filesystem::path(pw->pw_dir);
68 }
69
70 // Could not determine home directory — return empty path so callers can handle.
71 fprintf(stderr, "[Paths] Cannot determine home directory for uid %u\n",
72 static_cast<unsigned>(getuid()));
73 return std::filesystem::path{};
74 }
75
76 std::filesystem::path getDocumentsDir() const override
77 {
78 // Check XDG user dirs
79 const char* xdgDocuments = std::getenv("XDG_DOCUMENTS_DIR");
80 if (xdgDocuments && xdgDocuments[0] != '\0')
81 {
82 return std::filesystem::path(xdgDocuments);
83 }
84 return getHomeDir() / "Documents";
85 }
86
87 std::filesystem::path expandPath(const std::string& path) const override
88 {
89 if (path.empty())
90 {
91 return std::filesystem::path();
92 }
93
94 std::string result = path;
95
96 // Expand ~ at the beginning
97 if (result[0] == '~')
98 {
99 result = getHomeDir().string() + result.substr(1);
100 }
101
102 // Expand $VAR and ${VAR} style environment variables
103 size_t pos = 0;
104 while ((pos = result.find('$', pos)) != std::string::npos)
105 {
106 size_t end;
107 std::string varName;
108
109 if (pos + 1 < result.size() && result[pos + 1] == '{')
110 {
111 // ${VAR} style
112 end = result.find('}', pos + 2);
113 if (end == std::string::npos)
114 break;
115 varName = result.substr(pos + 2, end - pos - 2);
116 end++;
117 }
118 else
119 {
120 // $VAR style
121 end = pos + 1;
122 while (
123 end < result.size() &&
124 (std::isalnum(static_cast<unsigned char>(result[end])) || result[end] == '_'))
125 {
126 end++;
127 }
128 varName = result.substr(pos + 1, end - pos - 1);
129 }
130
131 const char* value = std::getenv(varName.c_str());
132 if (value)
133 {
134 result.replace(pos, end - pos, value);
135 pos += std::strlen(value);
136 }
137 else
138 {
139 pos = end;
140 }
141 }
142
143 return std::filesystem::path(result);
144 }
145};
146
147// Factory implementation
148std::unique_ptr<Paths> Paths::create()
149{
150 return std::make_unique<LinuxPaths>();
151}
152
153} // namespace platform
154
155#endif // PLATFORM_LINUX
static std::unique_ptr< Paths > create()