From 9ca96567a5b292914023ec9bc6d429076869eed0 Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Mon, 3 Jul 2023 11:05:58 -0700 Subject: [PATCH] move conversion logic to MiscUtils but keep minimal wrappers in SDL module so we don't leak memory --- docs/dev/Lua API.rst | 9 ++++----- library/LuaApi.cpp | 8 ++------ library/MiscUtils.cpp | 10 ++++++++++ library/include/MiscUtils.h | 4 ++++ library/include/modules/DFSDL.h | 11 ++++------- library/lua/gui/widgets.lua | 6 +++--- library/modules/DFSDL.cpp | 22 ++-------------------- 7 files changed, 29 insertions(+), 41 deletions(-) diff --git a/docs/dev/Lua API.rst b/docs/dev/Lua API.rst index ffd43aea1..830ba6b09 100644 --- a/docs/dev/Lua API.rst +++ b/docs/dev/Lua API.rst @@ -2815,14 +2815,13 @@ and are only documented here for completeness: Returns 0 if the address is not found. Requires a heap snapshot. -* ``dfhack.internal.getClipboardText()`` +* ``dfhack.internal.getClipboardTextCp437()`` - Gets the system clipboard text (converted to CP437 encoding). + Gets the system clipboard text (and converts text to CP437 encoding). -* ``dfhack.internal.setClipboardText(text)`` +* ``dfhack.internal.setClipboardTextCp437(text)`` - Converts the given text from CP437 to UTF-8 and sets the system clipboard - text. + Sets the system clipboard text from a CP437 string. .. _lua-core-context: diff --git a/library/LuaApi.cpp b/library/LuaApi.cpp index 13eb01c65..c04a8ef0f 100644 --- a/library/LuaApi.cpp +++ b/library/LuaApi.cpp @@ -47,7 +47,6 @@ distribution. #include "modules/Burrows.h" #include "modules/Constructions.h" #include "modules/Designations.h" -#include "modules/DFSDL.h" #include "modules/Filesystem.h" #include "modules/Gui.h" #include "modules/Items.h" @@ -3000,9 +2999,6 @@ static int msize_address(uintptr_t ptr) return -1; } -static std::string getClipboardText() { return DFSDL::DFSDL_GetClipboardTextCp437(); } -static void setClipboardText(std::string s) { DFSDL::DFSDL_SetClipboardTextCp437(s); } - static const LuaWrapper::FunctionReg dfhack_internal_module[] = { WRAP(getImageBase), WRAP(getRebaseDelta), @@ -3017,8 +3013,8 @@ static const LuaWrapper::FunctionReg dfhack_internal_module[] = { WRAPN(getAddressSizeInHeap, get_address_size_in_heap), WRAPN(getRootAddressOfHeapObject, get_root_address_of_heap_object), WRAPN(msizeAddress, msize_address), - WRAP(getClipboardText), - WRAP(setClipboardText), + WRAP(getClipboardTextCp437), + WRAP(setClipboardTextCp437), { NULL, NULL } }; diff --git a/library/MiscUtils.cpp b/library/MiscUtils.cpp index b959e756e..6b50da347 100644 --- a/library/MiscUtils.cpp +++ b/library/MiscUtils.cpp @@ -27,6 +27,8 @@ distribution. #include "MiscUtils.h" #include "ColorText.h" +#include "modules/DFSDL.h" + #ifndef LINUX_BUILD // We don't want min and max macros #define NOMINMAX @@ -470,3 +472,11 @@ DFHACK_EXPORT std::string DF2CONSOLE(DFHack::color_ostream &out, const std::stri { return out.is_console() ? DF2CONSOLE(in) : in; } + +DFHACK_EXPORT std::string getClipboardTextCp437() { + return UTF2DF(DFHack::DFSDL::DFSDL_GetClipboardText()); +} + +DFHACK_EXPORT bool setClipboardTextCp437(std::string text) { + return DFHack::DFSDL::DFSDL_SetClipboardText(DF2UTF(text).c_str()); +} diff --git a/library/include/MiscUtils.h b/library/include/MiscUtils.h index d14bdb6e9..099e86581 100644 --- a/library/include/MiscUtils.h +++ b/library/include/MiscUtils.h @@ -496,3 +496,7 @@ DFHACK_EXPORT std::string UTF2DF(const std::string &in); DFHACK_EXPORT std::string DF2UTF(const std::string &in); DFHACK_EXPORT std::string DF2CONSOLE(const std::string &in); DFHACK_EXPORT std::string DF2CONSOLE(DFHack::color_ostream &out, const std::string &in); + +// System clipboard -- submitted and returned text must be in CP437 +DFHACK_EXPORT std::string getClipboardTextCp437(); +DFHACK_EXPORT bool setClipboardTextCp437(std::string text); diff --git a/library/include/modules/DFSDL.h b/library/include/modules/DFSDL.h index 88dd08f05..6cee58342 100644 --- a/library/include/modules/DFSDL.h +++ b/library/include/modules/DFSDL.h @@ -48,13 +48,10 @@ DFHACK_EXPORT void DFSDL_FreeSurface(SDL_Surface *surface); // DFHACK_EXPORT int DFSDL_SemPost(SDL_sem *sem); DFHACK_EXPORT int DFSDL_PushEvent(SDL_Event *event); -// System clipboard -DFHACK_EXPORT std::string DFSDL_GetClipboardTextUtf8(); -DFHACK_EXPORT std::string DFSDL_GetClipboardTextCp437(); -DFHACK_EXPORT bool DFSDL_SetClipboardTextUtf8(const char *text); -DFHACK_EXPORT bool DFSDL_SetClipboardTextUtf8(const std::string &text); -DFHACK_EXPORT bool DFSDL_SetClipboardTextCp437(const char *text); -DFHACK_EXPORT bool DFSDL_SetClipboardTextCp437(const std::string &text); +// submitted and returned text is UTF-8 +// see wrapper functions in MiscUtils.h for cp-437 variants +DFHACK_EXPORT std::string DFSDL_GetClipboardText(); +DFHACK_EXPORT bool DFSDL_SetClipboardText(const char *text); } diff --git a/library/lua/gui/widgets.lua b/library/lua/gui/widgets.lua index 76b5c3b68..980d0104f 100644 --- a/library/lua/gui/widgets.lua +++ b/library/lua/gui/widgets.lua @@ -770,14 +770,14 @@ function EditField:onInput(keys) self:setCursor() return true elseif keys.CUSTOM_CTRL_C then - dfhack.internal.setClipboardText(self.text) + dfhack.internal.setClipboardTextCp437(self.text) return true elseif keys.CUSTOM_CTRL_X then - dfhack.internal.setClipboardText(self.text) + dfhack.internal.setClipboardTextCp437(self.text) self:setText('') return true elseif keys.CUSTOM_CTRL_V then - self:insert(dfhack.internal.getClipboardText()) + self:insert(dfhack.internal.getClipboardTextCp437()) return true end diff --git a/library/modules/DFSDL.cpp b/library/modules/DFSDL.cpp index 4ed4bc5e8..280fb04d6 100644 --- a/library/modules/DFSDL.cpp +++ b/library/modules/DFSDL.cpp @@ -3,7 +3,6 @@ #include "modules/DFSDL.h" #include "Debug.h" -#include "MiscUtils.h" #include "PluginManager.h" #include @@ -136,7 +135,7 @@ int DFSDL::DFSDL_PushEvent(SDL_Event *event) { return g_SDL_PushEvent(event); } -std::string DFSDL::DFSDL_GetClipboardTextUtf8() { +std::string DFSDL::DFSDL_GetClipboardText() { if (g_SDL_HasClipboardText() != SDL_TRUE) return ""; char *text = g_SDL_GetClipboardText(); @@ -145,23 +144,6 @@ std::string DFSDL::DFSDL_GetClipboardTextUtf8() { return ret; } -std::string DFSDL::DFSDL_GetClipboardTextCp437() { - std::string utf8text = DFSDL_GetClipboardTextUtf8(); - return UTF2DF(utf8text); -} - -bool DFSDL::DFSDL_SetClipboardTextUtf8(const char *text) { +bool DFSDL::DFSDL_SetClipboardText(const char *text) { return g_SDL_SetClipboardText(text) == 0; } - -bool DFSDL::DFSDL_SetClipboardTextUtf8(const std::string &text) { - return DFSDL_SetClipboardTextUtf8(text.c_str()); -} - -bool DFSDL::DFSDL_SetClipboardTextCp437(const char *text) { - return DFSDL_SetClipboardTextUtf8(DF2UTF(text)); -} - -bool DFSDL::DFSDL_SetClipboardTextCp437(const std::string &text) { - return DFSDL_SetClipboardTextUtf8(DF2UTF(text)); -}