2
* nvidia-settings: A tool for configuring the NVIDIA X driver on Unix
5
* Copyright (C) 2004 NVIDIA Corporation.
7
* This program is free software; you can redistribute it and/or
8
* modify it under the terms of Version 2 of the GNU General Public
9
* License as published by the Free Software Foundation.
11
* This program is distributed in the hope that it will be useful, but
12
* WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See Version 2
14
* of the GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the:
19
* Free Software Foundation, Inc.
20
* 59 Temple Place - Suite 330
21
* Boston, MA 02111-1307, USA
27
#include <sys/types.h>
33
#include "command-line.h"
34
#include "query-assign.h"
42
/* local prototypes */
44
static void print_assign_help(void);
45
static void print_query_help(void);
46
static void print_help(void);
47
static char *tilde_expansion(char *str);
48
static char *nvstrcat(const char *str, ...);
51
* print_version() - print version information
54
extern const char *pNV_ID;
56
static void print_version(void)
60
nv_msg(TAB, "The NVIDIA X Server Settings tool.");
62
nv_msg(TAB, "This program is used to configure the "
63
"NVIDIA Linux graphics driver.");
65
nv_msg(TAB, "Copyright (C) 2004 NVIDIA Corporation.");
68
} /* print_version() */
72
* Options table; the fields are:
74
* name - this is the long option name
76
* shortname - this is the one character short option name
78
* flags - bitmask; possible values are NVGETOPT_HAS_ARGUMENT and
81
* description - text for use by print_help() to describe the option
84
#define CONFIG_FILE_OPTION 1
86
static const NVGetoptOption __options[] = {
87
{ "version", 'v', 0, NULL,
88
"Print the nvidia-settings version and exit." },
90
{ "help", 'h', 0, NULL,
91
"Print usage information and exit." },
93
{ "config", CONFIG_FILE_OPTION, NVGETOPT_HAS_ARGUMENT, NULL,
94
"Use the configuration file [CONFIG] rather than the "
95
"default " DEFAULT_RC_FILE },
97
{ "ctrl-display", 'c', NVGETOPT_HAS_ARGUMENT, NULL,
98
"Control the specified X display. If this option is not given, then "
99
"nvidia-settings will control the display specifed by '--display'. If "
100
"that is not given, then the $DISPLAY environment variable is used." },
102
{ "load-config-only", 'l', 0, NULL,
103
"Load the configuration file, send the values specified therein to "
104
"the X server, and exit. This mode of operation is useful to place "
105
"in your .xinitrc file, for example." },
107
{ "assign", 'a', NVGETOPT_HAS_ARGUMENT, print_assign_help, NULL },
109
{ "query", 'q', NVGETOPT_HAS_ARGUMENT, print_query_help, NULL },
117
* print_assign_help() - print help information for the assign option.
120
static void print_assign_help(void)
122
nv_msg(BIGTAB, "The ASSIGN argument to the '--assign' commandline option "
127
nv_msg(BIGTAB TAB, "{DISPLAY}/{attribute name}[{display devices}]"
132
nv_msg(BIGTAB, "This assigns the attribute {attribute name} to the value "
133
"{value} on the X Display {DISPLAY}. {DISPLAY} follows the usual "
134
"{host}:{display}.{screen} syntax of the DISPLAY environment "
135
"variable and is optional; when it is not specified, then it is "
136
"implied following the same rule as the --ctrl-display option. "
137
"If the X screen is not specified, then the assignment is made to "
138
"all X screens. Note that the '/' is only required when {DISPLAY} "
139
"is present. The [{display devices}] portion is also optional; "
140
"if it is not specified, then the attribute is assigned to all "
141
"display devices. Some examples:");
145
nv_msg(BIGTAB TAB, "-a FSAA=5");
146
nv_msg(BIGTAB TAB, "-a localhost:0.0/DigitalVibrance[CRT-0]=0");
147
nv_msg(BIGTAB TAB, "--assign=\"SyncToVBlank=1\"");
149
} /* print_assign_help() */
154
* print_query_help() - print help information for the query option.
157
static void print_query_help(void)
159
nv_msg(BIGTAB, "The QUERY argument to the '--query' commandline option "
164
nv_msg(BIGTAB TAB, "{DISPLAY}/{attribute name}[{display devices}]");
168
nv_msg(BIGTAB, "This queries the current value of the attribute "
169
"{attribute name} on the X Display {DISPLAY}. The format is "
170
"the same as that for the '--assign' option, without "
171
"'={value}'. Specify '-q all' to query all attributes.");
173
} /* print_query_help() */
178
* print_help() - loop through the __options[] table, and print the
179
* description of each option.
182
void print_help(void)
185
char *msg, *tmp, scratch[64];
186
const NVGetoptOption *o;
191
nv_msg(NULL, "nvidia-settings [options]");
194
for (i = 0; __options[i].name; i++) {
196
if (isalpha(o->val)) {
197
sprintf(scratch, "%c", o->val);
198
msg = nvstrcat("-", scratch, ", --", o->name, NULL);
200
msg = nvstrcat("--", o->name, NULL);
202
if (o->flags & NVGETOPT_HAS_ARGUMENT) {
203
len = strlen(o->name);
204
for (j = 0; j < len; j++) scratch[j] = toupper(o->name[j]);
206
tmp = nvstrcat(msg, "=[", scratch, "]", NULL);
211
if (o->description) nv_msg(BIGTAB, o->description);
212
if (o->print_description) (*(o->print_description))();
223
* parse_command_line() - malloc an Options structure, initialize it
224
* with defaults, and fill in any pertinent data from the commandline
225
* arguments. This must be called after the gui is initialized (so
226
* that the gui can remove its commandline arguments from argv).
228
* XXX it's unfortunate that we need to init the gui before calling
229
* this, because many of the commandline options will cause us to not
233
Options *parse_command_line(int argc, char *argv[], char *dpy)
239
op = (Options *) malloc(sizeof (Options));
240
memset(op, 0, sizeof (Options));
242
op->config = DEFAULT_RC_FILE;
245
* initialize the controlled display to the gui display name
249
op->ctrl_display = dpy;
252
c = nvgetopt(argc, argv, __options, &strval, NULL);
258
case 'v': print_version(); exit(0); break;
259
case 'h': print_help(); exit(0); break;
260
case 'l': op->load = 1; break;
261
case 'c': op->ctrl_display = strval; break;
263
n = op->num_assignments;
264
op->assignments = realloc(op->assignments, sizeof(char *) * (n+1));
265
op->assignments[n] = strval;
266
op->num_assignments++;
270
op->queries = realloc(op->queries, sizeof(char *) * (n+1));
271
op->queries[n] = strval;
274
case CONFIG_FILE_OPTION: op->config = strval; break;
276
nv_error_msg("Invalid commandline, please run `%s --help` "
277
"for usage information.\n", argv[0]);
282
/* do tilde expansion on the config file path */
284
op->config = tilde_expansion(op->config);
288
} /* parse_command_line() */
293
* tilde_expansion() - do tilde expansion on the given path name;
294
* based loosely on code snippets found in the comp.unix.programmer
295
* FAQ. The tilde expansion rule is: if a tilde ('~') is alone or
296
* followed by a '/', then substitute the current user's home
297
* directory; if followed by the name of a user, then substitute that
298
* user's home directory.
301
static char *tilde_expansion(char *str)
304
char *replace, *user, *ret;
308
if ((!str) || (str[0] != '~')) return str;
310
if ((str[1] == '/') || (str[1] == '\0')) {
312
/* expand to the current user's home directory */
314
prefix = getenv("HOME");
317
/* $HOME isn't set; get the home directory from /etc/passwd */
319
pw = getpwuid(getuid());
320
if (pw) prefix = pw->pw_dir;
327
/* expand to the specified user's home directory */
329
replace = strchr(str, '/');
330
if (!replace) replace = str + strlen(str);
333
user = malloc(len + 1);
334
strncpy(user, str+1, len-1);
337
if (pw) prefix = pw->pw_dir;
341
if (!prefix) return str;
343
ret = malloc(strlen(prefix) + strlen(replace) + 1);
345
strcat(ret, replace);
349
} /* tilde_expansion() */
352
/* XXX useful utility function... where should this go? */
355
* nvstrcat() - allocate a new string, copying all given strings
356
* into it. taken from glib
359
static char *nvstrcat(const char *str, ...)
368
s = va_arg(args, char *);
372
s = va_arg(args, char *);
381
s = va_arg(args, char *);
384
s = va_arg(args, char *);