make userdir handling vital, use XDG standard directories
parent
242a51027b
commit
40264fdea9
|
@ -169,8 +169,6 @@ add_executable(quake WIN32
|
|||
target_compile_definitions(
|
||||
quake
|
||||
PUBLIC
|
||||
-DDO_USERDIRS=1
|
||||
-DUSE_SDL2=1
|
||||
-DUSE_CODEC_FLAC=1
|
||||
-DUSE_CODEC_VORBIS=1
|
||||
-DUSE_CODEC_OPUS=1
|
||||
|
|
|
@ -34,9 +34,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|||
#include <sys/time.h>
|
||||
#include <fcntl.h>
|
||||
#include <time.h>
|
||||
#ifdef DO_USERDIRS
|
||||
#include <pwd.h>
|
||||
#endif
|
||||
|
||||
#include <SDL.h>
|
||||
|
||||
|
@ -228,42 +226,49 @@ static int Sys_NumCPUs (void)
|
|||
}
|
||||
#endif
|
||||
|
||||
static char cwd[MAX_OSPATH];
|
||||
#ifdef DO_USERDIRS
|
||||
static char userdir[MAX_OSPATH];
|
||||
#ifdef PLATFORM_OSX
|
||||
#define SYS_USERDIR "Library/Application Support/QuakeSpasm"
|
||||
#else
|
||||
#define SYS_USERDIR ".quakespasm"
|
||||
#endif
|
||||
|
||||
static void Sys_GetUserdir (char *dst, size_t dstsize)
|
||||
static char const *Sys_GetHomeDir(void)
|
||||
{
|
||||
size_t n;
|
||||
const char *home_dir = NULL;
|
||||
struct passwd *pwent;
|
||||
|
||||
pwent = getpwuid( getuid() );
|
||||
if (pwent == NULL)
|
||||
pwent = getpwuid(getuid());
|
||||
|
||||
if(pwent == NULL)
|
||||
{
|
||||
perror("getpwuid");
|
||||
else
|
||||
home_dir = pwent->pw_dir;
|
||||
if (home_dir == NULL)
|
||||
home_dir = getenv("HOME");
|
||||
if (home_dir == NULL)
|
||||
Sys_Error ("Couldn't determine userspace directory");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* what would be a maximum path for a file in the user's directory...
|
||||
* $HOME/SYS_USERDIR/game_dir/dirname1/dirname2/dirname3/filename.ext
|
||||
* still fits in the MAX_OSPATH == 256 definition, but just in case :
|
||||
*/
|
||||
n = strlen(home_dir) + strlen(SYS_USERDIR) + 50;
|
||||
if (n >= dstsize)
|
||||
Sys_Error ("Insufficient array size for userspace directory");
|
||||
|
||||
q_snprintf (dst, dstsize, "%s/%s", home_dir, SYS_USERDIR);
|
||||
return pwent->pw_dir ? pwent->pw_dir : getenv("HOME");
|
||||
}
|
||||
|
||||
static char const *Sys_GetUserDir(void)
|
||||
{
|
||||
static char user_dir[MAX_OSPATH];
|
||||
static char app_name[] = "AGWQuake";
|
||||
|
||||
size_t n;
|
||||
char const *home_dir;
|
||||
|
||||
#ifdef PLATFORM_OSX
|
||||
if(!(home_dir = Sys_GetHomeDir()))
|
||||
return NULL;
|
||||
|
||||
q_snprintf(user_dir, sizeof(user_dir), "%s/Library/Application Support/%s", home_dir, app_name);
|
||||
#else
|
||||
char const *system_dir;
|
||||
|
||||
if((system_dir = getenv("XDG_DATA_HOME")))
|
||||
q_snprintf(user_dir, sizeof(user_dir), "%s/%s/%s", home_dir, system_dir, app_name);
|
||||
else
|
||||
{
|
||||
if(!(home_dir = Sys_GetHomeDir()))
|
||||
return NULL;
|
||||
q_snprintf(user_dir, sizeof(user_dir), "%s/.local/share/%s", home_dir, app_name);
|
||||
}
|
||||
#endif
|
||||
|
||||
return user_dir;
|
||||
}
|
||||
#endif /* DO_USERDIRS */
|
||||
|
||||
#ifdef PLATFORM_OSX
|
||||
static char *OSX_StripAppBundle (char *dir)
|
||||
|
@ -327,18 +332,25 @@ static void Sys_GetBasedir (char *argv0, char *dst, size_t dstsize)
|
|||
|
||||
void Sys_Init (void)
|
||||
{
|
||||
memset (cwd, 0, sizeof(cwd));
|
||||
static char cwd[MAX_OSPATH];
|
||||
|
||||
char const *user_dir;
|
||||
|
||||
/* set base dir to current directory */
|
||||
memset(cwd, 0, sizeof(cwd));
|
||||
Sys_GetBasedir(host_parms->argv[0], cwd, sizeof(cwd));
|
||||
host_parms->basedir = cwd;
|
||||
#ifndef DO_USERDIRS
|
||||
host_parms->userdir = host_parms->basedir; /* code elsewhere relies on this ! */
|
||||
#else
|
||||
memset (userdir, 0, sizeof(userdir));
|
||||
Sys_GetUserdir(userdir, sizeof(userdir));
|
||||
Sys_mkdir (userdir);
|
||||
host_parms->userdir = userdir;
|
||||
#endif
|
||||
host_parms->numcpus = Sys_NumCPUs ();
|
||||
|
||||
/* check for user directory, or use base dir */
|
||||
if((user_dir = Sys_GetUserDir()))
|
||||
{
|
||||
Sys_mkdir(user_dir);
|
||||
host_parms->userdir = user_dir;
|
||||
}
|
||||
else
|
||||
host_parms->userdir = host_parms->basedir;
|
||||
|
||||
host_parms->numcpus = Sys_NumCPUs();
|
||||
Sys_Printf("Detected %d CPUs.\n", host_parms->numcpus);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue