From 43ea9b94c3026ce05acddd297c31e4e9e720a826 Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Wed, 5 Jul 2023 12:08:18 -0700 Subject: [PATCH] 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()); }