Skip to content

Commit 04f5f6f

Browse files
committed
Implement VNC backend
Add VNC backend using `neatvnc`[1]. Allow users can view and interact with applications in Mado through VNC-compatible client software. [1]: https://github.com/any1/neatvnc Close #32
1 parent 980bd4c commit 04f5f6f

File tree

5 files changed

+313
-0
lines changed

5 files changed

+313
-0
lines changed

Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@ libtwin.a_files-y += backend/fbdev.c
110110
libtwin.a_files-y += backend/linux_input.c
111111
endif
112112

113+
ifeq ($(CONFIG_BACKEND_VNC), y)
114+
BACKEND = vnc
115+
libtwin.a_files-y += backend/vnc.c
116+
libtwin.a_cflags-y += $(shell pkg-config --cflags neatvnc aml pixman-1)
117+
TARGET_LIBS += $(shell pkg-config --libs neatvnc aml pixman-1)
118+
endif
119+
113120
# Standalone application
114121

115122
ifeq ($(CONFIG_DEMO_APPLICATIONS), y)

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ and the [SDL2 library](https://www.libsdl.org/).
7070
* macOS: `brew install sdl2 jpeg libpng`
7171
* Ubuntu Linux / Debian: `sudo apt install libsdl2-dev libjpeg-dev libpng-dev`
7272

73+
For those who want to run demo program with VNC as the backend, it is necessary to install the [aml](https://github.com/any1/aml) and [neatvnc](https://github.com/any1/neatvnc) libraries. Please note that using package managers like apt might install outdated versions of these libraries. To ensure you have the latest versions, you can build them from source by running the provided script:
74+
75+
```bash
76+
$sh build-neatvnc.sh
77+
```
78+
7379
### Configuration
7480

7581
Configure via [Kconfiglib](https://pypi.org/project/kconfiglib/), you should select either SDL
@@ -108,6 +114,14 @@ $ sudo usermod -a -G video $USERNAME
108114

109115
In addition, the framebuffer device can be assigned via the environment variable `FRAMEBUFFER`.
110116

117+
To run demo program with the neat-vnc backend:
118+
119+
```shell
120+
$./demo-vnc
121+
```
122+
123+
It would launch the vnc server. You could use any VNC client to connect with given IP address(default is "127.0.0.1") and port (default is 5900).
124+
111125
## License
112126

113127
`Mado` is available under a MIT-style license, permitting liberal commercial use.

backend/vnc.c

Lines changed: 271 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
1+
/*
2+
Twin - A Tiny Window System
3+
Copyright (c) 2024 National Cheng Kung University, Taiwan
4+
All rights reserved.
5+
*/
6+
#include <aml.h>
7+
#include <assert.h>
8+
#include <neatvnc.h>
9+
#include <pixman.h>
10+
#include <stdlib.h>
11+
#include <string.h>
12+
#include <twin.h>
13+
#include "twin_backend.h"
14+
#include "twin_private.h"
15+
16+
#define SCREEN(x) ((twin_context_t *) x)->screen
17+
#define PRIV(x) ((twin_vnc_t *) ((twin_context_t *) x)->priv)
18+
#define NEATVNC_HOST "NEATVNC_HOST"
19+
#define NEATVNC_PORT "NEATVNC_PORT"
20+
#define NEATVNC_HOST_DEFAULT "127.0.0.1"
21+
#define NEATVNC_PORT_DEFAULT "5900"
22+
23+
#ifndef DRM_FORMAT_ARGB8888
24+
#define fourcc_code(a, b, c, d) \
25+
((uint32_t) (a) | ((uint32_t) (b) << 8) | ((uint32_t) (c) << 16) | \
26+
((uint32_t) (d) << 24))
27+
#define DRM_FORMAT_ARGB8888 fourcc_code('A', 'R', '2', '4')
28+
#endif
29+
30+
typedef struct {
31+
twin_screen_t *screen;
32+
struct nvnc *server;
33+
struct nvnc_display *display;
34+
struct aml *aml;
35+
struct aml_handler *aml_handler;
36+
struct nvnc_fb *current_fb;
37+
struct pixman_region16 damage_region;
38+
uint32_t *framebuffer;
39+
int width;
40+
int height;
41+
} twin_vnc_t;
42+
43+
typedef struct {
44+
uint16_t px, py;
45+
enum nvnc_button_mask prev_button;
46+
} twin_peer_t;
47+
48+
struct fb_side_data {
49+
struct pixman_region16 damage;
50+
};
51+
52+
static void _twin_vnc_put_begin(twin_coord_t left,
53+
twin_coord_t top,
54+
twin_coord_t right,
55+
twin_coord_t bottom,
56+
void *closure)
57+
{
58+
twin_vnc_t *tx = PRIV(closure);
59+
pixman_region_init_rect(&tx->damage_region, 0, 0, tx->width, tx->height);
60+
}
61+
62+
static void _twin_vnc_put_span(twin_coord_t left,
63+
twin_coord_t top,
64+
twin_coord_t right,
65+
twin_argb32_t *pixels,
66+
void *closure)
67+
{
68+
twin_vnc_t *tx = PRIV(closure);
69+
uint32_t *fb_pixels = tx->framebuffer + top * tx->width + left;
70+
size_t span_width = right - left;
71+
72+
memcpy(fb_pixels, pixels, span_width * sizeof(uint32_t));
73+
74+
pixman_region_init_rect(&tx->damage_region, left, top, span_width, 1);
75+
76+
if (pixman_region_not_empty(&tx->damage_region)) {
77+
nvnc_display_feed_buffer(tx->display, tx->current_fb,
78+
&tx->damage_region);
79+
pixman_region_clear(&tx->damage_region);
80+
}
81+
aml_poll(tx->aml, 0);
82+
aml_dispatch(tx->aml);
83+
}
84+
85+
static void twin_vnc_get_screen_size(twin_vnc_t *tx, int *width, int *height)
86+
{
87+
*width = nvnc_fb_get_width(tx->current_fb);
88+
*height = nvnc_fb_get_height(tx->current_fb);
89+
}
90+
91+
static bool _twin_vnc_work(void *closure)
92+
{
93+
twin_screen_t *screen = SCREEN(closure);
94+
95+
if (twin_screen_damaged(screen))
96+
twin_screen_update(screen);
97+
return true;
98+
}
99+
100+
static void _twin_vnc_new_client(struct nvnc_client *client)
101+
{
102+
twin_peer_t *peer = malloc(sizeof(twin_peer_t));
103+
nvnc_set_userdata(client, peer, NULL);
104+
}
105+
106+
static bool _twin_vnc_read_events(int fd, twin_file_op_t op, void *closure)
107+
{
108+
return true;
109+
}
110+
111+
static void _twin_vnc_pointer_event(struct nvnc_client *client,
112+
uint16_t x,
113+
uint16_t y,
114+
enum nvnc_button_mask button)
115+
{
116+
twin_peer_t *peer = nvnc_get_userdata(client);
117+
twin_event_t tev;
118+
if ((button & NVNC_BUTTON_LEFT) &&
119+
!(peer->prev_button & NVNC_BUTTON_LEFT)) {
120+
tev.u.pointer.screen_x = x;
121+
tev.u.pointer.screen_y = y;
122+
tev.kind = TwinEventButtonDown;
123+
tev.u.pointer.button = 1;
124+
} else if (!(button & NVNC_BUTTON_LEFT) &&
125+
(peer->prev_button & NVNC_BUTTON_LEFT)) {
126+
tev.u.pointer.screen_x = x;
127+
tev.u.pointer.screen_y = y;
128+
tev.kind = TwinEventButtonUp;
129+
tev.u.pointer.button = 1;
130+
}
131+
if ((peer->px != x || peer->py != y)) {
132+
peer->px = x;
133+
peer->py = y;
134+
tev.u.pointer.screen_x = x;
135+
tev.u.pointer.screen_y = y;
136+
tev.u.pointer.button = 0;
137+
tev.kind = TwinEventMotion;
138+
}
139+
peer->prev_button = button;
140+
struct nvnc *server = nvnc_client_get_server(client);
141+
twin_vnc_t *tx = nvnc_get_userdata(server);
142+
twin_screen_dispatch(tx->screen, &tev);
143+
}
144+
145+
twin_context_t *twin_vnc_init(int width, int height)
146+
{
147+
twin_context_t *ctx = calloc(1, sizeof(twin_context_t));
148+
if (!ctx)
149+
return NULL;
150+
ctx->priv = calloc(1, sizeof(twin_vnc_t));
151+
if (!ctx->priv) {
152+
free(ctx);
153+
return NULL;
154+
}
155+
156+
twin_vnc_t *tx = ctx->priv;
157+
tx->width = width;
158+
tx->height = height;
159+
160+
tx->aml = aml_new();
161+
if (!tx->aml) {
162+
log_error("Failed to create aml");
163+
goto bail_priv;
164+
}
165+
aml_set_default(tx->aml);
166+
char *vnc_host = getenv(NEATVNC_HOST);
167+
if (!vnc_host) {
168+
log_info(
169+
"Environment variable $NEATVNC_HOST not set, use %s by default",
170+
NEATVNC_HOST_DEFAULT);
171+
vnc_host = NEATVNC_HOST_DEFAULT;
172+
}
173+
char *vnc_port = getenv(NEATVNC_PORT);
174+
if (!vnc_port) {
175+
log_info(
176+
"Environment variable $NEATVNC_PORT not set, use %s by default",
177+
NEATVNC_PORT_DEFAULT);
178+
vnc_port = NEATVNC_PORT_DEFAULT;
179+
}
180+
log_info("NeatVNC server IP %s PORT %s", vnc_host, vnc_port);
181+
tx->server = nvnc_open(vnc_host, atoi(vnc_port));
182+
if (!tx->server) {
183+
log_error("Failed to open neatvnc server");
184+
goto bail_aml;
185+
}
186+
187+
tx->display = nvnc_display_new(0, 0);
188+
if (!tx->display) {
189+
log_error("Failed to create neatvnc display");
190+
goto bail_server;
191+
}
192+
193+
nvnc_add_display(tx->server, tx->display);
194+
nvnc_set_name(tx->server, "Twin VNC Backend");
195+
nvnc_set_pointer_fn(tx->server, _twin_vnc_pointer_event);
196+
nvnc_set_new_client_fn(tx->server, _twin_vnc_new_client);
197+
nvnc_set_userdata(tx->server, tx, NULL);
198+
199+
ctx->screen = twin_screen_create(width, height, _twin_vnc_put_begin,
200+
_twin_vnc_put_span, ctx);
201+
if (!ctx->screen)
202+
goto bail_display;
203+
204+
tx->framebuffer = malloc(width * height * sizeof(uint32_t));
205+
if (!tx->framebuffer) {
206+
log_error("Failed to malloc framebuffer");
207+
goto bail_screen;
208+
}
209+
memset(tx->framebuffer, 0xff, width * height * sizeof(uint32_t));
210+
211+
tx->current_fb = nvnc_fb_from_buffer(tx->framebuffer, width, height,
212+
DRM_FORMAT_ARGB8888, width);
213+
if (!tx->current_fb) {
214+
log_error("Failed to init nvnc framebuffer");
215+
goto bail_framebuffer;
216+
}
217+
int aml_fd = aml_get_fd(tx->aml);
218+
tx->aml_handler =
219+
aml_handler_new(aml_get_fd(tx->aml), _twin_vnc_work, ctx, NULL);
220+
twin_set_file(_twin_vnc_read_events, aml_fd, TWIN_READ, tx);
221+
222+
twin_set_work(_twin_vnc_work, TWIN_WORK_REDISPLAY, ctx);
223+
tx->screen = ctx->screen;
224+
225+
return ctx;
226+
227+
bail_framebuffer:
228+
free(tx->framebuffer);
229+
bail_screen:
230+
twin_screen_destroy(ctx->screen);
231+
bail_display:
232+
nvnc_display_unref(tx->display);
233+
bail_server:
234+
nvnc_close(tx->server);
235+
bail_aml:
236+
aml_unref(tx->aml);
237+
bail_priv:
238+
free(ctx->priv);
239+
free(ctx);
240+
return NULL;
241+
}
242+
243+
static void twin_vnc_configure(twin_context_t *ctx)
244+
{
245+
int width, height;
246+
twin_vnc_t *tx = ctx->priv;
247+
twin_vnc_get_screen_size(tx, &width, &height);
248+
twin_screen_resize(ctx->screen, width, height);
249+
}
250+
251+
static void twin_vnc_exit(twin_context_t *ctx)
252+
{
253+
if (!ctx)
254+
return;
255+
256+
twin_vnc_t *tx = PRIV(ctx);
257+
258+
aml_unref(tx->aml_handler);
259+
nvnc_display_unref(tx->display);
260+
nvnc_close(tx->server);
261+
aml_unref(tx->aml);
262+
263+
free(ctx->priv);
264+
free(ctx);
265+
}
266+
267+
const twin_backend_t g_twin_backend = {
268+
.init = twin_vnc_init,
269+
.configure = twin_vnc_configure,
270+
.exit = twin_vnc_exit,
271+
};

build-neatvnc.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Update and install dependencies
2+
sudo apt update
3+
sudo apt install -y meson ninja-build libpixman-1-dev zlib1g-dev libdrm-dev pkgconf
4+
5+
# Clone and build aml
6+
git clone https://github.com/any1/aml.git
7+
cd aml
8+
meson build
9+
ninja -C build
10+
sudo ninja -C build install
11+
12+
# Clone and build NeatVNC
13+
cd ..
14+
git clone https://github.com/any1/neatvnc.git
15+
cd neatvnc
16+
git checkout v0.8
17+
meson build
18+
ninja -C build
19+
sudo ninja -C build install

configs/Kconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ config BACKEND_FBDEV
1515
config BACKEND_SDL
1616
bool "SDL video output support"
1717

18+
config BACKEND_VNC
19+
bool "VNC server output support"
1820
endchoice
1921

2022
menu "Features"

0 commit comments

Comments
 (0)