From 75b867128b70ba21ffd16036d24a7095c74f36fc Mon Sep 17 00:00:00 2001 From: Brandon Wright Date: Sat, 6 May 2017 18:39:01 -0500 Subject: [PATCH] Support XDG config directory. --- gtk/src/gtk_config.cpp | 56 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 49 insertions(+), 7 deletions(-) diff --git a/gtk/src/gtk_config.cpp b/gtk/src/gtk_config.cpp index 7bb1e6be..b957d1f4 100644 --- a/gtk/src/gtk_config.cpp +++ b/gtk/src/gtk_config.cpp @@ -15,25 +15,67 @@ #include "gtk_sound.h" #include "gtk_display.h" +static int +directory_exists (const char *directory) +{ + DIR *dir; + + dir = opendir (directory); + + if (dir) + { + closedir (dir); + return TRUE; + } + + return FALSE; +} + char * get_config_dir (void) { - char *homedir, *configdir; + char *home_dir = NULL, + *classic_config_dir = NULL, + *xdg_config_dir = NULL, + *xdg_snes9x_dir = NULL; /* Find config directory */ - homedir = getenv ("HOME"); + home_dir = getenv ("HOME"); + xdg_config_dir = getenv ("XDG_CONFIG_DIR"); - if (!homedir) + if (!home_dir && !xdg_config_dir) { - configdir = strdup (".snes9x"); + return strdup (".snes9x"); + } + + if (!xdg_config_dir) + { + xdg_snes9x_dir = (char *) malloc (strlen (home_dir) + 16); + sprintf (xdg_snes9x_dir, "%s/.config/snes9x", home_dir); } else { - configdir = (char *) malloc (strlen (homedir) + 9); - sprintf (configdir, "%s/.snes9x", homedir); + xdg_snes9x_dir = (char *) malloc (strlen (xdg_config_dir) + 9); + sprintf (xdg_snes9x_dir, "%s/snes9x", xdg_config_dir); } - return configdir; + classic_config_dir = (char *) malloc (strlen (home_dir) + 9); + sprintf (classic_config_dir, "%s/.snes9x", home_dir); + + char *config_dir; + + if (directory_exists (classic_config_dir) && !directory_exists(xdg_snes9x_dir)) + { + free (xdg_snes9x_dir); + config_dir = classic_config_dir; + } + else + { + free (classic_config_dir); + config_dir = xdg_snes9x_dir; + } + + return config_dir; } char *