diff --git a/.claude/skills/orca-wxwidgets/references/platforms.md b/.claude/skills/orca-wxwidgets/references/platforms.md index 7652e5c3fb..85c9fb505b 100644 --- a/.claude/skills/orca-wxwidgets/references/platforms.md +++ b/.claude/skills/orca-wxwidgets/references/platforms.md @@ -579,13 +579,15 @@ paths, when `/proc/driver/nvidia/version` exists and the linked WebKitGTK honour `WebView::SupportsForceSharedMemory()`), it sets `WEBKIT_DMABUF_RENDERER_FORCE_SHM=1` and `WEBKIT_FORCE_DMABUF_RENDERER=1` (non-replacing): the proprietary driver cannot back WebKit's hardware DMABUF renderer. On a hypervisor guest (`/proc/cpuinfo` `hypervisor` flag) and WebKitGTK >= 2.44 it sets -`WEBKIT_DMABUF_RENDERER_FORCE_SHM=1` alone, since virtual GPUs cannot back the hardware path either. On the default path it forces `GDK_BACKEND=x11` when wx lacks EGL and `WAYLAND_DISPLAY` -is set, and sets `WEBKIT_DISABLE_COMPOSITING_MODE=1` (non-replacing) inside that branch when the linked -WebKitGTK is older than 2.42: a forced X11 backend in a Wayland session is the only default-path case -that runs through XWayland, which `DISPLAY` and `WAYLAND_DISPLAY` alone do not tell you (a native -Wayland app inherits both). It calls `XInitThreads()` -only when `DISPLAY` is set. With neither variable set, the GUI refuses to start ("Neither DISPLAY nor -WAYLAND_DISPLAY set"). The details and the WebKit rules (c12912e0df) are in +`WEBKIT_DMABUF_RENDERER_FORCE_SHM=1` alone, since virtual GPUs cannot back the hardware path either. In a +Wayland session outside the Flatpak it also keeps the system WebKitGTK off its hardware DMABUF path. On +the default path it forces `GDK_BACKEND=x11` when wx lacks EGL and a Wayland socket is available +(`wayland_socket_available()`), and sets `WEBKIT_DISABLE_COMPOSITING_MODE=1` (non-replacing) inside that +branch when the linked WebKitGTK is older than 2.42: a forced X11 backend in a Wayland session is the +only default-path case that runs through XWayland, which `DISPLAY` and `WAYLAND_DISPLAY` alone do not +tell you (a native Wayland app inherits both). It calls `XInitThreads()` only when `DISPLAY` is set. +With no `DISPLAY` and no Wayland socket (`wayland_socket_available()`), the GUI refuses to start +("Neither DISPLAY nor a Wayland socket found"). The details and the WebKit rules (c12912e0df) are in `references/webview-gl-aui-media.md` §WebKitGTK on Linux sessions. **wx's own detectors**, for reference: diff --git a/.claude/skills/orca-wxwidgets/references/webview-gl-aui-media.md b/.claude/skills/orca-wxwidgets/references/webview-gl-aui-media.md index c9bb2350c4..a42497f664 100644 --- a/.claude/skills/orca-wxwidgets/references/webview-gl-aui-media.md +++ b/.claude/skills/orca-wxwidgets/references/webview-gl-aui-media.md @@ -61,8 +61,9 @@ available; GTK2/WebKit1 is an opt-out configuration the GUI does not support. 11. A web host that re-themes in place handles `EVT_WEBVIEW_RECREATED` without `Skip()`; one that needs a reload lets it through. §[Orca wrapper](#orca-webview-wrapper-widgetswebview) 12. Keep `WebViewWebKit`'s destructor removing the `"wx"` handler. §[Orca wrapper](#orca-webview-wrapper-widgetswebview) -13. Do not widen `WEBKIT_DISABLE_COMPOSITING_MODE` beyond the no-EGL default path where Orca forces - `GDK_BACKEND=x11`, for WebKitGTK older than 2.42, and never +13. Do not widen `WEBKIT_DISABLE_COMPOSITING_MODE` beyond its default-path cases (the no-EGL forced + `GDK_BACKEND=x11` on WebKitGTK older than 2.42, and Wayland outside the Flatpak on WebKitGTK 2.42 + and 2.43, which ignore FORCE_SHM), and never make a page's progress depend only on a C++→JS callback. §[WebKitGTK on Linux](#webkitgtk-on-linux-sessions) 14. GL attribute lists: legacy `int[]` lists end with `0` and spell out `WX_GL_RGBA` and `WX_GL_DOUBLEBUFFER`; `wxGLAttributes`/`wxGLContextAttrs` end with `EndList()`; MSAA is requested @@ -580,8 +581,21 @@ sets the DMABUF workarounds and then branches on `GDK_BACKEND`: Virtual GPUs (VirtualBox VMSVGA, VMware, QEMU) cannot back the hardware DMABUF path either, and FORCE_SHM alone was confirmed to fix web views in VirtualBox; the `WEBKIT_FORCE_DMABUF_RENDERER` pair is not needed without the Debian `isNVIDIA()` patch. The flag is x86-only, so ARM guests are not covered. +- **System WebKitGTK on Wayland**, same position, in a build with EGL and not on the `GDK_BACKEND=x11` + opt-in: when GTK will connect to Wayland and the app is not in the Flatpak, it sets + `WEBKIT_DMABUF_RENDERER_FORCE_SHM=1` non-replacing on WebKitGTK >= 2.44, and + `WEBKIT_DISABLE_COMPOSITING_MODE=1` on 2.42 and 2.43, which have the DMABUF renderer but ignore + FORCE_SHM. Ubuntu's WebKitGTK 2.52 on Intel GPUs and Debian's 2.54 on AMD froze the window on the + hardware path and left the 3D view drawn over other pages; affected builds can't be told apart, so + every system WebKitGTK avoids that path and only the Flatpak's runtime WebKitGTK keeps it. Both + helpers named here are in `libslic3r/Utils.hpp`. `wayland_socket_available()` follows + `wl_display_connect(NULL)`, the connection GTK tries before X11: `WAYLAND_SOCKET` when set (it must + name an open fd), otherwise the `WAYLAND_DISPLAY` socket (`wayland-0` when unset) under + `XDG_RUNTIME_DIR` unless the name is absolute. `XDG_SESSION_TYPE` is not used because an X11 client + can inherit it, and `is_running_in_flatpak()` checks only `/.flatpak-info` because a host process + started from a Flatpak can inherit `FLATPAK_ID`. - **Default path** (`GDK_BACKEND` unset or not starting with `x11`): if wx lacks EGL (`!wxHAS_EGL`) and - `WAYLAND_DISPLAY` is set, it forces `GDK_BACKEND=x11`, and in that same branch it sets + `wayland_socket_available()`, it forces `GDK_BACKEND=x11`, and in that same branch it sets `WEBKIT_DISABLE_COMPOSITING_MODE=1` non-replacing when the linked WebKitGTK predates 2.42 (`WebView::NeedsCompositingWorkaround()`). That forced X11 backend in a Wayland session is the only default-path case that really runs through XWayland; `DISPLAY` and `WAYLAND_DISPLAY` alone do not @@ -601,9 +615,10 @@ Runtime backend detection elsewhere uses Orca's `is_running_on_wayland()` / `is_ (see `references/platforms.md`), not these environment variables. **Pitfall.** -- **Rule:** On NVIDIA and hypervisor guests reach for `WEBKIT_DMABUF_RENDERER_FORCE_SHM` before - `WEBKIT_DISABLE_COMPOSITING_MODE`. **Why:** the proprietary driver and virtual GPUs cannot back WebKit's - hardware DMABUF renderer, leaving web views blank or slow; FORCE_SHM keeps accelerated compositing and only +- **Rule:** On NVIDIA, hypervisor guests and the system WebKitGTK on Wayland reach for + `WEBKIT_DMABUF_RENDERER_FORCE_SHM` before `WEBKIT_DISABLE_COMPOSITING_MODE`. **Why:** the proprietary + driver and virtual GPUs cannot back WebKit's hardware DMABUF renderer, leaving web views blank or slow, + and the system WebKitGTK on Wayland can freeze on it; FORCE_SHM keeps accelerated compositing and only skips the hardware path, while disabling compositing drops it. Threaded scrolling is a separate, release-dependent default (off in 2.44/2.45, on from 2.46). `WEBKIT_DISABLE_DMABUF_RENDERER` is not a substitute — it is read first, leaves the transport set empty (no compositing from 2.44 on, where the @@ -618,19 +633,21 @@ Runtime backend detection elsewhere uses Orca's `is_running_on_wayland()` / `is_ // Wrong: kills compositing and threaded scrolling (and can crash on modern WebKit) ::setenv("WEBKIT_DISABLE_DMABUF_RENDERER", "1", false); // Right: keep compositing, skip only the hardware DMABUF path - if (::access("/proc/driver/nvidia/version", F_OK) == 0 && WebView::SupportsForceSharedMemory()) { - ::setenv("WEBKIT_DMABUF_RENDERER_FORCE_SHM", "1", /*replace*/ false); - ::setenv("WEBKIT_FORCE_DMABUF_RENDERER", "1", /*replace*/ false); + if (WebView::SupportsForceSharedMemory()) { + if (nvidia || is_hypervisor_guest() || system_webkit_on_wayland) + ::setenv("WEBKIT_DMABUF_RENDERER_FORCE_SHM", "1", /*replace*/ false); + if (nvidia) + ::setenv("WEBKIT_FORCE_DMABUF_RENDERER", "1", /*replace*/ false); } ``` Cite: WebKit 2.52.6 `Source/WebKit/UIProcess/gtk/AcceleratedBackingStore.cpp` (`rendererBufferTransportMode()`), Debian `disable-nvidia-dmabuf.patch`, WebKit bug 262607. -- **Rule:** Set `WEBKIT_DISABLE_COMPOSITING_MODE` only on the default path, in the branch where wx - lacks EGL and Orca forces `GDK_BACKEND=x11`, non-replacing (`setenv(..., false)`, so a value the user - already exported wins), before GTK - initialises; never unconditionally, and only for the WebKitGTK releases that need it (older than - 2.42, `WebView::NeedsCompositingWorkaround()`). Keep JS-side fallbacks so a page never - waits forever for a C++→JS call. +- **Rule:** Set `WEBKIT_DISABLE_COMPOSITING_MODE` only on the default path, non-replacing + (`setenv(..., false)`, so a value the user already exported wins), before GTK initialises: in the + branch where wx lacks EGL and Orca forces `GDK_BACKEND=x11`, for WebKitGTK older than 2.42 + (`WebView::NeedsCompositingWorkaround()`), and as the fallback for the system WebKitGTK on Wayland + on 2.42 and 2.43, which have the DMABUF renderer but ignore FORCE_SHM. Never set it unconditionally. + Keep JS-side fallbacks so a page never waits forever for a C++→JS call. **Why:** WebKit2GTK accelerated compositing could fail silently under XWayland before 2.42, leaving web views blank (Setup Wizard stuck on "Loading..."); the variable also turns off threaded scrolling, so applying it on a newer WebKit needlessly drops both GPU compositing and async scrolling (the diff --git a/src/OrcaSlicer.cpp b/src/OrcaSlicer.cpp index 97d5ad83cd..3464a834ed 100644 --- a/src/OrcaSlicer.cpp +++ b/src/OrcaSlicer.cpp @@ -1224,11 +1224,11 @@ int CLI::run(int argc, char **argv) // // GDK_BACKEND unset (DEFAULT) Wayland path. // GTK auto-picks Wayland on Wayland - // sessions and X11 otherwise. WebKit - // XWayland-compositing workaround - // applied only when actually on - // XWayland. XInitThreads gated on - // DISPLAY presence. + // sessions and X11 otherwise. Outside + // the Flatpak, WebKit avoids its + // hardware DMABUF path on Wayland. + // XInitThreads gated on DISPLAY + // presence. // // GDK_BACKEND=x11 (OPT-IN) X11/XWayland mode. Applies the // v2.3.2 workarounds that the X11 @@ -1243,10 +1243,11 @@ int CLI::run(int argc, char **argv) // opt-in path. The ~2020-era WebKit2GTK XWayland compositor bug it // worked around appears fixed in WebKit2GTK >= 2.42; leaving it // unset preserves WebKit hardware acceleration on Device / Setup - // Wizard / login / store. The default path applies it only where it - // forces GDK_BACKEND=x11 below (a build without EGL, so the app really - // runs through XWayland), and only when the linked WebKitGTK predates - // the fix (< 2.42), as a conservative fallback for older WebKit. + // Wizard / login / store. The default path applies it where it forces + // GDK_BACKEND=x11 below (a build without EGL, which runs through + // XWayland) on WebKitGTK < 2.42, and in a Wayland session outside the + // Flatpak on WebKitGTK 2.42 and 2.43, which ignore the shared-memory + // transport. // ------------------------------------------------------------------ { const char* gdk_backend = ::getenv("GDK_BACKEND"); @@ -1254,16 +1255,33 @@ int CLI::run(int argc, char **argv) // honours the first backend in the comma-separated list). const bool x11_opt_in = gdk_backend && boost::starts_with(gdk_backend, "x11"); - // NVIDIA's proprietary driver cannot back WebKitGTK's DMABUF renderer. - // Force the shared-memory transport, which keeps compositing and threaded - // scrolling; the second variable clears Debian/Ubuntu/openSUSE's isNVIDIA(). - if (::access("/proc/driver/nvidia/version", F_OK) == 0 && WebView::SupportsForceSharedMemory()) { - ::setenv("WEBKIT_DMABUF_RENDERER_FORCE_SHM", "1", /* replace */ false); - ::setenv("WEBKIT_FORCE_DMABUF_RENDERER", "1", /* replace */ false); - } + const bool nvidia = ::access("/proc/driver/nvidia/version", F_OK) == 0; - if (is_hypervisor_guest() && WebView::SupportsForceSharedMemory()) - ::setenv("WEBKIT_DMABUF_RENDERER_FORCE_SHM", "1", /* replace */ false); + // The Wayland default below forces GDK_BACKEND=x11 in a build without EGL, so that build runs through XWayland. + #if defined(wxHAS_EGL) && wxHAS_EGL + constexpr bool wx_has_egl = true; + #else + constexpr bool wx_has_egl = false; + #endif + + // System WebKitGTK builds such as Ubuntu's 2.52 on Intel and Debian's 2.54 on AMD freeze the window under + // Wayland on their hardware DMABUF path; affected builds can't be told apart, so the default path avoids it + // for every WebKitGTK outside the Flatpak. + const bool system_webkit_on_wayland = wx_has_egl && !x11_opt_in && wayland_socket_available() && + !is_running_in_flatpak(); + + // NVIDIA's proprietary driver and virtual GPUs cannot back the DMABUF renderer either, and the + // shared-memory transport keeps compositing on. + if (WebView::SupportsForceSharedMemory()) { + if (nvidia || is_hypervisor_guest() || system_webkit_on_wayland) + ::setenv("WEBKIT_DMABUF_RENDERER_FORCE_SHM", "1", /* replace */ false); + // Clears the isNVIDIA() early return that Debian, Ubuntu and openSUSE patch into WebKitGTK. + if (nvidia) + ::setenv("WEBKIT_FORCE_DMABUF_RENDERER", "1", /* replace */ false); + } else if (system_webkit_on_wayland && !WebView::NeedsCompositingWorkaround()) { + // WebKitGTK 2.42 and 2.43 have the DMABUF renderer but ignore the shared-memory switch. + ::setenv("WEBKIT_DISABLE_COMPOSITING_MODE", "1", /* replace */ false); + } if (x11_opt_in) { // ===== X11 / XWayland (opted in via GDK_BACKEND=x11) ===== @@ -1287,8 +1305,7 @@ int CLI::run(int argc, char **argv) // deps were not rebuilt. #if !defined(wxHAS_EGL) || !wxHAS_EGL { - const char* wayland_env = ::getenv("WAYLAND_DISPLAY"); - if (wayland_env && *wayland_env) { + if (wayland_socket_available()) { BOOST_LOG_TRIVIAL(warning) << "Wayland detected but wxWidgets has no EGL support (wxHAS_EGL is OFF). Forcing X11 backend."; ::setenv("GDK_BACKEND", "x11", true); @@ -1488,9 +1505,8 @@ int CLI::run(int argc, char **argv) #if !defined(_WIN32) && !defined(__APPLE__) // likely some linux / unix system const char *display = boost::nowide::getenv("DISPLAY"); - const char *wayland_display = boost::nowide::getenv("WAYLAND_DISPLAY"); - if (! ((display && *display) || (wayland_display && *wayland_display))) { - boost::nowide::cerr << "Neither DISPLAY nor WAYLAND_DISPLAY set, GUI mode not available." << std::endl << std::endl; + if (! ((display && *display) || wayland_socket_available())) { + boost::nowide::cerr << "Neither DISPLAY nor a Wayland socket found, GUI mode not available." << std::endl << std::endl; this->print_help(false); // Indicate an error. return 1; diff --git a/src/libslic3r/Utils.hpp b/src/libslic3r/Utils.hpp index e3f7375f1a..290d7755a6 100644 --- a/src/libslic3r/Utils.hpp +++ b/src/libslic3r/Utils.hpp @@ -209,6 +209,11 @@ const std::string& data_dir(); // The per-user configuration directory an application gets when no --datadir is given: // $XDG_CONFIG_HOME/, or ~/.config/ when XDG_CONFIG_HOME is unset or empty. std::string xdg_config_dir(const std::string &app_name); +// True when this process runs inside a Flatpak sandbox. +bool is_running_in_flatpak(); +// True when the socket wl_display_connect(NULL) would open exists, which is the connection GTK's +// Wayland backend tries before X11. +bool wayland_socket_available(); #endif #ifdef __linux__ // The data directory a start without --datadir uses: a data_dir folder next to the executable diff --git a/src/libslic3r/utils.cpp b/src/libslic3r/utils.cpp index 4ba6d9d90b..617e5c2aa2 100644 --- a/src/libslic3r/utils.cpp +++ b/src/libslic3r/utils.cpp @@ -43,6 +43,7 @@ #include #include #ifndef _WIN32 +#include #include #include #endif @@ -398,6 +399,33 @@ std::string xdg_config_dir(const std::string &app_name) } return base + "/" + app_name; } + +bool is_running_in_flatpak() +{ + // Only the sandbox has this file, while a host process started from a Flatpak can inherit FLATPAK_ID. + return ::access("/.flatpak-info", F_OK) == 0; +} + +bool wayland_socket_available() +{ + if (const char *socket_fd = ::getenv("WAYLAND_SOCKET")) { + char *end = nullptr; + errno = 0; + const long fd = std::strtol(socket_fd, &end, 10); + return errno == 0 && end != socket_fd && *end == '\0' && ::fcntl(static_cast(fd), F_GETFD) != -1; + } + const char *name = ::getenv("WAYLAND_DISPLAY"); + if (name == nullptr) + name = "wayland-0"; + // libwayland uses an empty WAYLAND_DISPLAY as given, and connecting to it fails. + if (*name == '\0') + return false; + if (name[0] == '/') + return ::access(name, F_OK) == 0; + const char *runtime_dir = ::getenv("XDG_RUNTIME_DIR"); + return runtime_dir != nullptr && runtime_dir[0] == '/' && + ::access((std::string(runtime_dir) + "/" + name).c_str(), F_OK) == 0; +} #endif #ifdef __linux__ diff --git a/src/slic3r/GUI/GUI_App.cpp b/src/slic3r/GUI/GUI_App.cpp index 819e06adf7..e548ca290d 100644 --- a/src/slic3r/GUI/GUI_App.cpp +++ b/src/slic3r/GUI/GUI_App.cpp @@ -593,7 +593,7 @@ private: #ifdef __linux__ static void migrate_flatpak_legacy_datadir(const boost::filesystem::path &data_dir_path) { - if(!boost::filesystem::exists("/.flatpak-info")) + if (!is_running_in_flatpak()) return; // Not running as a Flatpak, nothing to migrate. namespace fs = boost::filesystem; diff --git a/src/slic3r/GUI/TroubleshootDialog.cpp b/src/slic3r/GUI/TroubleshootDialog.cpp index 85324f9776..240fcde4b8 100644 --- a/src/slic3r/GUI/TroubleshootDialog.cpp +++ b/src/slic3r/GUI/TroubleshootDialog.cpp @@ -794,9 +794,8 @@ wxString TroubleshootDialog::GetPackageType() return "Portable"; #elif defined(__LINUX__) if (wxGetEnv("APPIMAGE" , nullptr)) return "AppImage"; - if (wxGetEnv("FLATPAK_ID", nullptr)) return "Flatpak"; + if (Slic3r::is_running_in_flatpak()) return "Flatpak"; //if (wxGetEnv("SNAP" , nullptr)) return "Snap"; - if (wxFileExists("/.flatpak-info")) return "Flatpak"; //if (wxFileExists("/usr/bin/dpkg")) return "Debian/Ubuntu (deb)"; //if (wxFileExists("/usr/bin/rpm")) return "RPM-based (rpm)"; //if (wxFileExists("/usr/bin/pacman")) return "Arch (pacman)"; diff --git a/tests/libslic3r/test_utils.cpp b/tests/libslic3r/test_utils.cpp index 3b3f10df00..b3293be295 100644 --- a/tests/libslic3r/test_utils.cpp +++ b/tests/libslic3r/test_utils.cpp @@ -22,13 +22,15 @@ #include #include #include +#include #include #include #include #include #ifndef _WIN32 -#include // getuid +#include // open +#include // getuid, close #endif using namespace Slic3r; @@ -581,6 +583,82 @@ TEST_CASE("xdg_config_dir follows XDG_CONFIG_HOME and falls back to ~/.config", CHECK(Slic3r::xdg_config_dir("OrcaSlicer") == "/tmp/home-test/.config/OrcaSlicer"); } } + +TEST_CASE("is_running_in_flatpak ignores an inherited FLATPAK_ID", "[utils]") { + if (boost::filesystem::exists("/.flatpak-info")) + SKIP("running inside a Flatpak sandbox"); + ScopedEnvVar id("FLATPAK_ID", "com.orcaslicer.OrcaSlicer"); + CHECK_FALSE(Slic3r::is_running_in_flatpak()); +} + +namespace { +// An XDG_RUNTIME_DIR holding a wayland-0 entry, with WAYLAND_SOCKET unset. +struct WaylandRuntimeDir +{ + ScopedTemporaryDir dir; + ScopedEnvVar runtime_dir{"XDG_RUNTIME_DIR", dir.string()}; + ScopedEnvVar socket_fd{"WAYLAND_SOCKET", std::nullopt}; + WaylandRuntimeDir() { std::ofstream((dir.path() / "wayland-0").string()).close(); } +}; +} // namespace + +TEST_CASE("wayland_socket_available falls back to wayland-0 when WAYLAND_DISPLAY is unset", "[utils]") { + WaylandRuntimeDir runtime; + ScopedEnvVar display("WAYLAND_DISPLAY", std::nullopt); + CHECK(Slic3r::wayland_socket_available()); +} + +TEST_CASE("wayland_socket_available looks up WAYLAND_DISPLAY under XDG_RUNTIME_DIR", "[utils]") { + WaylandRuntimeDir runtime; + { + ScopedEnvVar display("WAYLAND_DISPLAY", "wayland-0"); + CHECK(Slic3r::wayland_socket_available()); + } + { + ScopedEnvVar display("WAYLAND_DISPLAY", "wayland-1"); + CHECK_FALSE(Slic3r::wayland_socket_available()); + } +} + +TEST_CASE("wayland_socket_available finds no socket for an empty WAYLAND_DISPLAY", "[utils]") { + WaylandRuntimeDir runtime; + ScopedEnvVar display("WAYLAND_DISPLAY", ""); + CHECK_FALSE(Slic3r::wayland_socket_available()); +} + +TEST_CASE("wayland_socket_available takes an absolute WAYLAND_DISPLAY without XDG_RUNTIME_DIR", "[utils]") { + WaylandRuntimeDir runtime; + ScopedEnvVar display("WAYLAND_DISPLAY", (runtime.dir.path() / "wayland-0").string()); + ScopedEnvVar no_runtime_dir("XDG_RUNTIME_DIR", std::nullopt); + CHECK(Slic3r::wayland_socket_available()); +} + +TEST_CASE("wayland_socket_available needs an absolute XDG_RUNTIME_DIR for a relative name", "[utils]") { + WaylandRuntimeDir runtime; + ScopedEnvVar display("WAYLAND_DISPLAY", "wayland-0"); + ScopedEnvVar relative_runtime_dir("XDG_RUNTIME_DIR", "run/user"); + CHECK_FALSE(Slic3r::wayland_socket_available()); +} + +TEST_CASE("wayland_socket_available uses WAYLAND_SOCKET instead of the socket file when it is set", "[utils]") { + WaylandRuntimeDir runtime; + ScopedEnvVar display("WAYLAND_DISPLAY", "wayland-0"); + { + ScopedEnvVar socket_fd("WAYLAND_SOCKET", "not-a-number"); + CHECK_FALSE(Slic3r::wayland_socket_available()); + } + const int fd = ::open((runtime.dir.path() / "wayland-0").string().c_str(), O_RDONLY); + REQUIRE(fd != -1); + { + ScopedEnvVar socket_fd("WAYLAND_SOCKET", std::to_string(fd)); + CHECK(Slic3r::wayland_socket_available()); + } + ::close(fd); + { + ScopedEnvVar socket_fd("WAYLAND_SOCKET", std::to_string(fd)); + CHECK_FALSE(Slic3r::wayland_socket_available()); + } +} #endif #ifdef __linux__ diff --git a/tests/test_utils.hpp b/tests/test_utils.hpp index f677b2de0b..71f403b86f 100644 --- a/tests/test_utils.hpp +++ b/tests/test_utils.hpp @@ -204,7 +204,8 @@ private: boost::filesystem::path m_previous; }; -// Sets an environment variable for the lifetime of the guard and restores the previous value. +// Sets, or with std::nullopt unsets, an environment variable for the lifetime of the guard and +// restores the previous value. class ScopedEnvVar { public: @@ -214,6 +215,12 @@ public: m_previous = old; boost::nowide::setenv(m_name.c_str(), value.c_str(), 1); } + ScopedEnvVar(std::string name, std::nullopt_t) : m_name(std::move(name)) + { + if (const char* old = boost::nowide::getenv(m_name.c_str())) + m_previous = old; + boost::nowide::unsetenv(m_name.c_str()); + } ~ScopedEnvVar() { if (m_previous)