reformat code

This commit is contained in:
sasha0552 2024-06-27 07:36:08 +00:00 committed by GitHub
parent 2432c6fbdf
commit 720de00eb7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,25 +1,22 @@
#include "nvapi.h" #include "nvapi.h"
// TODO: remove
#include <stdio.h>
#ifdef _WIN32 #ifdef _WIN32
#include <libloaderapi.h> # include <libloaderapi.h>
#elif __linux__ #elif __linux__
#include <dlfcn.h> # include <dlfcn.h>
#endif #endif
///// /////
static void* lib; static void * lib;
static bool load_success; static bool load_success;
typedef void* (*nvapi_QueryInterface_t)(int); typedef void * (*nvapi_QueryInterface_t)(int);
typedef int (*NvAPI_EnumPhysicalGPUs_t)(void*, void*); typedef int (*NvAPI_EnumPhysicalGPUs_t)(void *, void *);
typedef int (*NvAPI_GPU_SetForcePstate_t)(void*, int, int); typedef int (*NvAPI_GPU_SetForcePstate_t)(void *, int, int);
typedef int (*NvAPI_Initialize_t)(); typedef int (*NvAPI_Initialize_t)();
typedef int (*NvAPI_Unload_t)(); typedef int (*NvAPI_Unload_t)();
static nvapi_QueryInterface_t nvapi_QueryInterface; static nvapi_QueryInterface_t nvapi_QueryInterface;
static NvAPI_EnumPhysicalGPUs_t NvAPI_EnumPhysicalGPUs; static NvAPI_EnumPhysicalGPUs_t NvAPI_EnumPhysicalGPUs;
@ -30,90 +27,84 @@ static NvAPI_Unload_t NvAPI_Unload;
///// /////
void nvapi_init() { void nvapi_init() {
// load library // load library
#ifdef _WIN32 #ifdef _WIN32
if (!lib) { if (!lib) {
lib = LoadLibrary("nvapi64.dll"); lib = LoadLibrary("nvapi64.dll");
} }
if (!lib) { if (!lib) {
lib = LoadLibrary("nvapi.dll"); lib = LoadLibrary("nvapi.dll");
} }
#elif __linux__ #elif __linux__
if (!lib) { if (!lib) {
lib = dlopen("libnvidia-api.so.1", RTLD_LAZY); lib = dlopen("libnvidia-api.so.1", RTLD_LAZY);
} }
if (!lib) { if (!lib) {
lib = dlopen("libnvidia-api.so", RTLD_LAZY); lib = dlopen("libnvidia-api.so", RTLD_LAZY);
} }
#endif #endif
// lookup QueryInterface // lookup QueryInterface
if (lib) { if (lib) {
#ifdef _WIN32 #ifdef _WIN32
if (!nvapi_QueryInterface) {
nvapi_QueryInterface = (nvapi_QueryInterface_t) GetProcAddress(lib, "nvapi_QueryInterface"); nvapi_QueryInterface = (nvapi_QueryInterface_t) GetProcAddress(lib, "nvapi_QueryInterface");
} #elif __linux__
#elif __linux__
if (!nvapi_QueryInterface) {
nvapi_QueryInterface = (nvapi_QueryInterface_t) dlsym(lib, "nvapi_QueryInterface"); nvapi_QueryInterface = (nvapi_QueryInterface_t) dlsym(lib, "nvapi_QueryInterface");
} #endif
#endif }
}
// resolve functions // resolve functions
if (nvapi_QueryInterface) { if (nvapi_QueryInterface) {
NvAPI_EnumPhysicalGPUs = (NvAPI_EnumPhysicalGPUs_t) nvapi_QueryInterface(0xe5ac921f); NvAPI_EnumPhysicalGPUs = (NvAPI_EnumPhysicalGPUs_t) nvapi_QueryInterface(0xe5ac921f);
NvAPI_GPU_SetForcePstate = (NvAPI_GPU_SetForcePstate_t) nvapi_QueryInterface(0x025bfb10); NvAPI_GPU_SetForcePstate = (NvAPI_GPU_SetForcePstate_t) nvapi_QueryInterface(0x025bfb10);
NvAPI_Initialize = (NvAPI_Initialize_t) nvapi_QueryInterface(0x0150e828); NvAPI_Initialize = (NvAPI_Initialize_t) nvapi_QueryInterface(0x0150e828);
NvAPI_Unload = (NvAPI_Unload_t) nvapi_QueryInterface(0xd22bdd7e); NvAPI_Unload = (NvAPI_Unload_t) nvapi_QueryInterface(0xd22bdd7e);
} }
// initialize library // initialize library
if (NvAPI_Initialize()) { if (NvAPI_Initialize()) {
load_success = true; load_success = true;
} }
} }
void nvapi_free() { void nvapi_free() {
// deinitialize library // deinitialize library
if (load_success) { if (load_success) {
NvAPI_Unload(); NvAPI_Unload();
}
// free library
#ifdef _WIN32
if (lib) {
FreeLibrary(lib);
} }
#elif __linux__
if (lib) {
dlclose(lib);
}
#endif
// invalidate pointers // free library
lib = nullptr; if (lib) {
load_success = false; #ifdef _WIN32
FreeLibrary(lib);
#elif __linux__
dlclose(lib);
#endif
}
// invalidate pointers
lib = nullptr;
load_success = false;
} }
void nvapi_set_pstate(int ids[], int ids_size, int pstate) { void nvapi_set_pstate(int ids[], int ids_size, int pstate) {
if (!load_success) { if (!load_success) {
return; return;
} }
// TODO // TODO
(void) ids; (void) ids;
(void) ids_size; (void) ids_size;
(void) pstate; (void) pstate;
printf("nvapi_set_pstate: %d", pstate); printf("nvapi_set_pstate: %d", pstate);
} }
void nvapi_set_pstate_high() { void nvapi_set_pstate_high() {
nvapi_set_pstate({}, 0, 16); nvapi_set_pstate({}, 0, 16);
} }
void nvapi_set_pstate_low() { void nvapi_set_pstate_low() {
nvapi_set_pstate({}, 0, 8); nvapi_set_pstate({}, 0, 8);
} }