Skip to content

Commit

Permalink
POSIX: Use XDG specification for default save path.
Browse files Browse the repository at this point in the history
This is what the XDG Base Directory Specification suggests to use. We still
use ~/.scummvm as default path in case the directory exists.

This tackles an aspect of bug #6036 "POSIX: Use XDG dirs instead of HOME".
  • Loading branch information
Johannes Schickel committed Feb 2, 2016
1 parent 2622cde commit 687f606
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 13 deletions.
11 changes: 10 additions & 1 deletion README
Original file line number Diff line number Diff line change
Expand Up @@ -1741,7 +1741,16 @@ The platforms that currently have a different default directory are:
$HOME/Documents/ScummVM Savegames/

Other unices:
$HOME/.scummvm/
We follow the XDG Base Directory Specification. This means our
configuration can be found in:
$XDG_DATA_HOME/scummvm/saves/

If XDG_DATA_HOME is not defined or empty, ~/.local/share will be used
as value of XDG_DATA_HOME in accordance with the specification.

If an earlier version of ScummVM was installed on your system, the
previous default location of '~/.scummvm' will be kept. This is detected
based on the presence of the path '~/.scummvm'.

Windows Vista/7:
\Users\username\AppData\Roaming\ScummVM\Saved games\
Expand Down
71 changes: 59 additions & 12 deletions backends/saves/posix/posix-saves.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,19 @@
*/


// Enable getenv, mkdir and time.h stuff
#define FORBIDDEN_SYMBOL_EXCEPTION_getenv
#define FORBIDDEN_SYMBOL_EXCEPTION_mkdir
// Re-enable some forbidden symbols to avoid clashes with stat.h and unistd.h.
// Also with clock() in sys/time.h in some Mac OS X SDKs.
#define FORBIDDEN_SYMBOL_EXCEPTION_time_h //On IRIX, sys/stat.h includes sys/time.h
#define FORBIDDEN_SYMBOL_EXCEPTION_unistd_h
#define FORBIDDEN_SYMBOL_EXCEPTION_mkdir
#define FORBIDDEN_SYMBOL_EXCEPTION_getenv

#include "common/scummsys.h"

#if defined(POSIX) && !defined(DISABLE_DEFAULT_SAVEFILEMANAGER)

#include "backends/saves/posix/posix-saves.h"
#include "backends/fs/posix/posix-fs.h"

#include "common/config-manager.h"
#include "common/savefile.h"
Expand All @@ -41,26 +44,70 @@
#include <errno.h>
#include <sys/stat.h>


#ifdef MACOSX
#define DEFAULT_SAVE_PATH "Documents/ScummVM Savegames"
#else
#define DEFAULT_SAVE_PATH ".scummvm"
#endif

POSIXSaveFileManager::POSIXSaveFileManager() {
// Register default savepath based on HOME
// Register default savepath.
#if defined(SAMSUNGTV)
ConfMan.registerDefault("savepath", "/mtd_wiselink/scummvm savegames");
#else
Common::String savePath;

#if defined(MACOSX)
const char *home = getenv("HOME");
if (home && *home && strlen(home) < MAXPATHLEN) {
savePath = home;
savePath += "/" DEFAULT_SAVE_PATH;
savePath += "/Documents/ScummVM Savegames";

ConfMan.registerDefault("savepath", savePath);
}

#else
const char *envVar;

// Previously we placed our default savepath in HOME. If the directory
// still exists, we will use it for backwards compatability.
envVar = getenv("HOME");
if (envVar && *envVar) {
savePath = envVar;
savePath += "/.scummvm";

struct stat sb;
if (stat(savePath.c_str(), &sb) != 0 || !S_ISDIR(sb.st_mode)) {
savePath.clear();
}
}

if (savePath.empty()) {
Common::String prefix;

// On POSIX systems we follow the XDG Base Directory Specification for
// where to store files. The version we based our code upon can be found
// over here: http://standards.freedesktop.org/basedir-spec/basedir-spec-0.8.html
envVar = getenv("XDG_DATA_HOME");
if (!envVar || !*envVar) {
envVar = getenv("HOME");
if (envVar && *envVar) {
prefix = envVar;
savePath = ".local/share/";
}
} else {
prefix = envVar;
}

// Our default save path is '$XDG_DATA_HOME/scummvm/saves'
savePath += "scummvm/saves";

if (!Posix::assureDirectoryExists(savePath, prefix.c_str())) {
savePath.clear();
} else {
savePath = prefix + '/' + savePath;
}
}

if (!savePath.empty() && savePath.size() < MAXPATHLEN) {
ConfMan.registerDefault("savepath", savePath);
}
#endif

// The user can override the savepath with the SCUMMVM_SAVEPATH
// environment variable. This is weaker than a --savepath on the
// command line, but overrides the default savepath.
Expand Down

0 comments on commit 687f606

Please sign in to comment.