From 59419824227148f6af39c4283c2efd73eca442e0 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Tue, 27 Jun 2017 21:31:50 -0300 Subject: [PATCH] Error if a window isn't open when love.system.set/getClipboardText are called (resolves issue #1290). --HG-- branch : minor --- src/modules/system/sdl/System.cpp | 15 +++++++++++++++ src/modules/system/sdl/System.h | 2 ++ src/modules/system/wrap_System.cpp | 6 ++++-- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/modules/system/sdl/System.cpp b/src/modules/system/sdl/System.cpp index 2dc89bd81..baeea6e17 100644 --- a/src/modules/system/sdl/System.cpp +++ b/src/modules/system/sdl/System.cpp @@ -20,6 +20,7 @@ // LOVE #include "System.h" +#include "window/Window.h" // SDL #include @@ -46,13 +47,27 @@ int System::getProcessorCount() const return SDL_GetCPUCount(); } +bool System::isWindowOpen() const +{ + auto window = Module::getInstance(M_WINDOW); + return window != nullptr && window->isOpen(); +} + void System::setClipboardText(const std::string &text) const { + // SDL requires the video subsystem to be initialized and a window to be + // opened in order for clipboard text to work, on at least some platforms. + if (!isWindowOpen()) + throw love::Exception("A window must be created in order for setClipboardText to function properly."); + SDL_SetClipboardText(text.c_str()); } std::string System::getClipboardText() const { + if (!isWindowOpen()) + throw love::Exception("A window must be created in order for getClipboardText to function properly."); + std::string text(""); char *ctext = SDL_GetClipboardText(); diff --git a/src/modules/system/sdl/System.h b/src/modules/system/sdl/System.h index a75734875..06e6ca08f 100644 --- a/src/modules/system/sdl/System.h +++ b/src/modules/system/sdl/System.h @@ -54,6 +54,8 @@ public: private: + bool isWindowOpen() const; + static EnumMap::Entry powerEntries[]; static EnumMap powerStates; diff --git a/src/modules/system/wrap_System.cpp b/src/modules/system/wrap_System.cpp index 757192895..9e5d6ed76 100644 --- a/src/modules/system/wrap_System.cpp +++ b/src/modules/system/wrap_System.cpp @@ -44,13 +44,15 @@ int w_getProcessorCount(lua_State *L) int w_setClipboardText(lua_State *L) { const char *text = luaL_checkstring(L, 1); - instance()->setClipboardText(text); + luax_catchexcept(L, [&]() { instance()->setClipboardText(text); }); return 0; } int w_getClipboardText(lua_State *L) { - luax_pushstring(L, instance()->getClipboardText()); + std::string text; + luax_catchexcept(L, [&]() { text = instance()->getClipboardText(); }); + luax_pushstring(L, text); return 1; }