Skip to content

UTF8 capable Clipboard Functions - #176

Open
nob13 wants to merge 1 commit into
any1:masterfrom
nob13:nob/utf8_cut
Open

UTF8 capable Clipboard Functions#176
nob13 wants to merge 1 commit into
any1:masterfrom
nob13:nob/utf8_cut

Conversation

@nob13

@nob13 nob13 commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

Fix for #174

I started with nvnc_set_cut_text_with_encoding_fn and nvnc_send_cut_text_with_encoding, however there was a problem. If I send with utf8 enabled and the client is only latin1-capable, there needs to be a way to convert from utf8 to latin1. Same problem happens other way around: If the client uses cut text extensions, we need to convert from latin1 to utf8. Given that the server now has utf8 conversions methods, it can also directly expose utf8-cut-text versions. So we are now with nvnc_set_cut_text_utf8_fn and nvnc_send_cut_text_utf8.

When converting utf8 to latin1, unknown characters are replaced by ?.

The conversion routines are AI written.

I tested with an old Real VNC Viewer (no cut text extension) and NoVnc (has cut text extension).

I have read and understood CONTRIBUTING.md.

@any1 any1 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that we can just convert everything to utf-8 with the old cuttext api.

The thing is that wayvnc expects utf-8 anyway.

This is all broken as is, and converting like you're doing fixes it.

Comment thread src/server.c Outdated
Comment thread src/server.c Outdated
Comment thread src/server.c Outdated
@nob13
nob13 force-pushed the nob/utf8_cut branch 2 times, most recently from 7c0c5fb to 32d3ab3 Compare June 29, 2026 08:41
Comment thread src/server.c Outdated
return lf_buf;
}

static void notify_cut_text(struct nvnc_client* client, const char* text,

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

boolean arguments like this are bit of an anti-pattern imo.

I recommend splitting this into two functions instead, named: notify_cut_text_utf8 and notify_cut_text_latin1.

This makes the control flow evident without having to look into this function to see what the bool does.

Comment thread src/server.c Outdated
/* Broadcast clipboard text. Extended clients are sent utf8_text and legacy
* clients latin1_text; the two may point at the same buffer when no conversion
* is needed. */
static void send_cut_text(struct nvnc* server, const char* utf8_text,

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function split obfuscates things a bit. The fact that you or or LLM decided that it need an explanation is a bit of a tell.

Comment thread src/server.c Outdated
void nvnc_send_cut_text(struct nvnc* server, const char* text, uint32_t len)
{
/* text is UTF-8. Legacy clients need Latin-1, so convert only when one
* is actually connected. */

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/* comments should be
 * written like this
 */

/* they should not be written
 * like this */

Comment thread src/server.c Outdated
* is actually connected. */
struct nvnc_client* client;

bool legacy_in_use = false;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/legacy/latin1/

Comment thread src/utf8.c Outdated
}

/* Reject overlong encodings, UTF-16 surrogates and code points
* beyond the Unicode range. */

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Comment thread test/test-utf8.c Outdated
static void test_single_utf8_to_latin1(const char* input, const char* expected)
{
size_t outlen;
const char * got = utf8_to_latin1(input, strlen(input), &outlen);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style: asterisk

Comment thread test/test-utf8.c Outdated
static void test_latin_to_utf8_and_back(const char* input, const char* expected)
{
size_t outlen;
const char * got = latin1_to_utf8(input, strlen(input), &outlen);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Comment thread src/utf8.c Outdated
#include <stdint.h>
#include <stdlib.h>

char* latin1_to_utf8(const char* src, size_t len, size_t* out_len)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this project, the destination buffer is on the left and the source buffer on the right, just like in assignment

dst = src;

@nob13

nob13 commented Jul 31, 2026

Copy link
Copy Markdown
Contributor Author

Hi @any1, thanks for the review. It will take a couple of days, as we are on holiday right now.

Plain cut text is Latin-1 while the extended clipboard is UTF-8. Always
deliver received text to the handler as UTF-8, and accept UTF-8 in
nvnc_send_cut_text, converting to Latin-1 for clients without the
extended clipboard.
@nob13

nob13 commented Aug 11, 2026

Copy link
Copy Markdown
Contributor Author

@any1 Hi, I updated the PR and tried to address all issues.

Now rebased, testetd with RealVnc (legacy) and NoVNC(extended).

Feel free to edit cosmetic things directly.

@any1

any1 commented Aug 11, 2026

Copy link
Copy Markdown
Owner

Looks correct, both in form and function. I do wonder though if doing the conversion before iterating over clients is worth it, given that the number of clients is usually 1. With multiple clients video encoding cost dominates. Text encoding conversion is unlikely to ever be more expensive than that.

@nob13

nob13 commented Aug 12, 2026

Copy link
Copy Markdown
Contributor Author

Feel free to change, I am happy that it's tested right now and still readable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants