From 72c0826545b4cc173413699a381cc36da78437bf Mon Sep 17 00:00:00 2001 From: Matthew Elwin <10161574+m-elwin@users.noreply.github.com> Date: Fri, 26 Jul 2024 11:13:13 -0500 Subject: [PATCH] Detect wayland and make sure X rendering is used. (#1253) rviz2 does not work under wayland unless using X compatibility. The current workaround on wayland is to set the QT_QPA_PLATFORM environment variable to xcb. This patch now detects if rviz2 is started in a wayland session and if so sets that variable automatically. Signed-off-by: Matthew Elwin --- rviz2/src/main.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/rviz2/src/main.cpp b/rviz2/src/main.cpp index d694fdba1..21ac3e09d 100644 --- a/rviz2/src/main.cpp +++ b/rviz2/src/main.cpp @@ -34,6 +34,7 @@ #include #include // NOLINT: cpplint is unable to handle the include order here +#include // NOLINT: cpplint is unable to hande the include order here #include "rclcpp/rclcpp.hpp" #include "rviz_common/logging.hpp" @@ -44,6 +45,20 @@ int main(int argc, char ** argv) { // remove ROS arguments before passing to QApplication std::vector non_ros_args = rclcpp::remove_ros_arguments(argc, argv); + + // check for wayland and if so force the use of X to render everything + // but only if the user hasn't already tried to specify -platform + // or override QT_QPA_PLATFORM + auto env = QProcessEnvironment::systemEnvironment(); + if(env.value("XDG_SESSION_TYPE") == "wayland" && + non_ros_args.end() == std::find(non_ros_args.begin(), non_ros_args.end(), + "-platform") && + !env.contains("QT_QPA_PLATFORM")) + { + non_ros_args.push_back("-platform"); + non_ros_args.push_back("xcb"); + } + std::vector non_ros_args_c_strings; for (auto & arg : non_ros_args) { non_ros_args_c_strings.push_back(&arg.front());