From 606fa2794d80df7c3b15484b7e522d1d5624b80a Mon Sep 17 00:00:00 2001 From: Etaash Mathamsetty Date: Thu, 1 Aug 2024 17:20:04 -0400 Subject: [PATCH] use egl to get wayland display --- src/gl/inject_egl.cpp | 51 ++++++++++++++++++++++++++++++- src/mangohud.version | 2 -- src/meson.build | 1 - src/wayland_hook.cpp | 66 ---------------------------------------- src/wayland_keybinds.cpp | 2 ++ 5 files changed, 52 insertions(+), 70 deletions(-) delete mode 100644 src/wayland_hook.cpp diff --git a/src/gl/inject_egl.cpp b/src/gl/inject_egl.cpp index ea87add..522bd61 100644 --- a/src/gl/inject_egl.cpp +++ b/src/gl/inject_egl.cpp @@ -10,9 +10,12 @@ #include "mesa/util/os_time.h" #include "blacklist.h" #include "gl_hud.h" +#include "wayland_hook.h" using namespace MangoHud::GL; +#define EGL_PLATFORM_WAYLAND_KHR 0x31D8 + EXPORT_C_(void *) eglGetProcAddress(const char* procName); static void* get_egl_proc_address(const char* name) { @@ -82,15 +85,61 @@ EXPORT_C_(unsigned int) eglSwapBuffers( void* dpy, void* surf) return res; } +EXPORT_C_(void*) eglGetPlatformDisplay( unsigned int platform, void* native_display, const intptr_t* attrib_list); +EXPORT_C_(void*) eglGetPlatformDisplay( unsigned int platform, void* native_display, const intptr_t* attrib_list) +{ + static void* (*pfn_eglGetPlatformDisplay)(unsigned int, void*, const intptr_t*) = nullptr; + if (!pfn_eglGetPlatformDisplay) + pfn_eglGetPlatformDisplay = reinterpret_cast(get_egl_proc_address("eglGetPlatformDisplay")); + + if(platform == EGL_PLATFORM_WAYLAND_KHR) + { + wl_display_ptr = (struct wl_display*)native_display; + HUDElements.display_server = HUDElements.display_servers::WAYLAND; + wl_handle = real_dlopen("libwayland-client.so", RTLD_LAZY); + init_wayland_data(); + } + + return pfn_eglGetPlatformDisplay(platform, native_display, attrib_list); +} + +EXPORT_C_(void*) eglGetDisplay( void* native_display ); +EXPORT_C_(void*) eglGetDisplay( void* native_display ) +{ + static void* (*pfn_eglGetDisplay)(void*) = nullptr; + if (!pfn_eglGetDisplay) + pfn_eglGetDisplay = reinterpret_cast(get_egl_proc_address("eglGetDisplay")); + + try + { + void** display_ptr = (void**)native_display; + wl_interface* iface = (wl_interface*)*display_ptr; + if(strcmp(iface->name, wl_display_interface.name) == 0) + { + wl_display_ptr = (struct wl_display*)native_display; + HUDElements.display_server = HUDElements.display_servers::WAYLAND; + wl_handle = real_dlopen("libwayland-client.so", RTLD_LAZY); + init_wayland_data(); + } + } + catch(const std::exception& e) + { + } + + return pfn_eglGetDisplay(native_display); +} + struct func_ptr { const char *name; void *ptr; }; -static std::array name_to_funcptr_map = {{ +static std::array name_to_funcptr_map = {{ #define ADD_HOOK(fn) { #fn, (void *) fn } ADD_HOOK(eglGetProcAddress), ADD_HOOK(eglSwapBuffers), + ADD_HOOK(eglGetPlatformDisplay), + ADD_HOOK(eglGetDisplay) #undef ADD_HOOK }}; diff --git a/src/mangohud.version b/src/mangohud.version index 5be0a29..7fd09a4 100644 --- a/src/mangohud.version +++ b/src/mangohud.version @@ -8,7 +8,5 @@ dlsym; mangohud_find_glx_ptr; mangohud_find_egl_ptr; - wl_display_connect; - wl_display_connect_to_fd; local: *; }; diff --git a/src/meson.build b/src/meson.build index 9698b5b..1395678 100644 --- a/src/meson.build +++ b/src/meson.build @@ -156,7 +156,6 @@ if is_unixy pre_args += '-DHAVE_WAYLAND' vklayer_files += files( - 'wayland_hook.cpp', 'wayland_keybinds.cpp' ) endif diff --git a/src/wayland_hook.cpp b/src/wayland_hook.cpp deleted file mode 100644 index 98bbc2f..0000000 --- a/src/wayland_hook.cpp +++ /dev/null @@ -1,66 +0,0 @@ -#include -#include -#include -#include -#include "real_dlsym.h" -#include "wayland_hook.h" -#include "hud_elements.h" - -EXPORT_C_(struct wl_display*) wl_display_connect(const char *name); -EXPORT_C_(struct wl_display*) wl_display_connect_to_fd(int fd); - -typedef struct wl_display* (*pwl_display_connect)(const char *name); -typedef struct wl_display* (*pwl_display_connect_to_fd)(int fd); - -pwl_display_connect wl_display_connect_ptr = nullptr; -pwl_display_connect_to_fd wl_display_connect_to_fd_ptr = nullptr; -void* wl_handle = nullptr; -struct wl_display* wl_display_ptr = nullptr; - -EXPORT_C_(struct wl_display*) wl_display_connect(const char *name) -{ - struct wl_display *ret = nullptr; - - if (!wl_handle) { - wl_handle = real_dlopen("libwayland-client.so", RTLD_LAZY); - } - - if (wl_handle) { - wl_display_connect_ptr = (pwl_display_connect)real_dlsym(wl_handle, "wl_display_connect"); - wl_display_connect_to_fd_ptr = (pwl_display_connect_to_fd)real_dlsym(wl_handle, "wl_display_connect_to_fd"); - - ret = wl_display_connect_ptr(name); - - if (!wl_display_ptr) { - wl_display_ptr = ret; - HUDElements.display_server = HUDElements.display_servers::WAYLAND; - init_wayland_data(); - } - } - - return ret; -} - -EXPORT_C_(struct wl_display*) wl_display_connect_to_fd(int fd) -{ - struct wl_display *ret = nullptr; - - if (!wl_handle) { - wl_handle = real_dlopen("libwayland-client.so", RTLD_LAZY); - } - - if (wl_handle) { - wl_display_connect_to_fd_ptr = (pwl_display_connect_to_fd)real_dlsym(wl_handle, "wl_display_connect_to_fd"); - wl_display_connect_ptr = (pwl_display_connect)real_dlsym(wl_handle, "wl_display_connect"); - - ret = wl_display_connect_to_fd_ptr(fd); - - if (!wl_display_ptr) { - wl_display_ptr = ret; - HUDElements.display_server = HUDElements.display_servers::WAYLAND; - init_wayland_data(); - } - } - - return ret; -} diff --git a/src/wayland_keybinds.cpp b/src/wayland_keybinds.cpp index 72cb89f..b87fd0c 100644 --- a/src/wayland_keybinds.cpp +++ b/src/wayland_keybinds.cpp @@ -11,6 +11,8 @@ #include "timing.hpp" #include "keybinds.h" +void* wl_handle; +struct wl_display* wl_display_ptr; struct wl_seat* seat = nullptr; struct wl_keyboard* keyboard = nullptr; struct xkb_context *context_xkb = nullptr;