summaryrefslogtreecommitdiff
path: root/src/libXNVCtrlAttributes/NvCtrlAttributesXrandr.c
blob: 164d77be59d67df44f0acb8887cdfc3d9990e642 (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
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
/*
 * 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 of Version 2 of the GNU General Public
 * License 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 Version 2
 * of 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, write to the:
 *
 *           Free Software Foundation, Inc.
 *           59 Temple Place - Suite 330
 *           Boston, MA 02111-1307, USA
 *
 */

/*
 *  XRandR backend
 */

#include <sys/utsname.h>

#include <stdlib.h> /* 64 bit malloc */
#include <assert.h>
#include <string.h>

#include <dlfcn.h> /* To dynamically load libXrandr.so.2 */
#include <X11/Xlib.h>
#include <X11/extensions/Xrandr.h> /* Xrandr */

#include "NvCtrlAttributes.h"
#include "NvCtrlAttributesPrivate.h"
#include "NVCtrlLib.h"

#include "msg.h"
#include "parse.h"

/* Make sure we are compiling with XRandR version 1.1 or greater */
#define MIN_RANDR_MAJOR 1
#define MIN_RANDR_MINOR 1
#if (RANDR_MAJOR < MIN_RANDR_MAJOR) || ((RANDR_MAJOR == MIN_RANDR_MAJOR) && (RANDR_MINOR < MIN_RANDR_MINOR))
#error XRandR version 1.1 or greater is required.
#endif

typedef struct __libXrandrInfoRec {

    /* libXrandr.so library handle */
    void *handle;
    int   ref_count; /* # users of the library */
    
    
    /* XRandR functions used */
    Bool (* XRRQueryExtension)
         (Display *dpy, int *event_base, int *error_base);
         
    Status (* XRRQueryVersion)
         (Display *dpy, int *major_versionp, int *minor_versionp);

    void (* XRRSelectInput)
         (Display *dpy, Window window, int mask);
         
    XRRScreenConfiguration * (* XRRGetScreenInfo)
         (Display *dpy, Drawable draw);
         
    SizeID (* XRRConfigCurrentConfiguration)
         (XRRScreenConfiguration *config, Rotation *rotation);

    short (* XRRConfigCurrentRate)
         (XRRScreenConfiguration *config);    
         
    Rotation (* XRRConfigRotations)
         (XRRScreenConfiguration *config, Rotation *rotation);
     
    XRRScreenSize * (* XRRConfigSizes)
         (XRRScreenConfiguration *config, int *nsizes);
    
    short * (* XRRConfigRates)
         (XRRScreenConfiguration *config, int size_index, int *nrates);

    Status (* XRRSetScreenConfig)
         (Display *dpy, XRRScreenConfiguration *config, Drawable draw,
          int size_index, Rotation rotation, Time timestamp);

    Status (* XRRSetScreenConfigAndRate)
         (Display *dpy, XRRScreenConfiguration *config, Drawable draw,
          int size_index, Rotation rotation, short rate, Time timestamp);

    void (* XRRFreeScreenConfigInfo)
         (XRRScreenConfiguration *);

} __libXrandrInfo;

static __libXrandrInfo *__libXrandr = NULL;



/******************************************************************************
 *
 * Opens libXrandr for usage
 *
 ****/

static Bool open_libxrandr(void)
{
    const char *error_str = NULL;


    /* Initialize bookkeeping structure */
    if ( !__libXrandr ) {
        __libXrandr = (__libXrandrInfo *) calloc(1, sizeof(__libXrandrInfo));
        if ( !__libXrandr ) {
            error_str = "Could not allocate memory.";
            goto fail;
        }
    }
    
    /* Library was already opened */
    if ( __libXrandr->handle ) {
        __libXrandr->ref_count++;
        return True;
    }


    /* We are the first to open the library */
    __libXrandr->handle = dlopen("libXrandr.so.2", RTLD_LAZY);
    if ( !__libXrandr->handle ) {
        error_str = dlerror();
        goto fail;
    }


    /* Resolve XRandR functions */
    __libXrandr->XRRQueryExtension =
        NV_DLSYM(__libXrandr->handle, "XRRQueryExtension");
    if ((error_str = dlerror()) != NULL) goto fail;

    __libXrandr->XRRQueryVersion =
        NV_DLSYM(__libXrandr->handle, "XRRQueryVersion");
    if ((error_str = dlerror()) != NULL) goto fail;

    __libXrandr->XRRSelectInput =
        NV_DLSYM(__libXrandr->handle, "XRRSelectInput");
    if ((error_str = dlerror()) != NULL) goto fail;

    __libXrandr->XRRGetScreenInfo =
        NV_DLSYM(__libXrandr->handle, "XRRGetScreenInfo");
    if ((error_str = dlerror()) != NULL) goto fail;

    __libXrandr->XRRConfigCurrentConfiguration =
        NV_DLSYM(__libXrandr->handle, "XRRConfigCurrentConfiguration");
    if ((error_str = dlerror()) != NULL) goto fail;

    __libXrandr->XRRConfigCurrentRate =
        NV_DLSYM(__libXrandr->handle, "XRRConfigCurrentRate");
    if ((error_str = dlerror()) != NULL) goto fail;

    __libXrandr->XRRConfigRotations =
        NV_DLSYM(__libXrandr->handle, "XRRConfigRotations");
    if ((error_str = dlerror()) != NULL) goto fail;

    __libXrandr->XRRConfigSizes =
        NV_DLSYM(__libXrandr->handle, "XRRConfigSizes");
    if ((error_str = dlerror()) != NULL) goto fail;

    __libXrandr->XRRConfigRates =
        NV_DLSYM(__libXrandr->handle, "XRRConfigRates");
    if ((error_str = dlerror()) != NULL) goto fail;

    __libXrandr->XRRSetScreenConfig =
        NV_DLSYM(__libXrandr->handle, "XRRSetScreenConfig");
    if ((error_str = dlerror()) != NULL) goto fail;

    __libXrandr->XRRSetScreenConfigAndRate =
        NV_DLSYM(__libXrandr->handle, "XRRSetScreenConfigAndRate");
    if ((error_str = dlerror()) != NULL) goto fail;

    __libXrandr->XRRFreeScreenConfigInfo =
        NV_DLSYM(__libXrandr->handle, "XRRFreeScreenConfigInfo");
    if ((error_str = dlerror()) != NULL) goto fail;


    /* Up the ref count */
    __libXrandr->ref_count++;

    return True;


    /* Handle failures  */
 fail:
    if ( error_str ) {
        nv_error_msg("libXrandr setup error : %s\n", error_str);
    }
    if ( __libXrandr ) {
        if ( __libXrandr->handle ) {
            dlclose(__libXrandr->handle);
            __libXrandr->handle = NULL;
        }
        free(__libXrandr);
        __libXrandr = NULL;
    }
    return False;
    
} /* open_libxrandr() */



/******************************************************************************
 *
 * Closes libXrandr when it is no longer being used.
 *
 ****/

static void close_libxrandr(void)
{
    if ( __libXrandr && __libXrandr->handle && __libXrandr->ref_count ) {
        __libXrandr->ref_count--;
    
#if !defined(NV_BSD) /* WAR for FreeBSD static TLS data bug */
        if ( __libXrandr->ref_count == 0 ) {
            dlclose(__libXrandr->handle);
            __libXrandr->handle = NULL;
            free(__libXrandr);
            __libXrandr = NULL;
        }
#endif
    }

} /* close_libxrandr() */



/******************************************************************************
 *
 * Some XR&R functions fails on XFree86 4.3.0 with BadImplementation if the
 * screen resolution is not the one the server started with.  We work around
 * that by temporarily installing an error handler, trying the call, and then
 * disabling the rotation page if it fails.
 *
 ****/

static int errors = 0;
static int (*old_error_handler)(Display *, XErrorEvent *);

static int
error_handler (Display *dpy, XErrorEvent *err)
{
    errors++;
    return 0;

} /* error_handler() */



/******************************************************************************
 *
 * Initializes the NvCtrlXrandrAttributes Extension by linking the
 * libXrandr.so.2 library and resolving functions used.
 *
 ****/

NvCtrlXrandrAttributes *
NvCtrlInitXrandrAttributes (NvCtrlAttributePrivateHandle *h)
{
    NvCtrlXrandrAttributes * xrandr = NULL;
    XRRScreenConfiguration *sc;
    Rotation rotation, rotations;

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


    /* Open libXrandr.so.2 */
    if ( !open_libxrandr() ) {
        /* Silently fail */
        goto fail;
    }


    /* Create storage for XRandR attributes */
    xrandr =
        (NvCtrlXrandrAttributes *) calloc(1, sizeof(NvCtrlXrandrAttributes));
    if ( !xrandr ) {
        goto fail;
    }
    

    XSync(h->dpy, False);
    errors = 0;
    old_error_handler = XSetErrorHandler(error_handler);
    
    /* Verify server support of XRandR extension */
    if ( !__libXrandr->XRRQueryExtension(h->dpy,
                                         &(xrandr->event_base),
                                         &(xrandr->error_base)) ) {
        XSync(h->dpy, False);
        XSetErrorHandler(old_error_handler);
        goto fail;
    }

    /* Verify server version of the XRandR extension */ 
    if ( !__libXrandr->XRRQueryVersion(h->dpy, &(xrandr->major_version), &(xrandr->minor_version)) ||
         ((xrandr->major_version < MIN_RANDR_MAJOR) ||
          ((xrandr->major_version == MIN_RANDR_MAJOR) &&
           (xrandr->minor_version < MIN_RANDR_MINOR)))) {
        XSync(h->dpy, False);
        XSetErrorHandler(old_error_handler);
        goto fail;
    }
   
    /* Register to receive XRandR events */
    __libXrandr->XRRSelectInput(h->dpy, RootWindow(h->dpy, h->target_id),
                                RRScreenChangeNotifyMask);


    /* Try calling a couple functions that are known to fail */
    sc = __libXrandr->XRRGetScreenInfo(h->dpy, RootWindow(h->dpy, h->target_id));
    if ( sc ) {
        rotations = __libXrandr->XRRConfigRotations(sc, &rotation);
        __libXrandr->XRRFreeScreenConfigInfo(sc);
    } else {
        errors++;
    }

    XSync(h->dpy, False);
    XSetErrorHandler(old_error_handler);
    if ( errors > 0 ) {
        goto fail;
    }

    return xrandr;

 fail:
    if ( xrandr ) {
        free(xrandr);
    }
    return NULL;

} /* NvCtrlInitXrandrAttributes() */



/******************************************************************************
 *
 * Frees and relinquishes any resource used by the NvCtrlXrandrAttributes
 * extension.
 *
 ****/

void
NvCtrlXrandrAttributesClose (NvCtrlAttributePrivateHandle *h)
{
    /* Check parameters */
    if ( !h || !h->xrandr || h->target_type != NV_CTRL_TARGET_TYPE_X_SCREEN ) {
        return;
    }

    close_libxrandr();

    free(h->xrandr);
    h->xrandr = NULL;

} /* NvCtrlXrandrAttributesClose() */



/******************************************************************************
 *
 * Retrieves XRandR integer attributes
 *
 ****/

ReturnStatus
NvCtrlXrandrGetAttribute (NvCtrlAttributePrivateHandle *h,
                          int attr, int *val)
{
    XRRScreenConfiguration *sc;
    Rotation rotation, rotations;


    /* Validate */
    if ( !h || !h->dpy || h->target_type != NV_CTRL_TARGET_TYPE_X_SCREEN ) {
        return NvCtrlBadHandle;
    }
    if ( !h->xrandr || !__libXrandr ) {
        return NvCtrlMissingExtension;
    }
    if ( !val ) {
        return NvCtrlBadArgument;
    }


    /* Get current screen configuration information */
    sc = __libXrandr->XRRGetScreenInfo(h->dpy, RootWindow(h->dpy,
                                                          h->target_id));
    if ( !sc ) {
        return NvCtrlError;
    }
    rotations = __libXrandr->XRRConfigRotations(sc, &rotation);
    __libXrandr->XRRFreeScreenConfigInfo(sc);


    /* Fetch right attribute */
    switch ( attr ) {

    case NV_CTRL_ATTR_XRANDR_ROTATION_SUPPORTED:
        /* For rotation to be supported, there must
         * be at least 2 orientations
         */
        *val = ( rotations != 1 && rotations != 2 &&
                 rotations != 4 && rotations != 8 ) ? 1 : 0;
        break;

    case NV_CTRL_ATTR_XRANDR_ROTATIONS:
        /* Return the available rotations */
        *val = rotations;
        break;

    case NV_CTRL_ATTR_XRANDR_ROTATION:
        /* Return the current rotation */
        *val = (int)rotation;
        break;

    default:
        return NvCtrlNoAttribute;
        break;
    } /* Done fetching attribute */


    return NvCtrlSuccess;

} /* NvCtrlXrandrGetAttribute */



/******************************************************************************
 *
 * Sets XRandR integer attributes
 *
 ****/

static ReturnStatus
set_rotation(NvCtrlAttributePrivateHandle *h, Rotation rotation)
{
    XRRScreenConfiguration *sc;
    Rotation cur_rotation, rotations;
    SizeID cur_size;
    Status ret;


    assert(h->target_type == NV_CTRL_TARGET_TYPE_X_SCREEN);

    /* Get current screen configuration information */
    sc = __libXrandr->XRRGetScreenInfo(h->dpy, RootWindow(h->dpy,
                                                          h->target_id));
    if ( !sc ) {
        return NvCtrlError;
    }
    cur_size = __libXrandr->XRRConfigCurrentConfiguration(sc, &cur_rotation);
    rotations = __libXrandr->XRRConfigRotations(sc, &cur_rotation);


    /* Check orientation we want is supported */
    if ( !(rotations & rotation) ) {
        __libXrandr->XRRFreeScreenConfigInfo(sc);
        return NvCtrlBadArgument;
    }


    /* Set the orientation */
    ret = __libXrandr->XRRSetScreenConfig(h->dpy, sc,
                                          RootWindow(h->dpy, h->target_id),
                                          cur_size, rotation, CurrentTime);
    __libXrandr->XRRFreeScreenConfigInfo(sc);
    
    return ( ret == Success )?NvCtrlSuccess:NvCtrlError;

} /* set_rotation() */


ReturnStatus
NvCtrlXrandrSetAttribute (NvCtrlAttributePrivateHandle *h,
                          int attr, int val)
{
    /* Validate */
    if ( !h || !h->dpy || h->target_type != NV_CTRL_TARGET_TYPE_X_SCREEN ) {
        return NvCtrlBadHandle;
    }
    if ( !h->xrandr || !__libXrandr ) {
        return NvCtrlMissingExtension;
    }


    /* Set right attribute */
    switch ( attr ) {

    case NV_CTRL_ATTR_XRANDR_ROTATION_SUPPORTED:
        return NvCtrlReadOnlyAttribute;
        break;

    case NV_CTRL_ATTR_XRANDR_ROTATIONS:
        return NvCtrlReadOnlyAttribute;
        break;

    case NV_CTRL_ATTR_XRANDR_ROTATION:
        return set_rotation(h, (Rotation)val);
        break;

    default:
        return NvCtrlNoAttribute;
        break;
    }

    return NvCtrlSuccess;

} /* NvCtrlXrandrSetAttribute */


/*
 * Get Xrandr String Attribute Values
 */

ReturnStatus
NvCtrlXrandrGetStringAttribute (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->xrandr || !__libXrandr ) {
        return NvCtrlMissingExtension;
    }

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

    return NvCtrlNoAttribute;

} /* NvCtrlXrandrGetStringAttribute() */


/******************************************************************************
 *
 * Sets XRandR size and refresh rate.
 *
 ****/

ReturnStatus
NvCtrlXrandrSetScreenMagicMode (NvCtrlAttributePrivateHandle *h,
                                int width, int height, int magic_ref_rate)
{
    XRRScreenConfiguration *sc;
    Rotation cur_rotation;
    int nsizes;
    XRRScreenSize *sizes;
    int nrates;
    short *rates;
    Status ret;


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

    if ( !h->xrandr || !__libXrandr ) {
        return NvCtrlMissingExtension;
    }


    /* Get current screen configuration information */
    sc = __libXrandr->XRRGetScreenInfo(h->dpy, RootWindow(h->dpy,
                                                          h->target_id));
    if ( !sc ) {
        return NvCtrlError;
    }
    __libXrandr->XRRConfigRotations(sc, &cur_rotation);


    /* Look for size that has matching refresh rate */
    sizes = __libXrandr->XRRConfigSizes(sc, &nsizes);
    while ( --nsizes >= 0 ) {

        /* Must match in size */
        if ( sizes[nsizes].width  != width ||
             sizes[nsizes].height != height ) {
            continue;
        }

        rates = __libXrandr->XRRConfigRates(sc, nsizes, &nrates);
        while ( --nrates >= 0 ) {

            /* Set the mode if we found the rate */
            if ( *rates == magic_ref_rate ) {
                ret = __libXrandr->XRRSetScreenConfigAndRate
                    (h->dpy, sc, RootWindow(h->dpy, h->target_id), nsizes,
                     cur_rotation, magic_ref_rate, CurrentTime);
                
                __libXrandr->XRRFreeScreenConfigInfo(sc);
                return (ret == Success)?NvCtrlSuccess:NvCtrlError;
            }
            rates++;
        }
    }
    
    /* If we are here, then we could not find the correct mode to set */
    __libXrandr->XRRFreeScreenConfigInfo(sc);
    return NvCtrlError;

} /* NvCtrlXrandrSetScreenMagicMode */



/******************************************************************************
 *
 * Gets XRandR size and refresh rate.
 *
 ****/

ReturnStatus
NvCtrlXrandrGetScreenMagicMode (NvCtrlAttributePrivateHandle *h,
                                int *width, int *height, int *magic_ref_rate)
{
    XRRScreenConfiguration *sc;
    Rotation cur_rotation;
    int nsizes;
    XRRScreenSize *sizes;
    int cur_size;
    short cur_rate;


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

    if ( !h->xrandr || !__libXrandr ) {
        return NvCtrlMissingExtension;
    }


    /* Get current screen configuration information */
    sc = __libXrandr->XRRGetScreenInfo(h->dpy, RootWindow(h->dpy,
                                                          h->target_id));
    if ( !sc ) {
        return NvCtrlError;
    }
    cur_size = __libXrandr->XRRConfigCurrentConfiguration(sc, &cur_rotation);
    cur_rate = __libXrandr->XRRConfigCurrentRate(sc);
    sizes    = __libXrandr->XRRConfigSizes(sc, &nsizes);
    if (cur_size >= nsizes) {
        __libXrandr->XRRFreeScreenConfigInfo(sc);
        return NvCtrlError;
    }


    /* Get the width & height from the size information */
    *width = sizes[cur_size].width;
    *height = sizes[cur_size].height;
    *magic_ref_rate = (int)cur_rate;
    
    __libXrandr->XRRFreeScreenConfigInfo(sc);
    return NvCtrlSuccess;

} /* NvCtrlXrandrGetScreenMagicMode */