summaryrefslogtreecommitdiff
path: root/samples/nv-control-dvc.c
blob: dde9c0efaaf8a68ec835531daaabd0b3e3f2ff4f (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
/*
 * Copyright (c) 2004 NVIDIA, Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/*
 * nv-control-dvc.c - trivial sample NV-CONTROL client that
 * demonstrates how to query and set integer attributes.
 *
 * The attribute NV_CTRL_DIGITAL_VIBRANCE ("Digital Vibrance Control")
 * is used as an example.  This attribute is interesting because it
 * can be controlled on a per-display device basis.
 *
 * Please see the section "DISPLAY DEVICES" in NV-CONTROL-API.txt for
 * an explanation of display devices.
 */

#define __STDC_FORMAT_MACROS
#include <inttypes.h>

#include <stdio.h>
#include <stdlib.h>

#include <X11/Xlib.h>

#include "NVCtrl.h"
#include "NVCtrlLib.h"

#include "nv-control-screen.h"


/*
 * display_device_name() - return the display device name correspoding
 * to the display device mask.
 */

char *display_device_name(int mask)
{
    switch (mask) {
    case (1 <<  0): return "CRT-0"; break;
    case (1 <<  1): return "CRT-1"; break;
    case (1 <<  2): return "CRT-2"; break;
    case (1 <<  3): return "CRT-3"; break;
    case (1 <<  4): return "CRT-4"; break;
    case (1 <<  5): return "CRT-5"; break;
    case (1 <<  6): return "CRT-6"; break;
    case (1 <<  7): return "CRT-7"; break;

    case (1 <<  8): return "TV-0"; break;
    case (1 <<  9): return "TV-1"; break;
    case (1 << 10): return "TV-2"; break;
    case (1 << 11): return "TV-3"; break;
    case (1 << 12): return "TV-4"; break;
    case (1 << 13): return "TV-5"; break;
    case (1 << 14): return "TV-6"; break;
    case (1 << 15): return "TV-7"; break;

    case (1 << 16): return "DFP-0"; break;
    case (1 << 17): return "DFP-1"; break;
    case (1 << 18): return "DFP-2"; break;
    case (1 << 19): return "DFP-3"; break;
    case (1 << 20): return "DFP-4"; break;
    case (1 << 21): return "DFP-5"; break;
    case (1 << 22): return "DFP-6"; break;
    case (1 << 23): return "DFP-7"; break;
    default: return "Unknown";
    }
} /* display_device_name() */



int main(int argc, char *argv[])
{
    Display *dpy;
    Bool ret;
    int screen, retval, setval = -1;
    int display_devices, mask;
    NVCTRLAttributeValidValuesRec valid_values;

    /*
     * If there is a commandline argument, interpret it as the value
     * to use to set DVC.
     */
    
    if (argc == 2) {
        setval = atoi(argv[1]);
    }


    /*
     * Open a display connection, and make sure the NV-CONTROL X
     * extension is present on the screen we want to use.
     */

    dpy = XOpenDisplay(NULL);
    if (!dpy) {
        fprintf(stderr, "Cannot open display '%s'.\n", XDisplayName(NULL));
        return 1;
    }
    
    screen = GetNvXScreen(dpy);


    /*
     * Get the bitmask of enabled display devices
     */

    ret = XNVCTRLQueryAttribute(dpy,
                                screen,
                                0,
                                NV_CTRL_ENABLED_DISPLAYS,
                                &display_devices);
    if (!ret) {
        fprintf(stderr, "Unable to determine enabled display devices for "
                "screen %d of '%s'\n", screen, XDisplayName(NULL));
        return 1;
    }
    
    
    /*
     * loop over each enabled display device
     */

    for (mask = 1; mask < (1<<24); mask <<= 1) {

        if (!(mask & display_devices)) continue;

        /*
         * Query the valid values for NV_CTRL_DIGITAL_VIBRANCE
         */

        ret = XNVCTRLQueryValidAttributeValues(dpy,
                                               screen,
                                               mask,
                                               NV_CTRL_DIGITAL_VIBRANCE,
                                               &valid_values);
        if (!ret) {
            fprintf(stderr, "Unable to query the valid values for "
                    "NV_CTRL_DIGITAL_VIBRANCE on display device %s of "
                    "screen %d of '%s'.\n",
                    display_device_name(mask),
                    screen, XDisplayName(NULL));
            return 1;
        }

        /* we assume that NV_CTRL_DIGITAL_VIBRANCE is a range type */
        
        if (valid_values.type != ATTRIBUTE_TYPE_RANGE) {
            fprintf(stderr, "NV_CTRL_DIGITAL_VIBRANCE is not of "
                    "type RANGE.\n");
            return 1;
        }

        /* print the range of valid values */

        printf("Valid values for NV_CTRL_DIGITAL_VIBRANCE: "
               "(%" PRId64 " - %" PRId64 ").\n",
               valid_values.u.range.min, valid_values.u.range.max);
    
        /*
         * if a value was specified on the commandline, set it;
         * otherwise, query the current value
         */
        
        if (setval != -1) {
        
            XNVCTRLSetAttribute(dpy,
                                screen,
                                mask,
                                NV_CTRL_DIGITAL_VIBRANCE,
                                setval);
            XFlush(dpy);

            printf("Set NV_CTRL_DIGITAL_VIBRANCE to %d on display device "
                   "%s of screen %d of '%s'.\n", setval,
                   display_device_name(mask),
                   screen, XDisplayName(NULL));
        } else {
        
            ret = XNVCTRLQueryAttribute(dpy,
                                        screen,
                                        mask,
                                        NV_CTRL_DIGITAL_VIBRANCE,
                                        &retval);

            printf("The current value of NV_CTRL_DIGITAL_VIBRANCE "
                   "is %d on display device %s of screen %d of '%s'.\n",
                   retval, display_device_name(mask),
                   screen, XDisplayName(NULL));
        }
    }
    
    return 0;
}