-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddon.cc
158 lines (128 loc) · 4.81 KB
/
addon.cc
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
#include <Windows.h>
#include <node.h>
using namespace v8;
typedef struct {
unsigned int version;
int currentDV;
int minDV;
int maxDV;
} NV_DISPLAY_DVC_INFO;
typedef int (*nvapi_Initialize_t)();
typedef int *(*nvapi_QueryInterface_t)(unsigned int offset);
typedef int (*nvapi_GetDVCInfo_t)(int displayHandle, int outputId,
NV_DISPLAY_DVC_INFO *DVCInfo);
typedef int (*nvapi_EnumNvidiaDisplayHandle_t)(int thisEnum, int *handle);
typedef int (*nvapi_EnumPhysicalGPUs_t)(int **handles, int *count);
typedef int (*nvapi_SetDVCLevel_t)(int handle, int outputId, int level);
nvapi_QueryInterface_t nvapi_QueryInterface = NULL;
nvapi_Initialize_t nvapi_Initialize = NULL;
nvapi_GetDVCInfo_t nvapi_GetDVCInfo = NULL;
nvapi_EnumNvidiaDisplayHandle_t nvapi_EnumNvidiaDisplayHandle = NULL;
nvapi_EnumPhysicalGPUs_t nvapi_EnumPhysicalGPUs = NULL;
nvapi_SetDVCLevel_t nvapi_SetDVCLevel = NULL;
int handle = 0;
int InitializeNVApi() {
if (handle > 0) {
return handle;
}
Isolate *isolate = Isolate::GetCurrent();
HMODULE nvapi_h = LoadLibrary("nvapi64.dll");
if (nvapi_h == NULL) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Could not load nvapi64.dll")
.ToLocalChecked()));
return -1;
}
nvapi_QueryInterface =
(nvapi_QueryInterface_t)GetProcAddress(nvapi_h, "nvapi_QueryInterface");
if (nvapi_QueryInterface == NULL) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate,
"Could not find function NvAPI_QueryInterface")
.ToLocalChecked()));
return -1;
}
nvapi_Initialize = (nvapi_Initialize_t)(*nvapi_QueryInterface)(0x0150E828);
int initialized = (*nvapi_Initialize)();
if (initialized != 0) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Could not initialize nvapi")
.ToLocalChecked()));
return initialized;
}
nvapi_EnumNvidiaDisplayHandle =
(nvapi_EnumNvidiaDisplayHandle_t)(*nvapi_QueryInterface)(0x9ABDD40D);
int status = (*nvapi_EnumNvidiaDisplayHandle)(0, &handle);
if (status != 0) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Could not get display handle")
.ToLocalChecked()));
return status;
}
return 0;
}
NV_DISPLAY_DVC_INFO getDVCInfo() {
Isolate *isolate = Isolate::GetCurrent();
NV_DISPLAY_DVC_INFO info = {};
info.version = sizeof(NV_DISPLAY_DVC_INFO) | 0x10000;
nvapi_GetDVCInfo = (nvapi_GetDVCInfo_t)(*nvapi_QueryInterface)(0x4085DE45);
if (nvapi_GetDVCInfo == NULL) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Could not find `nvapi_GetDVCInfo_t`")
.ToLocalChecked()));
return info;
}
int status = (*nvapi_GetDVCInfo)(handle, 0, &info);
if (status != 0) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Could not get digital vibrance")
.ToLocalChecked()));
return info;
}
return info;
}
void GetDigitalVibrance(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
NV_DISPLAY_DVC_INFO info = getDVCInfo();
Local<Context> context = isolate->GetCurrentContext();
Local<Object> obj = Object::New(isolate);
obj->Set(context, String::NewFromUtf8(isolate, "minDV").ToLocalChecked(),
Number::New(isolate, info.minDV))
.FromJust();
obj->Set(context, String::NewFromUtf8(isolate, "maxDV").ToLocalChecked(),
Number::New(isolate, info.maxDV))
.FromJust();
obj->Set(context, String::NewFromUtf8(isolate, "currentDV").ToLocalChecked(),
Number::New(isolate, info.currentDV))
.FromJust();
args.GetReturnValue().Set(obj);
}
void ToggleDigitalVibrance(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
NV_DISPLAY_DVC_INFO info = getDVCInfo();
nvapi_SetDVCLevel = (nvapi_SetDVCLevel_t)(*nvapi_QueryInterface)(0x172409B4);
if (nvapi_SetDVCLevel == NULL) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Could not find `nvapi_SetDVCLevel`")
.ToLocalChecked()));
return;
}
int status = (*nvapi_SetDVCLevel)(
handle, 0, info.currentDV == 0 ? info.maxDV : info.minDV);
if (status != 0) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Could not toggle digital vibrance")
.ToLocalChecked()));
return;
}
Local<Number> num = Number::New(isolate, status);
args.GetReturnValue().Set(num);
}
void Init(Local<Object> exports) {
if (InitializeNVApi() != 0) {
return;
}
NODE_SET_METHOD(exports, "getDigitalVibrance", GetDigitalVibrance);
NODE_SET_METHOD(exports, "toggleDigitalVibrance", ToggleDigitalVibrance);
}
NODE_MODULE(NODE_GYP_MODULE_NAME, Init)