|
15 | 15 | #include <boost/nowide/iostream.hpp> |
16 | 16 | #include <SDL.h> |
17 | 17 | #include <algorithm> |
| 18 | +#include <memory> |
18 | 19 |
|
19 | 20 | #ifdef _WIN32 |
20 | 21 | # include <boost/nowide/convert.hpp> |
|
28 | 29 | PrintError(SDL_GetError()); \ |
29 | 30 | } while(false) |
30 | 31 |
|
| 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 | + |
31 | 43 | IVideoDriver* CreateVideoInstance(VideoDriverLoaderInterface* CallBack) |
32 | 44 | { |
33 | 45 | return new VideoSDL2(CallBack); |
@@ -213,29 +225,19 @@ void VideoSDL2::PrintError(const std::string& msg) const |
213 | 225 |
|
214 | 226 | void VideoSDL2::HandlePaste() |
215 | 227 | { |
216 | | -#ifdef _WIN32 |
217 | | - if(!IsClipboardFormatAvailable(CF_UNICODETEXT)) |
| 228 | + if(!SDL_HasClipboardText()) |
218 | 229 | return; |
219 | 230 |
|
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."); |
221 | 234 |
|
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())) |
227 | 237 | { |
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); |
233 | 239 | CallBack->Msg_KeyDown(ke); |
234 | 240 | } |
235 | | - |
236 | | - GlobalUnlock(hData); |
237 | | - CloseClipboard(); |
238 | | -#endif |
239 | 241 | } |
240 | 242 |
|
241 | 243 | void VideoSDL2::DestroyScreen() |
|
0 commit comments