Skip to content

Commit

Permalink
Support XDG Base directory specification for Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed Jan 3, 2025
1 parent 1b2dd29 commit 0c2f8d5
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 10 deletions.
4 changes: 3 additions & 1 deletion libaegisub/common/path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ constexpr std::string_view tokens[] = {
"?script",
"?temp",
"?user",
"?video"
"?video",
"?state"
};

int find_token(std::string_view str) {
Expand All @@ -44,6 +45,7 @@ int find_token(std::string_view str) {
case 't' + 'p': idx = 5; break;
case 'u' + 'r': idx = 6; break;
case 'v' + 'e': idx = 7; break;
case 's' + 't': idx = 8; break;
default: return -1;
}
return str.starts_with(tokens[idx]) ? idx : -1;
Expand Down
33 changes: 30 additions & 3 deletions libaegisub/unix/path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ std::string home_dir() {
throw agi::EnvironmentError("Could not get home directory. Make sure HOME is set.");
}

std::string xdg_dir(std::string_view token, agi::fs::path const& fallback) {
const char *env = getenv(token.data());
if (env) return env;
return fallback.string();
}

#ifdef APPIMAGE_BUILD
std::string exe_dir() {
char *exe, *dir;
Expand Down Expand Up @@ -70,12 +76,32 @@ namespace agi {
void Path::FillPlatformSpecificPaths() {
#ifndef __APPLE__
agi::fs::path home = home_dir();
SetToken("?user", home/".aegisub");
SetToken("?local", home/".aegisub");
agi::fs::path old_root = home/".aegisub";

agi::fs::path xdg_config = xdg_dir("XDG_CONFIG_HOME", home/".config");
agi::fs::path xdg_cache = xdg_dir("XDG_CACHE_HOME", home/"cache");
agi::fs::path xdg_state = xdg_dir("XDG_STATE_HOME", home/".local/state");
agi::fs::path xdg_data = xdg_dir("XDG_DATA_HOME", home/".local/share");

if (agi::fs::DirectoryExists(old_root)) {
SetToken("?user", old_root);
SetToken("?local", old_root);
SetToken("?state", old_root);
} else {
SetToken("?user", xdg_config/"aegisub");
SetToken("?local", xdg_cache/"aegisub");
SetToken("?state", xdg_state/"aegisub");
}

#ifdef APPIMAGE_BUILD
agi::fs::path data = exe_dir();
if (data == "") data = home/".aegisub";
if (data == "") {
if (agi::fs::DirectoryExists(old_root)) {
data = old_root;
} else {
data = xdg_data/"aegisub"
}
}
SetToken("?data", data);
SetToken("?dictionary", Decode("?data/dictionaries"));
#else
Expand All @@ -87,6 +113,7 @@ void Path::FillPlatformSpecificPaths() {
agi::fs::path app_support = agi::util::GetApplicationSupportDirectory();
SetToken("?user", app_support/"Aegisub");
SetToken("?local", app_support/"Aegisub");
SetToken("?state", app_support/"Aegisub");
SetToken("?data", agi::util::GetBundleSharedSupportDirectory());
SetToken("?dictionary", Decode("?data/dictionaries"));
#endif
Expand Down
1 change: 1 addition & 0 deletions libaegisub/windows/path_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ void Path::FillPlatformSpecificPaths() {

SetToken("?user", WinGetFolderPath(CSIDL_APPDATA)/"Aegisub");
SetToken("?local", WinGetFolderPath(CSIDL_LOCAL_APPDATA)/"Aegisub");
SetToken("?state", WinGetFolderPath(CSIDL_APPDATA)/"Aegisub");

std::wstring filename(MAX_PATH + 1, L'\0');
while (static_cast<DWORD>(filename.size()) == GetModuleFileNameW(nullptr, &filename[0], filename.size()))
Expand Down
2 changes: 1 addition & 1 deletion src/dialog_autosave.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ DialogAutosave::DialogAutosave(wxWindow *parent)
std::map<wxString, AutosaveFile> files_map;
Populate(files_map, OPT_GET("Path/Auto/Save")->GetString(), ".AUTOSAVE.ass", "%s");
Populate(files_map, OPT_GET("Path/Auto/Backup")->GetString(), ".ORIGINAL.ass", _("%s [ORIGINAL BACKUP]"));
Populate(files_map, "?user/recovered", ".ass", _("%s [RECOVERED]"));
Populate(files_map, "?state/recovered", ".ass", _("%s [RECOVERED]"));

for (auto& file : files_map | boost::adaptors::map_values)
files.emplace_back(std::move(file));
Expand Down
2 changes: 1 addition & 1 deletion src/dialog_shift_times.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ static wxString get_history_string(json::Object &obj) {
DialogShiftTimes::DialogShiftTimes(agi::Context *context)
: wxDialog(context->parent, -1, _("Shift Times"))
, context(context)
, history_filename(config::path->Decode("?user/shift_history.json"))
, history_filename(config::path->Decode("?state/shift_history.json"))
, timecodes_loaded_slot(context->project->AddTimecodesListener(&DialogShiftTimes::OnTimecodesLoaded, this))
, selected_set_changed_slot(context->selectionController->AddSelectionListener(&DialogShiftTimes::OnSelectedSetChanged, this))
{
Expand Down
4 changes: 2 additions & 2 deletions src/libresrc/default_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,8 @@

"Path" : {
"Auto" : {
"Backup" : "?user/autoback",
"Save" : "?user/autosave"
"Backup" : "?state/autoback",
"Save" : "?state/autosave"
},
"Automation" : {
"Autoload" : "?user/automation/autoload/|?data/automation/autoload/",
Expand Down
5 changes: 3 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ bool AegisubApp::OnInit() {
// Local config, make ?user mean ?data so all user settings are placed in install dir
config::path->SetToken("?user", config::path->Decode("?data"));
config::path->SetToken("?local", config::path->Decode("?data"));
config::path->SetToken("?state", config::path->Decode("?data"));
crash_writer::Initialize(config::path->Decode("?user"));
} catch (agi::fs::FileSystemError const&) {
// File doesn't exist or we can't read it
Expand All @@ -162,7 +163,7 @@ bool AegisubApp::OnInit() {
#endif

StartupLog("Create log writer");
auto path_log = config::path->Decode("?user/log/");
auto path_log = config::path->Decode("?state/log/");
agi::fs::CreateDirectory(path_log);
agi::log::log->Subscribe(std::make_unique<agi::log::JsonEmitter>(path_log));
CleanCache(path_log, "*.json", 10, 100);
Expand Down Expand Up @@ -373,7 +374,7 @@ void AegisubApp::UnhandledException(bool stackWalk) {
auto c = frame->context.get();
if (!c || !c->ass || !c->subsController) continue;

path = config::path->Decode("?user/recovered");
path = config::path->Decode("?state/recovered");
agi::fs::CreateDirectory(path);

auto filename = c->subsController->Filename().stem();
Expand Down

0 comments on commit 0c2f8d5

Please sign in to comment.