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