Changeset 18852 in vbox for trunk/src/VBox
- Timestamp:
- Apr 8, 2009 7:19:05 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/GuestHost/SharedClipboard/x11-clipboard.cpp
r18756 r18852 47 47 #include <VBox/HostServices/VBoxClipboardSvc.h> 48 48 49 /** Do we want to test Utf16 by disabling other text formats? */50 static bool g_testUtf16 = false;51 49 /** Do we want to test Utf8 by disabling other text formats? */ 52 50 static bool g_testUtf8 = false; … … 62 60 TARGETS, 63 61 CTEXT, 64 UTF8, 65 UTF16 62 UTF8 66 63 }; 67 64 … … 97 94 /** X11 atom refering to the clipboard timestamp target: TIMESTAMP */ 98 95 Atom atomTimestamp; 99 /** X11 atom refering to the clipboard utf16 text format: text/plain;charset=ISO-10646-UCS-2 */100 Atom atomUtf16;101 96 /** X11 atom refering to the clipboard utf8 text format: UTF8_STRING */ 102 97 Atom atomUtf8; … … 142 137 static bool g_fHaveX11; 143 138 144 /** 145 * Convert the UTF-16 text obtained from the X11 clipboard to UTF-16LE with 146 * Windows EOLs, place it in the buffer supplied and signal that data has 147 * arrived. 148 * 149 * @param pValue Source UTF-16 text 150 * @param cwSourceLen Length in 16-bit words of the source text 151 * @param pv Where to store the converted data 152 * @param cb Length in bytes of the buffer pointed to by cb 153 * @param pcbActual Where to store the size of the converted data 154 * @param pClient Pointer to the client context structure 155 * @note X11 backend code, called from the Xt callback when we wish to read 156 * the X11 clipboard. 157 */ 158 static void vboxClipboardGetUtf16(VBOXCLIPBOARDCONTEXTX11 *pCtx, 159 XtPointer pValue, unsigned cwSrcLen, 160 void *pv, unsigned cb, 161 uint32_t *pcbActual) 139 static int vboxClipboardWriteUtf16LE(VBOXCLIPBOARDCONTEXTX11 *pCtx, 140 PRTUTF16 pu16SrcText, 141 size_t cwSrcLen, 142 void *pv, unsigned cb, 143 uint32_t *pcbActual) 162 144 { 163 145 size_t cwDestLen; 164 PRTUTF16 pu16SrcText = reinterpret_cast<PRTUTF16>(pValue);165 146 PRTUTF16 pu16DestText = reinterpret_cast<PRTUTF16>(pv); 166 167 LogFlowFunc (("converting Utf-16 to Utf-16LE. cwSrcLen=%d, cb=%d, pu16SrcText+1=%.*ls\n", 168 cwSrcLen, cb, cwSrcLen - 1, pu16SrcText + 1)); 169 *pcbActual = 0; /* Only set this to the right value on success. */ 170 /* How long will the converted text be? */ 171 int rc = vboxClipboardUtf16GetWinSize(pu16SrcText, cwSrcLen, &cwDestLen); 147 int rc = VINF_SUCCESS; 148 /* Check how much longer will the converted text will be. */ 149 rc = vboxClipboardUtf16GetWinSize(pu16SrcText, cwSrcLen, &cwDestLen); 172 150 if (RT_SUCCESS(rc) && (cb < cwDestLen * 2)) 173 151 { … … 186 164 *pcbActual = cwDestLen * 2; 187 165 } 188 /* We need to do this whether we succeed or fail. */ 189 XtFree(reinterpret_cast<char *>(pValue)); 190 RTSemEventSignal(pCtx->waitForData); 191 LogFlowFunc(("Returning. Status is %Rrc\n", rc)); 166 return rc; 192 167 } 193 168 … … 211 186 uint32_t *pcbActual) 212 187 { 213 size_t cwSrcLen , cwDestLen;188 size_t cwSrcLen; 214 189 char *pu8SrcText = reinterpret_cast<char *>(pValue); 215 190 PRTUTF16 pu16SrcText = NULL; 216 PRTUTF16 pu16DestText = reinterpret_cast<PRTUTF16>(pv);217 191 218 192 LogFlowFunc (("converting Utf-8 to Utf-16LE. cbSrcLen=%d, cb=%d, pu8SrcText=%.*s\n", … … 221 195 /* First convert the UTF8 to UTF16 */ 222 196 int rc = RTStrToUtf16Ex(pu8SrcText, cbSrcLen, &pu16SrcText, 0, &cwSrcLen); 223 /* Check how much longer will the converted text will be. */224 197 if (RT_SUCCESS(rc)) 225 rc = vboxClipboardUtf16GetWinSize(pu16SrcText, cwSrcLen, &cwDestLen); 226 if (RT_SUCCESS(rc) && (cb < cwDestLen * 2)) 227 { 228 /* Not enough buffer space provided - report the amount needed. */ 229 LogFlowFunc (("guest buffer too small: size %d bytes, needed %d. Returning.\n", 230 cb, cwDestLen * 2)); 231 *pcbActual = cwDestLen * 2; 232 rc = VERR_BUFFER_OVERFLOW; 233 } 234 /* Convert the text. */ 235 if (RT_SUCCESS(rc)) 236 rc = vboxClipboardUtf16LinToWin(pu16SrcText, cwSrcLen, pu16DestText, cb / 2); 237 if (RT_SUCCESS(rc)) 238 { 239 LogFlowFunc (("converted string is %.*ls.\n", cwDestLen, pu16DestText)); 240 *pcbActual = cwDestLen * 2; 241 } 198 rc = vboxClipboardWriteUtf16LE(pCtx, pu16SrcText, cwSrcLen, 199 pv, cb, pcbActual); 242 200 XtFree(reinterpret_cast<char *>(pValue)); 243 201 RTUtf16Free(pu16SrcText); … … 265 223 uint32_t *pcbActual) 266 224 { 267 size_t cwSrcLen , cwDestLen;225 size_t cwSrcLen; 268 226 char **ppu8SrcText = NULL; 269 227 PRTUTF16 pu16SrcText = NULL; 270 PRTUTF16 pu16DestText = reinterpret_cast<PRTUTF16>(pv);271 228 XTextProperty property; 272 229 int rc = VINF_SUCCESS; … … 305 262 if (RT_SUCCESS(rc)) 306 263 rc = RTStrToUtf16Ex(*ppu8SrcText, cbSrcLen, &pu16SrcText, 0, &cwSrcLen); 307 /* Check how much longer will the converted text will be. */308 264 if (RT_SUCCESS(rc)) 309 rc = vboxClipboardUtf16GetWinSize(pu16SrcText, cwSrcLen, &cwDestLen); 310 if (RT_SUCCESS(rc) && (cb < cwDestLen * 2)) 311 { 312 /* Not enough buffer space provided - report the amount needed. */ 313 LogFlowFunc (("guest buffer too small: size %d bytes, needed %d. Returning.\n", 314 cb, cwDestLen * 2)); 315 *pcbActual = cwDestLen * 2; 316 rc = VERR_BUFFER_OVERFLOW; 317 } 318 /* Convert the text. */ 319 if (RT_SUCCESS(rc)) 320 rc = vboxClipboardUtf16LinToWin(pu16SrcText, cwSrcLen, pu16DestText, cb / 2); 321 if (RT_SUCCESS(rc)) 322 { 323 LogFlowFunc (("converted string is %.*ls\n", cwDestLen, pu16DestText)); 324 *pcbActual = cwDestLen * 2; 325 } 265 rc = vboxClipboardWriteUtf16LE(pCtx, pu16SrcText, cwSrcLen, 266 pv, cb, pcbActual); 326 267 if (ppu8SrcText != NULL) 327 268 XFreeStringList(ppu8SrcText); … … 418 359 switch (pCtx->X11TextFormat) 419 360 { 420 case UTF16:421 vboxClipboardGetUtf16(pCtx, pValue, cTextLen / 2, pRequest->pv,422 pRequest->cb, pRequest->pcbActual);423 break;424 361 case CTEXT: 425 362 vboxClipboardGetCTextFromX11(pCtx, pValue, cTextLen, pRequest->pv, … … 660 597 pCtx->atomMultiple = XInternAtom(XtDisplay(pCtx->widget), "MULTIPLE", false); 661 598 pCtx->atomTimestamp = XInternAtom(XtDisplay(pCtx->widget), "TIMESTAMP", false); 662 pCtx->atomUtf16 = XInternAtom(XtDisplay(pCtx->widget),663 "text/plain;charset=ISO-10646-UCS-2", false);664 599 pCtx->atomUtf8 = XInternAtom(XtDisplay(pCtx->widget), "UTF_STRING", false); 665 600 /* And build up the vector of supported formats */ 666 601 pCtx->atomCText = XInternAtom(XtDisplay(pCtx->widget), "COMPOUND_TEXT", false); 667 602 /* And build up the vector of supported formats */ 668 if (!g_testUtf8 && !g_testCText) 669 vboxClipboardAddFormat(pCtx, 670 "text/plain;charset=ISO-10646-UCS-2", 671 UTF16, 672 VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT); 673 if (!g_testUtf16 && !g_testCText) 603 if (!g_testCText) 674 604 { 675 605 vboxClipboardAddFormat(pCtx, "UTF8_STRING", UTF8, … … 686 616 VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT); 687 617 } 688 if (!g_testUtf 16 && !g_testUtf8)618 if (!g_testUtf8) 689 619 vboxClipboardAddFormat(pCtx, "COMPOUND_TEXT", CTEXT, 690 620 VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT); … … 721 651 } 722 652 723 if (RTEnvGet("VBOX_CBTEST_UTF16")) 724 { 725 g_testUtf16 = true; 726 LogRel(("Host clipboard: testing Utf16\n")); 727 } 728 else if (RTEnvGet("VBOX_CBTEST_UTF8")) 653 if (RTEnvGet("VBOX_CBTEST_UTF8")) 729 654 { 730 655 g_testUtf8 = true; … … 938 863 *pcLenReturn = cTargets + 3; 939 864 *piFormatReturn = 32; 940 return true;941 }942 943 /**944 * Satisfy a request from X11 to convert the clipboard text to Utf16. We945 * return non-zero terminated text.946 * @todo that works, but it is bad. Change it to return zero-terminated947 * text.948 *949 * @returns true if we successfully convert the data to the format950 * requested, false otherwise.951 *952 * @param atomTypeReturn Where to store the atom for the type of the data953 * we are returning954 * @param pValReturn Where to store the pointer to the data we are955 * returning. This should be to memory allocated by956 * XtMalloc, which will be freed by the Xt toolkit957 * later.958 * @param pcLenReturn Where to store the length of the data we are959 * returning960 * @param piFormatReturn Where to store the bit width (8, 16, 32) of the961 * data we are returning962 * @note X11 backend code, called by the callback for XtOwnSelection.963 */964 static Boolean vboxClipboardConvertUtf16(VBOXCLIPBOARDCONTEXTX11 *pCtx,965 Atom *atomTypeReturn,966 XtPointer *pValReturn,967 unsigned long *pcLenReturn,968 int *piFormatReturn)969 {970 PRTUTF16 pu16SrcText, pu16DestText;971 void *pvVBox;972 uint32_t cbVBox;973 size_t cwSrcLen, cwDestLen;974 int rc;975 976 LogFlowFunc (("called\n"));977 rc = VBoxX11ClipboardReadVBoxData(pCtx->pFrontend, VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT, &pvVBox, &cbVBox);978 if ((RT_FAILURE(rc)) || (cbVBox == 0))979 {980 /* If VBoxX11ClipboardReadVBoxData fails then pClient may be invalid */981 LogRelFunc (("VBoxX11ClipboardReadVBoxData returned %Rrc%s\n", rc,982 RT_SUCCESS(rc) ? ", cbVBox == 0" : ""));983 RTMemFree(pvVBox);984 return false;985 }986 pu16SrcText = reinterpret_cast<PRTUTF16>(pvVBox);987 cwSrcLen = cbVBox / 2;988 /* How long will the converted text be? */989 rc = vboxClipboardUtf16GetLinSize(pu16SrcText, cwSrcLen, &cwDestLen);990 if (RT_FAILURE(rc))991 {992 LogRel(("vboxClipboardConvertUtf16: clipboard conversion failed. vboxClipboardUtf16GetLinSize returned %Rrc. Abandoning.\n", rc));993 RTMemFree(pvVBox);994 AssertRCReturn(rc, false);995 }996 if (cwDestLen == 0)997 {998 LogFlowFunc(("received empty clipboard data from the guest, returning false.\n"));999 RTMemFree(pvVBox);1000 return false;1001 }1002 pu16DestText = reinterpret_cast<PRTUTF16>(XtMalloc(cwDestLen * 2));1003 if (pu16DestText == 0)1004 {1005 LogRel(("vboxClipboardConvertUtf16: failed to allocate %d bytes\n", cwDestLen * 2));1006 RTMemFree(pvVBox);1007 return false;1008 }1009 /* Convert the text. */1010 rc = vboxClipboardUtf16WinToLin(pu16SrcText, cwSrcLen, pu16DestText, cwDestLen);1011 if (RT_FAILURE(rc))1012 {1013 LogRel(("vboxClipboardConvertUtf16: clipboard conversion failed. vboxClipboardUtf16WinToLin returned %Rrc. Abandoning.\n", rc));1014 XtFree(reinterpret_cast<char *>(pu16DestText));1015 RTMemFree(pvVBox);1016 return false;1017 }1018 LogFlowFunc (("converted string is %.*ls. Returning.\n", cwDestLen, pu16DestText));1019 RTMemFree(pvVBox);1020 *atomTypeReturn = pCtx->atomUtf16;1021 *pValReturn = reinterpret_cast<XtPointer>(pu16DestText);1022 *pcLenReturn = cwDestLen;1023 *piFormatReturn = 16;1024 865 return true; 1025 866 } … … 1321 1162 pValReturn, pcLenReturn, 1322 1163 piFormatReturn); 1323 case UTF16:1324 return vboxClipboardConvertUtf16(pCtx, atomTypeReturn, pValReturn,1325 pcLenReturn, piFormatReturn);1326 1164 case UTF8: 1327 1165 return vboxClipboardConvertToUtf8ForX11(pCtx, atomTypeReturn,
Note:
See TracChangeset
for help on using the changeset viewer.