Skip to content

Commit 5907058

Browse files
falbrechtskirchingerFlow86
authored andcommitted
Implement platform-agnostic paste in SDL2 backend
1 parent 5bf5c92 commit 5907058

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

extras/videoDrivers/SDL2/VideoSDL2.cpp

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <boost/nowide/iostream.hpp>
1616
#include <SDL.h>
1717
#include <algorithm>
18+
#include <memory>
1819

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

32+
namespace {
33+
template<typename T>
34+
struct SDLMemoryDeleter
35+
{
36+
void operator()(T* p) const { SDL_free(p); }
37+
};
38+
39+
template<typename T>
40+
using SDL_memory = std::unique_ptr<T, SDLMemoryDeleter<T>>;
41+
} // namespace
42+
3143
IVideoDriver* CreateVideoInstance(VideoDriverLoaderInterface* CallBack)
3244
{
3345
return new VideoSDL2(CallBack);
@@ -213,29 +225,19 @@ void VideoSDL2::PrintError(const std::string& msg) const
213225

214226
void VideoSDL2::HandlePaste()
215227
{
216-
#ifdef _WIN32
217-
if(!IsClipboardFormatAvailable(CF_UNICODETEXT))
228+
if(!SDL_HasClipboardText())
218229
return;
219230

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

222-
HANDLE hData = GetClipboardData(CF_UNICODETEXT);
223-
const wchar_t* pData = (const wchar_t*)GlobalLock(hData);
224-
225-
KeyEvent ke = {KeyType::Invalid, 0, false, false, false};
226-
while(pData && *pData)
235+
KeyEvent ke = {KeyType::Char, 0, false, false, false};
236+
for(const char32_t c : s25util::utf8to32(text.get()))
227237
{
228-
ke.c = *(pData++);
229-
if(ke.c == L' ')
230-
ke.kt = KeyType::Space;
231-
else
232-
ke.kt = KeyType::Char;
238+
ke.c = static_cast<unsigned>(c);
233239
CallBack->Msg_KeyDown(ke);
234240
}
235-
236-
GlobalUnlock(hData);
237-
CloseClipboard();
238-
#endif
239241
}
240242

241243
void VideoSDL2::DestroyScreen()

0 commit comments

Comments
 (0)