From ae77a6e579252a894c82cccea98f390d1b17ea88 Mon Sep 17 00:00:00 2001 From: Ari Vuollet Date: Sun, 28 Jul 2024 17:49:12 +0300 Subject: [PATCH] Fallback to X11 message box implementation when SDL fails --- Source/Engine/Platform/Linux/LinuxPlatform.cpp | 9 +++++++-- Source/Engine/Platform/MessageBox.h | 5 +++++ Source/Engine/Platform/SDL/SDLPlatform.cpp | 10 +++++++++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/Source/Engine/Platform/Linux/LinuxPlatform.cpp b/Source/Engine/Platform/Linux/LinuxPlatform.cpp index ccf5de3ff..0ba8c02f2 100644 --- a/Source/Engine/Platform/Linux/LinuxPlatform.cpp +++ b/Source/Engine/Platform/Linux/LinuxPlatform.cpp @@ -370,8 +370,6 @@ static int X11_MessageBoxInitPositions(MessageBoxData* data) return 0; } -#if !PLATFORM_SDL - // Create and set up X11 dialog box window static int X11_MessageBoxCreateWindow(MessageBoxData* data) { @@ -659,7 +657,11 @@ static int X11_MessageBoxLoop(MessageBoxData* data) return 0; } +#if !PLATFORM_SDL DialogResult MessageBox::Show(Window* parent, const StringView& text, const StringView& caption, MessageBoxButtons buttons, MessageBoxIcon icon) +#else +DialogResult MessageBox::ShowFallback(Window* parent, const StringView& text, const StringView& caption, MessageBoxButtons buttons, MessageBoxIcon icon) +#endif { if (CommandLine::Options.Headless.IsTrue()) return DialogResult::None; @@ -846,6 +848,8 @@ DialogResult MessageBox::Show(Window* parent, const StringView& text, const Stri return data.resultButtonIndex == -1 ? DialogResult::None : data.buttons[data.resultButtonIndex].result; } +#if !PLATFORM_SDL + int X11ErrorHandler(X11::Display* display, X11::XErrorEvent* event) { if (event->error_code == 5) @@ -855,6 +859,7 @@ int X11ErrorHandler(X11::Display* display, X11::XErrorEvent* event) LOG(Error, "X11 Error: {0}", String(buffer)); return 0; } + #endif int32 CalculateDpi() diff --git a/Source/Engine/Platform/MessageBox.h b/Source/Engine/Platform/MessageBox.h index 16ae161cb..80c8e7938 100644 --- a/Source/Engine/Platform/MessageBox.h +++ b/Source/Engine/Platform/MessageBox.h @@ -237,4 +237,9 @@ DECLARE_SCRIPTING_TYPE_NO_SPAWN(MessageBox); /// One of the MessageBoxIcon values that specifies which icon to display in the message box. /// The message box dialog result. API_FUNCTION() static DialogResult Show(Window* parent, const StringView& text, const StringView& caption, MessageBoxButtons buttons, MessageBoxIcon icon); + +private: +#if PLATFORM_SDL && PLATFORM_LINUX + static DialogResult ShowFallback(Window* parent, const StringView& text, const StringView& caption, MessageBoxButtons buttons, MessageBoxIcon icon); +#endif }; diff --git a/Source/Engine/Platform/SDL/SDLPlatform.cpp b/Source/Engine/Platform/SDL/SDLPlatform.cpp index 30d82962a..40dea2d07 100644 --- a/Source/Engine/Platform/SDL/SDLPlatform.cpp +++ b/Source/Engine/Platform/SDL/SDLPlatform.cpp @@ -475,7 +475,15 @@ DialogResult MessageBox::Show(Window* parent, const StringView& text, const Stri int result = -1; if (SDL_ShowMessageBox(&data, &result) != 0) { - LOG(Error, "Failed to show message box: {0}", String(SDL_GetError())); +#if PLATFORM_LINUX + // Fallback to native messagebox implementation in case some system fonts are missing + if (SDLPlatform::UsesX11()) + { + LOG(Warning, "Failed to show SDL message box: {0}", String(SDL_GetError())); + return ShowFallback(parent, text, caption, buttons, icon); + } +#endif + LOG(Error, "Failed to show SDL message box: {0}", String(SDL_GetError())); return DialogResult::Abort; } if (result < 0)