diff options
author | 2021-11-12 22:48:42 +1100 | |
---|---|---|
committer | 2021-11-12 22:48:42 +1100 | |
commit | 64af120bab29a76161b960ed2a3639c6a2b19afd (patch) | |
tree | dc06dfcac5e39a445b0aa69557a9376c2233886f /src/osd/windows/winmain.cpp | |
parent | fa9ece2c1810c812c082c684098be101b1962098 (diff) | |
parent | b8aa7a3d6535d1572932b4817abf16eb5d93643e (diff) |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'src/osd/windows/winmain.cpp')
-rw-r--r-- | src/osd/windows/winmain.cpp | 67 |
1 files changed, 64 insertions, 3 deletions
diff --git a/src/osd/windows/winmain.cpp b/src/osd/windows/winmain.cpp index 672fe0648f6..6211aafd720 100644 --- a/src/osd/windows/winmain.cpp +++ b/src/osd/windows/winmain.cpp @@ -25,6 +25,10 @@ #include <clocale> #include <cstdarg> #include <cstdio> +#include <mutex> +#include <optional> +#include <sstream> +#include <thread> // standard windows headers #include <windows.h> @@ -55,6 +59,28 @@ class winui_output_error : public osd_output { +private: + struct ui_state + { + ~ui_state() + { + std::lock_guard guard(mutex); + if (thread) + thread->join(); + } + + std::ostringstream buffer; + std::optional<std::thread> thread; + std::mutex mutex; + bool active; + }; + + static ui_state &get_state() + { + static ui_state state; + return state; + } + public: virtual void output_callback(osd_output_channel channel, const util::format_argument_pack<std::ostream> &args) override { @@ -64,12 +90,47 @@ public: if ((video_config.windowed == 0) && !osd_common_t::s_window_list.empty()) winwindow_toggle_full_screen(); - std::ostringstream buffer; - util::stream_format(buffer, args); - win_message_box_utf8(!osd_common_t::s_window_list.empty() ? std::static_pointer_cast<win_window_info>(osd_common_t::s_window_list.front())->platform_window() : nullptr, buffer.str().c_str(), emulator_info::get_appname(), MB_OK); + auto &state(get_state()); + std::lock_guard<std::mutex> guard(state.mutex); + util::stream_format(state.buffer, args); + if (!state.active) + { + if (state.thread) + { + state.thread->join(); + state.thread = std::nullopt; + } + auto const parent = !osd_common_t::s_window_list.empty() + ? std::static_pointer_cast<win_window_info>(osd_common_t::s_window_list.front())->platform_window() + : nullptr; + state.thread.emplace( + [parent] () + { + auto &state(get_state()); + std::string message; + while (true) + { + { + std::lock_guard<std::mutex> guard(state.mutex); + message = std::move(state.buffer).str(); + if (message.empty()) + { + state.active = false; + return; + } + state.buffer.str(std::string()); + } + // don't hold any locks lock while calling MessageBox + win_message_box_utf8(parent, message.c_str(), emulator_info::get_appname(), MB_OK); + } + }); + state.active = true; + } } else + { chain_output(channel, args); + } } }; |