Win32: Remove intermediate clipboard string copy

This commit is contained in:
Camilla Berglund 2016-06-23 17:06:03 +02:00
parent e81f4b29db
commit 0ee8159f6c

View File

@ -1545,37 +1545,41 @@ void _glfwPlatformSetCursor(_GLFWwindow* window, _GLFWcursor* cursor)
void _glfwPlatformSetClipboardString(_GLFWwindow* window, const char* string) void _glfwPlatformSetClipboardString(_GLFWwindow* window, const char* string)
{ {
WCHAR* wideString; int characterCount;
HANDLE stringHandle; HANDLE stringHandle;
size_t wideSize; void* clipboardBuffer;
wideString = _glfwCreateWideStringFromUTF8Win32(string); characterCount = MultiByteToWideChar(CP_UTF8, 0, string, -1, NULL, 0);
if (!wideString) if (!characterCount)
{ {
_glfwInputError(GLFW_PLATFORM_ERROR, _glfwInputError(GLFW_PLATFORM_ERROR,
"Win32: Failed to convert string to UTF-16"); "Win32: Failed to convert clipboard string to UTF-16");
return; return;
} }
wideSize = (wcslen(wideString) + 1) * sizeof(WCHAR); stringHandle = GlobalAlloc(GMEM_MOVEABLE, characterCount * sizeof(WCHAR));
stringHandle = GlobalAlloc(GMEM_MOVEABLE, wideSize);
if (!stringHandle) if (!stringHandle)
{ {
free(wideString);
_glfwInputError(GLFW_PLATFORM_ERROR, _glfwInputError(GLFW_PLATFORM_ERROR,
"Win32: Failed to allocate global handle for clipboard"); "Win32: Failed to allocate global handle for clipboard");
return; return;
} }
memcpy(GlobalLock(stringHandle), wideString, wideSize); clipboardBuffer = GlobalLock(stringHandle);
if (!clipboardBuffer)
{
GlobalFree(stringHandle);
_glfwInputError(GLFW_PLATFORM_ERROR, "Win32: Failed to lock global handle");
return;
}
MultiByteToWideChar(CP_UTF8, 0, string, -1, clipboardBuffer, characterCount);
GlobalUnlock(stringHandle); GlobalUnlock(stringHandle);
if (!OpenClipboard(_glfw.win32.helperWindowHandle)) if (!OpenClipboard(_glfw.win32.helperWindowHandle))
{ {
GlobalFree(stringHandle); GlobalFree(stringHandle);
free(wideString);
_glfwInputError(GLFW_PLATFORM_ERROR, "Win32: Failed to open clipboard"); _glfwInputError(GLFW_PLATFORM_ERROR, "Win32: Failed to open clipboard");
return; return;
@ -1584,8 +1588,6 @@ void _glfwPlatformSetClipboardString(_GLFWwindow* window, const char* string)
EmptyClipboard(); EmptyClipboard();
SetClipboardData(CF_UNICODETEXT, stringHandle); SetClipboardData(CF_UNICODETEXT, stringHandle);
CloseClipboard(); CloseClipboard();
free(wideString);
} }
const char* _glfwPlatformGetClipboardString(_GLFWwindow* window) const char* _glfwPlatformGetClipboardString(_GLFWwindow* window)