Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions mujoco_ros2_control/src/mujoco_simulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "mujoco_ros2_control/mujoco_simulation.hpp"
#include "array_safety.h"
#include "mujoco_ros2_control/sim_display_text.hpp"
#include "mujoco_ros2_control_plugins/glfw_platform.hpp"

#include <unistd.h>
#include <cerrno>
Expand Down Expand Up @@ -555,6 +556,8 @@ bool MujocoSimulation::initialize(rclcpp::Node::SharedPtr node, const std::strin
sim_speed_factor_ = sim_speed_factor;
headless_ = headless;

mujoco_ros2_control_plugins::prefer_x11_without_wayland_display();

if (sim_speed_factor_ > 0)
{
RCLCPP_INFO(get_logger(), "Running the simulation at %.2f percent speed", sim_speed_factor_ * 100.0);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef MUJOCO_ROS2_CONTROL_PLUGINS__GLFW_PLATFORM_HPP_
#define MUJOCO_ROS2_CONTROL_PLUGINS__GLFW_PLATFORM_HPP_

#include <cstdlib>

#include <GLFW/glfw3.h>

namespace mujoco_ros2_control_plugins
{

/**
* GLFW >= 3.4 built with both Wayland and X11 probes Wayland first unless XDG_SESSION_TYPE says otherwise. Without
* a Wayland compositor (e.g. inside Docker) libwayland prints "XDG_RUNTIME_DIR is invalid or not set" before GLFW
* falls back to X11. Skip the probe when no Wayland display is advertised. Must run before glfwInit().
*
* Static so that each shared library calls the GLFW it links against; a statically linked GLFW has per-library state.
*/
static inline void prefer_x11_without_wayland_display()
{
#if GLFW_VERSION_MAJOR > 3 || (GLFW_VERSION_MAJOR == 3 && GLFW_VERSION_MINOR >= 4)
if (std::getenv("WAYLAND_DISPLAY") == nullptr && glfwPlatformSupported(GLFW_PLATFORM_X11))
{
glfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_X11);
}
#endif
}

} // namespace mujoco_ros2_control_plugins

#endif // MUJOCO_ROS2_CONTROL_PLUGINS__GLFW_PLATFORM_HPP_
3 changes: 3 additions & 0 deletions mujoco_ros2_control_plugins/src/camera_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#include "camera_plugin.hpp"

#include "mujoco_ros2_control_plugins/glfw_platform.hpp"

namespace mujoco_ros2_control_plugins
{

Expand Down Expand Up @@ -51,6 +53,7 @@ bool CameraPlugin::init(rclcpp::Node::SharedPtr node, const mjModel* model, mjDa

// Start the rendering thread process
// Try GLFW first, fall back to EGL for headless environments
prefer_x11_without_wayland_display();
if (glfw_init_fn())
{
use_egl_ = false;
Expand Down
Loading