From 12ce9d6fe628fcf4de80e5a78ca0e1d940888910 Mon Sep 17 00:00:00 2001 From: HanifKoh <76276251+HanifKoh@users.noreply.github.com> Date: Mon, 26 Oct 2026 14:02:54 +0800 Subject: [PATCH] Make the Discrete GPU a Linux Preference Instead of a Desktop-Entry Hint (#16875) * Make the Discrete GPU a Linux Preference Instead of a Desktop-Entry Hint The desktop entries asked the launcher for the discrete GPU with PrefersNonDefaultGPU and X-KDE-RunOnDiscreteGpu. KDE and GNOME honour that by adding the PRIME variables to every menu launch, so on a hybrid laptop under Wayland the 3D view was rendered by zink on the GPU that does not drive the display and its frames never reached the compositor: an empty Prepare and Preview canvas from the menu, a working one from a terminal. Drop the two keys from the installed desktop file, the AppImage's embedded entry and the entries the Desktop Integration dialog writes. Add a "Render on the discrete GPU" preference (Linux, default off, requires restart) that exports DRI_PRIME and, with the NVIDIA kernel module loaded, the NVIDIA render-offload variables before GTK opens the display. The GDK_BACKEND=x11 opt-in path exported them unconditionally; it now leaves GPU selection to the preference as well. The preference acts before GUI_App loads the configuration, so AppConfig::peek_bool reads one app boolean straight from the saved file and xdg_config_dir resolves the default Linux data dir the way the wx code did; GUI_App's two copies of that lookup use the same helper, and AppConfig::config_path gained a static form so the file name lives in one place. * Share the Linux Data-Dir Lookup and Let DRI_PRIME Override the GPU Preference default_data_dir() in libslic3r resolves the Linux data dir a start without --datadir uses: a portable data_dir folder next to the executable, else the XDG config dir. GUI_App::init_app_config and the discrete-GPU start-up code both call it, so they cannot drift apart. A DRI_PRIME already in the environment now skips the export entirely, so one start with DRI_PRIME=0 gets back to Preferences when the discrete GPU cannot open the window. The tooltip says so. * Attach the DRI_PRIME Translator Note to the Tooltip It Describes --- .../orca-wxwidgets/references/platforms.md | 7 ++- .../references/webview-gl-aui-media.md | 10 +++- src/OrcaSlicer.cpp | 57 +++++++++++++------ .../platform/unix/build_appimage.sh.in | 2 - .../unix/com.orcaslicer.OrcaSlicer.desktop | 2 - src/libslic3r/AppConfig.cpp | 43 +++++++++++--- src/libslic3r/AppConfig.hpp | 10 +++- src/libslic3r/Utils.hpp | 10 ++++ src/libslic3r/utils.cpp | 45 +++++++++++++++ src/slic3r/GUI/DesktopIntegrationDialog.cpp | 4 -- src/slic3r/GUI/GUI_App.cpp | 34 +++++------ src/slic3r/GUI/Preferences.cpp | 10 ++++ tests/libslic3r/test_appconfig.cpp | 42 ++++++++++++++ tests/libslic3r/test_utils.cpp | 23 ++++++++ 14 files changed, 240 insertions(+), 59 deletions(-) diff --git a/.claude/skills/orca-wxwidgets/references/platforms.md b/.claude/skills/orca-wxwidgets/references/platforms.md index 882a7dc59c..7652e5c3fb 100644 --- a/.claude/skills/orca-wxwidgets/references/platforms.md +++ b/.claude/skills/orca-wxwidgets/references/platforms.md @@ -379,9 +379,10 @@ form a chain of parents. Window icons come from the `.desktop` file. The full li ### XWayland (GTK3 X11 backend inside a Wayland session) Users opt in with `GDK_BACKEND=x11…`. GTK then talks X11, so `is_running_on_x11()` is true and Orca -uses GLX through `PreferGLX()`. `CLI::run` prepares this path before GTK starts (PRIME variables, -`XInitThreads()`, no WebKit compositing workaround; the source comment says multi-monitor handling -is compromised there) → [Runtime detection](#runtime-x11wayland-detection), +uses GLX through `PreferGLX()`. `CLI::run` prepares this path before GTK starts (`XInitThreads()`, no +WebKit compositing workaround; GPU choice is the `prefer_discrete_gpu` preference on either backend; +the source comment says multi-monitor handling is compromised there) → +[Runtime detection](#runtime-x11wayland-detection), `references/webview-gl-aui-media.md` §WebKitGTK on Linux sessions. Intel's XWayland GL exposes a smaller `GL_MAX_TEXTURE_SIZE`, so the ImGui font atlas is re-packed to fit (22e121f4e4) → `references/webview-gl-aui-media.md`. 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 e707c99ebd..c9bb2350c4 100644 --- a/.claude/skills/orca-wxwidgets/references/webview-gl-aui-media.md +++ b/.claude/skills/orca-wxwidgets/references/webview-gl-aui-media.md @@ -587,9 +587,15 @@ sets the DMABUF workarounds and then branches on `GDK_BACKEND`: default-path case that really runs through XWayland; `DISPLAY` and `WAYLAND_DISPLAY` alone do not identify it, because a native Wayland app inherits both. `XInitThreads()` runs only when `DISPLAY` is set. -- **`GDK_BACKEND=x11…` opt-in path**: sets `DRI_PRIME=1` (and the NVIDIA PRIME variables when - `/proc/driver/nvidia/version` exists), calls `XInitThreads()`, and deliberately does **not** disable +- **`GDK_BACKEND=x11…` opt-in path**: calls `XInitThreads()` and deliberately does **not** disable compositing, which keeps hardware acceleration for Device / Setup Wizard / login pages. +- **GPU selection** is independent of the backend. The Linux preference `prefer_discrete_gpu` ("Render on + the discrete GPU", default off) makes `apply_discrete_gpu_preference()` export `DRI_PRIME=1` and, when + `/proc/driver/nvidia/version` exists, the NVIDIA render-offload variables before GTK opens the display; + it reads the saved configuration through `AppConfig::peek_bool` because no `AppConfig` exists yet. The + desktop entries carry no `PrefersNonDefaultGPU` hint, so a menu launch renders on the display GPU like + a terminal launch; the hint made hybrid laptops on Wayland render through zink on the other GPU with + an empty 3D view. Runtime backend detection elsewhere uses Orca's `is_running_on_wayland()` / `is_running_on_x11()` (see `references/platforms.md`), not these environment variables. diff --git a/src/OrcaSlicer.cpp b/src/OrcaSlicer.cpp index 2bf5cb4825..24779c18c7 100644 --- a/src/OrcaSlicer.cpp +++ b/src/OrcaSlicer.cpp @@ -1146,6 +1146,38 @@ static bool is_hypervisor_guest() } #endif +#if defined(__WXGTK__) && defined(__linux__) +// Moves rendering to the discrete GPU of a dual-GPU system when the user asked for it in +// Preferences. The PRIME variables have to be in the environment before GTK opens the display, +// long before GUI_App loads the configuration, so the preference is read straight from the +// saved editor configuration (the G-code viewer follows the editor's choice), in the data dir +// GUI_App::init_app_config will pick: --datadir, else default_data_dir(). +static void apply_discrete_gpu_preference() +{ + const std::string dir = data_dir().empty() ? default_data_dir() : data_dir(); + if (!AppConfig::peek_bool(dir, "prefer_discrete_gpu")) + return; + // A DRI_PRIME already in the environment wins over the preference, whatever its value: one + // start with DRI_PRIME=0 gets back to Preferences when the discrete GPU cannot open the window. + if (::getenv("DRI_PRIME") != nullptr) { + BOOST_LOG_TRIVIAL(info) << "Discrete GPU preference ignored: DRI_PRIME is already set."; + return; + } + BOOST_LOG_TRIVIAL(info) << "Rendering on the discrete GPU by preference; exporting the PRIME variables."; + // DRI_PRIME=1 selects the non-default GPU for the Mesa drivers (AMD, Intel, nouveau). + ::setenv("DRI_PRIME", "1", /* replace */ false); + // The NVIDIA proprietary driver has its own render-offload variables. Only when its kernel + // module is loaded, so systems without NVIDIA are untouched. This is the set switcheroo-control + // hands a launcher for the NVIDIA GPU; the Vulkan layer variable keeps Vulkan consumers such as + // zink on the same GPU as GL. + if (::access("/proc/driver/nvidia/version", F_OK) == 0) { + ::setenv("__NV_PRIME_RENDER_OFFLOAD", "1", /* replace */ false); + ::setenv("__GLX_VENDOR_LIBRARY_NAME", "nvidia", /* replace */ false); + ::setenv("__VK_LAYER_NV_optimus", "NVIDIA_only", /* replace */ false); + } +} +#endif + int CLI::run(int argc, char **argv) { // Mark the main thread for the debugger and for runtime checks. @@ -1167,12 +1199,13 @@ int CLI::run(int argc, char **argv) // // GDK_BACKEND=x11 (OPT-IN) X11/XWayland mode. Applies the // v2.3.2 workarounds that the X11 - // path benefits from: DRI_PRIME for - // AMD/nouveau PRIME, NVIDIA PRIME - // render offload (when nvidia.ko is - // loaded), XInitThreads. + // path benefits from (XInitThreads). // Multi monitor is compromised // + // Which GPU renders on a dual-GPU system is independent of the backend: + // the "Render on the discrete GPU" preference exports the PRIME + // variables in apply_discrete_gpu_preference(), on both paths. + // // WEBKIT_DISABLE_COMPOSITING_MODE is deliberately NOT set on the X11 // opt-in path. The ~2020-era WebKit2GTK XWayland compositor bug it // worked around appears fixed in WebKit2GTK >= 2.42; leaving it @@ -1203,19 +1236,6 @@ int CLI::run(int argc, char **argv) // ===== X11 / XWayland (opted in via GDK_BACKEND=x11) ===== BOOST_LOG_TRIVIAL(info) << "GDK_BACKEND=x11 detected; applying v2.3.2 X11/XWayland workarounds."; - // On Linux dual-GPU systems, request the high-performance - // discrete GPU. DRI_PRIME=1 handles AMD and nouveau PRIME - // setups; harmless on single-GPU systems (Mesa ignores it). - ::setenv("DRI_PRIME", "1", /* replace */ false); - - // For NVIDIA proprietary driver PRIME render offload, set - // additional variables. Only set if the NVIDIA kernel module - // is loaded to avoid breaking systems without NVIDIA. - if (::access("/proc/driver/nvidia/version", F_OK) == 0) { - ::setenv("__NV_PRIME_RENDER_OFFLOAD", "1", /* replace */ false); - ::setenv("__GLX_VENDOR_LIBRARY_NAME", "nvidia", /* replace */ false); - } - // Tell Xlib we will be using threads, lest we crash when // GStreamer fires up. Safe to call here since the user has // explicitly opted into X11. @@ -1436,6 +1456,9 @@ int CLI::run(int argc, char **argv) return 1; } #endif // some linux / unix system + #if defined(__WXGTK__) && defined(__linux__) + apply_discrete_gpu_preference(); + #endif Slic3r::GUI::GUI_InitParams params; params.argc = argc; params.argv = argv; diff --git a/src/dev-utils/platform/unix/build_appimage.sh.in b/src/dev-utils/platform/unix/build_appimage.sh.in index f41025c45a..526877639a 100644 --- a/src/dev-utils/platform/unix/build_appimage.sh.in +++ b/src/dev-utils/platform/unix/build_appimage.sh.in @@ -25,8 +25,6 @@ Name=@SLIC3R_APP_KEY@ Exec=AppRun %F Icon=@SLIC3R_APP_KEY@ Type=Application -PrefersNonDefaultGPU=true -X-KDE-RunOnDiscreteGpu=true Categories=Utility; MimeType=model/stl;application/vnd.ms-3mfdocument;application/prs.wavefront-obj;application/x-amf; EOF diff --git a/src/dev-utils/platform/unix/com.orcaslicer.OrcaSlicer.desktop b/src/dev-utils/platform/unix/com.orcaslicer.OrcaSlicer.desktop index 1cf85b7144..188d2ead34 100644 --- a/src/dev-utils/platform/unix/com.orcaslicer.OrcaSlicer.desktop +++ b/src/dev-utils/platform/unix/com.orcaslicer.OrcaSlicer.desktop @@ -5,8 +5,6 @@ Icon=OrcaSlicer Exec=orca-slicer %U Terminal=false Type=Application -PrefersNonDefaultGPU=true -X-KDE-RunOnDiscreteGpu=true MimeType=model/stl;model/3mf;application/vnd.ms-3mfdocument;application/prs.wavefront-obj;application/x-amf;x-scheme-handler/orcaslicer;model/step; Categories=Graphics;3DGraphics;Engineering; Keywords=3D;Printing;Slicer;slice;3D;printer;convert;gcode;stl;obj;amf;SLA diff --git a/src/libslic3r/AppConfig.cpp b/src/libslic3r/AppConfig.cpp index 8f811a6e42..13fb716298 100644 --- a/src/libslic3r/AppConfig.cpp +++ b/src/libslic3r/AppConfig.cpp @@ -713,6 +713,9 @@ void AppConfig::set_defaults() #ifdef __linux__ if (get("window_buttons_on_left").empty()) set_bool("window_buttons_on_left", false); + + if (get("prefer_discrete_gpu").empty()) + set_bool("prefer_discrete_gpu", false); #endif if (get("use_printer_agents").empty()) @@ -1903,19 +1906,43 @@ std::string AppConfig::lock_path() return Slic3r::data_dir().empty() ? std::string() : config_path() + ".lock"; } -std::string AppConfig::config_path() +std::string AppConfig::config_path(const std::string &data_dir, EAppMode mode) { #ifdef USE_JSON_CONFIG - std::string path = (m_mode == EAppMode::Editor) ? - (boost::filesystem::path(Slic3r::data_dir()) / (SLIC3R_APP_KEY ".conf")).make_preferred().string() : - (boost::filesystem::path(Slic3r::data_dir()) / (GCODEVIEWER_APP_KEY ".conf")).make_preferred().string(); + const char *extension = ".conf"; #else - std::string path = (m_mode == EAppMode::Editor) ? - (boost::filesystem::path(Slic3r::data_dir()) / (SLIC3R_APP_KEY ".ini")).make_preferred().string() : - (boost::filesystem::path(Slic3r::data_dir()) / (GCODEVIEWER_APP_KEY ".ini")).make_preferred().string(); + const char *extension = ".ini"; #endif + const char *name = (mode == EAppMode::Editor) ? SLIC3R_APP_KEY : GCODEVIEWER_APP_KEY; + return (boost::filesystem::path(data_dir) / (std::string(name) + extension)).make_preferred().string(); +} - return path; +std::string AppConfig::config_path() +{ + return config_path(Slic3r::data_dir(), m_mode); +} + +bool AppConfig::peek_bool(const std::string &data_dir, const std::string &key) +{ + try { + boost::nowide::ifstream ifs(config_path(data_dir, EAppMode::Editor)); + if (!ifs.is_open()) + return false; + // Parses the leading JSON value only, so the checksum line Windows appends is ignored. + json j; + ifs >> j; + const auto app = j.find("app"); + if (app == j.end() || !app->is_object()) + return false; + const auto it = app->find(key); + if (it == app->end()) + return false; + if (it->is_boolean()) + return it->get(); + return it->is_string() && is_true(it->get()); + } catch (const std::exception &) { + return false; + } } std::string AppConfig::version_check_url() const diff --git a/src/libslic3r/AppConfig.hpp b/src/libslic3r/AppConfig.hpp index e6152dac74..938dbdecfe 100644 --- a/src/libslic3r/AppConfig.hpp +++ b/src/libslic3r/AppConfig.hpp @@ -160,10 +160,16 @@ public: { std::string value; this->get(section, key, value); return value; } std::string get(const std::string &key) const { std::string value; this->get("app", key, value); return value; } + // The spellings a boolean has in a saved configuration ("1" is the legacy form). + static bool is_true(const std::string &value) { return value == "true" || value == "1"; } bool get_bool(const std::string §ion, const std::string &key) const - { const std::string value = this->get(section, key); return value == "true" || value == "1"; } + { return is_true(this->get(section, key)); } bool get_bool(const std::string &key) const { return this->get_bool("app", key); } + // Reads one "app" boolean straight from the editor configuration saved in data_dir, for the + // few settings that must act before the configuration is loaded. False when the file is + // missing, unreadable or has no such key. + static bool peek_bool(const std::string &data_dir, const std::string &key); void set(const std::string §ion, const std::string &key, const std::string &value) { #ifndef NDEBUG @@ -351,6 +357,8 @@ public: // Get the default config path from Slic3r::data_dir(). std::string config_path(); + // The configuration file of the given mode inside data_dir. + static std::string config_path(const std::string &data_dir, EAppMode mode); // Lock file guarding config_path() against other running instances; empty without a data dir. std::string lock_path(); diff --git a/src/libslic3r/Utils.hpp b/src/libslic3r/Utils.hpp index 9d2f11b021..e3f7375f1a 100644 --- a/src/libslic3r/Utils.hpp +++ b/src/libslic3r/Utils.hpp @@ -205,6 +205,16 @@ const std::string& custom_gcodes_dir(); void set_data_dir(const std::string &path); // Return a full path to the GUI resource files. const std::string& data_dir(); +#ifndef _WIN32 +// 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); +#endif +#ifdef __linux__ +// The data directory a start without --datadir uses: a data_dir folder next to the executable +// when one exists, otherwise xdg_config_dir(SLIC3R_APP_KEY). Does not throw. +std::string default_data_dir(); +#endif // BBL: true: succeed create or dir exists; false: fail to create bool makedir(const std::string path); diff --git a/src/libslic3r/utils.cpp b/src/libslic3r/utils.cpp index e197473b7d..4ba6d9d90b 100644 --- a/src/libslic3r/utils.cpp +++ b/src/libslic3r/utils.cpp @@ -6,8 +6,10 @@ #include #include #include +#include #include #include +#include #include #include #include @@ -39,6 +41,11 @@ #include #include #include +#include +#ifndef _WIN32 +#include +#include +#endif #include #include #include @@ -367,6 +374,44 @@ const std::string& data_dir() return g_data_dir; } +#ifndef _WIN32 +std::string xdg_config_dir(const std::string &app_name) +{ + std::string base; + if (const char *xdg = ::getenv("XDG_CONFIG_HOME"); xdg != nullptr && *xdg != '\0') { + base = xdg; + } else { + // The same chain as wxGetHomeDir(): $HOME, then the passwd entry of $USER / $LOGNAME, + // then the one of the real uid. + const char *home = ::getenv("HOME"); + if (home == nullptr) { + const passwd *pw = nullptr; + for (const char *var : {"USER", "LOGNAME"}) + if (const char *user = ::getenv(var); user != nullptr && (pw = ::getpwnam(user)) != nullptr) + break; + if (pw == nullptr) + pw = ::getpwuid(::getuid()); + if (pw != nullptr) + home = pw->pw_dir; + } + base = std::string(home != nullptr ? home : "") + "/.config"; + } + return base + "/" + app_name; +} +#endif + +#ifdef __linux__ +std::string default_data_dir() +{ + // Runs before any error handling is in place, so a lookup that fails only means no portable dir. + boost::system::error_code ec; + const boost::filesystem::path portable = boost::dll::program_location(ec).parent_path() / "data_dir"; + if (!ec && boost::filesystem::exists(portable, ec)) + return portable.string(); + return xdg_config_dir(SLIC3R_APP_KEY); +} +#endif + std::string custom_shapes_dir() { return (boost::filesystem::path(g_data_dir) / "shapes").string(); diff --git a/src/slic3r/GUI/DesktopIntegrationDialog.cpp b/src/slic3r/GUI/DesktopIntegrationDialog.cpp index 0f940a8f16..c073ecc51c 100644 --- a/src/slic3r/GUI/DesktopIntegrationDialog.cpp +++ b/src/slic3r/GUI/DesktopIntegrationDialog.cpp @@ -355,8 +355,6 @@ void DesktopIntegrationDialog::perform_desktop_integration() "Exec=\"%3%\" %%F\n" "Terminal=false\n" "Type=Application\n" - "PrefersNonDefaultGPU=true\n" - "X-KDE-RunOnDiscreteGpu=true\n" "MimeType=model/stl;application/vnd.ms-3mfdocument;application/prs.wavefront-obj;application/x-amf;\n" "Categories=Graphics;3DGraphics;Engineering;\n" "Keywords=3D;Printing;Slicer;slice;3D;printer;convert;gcode;stl;obj;amf;SLA\n" @@ -426,8 +424,6 @@ void DesktopIntegrationDialog::perform_desktop_integration() "Exec=\"%3%\" --gcodeviewer %%F\n" "Terminal=false\n" "Type=Application\n" - "PrefersNonDefaultGPU=true\n" - "X-KDE-RunOnDiscreteGpu=true\n" "MimeType=text/x.gcode;\n" "Categories=Graphics;3DGraphics;\n" "Keywords=3D;Printing;Slicer;\n" diff --git a/src/slic3r/GUI/GUI_App.cpp b/src/slic3r/GUI/GUI_App.cpp index 39462af631..1163a520af 100644 --- a/src/slic3r/GUI/GUI_App.cpp +++ b/src/slic3r/GUI/GUI_App.cpp @@ -633,10 +633,7 @@ bool static check_old_linux_datadir(const wxString& app_name) { std::string new_path = Slic3r::data_dir(); - wxString dir; - if (! wxGetEnv(wxS("XDG_CONFIG_HOME"), &dir) || dir.empty() ) - dir = wxFileName::GetHomeDir() + wxS("/.config"); - std::string default_path = (dir + "/" + app_name).ToUTF8().data(); + std::string default_path = xdg_config_dir(app_name.ToUTF8().data()); if (new_path != default_path) { // This happens when the user specifies a custom --datadir. @@ -2744,6 +2741,14 @@ void GUI_App::init_app_config() // Mac : "~/Library/Application Support/Slic3r" if (data_dir().empty()) { +#ifdef __linux__ + // The same lookup the start-up code performs before GUI_App exists, so both read one file. + const boost::filesystem::path data_dir_path(default_data_dir()); + migrate_flatpak_legacy_datadir(data_dir_path); + set_data_dir(data_dir_path.string()); + if (!boost::filesystem::exists(data_dir_path)) + boost::filesystem::create_directory(data_dir_path); +#else // Orca: check if data_dir folder exists in application folder use it if it exists // Note:wxStandardPaths::Get().GetExecutablePath() return following paths // Unix: /usr/local/bin/exename @@ -2760,26 +2765,15 @@ void GUI_App::init_app_config() set_data_dir(app_data_dir_path.string()); } else{ - boost::filesystem::path data_dir_path; - #ifndef __linux__ - std::string data_dir = wxStandardPaths::Get().GetUserDataDir().ToUTF8().data(); - //BBS create folder if not exists - data_dir_path = boost::filesystem::path(data_dir); - set_data_dir(data_dir); - #else - // Since version 2.3, config dir on Linux is in ${XDG_CONFIG_HOME}. - // https://github.com/prusa3d/PrusaSlicer/issues/2911 - wxString dir; - if (! wxGetEnv(wxS("XDG_CONFIG_HOME"), &dir) || dir.empty() ) - dir = wxFileName::GetHomeDir() + wxS("/.config"); - data_dir_path = boost::filesystem::path((dir + "/" + GetAppName()).ToUTF8().data()); - migrate_flatpak_legacy_datadir(data_dir_path); - set_data_dir(data_dir_path.string()); - #endif + std::string data_dir = wxStandardPaths::Get().GetUserDataDir().ToUTF8().data(); + //BBS create folder if not exists + boost::filesystem::path data_dir_path(data_dir); + set_data_dir(data_dir); if (!boost::filesystem::exists(data_dir_path)){ boost::filesystem::create_directory(data_dir_path); } } +#endif // Change current dirtory of application diff --git a/src/slic3r/GUI/Preferences.cpp b/src/slic3r/GUI/Preferences.cpp index 567a9c218f..e3137ff131 100644 --- a/src/slic3r/GUI/Preferences.cpp +++ b/src/slic3r/GUI/Preferences.cpp @@ -1714,6 +1714,16 @@ void PreferencesDialog::create_items() #ifdef __linux__ auto item_window_button_pos = create_item_checkbox(_L("Use window buttons on left side"), "", "window_buttons_on_left", _L("(Requires restart)")); g_sizer->Add(item_window_button_pos); + + // TRN "GPU" is the graphics card; the option only matters on computers that have two of them. + auto item_discrete_gpu = create_item_checkbox(_L("Render on the discrete GPU"), + // TRN DRI_PRIME is an environment variable name and must stay as it is. + _L("On a computer with two GPUs, run OrcaSlicer on the discrete GPU instead of the one that drives the display. " + "Some Wayland systems cannot show frames rendered on the other GPU, or fail to open the window at all. " + "Turn this off if the 3D view stays empty; if OrcaSlicer no longer starts, start it once with DRI_PRIME=0 " + "in the environment to get back here."), + "prefer_discrete_gpu", _L("(Requires restart)")); + g_sizer->Add(item_discrete_gpu); #endif //auto item_hints = create_item_checkbox(_L("Show \"Daily Tips\" after start"), page, _L("If enabled, useful hints are displayed at startup."), "show_daily_tips"); diff --git a/tests/libslic3r/test_appconfig.cpp b/tests/libslic3r/test_appconfig.cpp index d4149c4fce..fae3c4d3cb 100644 --- a/tests/libslic3r/test_appconfig.cpp +++ b/tests/libslic3r/test_appconfig.cpp @@ -5,6 +5,12 @@ #include "libslic3r/AppConfig.hpp" #include "libslic3r/Thread.hpp" #include "../slic3rutils/plugin_test_utils.hpp" +#include "test_utils.hpp" + +#include + +#include +#include using namespace Slic3r; @@ -220,3 +226,39 @@ TEST_CASE("AppConfig Speed Dial recent count defaults, clamps and parses", "[App REQUIRE(config.get_speed_dial_recent_count() == SPEED_DIAL_RECENT_COUNT_DEFAULT); } } + +TEST_CASE("peek_bool reads an app boolean from a saved configuration without loading it", "[AppConfig]") +{ + ScopedDataDir data_dir("peek-bool"); + save_main_thread_id(); + AppConfig config; + config.set_bool("prefer_discrete_gpu", true); + config.save(); + const std::string dir = data_dir.dir.string(); + + CHECK(AppConfig::peek_bool(dir, "prefer_discrete_gpu")); + CHECK_FALSE(AppConfig::peek_bool(dir, "no_such_key")); + + config.set_bool("prefer_discrete_gpu", false); + config.save(); + CHECK_FALSE(AppConfig::peek_bool(dir, "prefer_discrete_gpu")); +} + +TEST_CASE("peek_bool accepts the legacy spellings and tolerates a bad or missing file", "[AppConfig]") +{ + ScopedTemporaryDir tmp("orca-peek-bool"); + // Each case gets its own data dir holding an editor configuration with the given text. + const auto write = [&tmp](const std::string &name, const std::string &text) { + const std::string dir = (tmp.path() / name).string(); + boost::filesystem::create_directory(dir); + std::ofstream(AppConfig::config_path(dir, AppConfig::EAppMode::Editor)) << text; + return dir; + }; + + CHECK(AppConfig::peek_bool(write("one", R"({"app":{"k":"1"}})"), "k")); + CHECK(AppConfig::peek_bool(write("true", R"({"app":{"k":"true"}})"), "k")); + CHECK_FALSE(AppConfig::peek_bool(write("zero", R"({"app":{"k":"0"}})"), "k")); + CHECK_FALSE(AppConfig::peek_bool(write("noapp", R"({"k":true})"), "k")); + CHECK_FALSE(AppConfig::peek_bool(write("broken", "{not json"), "k")); + CHECK_FALSE(AppConfig::peek_bool((tmp.path() / "missing").string(), "k")); +} diff --git a/tests/libslic3r/test_utils.cpp b/tests/libslic3r/test_utils.cpp index d87b8e686e..3b3f10df00 100644 --- a/tests/libslic3r/test_utils.cpp +++ b/tests/libslic3r/test_utils.cpp @@ -10,6 +10,7 @@ #include #include #include "libslic3r/Utils.hpp" +#include "libslic3r_version.h" #include "test_utils.hpp" @@ -567,3 +568,25 @@ TEST_CASE("extended_path turns a Windows path into its long-path form", "[Utils] } #endif } + +#ifndef _WIN32 +TEST_CASE("xdg_config_dir follows XDG_CONFIG_HOME and falls back to ~/.config", "[utils]") { + { + ScopedEnvVar xdg("XDG_CONFIG_HOME", "/tmp/xdg-test"); + CHECK(Slic3r::xdg_config_dir("OrcaSlicer") == "/tmp/xdg-test/OrcaSlicer"); + } + { + ScopedEnvVar xdg("XDG_CONFIG_HOME", ""); + ScopedEnvVar home("HOME", "/tmp/home-test"); + CHECK(Slic3r::xdg_config_dir("OrcaSlicer") == "/tmp/home-test/.config/OrcaSlicer"); + } +} +#endif + +#ifdef __linux__ +TEST_CASE("default_data_dir is the XDG config dir without a portable data_dir folder", "[utils]") { + ScopedEnvVar xdg("XDG_CONFIG_HOME", "/tmp/xdg-test"); + // A data_dir folder next to the test binary would make it a portable install; none is expected. + CHECK(Slic3r::default_data_dir() == "/tmp/xdg-test/" SLIC3R_APP_KEY); +} +#endif