Skip to content

Commit

Permalink
Save the settings file under XDG_CONFIG_HOME by default
Browse files Browse the repository at this point in the history
If the settings file does not exist in its legacy location in
`~/.nvidia-settings-rc`, store it in
`$XDG_CONFIG_HOME/nvidia/settings-rc` instead.

Fixes #30.
  • Loading branch information
liff committed Sep 12, 2024
1 parent 39fd2b1 commit 64269c6
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 7 deletions.
66 changes: 65 additions & 1 deletion src/command-line.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
* along with this program. If not, see <http://www.gnu.org/licenses>.
*/

#include <errno.h>
#include <stdlib.h>
#include <pwd.h>
#include <sys/types.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <stdio.h>
#include <ctype.h>
#include <sys/stat.h>

#include "option-table.h"
#include "query-assign.h"
Expand Down Expand Up @@ -199,6 +202,67 @@ void print_help(void)
nvgetopt_print_help(__options, 0, print_help_helper);
}

/* locate_default_rc_file() - find a suitable location for the default
* configuration file. This will be one of:
* - $XDG_CONFIG_HOME/nvidia/settings-rc,
* - $HOME/.config/nvidia/settings-rc or
* - $HOME/.nvidia-settings-rc
* The last of which is chosen only if the file already exists, for
* backwards compatibility.
*
* The parent directory, `nvidia` for the first two options will be
* created if it doesn't already exist.
*
* The string returned is malloc'ed, but must not be freed as it is
* re-used if this is called multiple times.
*/
const char *locate_default_rc_file(void)
{
static char *default_rc_file = NULL;
const char *home;
const char *xdg_config_home;

if (default_rc_file) {
return default_rc_file;
}

home = get_user_home();
xdg_config_home = getenv("XDG_CONFIG_HOME");

/* Prefer the legacy dot-file in $HOME if it exists. */

if (home) {
nv_append_sprintf(&default_rc_file, "%s/.nvidia-settings-rc", home);

if (access(default_rc_file, F_OK) == 0) {
return default_rc_file;
}

nvfree(default_rc_file);
default_rc_file = NULL;
}

if (xdg_config_home) {
nv_append_sprintf(&default_rc_file, "%s/nvidia", xdg_config_home);
} else if (home) {
nv_append_sprintf(&default_rc_file, "%s/.config/nvidia", home);
} else {
fprintf(stderr, "Failed to locate the default rc file.\n");
exit(1);
}

if (access(default_rc_file, F_OK) != 0) {
if (mkdir(default_rc_file, 0755) < 0) {
fprintf(stderr,
"Failed to create the default configuration directory '%s': %s.\n",
default_rc_file, strerror(errno));
}
}

nv_append_sprintf(&default_rc_file, "/settings-rc");

return default_rc_file;
}

/*
* parse_command_line() - malloc an Options structure, initialize it
Expand All @@ -218,7 +282,7 @@ Options *parse_command_line(int argc, char *argv[],
int boolval;

op = nvalloc(sizeof(Options));
op->config = DEFAULT_RC_FILE;
op->config = locate_default_rc_file();
op->write_config = NV_TRUE;

/*
Expand Down
7 changes: 3 additions & 4 deletions src/command-line.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
*/
struct _CtrlSystemList;

#define DEFAULT_RC_FILE "~/.nvidia-settings-rc"
#define CONFIG_FILE_OPTION 1
#define DISPLAY_OPTION 2

Expand All @@ -47,8 +46,8 @@ typedef struct {
char *config; /*
* The name of the configuration file (to be
* read from, and to be written to); defaults
* to the value of the constant
* DEFAULT_RC_FILE.
* to $XDG_CONFIG_HOME/nvidia/settings-rc or
* ~/.nvidia-settings-rc if the latter exists.
*/

char **assignments; /*
Expand Down Expand Up @@ -124,7 +123,7 @@ typedef struct {

} Options;


const char *locate_default_rc_file(void);
Options *parse_command_line(int argc, char *argv[],
struct _CtrlSystemList *systems);

Expand Down
5 changes: 4 additions & 1 deletion src/gtk+-2.x/ctkconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/

#include "ctkconfig.h"
#include "command-line.h"
#include "ctkhelp.h"
#include "ctkwindow.h"
#include "ctkutils.h"
Expand Down Expand Up @@ -305,12 +306,14 @@ static void save_rc_clicked(GtkWidget *widget, gpointer user_data)
CtkWindow *ctk_window =
CTK_WINDOW(ctk_get_parent_window(GTK_WIDGET(ctk_config)));

char *default_rc_file = locate_default_rc_file();

filename =
ctk_get_filename_from_dialog("Please select a file to save to.",
GTK_WINDOW(ctk_window),
ctk_config->rc_filename ?
ctk_config->rc_filename :
DEFAULT_RC_FILE);
default_rc_file);

if (!filename) {
return;
Expand Down
2 changes: 1 addition & 1 deletion src/option-table.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ static const NVGetoptOption __options[] = {
{ "config", CONFIG_FILE_OPTION,
NVGETOPT_STRING_ARGUMENT | NVGETOPT_HELP_ALWAYS, NULL,
"Use the configuration file &CONFIG& rather than the "
"default &" DEFAULT_RC_FILE "&" },
"default &$XDG_CONFIG_HOME/nvidia/settings-rc&" },

{ "ctrl-display", 'c',
NVGETOPT_STRING_ARGUMENT | NVGETOPT_HELP_ALWAYS, NULL,
Expand Down

0 comments on commit 64269c6

Please sign in to comment.