From 723c6bedeb950f408b5c7e3d26fb7f3cf749ed21 Mon Sep 17 00:00:00 2001 From: jackun Date: Tue, 14 Apr 2020 00:25:27 +0300 Subject: [PATCH] Fix `init_x11` return value and X11/XNVCtrl logic checks --- src/nvctrl.cpp | 16 +++++++++------- src/shared_x11.cpp | 29 ++++++++++++++++++++--------- 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/src/nvctrl.cpp b/src/nvctrl.cpp index 3123a689..a16fda29 100644 --- a/src/nvctrl.cpp +++ b/src/nvctrl.cpp @@ -16,15 +16,17 @@ bool nvctrlSuccess = false; bool checkXNVCtrl() { - if (init_x11() && nvctrl.IsLoaded()) { - nvctrlSuccess = nvctrl.XNVCTRLIsNvScreen(get_xdisplay(), 0); - if (!nvctrlSuccess) - std::cerr << "MANGOHUD: XNVCtrl didn't find the correct display" << std::endl; - return nvctrlSuccess; + if (init_x11()) { + if (nvctrl.IsLoaded()) { + nvctrlSuccess = nvctrl.XNVCTRLIsNvScreen(get_xdisplay(), 0); + if (!nvctrlSuccess) + std::cerr << "MANGOHUD: XNVCtrl didn't find the correct display" << std::endl; + return nvctrlSuccess; + } else { + std::cerr << "MANGOHUD: XNVCtrl failed to load\n"; + } } - std::cerr << "MANGOHUD: XNVCtrl failed to load\n"; - return false; } diff --git a/src/shared_x11.cpp b/src/shared_x11.cpp index eec9275d..88eb1bf4 100644 --- a/src/shared_x11.cpp +++ b/src/shared_x11.cpp @@ -9,23 +9,34 @@ static std::unique_ptr> display; bool init_x11() { static bool failed = false; - if (failed || !g_x11->IsLoaded()) + if (failed) return false; if (display) return true; + if (!g_x11->IsLoaded()) { + std::cerr << "MANGOHUD: X11 loader failed to load\n"; + failed = true; + return false; + } + const char *displayid = getenv("DISPLAY"); - auto local_x11 = g_x11; - display = { g_x11->XOpenDisplay(displayid), - [local_x11](Display* dpy) { - if (dpy) - local_x11->XCloseDisplay(dpy); - } - }; + if (displayid) { + auto local_x11 = g_x11; + display = { g_x11->XOpenDisplay(displayid), + [local_x11](Display* dpy) { + if (dpy) + local_x11->XCloseDisplay(dpy); + } + }; + } failed = !display; - return failed; + if (failed) + std::cerr << "MANGOHUD: XOpenDisplay failed to open display '" << displayid << "'\n"; + + return !!display; } Display* get_xdisplay()