move conversion logic to MiscUtils

but keep minimal wrappers in SDL module so we don't leak memory
develop
Myk Taylor 2023-07-03 11:05:58 -07:00
parent 07e8edcdca
commit 9ca96567a5
No known key found for this signature in database
GPG Key ID: 8A39CA0FA0C16E78
7 changed files with 29 additions and 41 deletions

@ -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:

@ -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 }
};

@ -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());
}

@ -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);

@ -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);
}

@ -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

@ -3,7 +3,6 @@
#include "modules/DFSDL.h"
#include "Debug.h"
#include "MiscUtils.h"
#include "PluginManager.h"
#include <SDL.h>
@ -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));
}