From 480791a05662a05c7b34be95e27be51705847ed5 Mon Sep 17 00:00:00 2001 From: Joel Linn Date: Tue, 29 Jun 2021 23:32:22 +0200 Subject: [PATCH] [Base] Implement message boxes on Linux --- src/xenia/base/main_posix.cc | 5 +++- src/xenia/base/system.h | 2 +- src/xenia/base/system_linux.cc | 43 +++++++++++++++++++++++++++++++--- 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/src/xenia/base/main_posix.cc b/src/xenia/base/main_posix.cc index 8d14f9219..d3f0ef403 100644 --- a/src/xenia/base/main_posix.cc +++ b/src/xenia/base/main_posix.cc @@ -7,6 +7,9 @@ ****************************************************************************** */ +#include +#include + #include "xenia/base/cvar.h" #include "xenia/base/main.h" @@ -16,7 +19,7 @@ namespace xe { -bool has_console_attached() { return true; } +bool has_console_attached() { return isatty(fileno(stdin)) == 1; } void AttachConsole() {} diff --git a/src/xenia/base/system.h b/src/xenia/base/system.h index e69a95eb5..4bd0eac2b 100644 --- a/src/xenia/base/system.h +++ b/src/xenia/base/system.h @@ -11,7 +11,7 @@ #define XENIA_BASE_SYSTEM_H_ #include -#include +#include #include "xenia/base/string.h" diff --git a/src/xenia/base/system_linux.cc b/src/xenia/base/system_linux.cc index 7be020cda..920c9ab9e 100644 --- a/src/xenia/base/system_linux.cc +++ b/src/xenia/base/system_linux.cc @@ -7,15 +7,20 @@ ****************************************************************************** */ +#include +#include #include -#include +#include #include "xenia/base/assert.h" -#include "xenia/base/platform_linux.h" +#include "xenia/base/logging.h" #include "xenia/base/string.h" #include "xenia/base/system.h" +// Use headers in third party to not depend on system sdl headers for building +#include "third_party/SDL2/include/SDL.h" + namespace xe { void LaunchWebBrowser(const std::string_view url) { @@ -27,7 +32,39 @@ void LaunchWebBrowser(const std::string_view url) { void LaunchFileExplorer(const std::filesystem::path& path) { assert_always(); } void ShowSimpleMessageBox(SimpleMessageBoxType type, std::string_view message) { - assert_always(); + void* libsdl2 = dlopen("libSDL2.so", RTLD_LAZY | RTLD_LOCAL); + assert_not_null(libsdl2); + if (libsdl2) { + auto* pSDL_ShowSimpleMessageBox = + reinterpret_cast( + dlsym(libsdl2, "SDL_ShowSimpleMessageBox")); + assert_not_null(pSDL_ShowSimpleMessageBox); + if (pSDL_ShowSimpleMessageBox) { + Uint32 flags; + const char* title; + char* message_copy = reinterpret_cast(alloca(message.size() + 1)); + std::memcpy(message_copy, message.data(), message.size()); + message_copy[message.size()] = '\0'; + + switch (type) { + default: + case SimpleMessageBoxType::Help: + title = "Xenia Help"; + flags = SDL_MESSAGEBOX_INFORMATION; + break; + case SimpleMessageBoxType::Warning: + title = "Xenia Warning"; + flags = SDL_MESSAGEBOX_WARNING; + break; + case SimpleMessageBoxType::Error: + title = "Xenia Error"; + flags = SDL_MESSAGEBOX_ERROR; + break; + } + pSDL_ShowSimpleMessageBox(flags, title, message_copy, NULL); + } + dlclose(libsdl2); + } } } // namespace xe