Use non-static member vars for SDL clipboard / primary selection buffers

This commit is contained in:
DS 2023-03-26 14:13:58 +02:00 committed by GitHub
parent a67f3003de
commit ba1cd19983
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 14 deletions

@ -30,7 +30,7 @@
#include "fast_atof.h"
#if defined(_IRR_COMPILE_WITH_SDL_DEVICE_)
static bool sdl_supports_primary_selection = [] {
static const bool sdl_supports_primary_selection = [] {
#if SDL_VERSION_ATLEAST(2, 25, 0)
SDL_version linked_version;
SDL_GetVersion(&linked_version);
@ -62,6 +62,15 @@ COSOperator::COSOperator(const core::stringc& osVersion) : OperatingSystem(osVer
}
COSOperator::~COSOperator()
{
#ifdef _IRR_COMPILE_WITH_SDL_DEVICE_
SDL_free(ClipboardSelectionText);
SDL_free(PrimarySelectionText);
#endif
}
//! returns the current operating system version as string.
const core::stringc& COSOperator::getOperatingSystemVersion() const
{
@ -142,11 +151,9 @@ void COSOperator::copyToPrimarySelection(const c8 *text) const
const c8* COSOperator::getTextFromClipboard() const
{
#if defined(_IRR_COMPILE_WITH_SDL_DEVICE_)
static char *text = nullptr;
if (text)
SDL_free(text);
text = SDL_GetClipboardText();
return text;
SDL_free(ClipboardSelectionText);
ClipboardSelectionText = SDL_GetClipboardText();
return ClipboardSelectionText;
#elif defined(_IRR_WINDOWS_API_)
if (!OpenClipboard(NULL))
@ -195,19 +202,17 @@ const c8* COSOperator::getTextFromPrimarySelection() const
#if defined(_IRR_COMPILE_WITH_SDL_DEVICE_)
#if SDL_VERSION_ATLEAST(2, 25, 0)
if (sdl_supports_primary_selection) {
static char *text = nullptr;
if (text)
SDL_free(text);
text = SDL_GetPrimarySelectionText();
return text;
SDL_free(PrimarySelectionText);
PrimarySelectionText = SDL_GetPrimarySelectionText();
return PrimarySelectionText;
}
#endif
return 0;
#elif defined(_IRR_COMPILE_WITH_X11_DEVICE_)
if ( IrrDeviceLinux )
return IrrDeviceLinux->getTextFromPrimarySelection();
return 0;
if ( IrrDeviceLinux )
return IrrDeviceLinux->getTextFromPrimarySelection();
return 0;
#else

@ -23,6 +23,11 @@ public:
#endif
COSOperator(const core::stringc& osversion);
~COSOperator();
COSOperator(const COSOperator &) = delete;
COSOperator &operator=(const COSOperator &) = delete;
//! returns the current operation system version as string.
const core::stringc& getOperatingSystemVersion() const override;
@ -56,6 +61,12 @@ private:
mutable core::stringc ClipboardBuf;
#endif
#ifdef _IRR_COMPILE_WITH_SDL_DEVICE_
// These need to be freed with SDL_free
mutable char *ClipboardSelectionText = nullptr;
mutable char *PrimarySelectionText = nullptr;
#endif
};
} // end namespace