From 3979588f85eb3ec7252496d4d181d93fe7be03af Mon Sep 17 00:00:00 2001 From: JailDoctor Date: Mon, 13 Dec 2021 20:02:17 +0100 Subject: [PATCH] [FEATURE] toclipboard & fromclipboard --- ascii.cpp | 21 ++++++++++++++++++++- ascii.h | 3 +++ lua.cpp | 13 +++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/ascii.cpp b/ascii.cpp index 0e40e39..0093f48 100644 --- a/ascii.cpp +++ b/ascii.cpp @@ -431,7 +431,7 @@ int rnd(int x) { srand(x); }*/ -char str_tmp[256]; +char str_tmp[1024]; const char* tostr(float val) { return SDL_itoa(val, str_tmp, 10); } @@ -571,3 +571,22 @@ void filein(const char* str, uint16_t addr, uint16_t size) { fread(&mem[addr], size, 1, f); fclose(f); } + +void toclipboard(const char* str) { + SDL_SetClipboardText(str); +} + +const char* fromclipboard() { + char* text = SDL_GetClipboardText(); + int len = strlen(text); + if (len > 1023) { + len = 27; + SDL_memcpy(str_tmp, "ERROR! CLIPBOARD TOO LARGE", len); + } else { + SDL_memcpy(str_tmp, text, len); + } + str_tmp[len] = '\0'; + SDL_free((void*)text); + + return str_tmp; +} \ No newline at end of file diff --git a/ascii.h b/ascii.h index 1ef2029..6650f2e 100644 --- a/ascii.h +++ b/ascii.h @@ -180,3 +180,6 @@ void load(const char* str); void fileout(const char* str, uint16_t addr, uint16_t size); void filein(const char* str, uint16_t addr, uint16_t size); + +void toclipboard(const char* str); +const char* fromclipboard(); \ No newline at end of file diff --git a/lua.cpp b/lua.cpp index 0594923..97a9988 100644 --- a/lua.cpp +++ b/lua.cpp @@ -381,6 +381,17 @@ extern "C" { filein(str, addr, size); return 0; } + + static int cpp_toclipboard(lua_State *L) { + const char* str = luaL_checkstring(L, 1); + toclipboard(str); + return 0; + } + + static int cpp_fromclipboard(lua_State *L) { + lua_pushstring(L, fromclipboard()); + return 1; + } } #define STATE_STOPPED 0 @@ -473,6 +484,8 @@ void lua_init(const char* filename, const bool start_playing) { lua_pushcfunction(L,cpp_fileout); lua_setglobal(L, "fileout"); lua_pushcfunction(L,cpp_filein); lua_setglobal(L, "filein"); + lua_pushcfunction(L,cpp_toclipboard); lua_setglobal(L, "toclipboard"); + lua_pushcfunction(L,cpp_fromclipboard); lua_setglobal(L, "fromclipboard"); lua_pushinteger(L, 0); lua_setglobal(L, "KEY_UNKNOWN"); lua_pushinteger(L, 4); lua_setglobal(L, "KEY_A");