Compare commits

..

6 Commits

Author SHA1 Message Date
Andri Yngvason 2d94b8c4aa HACK 2023-04-11 19:51:49 +00:00
Andri Yngvason 1f84086ecd Add performance report timer 2023-04-11 19:51:40 +00:00
Andri Yngvason 443fd9f634 Create NTP processing module 2023-04-11 19:49:40 +00:00
Andri Yngvason ea17c642e8 rfbproto: Add NTP messages 2023-04-09 11:00:03 +00:00
Andri Yngvason 9fd5caaa4b rfbproto: Add #define for rfbEncodingPts 2023-04-09 09:15:12 +00:00
Andri Yngvason 82080db09a .gitignore: Add a place to stash files 2023-04-09 09:07:04 +00:00
18 changed files with 441 additions and 723 deletions

2
.gitignore vendored
View File

@ -1,4 +1,4 @@
build*
subprojects
.clang_complete
.vscode
sandbox

View File

@ -1,24 +0,0 @@
#include "wlr-data-control-unstable-v1.h"
#include "seat.h"
typedef void (*vnc_write_clipboard_t)(char* text, size_t size);
struct data_control {
struct wl_display* wl_display;
struct nvnc* server;
struct zwlr_data_control_manager_v1* manager;
struct zwlr_data_control_device_v1* device;
struct zwlr_data_control_source_v1* selection;
struct zwlr_data_control_source_v1* primary_selection;
struct zwlr_data_control_offer_v1* offer;
const char* mime_type;
char* cb_data;
size_t cb_len;
vnc_write_clipboard_t vnc_write_clipboard;
};
void data_control_init(struct data_control* self, struct seat* seat, struct zwlr_data_control_manager_v1 *manager);
void data_control_to_clipboard(struct data_control* self, const char* text, size_t len);
void data_control_destroy(struct data_control* self);
#pragma once

59
include/ntp.h 100644
View File

@ -0,0 +1,59 @@
/*
* Copyright (c) 2023 Andri Yngvason
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
#pragma once
#include <stdint.h>
#include <stdbool.h>
#define NTP_SAMPLE_PERIOD 1000000 // µs
#define NTP_MIN_SAMPLE_COUNT 3
#define NTP_SAMPLE_SIZE 16
struct aml_ticker;
struct ntp_client;
typedef void (*ntp_client_ping_fn)(struct ntp_client*, uint32_t t0, uint32_t t1,
uint32_t t2, uint32_t t3);
struct ntp_sample {
int32_t theta;
uint32_t delta;
};
struct ntp_client {
struct ntp_sample samples[NTP_SAMPLE_SIZE];
int sample_index;
int sample_count;
struct aml_ticker* ping_ticker;
ntp_client_ping_fn send_ping;
void* userdata;
};
int ntp_client_init(struct ntp_client*, ntp_client_ping_fn send_ping,
void* userdata);
void ntp_client_deinit(struct ntp_client*);
void ntp_client_process_pong(struct ntp_client*, uint32_t t0, uint32_t t1,
uint32_t t2, uint32_t t3);
bool ntp_client_get_best_sample(const struct ntp_client* self,
struct ntp_sample* sample);
bool ntp_client_translate_server_time(const struct ntp_client* self,
uint32_t* dst, const uint32_t t);

View File

@ -0,0 +1,30 @@
#pragma once
#define PERF_FRAME_LATENCY_SAMPLE_SIZE 60
struct perf_sample_stats {
double min, max, average;
};
struct perf_sample_buffer {
int length;
int count;
int index;
double samples[];
};
struct perf {
struct perf_sample_buffer* frame_latency;
};
struct perf_sample_buffer* perf_sample_buffer_create(int length);
void perf_sample_buffer_add(struct perf_sample_buffer* self, double sample);
void perf_sample_buffer_get_stats(const struct perf_sample_buffer* self,
struct perf_sample_stats* stats);
void perf_init(struct perf*);
void perf_deinit(struct perf*);
void perf_dump_latency_report(const struct perf*);

View File

@ -225,6 +225,9 @@ typedef char* (*GetUserProc)(struct _rfbClient* client);
typedef char* (*GetSASLMechanismProc)(struct _rfbClient* client, char* mechlist);
#endif /* LIBVNCSERVER_HAVE_SASL */
typedef void (*NtpEventProc)(struct _rfbClient* client, uint32_t t0,
uint32_t t1, uint32_t t2, uint32_t t3);
typedef struct _rfbClient {
uint8_t* frameBuffer;
int width, height;
@ -467,6 +470,8 @@ typedef struct _rfbClient {
StartingFrameBufferUpdateProc StartingFrameBufferUpdate;
CancelledFrameBufferUpdateProc CancelledFrameBufferUpdate;
NtpEventProc NtpEvent;
} rfbClient;
/* cursor.c */
@ -612,6 +617,9 @@ extern rfbBool HandleRFBServerMessage(rfbClient* client);
extern rfbBool ReadToBuffer(rfbClient* client);
extern rfbBool SendClientNtpEvent(rfbClient* client, uint32_t t0, uint32_t t1,
uint32_t t2, uint32_t t3);
/**
* Sends a text chat message to the server.
* @param client The client through which to send the message

View File

@ -398,8 +398,7 @@ typedef struct {
#define rfbSetDesktopSize 251
#define rfbQemuEvent 255
#define rfbNtpEvent 160
/*****************************************************************************
*
@ -498,6 +497,8 @@ typedef struct {
#define rfbEncodingSupportedEncodings 0xFFFE0002
#define rfbEncodingServerIdentity 0xFFFE0003
#define rfbEncodingPts -1000
#define rfbEncodingNtp -1001
/*****************************************************************************
*
@ -1138,6 +1139,12 @@ typedef struct rfbExtDesktopScreen {
uint32_t flags;
} rfbExtDesktopScreen;
struct rfbNtpMsg {
uint8_t type;
uint8_t padding[3];
uint32_t t0, t1, t2, t3;
};
#define sz_rfbExtDesktopSizeMsg (4)
#define sz_rfbExtDesktopScreen (16)
@ -1228,6 +1235,7 @@ typedef union {
rfbTextChatMsg tc;
rfbXvpMsg xvp;
rfbExtDesktopSizeMsg eds;
struct rfbNtpMsg ntp;
} rfbServerToClientMsg;

View File

@ -17,7 +17,6 @@
#pragma once
#include "rfbclient.h"
#include "data-control.h"
#include <stdbool.h>
#include <unistd.h>
@ -37,7 +36,6 @@ struct vnc_av_frame {
struct vnc_client {
rfbClient* client;
struct data_control* data_control;
struct open_h264* open_h264;
bool current_rect_is_av_frame;
struct vnc_av_frame* av_frames[VNC_CLIENT_MAX_AV_FRAMES];
@ -47,6 +45,8 @@ struct vnc_client {
int (*alloc_fb)(struct vnc_client*);
void (*update_fb)(struct vnc_client*);
void (*cut_text)(struct vnc_client*, const char*, size_t);
void (*ntp_event)(struct vnc_client*, uint32_t t0, uint32_t t1,
uint32_t t2, uint32_t t3);
void* userdata;
struct pixman_region16 damage;
@ -55,7 +55,7 @@ struct vnc_client {
bool is_updating;
};
struct vnc_client* vnc_client_create(struct data_control* data_control);
struct vnc_client* vnc_client_create(void);
void vnc_client_destroy(struct vnc_client* self);
int vnc_client_connect(struct vnc_client* self, const char* address, int port);
@ -81,5 +81,5 @@ void vnc_client_set_compression_level(struct vnc_client* self, int value);
void vnc_client_send_cut_text(struct vnc_client* self, const char* text,
size_t len);
void vnc_client_clear_av_frames(struct vnc_client* self);
rfbCredential* handle_vnc_authentication(struct _rfbClient *client, int credentialType);
void cut_text (struct vnc_client* self, const char* text, size_t size);
void vnc_client_send_ntp_event(struct vnc_client* self, uint32_t t0,
uint32_t t1, uint32_t t2, uint32_t t3);

View File

@ -70,7 +70,6 @@ sources = [
'src/pointer.c',
'src/keyboard.c',
'src/vnc.c',
'src/data-control.c',
'src/strlcpy.c',
'src/evdev-to-qnum.c',
'src/pixels.c',
@ -83,6 +82,8 @@ sources = [
'src/rfbproto.c',
'src/sockets.c',
'src/vncviewer.c',
'src/ntp.c',
'src/performance.c',
]
dependencies = [

View File

@ -16,7 +16,6 @@ wayland_scanner_client = generator(
client_protocols = [
'xdg-shell.xml',
'linux-dmabuf-unstable-v1.xml',
'wlr-data-control-unstable-v1.xml'
]
client_protos_src = []

View File

@ -1,278 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<protocol name="wlr_data_control_unstable_v1">
<copyright>
Copyright © 2018 Simon Ser
Copyright © 2019 Ivan Molodetskikh
Permission to use, copy, modify, distribute, and sell this
software and its documentation for any purpose is hereby granted
without fee, provided that the above copyright notice appear in
all copies and that both that copyright notice and this permission
notice appear in supporting documentation, and that the name of
the copyright holders not be used in advertising or publicity
pertaining to distribution of the software without specific,
written prior permission. The copyright holders make no
representations about the suitability of this software for any
purpose. It is provided "as is" without express or implied
warranty.
THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
THIS SOFTWARE.
</copyright>
<description summary="control data devices">
This protocol allows a privileged client to control data devices. In
particular, the client will be able to manage the current selection and take
the role of a clipboard manager.
Warning! The protocol described in this file is experimental and
backward incompatible changes may be made. Backward compatible changes
may be added together with the corresponding interface version bump.
Backward incompatible changes are done by bumping the version number in
the protocol and interface names and resetting the interface version.
Once the protocol is to be declared stable, the 'z' prefix and the
version number in the protocol and interface names are removed and the
interface version number is reset.
</description>
<interface name="zwlr_data_control_manager_v1" version="2">
<description summary="manager to control data devices">
This interface is a manager that allows creating per-seat data device
controls.
</description>
<request name="create_data_source">
<description summary="create a new data source">
Create a new data source.
</description>
<arg name="id" type="new_id" interface="zwlr_data_control_source_v1"
summary="data source to create"/>
</request>
<request name="get_data_device">
<description summary="get a data device for a seat">
Create a data device that can be used to manage a seat's selection.
</description>
<arg name="id" type="new_id" interface="zwlr_data_control_device_v1"/>
<arg name="seat" type="object" interface="wl_seat"/>
</request>
<request name="destroy" type="destructor">
<description summary="destroy the manager">
All objects created by the manager will still remain valid, until their
appropriate destroy request has been called.
</description>
</request>
</interface>
<interface name="zwlr_data_control_device_v1" version="2">
<description summary="manage a data device for a seat">
This interface allows a client to manage a seat's selection.
When the seat is destroyed, this object becomes inert.
</description>
<request name="set_selection">
<description summary="copy data to the selection">
This request asks the compositor to set the selection to the data from
the source on behalf of the client.
The given source may not be used in any further set_selection or
set_primary_selection requests. Attempting to use a previously used
source is a protocol error.
To unset the selection, set the source to NULL.
</description>
<arg name="source" type="object" interface="zwlr_data_control_source_v1"
allow-null="true"/>
</request>
<request name="destroy" type="destructor">
<description summary="destroy this data device">
Destroys the data device object.
</description>
</request>
<event name="data_offer">
<description summary="introduce a new wlr_data_control_offer">
The data_offer event introduces a new wlr_data_control_offer object,
which will subsequently be used in either the
wlr_data_control_device.selection event (for the regular clipboard
selections) or the wlr_data_control_device.primary_selection event (for
the primary clipboard selections). Immediately following the
wlr_data_control_device.data_offer event, the new data_offer object
will send out wlr_data_control_offer.offer events to describe the MIME
types it offers.
</description>
<arg name="id" type="new_id" interface="zwlr_data_control_offer_v1"/>
</event>
<event name="selection">
<description summary="advertise new selection">
The selection event is sent out to notify the client of a new
wlr_data_control_offer for the selection for this device. The
wlr_data_control_device.data_offer and the wlr_data_control_offer.offer
events are sent out immediately before this event to introduce the data
offer object. The selection event is sent to a client when a new
selection is set. The wlr_data_control_offer is valid until a new
wlr_data_control_offer or NULL is received. The client must destroy the
previous selection wlr_data_control_offer, if any, upon receiving this
event.
The first selection event is sent upon binding the
wlr_data_control_device object.
</description>
<arg name="id" type="object" interface="zwlr_data_control_offer_v1"
allow-null="true"/>
</event>
<event name="finished">
<description summary="this data control is no longer valid">
This data control object is no longer valid and should be destroyed by
the client.
</description>
</event>
<!-- Version 2 additions -->
<event name="primary_selection" since="2">
<description summary="advertise new primary selection">
The primary_selection event is sent out to notify the client of a new
wlr_data_control_offer for the primary selection for this device. The
wlr_data_control_device.data_offer and the wlr_data_control_offer.offer
events are sent out immediately before this event to introduce the data
offer object. The primary_selection event is sent to a client when a
new primary selection is set. The wlr_data_control_offer is valid until
a new wlr_data_control_offer or NULL is received. The client must
destroy the previous primary selection wlr_data_control_offer, if any,
upon receiving this event.
If the compositor supports primary selection, the first
primary_selection event is sent upon binding the
wlr_data_control_device object.
</description>
<arg name="id" type="object" interface="zwlr_data_control_offer_v1"
allow-null="true"/>
</event>
<request name="set_primary_selection" since="2">
<description summary="copy data to the primary selection">
This request asks the compositor to set the primary selection to the
data from the source on behalf of the client.
The given source may not be used in any further set_selection or
set_primary_selection requests. Attempting to use a previously used
source is a protocol error.
To unset the primary selection, set the source to NULL.
The compositor will ignore this request if it does not support primary
selection.
</description>
<arg name="source" type="object" interface="zwlr_data_control_source_v1"
allow-null="true"/>
</request>
<enum name="error" since="2">
<entry name="used_source" value="1"
summary="source given to set_selection or set_primary_selection was already used before"/>
</enum>
</interface>
<interface name="zwlr_data_control_source_v1" version="1">
<description summary="offer to transfer data">
The wlr_data_control_source object is the source side of a
wlr_data_control_offer. It is created by the source client in a data
transfer and provides a way to describe the offered data and a way to
respond to requests to transfer the data.
</description>
<enum name="error">
<entry name="invalid_offer" value="1"
summary="offer sent after wlr_data_control_device.set_selection"/>
</enum>
<request name="offer">
<description summary="add an offered MIME type">
This request adds a MIME type to the set of MIME types advertised to
targets. Can be called several times to offer multiple types.
Calling this after wlr_data_control_device.set_selection is a protocol
error.
</description>
<arg name="mime_type" type="string"
summary="MIME type offered by the data source"/>
</request>
<request name="destroy" type="destructor">
<description summary="destroy this source">
Destroys the data source object.
</description>
</request>
<event name="send">
<description summary="send the data">
Request for data from the client. Send the data as the specified MIME
type over the passed file descriptor, then close it.
</description>
<arg name="mime_type" type="string" summary="MIME type for the data"/>
<arg name="fd" type="fd" summary="file descriptor for the data"/>
</event>
<event name="cancelled">
<description summary="selection was cancelled">
This data source is no longer valid. The data source has been replaced
by another data source.
The client should clean up and destroy this data source.
</description>
</event>
</interface>
<interface name="zwlr_data_control_offer_v1" version="1">
<description summary="offer to transfer data">
A wlr_data_control_offer represents a piece of data offered for transfer
by another client (the source client). The offer describes the different
MIME types that the data can be converted to and provides the mechanism
for transferring the data directly from the source client.
</description>
<request name="receive">
<description summary="request that the data is transferred">
To transfer the offered data, the client issues this request and
indicates the MIME type it wants to receive. The transfer happens
through the passed file descriptor (typically created with the pipe
system call). The source client writes the data in the MIME type
representation requested and then closes the file descriptor.
The receiving client reads from the read end of the pipe until EOF and
then closes its end, at which point the transfer is complete.
This request may happen multiple times for different MIME types.
</description>
<arg name="mime_type" type="string"
summary="MIME type desired by receiver"/>
<arg name="fd" type="fd" summary="file descriptor for data transfer"/>
</request>
<request name="destroy" type="destructor">
<description summary="destroy this offer">
Destroys the data offer object.
</description>
</request>
<event name="offer">
<description summary="advertise offered MIME type">
Sent immediately after creating the wlr_data_control_offer object.
One event per offered MIME type.
</description>
<arg name="mime_type" type="string" summary="offered MIME type"/>
</event>
</interface>
</protocol>

View File

@ -1,308 +0,0 @@
#include "data-control.h"
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <assert.h>
#include <errno.h>
#include <signal.h>
#include <getopt.h>
#include <aml.h>
#include <seat.h>
#include <vnc.h>
struct receive_context {
//struct data_control* data_control;
struct zwlr_data_control_offer_v1* offer;
int fd;
FILE* mem_fp;
size_t mem_size;
char* mem_data;
struct data_control* self;
};
static char* mime_type = "text/plain;charset=utf-8";
static bool isStringEqual(int lena, char* a, int lenb, char* b) {
if (lena != lenb) {
return false;
}
// Compare every character
for (size_t i = 0; i < lena; ++i) {
if (a[i] != b[i]) {
return false;
}
}
return true;
}
static void on_receive(void* handler)
{
struct receive_context* ctx = aml_get_userdata(handler);
int fd = aml_get_fd(handler);
assert(ctx->fd == fd);
char buf[4096];
ssize_t ret = read(fd, &buf, sizeof(buf));
if (ret > 0) {
fwrite(&buf, 1, ret, ctx->mem_fp);
return;
}
fclose(ctx->mem_fp);
ctx->mem_fp = NULL;
if (ctx->mem_size && ctx->self->vnc_write_clipboard) {
// If we receive clipboard data from the VNC server, we set the clipboard of the client.
// This "change" of clipboard data results into this "on_receive" event. That would leed
// to an endless loop...
// So we check if we send exactle that clipboard string to avoid an loop
if (!ctx->self->cb_data || !isStringEqual(ctx->self->cb_len, ctx->self->cb_data, strlen(ctx->mem_data), ctx->mem_data)) {
//printf("Received data FROM clipboard: %s\n", ctx->mem_data);
ctx->self->vnc_write_clipboard(ctx->mem_data, ctx->mem_size);
}
}
aml_stop(aml_get_default(), handler);
}
static void destroy_receive_context(void* raw_ctx)
{
struct receive_context* ctx = raw_ctx;
int fd = ctx->fd;
if (ctx->mem_fp)
fclose(ctx->mem_fp);
free(ctx->mem_data);
zwlr_data_control_offer_v1_destroy(ctx->offer);
close(fd);
free(ctx);
}
static void receive_data(void* data,
struct zwlr_data_control_offer_v1* offer)
{
struct data_control* self = data;
int pipe_fd[2];
if (pipe(pipe_fd) == -1) {
printf("pipe() failed");
return;
}
struct receive_context* ctx = calloc(1, sizeof(*ctx));
if (!ctx) {
printf("OOM");
close(pipe_fd[0]);
close(pipe_fd[1]);
return;
}
zwlr_data_control_offer_v1_receive(self->offer, mime_type, pipe_fd[1]);
//wl_display_flush(self->wl_display);
close(pipe_fd[1]);
ctx->self = self;
ctx->fd = pipe_fd[0];
//ctx->data_control = self;
ctx->offer = self->offer;
ctx->mem_fp = open_memstream(&ctx->mem_data, &ctx->mem_size);
if (!ctx->mem_fp) {
close(ctx->fd);
free(ctx);
printf("open_memstream() failed");
return;
}
struct aml_handler* handler = aml_handler_new(ctx->fd, on_receive,
ctx, destroy_receive_context);
if (!handler) {
close(ctx->fd);
free(ctx);
return;
}
aml_start(aml_get_default(), handler);
aml_unref(handler);
}
static void data_control_offer(void* data,
struct zwlr_data_control_offer_v1* zwlr_data_control_offer_v1,
const char* mime_type)
{
struct data_control* self = data;
if (self->offer)
return;
if (strcmp(mime_type, mime_type) != 0) {
return;
}
self->offer = zwlr_data_control_offer_v1;
}
struct zwlr_data_control_offer_v1_listener data_control_offer_listener = {
data_control_offer
};
static void data_control_device_offer(void* data,
struct zwlr_data_control_device_v1* zwlr_data_control_device_v1,
struct zwlr_data_control_offer_v1* id)
{
if (!id)
return;
zwlr_data_control_offer_v1_add_listener(id, &data_control_offer_listener, data);
}
static void data_control_device_selection(void* data,
struct zwlr_data_control_device_v1* zwlr_data_control_device_v1,
struct zwlr_data_control_offer_v1* id)
{
struct data_control* self = data;
if (id && self->offer == id) {
receive_data(data, id);
self->offer = NULL;
}
}
static void data_control_device_primary_selection(void* data,
struct zwlr_data_control_device_v1* zwlr_data_control_device_v1,
struct zwlr_data_control_offer_v1* id)
{
struct data_control* self = data;
if (id && self->offer == id) {
receive_data(data, id);
self->offer = NULL;
return;
}
}
static void data_control_device_finished(void* data,
struct zwlr_data_control_device_v1* zwlr_data_control_device_v1)
{
zwlr_data_control_device_v1_destroy(zwlr_data_control_device_v1);
}
static struct zwlr_data_control_device_v1_listener data_control_device_listener = {
.data_offer = data_control_device_offer,
.selection = data_control_device_selection,
.finished = data_control_device_finished,
.primary_selection = data_control_device_primary_selection
};
static void
data_control_source_send(void* data,
struct zwlr_data_control_source_v1* zwlr_data_control_source_v1,
const char* mime_type,
int32_t fd)
{
struct data_control* self = data;
char* d = self->cb_data;
size_t len = self->cb_len;
int ret;
assert(d);
ret = write(fd, d, len);
if (ret < (int)len)
printf("write from clipboard incomplete");
close(fd);
}
static void data_control_source_cancelled(void* data,
struct zwlr_data_control_source_v1* zwlr_data_control_source_v1)
{
struct data_control* self = data;
if (self->selection == zwlr_data_control_source_v1) {
self->selection = NULL;
}
if (self->primary_selection == zwlr_data_control_source_v1) {
self->primary_selection = NULL;
}
zwlr_data_control_source_v1_destroy(zwlr_data_control_source_v1);
}
struct zwlr_data_control_source_v1_listener data_control_source_listener = {
.send = data_control_source_send,
.cancelled = data_control_source_cancelled
};
static struct zwlr_data_control_source_v1* set_selection(struct data_control* self, bool primary) {
struct zwlr_data_control_source_v1* selection;
selection = zwlr_data_control_manager_v1_create_data_source(self->manager);
if (selection == NULL) {
printf("zwlr_data_control_manager_v1_create_data_source() failed");
free(self->cb_data);
self->cb_data = NULL;
return NULL;
}
zwlr_data_control_source_v1_add_listener(selection, &data_control_source_listener, self);
zwlr_data_control_source_v1_offer(selection, mime_type);
if (primary)
zwlr_data_control_device_v1_set_primary_selection(self->device, selection);
else
zwlr_data_control_device_v1_set_selection(self->device, selection);
return selection;
}
void data_control_init(struct data_control* self, struct seat* seat, struct zwlr_data_control_manager_v1 *manager) {
self->manager = manager;
self->device = zwlr_data_control_manager_v1_get_data_device(self->manager, seat->wl_seat);
self->cb_data = NULL;
self->cb_len = 0;
zwlr_data_control_device_v1_add_listener(self->device, &data_control_device_listener, self);
}
void data_control_to_clipboard(struct data_control* self, const char* text, size_t len)
{
//printf("Writing text TO CLIPBOARD: %s\n", text);
if (!len) {
printf("%s called with 0 length", __func__);
return;
}
if (self->cb_data) {
free(self->cb_data);
}
self->cb_data = malloc(len);
if (!self->cb_data) {
printf("OOM");
return;
}
memcpy(self->cb_data, text, len);
self->cb_len = len;
// Set copy/paste buffer
self->selection = set_selection(self, false);
// Set highlight/middle_click buffer
self->primary_selection = set_selection(self, true);
}
void data_control_destroy(struct data_control* self)
{
if (self->selection) {
zwlr_data_control_source_v1_destroy(self->selection);
self->selection = NULL;
}
if (self->primary_selection) {
zwlr_data_control_source_v1_destroy(self->primary_selection);
self->primary_selection = NULL;
}
zwlr_data_control_device_v1_destroy(self->device);
free(self->cb_data);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 - 2022 Andri Yngvason
* Copyright (c) 2020 - 2023 Andri Yngvason
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
@ -47,10 +47,12 @@
#include "linux-dmabuf-unstable-v1.h"
#include "time-util.h"
#include "output.h"
#include "data-control.h"
#include "ntp.h"
#include "performance.h"
#define CANARY_TICK_PERIOD INT64_C(100000) // us
#define CANARY_LETHALITY_LEVEL INT64_C(8000) // us
#define LATENCY_REPORT_PERIOD INT64_C(250000) // us
struct point {
double x, y;
@ -88,9 +90,8 @@ struct pointer_collection* pointers;
struct keyboard_collection* keyboards;
static int drm_fd = -1;
static uint64_t last_canary_tick;
static struct data_control* data_control;
static struct zwlr_data_control_manager_v1 *manager;
static struct vnc_client* vnc;
static struct ntp_client ntp;
static struct perf perf;
static bool have_egl = false;
@ -118,9 +119,6 @@ static void on_seat_capability_change(struct seat* seat)
struct wl_keyboard* wl_keyboard =
wl_seat_get_keyboard(seat->wl_seat);
keyboard_collection_add_wl_keyboard(keyboards, wl_keyboard);
data_control = malloc(sizeof(data_control));
data_control_init(data_control, seat, manager);
} else {
// TODO Remove
}
@ -162,8 +160,6 @@ static void registry_add(void* data, struct wl_registry* registry, uint32_t id,
}
wl_list_insert(&outputs, &output->link);
} else if (strcmp(interface, zwlr_data_control_manager_v1_interface.name) == 0) {
manager = wl_registry_bind(registry, id, &zwlr_data_control_manager_v1_interface, 2);
}
}
@ -617,6 +613,20 @@ static void window_damage_region(struct window* w,
}
}
static void update_frame_latency_stats(void)
{
uint32_t server_pts = window->vnc->pts;
uint32_t client_pts = 0;
if (!ntp_client_translate_server_time(&ntp, &client_pts, server_pts))
return;
uint32_t now = gettime_us();
int32_t latency = (int32_t)(now - client_pts);
perf_sample_buffer_add(perf.frame_latency, latency);
}
static void render_from_vnc(void)
{
if (!pixman_region_not_empty(&window->current_damage) &&
@ -662,6 +672,8 @@ static void render_from_vnc(void)
window_commit(window);
window_swap(window);
update_frame_latency_stats();
pixman_region_clear(&window->current_damage);
vnc_client_clear_av_frames(window->vnc);
}
@ -813,6 +825,18 @@ static void on_canary_tick(void* obj)
delay);
}
static void send_ntp_ping(struct ntp_client* ntp_client, uint32_t t0,
uint32_t t1, uint32_t t2, uint32_t t3)
{
vnc_client_send_ntp_event(window->vnc, t0, t1, t2, t3);
}
static void on_ntp_event(struct vnc_client* vnc, uint32_t t0, uint32_t t1,
uint32_t t2, uint32_t t3)
{
ntp_client_process_pong(&ntp, t0, t1, t2, t3);
}
static void create_canary_ticker(void)
{
last_canary_tick = gettime_us();
@ -824,8 +848,20 @@ static void create_canary_ticker(void)
aml_unref(ticker);
}
static void vnc_send_clipboard(char* text, size_t size) {
vnc_client_send_cut_text(vnc, text, size);
static void on_latency_report_tick(void* handler)
{
(void)handler;
perf_dump_latency_report(&perf);
}
static void create_latency_report_ticker(void)
{
struct aml* aml = aml_get_default();
struct aml_ticker* ticker = aml_ticker_new(LATENCY_REPORT_PERIOD,
on_latency_report_tick, NULL, NULL);
aml_start(aml, ticker);
aml_unref(ticker);
}
void run_main_loop_once(void)
@ -977,13 +1013,15 @@ int main(int argc, char* argv[])
wl_display_roundtrip(wl_display);
wl_display_roundtrip(wl_display);
vnc = vnc_client_create(data_control);
struct vnc_client* vnc = vnc_client_create();
if (!vnc)
goto vnc_failure;
vnc->userdata = window;
vnc->alloc_fb = on_vnc_client_alloc_fb;
vnc->update_fb = on_vnc_client_update_fb;
data_control->vnc_write_clipboard = vnc_send_clipboard;
vnc->ntp_event = on_ntp_event;
if (vnc_client_set_pixel_format(vnc, shm_format) < 0) {
fprintf(stderr, "Unsupported pixel format\n");
@ -1022,12 +1060,16 @@ int main(int argc, char* argv[])
goto vnc_setup_failure;
}
perf_init(&perf);
ntp_client_init(&ntp, send_ntp_ping, window);
pointers->userdata = vnc;
keyboards->userdata = vnc;
wl_display_dispatch(wl_display);
create_canary_ticker();
create_latency_report_ticker();
while (do_run)
run_main_loop_once();
@ -1035,6 +1077,10 @@ int main(int argc, char* argv[])
rc = 0;
if (window)
window_destroy(window);
ntp_client_deinit(&ntp);
perf_deinit(&perf);
vnc_setup_failure:
vnc_client_destroy(vnc);
vnc_failure:
@ -1063,9 +1109,5 @@ display_failure:
signal_handler_failure:
aml_unref(aml);
printf("Exiting...\n");
// @TODO this will throw an segfault (can't determine proxy version, but why?)
if (data_control)
data_control_destroy(data_control);
return rc;
}

120
src/ntp.c 100644
View File

@ -0,0 +1,120 @@
/*
* Copyright (c) 2023 Andri Yngvason
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
#include "ntp.h"
#include "time-util.h"
#include <assert.h>
#include <aml.h>
#include <stdio.h>
static inline int32_t clamp_to_zero(int32_t v)
{
return v >= 0 ? v : 0;
}
void ntp_client__tick(void* handler)
{
struct ntp_client* self = aml_get_userdata(handler);
assert(self);
assert(self->send_ping);
uint32_t t0 = gettime_us();
self->send_ping(self, t0, 0, 0, 0);
}
int ntp_client_init(struct ntp_client* self, ntp_client_ping_fn send_ping,
void* userdata)
{
self->send_ping = send_ping;
self->userdata = userdata;
struct aml_ticker* ticker = aml_ticker_new(NTP_SAMPLE_PERIOD,
ntp_client__tick, self, NULL);
if (!ticker)
return -1;
int rc = aml_start(aml_get_default(), ticker);
if (rc >= 0)
self->ping_ticker = ticker;
else
aml_unref(ticker);
return rc;
}
void ntp_client_deinit(struct ntp_client* self)
{
if (self->ping_ticker) {
aml_stop(aml_get_default(), self->ping_ticker);
aml_unref(self->ping_ticker);
}
}
void ntp_client_process_pong(struct ntp_client* self, uint32_t t0, uint32_t t1,
uint32_t t2, uint32_t t3)
{
t3 = gettime_us();
int32_t theta = ((int32_t)(t1 - t0) + (int32_t)(t2 - t3)) / 2;
uint32_t delta = clamp_to_zero((int32_t)(t3 - t0) - (int32_t)(t2 - t1));
struct ntp_sample sample = {
.theta = theta,
.delta = delta,
};
self->samples[self->sample_index] = sample;
self->sample_index = (self->sample_index + 1) % NTP_SAMPLE_SIZE;
if (self->sample_count < NTP_SAMPLE_SIZE)
self->sample_count++;
// printf("%.3f %.3f\n", delta / 1e3, theta / 1e3);
}
bool ntp_client_get_best_sample(const struct ntp_client* self,
struct ntp_sample* out)
{
if (self->sample_count < NTP_MIN_SAMPLE_COUNT)
return false;
struct ntp_sample result = {
.theta = 0,
.delta = UINT32_MAX,
};
for (int i = 0; i < self->sample_count; ++i) {
const struct ntp_sample *sample = &self->samples[i];
if (sample->delta < result.delta) {
result = *sample;
}
}
*out = result;
return true;
}
bool ntp_client_translate_server_time(const struct ntp_client* self,
uint32_t* dst, const uint32_t t)
{
struct ntp_sample sample;
if (!ntp_client_get_best_sample(self, &sample))
return false;
*dst = (int32_t)t - sample.theta;
return true;
}

View File

@ -153,9 +153,6 @@ static void reset_all_contexts(struct open_h264* self)
struct open_h264* open_h264_create(rfbClient* client)
{
// Use this to enable debug logs
// av_log_set_level(AV_LOG_DEBUG);
struct open_h264* self = calloc(1, sizeof(*self));
if (!self)
return NULL;

72
src/performance.c 100644
View File

@ -0,0 +1,72 @@
#include "performance.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
struct perf_sample_buffer* perf_sample_buffer_create(int length)
{
struct perf_sample_buffer* self = malloc(sizeof(*self)
+ length * sizeof(self->samples[0]));
self->count = 0;
self->index = 0;
self->length = length;
return self;
}
void perf_sample_buffer_add(struct perf_sample_buffer* self, double sample)
{
self->samples[self->index] = sample;
self->index = (self->index + 1) % self->length;
if (self->count < self->length)
self->count += 1;
}
void perf_sample_buffer_get_stats(const struct perf_sample_buffer* self,
struct perf_sample_stats* stats)
{
memset(stats, 0, sizeof(*stats));
double sum = 0;
double minimum = INFINITY;
double maximum = 0;
for (int i = 0; i < self->count; ++i) {
double sample = self->samples[i];
sum += sample;
if (sample < minimum)
minimum = sample;
if (sample > maximum)
maximum = sample;
}
stats->min = minimum;
stats->max = maximum;
stats->average = sum / (double)self->count;
}
void perf_init(struct perf* self)
{
memset(self, 0, sizeof(*self));
self->frame_latency =
perf_sample_buffer_create(PERF_FRAME_LATENCY_SAMPLE_SIZE);
}
void perf_deinit(struct perf* self)
{
free(self->frame_latency);
}
void perf_dump_latency_report(const struct perf* self)
{
struct perf_sample_stats stats;
perf_sample_buffer_get_stats(self->frame_latency, &stats);
printf("Latency report: frame-latency (min, avg, max): %.1f, %.1f, %.1f\n",
stats.min / 1e3, stats.average / 1e3, stats.max / 1e3);
}

View File

@ -524,34 +524,10 @@ static rfbBool ReadSupportedSecurityType(rfbClient* client, uint32_t* result,
static rfbBool HandleVncAuth(rfbClient* client)
{
uint8_t challenge[CHALLENGESIZE];
char* passwd = NULL;
int i;
if (!ReadFromRFBServer(client, (char*)challenge, CHALLENGESIZE))
return FALSE;
if (client->GetPassword)
passwd = client->GetPassword(client);
if ((!passwd) || (strlen(passwd) == 0)) {
rfbClientLog("Reading password failed\n");
return FALSE;
}
if (strlen(passwd) > 8) {
passwd[8] = '\0';
}
rfbClientEncryptBytes(challenge, passwd);
/* Lose the password from memory */
for (i = strlen(passwd); i >= 0; i--) {
passwd[i] = '\0';
}
free(passwd);
if (!WriteToRFBServer(client, (char*)challenge, CHALLENGESIZE))
return FALSE;
/* Handle the SecurityResult message */
if (!rfbHandleAuthResult(client))
return FALSE;
@ -1420,7 +1396,11 @@ rfbBool SetFormatAndEncodings(rfbClient* client)
/* pts */
if (se->nEncodings < MAX_ENCODINGS)
encs[se->nEncodings++] = rfbClientSwap32IfLE(-1000);
encs[se->nEncodings++] = rfbClientSwap32IfLE(rfbEncodingPts);
/* ntp */
if (se->nEncodings < MAX_ENCODINGS)
encs[se->nEncodings++] = rfbClientSwap32IfLE(rfbEncodingNtp);
len = sz_rfbSetEncodingsMsg + se->nEncodings * 4;
@ -1766,6 +1746,22 @@ rfbBool SendClientCutText(rfbClient* client, char* str, int len)
WriteToRFBServer(client, str, len));
}
rfbBool SendClientNtpEvent(rfbClient* client, uint32_t t0, uint32_t t1,
uint32_t t2, uint32_t t3)
{
// if (!SupportsClient2Server(client, rfbNtpEvent))
// return FALSE;
struct rfbNtpMsg msg = {
.type = rfbNtpEvent,
.t0 = rfbClientSwap32IfLE(t0),
.t1 = rfbClientSwap32IfLE(t1),
.t2 = rfbClientSwap32IfLE(t2),
.t3 = rfbClientSwap32IfLE(t3),
};
return WriteToRFBServer(client, (char*)&msg, sizeof(msg));
}
static rfbBool HandleFramebufferUpdate(rfbClient* client,
rfbServerToClientMsg* msg)
{
@ -2324,6 +2320,10 @@ static rfbBool HandleFramebufferUpdate(rfbClient* client,
SetClient2Server(client, rfbQemuEvent);
break;
case rfbEncodingNtp:
SetClient2Server(client, rfbNtpEvent);
break;
default: {
rfbBool handled = FALSE;
rfbClientProtocolExtension* e;
@ -2363,6 +2363,22 @@ failure:
return FALSE;
}
rfbBool handleNtpEvent(rfbClient* client, struct rfbNtpMsg* msg)
{
if (!ReadFromRFBServer(client, (char*)msg + 1, sizeof(*msg) - 1))
return FALSE;
uint32_t t0 = rfbClientSwap32IfLE(msg->t0);
uint32_t t1 = rfbClientSwap32IfLE(msg->t1);
uint32_t t2 = rfbClientSwap32IfLE(msg->t2);
uint32_t t3 = rfbClientSwap32IfLE(msg->t3);
if (client->NtpEvent)
client->NtpEvent(client, t0, t1, t2, t3);
return TRUE;
}
/*
* HandleRFBServerMessage.
*/
@ -2551,6 +2567,9 @@ rfbBool HandleRFBServerMessage(rfbClient* client)
break;
}
case rfbNtpEvent:
return handleNtpEvent(client, &msg.ntp);
default: {
rfbBool handled = FALSE;
rfbClientProtocolExtension* e;

View File

@ -25,7 +25,7 @@
#include <gnutls/x509.h>
#include <errno.h>
static const char *rfbTLSPriority = "NORMAL:+DHE-DSS:+RSA:+DHE-RSA";
static const char *rfbTLSPriority = "NORMAL:+DHE-DSS:+RSA:+DHE-RSA:+SRP";
static const char *rfbAnonTLSPriority= "NORMAL:+ANON-DH";
#define DH_BITS 1024
@ -112,13 +112,12 @@ verify_certificate_callback (gnutls_session_t session)
return GNUTLS_E_CERTIFICATE_ERROR;
}
// Hostname verification does NOT work
//if (!gnutls_x509_crt_check_hostname (cert, hostname))
// {
// rfbClientLog("The certificate's owner does not match hostname '%s'\n",
// hostname);
// return GNUTLS_E_CERTIFICATE_ERROR;
// }
if (!gnutls_x509_crt_check_hostname (cert, hostname))
{
rfbClientLog("The certificate's owner does not match hostname '%s'\n",
hostname);
return GNUTLS_E_CERTIFICATE_ERROR;
}
gnutls_x509_crt_deinit (cert);
@ -338,9 +337,6 @@ FreeX509Credential(rfbCredential *cred)
static gnutls_certificate_credentials_t
CreateX509CertCredential(rfbCredential *cred)
{
// Use this to enable debug logs
//gnutls_global_set_log_level(GNUTLS_DEBUG_LEVEL);
gnutls_certificate_credentials_t x509_cred;
int ret;

View File

@ -23,8 +23,6 @@
#include <pixman.h>
#include <libdrm/drm_fourcc.h>
#include <libavutil/frame.h>
#include <stdio.h>
#include <data-control.h>
#include "rfbclient.h"
#include "vnc.h"
@ -136,9 +134,16 @@ static void vnc_client_got_cut_text(rfbClient* client, const char* text,
if (self->cut_text)
self->cut_text(self, text, len);
else {
printf("Cut text is not defined!\n");
}
}
static void vnc_client_ntp_event(rfbClient* client, uint32_t t0, uint32_t t1,
uint32_t t2, uint32_t t3)
{
struct vnc_client* self = rfbClientGetClientData(client, NULL);
assert(self);
if (self->ntp_event)
self->ntp_event(self, t0, t1, t2, t3);
}
static rfbBool vnc_client_handle_open_h264_rect(rfbClient* client,
@ -221,7 +226,7 @@ static void vnc_client_init_pts_ext(void)
rfbClientRegisterExtension(&ext);
}
struct vnc_client* vnc_client_create(struct data_control* data_control)
struct vnc_client* vnc_client_create(void)
{
vnc_client_init_open_h264();
vnc_client_init_pts_ext();
@ -243,7 +248,6 @@ struct vnc_client* vnc_client_create(struct data_control* data_control)
goto failure;
self->client = client;
self->data_control = data_control;
rfbClientSetClientData(client, NULL, self);
client->MallocFrameBuffer = vnc_client_alloc_fb;
@ -252,13 +256,10 @@ struct vnc_client* vnc_client_create(struct data_control* data_control)
client->StartingFrameBufferUpdate = vnc_client_start_update;
client->CancelledFrameBufferUpdate = vnc_client_cancel_update;
client->GotXCutText = vnc_client_got_cut_text;
self->cut_text = cut_text;
client->NtpEvent = vnc_client_ntp_event;
self->pts = NO_PTS;
// Handle authentication
client->GetCredential = handle_vnc_authentication;
return self;
failure:
@ -266,36 +267,6 @@ failure:
return NULL;
}
rfbCredential* handle_vnc_authentication(struct _rfbClient *client, int credentialType) {
rfbCredential* creds = (rfbCredential*) malloc(sizeof(rfbCredential));
if (client->authScheme == rfbVeNCrypt && credentialType == rfbCredentialTypeX509) {
char* path = getenv("TLS_CA");
rfbClientLog("Using TLS CA certificate from env 'TLS_CA': %s\n", path);
creds->x509Credential.x509CACertFile = malloc(strlen(path) + 1);
strcpy(creds->x509Credential.x509CACertFile, path);
creds->x509Credential.x509CrlVerifyMode = rfbX509CrlVerifyAll;
} else if (client->authScheme == rfbVeNCrypt && credentialType == rfbCredentialTypeUser) {
const* username = getenv("VNC_USERNAME");
const* password = getenv("VNC_PASSWORD");
rfbClientLog("Using username and password for VNC authentication 'VNC_USERNAME', 'VNC_PASSWORD'\n");
creds->userCredential.password = malloc(strlen(password) + 1);
creds->userCredential.username = malloc(strlen(username) + 1);
strcpy(creds->userCredential.password, password);
strcpy(creds->userCredential.username, username);
} else {
}
return creds;
}
void cut_text (struct vnc_client* self, const char* text, size_t size) {
data_control_to_clipboard(self->data_control, text, size);
//printf("Received string FROM vnc_server: %s\n", text);
}
void vnc_client_destroy(struct vnc_client* self)
{
vnc_client_clear_av_frames(self);
@ -483,3 +454,9 @@ void vnc_client_send_cut_text(struct vnc_client* self, const char* text,
// libvncclient doesn't modify text, so typecast is OK.
SendClientCutText(self->client, (char*)text, len);
}
void vnc_client_send_ntp_event(struct vnc_client* self, uint32_t t0,
uint32_t t1, uint32_t t2, uint32_t t3)
{
SendClientNtpEvent(self->client, t0, t1, t2, t3);
}