forked from BackupGGCode/overdrive5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cadl.hpp
359 lines (309 loc) · 11.5 KB
/
cadl.hpp
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
#ifndef __CADL_HPP__
#define __CADL_HPP__
// Class for ADL interface
class CADLCalls
{
# if defined (LINUX)
typedef void* HINSTANCE;
# endif
enum CallID
{
E_ADL_Main_Control_Create,
E_ADL_Main_Control_Destroy,
E_ADL_Adapter_Active_Get,
E_ADL_Adapter_NumberOfAdapters_Get,
E_ADL_Adapter_AdapterInfo_Get,
E_ADL_Adapter_ID_Get,
E_ADL_Overdrive5_CurrentActivity_Get,
E_ADL_Overdrive5_ThermalDevices_Enum,
E_ADL_Overdrive5_Temperature_Get,
E_ADL_Overdrive5_FanSpeedInfo_Get,
E_ADL_Overdrive5_FanSpeed_Get,
E_ADL_Overdrive5_FanSpeed_Set,
E_ADL_Overdrive5_FanSpeedToDefault_Set,
E_ADL_Overdrive5_ODParameters_Get,
E_ADL_Overdrive5_ODPerformanceLevels_Get,
E_ADL_Overdrive5_ODPerformanceLevels_Set,
E_ADL_Overdrive5_PowerControl_Caps,
E_ADL_Overdrive5_PowerControlInfo_Get,
E_ADL_Overdrive5_PowerControl_Get,
E_ADL_Overdrive5_PowerControl_Set,
E_ADL_CALLID_MAX
};
int Error; // error code of the last call
protected:
HINSTANCE mHDLL; // Handle to DLL
char *mCallName[E_ADL_CALLID_MAX];
void *mProcAddress[E_ADL_CALLID_MAX];
static int ADL_Call_Stub(void)
{
return ADL_ERR_NOT_SUPPORTED;
}
public:
CADLCalls(): Error(ADL_ERR_NOT_INIT), mHDLL(NULL)
{
for (int i = 0; i < E_ADL_CALLID_MAX; i++)
mProcAddress[i] = (void*) ADL_Call_Stub;
};
CADLCalls(const char* dll_name): Error(ADL_ERR_NOT_INIT), mHDLL(NULL)
{
for (int i = 0; i < E_ADL_CALLID_MAX; i++)
mProcAddress[i] = (void*) ADL_Call_Stub;
Init(dll_name);
};
~CADLCalls()
{
if (mHDLL) FreeLibrary(mHDLL);
}
bool IsOK() { return mHDLL != NULL; }
int LastError() { return Error; }
const char* LastErrorStr()
{
switch (Error)
{
case ADL_OK_WAIT:
return "All OK, but need to wait";
case ADL_OK_RESTART:
return "All OK, but need restart";
case ADL_OK_MODE_CHANGE:
return "All OK but need mode change";
case ADL_OK_WARNING:
return "All OK, but with warning";
case ADL_OK:
return "ADL function completed successfully";
case ADL_ERR:
return "Generic Error. Most likely one or more of the Escape calls to the driver failed!";
case ADL_ERR_NOT_INIT:
return "ADL not initialized";
case ADL_ERR_INVALID_PARAM:
return "One of the parameter passed is invalid";
case ADL_ERR_INVALID_PARAM_SIZE:
return "One of the parameter size is invalid";
case ADL_ERR_INVALID_ADL_IDX:
return "Invalid ADL index passed";
case ADL_ERR_INVALID_CONTROLLER_IDX:
return "Invalid controller index passed";
case ADL_ERR_INVALID_DIPLAY_IDX:
return "Invalid display index passed";
case ADL_ERR_NOT_SUPPORTED:
return "Function not supported by the driver";
case ADL_ERR_NULL_POINTER:
return "Null Pointer error";
case ADL_ERR_DISABLED_ADAPTER:
return "Call can't be made due to disabled adapter";
case ADL_ERR_INVALID_CALLBACK:
return "Invalid Callback";
case ADL_ERR_RESOURCE_CONFLICT:
return "Display Resource conflict";
default:
return "Unknown error";
}
}
bool Init(const char* dll_name)
{
mCallName[E_ADL_Main_Control_Create] = "ADL_Main_Control_Create";
mCallName[E_ADL_Main_Control_Destroy] = "ADL_Main_Control_Destroy";
mCallName[E_ADL_Adapter_Active_Get] = "ADL_Adapter_Active_Get";
mCallName[E_ADL_Adapter_NumberOfAdapters_Get] = "ADL_Adapter_NumberOfAdapters_Get";
mCallName[E_ADL_Adapter_AdapterInfo_Get] = "ADL_Adapter_AdapterInfo_Get";
mCallName[E_ADL_Adapter_ID_Get] = "ADL_Adapter_ID_Get";
mCallName[E_ADL_Overdrive5_CurrentActivity_Get] = "ADL_Overdrive5_CurrentActivity_Get";
mCallName[E_ADL_Overdrive5_ThermalDevices_Enum] = "ADL_Overdrive5_ThermalDevices_Enum";
mCallName[E_ADL_Overdrive5_Temperature_Get] = "ADL_Overdrive5_Temperature_Get";
mCallName[E_ADL_Overdrive5_FanSpeedInfo_Get] = "ADL_Overdrive5_FanSpeedInfo_Get";
mCallName[E_ADL_Overdrive5_FanSpeed_Get] = "ADL_Overdrive5_FanSpeed_Get";
mCallName[E_ADL_Overdrive5_FanSpeed_Set] = "ADL_Overdrive5_FanSpeed_Set";
mCallName[E_ADL_Overdrive5_FanSpeedToDefault_Set] = "ADL_Overdrive5_FanSpeedToDefault_Set";
mCallName[E_ADL_Overdrive5_ODParameters_Get] = "ADL_Overdrive5_ODParameters_Get";
mCallName[E_ADL_Overdrive5_ODPerformanceLevels_Get] = "ADL_Overdrive5_ODPerformanceLevels_Get";
mCallName[E_ADL_Overdrive5_ODPerformanceLevels_Set] = "ADL_Overdrive5_ODPerformanceLevels_Set";
mCallName[E_ADL_Overdrive5_PowerControl_Caps] = "ADL_Overdrive5_PowerControl_Caps";
mCallName[E_ADL_Overdrive5_PowerControlInfo_Get] = "ADL_Overdrive5_PowerControlInfo_Get";
mCallName[E_ADL_Overdrive5_PowerControl_Get] = "ADL_Overdrive5_PowerControl_Get";
mCallName[E_ADL_Overdrive5_PowerControl_Set] = "ADL_Overdrive5_PowerControl_Set";
mHDLL = LoadLibrary(dll_name);
if (mHDLL)
{
for (int i = 0; i < E_ADL_CALLID_MAX; i++)
{
void *addr = GetProcAddress(mHDLL, mCallName[i]);
if (addr)
mProcAddress[i] = addr;
}
}
//else fprintf(stderr, "Error: cannot load %s.\n", dll_name);
return IsOK();
}
# if defined (LINUX)
HINSTANCE LoadLibrary(const char* dll_name)
{
return dlopen(dll_name, RTLD_LAZY|RTLD_GLOBAL);
}
void FreeLibrary(HINSTANCE dll)
{
dlclose(dll);
}
void* GetProcAddress(HINSTANCE dll, const char* call_name)
{
return dlsym(dll, call_name);
}
# endif
// ------------------------ wrappers for DLL calls ------------------------
int ADL_Main_Control_Create(ADL_MAIN_MALLOC_CALLBACK callback, int iEnumConnectedAdapters)
{
return Error = ((int (*)(ADL_MAIN_MALLOC_CALLBACK, int)) (mProcAddress[E_ADL_Main_Control_Create]))
(callback, iEnumConnectedAdapters);
};
int ADL_Main_Control_Destroy()
{
return Error = ((int (*)()) (mProcAddress[E_ADL_Main_Control_Destroy]))
();
};
int ADL_Adapter_Active_Get(int iAdapterIndex, int *lpStatus)
{
return Error = ((int (*)(int,int*)) (mProcAddress[E_ADL_Adapter_Active_Get]))
(iAdapterIndex, lpStatus);
}
int ADL_Adapter_NumberOfAdapters_Get(int *lpNumAdapters)
{
return Error = ((int (*)(int*)) (mProcAddress[E_ADL_Adapter_NumberOfAdapters_Get]))
(lpNumAdapters);
}
int ADL_Adapter_AdapterInfo_Get(LPAdapterInfo lpInfo, int iInputSize)
{
return Error = ((int (*)(LPAdapterInfo, int)) (mProcAddress[E_ADL_Adapter_AdapterInfo_Get]))
(lpInfo, iInputSize);
}
int ADL_Adapter_ID_Get(int iAdapterIndex, int *lpAdapterID)
{
return Error = ((int (*)(int,int*)) (mProcAddress[E_ADL_Adapter_ID_Get]))
(iAdapterIndex, lpAdapterID);
}
// Overdrive 5 API
int ADL_Overdrive5_CurrentActivity_Get(int iAdapterIndex, ADLPMActivity *lpActivity)
{
return Error = ((int (*)(int,ADLPMActivity*)) (mProcAddress[E_ADL_Overdrive5_CurrentActivity_Get]))
(iAdapterIndex, lpActivity);
}
int ADL_Overdrive5_ThermalDevices_Enum(int iAdapterIndex, int iThermalControllerIndex, ADLThermalControllerInfo *lpThermalControllerInfo)
{
return Error = ((int (*)(int,int,ADLThermalControllerInfo*)) (mProcAddress[E_ADL_Overdrive5_ThermalDevices_Enum]))
(iAdapterIndex, iThermalControllerIndex, lpThermalControllerInfo);
}
int ADL_Overdrive5_Temperature_Get(int iAdapterIndex, int iThermalControllerIndex, ADLTemperature *lpTemperature)
{
return Error = ((int (*)(int,int,ADLTemperature*)) (mProcAddress[E_ADL_Overdrive5_Temperature_Get]))
(iAdapterIndex, iThermalControllerIndex, lpTemperature);
}
int ADL_Overdrive5_FanSpeedInfo_Get(int iAdapterIndex, int iThermalControllerIndex, ADLFanSpeedInfo *lpFanSpeedInfo)
{
return Error = ((int (*)(int,int,ADLFanSpeedInfo*)) (mProcAddress[E_ADL_Overdrive5_FanSpeedInfo_Get]))
(iAdapterIndex, iThermalControllerIndex, lpFanSpeedInfo);
}
int ADL_Overdrive5_FanSpeed_Get(int iAdapterIndex, int iThermalControllerIndex, ADLFanSpeedValue *lpFanSpeedValue)
{
return Error = ((int (*)(int,int,ADLFanSpeedValue*)) (mProcAddress[E_ADL_Overdrive5_FanSpeed_Get]))
(iAdapterIndex, iThermalControllerIndex, lpFanSpeedValue);
}
int ADL_Overdrive5_FanSpeed_Set(int iAdapterIndex, int iThermalControllerIndex, ADLFanSpeedValue *lpFanSpeedValue)
{
return Error = ((int (*)(int,int,ADLFanSpeedValue*)) (mProcAddress[E_ADL_Overdrive5_FanSpeed_Set]))
(iAdapterIndex, iThermalControllerIndex, lpFanSpeedValue);
}
int ADL_Overdrive5_FanSpeedToDefault_Set(int iAdapterIndex, int iThermalControllerIndex)
{
return Error = ((int (*)(int,int)) (mProcAddress[E_ADL_Overdrive5_FanSpeedToDefault_Set]))
(iAdapterIndex, iThermalControllerIndex);
}
int ADL_Overdrive5_ODParameters_Get(int iAdapterIndex, ADLODParameters *lpOdParameters)
{
return Error = ((int (*)(int,ADLODParameters*)) (mProcAddress[E_ADL_Overdrive5_ODParameters_Get]))
(iAdapterIndex, lpOdParameters);
}
int ADL_Overdrive5_ODPerformanceLevels_Get(int iAdapterIndex, int iDefault, ADLODPerformanceLevels *lpOdPerformanceLevels)
{
return Error = ((int (*)(int,int,ADLODPerformanceLevels*)) (mProcAddress[E_ADL_Overdrive5_ODPerformanceLevels_Get]))
(iAdapterIndex, iDefault, lpOdPerformanceLevels);
}
int ADL_Overdrive5_ODPerformanceLevels_Set(int iAdapterIndex, ADLODPerformanceLevels *lpOdPerformanceLevels)
{
return Error = ((int (*)(int,ADLODPerformanceLevels*)) (mProcAddress[E_ADL_Overdrive5_ODPerformanceLevels_Set]))
(iAdapterIndex, lpOdPerformanceLevels);
}
int ADL_Overdrive5_PowerControl_Caps(int iAdapterIndex, int *lpSupported)
{
return Error = ((int (*)(int,int*)) (mProcAddress[E_ADL_Overdrive5_PowerControl_Caps]))
(iAdapterIndex, lpSupported);
}
int ADL_Overdrive5_PowerControlInfo_Get(int iAdapterIndex, ADLPowerControlInfo *lpPowerControlInfo)
{
return Error = ((int (*)(int,ADLPowerControlInfo*)) (mProcAddress[E_ADL_Overdrive5_PowerControlInfo_Get]))
(iAdapterIndex, lpPowerControlInfo);
}
int ADL_Overdrive5_PowerControl_Get(int iAdapterIndex, int *lpCurrentValue, int *lpDefaultValue)
{
return Error = ((int (*)(int,int*,int*)) (mProcAddress[E_ADL_Overdrive5_PowerControl_Get]))
(iAdapterIndex, lpCurrentValue, lpDefaultValue);
}
int ADL_Overdrive5_PowerControl_Set(int iAdapterIndex, int iValue)
{
return Error = ((int (*)(int,int)) (mProcAddress[E_ADL_Overdrive5_PowerControl_Set]))
(iAdapterIndex, iValue);
}
};
class CADL: public CADLCalls
{
bool mOK;
protected:
static void* __stdcall ADL_Main_Memory_Alloc(int size)
{
return malloc(size);
}
static void __stdcall ADL_Main_Memory_Free(void* ptr)
{
if (ptr) free(ptr);
}
public:
int mNumberAdapters;
LPAdapterInfo mpAdapterInfo;
CADL(): mOK(false), mNumberAdapters(0), mpAdapterInfo(NULL) {}
~CADL()
{
ADL_Main_Control_Destroy();
if (mpAdapterInfo) delete[] mpAdapterInfo;
}
bool IsOK() { return mOK && CADLCalls::IsOK(); }
// init parent and some usefull structs
bool Init(const char *dll_name)
{
if (CADLCalls::Init(dll_name))
{
if (ADL_OK == ADL_Main_Control_Create(ADL_Main_Memory_Alloc, 1))
{
if (ADL_OK == ADL_Adapter_NumberOfAdapters_Get(&mNumberAdapters))
{
if (mNumberAdapters)
{
mpAdapterInfo = new AdapterInfo[mNumberAdapters];
if (mpAdapterInfo)
{
memset(mpAdapterInfo, 0, sizeof(AdapterInfo)*mNumberAdapters);
// Get the AdapterInfo structure for all adapters in the system
if (ADL_OK == ADL_Adapter_AdapterInfo_Get(mpAdapterInfo, sizeof(AdapterInfo)*mNumberAdapters))
{
mOK = true;
}
else fprintf(stderr, "Error: cannot get adapter info.\n");
}
else fprintf(stderr, "Error: cannot allocate memory for adapters.\n");
}
else fprintf(stderr, "Error: no any adapters present.\n");
}
else fprintf(stderr, "Error: cannot get the number of adapters.\n");
}
else fprintf(stderr, "Error: cannot initialize ADL interface.\n");
}
return mOK;
};
};
#endif //__CADL_HPP__