From 175c249d29cc62675a0b5da85e14da295c77cea7 Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Sun, 2 Jul 2023 18:04:06 -0700 Subject: [PATCH 1/7] support copy/paste from system clipboard --- docs/changelog.txt | 1 + docs/dev/Lua API.rst | 14 +++++++++++ library/LuaApi.cpp | 6 +++++ library/include/modules/DFSDL.h | 8 +++++++ library/lua/gui/widgets.lua | 10 ++++++++ library/modules/DFSDL.cpp | 41 +++++++++++++++++++++++++++++++++ 6 files changed, 80 insertions(+) diff --git a/docs/changelog.txt b/docs/changelog.txt index d10407621..c30d313d5 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -38,6 +38,7 @@ changelog.txt uses a syntax similar to RST, with a few special sequences: ## Fixes ## Misc Improvements +- ``widgets.EditField``: DFHack edit fields now support cut/copy/paste with the system clipboard with Ctrl-X/Ctrl-C/Ctrl-V ## Documentation diff --git a/docs/dev/Lua API.rst b/docs/dev/Lua API.rst index d07cb045e..ffd43aea1 100644 --- a/docs/dev/Lua API.rst +++ b/docs/dev/Lua API.rst @@ -2815,6 +2815,14 @@ and are only documented here for completeness: Returns 0 if the address is not found. Requires a heap snapshot. +* ``dfhack.internal.getClipboardText()`` + + Gets the system clipboard text (converted to CP437 encoding). + +* ``dfhack.internal.setClipboardText(text)`` + + Converts the given text from CP437 to UTF-8 and sets the system clipboard + text. .. _lua-core-context: @@ -4688,6 +4696,12 @@ following keyboard hotkeys: - Ctrl-B/Ctrl-F: move the cursor one word back or forward. - Ctrl-A/Ctrl-E: move the cursor to the beginning/end of the text. +The widget also supports integration with the system clipboard: + +- Ctrl-C: copy current text to the system clipboard +- Ctrl-X: copy current text to the system clipboard and clear text in widget +- Ctrl-V: paste text from the system clipboard (text is converted to cp437) + The ``EditField`` class also provides the following functions: * ``editfield:setCursor([cursor_pos])`` diff --git a/library/LuaApi.cpp b/library/LuaApi.cpp index d2c32cf63..13eb01c65 100644 --- a/library/LuaApi.cpp +++ b/library/LuaApi.cpp @@ -47,6 +47,7 @@ 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" @@ -2999,6 +3000,9 @@ 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), @@ -3013,6 +3017,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), { NULL, NULL } }; diff --git a/library/include/modules/DFSDL.h b/library/include/modules/DFSDL.h index 626224d60..88dd08f05 100644 --- a/library/include/modules/DFSDL.h +++ b/library/include/modules/DFSDL.h @@ -48,6 +48,14 @@ 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); + } } diff --git a/library/lua/gui/widgets.lua b/library/lua/gui/widgets.lua index a58ac228c..8759f97bf 100644 --- a/library/lua/gui/widgets.lua +++ b/library/lua/gui/widgets.lua @@ -772,6 +772,16 @@ function EditField:onInput(keys) elseif keys.CUSTOM_CTRL_E then -- end self:setCursor() return true + elseif keys.CUSTOM_CTRL_C then + dfhack.internal.setClipboardText(self.text) + return true + elseif keys.CUSTOM_CTRL_X then + dfhack.internal.setClipboardText(self.text) + self:setText('') + return true + elseif keys.CUSTOM_CTRL_V then + self:insert(dfhack.internal.getClipboardText()) + return true end -- if we're modal, then unconditionally eat all the input diff --git a/library/modules/DFSDL.cpp b/library/modules/DFSDL.cpp index 7aa7f36d5..4ed4bc5e8 100644 --- a/library/modules/DFSDL.cpp +++ b/library/modules/DFSDL.cpp @@ -3,8 +3,11 @@ #include "modules/DFSDL.h" #include "Debug.h" +#include "MiscUtils.h" #include "PluginManager.h" +#include + namespace DFHack { DBG_DECLARE(core, dfsdl, DebugCategory::LINFO); } @@ -35,6 +38,10 @@ void (*g_SDL_FreeSurface)(SDL_Surface *) = nullptr; // int (*g_SDL_SemWait)(DFSDL_sem *) = nullptr; // int (*g_SDL_SemPost)(DFSDL_sem *) = nullptr; int (*g_SDL_PushEvent)(SDL_Event *) = nullptr; +SDL_bool (*g_SDL_HasClipboardText)(); +int (*g_SDL_SetClipboardText)(const char *text); +char * (*g_SDL_GetClipboardText)(); +void (*g_SDL_free)(void *); bool DFSDL::init(color_ostream &out) { for (auto &lib_str : SDL_LIBS) { @@ -71,6 +78,10 @@ bool DFSDL::init(color_ostream &out) { // bind(g_sdl_handle, SDL_SemWait); // bind(g_sdl_handle, SDL_SemPost); bind(g_sdl_handle, SDL_PushEvent); + bind(g_sdl_handle, SDL_HasClipboardText); + bind(g_sdl_handle, SDL_SetClipboardText); + bind(g_sdl_handle, SDL_GetClipboardText); + bind(g_sdl_handle, SDL_free); #undef bind DEBUG(dfsdl,out).print("sdl successfully loaded\n"); @@ -124,3 +135,33 @@ void DFSDL::DFSDL_FreeSurface(SDL_Surface *surface) { int DFSDL::DFSDL_PushEvent(SDL_Event *event) { return g_SDL_PushEvent(event); } + +std::string DFSDL::DFSDL_GetClipboardTextUtf8() { + if (g_SDL_HasClipboardText() != SDL_TRUE) + return ""; + char *text = g_SDL_GetClipboardText(); + std::string ret = text; + g_SDL_free(text); + return ret; +} + +std::string DFSDL::DFSDL_GetClipboardTextCp437() { + std::string utf8text = DFSDL_GetClipboardTextUtf8(); + return UTF2DF(utf8text); +} + +bool DFSDL::DFSDL_SetClipboardTextUtf8(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)); +} From 07e8edcdcaf4c58ac13cb359d433536a217b5232 Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Sun, 2 Jul 2023 19:27:07 -0700 Subject: [PATCH 2/7] ensure changing text fires the on_change event --- library/lua/gui/widgets.lua | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/library/lua/gui/widgets.lua b/library/lua/gui/widgets.lua index 8759f97bf..76b5c3b68 100644 --- a/library/lua/gui/widgets.lua +++ b/library/lua/gui/widgets.lua @@ -640,8 +640,12 @@ function EditField:setCursor(cursor) end function EditField:setText(text, cursor) + local old = self.text self.text = text self:setCursor(cursor) + if self.on_change and text ~= old then + self.on_change(self.text, old) + end end function EditField:postUpdateLayout() @@ -699,11 +703,7 @@ function EditField:onInput(keys) end if self.key and (keys.LEAVESCREEN or keys._MOUSE_R_DOWN) then - local old = self.text self:setText(self.saved_text) - if self.on_change and old ~= self.saved_text then - self.on_change(self.text, old) - end self:setFocus(false) return true end @@ -747,9 +747,6 @@ function EditField:onInput(keys) return self.modal end end - if self.on_change and self.text ~= old then - self.on_change(self.text, old) - end return true elseif keys.KEYBOARD_CURSOR_LEFT then self:setCursor(self.cursor - 1) From 9ca96567a5b292914023ec9bc6d429076869eed0 Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Mon, 3 Jul 2023 11:05:58 -0700 Subject: [PATCH 3/7] 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)); -} From ae545aa1d6a3577f6b4a2fbf485335544f4b23f4 Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Mon, 3 Jul 2023 11:18:14 -0700 Subject: [PATCH 4/7] add new linkage dependency on dfhack --- library/CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt index 5d2698bb0..40006a432 100644 --- a/library/CMakeLists.txt +++ b/library/CMakeLists.txt @@ -408,8 +408,8 @@ endif() target_link_libraries(dfhack protobuf-lite clsocket lua jsoncpp_static dfhack-version ${PROJECT_LIBS}) set_target_properties(dfhack PROPERTIES INTERFACE_LINK_LIBRARIES "") -target_link_libraries(dfhack-client protobuf-lite clsocket jsoncpp_static) -target_link_libraries(dfhack-run dfhack-client) +target_link_libraries(dfhack-client protobuf-lite clsocket jsoncpp_static dfhack) +target_link_libraries(dfhack-run dfhack-client dfhack) if(APPLE) add_custom_command(TARGET dfhack-run COMMAND ${dfhack_SOURCE_DIR}/package/darwin/fix-libs.sh WORKING_DIRECTORY ../ COMMENT "Fixing library dependencies...") From 0bbb72b44f59cadb4246081ef6ff9d4121fcbf22 Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Wed, 5 Jul 2023 11:46:33 -0700 Subject: [PATCH 5/7] Revert "add new linkage dependency on dfhack" This reverts commit ae545aa1d6a3577f6b4a2fbf485335544f4b23f4. --- library/CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt index 40006a432..5d2698bb0 100644 --- a/library/CMakeLists.txt +++ b/library/CMakeLists.txt @@ -408,8 +408,8 @@ endif() target_link_libraries(dfhack protobuf-lite clsocket lua jsoncpp_static dfhack-version ${PROJECT_LIBS}) set_target_properties(dfhack PROPERTIES INTERFACE_LINK_LIBRARIES "") -target_link_libraries(dfhack-client protobuf-lite clsocket jsoncpp_static dfhack) -target_link_libraries(dfhack-run dfhack-client dfhack) +target_link_libraries(dfhack-client protobuf-lite clsocket jsoncpp_static) +target_link_libraries(dfhack-run dfhack-client) if(APPLE) add_custom_command(TARGET dfhack-run COMMAND ${dfhack_SOURCE_DIR}/package/darwin/fix-libs.sh WORKING_DIRECTORY ../ COMMENT "Fixing library dependencies...") From 43ea9b94c3026ce05acddd297c31e4e9e720a826 Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Wed, 5 Jul 2023 12:08:18 -0700 Subject: [PATCH 6/7] move clipboard wrappers into DFSDL module (but not in the namespace) --- library/MiscUtils.cpp | 8 -------- library/include/MiscUtils.h | 4 ---- library/include/modules/DFSDL.h | 11 ++++++++--- library/modules/DFSDL.cpp | 28 +++++++++++++++++++++------- 4 files changed, 29 insertions(+), 22 deletions(-) diff --git a/library/MiscUtils.cpp b/library/MiscUtils.cpp index 6b50da347..7ffd4e1c6 100644 --- a/library/MiscUtils.cpp +++ b/library/MiscUtils.cpp @@ -472,11 +472,3 @@ 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 099e86581..d14bdb6e9 100644 --- a/library/include/MiscUtils.h +++ b/library/include/MiscUtils.h @@ -496,7 +496,3 @@ 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 6cee58342..36dd641d5 100644 --- a/library/include/modules/DFSDL.h +++ b/library/include/modules/DFSDL.h @@ -47,12 +47,17 @@ DFHACK_EXPORT void DFSDL_FreeSurface(SDL_Surface *surface); // DFHACK_EXPORT int DFSDL_SemWait(SDL_sem *sem); // DFHACK_EXPORT int DFSDL_SemPost(SDL_sem *sem); DFHACK_EXPORT int DFSDL_PushEvent(SDL_Event *event); +DFHACK_EXPORT void DFSDL_free(void *ptr); // 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); +// see wrapper functions below for cp-437 variants +DFHACK_EXPORT char * DFSDL_GetClipboardText(); +DFHACK_EXPORT int DFSDL_SetClipboardText(const char *text); } +// 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/modules/DFSDL.cpp b/library/modules/DFSDL.cpp index 280fb04d6..bc50769d0 100644 --- a/library/modules/DFSDL.cpp +++ b/library/modules/DFSDL.cpp @@ -135,15 +135,29 @@ int DFSDL::DFSDL_PushEvent(SDL_Event *event) { return g_SDL_PushEvent(event); } -std::string DFSDL::DFSDL_GetClipboardText() { - if (g_SDL_HasClipboardText() != SDL_TRUE) +void DFSDL::DFSDL_free(void *ptr) { + g_SDL_free(ptr); +} + +char * DFSDL::DFSDL_GetClipboardText() { + return g_SDL_GetClipboardText(); +} + +int DFSDL::DFSDL_SetClipboardText(const char *text) { + return g_SDL_SetClipboardText(text); +} + +DFHACK_EXPORT std::string DFHack::getClipboardTextCp437() { + if (!g_sdl_handle || g_SDL_HasClipboardText() != SDL_TRUE) return ""; char *text = g_SDL_GetClipboardText(); - std::string ret = text; - g_SDL_free(text); - return ret; + std::string textcp437 = UTF2DF(text); + DFHack::DFSDL::DFSDL_free(text); + return textcp437; } -bool DFSDL::DFSDL_SetClipboardText(const char *text) { - return g_SDL_SetClipboardText(text) == 0; +DFHACK_EXPORT bool DFHack::setClipboardTextCp437(std::string text) { + if (!g_sdl_handle) + return false; + return 0 == DFHack::DFSDL::DFSDL_SetClipboardText(DF2UTF(text).c_str()); } From 785405b281d5c4cc09e87146fe26eed450216667 Mon Sep 17 00:00:00 2001 From: Myk Date: Wed, 5 Jul 2023 22:34:25 -0700 Subject: [PATCH 7/7] Update DFSDL.cpp --- library/modules/DFSDL.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/modules/DFSDL.cpp b/library/modules/DFSDL.cpp index bc50769d0..aa54cf66c 100644 --- a/library/modules/DFSDL.cpp +++ b/library/modules/DFSDL.cpp @@ -5,7 +5,7 @@ #include "Debug.h" #include "PluginManager.h" -#include +#include namespace DFHack { DBG_DECLARE(core, dfsdl, DebugCategory::LINFO);