Skip to content

Commit 9eb5ba6

Browse files
zaodclamage
authored andcommitted
Assume clipboard text from PoB is UTF-8 or ASCII
The Copy function previously put the copied text from Lua verbatim on the clipboard as `CF_TEXT` which if not annotated with a locale is assumed to be in the current "input locale". This commit changes it to instead assume that it's either in UTF-8 (or ASCII, which is a subset of UTF-8), transcoding it to UTF-16LE for `CF_UNICODETEXT`. This enables the ability for the Lua side to copy out UTF-8 text which it may have obtained from an external source like trade or another API. A brief audit of the current Lua source code does not reveal any call site that should contain text in the local codepage as build XML is supposed to be UTF-8 and no file paths seem to be copied.
1 parent 4b386cc commit 9eb5ba6

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

engine/system/win/sys_main.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -385,14 +385,15 @@ void sys_main_c::ShowCursor(int doShow)
385385
void sys_main_c::ClipboardCopy(const char* str)
386386
{
387387
size_t len = strlen(str);
388-
HGLOBAL hg = GlobalAlloc(GMEM_MOVEABLE, len + 1);
388+
auto cch = MultiByteToWideChar(CP_UTF8, 0, str, len + 1, nullptr, 0);
389+
HGLOBAL hg = GlobalAlloc(GMEM_MOVEABLE, cch * sizeof(wchar_t));
389390
if ( !hg ) return;
390-
char* cp = (char*)GlobalLock(hg);
391-
strcpy(cp, str);
391+
wchar_t* cp = (wchar_t*)GlobalLock(hg); // 8-byte aligned so cast is safe
392+
MultiByteToWideChar(CP_UTF8, 0, str, len + 1, cp, cch);
392393
GlobalUnlock(hg);
393394
OpenClipboard((HWND)video->GetWindowHandle());
394395
EmptyClipboard();
395-
SetClipboardData(CF_TEXT, hg);
396+
SetClipboardData(CF_UNICODETEXT, hg);
396397
CloseClipboard();
397398
}
398399

0 commit comments

Comments
 (0)