forked from dahenry/gl-gsync-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gsync.c
129 lines (101 loc) · 3.23 KB
/
gsync.c
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
#include <stdio.h>
#include <GL/glxew.h>
#include <NVCtrl/NVCtrl.h>
#include <NVCtrl/NVCtrlLib.h>
#include "gsync.h"
int gsyncInitialize(struct GSyncController *controller)
{
int event_base, error_base, value;
controller->isAvailable = false;
/*
* Open a connection to the X server indicated by the DISPLAY
* environment variable
*/
controller->dpy = XOpenDisplay(NULL);
if (!controller->dpy) {
fprintf(stderr, "Cannot open display '%s'.\n", XDisplayName(NULL));
return 0;
}
/*
* Check if the NV-CONTROL X extension is present on this X server
*/
if (!XNVCTRLQueryExtension(controller->dpy, &event_base, &error_base)) {
fprintf(stderr, "The NV-CONTROL X extension does not exist on '%s'.\n", XDisplayName(NULL));
return 0;
}
/*
* Check if the GSYNC attribute is present
*/
if (!XNVCTRLQueryAttribute(controller->dpy, 0, 0, NV_CTRL_GSYNC_ALLOWED, &value)) {
fprintf(stderr, "The NV-CONTROL GSYNC attribute is not available on '%s'.\n", XDisplayName(NULL));
return 0;
}
controller->isAvailable = true;
/*
* Save current attributes
*/
controller->initialGSYNCValue = gsyncIsAllowed(controller);
controller->initialGSYNCVisualIndicatorValue = gsyncIsVisualIndicatorShown(controller);
return 1;
}
void gsyncFinalize(struct GSyncController *controller)
{
if (controller->isAvailable) {
/* Restore initial attributes */
gsyncSetAllowed(controller, controller->initialGSYNCValue);
gsyncShowVisualIndicator(controller, controller->initialGSYNCVisualIndicatorValue);
}
if (controller->dpy != NULL) {
XCloseDisplay(controller->dpy);
}
}
bool gsyncIsAvailable(struct GSyncController *controller)
{
return controller->isAvailable;
}
bool gsyncIsAllowed(struct GSyncController *controller)
{
int value;
if (controller->dpy != NULL) {
if (XNVCTRLQueryAttribute(controller->dpy, 0, 0, NV_CTRL_GSYNC_ALLOWED, &value)) {
switch(value) {
case NV_CTRL_GSYNC_ALLOWED_FALSE: return false;
case NV_CTRL_GSYNC_ALLOWED_TRUE: return true;
default: return false;
}
}
}
return false;
}
void gsyncSetAllowed(struct GSyncController *controller, bool enable)
{
int value;
switch (enable) {
case false: value = NV_CTRL_GSYNC_ALLOWED_FALSE; break;
case true: value = NV_CTRL_GSYNC_ALLOWED_TRUE; break;
}
XNVCTRLSetAttribute(controller->dpy, 0, 0, NV_CTRL_GSYNC_ALLOWED, value);
}
bool gsyncIsVisualIndicatorShown(struct GSyncController *controller)
{
int value;
if (controller->dpy != NULL) {
if (XNVCTRLQueryAttribute(controller->dpy, 0, 0, NV_CTRL_SHOW_GSYNC_VISUAL_INDICATOR, &value)) {
switch(value) {
case NV_CTRL_SHOW_GSYNC_VISUAL_INDICATOR_FALSE: return false;
case NV_CTRL_SHOW_GSYNC_VISUAL_INDICATOR_TRUE: return true;
default: return false;
}
}
}
return false;
}
void gsyncShowVisualIndicator(struct GSyncController *controller, bool enable)
{
int value;
switch (enable) {
case false: value = NV_CTRL_SHOW_GSYNC_VISUAL_INDICATOR_FALSE; break;
case true: value = NV_CTRL_SHOW_GSYNC_VISUAL_INDICATOR_TRUE; break;
}
XNVCTRLSetAttribute(controller->dpy, 0, 0, NV_CTRL_SHOW_GSYNC_VISUAL_INDICATOR, value);
}