summaryrefslogtreecommitdiff
path: root/src/libXNVCtrlAttributes/NvCtrlAttributesNvControl.c
blob: 4f5bce985006a6f184cccb97c3b491898490f6f1 (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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
/*
 * 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 "NvCtrlAttributes.h"
#include "NvCtrlAttributesPrivate.h"

#include "NVCtrlLib.h"

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

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

/*
 * NvCtrlInitNvControlAttributes() - check for the NV-CONTROL
 * extension and make sure we have an adequate version.  Returns a
 * malloced and initialized NvCtrlNvControlAttributes structure if
 * successful, or NULL otherwise.
 */

NvCtrlNvControlAttributes *
NvCtrlInitNvControlAttributes (NvCtrlAttributePrivateHandle *h)
{
    NvCtrlNvControlAttributes *nv;
    int ret, major, minor, event, error;
    const CtrlTargetTypeInfo *targetTypeInfo;
    
    ret = XNVCTRLQueryExtension (h->dpy, &event, &error);
    if (ret != True) {
        nv_warning_msg("NV-CONTROL extension not found on this Display.");
        return NULL;
    }
    
    ret = XNVCTRLQueryVersion (h->dpy, &major, &minor);
    if (ret != True) {
        nv_error_msg("Failed to query NV-CONTROL extension version.");
        return NULL;
    }

    if (NV_VERSION2(major, minor) < NV_VERSION2(NV_MINMAJOR, NV_MINMINOR)) {
        nv_error_msg("NV-CONTROL extension version %d.%d is too old; "
                     "the minimum required version is %d.%d.",
                     major, minor, NV_MINMAJOR, NV_MINMINOR);
        return NULL;
    }

    if (h->target_type == X_SCREEN_TARGET) {
        ret = XNVCTRLIsNvScreen (h->dpy, h->target_id);
        if (ret != True) {
            nv_warning_msg("NV-CONTROL extension not present on screen %d "
                           "of this Display.", h->target_id);
            return NULL;
        }
    }

    nv = nvalloc(sizeof(NvCtrlNvControlAttributes));

    targetTypeInfo = NvCtrlGetTargetTypeInfo(h->target_type);
    if (targetTypeInfo == NULL) {
        nv_error_msg("Invalid or unknown target type");
        return NULL;
    }

    ret = XNVCtrlSelectTargetNotify(h->dpy,
                                    targetTypeInfo->nvctrl,
                                    h->target_id,
                                    TARGET_ATTRIBUTE_CHANGED_EVENT,
                                    True);
    if (ret != True) {
        nv_warning_msg("Unable to select attribute changed NV-CONTROL "
                       "events.");
    }

    /*
     * TARGET_ATTRIBUTE_AVAILABILITY_CHANGED_EVENT was added in NV-CONTROL
     * 1.15
     */

    if (NV_VERSION2(major, minor) >= NV_VERSION2(1, 15)) {
        ret = XNVCtrlSelectTargetNotify(h->dpy,
                                        targetTypeInfo->nvctrl,
                                        h->target_id,
                                        TARGET_ATTRIBUTE_AVAILABILITY_CHANGED_EVENT,
                                        True);
        if (ret != True) {
            nv_warning_msg("Unable to select attribute changed NV-CONTROL "
                           "events.");
        }
    }
    
    /*
     * TARGET_STRING_ATTRIBUTE_CHANGED_EVENT was added in NV-CONTROL
     * 1.16
     */
    
    if (NV_VERSION2(major, minor) >= NV_VERSION2(1, 16)) {
        ret = XNVCtrlSelectTargetNotify(h->dpy,
                                        targetTypeInfo->nvctrl,
                                        h->target_id,
                                        TARGET_STRING_ATTRIBUTE_CHANGED_EVENT,
                                        True);
        if (ret != True) {
            nv_warning_msg("Unable to select attribute changed NV-CONTROL string"
                           "events.");
        }
    }

    /*
     * TARGET_BINARY_ATTRIBUTE_CHANGED_EVENT was added in NV-CONTROL
     * 1.17
     */
    if (NV_VERSION2(major, minor) >= NV_VERSION2(1, 17)) {
        ret = XNVCtrlSelectTargetNotify(h->dpy,
                                        targetTypeInfo->nvctrl,
                                        h->target_id,
                                        TARGET_BINARY_ATTRIBUTE_CHANGED_EVENT,
                                        True);
        if (ret != True) {
            nv_warning_msg("Unable to select attribute changed NV-CONTROL binary"
                           "events.");
        }
    }

    nv->event_base = event;
    nv->error_base = error;
    nv->major_version = major;
    nv->minor_version = minor;

    return (nv);

} /* NvCtrlInitNvControlAttributes() */


ReturnStatus
NvCtrlNvControlQueryTargetCount(const NvCtrlAttributePrivateHandle *h,
                                CtrlTargetType target_type, int *val)
{
    int ret;
    const CtrlTargetTypeInfo *targetTypeInfo;

    targetTypeInfo = NvCtrlGetTargetTypeInfo(target_type);
    if (targetTypeInfo == NULL) {
        return NvCtrlBadArgument;
    }

    ret = XNVCTRLQueryTargetCount(h->dpy, targetTypeInfo->nvctrl, val);
    return (ret) ? NvCtrlSuccess : NvCtrlError;

} /* NvCtrlNvControlQueryTargetCount() */


ReturnStatus NvCtrlNvControlGetAttribute(const NvCtrlAttributePrivateHandle *h,
                                         unsigned int display_mask,
                                         int attr, int64_t *val)
{
    ReturnStatus status;
    int value_32;
    int major, minor;

    major = h->nv->major_version;
    minor = h->nv->minor_version;

    if (attr <= NV_CTRL_LAST_ATTRIBUTE) {
        const CtrlTargetTypeInfo *targetTypeInfo;
        targetTypeInfo = NvCtrlGetTargetTypeInfo(h->target_type);
        if (targetTypeInfo == NULL) {
            return NvCtrlBadHandle;
        }

        if (NV_VERSION2(major, minor) > NV_VERSION2(1, 20)) {
            status = XNVCTRLQueryTargetAttribute64(h->dpy,
                                                   targetTypeInfo->nvctrl,
                                                   h->target_id,
                                                   display_mask, attr,
                                                   val);
        } else {
            status = XNVCTRLQueryTargetAttribute(h->dpy,
                                                 targetTypeInfo->nvctrl,
                                                 h->target_id,
                                                 display_mask, attr,
                                                 &value_32);
            *val = value_32;
        }
        if (status) {
            return NvCtrlSuccess;
        } else {
            return NvCtrlAttributeNotAvailable;
        }
    }

    if ((attr >= NV_CTRL_ATTR_NV_BASE) &&
        (attr <= NV_CTRL_ATTR_NV_LAST_ATTRIBUTE)) {

        if (!h->nv) return NvCtrlMissingExtension;

        switch (attr) {
        case NV_CTRL_ATTR_NV_MAJOR_VERSION:
            *val = major;
            return NvCtrlSuccess;
        case NV_CTRL_ATTR_NV_MINOR_VERSION:
            *val = minor;
            return NvCtrlSuccess;
        }
    }

    return NvCtrlNoAttribute;
    
} /* NvCtrlNvControlGetAttribute() */


ReturnStatus NvCtrlNvControlSetAttribute (NvCtrlAttributePrivateHandle *h,
                                          unsigned int display_mask,
                                          int attr, int val)
{
    if (attr <= NV_CTRL_LAST_ATTRIBUTE) {
        Bool bRet;
        const CtrlTargetTypeInfo *targetTypeInfo;

        targetTypeInfo = NvCtrlGetTargetTypeInfo(h->target_type);
        if (targetTypeInfo == NULL) {
            return NvCtrlBadHandle;
        }

        bRet = XNVCTRLSetTargetAttributeAndGetStatus(h->dpy,
                                                     targetTypeInfo->nvctrl,
                                                     h->target_id,
                                                     display_mask, attr, val);
        if (!bRet) {
            return NvCtrlError;
        }
        return NvCtrlSuccess;
    }

    return NvCtrlNoAttribute;
}


/*
 * Helper function for converting NV-CONTROL specific permission data into
 * CtrlAttributePerms (API agnostic) permission data that the front-end can use.
 */

static void convertFromNvCtrlPermissions(CtrlAttributePerms *dst,
                                         unsigned int permissions)
{
    memset(dst, 0, sizeof(*dst));

    dst->read = (permissions & ATTRIBUTE_TYPE_READ) ? NV_TRUE : NV_FALSE;
    dst->write = (permissions & ATTRIBUTE_TYPE_WRITE) ? NV_TRUE : NV_FALSE;

    if (permissions & ATTRIBUTE_TYPE_X_SCREEN) {
        dst->valid_targets |= CTRL_TARGET_PERM_BIT(X_SCREEN_TARGET);
    }
    if (permissions & ATTRIBUTE_TYPE_DISPLAY) {
        dst->valid_targets |= CTRL_TARGET_PERM_BIT(DISPLAY_TARGET);
    }
    if (permissions & ATTRIBUTE_TYPE_GPU) {
        dst->valid_targets |= CTRL_TARGET_PERM_BIT(GPU_TARGET);
    }
    if (permissions & ATTRIBUTE_TYPE_FRAMELOCK) {
        dst->valid_targets |= CTRL_TARGET_PERM_BIT(FRAMELOCK_TARGET);
    }
    if (permissions & ATTRIBUTE_TYPE_COOLER) {
        dst->valid_targets |= CTRL_TARGET_PERM_BIT(COOLER_TARGET);
    }
    if (permissions & ATTRIBUTE_TYPE_THERMAL_SENSOR) {
        dst->valid_targets |= CTRL_TARGET_PERM_BIT(THERMAL_SENSOR_TARGET);
    }
    if (permissions & ATTRIBUTE_TYPE_3D_VISION_PRO_TRANSCEIVER) {
        dst->valid_targets |=
            CTRL_TARGET_PERM_BIT(NVIDIA_3D_VISION_PRO_TRANSCEIVER_TARGET);
    }
    if (permissions & ATTRIBUTE_TYPE_MUX) {
        dst->valid_targets |= CTRL_TARGET_PERM_BIT(MUX_TARGET);
    }
}


ReturnStatus
NvCtrlNvControlGetAttributePerms(const NvCtrlAttributePrivateHandle *h,
                                 CtrlAttributeType attr_type, int attr,
                                 CtrlAttributePerms *perms)
{
    NVCTRLAttributePermissionsRec nvctrlPerms;

    switch (attr_type) {
        case CTRL_ATTRIBUTE_TYPE_INTEGER:
            XNVCTRLQueryAttributePermissions(h->dpy, attr, &nvctrlPerms);
            convertFromNvCtrlPermissions(perms, nvctrlPerms.permissions);
            break;

        case CTRL_ATTRIBUTE_TYPE_STRING:
            XNVCTRLQueryStringAttributePermissions(h->dpy, attr, &nvctrlPerms);
            convertFromNvCtrlPermissions(perms, nvctrlPerms.permissions);
            break;

        case CTRL_ATTRIBUTE_TYPE_BINARY_DATA:
            XNVCTRLQueryBinaryDataAttributePermissions(h->dpy, attr, &nvctrlPerms);
            convertFromNvCtrlPermissions(perms, nvctrlPerms.permissions);
            break;

        case CTRL_ATTRIBUTE_TYPE_STRING_OPERATION:
            XNVCTRLQueryStringOperationAttributePermissions(h->dpy, attr,
                                                            &nvctrlPerms);
            convertFromNvCtrlPermissions(perms, nvctrlPerms.permissions);
            break;

        default:
            return NvCtrlBadArgument;
    }

    return NvCtrlSuccess;

}


/*
 * Helper function for converting NV-CONTROL specific valid values data into
 * CtrlAttributeValidValues (API agnostic) data that the front-end can use.
 */

static void
convertFromNvCtrlValidValues(CtrlAttributeValidValues *dst,
                             const NVCTRLAttributeValidValuesRec *src)
{
    memset(dst, 0, sizeof(*dst));

    switch (src->type) {
        case ATTRIBUTE_TYPE_INTEGER:
            dst->valid_type = CTRL_ATTRIBUTE_VALID_TYPE_INTEGER;
            break;
        case ATTRIBUTE_TYPE_BITMASK:
            dst->valid_type = CTRL_ATTRIBUTE_VALID_TYPE_BITMASK;
            break;
        case ATTRIBUTE_TYPE_BOOL:
            dst->valid_type = CTRL_ATTRIBUTE_VALID_TYPE_BOOL;
            break;
        case ATTRIBUTE_TYPE_RANGE:
            dst->valid_type = CTRL_ATTRIBUTE_VALID_TYPE_RANGE;
            break;
        case ATTRIBUTE_TYPE_INT_BITS:
            dst->valid_type = CTRL_ATTRIBUTE_VALID_TYPE_INT_BITS;
            break;
        case ATTRIBUTE_TYPE_64BIT_INTEGER:
            dst->valid_type = CTRL_ATTRIBUTE_VALID_TYPE_64BIT_INTEGER;
            break;
        case ATTRIBUTE_TYPE_STRING:
            dst->valid_type = CTRL_ATTRIBUTE_VALID_TYPE_STRING;
            break;
        case ATTRIBUTE_TYPE_BINARY_DATA:
            dst->valid_type = CTRL_ATTRIBUTE_VALID_TYPE_BINARY_DATA;
            break;
        case ATTRIBUTE_TYPE_STRING_OPERATION:
            dst->valid_type = CTRL_ATTRIBUTE_VALID_TYPE_STRING_OPERATION;
            break;
        default:
            dst->valid_type = CTRL_ATTRIBUTE_VALID_TYPE_UNKNOWN;
    }

    if (src->type == ATTRIBUTE_TYPE_RANGE) {
        dst->range.min = src->u.range.min;
        dst->range.max = src->u.range.max;
    }
    else if (src->type == ATTRIBUTE_TYPE_INT_BITS) {
        dst->allowed_ints = src->u.bits.ints;
    }

    convertFromNvCtrlPermissions(&(dst->permissions), src->permissions);
}

ReturnStatus
NvCtrlNvControlGetValidAttributeValues(const NvCtrlAttributePrivateHandle *h,
                                       unsigned int display_mask,
                                       int attr,
                                       CtrlAttributeValidValues *val)
{
    if (attr <= NV_CTRL_LAST_ATTRIBUTE) {
        const CtrlTargetTypeInfo *targetTypeInfo;
        NVCTRLAttributeValidValuesRec valid;

        targetTypeInfo = NvCtrlGetTargetTypeInfo(h->target_type);
        if (targetTypeInfo == NULL) {
            return NvCtrlBadHandle;
        }

        if (XNVCTRLQueryValidTargetAttributeValues(h->dpy,
                                                   targetTypeInfo->nvctrl,
                                                   h->target_id, display_mask,
                                                   attr, &valid)) {
            if (val != NULL) {
                convertFromNvCtrlValidValues(val, &valid);
            }
            return NvCtrlSuccess;
        } else {
            return NvCtrlAttributeNotAvailable;
        }
    }

    return NvCtrlNoAttribute;

} /* NvCtrlNvControlGetValidAttributeValues() */


ReturnStatus
NvCtrlNvControlGetValidStringDisplayAttributeValues
                                       (const NvCtrlAttributePrivateHandle *h,
                                        unsigned int display_mask,
                                        int attr,
                                        CtrlAttributeValidValues *val)
{
    if (attr <= NV_CTRL_STRING_LAST_ATTRIBUTE) {
        if (NV_VERSION2(h->nv->major_version, h->nv->minor_version)
            >= NV_VERSION2(1, 22)) {

            const CtrlTargetTypeInfo *targetTypeInfo;
            NVCTRLAttributeValidValuesRec valid;

            targetTypeInfo = NvCtrlGetTargetTypeInfo(h->target_type);
            if (targetTypeInfo == NULL) {
                return NvCtrlBadHandle;
            }

            if (XNVCTRLQueryValidTargetStringAttributeValues(h->dpy,
                                                             targetTypeInfo->nvctrl,
                                                             h->target_id,
                                                             display_mask,
                                                             attr, &valid)) {
                if (val != NULL) {
                    convertFromNvCtrlValidValues(val, &valid);
                }
                return NvCtrlSuccess;
            } else {
                return NvCtrlAttributeNotAvailable;
            }
        } else {
            if (val) {
                memset(val, 0, sizeof(*val));
                val->valid_type = CTRL_ATTRIBUTE_VALID_TYPE_STRING;
                val->permissions.read = NV_TRUE;
                val->permissions.valid_targets = CTRL_TARGET_PERM_BIT(X_SCREEN_TARGET);
                return NvCtrlSuccess;
            } else {
                return NvCtrlBadArgument;
            }
        }
    }

    return NvCtrlNoAttribute;

} /* NvCtrlNvControlGetValidStringDisplayAttributeValues() */


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

    if (attr == NV_CTRL_STRING_NV_CONTROL_VERSION) {
        char str[24];
        if (h->target_type != X_SCREEN_TARGET) {
            return NvCtrlBadHandle;
        }
        sprintf(str, "%d.%d", h->nv->major_version, h->nv->minor_version);
        *ptr = strdup(str);
        return NvCtrlSuccess;
    }

    if (attr <= NV_CTRL_STRING_LAST_ATTRIBUTE) {
        char *tmp;
        const CtrlTargetTypeInfo *targetTypeInfo;

        targetTypeInfo = NvCtrlGetTargetTypeInfo(h->target_type);
        if (targetTypeInfo == NULL) {
            return NvCtrlBadHandle;
        }

        if (XNVCTRLQueryTargetStringAttribute(h->dpy,
                                              targetTypeInfo->nvctrl,
                                              h->target_id, display_mask,
                                              attr, &tmp)) {
            if (tmp) {
                *ptr = strdup(tmp);
                XFree(tmp);
            } else {
                *ptr = NULL;
            }
            return NvCtrlSuccess;
        } else {
            return NvCtrlAttributeNotAvailable;
        }
    }

    return NvCtrlNoAttribute;

} /* NvCtrlNvControlGetStringAttribute() */


ReturnStatus
NvCtrlNvControlSetStringAttribute (NvCtrlAttributePrivateHandle *h,
                                   unsigned int display_mask,
                                   int attr, const char *ptr)
{
    if (attr <= NV_CTRL_LAST_ATTRIBUTE) {
        Bool ret;

        /* NV-CONTROL 1.19 and above has support for setting string attributes
         * on targets other than X screens.
         */
        if (NV_VERSION2(h->nv->major_version, h->nv->minor_version) >=
            NV_VERSION2(1, 19)) {

            const CtrlTargetTypeInfo *targetTypeInfo;
            targetTypeInfo = NvCtrlGetTargetTypeInfo(h->target_type);
            if (targetTypeInfo == NULL) {
                return NvCtrlBadHandle;
            }

            ret = XNVCTRLSetTargetStringAttribute(h->dpy,
                                                  targetTypeInfo->nvctrl,
                                                  h->target_id, display_mask,
                                                  attr, ptr);
        } else {
            if (h->target_type != X_SCREEN_TARGET) {
                return NvCtrlBadHandle;
            }
            ret =
                XNVCTRLSetStringAttribute(h->dpy, h->target_id, display_mask,
                                          attr, ptr);
        }

        if ( ret ) {
            return NvCtrlSuccess;
        } else {
            return NvCtrlAttributeNotAvailable;
        }
    }

    return NvCtrlNoAttribute;

} /* NvCtrlNvControlSetStringAttribute() */


ReturnStatus
NvCtrlNvControlGetBinaryAttribute(const NvCtrlAttributePrivateHandle *h,
                                  unsigned int display_mask, int attr,
                                  unsigned char **data, int *len)
{
    unsigned char *tmp;
    Bool ret;
    int localLen;
    const CtrlTargetTypeInfo *targetTypeInfo;

    if (!h->nv) return NvCtrlMissingExtension;

    /* the X_nvCtrlQueryBinaryData opcode was added in 1.7 */

    if (NV_VERSION2(h->nv->major_version, h->nv->minor_version) <
        NV_VERSION2(1, 7)) {
        return NvCtrlNoAttribute;
    }

    if (len == NULL) {
        len = &localLen;
    }

    targetTypeInfo = NvCtrlGetTargetTypeInfo(h->target_type);
    if (targetTypeInfo == NULL) {
        return NvCtrlBadHandle;
    }

    ret = XNVCTRLQueryTargetBinaryData(h->dpy,
                                       targetTypeInfo->nvctrl,
                                       h->target_id,
                                       display_mask, attr, &tmp, len);
    if (!ret) {
        return NvCtrlError;
    }

    if (tmp == NULL) {
        *data = NULL;
        return NvCtrlSuccess;
    }

    *data = malloc(*len);
    if (*data == NULL) {
        *len = 0;
        XFree(tmp);
        return NvCtrlError;
    }

    memcpy(*data, tmp, *len);
    XFree(tmp);

    return NvCtrlSuccess;
}


ReturnStatus
NvCtrlNvControlStringOperation(NvCtrlAttributePrivateHandle *h,
                               unsigned int display_mask, int attr,
                               const char *ptrIn, char **ptrOut)
{
    char *tmp;
    if (attr <= NV_CTRL_STRING_OPERATION_LAST_ATTRIBUTE) {
        const CtrlTargetTypeInfo *targetTypeInfo;
        targetTypeInfo = NvCtrlGetTargetTypeInfo(h->target_type);
        if (targetTypeInfo == NULL) {
            return NvCtrlBadHandle;
        }

        if (XNVCTRLStringOperation(h->dpy, targetTypeInfo->nvctrl,
                                   h->target_id, display_mask,
                                   attr, ptrIn, &tmp)) {
            if (tmp) {
                *ptrOut = strdup(tmp);
                XFree(tmp);
            } else {
                *ptrOut = NULL;
            }
            return NvCtrlSuccess;
        } else {
            return NvCtrlAttributeNotAvailable;
        }
    }

    return NvCtrlNoAttribute;

} /* NvCtrlNvControlStringOperation() */