From 74ec3a92dfe6f4a78bb1cc2b5c102210c58820e7 Mon Sep 17 00:00:00 2001 From: arch1t3cht Date: Sat, 25 Feb 2023 12:31:27 +0100 Subject: [PATCH] lua: Interact with clipboard on main gui thread Fixes arch1t3cht/Aegisub#22 . --- src/auto4_lua.cpp | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/src/auto4_lua.cpp b/src/auto4_lua.cpp index c34c87e226..69a308efe3 100644 --- a/src/auto4_lua.cpp +++ b/src/auto4_lua.cpp @@ -124,7 +124,8 @@ namespace { const char *clipboard_get() { - std::string data = GetClipboard(); + std::string data; + agi::dispatch::Main().Sync([&] { data = GetClipboard(); }); if (data.empty()) return nullptr; return strndup(data); @@ -134,18 +135,14 @@ namespace { { bool succeeded = false; -#if wxUSE_OLE - // OLE needs to be initialized on each thread that wants to write to - // the clipboard, which wx does not handle automatically - wxClipboard cb; -#else - wxClipboard &cb = *wxTheClipboard; -#endif - if (cb.Open()) { - succeeded = cb.SetData(new wxTextDataObject(wxString::FromUTF8(str))); - cb.Close(); - cb.Flush(); - } + agi::dispatch::Main().Sync([&] { + wxClipboard &cb = *wxTheClipboard; + if (cb.Open()) { + succeeded = cb.SetData(new wxTextDataObject(wxString::FromUTF8(str))); + cb.Close(); + cb.Flush(); + } + }); return succeeded; }