summaryrefslogtreecommitdiffstats
path: root/win
diff options
context:
space:
mode:
authorPierre Ossman <ossman@cendio.se>2019-05-10 11:44:19 +0200
committerPierre Ossman <ossman@cendio.se>2019-07-01 11:18:27 +0200
commit5fbbe10b6c180ae8c0545695db6ad308cb5caea7 (patch)
treedb83c70816ef00328e0da860a5b40522ce6435ae /win
parent615d16bd5ba11e89262cc5cfe94a35b6d6e7a628 (diff)
downloadtigervnc-5fbbe10b6c180ae8c0545695db6ad308cb5caea7.tar.gz
tigervnc-5fbbe10b6c180ae8c0545695db6ad308cb5caea7.zip
Use UTF-8 in clipboard API
In prepartion for better clipboard extensions that can send Unicode data between the client and server.
Diffstat (limited to 'win')
-rw-r--r--win/rfb_win32/Clipboard.cxx73
1 files changed, 19 insertions, 54 deletions
diff --git a/win/rfb_win32/Clipboard.cxx b/win/rfb_win32/Clipboard.cxx
index 306cfbad..11963675 100644
--- a/win/rfb_win32/Clipboard.cxx
+++ b/win/rfb_win32/Clipboard.cxx
@@ -30,41 +30,6 @@ using namespace rfb::win32;
static LogWriter vlog("Clipboard");
-
-//
-// -=- CR/LF handlers
-//
-
-char*
-unix2dos(const char* text) {
- int len = strlen(text)+1;
- char* dos = new char[strlen(text)*2+1];
- int i, j=0;
- for (i=0; i<len; i++) {
- if (text[i] == '\x0a')
- dos[j++] = '\x0d';
- dos[j++] = text[i];
- }
- return dos;
-}
-
-
-//
-// -=- ISO-8859-1 (Latin 1) filter (in-place)
-//
-
-void
-removeNonISOLatin1Chars(char* text) {
- int len = strlen(text);
- int i=0, j=0;
- for (; i<len; i++) {
- if (((text[i] >= 1) && (text[i] <= 127)) ||
- ((text[i] >= 160) && (text[i] <= 255)))
- text[j++] = text[i];
- }
- text[j] = 0;
-}
-
//
// -=- Clipboard object
//
@@ -106,7 +71,7 @@ Clipboard::processMessage(UINT msg, WPARAM wParam, LPARAM lParam) {
if (notifier == NULL)
vlog.debug("no clipboard notifier registered");
else
- notifier->notifyClipboardChanged(IsClipboardFormatAvailable(CF_TEXT));
+ notifier->notifyClipboardChanged(IsClipboardFormatAvailable(CF_UNICODETEXT));
}
}
if (next_window)
@@ -120,35 +85,34 @@ Clipboard::processMessage(UINT msg, WPARAM wParam, LPARAM lParam) {
char*
Clipboard::getClipText() {
HGLOBAL cliphandle;
- char* clipdata;
- char* filtered;
+ wchar_t* clipdata;
+ CharArray utf8;
// Open the clipboard
if (!OpenClipboard(getHandle()))
return NULL;
// Get the clipboard data
- cliphandle = GetClipboardData(CF_TEXT);
+ cliphandle = GetClipboardData(CF_UNICODETEXT);
if (!cliphandle) {
CloseClipboard();
return NULL;
}
- clipdata = (char*) GlobalLock(cliphandle);
+ clipdata = (wchar_t*) GlobalLock(cliphandle);
if (!clipdata) {
CloseClipboard();
return NULL;
}
- // Filter out anything unwanted
- filtered = convertLF(clipdata, strlen(clipdata));
- removeNonISOLatin1Chars(filtered);
+ // Convert it to UTF-8
+ utf8.replaceBuf(utf16ToUTF8(clipdata));
// Release the buffer and close the clipboard
GlobalUnlock(cliphandle);
CloseClipboard();
- return filtered;
+ return convertLF(utf8.buf);
}
void
@@ -161,26 +125,27 @@ Clipboard::setClipText(const char* text) {
if (!OpenClipboard(getHandle()))
throw rdr::SystemException("unable to open Win32 clipboard", GetLastError());
- // - Pre-process the supplied clipboard text into DOS format
- CharArray dos_text;
- dos_text.buf = unix2dos(text);
- removeNonISOLatin1Chars(dos_text.buf);
- int dos_text_len = strlen(dos_text.buf);
+ // - Convert the supplied clipboard text into UTF-16 format with CRLF
+ CharArray filtered(convertCRLF(text));
+ wchar_t* utf16;
+
+ utf16 = utf8ToUTF16(filtered.buf);
// - Allocate global memory for the data
- clip_handle = ::GlobalAlloc(GMEM_MOVEABLE, dos_text_len+1);
+ clip_handle = ::GlobalAlloc(GMEM_MOVEABLE, (wcslen(utf16) + 1) * 2);
- char* data = (char*) GlobalLock(clip_handle);
- memcpy(data, dos_text.buf, dos_text_len+1);
- data[dos_text_len] = 0;
+ wchar_t* data = (wchar_t*) GlobalLock(clip_handle);
+ wcscpy(data, utf16);
GlobalUnlock(clip_handle);
+ strFree(utf16);
+
// - Next, we must clear out any existing data
if (!EmptyClipboard())
throw rdr::SystemException("unable to empty Win32 clipboard", GetLastError());
// - Set the new clipboard data
- if (!SetClipboardData(CF_TEXT, clip_handle))
+ if (!SetClipboardData(CF_UNICODETEXT, clip_handle))
throw rdr::SystemException("unable to set Win32 clipboard", GetLastError());
clip_handle = 0;