diff --git a/meson.build b/meson.build index 31985b4f..70968c90 100644 --- a/meson.build +++ b/meson.build @@ -98,7 +98,7 @@ dep_wayland_client = dependency('wayland-client', if dep_x11.found() vulkan_wsi_args += ['-DVK_USE_PLATFORM_XLIB_KHR'] - vulkan_wsi_deps += dep_x11 + vulkan_wsi_deps += dep_x11.partial_dependency(compile_args : true, includes : true) endif if dep_wayland_client.found() vulkan_wsi_args += ['-DVK_USE_PLATFORM_WAYLAND_KHR'] diff --git a/src/keybinds.h b/src/keybinds.h index f72bccf1..30abd254 100644 --- a/src/keybinds.h +++ b/src/keybinds.h @@ -1,18 +1,23 @@ -#include -#include -#include "X11/keysym.h" -#include +#pragma once +#include "shared_x11.h" +#include "loaders/loader_x11.h" + +#ifndef KeySym +typedef unsigned long KeySym; +#endif double elapsedF2, elapsedF12, elapsedReloadCfg; uint64_t last_f2_press, last_f12_press, reload_cfg_press; pthread_t f2; -char *displayid = getenv("DISPLAY"); -std::unique_ptr> dpy(XOpenDisplay(displayid), [](Display* d) { XCloseDisplay(d); }); bool key_is_pressed(KeySym ks) { + + if (!init_x11()) + return false; + char keys_return[32]; - XQueryKeymap(dpy.get(), keys_return); - KeyCode kc2 = XKeysymToKeycode(dpy.get(), ks); + g_x11->XQueryKeymap(get_xdisplay(), keys_return); + KeyCode kc2 = g_x11->XKeysymToKeycode(get_xdisplay(), ks); bool isPressed = !!(keys_return[kc2 >> 3] & (1 << (kc2 & 7))); return isPressed; } diff --git a/src/meson.build b/src/meson.build index 7f3f212c..7cfb252a 100644 --- a/src/meson.build +++ b/src/meson.build @@ -61,7 +61,8 @@ opengl_files = files( 'gl/gl3w/GL/gl3w.c', ) -if get_option('with_xnvctrl').enabled() +if get_option('with_x11').enabled() and \ + get_option('with_xnvctrl').enabled() pre_args += '-DHAVE_XNVCTRL' vklayer_files += files( 'loaders/loader_nvctrl.cpp', @@ -74,6 +75,8 @@ if get_option('with_x11').enabled() opengl_files += files( 'gl/inject_glx.cpp', 'loaders/loader_glx.cpp', + 'loaders/loader_x11.cpp', + 'shared_x11.cpp', ) endif diff --git a/src/nvctrl.cpp b/src/nvctrl.cpp index 2b1be80a..3123a689 100644 --- a/src/nvctrl.cpp +++ b/src/nvctrl.cpp @@ -5,10 +5,10 @@ #include "nvctrl.h" #include "loaders/loader_nvctrl.h" #include "string_utils.h" +#include "shared_x11.h" typedef std::unordered_map string_map; -Display *display = XOpenDisplay(NULL); libnvctrl_loader nvctrl("libXNVCtrl.so"); struct nvctrlInfo nvctrl_info; @@ -16,8 +16,8 @@ bool nvctrlSuccess = false; bool checkXNVCtrl() { - if (nvctrl.IsLoaded()) { - nvctrlSuccess = nvctrl.XNVCTRLIsNvScreen(display, 0); + 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; @@ -47,7 +47,7 @@ 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)) { + if (!nvctrl.XNVCTRLQueryTargetStringAttribute(get_xdisplay(), target_type, target_id, 0, attr, &c)) { std::cerr << "Failed to query attribute '" << attr << "'.\n"; } return c; @@ -57,6 +57,9 @@ void getNvctrlInfo(){ string_map params; std::string token; + if (!init_x11()) + return; + int enums[] = { NV_CTRL_STRING_GPU_UTILIZATION, NV_CTRL_STRING_GPU_CURRENT_CLOCK_FREQS, @@ -83,7 +86,7 @@ void getNvctrlInfo(){ nvctrl_info.MemClock = 0; int64_t temp = 0; - nvctrl.XNVCTRLQueryTargetAttribute64(display, + nvctrl.XNVCTRLQueryTargetAttribute64(get_xdisplay(), NV_CTRL_TARGET_TYPE_GPU, 0, 0, @@ -92,7 +95,7 @@ void getNvctrlInfo(){ nvctrl_info.temp = temp; int64_t memtotal = 0; - nvctrl.XNVCTRLQueryTargetAttribute64(display, + nvctrl.XNVCTRLQueryTargetAttribute64(get_xdisplay(), NV_CTRL_TARGET_TYPE_GPU, 0, 0, @@ -101,11 +104,11 @@ void getNvctrlInfo(){ nvctrl_info.memoryTotal = memtotal; int64_t memused = 0; - nvctrl.XNVCTRLQueryTargetAttribute64(display, + nvctrl.XNVCTRLQueryTargetAttribute64(get_xdisplay(), NV_CTRL_TARGET_TYPE_GPU, 0, 0, NV_CTRL_USED_DEDICATED_GPU_MEMORY, &memused); nvctrl_info.memoryUsed = memused; -} \ No newline at end of file +} diff --git a/src/overlay.cpp b/src/overlay.cpp index b978e713..5354731a 100644 --- a/src/overlay.cpp +++ b/src/overlay.cpp @@ -810,13 +810,19 @@ void init_system_info(){ } void check_keybinds(struct overlay_params& params){ + bool pressed = false; // FIXME just a placeholder until wayland support uint64_t now = os_time_get(); /* us */ elapsedF2 = (double)(now - last_f2_press); elapsedF12 = (double)(now - last_f12_press); elapsedReloadCfg = (double)(now - reload_cfg_press); if (elapsedF2 >= 500000 && !params.output_file.empty()){ - if (key_is_pressed(params.toggle_logging)){ +#ifdef HAVE_X11 + pressed = key_is_pressed(params.toggle_logging); +#else + pressed = false; +#endif + if (pressed){ last_f2_press = now; log_start = now; loggingOn = !loggingOn; @@ -828,14 +834,24 @@ void check_keybinds(struct overlay_params& params){ } if (elapsedF12 >= 500000){ - if (key_is_pressed(params.toggle_hud)){ +#ifdef HAVE_X11 + pressed = key_is_pressed(params.toggle_hud); +#else + pressed = false; +#endif + if (pressed){ last_f12_press = now; params.no_display = !params.no_display; } } if (elapsedReloadCfg >= 500000){ - if (key_is_pressed(params.reload_cfg)){ +#ifdef HAVE_X11 + pressed = key_is_pressed(params.reload_cfg); +#else + pressed = false; +#endif + if (pressed){ parse_overlay_config(¶ms, getenv("MANGOHUD_CONFIG")); reload_cfg_press = now; } diff --git a/src/overlay_params.cpp b/src/overlay_params.cpp index 9c461c7e..ad3c63b9 100644 --- a/src/overlay_params.cpp +++ b/src/overlay_params.cpp @@ -26,8 +26,6 @@ #include #include #include -#include -#include #include #include "imgui.h" #include @@ -38,6 +36,11 @@ #include "mesa/util/os_socket.h" +#ifdef HAVE_X11 +#include +#include "loaders/loader_x11.h" +#endif + static enum overlay_param_position parse_position(const char *str) @@ -86,23 +89,31 @@ parse_alpha(const char *str) return strtof(str, NULL); } +#ifdef HAVE_X11 static KeySym parse_toggle_hud(const char *str) { - return XStringToKeysym(str); + if (g_x11->IsLoaded()) + return g_x11->XStringToKeysym(str); + return 0; } static KeySym parse_toggle_logging(const char *str) { - return XStringToKeysym(str); + if (g_x11->IsLoaded()) + return g_x11->XStringToKeysym(str); + return 0; } static KeySym parse_reload_cfg(const char *str) { - return XStringToKeysym(str); + if (g_x11->IsLoaded()) + return g_x11->XStringToKeysym(str); + return 0; } +#endif static uint32_t parse_fps_sampling_period(const char *str)