Skip to content

Commit

Permalink
Merged (squash) pull request "Add XDG Support": NVIDIA#250
Browse files Browse the repository at this point in the history
These changes move the user data folder on Linux from ${HOME}/.quake2rtx to ${XDG_DATA_HOME}/quake2rtx in order to conform to the XDG specification, see https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
Files previously created in the old location are moved to the new location by q2rtx.sh.
  • Loading branch information
apanteleev committed Aug 24, 2023
1 parent a15b236 commit 8cb6c67
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
6 changes: 5 additions & 1 deletion setup/find-retail-paks.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#!/bin/bash

DEST_DIR="${HOME}/.quake2rtx"
if [[ -n "${XDG_DATA_HOME}" ]]; then
XDG_DATA_HOME = "${HOME}/.local/share"
fi

DEST_DIR="${XDG_DATA_HOME}/quake2rtx"

# Check for zenity/yad
ZEN=""
Expand Down
15 changes: 11 additions & 4 deletions setup/q2rtx.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,28 @@

BIN_PREFIX="."

mkdir -p "${XDG_DATA_HOME:="${HOME}/.local/share"}"

# If the game is installed via a package manager q2rtx won't be in the same
# directory as q2rtx.sh
if [[ -d "/usr/share/quake2rtx" ]]; then
BIN_PREFIX="/usr/share/quake2rtx/bin"
fi

if [[ -d "${HOME}/.quake2rtx" && ! -d "${XDG_DATA_HOME}/quake2rtx" ]]; then
mv "${HOME}/.quake2rtx" "${XDG_DATA_HOME}/quake2rtx"
fi


# Generate the user's game dir if doesn't exist
if [[ ! -d "${HOME}/.quake2rtx/baseq2" ]]; then
mkdir -p "${HOME}/.quake2rtx/baseq2"
if [[ ! -d "${XDG_DATA_HOME}/quake2rtx/baseq2" ]]; then
mkdir -p "${XDG_DATA_HOME}/quake2rtx/baseq2"
fi

# Only run this script on first-launch
if [[ ! -f "${HOME}/.quake2rtx/.retail_checked" ]]; then
if [[ ! -f "${XDG_DATA_HOME}/quake2rtx/.retail_checked" ]]; then
${BIN_PREFIX}/find-retail-paks.sh
touch ${HOME}/.quake2rtx/.retail_checked
touch ${XDG_DATA_HOME}/quake2rtx/.retail_checked
fi

${BIN_PREFIX}/q2rtx "$@"
35 changes: 32 additions & 3 deletions src/unix/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
Expand Down Expand Up @@ -307,7 +308,9 @@ Sys_Init
void Sys_Init(void)
{
char *homedir;
char *xdg_data_home_dir;
char homegamedir[PATH_MAX];
int check_snprintf;
cvar_t *sys_parachute;
DIR *dir_hnd;

Expand All @@ -328,7 +331,7 @@ void Sys_Init(void)
}

if (!baseDirectory[0]) {
Sys_Error("Game basedir not found!\n");
Sys_Error("Game basedir not found!\n");
}
// basedir <path>
// allows the game to run from outside the data tree
Expand All @@ -338,9 +341,35 @@ void Sys_Init(void)
// specifies per-user writable directory for demos, screenshots, etc
homedir = getenv("HOME");
if (!homedir) {
Sys_Error("Homedir not found!\n");
Sys_Error("Homedir not found!\n");
}
// Attempt to respect user's XDG_DATA_HOME environment variable
xdg_data_home_dir = getenv("XDG_DATA_HOME");
if (!xdg_data_home_dir) {
size_t xdg_unset_len = strlen(homedir) + strlen("/.local/share") + 1;

xdg_data_home_dir = malloc(xdg_unset_len);

if (xdg_data_home_dir == NULL) {
Sys_Error("%s:xdg_data_home_dir: malloc() failed.\n", __func__);
}
check_snprintf = snprintf(xdg_data_home_dir, xdg_unset_len,
"%s/%s", homedir, ".local/share");
if (check_snprintf < 0) {
Sys_Error("xdg_data_home_dir: XDG_DATA_HOME not set, and "
"storing the default path (${HOME}/.local/share) "
"failed.\n");
}
}
check_snprintf = snprintf(homegamedir, sizeof(homegamedir),
"%s/%s", xdg_data_home_dir, "quake2rtx");

free(xdg_data_home_dir);

if (check_snprintf < 0) {
Sys_Error("%s:homegamedir: snprintf() failed with return "
"value %d.\n", __func__, check_snprintf);
}
sprintf(homegamedir, "%s/%s", homedir, ".quake2rtx");
sys_homedir = Cvar_Get("homedir", homegamedir, CVAR_NOSET);
sys_libdir = Cvar_Get("libdir", baseDirectory, CVAR_NOSET);
sys_forcegamelib = Cvar_Get("sys_forcegamelib", "", CVAR_NOSET);
Expand Down

0 comments on commit 8cb6c67

Please sign in to comment.