From 049e9605894915136c76d25b7e2802dcd13cd11b Mon Sep 17 00:00:00 2001 From: jackun Date: Mon, 30 Mar 2020 22:48:56 +0300 Subject: [PATCH] Add 'use_xnvctrl' feature to allow building without libXNVCtrl support --- meson_options.txt | 1 + src/gpu.cpp | 5 ++++- src/meson.build | 12 +++++++++--- src/nvctrl.cpp | 22 ++++++++++------------ src/nvidia_info.h | 2 +- src/nvml.cpp | 3 ++- src/overlay.cpp | 11 ++++++----- 7 files changed, 33 insertions(+), 23 deletions(-) diff --git a/meson_options.txt b/meson_options.txt index f3166bf4..b183bfe1 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -3,3 +3,4 @@ option('use_system_vulkan', type : 'feature', value : 'disabled', description: ' option('mangohud_prefix', type : 'string', value : '', description: 'Add prefix to cross-compiled library, like "lib32-".') option('append_libdir_mangohud', type : 'boolean', value : true, description: 'Append "mangohud" to libdir path or not.') option('include_doc', type : 'boolean', value : true, description: 'Include the example config') +option('use_xnvctrl', type : 'feature', value : 'enabled', description: 'Enable XNVCtrl support') \ No newline at end of file diff --git a/src/gpu.cpp b/src/gpu.cpp index 191ae67a..e53aef8b 100644 --- a/src/gpu.cpp +++ b/src/gpu.cpp @@ -14,7 +14,9 @@ void *getNvidiaGpuInfo(void *){ gpu_info.memoryUsed = nvidiaMemory.used / (1024.f * 1024.f * 1024.f); gpu_info.CoreClock = nvidiaCoreClock; gpu_info.MemClock = nvidiaMemClock; - } else if (nvctrlSuccess) { + } +#ifdef HAVE_XNVCTRL + else if (nvctrlSuccess) { getNvctrlInfo(); gpu_info.load = nvctrl_info.load; gpu_info.temp = nvctrl_info.temp; @@ -22,6 +24,7 @@ void *getNvidiaGpuInfo(void *){ gpu_info.CoreClock = nvctrl_info.CoreClock; gpu_info.MemClock = nvctrl_info.MemClock; } +#endif pthread_detach(gpuThread); return NULL; diff --git a/src/meson.build b/src/meson.build index 48528f72..22ada33e 100644 --- a/src/meson.build +++ b/src/meson.build @@ -44,7 +44,6 @@ vklayer_files = files( 'font_unispace.c', 'cpu.cpp', 'loaders/loader_nvml.cpp', - 'loaders/loader_nvctrl.cpp', 'nvml.cpp', 'file_utils.cpp', 'memory.cpp', @@ -53,7 +52,6 @@ vklayer_files = files( 'gpu.cpp', 'notify.cpp', 'elfhacks.cpp', - 'nvctrl.cpp', ) opengl_files = files( @@ -63,10 +61,18 @@ opengl_files = files( 'loaders/loader_gl.cpp', 'gl/gl3w/GL/gl3w.c', ) + +if get_option('use_xnvctrl').enabled() + pre_args += '-DHAVE_XNVCTRL' + vklayer_files += files( + 'loaders/loader_nvctrl.cpp', + 'nvctrl.cpp', + ) +endif + pre_args += '-DHOOK_DLSYM' inc_opengl = include_directories('gl/gl3w') -# lib_xnvctrl = cc.find_library('XNVCtrl') vklayer_mesa_overlay = shared_library( 'MangoHud', util_files, diff --git a/src/nvctrl.cpp b/src/nvctrl.cpp index 12e60092..2b1be80a 100644 --- a/src/nvctrl.cpp +++ b/src/nvctrl.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include @@ -18,12 +18,12 @@ bool checkXNVCtrl() { if (nvctrl.IsLoaded()) { nvctrlSuccess = nvctrl.XNVCTRLIsNvScreen(display, 0); + if (!nvctrlSuccess) + std::cerr << "MANGOHUD: XNVCtrl didn't find the correct display" << std::endl; return nvctrlSuccess; - } else { - printf("MANGOHUD: XNVCtrl failed to load"); } - if (!nvctrlSuccess) - printf("MANGOHUD: XNVCtrl didn't find the correct display"); + + std::cerr << "MANGOHUD: XNVCtrl failed to load\n"; return false; } @@ -46,13 +46,11 @@ void parse_token(std::string token, string_map& options) { } char* get_attr_target_string(int attr, int target_type, int target_id) { - char* c = nullptr; - - if (!nvctrl.XNVCTRLQueryTargetStringAttribute(display, target_type, target_id, 0, attr, &c)) { - fprintf(stderr, "Failed to query attribute '%d'.\n", attr); - - } - return c; + char* c = nullptr; + if (!nvctrl.XNVCTRLQueryTargetStringAttribute(display, target_type, target_id, 0, attr, &c)) { + std::cerr << "Failed to query attribute '" << attr << "'.\n"; + } + return c; } void getNvctrlInfo(){ diff --git a/src/nvidia_info.h b/src/nvidia_info.h index b914e42f..2da55fd1 100644 --- a/src/nvidia_info.h +++ b/src/nvidia_info.h @@ -10,4 +10,4 @@ extern struct nvmlMemory_st nvidiaMemory; extern bool nvmlSuccess; bool checkNVML(void); -void getNVMLInfo(void); \ No newline at end of file +bool getNVMLInfo(void); \ No newline at end of file diff --git a/src/nvml.cpp b/src/nvml.cpp index 66429dc0..df41a219 100644 --- a/src/nvml.cpp +++ b/src/nvml.cpp @@ -25,7 +25,7 @@ bool checkNVML(){ return false; } -void getNVMLInfo(){ +bool getNVMLInfo(){ nvmlReturn_t response; nvml.nvmlDeviceGetHandleByIndex(0, &nvidiaDevice); response = nvml.nvmlDeviceGetUtilizationRates(nvidiaDevice, &nvidiaUtilization); @@ -35,4 +35,5 @@ void getNVMLInfo(){ nvml.nvmlDeviceGetClockInfo(nvidiaDevice, NVML_CLOCK_MEM, &nvidiaMemClock); if (response == NVML_ERROR_NOT_SUPPORTED) nvmlSuccess = false; + return nvmlSuccess; } \ No newline at end of file diff --git a/src/overlay.cpp b/src/overlay.cpp index 069bbc9f..b978e713 100644 --- a/src/overlay.cpp +++ b/src/overlay.cpp @@ -710,13 +710,14 @@ void init_gpu_stats(uint32_t& vendorID, overlay_params& params) if (vendorID == 0x8086 || vendorID == 0x10de) { - if (checkNVML()) - getNVMLInfo(); + bool nvSuccess = (checkNVML() && getNVMLInfo()); - if (!nvmlSuccess) - checkXNVCtrl(); +#ifdef HAVE_XNVCTRL + if (!nvSuccess) + nvSuccess = checkXNVCtrl(); +#endif - if ((params.enabled[OVERLAY_PARAM_ENABLED_gpu_stats] = (nvmlSuccess || nvctrlSuccess))) { + if ((params.enabled[OVERLAY_PARAM_ENABLED_gpu_stats] = nvSuccess)) { vendorID = 0x10de; } }