Skip to content

Commit

Permalink
Implement platform-agnostic paste in SDL2 backend
Browse files Browse the repository at this point in the history
  • Loading branch information
falbrechtskirchinger authored and Flow86 committed Jul 22, 2023
1 parent 5bf5c92 commit 5907058
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions extras/videoDrivers/SDL2/VideoSDL2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <boost/nowide/iostream.hpp>
#include <SDL.h>
#include <algorithm>
#include <memory>

#ifdef _WIN32
# include <boost/nowide/convert.hpp>
Expand All @@ -28,6 +29,17 @@
PrintError(SDL_GetError()); \
} while(false)

namespace {
template<typename T>
struct SDLMemoryDeleter
{
void operator()(T* p) const { SDL_free(p); }
};

template<typename T>
using SDL_memory = std::unique_ptr<T, SDLMemoryDeleter<T>>;
} // namespace

IVideoDriver* CreateVideoInstance(VideoDriverLoaderInterface* CallBack)
{
return new VideoSDL2(CallBack);
Expand Down Expand Up @@ -213,29 +225,19 @@ void VideoSDL2::PrintError(const std::string& msg) const

void VideoSDL2::HandlePaste()
{
#ifdef _WIN32
if(!IsClipboardFormatAvailable(CF_UNICODETEXT))
if(!SDL_HasClipboardText())
return;

OpenClipboard(nullptr);
SDL_memory<char> text(SDL_GetClipboardText());
if(!text || *text == '\0') // empty string indicates error
PrintError(text ? SDL_GetError() : "Paste failed.");

HANDLE hData = GetClipboardData(CF_UNICODETEXT);
const wchar_t* pData = (const wchar_t*)GlobalLock(hData);

KeyEvent ke = {KeyType::Invalid, 0, false, false, false};
while(pData && *pData)
KeyEvent ke = {KeyType::Char, 0, false, false, false};
for(const char32_t c : s25util::utf8to32(text.get()))
{
ke.c = *(pData++);
if(ke.c == L' ')
ke.kt = KeyType::Space;
else
ke.kt = KeyType::Char;
ke.c = static_cast<unsigned>(c);
CallBack->Msg_KeyDown(ke);
}

GlobalUnlock(hData);
CloseClipboard();
#endif
}

void VideoSDL2::DestroyScreen()
Expand Down

0 comments on commit 5907058

Please sign in to comment.