summaryrefslogtreecommitdiff
path: root/src/command-line.c
blob: 63277801a67ddb036022735272bfae3126ecb8d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
 * nvidia-settings: A tool for configuring the NVIDIA X driver on Unix
 * and Linux systems.
 *
 * Copyright (C) 2004 NVIDIA Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses>.
 */

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

#include "option-table.h"
#include "query-assign.h"
#include "msg.h"
#include "nvgetopt.h"
#include "glxinfo.h"

#include "NvCtrlAttributes.h"

#include "common-utils.h"
#include "config-file.h"

/* local prototypes */

static void print_attribute_help(const char *attr);
static void print_help(void);

/*
 * print_version() - print version information
 */

extern const char *pNV_ID;

static void print_version(void)
{
    nv_msg(NULL, "");
    nv_msg(NULL, "%s", pNV_ID);
    nv_msg(TAB, "The NVIDIA X Server Settings tool.");
    nv_msg(NULL, "");
    nv_msg(TAB, "This program is used to configure the "
           "NVIDIA Linux graphics driver.");
    nv_msg(TAB, "For more detail, please see the nvidia-settings(1) "
           "man page.");
    nv_msg(NULL, "");
    nv_msg(TAB, "Copyright (C) 2004 - 2010 NVIDIA Corporation.");
    nv_msg(NULL, "");
    
} /* print_version() */

/*
 * print_attribute_help() - print information about the specified attribute.
 */

static void print_attribute_help(const char *attr)
{
    int i;
    int found = 0;
    int list_all = 0;
    int show_desc = 1;


    if (!strcasecmp(attr, "all")) {
        list_all = 1;

    } else if (!strcasecmp(attr, "list")) {
        list_all = 1;
        show_desc = 0;
    }

    nv_msg(NULL, "");

    for (i = 0; i < attributeTableLen; i++) {
        const AttributeTableEntry *entry = attributeTable + i;

        if (list_all || !strcasecmp(attr, entry->name)) {

            if (show_desc) {
                nv_msg(NULL, "Attribute '%s':", entry->name);

                /* Attribute type (value) information */

                switch (entry->type) {
                case CTRL_ATTRIBUTE_TYPE_INTEGER:
                    nv_msg(NULL, "  - Attribute value is an integer.");
                    break;
                case CTRL_ATTRIBUTE_TYPE_STRING:
                case CTRL_ATTRIBUTE_TYPE_STRING_OPERATION:
                    nv_msg(NULL, "  - Attribute value is a string.");
                    break;
                case CTRL_ATTRIBUTE_TYPE_BINARY_DATA:
                    nv_msg(NULL, "  - Attribute value is binary data.");
                    break;
                case CTRL_ATTRIBUTE_TYPE_COLOR:
                    nv_msg(NULL, "  - Attribute value is a color.");
                    break;
                case CTRL_ATTRIBUTE_TYPE_SDI_CSC:
                    nv_msg(NULL, "  - Attribute value is a SDI CSC matrix.");
                    break;
                }

                /* Attribute flags (common) */

                if (entry->flags.is_gui_attribute) {
                    nv_msg(NULL, "  - Is GUI attribute.");
                }
                if (entry->flags.is_framelock_attribute) {
                    nv_msg(NULL, "  - Is Frame Lock attribute.");
                }
                if (entry->flags.is_sdi_attribute) {
                    nv_msg(NULL, "  - Is SDI attribute.");
                }
                if (entry->flags.no_config_write) {
                    nv_msg(NULL, "  - Attribute is not written to the rc file.");
                }
                if (entry->flags.no_query_all) {
                    nv_msg(NULL, "  - Attribute not queried in 'query all'.");
                }

                /* Attribute type-specific flags */

                switch (entry->type) {
                case CTRL_ATTRIBUTE_TYPE_INTEGER:
                    if (entry->f.int_flags.is_100Hz) {
                        nv_msg(NULL, "  - Attribute value is in units of Centihertz "
                               "(1/100Hz).");
                    }
                    if (entry->f.int_flags.is_1000Hz) {
                        nv_msg(NULL, "  - Attribute value is in units of Milihertz "
                               "(1/1000 Hz).");
                    }
                    if (entry->f.int_flags.is_packed) {
                        nv_msg(NULL, "  - Attribute value is packed integer.");
                    }
                    if (entry->f.int_flags.is_display_mask) {
                        nv_msg(NULL, "  - Attribute value is a display mask.");
                    }
                    if (entry->f.int_flags.is_display_id) {
                        nv_msg(NULL, "  - Attribute value is a display ID.");
                    }
                    if (entry->f.int_flags.no_zero) {
                        nv_msg(NULL, "  - Attribute cannot be zero.");
                    }
                    if (entry->f.int_flags.is_switch_display) {
                        nv_msg(NULL, "  - Attribute value is switch display.");
                    }
                    break;
                case CTRL_ATTRIBUTE_TYPE_STRING:
                case CTRL_ATTRIBUTE_TYPE_COLOR:
                case CTRL_ATTRIBUTE_TYPE_SDI_CSC:
                case CTRL_ATTRIBUTE_TYPE_STRING_OPERATION:
                case CTRL_ATTRIBUTE_TYPE_BINARY_DATA:
                    /* Nothing specific to report for these */
                    break;
                }

                nv_msg(TAB, "%s", entry->desc);
                nv_msg(NULL, "");
            } else {
                nv_msg(NULL, "%s", entry->name);
            }

            found = 1;
            if (!list_all) break;
        }
    }

    if (!found && !list_all) {
        nv_error_msg("Unrecognized attribute name '%s'.\n", attr);
    }

} /* print_attribute_help() */


static void print_help_helper(const char *name, const char *description)
{
    nv_msg(TAB, "%s", name);
    nv_msg_preserve_whitespace(BIGTAB, "%s", description);
    nv_msg(NULL, "");
}

/*
 * print_help() - loop through the __options[] table, and print the
 * description of each option.
 */

void print_help(void)
{
    print_version();

    nv_msg(NULL, "");
    nv_msg(NULL, "nvidia-settings [options]");
    nv_msg(NULL, "");

    nvgetopt_print_help(__options, 0, print_help_helper);
}


/*
 * parse_command_line() - malloc an Options structure, initialize it
 * with defaults, and fill in any pertinent data from the commandline
 * arguments.  This must be called before the gui is initialized so
 * that the correct gui library can be used. Arguments for the gui must
 * follow a '--' marker. The marker will be removed before passing the
 * commandline arguments to the gui init function.
 */

Options *parse_command_line(int argc, char *argv[],
                            CtrlSystemList *systems)
{
    Options *op;
    int n, c;
    char *strval;
    int boolval;

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

    /*
     * initialize the controlled display to the gui display name
     * passed in.
     */

    while (1) {
        c = nvgetopt(argc, argv, __options, &strval,
                     &boolval,  /* boolval */
                     NULL,  /* intval */
                     NULL,  /* doubleval */
                     NULL); /* disable_val */

        if (c == -1)
            break;
        
        switch (c) {
        case 'v': print_version(); exit(0); break;
        case 'h': print_help(); exit(0); break;
        case 'l': op->only_load = 1; break;
        case 'n': op->no_load = 1; break;
        case 'r': op->rewrite = 1; break;
        case 'c': op->ctrl_display = strval; break;
        case DISPLAY_OPTION:
            /*
             * --ctrl-display and --display can both be specified so only assign
             * --display to ctrl_display if it is not yet assigned.
             */
            if (!op->ctrl_display) {
                op->ctrl_display = strval;
            }
            break;
        case 'p': op->page = strval; break;
        case 'V':
            nv_set_verbosity(NV_VERBOSITY_DEFAULT);
            if (!strval) {
                /* user didn't give argument, assume "all" */
                nv_set_verbosity(NV_VERBOSITY_ALL);
            } else if (nv_strcasecmp(strval, "none") == NV_TRUE) {
                nv_set_verbosity(NV_VERBOSITY_NONE);
            } else if (nv_strcasecmp(strval, "errors") == NV_TRUE) {
                nv_set_verbosity(NV_VERBOSITY_ERROR);
            } else if (nv_strcasecmp(strval, "deprecations") == NV_TRUE) {
                nv_set_verbosity(NV_VERBOSITY_DEPRECATED);
            } else if (nv_strcasecmp(strval, "warnings") == NV_TRUE) {
                nv_set_verbosity(NV_VERBOSITY_WARNING);
            } else if (nv_strcasecmp(strval, "all") == NV_TRUE) {
                nv_set_verbosity(NV_VERBOSITY_ALL);
            } else {
                nv_error_msg("Invalid verbosity level '%s'.  Please run "
                             "`%s --help` for usage information.\n",
                             strval, argv[0]);
                exit(0);
            }
            set_dynamic_verbosity(NV_FALSE);
            break;
        case 'a':
            n = op->num_assignments;
            op->assignments = nvrealloc(op->assignments,
                                        sizeof(char *) * (n+1));
            op->assignments[n] = strval;
            op->num_assignments++;
            break;
        case 'q':
            n = op->num_queries;
            op->queries = nvrealloc(op->queries, sizeof(char *) * (n+1));
            op->queries[n] = strval;
            op->num_queries++;
            break;
        case CONFIG_FILE_OPTION: op->config = strval; break;
        case 'g': print_glxinfo(NULL, systems); exit(0); break;
        case 't': op->terse = NV_TRUE; break;
        case 'd': op->dpy_string = NV_TRUE; break;
        case 'e': print_attribute_help(strval); exit(0); break;
        case 'L': op->list_targets = NV_TRUE; break;
        case 'w': op->write_config = boolval; break;
        case 'i': op->use_gtk2 = NV_TRUE; break;
        case 'I': op->gtk_lib_path = strval; break;
        default:
            nv_error_msg("Invalid commandline, please run `%s --help` "
                         "for usage information.\n", argv[0]);
            exit(0);
        }
    }

    /* do tilde expansion on the config file path */

    op->config = tilde_expansion(op->config);
    
    return op;

} /* parse_command_line() */