Error if a window isn't open when love.system.set/getClipboardText are called (resolves issue #1290).

--HG--
branch : minor
This commit is contained in:
Alex Szpakowski
2017-06-27 21:31:50 -03:00
parent 9084dce58f
commit 5941982422
3 changed files with 21 additions and 2 deletions
+15
View File
@@ -20,6 +20,7 @@
// LOVE
#include "System.h"
#include "window/Window.h"
// SDL
#include <SDL_clipboard.h>
@@ -46,13 +47,27 @@ int System::getProcessorCount() const
return SDL_GetCPUCount();
}
bool System::isWindowOpen() const
{
auto window = Module::getInstance<window::Window>(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();
+2
View File
@@ -54,6 +54,8 @@ public:
private:
bool isWindowOpen() const;
static EnumMap<PowerState, SDL_PowerState, POWER_MAX_ENUM>::Entry powerEntries[];
static EnumMap<PowerState, SDL_PowerState, POWER_MAX_ENUM> powerStates;
+4 -2
View File
@@ -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;
}