mirror of
https://github.com/love2d/love.git
synced 2026-08-16 08:11:02 +02:00
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:
@@ -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();
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user