Add UTF-8 clipboard support

Changes:
- Use rfbSendServerCutTextUTF8() to send clipboard text in UTF-8, falling back to Latin1 if UTF-8 is not supported.
- Implement setXCutTextUTF8() to handle received UTF-8 clipboard content.

CCBUG: 430339
This commit is contained in:
Wendi Gan
2025-04-30 17:03:47 +08:00
committed by Alexey Min
parent c5b2b0cea3
commit c434224d23
2 changed files with 16 additions and 4 deletions

View File

@@ -92,6 +92,7 @@ bool RfbServer::start()
d->screen->ptrAddEvent = pointerHook; d->screen->ptrAddEvent = pointerHook;
d->screen->passwordCheck = passwordCheck; d->screen->passwordCheck = passwordCheck;
d->screen->setXCutText = clipboardHook; d->screen->setXCutText = clipboardHook;
d->screen->setXCutTextUTF8 = clipboardHookUtf8;
} else { } else {
//if we already have a screen, stop listening first //if we already have a screen, stop listening first
rfbShutdownServer(d->screen, false); rfbShutdownServer(d->screen, false);
@@ -218,10 +219,12 @@ void RfbServer::updateCursorPosition(const QPoint & position)
void RfbServer::krfbSendServerCutText() void RfbServer::krfbSendServerCutText()
{ {
if(d->screen) { if (d->screen) {
QString text = QApplication::clipboard()->text(); QString text = QApplication::clipboard()->text();
rfbSendServerCutText(d->screen, QByteArray utf8Bytes = text.toUtf8();
text.toLocal8Bit().data(),text.length()); QByteArray latin1Bytes = text.toLatin1();
// Try to send the clipboard text to the client in UTF-8. Use Latin1 if UTF-8 is not supported.
rfbSendServerCutTextUTF8(d->screen, utf8Bytes.data(), utf8Bytes.length(), latin1Bytes.data(), latin1Bytes.length());
} }
} }
@@ -287,5 +290,13 @@ void RfbServer::pointerHook(int bm, int x, int y, rfbClientPtr cl)
//static //static
void RfbServer::clipboardHook(char *str, int len, rfbClientPtr /*cl*/) void RfbServer::clipboardHook(char *str, int len, rfbClientPtr /*cl*/)
{ {
QApplication::clipboard()->setText(QString::fromLocal8Bit(str,len)); QString text = QString::fromLatin1(str, len);
QApplication::clipboard()->setText(text);
}
void RfbServer::clipboardHookUtf8(char *str, int len, rfbClientPtr /*cl*/)
{
// The last byte is a null terminator
QString text = QString::fromUtf8(str, len - 1);
QApplication::clipboard()->setText(text);
} }

View File

@@ -52,6 +52,7 @@ private:
static void keyboardHook(rfbBool down, rfbKeySym keySym, rfbClientPtr cl); static void keyboardHook(rfbBool down, rfbKeySym keySym, rfbClientPtr cl);
static void pointerHook(int bm, int x, int y, rfbClientPtr cl); static void pointerHook(int bm, int x, int y, rfbClientPtr cl);
static void clipboardHook(char *str, int len, rfbClientPtr cl); static void clipboardHook(char *str, int len, rfbClientPtr cl);
static void clipboardHookUtf8(char *str, int len, rfbClientPtr cl);
Q_DISABLE_COPY(RfbServer) Q_DISABLE_COPY(RfbServer)