Skip to content

Commit 7fbed05

Browse files
committed
Prefer $XDG_CONFIG_HOME to $HOME on Unix platforms.
1 parent 8fdafde commit 7fbed05

File tree

1 file changed

+35
-46
lines changed

1 file changed

+35
-46
lines changed

src/musikcore/support/Common.cpp

Lines changed: 35 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -59,48 +59,32 @@
5959
// given the #ifdef/#else above, the following is not required.
6060
// Nor it is the #if FreeBSD below.
6161
#ifdef __OpenBSD__
62-
#include <sys/types.h>
63-
#include <sys/sysctl.h>
64-
#include <unistd.h>
65-
#include <limits.h>
62+
#include <sys/types.h>
63+
#include <sys/sysctl.h>
64+
#include <unistd.h>
65+
#include <limits.h>
6666
#endif
6767

6868
#ifdef __FreeBSD__
6969
#include <sys/types.h>
7070
#include <sys/sysctl.h>
7171
#endif
7272

73-
static std::string GetHomeDirectory() {
74-
std::string directory;
75-
76-
#ifdef WIN32
77-
DWORD bufferSize = GetEnvironmentVariable(L"APPDATA", 0, 0);
78-
wchar_t *buffer = new wchar_t[bufferSize + 2];
79-
GetEnvironmentVariable(L"APPDATA", buffer, bufferSize);
80-
directory.assign(u16to8(buffer));
81-
delete[] buffer;
82-
#else
83-
directory = std::string(std::getenv("HOME"));
84-
#endif
85-
86-
return directory;
87-
}
88-
89-
static std::string getDataDirectoryRoot() {
90-
#ifdef WIN32
91-
return GetHomeDirectory();
92-
#else
93-
return GetHomeDirectory() + "/.config";
94-
#endif
95-
}
96-
9773
static inline void silentDelete(const std::string fn) {
9874
boost::system::error_code ec;
9975
boost::filesystem::remove(boost::filesystem::path(fn), ec);
10076
}
10177

10278
namespace musik { namespace core {
10379

80+
static std::string getDataDirectoryRoot() {
81+
#ifdef WIN32
82+
return GetHomeDirectory();
83+
#else
84+
return GetHomeDirectory() + "/.config";
85+
#endif
86+
}
87+
10488
std::string GetPluginDirectory() {
10589
std::string path(GetApplicationDirectory());
10690
path.append("/plugins/");
@@ -135,26 +119,25 @@ namespace musik { namespace core {
135119
size_t bufsize = sizeof(pathbuf);
136120
sysctl(mib, 4, pathbuf, &bufsize, nullptr, 0);
137121
#elif defined __OpenBSD__
138-
int mib[4];
139-
char **argv;
140-
size_t len = ARG_MAX;
122+
int mib[4];
123+
char **argv;
124+
size_t len = ARG_MAX;
141125

142-
mib[0] = CTL_KERN;
143-
mib[1] = KERN_PROC_ARGS;
144-
mib[2] = getpid();
145-
mib[3] = KERN_PROC_ARGV;
126+
mib[0] = CTL_KERN;
127+
mib[1] = KERN_PROC_ARGS;
128+
mib[2] = getpid();
129+
mib[3] = KERN_PROC_ARGV;
146130

147-
argv = new char*[len];
148-
if (sysctl(mib, 4, argv, &len, nullptr, 0) < 0) abort();
131+
argv = new char*[len];
132+
if (sysctl(mib, 4, argv, &len, nullptr, 0) < 0) abort();
149133

150-
boost::filesystem::path command = boost::filesystem::system_complete(argv[0]);
151-
realpath(command.c_str(), pathbuf);
152-
delete[] argv;
134+
boost::filesystem::path command = boost::filesystem::system_complete(argv[0]);
135+
realpath(command.c_str(), pathbuf);
136+
delete[] argv;
153137
#else
154138
std::string pathToProc = u8fmt("/proc/%d/exe", (int) getpid());
155139
readlink(pathToProc.c_str(), pathbuf, PATH_MAX);
156-
#endif
157-
140+
#endif
158141
result.assign(pathbuf);
159142
size_t last = result.find_last_of("/");
160143
result = result.substr(0, last); /* remove filename component */
@@ -167,13 +150,19 @@ namespace musik { namespace core {
167150
std::string directory;
168151

169152
#ifdef WIN32
170-
DWORD bufferSize = GetEnvironmentVariable(L"USERPROFILE", 0, 0);
171-
wchar_t *buffer = new wchar_t[bufferSize + 2];
172-
GetEnvironmentVariable(L"USERPROFILE", buffer, bufferSize);
153+
DWORD bufferSize = GetEnvironmentVariable(L"APPDATA", 0, 0);
154+
wchar_t* buffer = new wchar_t[bufferSize + 2];
155+
GetEnvironmentVariable(L"APPDATA", buffer, bufferSize);
173156
directory.assign(u16to8(buffer));
174157
delete[] buffer;
175158
#else
176-
directory = std::string(std::getenv("HOME"));
159+
const char* result = std::getenv("XDG_CONFIG_HOME");
160+
if (result && strlen(result)) {
161+
directory = std::string(result);
162+
}
163+
else {
164+
directory = std::string(std::getenv("HOME"));
165+
}
177166
#endif
178167

179168
return directory;

0 commit comments

Comments
 (0)