From 3a385b0a275d748ffa043148e997909fb661f2ed Mon Sep 17 00:00:00 2001 From: Filippo Luca Ferretti Date: Fri, 20 Dec 2024 17:55:44 +0100 Subject: [PATCH 1/5] Add support for Wayland in `iDynTree::Visualizer` --- src/visualization/src/Visualizer.cpp | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/visualization/src/Visualizer.cpp b/src/visualization/src/Visualizer.cpp index d76f7311f0..1e24f6c028 100644 --- a/src/visualization/src/Visualizer.cpp +++ b/src/visualization/src/Visualizer.cpp @@ -26,6 +26,7 @@ #define GLFW_EXPOSE_NATIVE_NSGL #elif defined(__linux__) #define GLFW_EXPOSE_NATIVE_X11 + #define GLFW_EXPOSE_NATIVE_WAYLAND #define GLFW_EXPOSE_NATIVE_GLX #endif @@ -162,7 +163,7 @@ struct Visualizer::VisualizerPimpl #elif defined(__APPLE__) id m_windowId; #elif defined(__linux__) - Window m_windowId; + void* m_windowId; // Pointer to either wl_surface* or X11 Window #endif #endif @@ -505,8 +506,22 @@ bool Visualizer::init(const VisualizerOptions &visualizerOptions) pimpl->m_windowId = glfwGetCocoaWindow(pimpl->m_window); irrDevParams.WindowId = (void*)(pimpl->m_windowId); #elif defined(__linux__) - pimpl->m_windowId = glfwGetX11Window(pimpl->m_window); - irrDevParams.WindowId = (void*)(pimpl->m_windowId); + void* nativeWindow = nullptr; + + // Try Wayland first + struct wl_surface* waylandWindow = glfwGetWaylandWindow(pimpl->m_window); + if (waylandWindow) + { + nativeWindow = static_cast(waylandWindow); + } + else + { + // Fallback to X11 + Window x11Window = glfwGetX11Window(pimpl->m_window); + if (x11Window) + nativeWindow = static_cast(reinterpret_cast(x11Window)); + } + irrDevParams.WindowId = nativeWindow; #endif irrDevParams.DeviceType = irr::EIDT_SDL; From 7021e357df030138ab75aebf17af8bdc601d8d16 Mon Sep 17 00:00:00 2001 From: Filippo Luca Ferretti Date: Tue, 7 Jan 2025 16:29:51 +0100 Subject: [PATCH 2/5] Optionally enable Wayland support Co-authored-by: Silvio Traversaro --- src/visualization/src/Visualizer.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/visualization/src/Visualizer.cpp b/src/visualization/src/Visualizer.cpp index 1e24f6c028..63093118f6 100644 --- a/src/visualization/src/Visualizer.cpp +++ b/src/visualization/src/Visualizer.cpp @@ -28,6 +28,7 @@ #define GLFW_EXPOSE_NATIVE_X11 #define GLFW_EXPOSE_NATIVE_WAYLAND #define GLFW_EXPOSE_NATIVE_GLX + #define IDYNTREE_USES_WAYLAND #endif #include @@ -506,10 +507,16 @@ bool Visualizer::init(const VisualizerOptions &visualizerOptions) pimpl->m_windowId = glfwGetCocoaWindow(pimpl->m_window); irrDevParams.WindowId = (void*)(pimpl->m_windowId); #elif defined(__linux__) + void* nativeWindow = nullptr; - // Try Wayland first - struct wl_surface* waylandWindow = glfwGetWaylandWindow(pimpl->m_window); + #ifdef IDYNTREE_USES_WAYLAND + // Try Wayland first + struct wl_surface* waylandWindow = glfwGetWaylandWindow(pimpl->m_window); + #else + void* waylandWindow = nullptr; + #endif + if (waylandWindow) { nativeWindow = static_cast(waylandWindow); From 4427a68b1365ea5e390a58d56f384949d5b402ec Mon Sep 17 00:00:00 2001 From: Filippo Luca Ferretti <102977828+flferretti@users.noreply.github.com> Date: Thu, 9 Jan 2025 21:03:05 +0100 Subject: [PATCH 3/5] Remove compile option default value --- src/visualization/src/Visualizer.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/visualization/src/Visualizer.cpp b/src/visualization/src/Visualizer.cpp index 63093118f6..40aa0f7e50 100644 --- a/src/visualization/src/Visualizer.cpp +++ b/src/visualization/src/Visualizer.cpp @@ -28,7 +28,6 @@ #define GLFW_EXPOSE_NATIVE_X11 #define GLFW_EXPOSE_NATIVE_WAYLAND #define GLFW_EXPOSE_NATIVE_GLX - #define IDYNTREE_USES_WAYLAND #endif #include From 9c8240070df1c60b25ae1813251c44faef5ec5af Mon Sep 17 00:00:00 2001 From: Silvio Traversaro Date: Fri, 10 Jan 2025 14:30:47 +0100 Subject: [PATCH 4/5] Rename macro IDYNTREE_GLFW_TRY_WAYLAND_FIRST --- src/visualization/src/Visualizer.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/visualization/src/Visualizer.cpp b/src/visualization/src/Visualizer.cpp index 40aa0f7e50..302ff639f9 100644 --- a/src/visualization/src/Visualizer.cpp +++ b/src/visualization/src/Visualizer.cpp @@ -26,7 +26,9 @@ #define GLFW_EXPOSE_NATIVE_NSGL #elif defined(__linux__) #define GLFW_EXPOSE_NATIVE_X11 - #define GLFW_EXPOSE_NATIVE_WAYLAND + #if defined(IDYNTREE_GLFW_TRY_WAYLAND_FIRST) + #define GLFW_EXPOSE_NATIVE_WAYLAND + #endif #define GLFW_EXPOSE_NATIVE_GLX #endif @@ -509,7 +511,7 @@ bool Visualizer::init(const VisualizerOptions &visualizerOptions) void* nativeWindow = nullptr; - #ifdef IDYNTREE_USES_WAYLAND + #ifdef IDYNTREE_GLFW_TRY_WAYLAND_FIRST // Try Wayland first struct wl_surface* waylandWindow = glfwGetWaylandWindow(pimpl->m_window); #else From cedd84291e796628321cf2d8a317bb4c2a34f260 Mon Sep 17 00:00:00 2001 From: Silvio Traversaro Date: Fri, 10 Jan 2025 14:31:38 +0100 Subject: [PATCH 5/5] Expose IDYNTREE_GLFW_TRY_WAYLAND_FIRST option to try to open Windows with Wayland first on Linux --- src/visualization/CMakeLists.txt | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/visualization/CMakeLists.txt b/src/visualization/CMakeLists.txt index 7907eb7c71..ed2e6ed184 100644 --- a/src/visualization/CMakeLists.txt +++ b/src/visualization/CMakeLists.txt @@ -97,6 +97,25 @@ if(IDYNTREE_USES_IRRLICHT) find_library(IOKIT_LIBRARY IOKit) target_link_libraries(${libraryname} LINK_PRIVATE ${CARBON_LIBRARY} ${COCOA_LIBRARY} ${IOKIT_LIBRARY}) endif () + + # On Linux, in some system for some reason creating a windows with X does not work, but creating it with Wayland + # yes. For this reason, we expose an option to permit to try to create a glfw window via wayland. This is an + # option and is not enabled by default as the glfw version shipped via apt with Ubuntu version before 24.10, so + # we expose this as an option and we enable it by default only if we are configuring inside a conda environment + # Once we drop support for apt dependencies on Ubuntu 24.04, we will be able to remove this code and always + # try wayland first + if(NOT WIN32 AND NOT APPLE) + if(DEFINED ENV{CONDA_PREFIX}) + set(IDYNTREE_GLFW_TRY_WAYLAND_FIRST_DEFAULT_VALUE ON) + else() + set(IDYNTREE_GLFW_TRY_WAYLAND_FIRST_DEFAULT_VALUE OFF) + endif() + option(IDYNTREE_GLFW_TRY_WAYLAND_FIRST "If enabled, when creating a window iDynTree will try first to use wayland and only on failure X11" ${IDYNTREE_GLFW_TRY_WAYLAND_FIRST_DEFAULT_VALUE}) + mark_as_advanced(IDYNTREE_GLFW_TRY_WAYLAND_FIRST) + if(IDYNTREE_GLFW_TRY_WAYLAND_FIRST) + add_definitions(-DIDYNTREE_GLFW_TRY_WAYLAND_FIRST) + endif() + endif() endif() if(IDYNTREE_USES_MESHCATCPP)