summaryrefslogtreecommitdiff
path: root/src/libXNVCtrlAttributes/NvCtrlAttributesVidMode.c
blob: 0949cebd885e08653d26ccc2917c27cdcba7436b (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
/*
 * nvidia-settings: A tool for configuring the NVIDIA X driver on Unix
 * and Linux systems.
 *
 * Copyright (C) 2004,2012 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 "NvCtrlAttributes.h"
#include "NvCtrlAttributesPrivate.h"

#include "common-utils.h"
#include "msg.h"

#include <X11/extensions/xf86vmode.h>

#include <stdlib.h>
#include <string.h>


NvCtrlVidModeAttributes *
NvCtrlInitVidModeAttributes(NvCtrlAttributePrivateHandle *h)
{
    NvCtrlVidModeAttributes *vm = NULL;
    int event, error, size, permissions = 0;
    Bool ret;

    /* Check parameters */
    if (!h || !h->dpy || h->target_type != NV_CTRL_TARGET_TYPE_X_SCREEN) {
        goto failed;
    }

    ret = XF86VidModeQueryExtension(h->dpy, &event, &error);
    if (!ret) goto failed;

    vm = nvalloc(sizeof(NvCtrlVidModeAttributes));

    ret = XF86VidModeQueryVersion(h->dpy, &(vm->major_version), &(vm->minor_version));
    if (!ret) goto failed;

    if (vm->major_version < VM_MINMAJOR ||
        (vm->major_version == VM_MINMAJOR && vm->minor_version < VM_MINMINOR)) {
        nv_warning_msg("The version of the XF86VidMode extension present "
                       "on this display (%d.%d) does not support updating "
                       "gamma ramps.  If you'd like to be able to adjust "
                       "gamma ramps, please update your X server such that "
                       "the version of the XF86VidMode extension is %d.%d "
                       "or higher.", vm->major_version, vm->minor_version,
                       VM_MINMAJOR, VM_MINMINOR);
        goto failed;
    }

    ret = XF86VidModeGetPermissions(h->dpy, h->target_id, &permissions);

    if (!ret) {
        goto failed;
    }

    if (((permissions & XF86VM_READ_PERMISSION) == 0) ||
        ((permissions & XF86VM_WRITE_PERMISSION) == 0)) {
        goto failed;
    }


    ret = XF86VidModeGetGammaRampSize(h->dpy, h->target_id, &size);

    if (!ret) goto failed;

    vm->lut[RED_CHANNEL_INDEX]   = nvalloc(sizeof(unsigned short) * size);
    vm->lut[GREEN_CHANNEL_INDEX] = nvalloc(sizeof(unsigned short) * size);
    vm->lut[BLUE_CHANNEL_INDEX]  = nvalloc(sizeof(unsigned short) * size);

    vm->gammaRampSize = size;

    ret = XF86VidModeGetGammaRamp(h->dpy, h->target_id, size,
                                  vm->lut[RED_CHANNEL_INDEX],
                                  vm->lut[GREEN_CHANNEL_INDEX],
                                  vm->lut[BLUE_CHANNEL_INDEX]);
    if (!ret) goto failed;

    NvCtrlInitGammaInputStruct(&vm->gammaInput);

    return vm;

 failed:
    if (vm) {
        free(vm->lut[RED_CHANNEL_INDEX]);
        free(vm->lut[GREEN_CHANNEL_INDEX]);
        free(vm->lut[BLUE_CHANNEL_INDEX]);
        free(vm);
    }

    return NULL;

} /* NvCtrlInitVidModeAttributes() */


ReturnStatus NvCtrlVidModeGetColorAttributes(NvCtrlAttributePrivateHandle *h,
                                             float contrast[3],
                                             float brightness[3],
                                             float gamma[3])
{
    int i;

    if (!h->vm) return NvCtrlMissingExtension;

    for (i = FIRST_COLOR_CHANNEL; i <= LAST_COLOR_CHANNEL; i++) {
        contrast[i]   = h->vm->gammaInput.contrast[i];
        brightness[i] = h->vm->gammaInput.brightness[i];
        gamma[i]      = h->vm->gammaInput.gamma[i];
    }
    
    return NvCtrlSuccess;
    
} /* NvCtrlGetColorAttributes() */





/*
 * NvCtrlSetColorAttributes() - update the color attributes specified
 * by bitmask, recompute the LUT, and send the LUT to the X server.
 *
 * The bitmask parameter is a bitmask of which channels (RED_CHANNEL,
 * GREEN_CHANNEL, and BLUE_CHANNEL) and which values (CONTRAST_VALUE,
 * BRIGHTNESS_VALUE, GAMMA_VALUE) should be updated.
 *
 * XXX future optimization: if each channel has the same c/b/g values,
 * don't need to compute the ramp separately per channel.
 *
 * XXX future optimization: if the input is the same as what we
 * already have, we don't actually need to recompute the ramp and send
 * it to the X server.
 */

ReturnStatus NvCtrlVidModeSetColorAttributes(NvCtrlAttributePrivateHandle *h,
                                             float c[3],
                                             float b[3],
                                             float g[3],
                                             unsigned int bitmask)
{
    Bool ret;

    if (!h || !h->dpy || h->target_type != NV_CTRL_TARGET_TYPE_X_SCREEN) {
        return NvCtrlBadHandle;
    }

    if (!h->vm) {
        return NvCtrlMissingExtension;
    }

    NvCtrlAssignGammaInput(&h->vm->gammaInput, c, b, g, bitmask);

    NvCtrlUpdateGammaRamp(&h->vm->gammaInput,
                          h->vm->gammaRampSize,
                          h->vm->lut,
                          bitmask);

    ret = XF86VidModeSetGammaRamp(h->dpy, h->target_id, h->vm->gammaRampSize,
                                  h->vm->lut[RED_CHANNEL_INDEX],
                                  h->vm->lut[GREEN_CHANNEL_INDEX],
                                  h->vm->lut[BLUE_CHANNEL_INDEX]);

    return ret ? NvCtrlSuccess : NvCtrlError;
}


ReturnStatus NvCtrlVidModeGetColorRamp(NvCtrlAttributePrivateHandle *h,
                                       unsigned int channel,
                                       unsigned short **lut,
                                       int *n)
{
    if (!h || !h->dpy || h->target_type != NV_CTRL_TARGET_TYPE_X_SCREEN) {
        return NvCtrlBadHandle;
    }
    if (!h->vm) {
        return NvCtrlMissingExtension;
    }

    *n = h->vm->gammaRampSize;

    switch (channel) {
      case RED_CHANNEL:   *lut = h->vm->lut[RED_CHANNEL_INDEX];   break;
      case GREEN_CHANNEL: *lut = h->vm->lut[GREEN_CHANNEL_INDEX]; break;
      case BLUE_CHANNEL:  *lut = h->vm->lut[BLUE_CHANNEL_INDEX];  break;
      default: return NvCtrlBadArgument;
    }
    
    return NvCtrlSuccess;
}

/*
 * Get XF86 Video Mode String Attribute Values
 */

ReturnStatus
NvCtrlVidModeGetStringAttribute (NvCtrlAttributePrivateHandle *h,
                                   unsigned int display_mask,
                                   int attr, char **ptr)
{
    /* Validate */
    if ( !h || !h->dpy || h->target_type != NV_CTRL_TARGET_TYPE_X_SCREEN ) {
        return NvCtrlBadHandle;
    }

    if ( !h->vm) {
        return NvCtrlMissingExtension;
    }

    /* Get Video Mode major & minor versions */
    if (attr == NV_CTRL_STRING_XF86VIDMODE_VERSION) {
        char str[16];
        sprintf(str, "%d.%d", h->vm->major_version, h->vm->minor_version);
        *ptr = strdup(str);
        return NvCtrlSuccess;
    }

    return NvCtrlNoAttribute;

} /* NvCtrlVidModeGetStringAttribute() */