Skip to content

Commit 538a367

Browse files
committed
Save the settings file under XDG_CONFIG_HOME by default
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.
1 parent 39fd2b1 commit 538a367

File tree

4 files changed

+75
-7
lines changed

4 files changed

+75
-7
lines changed

src/command-line.c

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@
1717
* along with this program. If not, see <http://www.gnu.org/licenses>.
1818
*/
1919

20+
#include <errno.h>
2021
#include <stdlib.h>
2122
#include <pwd.h>
2223
#include <sys/types.h>
2324
#include <string.h>
25+
#include <time.h>
2426
#include <unistd.h>
2527
#include <stdio.h>
2628
#include <ctype.h>
29+
#include <sys/stat.h>
2730

2831
#include "option-table.h"
2932
#include "query-assign.h"
@@ -199,6 +202,69 @@ void print_help(void)
199202
nvgetopt_print_help(__options, 0, print_help_helper);
200203
}
201204

205+
/*
206+
* locate_default_rc_file() - find a suitable location for the default
207+
* configuration file. This will be one of:
208+
* - $XDG_CONFIG_HOME/nvidia/settings-rc,
209+
* - $HOME/.config/nvidia/settings-rc or
210+
* - $HOME/.nvidia-settings-rc
211+
* The last of which is chosen only if the file already exists, for
212+
* backwards compatibility.
213+
*
214+
* The parent directory, `nvidia` for the first two options will be
215+
* created if it doesn't already exist.
216+
*
217+
* The string returned is malloc'ed, but must not be freed as it is
218+
* re-used if this is called multiple times.
219+
*/
220+
const char *locate_default_rc_file(void)
221+
{
222+
static char *default_rc_file = NULL;
223+
const char *home;
224+
const char *xdg_config_home;
225+
226+
if (default_rc_file) {
227+
return default_rc_file;
228+
}
229+
230+
home = get_user_home();
231+
xdg_config_home = getenv("XDG_CONFIG_HOME");
232+
233+
/* Prefer the legacy dot-file in $HOME if it exists. */
234+
235+
if (home) {
236+
nv_append_sprintf(&default_rc_file, "%s/.nvidia-settings-rc", home);
237+
238+
if (access(default_rc_file, F_OK) == 0) {
239+
return default_rc_file;
240+
}
241+
242+
nvfree(default_rc_file);
243+
default_rc_file = NULL;
244+
}
245+
246+
if (xdg_config_home) {
247+
nv_append_sprintf(&default_rc_file, "%s/nvidia", xdg_config_home);
248+
} else if (home) {
249+
nv_append_sprintf(&default_rc_file, "%s/.config/nvidia", home);
250+
} else {
251+
/* Store in the current directory as the last resort if we cannot find a home. */
252+
253+
return ".nvidia-settings-rc";
254+
}
255+
256+
if (access(default_rc_file, F_OK) != 0) {
257+
if (mkdir(default_rc_file, 0755) < 0) {
258+
fprintf(stderr,
259+
"Failed to create the default configuration directory '%s': %s.\n",
260+
default_rc_file, strerror(errno));
261+
}
262+
}
263+
264+
nv_append_sprintf(&default_rc_file, "/settings-rc");
265+
266+
return default_rc_file;
267+
}
202268

203269
/*
204270
* parse_command_line() - malloc an Options structure, initialize it
@@ -218,7 +284,7 @@ Options *parse_command_line(int argc, char *argv[],
218284
int boolval;
219285

220286
op = nvalloc(sizeof(Options));
221-
op->config = DEFAULT_RC_FILE;
287+
op->config = locate_default_rc_file();
222288
op->write_config = NV_TRUE;
223289

224290
/*

src/command-line.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
*/
2828
struct _CtrlSystemList;
2929

30-
#define DEFAULT_RC_FILE "~/.nvidia-settings-rc"
3130
#define CONFIG_FILE_OPTION 1
3231
#define DISPLAY_OPTION 2
3332

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

5453
char **assignments; /*
@@ -124,7 +123,7 @@ typedef struct {
124123

125124
} Options;
126125

127-
126+
const char *locate_default_rc_file(void);
128127
Options *parse_command_line(int argc, char *argv[],
129128
struct _CtrlSystemList *systems);
130129

src/gtk+-2.x/ctkconfig.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*/
2525

2626
#include "ctkconfig.h"
27+
#include "command-line.h"
2728
#include "ctkhelp.h"
2829
#include "ctkwindow.h"
2930
#include "ctkutils.h"
@@ -305,12 +306,14 @@ static void save_rc_clicked(GtkWidget *widget, gpointer user_data)
305306
CtkWindow *ctk_window =
306307
CTK_WINDOW(ctk_get_parent_window(GTK_WIDGET(ctk_config)));
307308

309+
char *default_rc_file = locate_default_rc_file();
310+
308311
filename =
309312
ctk_get_filename_from_dialog("Please select a file to save to.",
310313
GTK_WINDOW(ctk_window),
311314
ctk_config->rc_filename ?
312315
ctk_config->rc_filename :
313-
DEFAULT_RC_FILE);
316+
default_rc_file);
314317

315318
if (!filename) {
316319
return;

src/option-table.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ static const NVGetoptOption __options[] = {
4242
{ "config", CONFIG_FILE_OPTION,
4343
NVGETOPT_STRING_ARGUMENT | NVGETOPT_HELP_ALWAYS, NULL,
4444
"Use the configuration file &CONFIG& rather than the "
45-
"default &" DEFAULT_RC_FILE "&" },
45+
"default &$XDG_CONFIG_HOME/nvidia/settings-rc&" },
4646

4747
{ "ctrl-display", 'c',
4848
NVGETOPT_STRING_ARGUMENT | NVGETOPT_HELP_ALWAYS, NULL,

0 commit comments

Comments
 (0)