From c151c9271f1f36a05c807cea4f48aef554724c1d Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 24 Oct 2023 00:07:39 +0200 Subject: [PATCH 1/4] Make GTK wxSystemSettings initialization more robust Add more checks which should not be really needed, but don't do any harm neither and ensure that we don't use invalid pointers. --- src/gtk/settings.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/gtk/settings.cpp b/src/gtk/settings.cpp index 1202e06fb0..d9245f9df5 100644 --- a/src/gtk/settings.cpp +++ b/src/gtk/settings.cpp @@ -186,16 +186,24 @@ static void notify_gtk_font_name(GObject*, GParamSpec*, void*) static bool UpdatePreferDark(GVariant* value) { + GtkSettings* const settings = gtk_settings_get_default(); + // This shouldn't happen, but don't bother doing anything else if it does. + if (!settings) + return false; + // 0: No preference, 1: Prefer dark appearance, 2: Prefer light appearance gboolean preferDark = g_variant_get_uint32(value) == 1; - GtkSettings* settings = gtk_settings_get_default(); - char* themeName; - gboolean preferDarkPrev; + char* themeName = nullptr; + gboolean preferDarkPrev = false; g_object_get(settings, "gtk-theme-name", &themeName, "gtk-application-prefer-dark-theme", &preferDarkPrev, nullptr); + // This is not supposed to happen neither, but don't crash if it does. + if (!themeName) + return false; + // We don't need to enable prefer-dark if the theme is already dark if (strstr(themeName, "-dark") || strstr(themeName, "-Dark")) preferDark = false; From ea393d76adf84394edb1eb51e2a06b3af95c8dfc Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 24 Oct 2023 00:30:30 +0200 Subject: [PATCH 2/4] Allow running GUI test in console-like app mode This allows to test that GUI applications not initializing the GUI can at least start correctly and work as console ones (at least under Unix). --- tests/test.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/tests/test.cpp b/tests/test.cpp index 5e004fb04f..e07c7118b4 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -340,6 +340,9 @@ public: virtual int OnRun() override { + if ( !IsGUIEnabled() ) + return 0; + if ( TestAppBase::OnRun() != 0 ) m_exitcode = EXIT_FAILURE; @@ -352,6 +355,35 @@ public: } #endif // wxUSE_GUI/!wxUSE_GUI + // Hack to test that GUI applications not using GUI at all work: this was + // broken in the past (see #23981), so now the test suite checks that + // running this test with WX_TEST_DISABLE_GUI works. +#if wxUSE_GUI + bool IsGUIEnabled() const + { + return !wxGetEnv(wxASCII_STR("WX_TEST_DISABLE_GUI"), nullptr); + } + + virtual bool Initialize(int& argc, wxChar **argv) override + { + return IsGUIEnabled() ? wxApp::Initialize(argc, argv) + : wxAppConsole::Initialize(argc, argv); + } + + virtual bool OnInitGui() override + { + return !IsGUIEnabled() || wxApp::OnInitGui(); + } + + virtual void CleanUp() override + { + if ( IsGUIEnabled() ) + wxApp::CleanUp(); + else + wxAppConsole::CleanUp(); + } +#endif // wxUSE_GUI + private: int RunTests(); @@ -592,6 +624,14 @@ TestApp::TestApp() // bool TestApp::OnInit() { +#if wxUSE_GUI + if ( !IsGUIEnabled() ) + { + wxFputs(wxASCII_STR("Not running tests because GUI is disabled.\n"), stderr); + return true; + } +#endif // wxUSE_GUI + // Hack: don't call TestAppBase::OnInit() to let CATCH handle command line. // Output some important information about the test environment. @@ -688,6 +728,9 @@ int TestApp::RunTests() int TestApp::OnExit() { #if wxUSE_GUI + if ( !IsGUIEnabled() ) + return wxAppConsole::OnExit(); + delete GetTopWindow(); #endif // wxUSE_GUI From 5af28133a0b787ecf99d9c25e973e0fa43b1c18d Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 24 Oct 2023 01:09:48 +0200 Subject: [PATCH 3/4] Fix crash on shutdown of wxX11 GUI applications not using GUI This should have been done back in f18b415ee3 (Don't crash on startup of console programs in monolithic wxX11 build., 2011-05-03) when a related crash on startup was fixed. --- src/x11/window.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/x11/window.cpp b/src/x11/window.cpp index 9b83f20900..14a8de5b9c 100644 --- a/src/x11/window.cpp +++ b/src/x11/window.cpp @@ -1767,6 +1767,6 @@ bool wxWinModule::OnInit() void wxWinModule::OnExit() { - Display *xdisplay = wxGlobalDisplay(); - XFreeGC( xdisplay, g_eraseGC ); + if (Display *xdisplay = wxGlobalDisplay()) + XFreeGC( xdisplay, g_eraseGC ); } From c07a1ec7183e3278e336441b3d95b9b91bc5c6a9 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 24 Oct 2023 00:35:17 +0200 Subject: [PATCH 4/4] Check running the GUI test without GUI in Unix CI builds This just checks that launching a GUI program and not initializing GUI in it works as expected and, notably, does not crash. --- .github/workflows/ci.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1133342049..a44b2b6fb4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -344,6 +344,11 @@ jobs: exit $rc fi + - name: Testing launching GUI test + working-directory: tests + run: | + WX_TEST_DISABLE_GUI=1 ./test_gui + - name: Testing GUI using Xvfb if: matrix.use_xvfb working-directory: tests