Skip to content

Commit

Permalink
Enable runtime window system selection on Linux in `iDynTree::Visual…
Browse files Browse the repository at this point in the history
…izer` (#1223)

Co-authored-by: Silvio Traversaro <[email protected]>
  • Loading branch information
flferretti and traversaro authored Jan 10, 2025
1 parent cecb209 commit a788a3b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
19 changes: 19 additions & 0 deletions src/visualization/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
29 changes: 26 additions & 3 deletions src/visualization/src/Visualizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
#define GLFW_EXPOSE_NATIVE_NSGL
#elif defined(__linux__)
#define GLFW_EXPOSE_NATIVE_X11
#if defined(IDYNTREE_GLFW_TRY_WAYLAND_FIRST)
#define GLFW_EXPOSE_NATIVE_WAYLAND
#endif
#define GLFW_EXPOSE_NATIVE_GLX
#endif

Expand Down Expand Up @@ -162,7 +165,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

Expand Down Expand Up @@ -505,8 +508,28 @@ 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;

#ifdef IDYNTREE_GLFW_TRY_WAYLAND_FIRST
// Try Wayland first
struct wl_surface* waylandWindow = glfwGetWaylandWindow(pimpl->m_window);
#else
void* waylandWindow = nullptr;
#endif

if (waylandWindow)
{
nativeWindow = static_cast<void*>(waylandWindow);
}
else
{
// Fallback to X11
Window x11Window = glfwGetX11Window(pimpl->m_window);
if (x11Window)
nativeWindow = static_cast<void*>(reinterpret_cast<void*>(x11Window));
}
irrDevParams.WindowId = nativeWindow;
#endif

irrDevParams.DeviceType = irr::EIDT_SDL;
Expand Down

0 comments on commit a788a3b

Please sign in to comment.