2017-10-14 13:38:05 -04:00
|
|
|
/*
|
2022-11-13 16:37:58 -05:00
|
|
|
* 86Box A hypervisor and IBM PC system emulator that specializes in
|
|
|
|
|
* running old operating systems and software designed for IBM
|
|
|
|
|
* PC systems and compatibles from 1981 through fairly recent
|
|
|
|
|
* system designs based on the PCI bus.
|
2017-10-14 13:38:05 -04:00
|
|
|
*
|
2022-11-13 16:37:58 -05:00
|
|
|
* This file is part of the 86Box distribution.
|
2017-10-14 13:38:05 -04:00
|
|
|
*
|
2022-11-13 16:37:58 -05:00
|
|
|
* Implement the VNC remote renderer with LibVNCServer.
|
2017-10-14 13:38:05 -04:00
|
|
|
*
|
2020-03-25 00:46:02 +02:00
|
|
|
*
|
2017-10-14 13:38:05 -04:00
|
|
|
*
|
2022-11-13 16:37:58 -05:00
|
|
|
* Authors: Fred N. van Kempen, <decwiz@yahoo.com>
|
|
|
|
|
* Based on raw code by RichardG, <richardg867@gmail.com>
|
2017-10-14 13:38:05 -04:00
|
|
|
*
|
2022-11-13 16:37:58 -05:00
|
|
|
* Copyright 2017-2019 Fred N. van Kempen.
|
2017-10-14 13:38:05 -04:00
|
|
|
*/
|
2018-05-21 19:04:05 +02:00
|
|
|
#include <stdarg.h>
|
2017-10-14 13:38:05 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdint.h>
|
2017-10-19 04:27:04 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <wchar.h>
|
2017-10-23 05:20:18 -04:00
|
|
|
#include <rfb/rfb.h>
|
2018-05-21 19:04:05 +02:00
|
|
|
#define HAVE_STDARG_H
|
2020-03-29 14:24:42 +02:00
|
|
|
#include <86box/86box.h>
|
|
|
|
|
#include <86box/device.h>
|
|
|
|
|
#include <86box/video.h>
|
|
|
|
|
#include <86box/keyboard.h>
|
|
|
|
|
#include <86box/mouse.h>
|
|
|
|
|
#include <86box/plat.h>
|
|
|
|
|
#include <86box/ui.h>
|
|
|
|
|
#include <86box/vnc.h>
|
2017-10-19 04:27:04 -04:00
|
|
|
|
2022-09-18 17:11:43 -04:00
|
|
|
#define VNC_MIN_X 320
|
|
|
|
|
#define VNC_MAX_X 2048
|
|
|
|
|
#define VNC_MIN_Y 200
|
|
|
|
|
#define VNC_MAX_Y 2048
|
|
|
|
|
|
|
|
|
|
static rfbScreenInfoPtr rfb = NULL;
|
|
|
|
|
static int clients;
|
|
|
|
|
static int updatingSize;
|
|
|
|
|
static int allowedX,
|
|
|
|
|
allowedY;
|
|
|
|
|
static int ptr_x, ptr_y, ptr_but;
|
2017-10-14 13:38:05 -04:00
|
|
|
|
2022-12-30 06:05:57 +01:00
|
|
|
typedef struct {
|
|
|
|
|
int buttons;
|
|
|
|
|
int dx;
|
|
|
|
|
int dy;
|
|
|
|
|
int dwheel;
|
|
|
|
|
} MOUSESTATE;
|
|
|
|
|
|
|
|
|
|
static MOUSESTATE ms;
|
|
|
|
|
|
2018-05-21 19:04:05 +02:00
|
|
|
#ifdef ENABLE_VNC_LOG
|
|
|
|
|
int vnc_do_log = ENABLE_VNC_LOG;
|
|
|
|
|
|
|
|
|
|
static void
|
2018-10-19 00:39:32 +02:00
|
|
|
vnc_log(const char *fmt, ...)
|
2018-05-21 19:04:05 +02:00
|
|
|
{
|
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
|
|
if (vnc_do_log) {
|
2022-09-18 17:11:43 -04:00
|
|
|
va_start(ap, fmt);
|
|
|
|
|
pclog_ex(fmt, ap);
|
|
|
|
|
va_end(ap);
|
2018-05-21 19:04:05 +02:00
|
|
|
}
|
|
|
|
|
}
|
2018-10-19 00:39:32 +02:00
|
|
|
#else
|
2022-09-18 17:11:43 -04:00
|
|
|
# define vnc_log(fmt, ...)
|
2018-10-19 00:39:32 +02:00
|
|
|
#endif
|
2018-05-21 19:04:05 +02:00
|
|
|
|
2017-10-19 04:27:04 -04:00
|
|
|
static void
|
|
|
|
|
vnc_kbdevent(rfbBool down, rfbKeySym k, rfbClientPtr cl)
|
|
|
|
|
{
|
2022-09-18 17:11:43 -04:00
|
|
|
(void) cl;
|
2017-10-19 04:27:04 -04:00
|
|
|
|
2017-10-23 05:20:18 -04:00
|
|
|
/* Handle it through the lookup tables. */
|
2022-09-18 17:11:43 -04:00
|
|
|
vnc_kbinput(down ? 1 : 0, (int) k);
|
2017-10-19 04:27:04 -04:00
|
|
|
}
|
|
|
|
|
|
2022-12-30 06:05:57 +01:00
|
|
|
void
|
|
|
|
|
vnc_mouse_poll(void)
|
2017-10-19 04:27:04 -04:00
|
|
|
{
|
2022-12-30 06:05:57 +01:00
|
|
|
static int b = 0;
|
|
|
|
|
if (ms.dx != 0 || ms.dy != 0) {
|
|
|
|
|
mouse_x += ms.dx;
|
|
|
|
|
mouse_y += ms.dy;
|
2022-09-18 17:11:43 -04:00
|
|
|
|
2023-02-28 23:24:58 -05:00
|
|
|
ms.dx = 0;
|
|
|
|
|
ms.dy = 0;
|
2022-12-30 06:05:57 +01:00
|
|
|
|
|
|
|
|
// pclog("dx=%d, dy=%d, dwheel=%d\n", mouse_x, mouse_y, mouse_z);
|
2022-09-18 17:11:43 -04:00
|
|
|
}
|
2017-10-19 04:27:04 -04:00
|
|
|
|
2022-12-30 06:05:57 +01:00
|
|
|
if (b != ms.buttons) {
|
|
|
|
|
mouse_buttons = ms.buttons;
|
|
|
|
|
b = ms.buttons;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
vnc_ptrevent(int but, int x, int y, rfbClientPtr cl)
|
|
|
|
|
{
|
|
|
|
|
ms.buttons = 0;
|
|
|
|
|
if (but & 0x01)
|
|
|
|
|
ms.buttons |= 0x01;
|
|
|
|
|
if (but & 0x02)
|
|
|
|
|
ms.buttons |= 0x04;
|
|
|
|
|
if (but & 0x04)
|
|
|
|
|
ms.buttons |= 0x02;
|
|
|
|
|
ptr_but = but;
|
|
|
|
|
|
|
|
|
|
/* VNC uses absolute positions within the window, no deltas. */
|
|
|
|
|
ms.dx += (x - ptr_x) / 0.96; /* TODO: Figure out the correct scale factor for X and Y. */
|
|
|
|
|
ms.dy += (y - ptr_y) / 0.96;
|
|
|
|
|
ptr_x = x;
|
|
|
|
|
ptr_y = y;
|
|
|
|
|
|
2022-09-18 17:11:43 -04:00
|
|
|
rfbDefaultPtrAddEvent(but, x, y, cl);
|
|
|
|
|
}
|
2017-10-19 04:27:04 -04:00
|
|
|
|
2017-10-14 13:38:05 -04:00
|
|
|
static void
|
|
|
|
|
vnc_clientgone(rfbClientPtr cl)
|
|
|
|
|
{
|
2018-05-21 19:04:05 +02:00
|
|
|
vnc_log("VNC: client disconnected: %s\n", cl->host);
|
2017-10-14 13:38:05 -04:00
|
|
|
|
2017-10-14 20:04:21 -04:00
|
|
|
if (clients > 0)
|
2022-09-18 17:11:43 -04:00
|
|
|
clients--;
|
2017-10-14 20:04:21 -04:00
|
|
|
if (clients == 0) {
|
2022-09-18 17:11:43 -04:00
|
|
|
/* No more clients, pause the emulator. */
|
|
|
|
|
vnc_log("VNC: no clients, pausing..\n");
|
2017-10-26 04:54:50 -04:00
|
|
|
|
2022-09-18 17:11:43 -04:00
|
|
|
/* Disable the mouse. */
|
2022-12-30 06:05:57 +01:00
|
|
|
// plat_mouse_capture(0);
|
|
|
|
|
mouse_set_poll_ex(NULL);
|
2017-10-26 04:54:50 -04:00
|
|
|
|
2022-09-18 17:11:43 -04:00
|
|
|
plat_pause(1);
|
2017-10-14 13:38:05 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static enum rfbNewClientAction
|
|
|
|
|
vnc_newclient(rfbClientPtr cl)
|
|
|
|
|
{
|
|
|
|
|
/* Hook the ClientGone function so we know when they're gone. */
|
|
|
|
|
cl->clientGoneHook = vnc_clientgone;
|
|
|
|
|
|
2018-05-21 19:04:05 +02:00
|
|
|
vnc_log("VNC: new client: %s\n", cl->host);
|
2017-10-14 20:04:21 -04:00
|
|
|
if (++clients == 1) {
|
2022-09-18 17:11:43 -04:00
|
|
|
/* Reset the mouse. */
|
|
|
|
|
ptr_x = allowedX / 2;
|
|
|
|
|
ptr_y = allowedY / 2;
|
|
|
|
|
mouse_x = mouse_y = mouse_z = 0;
|
|
|
|
|
mouse_buttons = 0x00;
|
2022-12-30 06:05:57 +01:00
|
|
|
memset(&ms, 0, sizeof(MOUSESTATE));
|
2017-10-23 05:20:18 -04:00
|
|
|
|
2022-09-18 17:11:43 -04:00
|
|
|
/* We now have clients, un-pause the emulator if needed. */
|
|
|
|
|
vnc_log("VNC: unpausing..\n");
|
2017-10-26 04:54:50 -04:00
|
|
|
|
2022-09-18 17:11:43 -04:00
|
|
|
/* Enable the mouse. */
|
2022-12-30 06:05:57 +01:00
|
|
|
// plat_mouse_capture(1);
|
|
|
|
|
mouse_set_poll_ex(vnc_mouse_poll);
|
2017-10-26 04:54:50 -04:00
|
|
|
|
2022-09-18 17:11:43 -04:00
|
|
|
plat_pause(0);
|
2017-10-14 13:38:05 -04:00
|
|
|
}
|
|
|
|
|
|
2017-10-19 04:27:04 -04:00
|
|
|
/* For now, we always accept clients. */
|
2022-09-18 17:11:43 -04:00
|
|
|
return (RFB_CLIENT_ACCEPT);
|
2017-10-14 13:38:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
vnc_display(rfbClientPtr cl)
|
|
|
|
|
{
|
|
|
|
|
/* Avoid race condition between resize and update. */
|
|
|
|
|
if (!updatingSize && cl->newFBSizePending) {
|
2022-09-18 17:11:43 -04:00
|
|
|
updatingSize = 1;
|
2017-10-14 13:38:05 -04:00
|
|
|
} else if (updatingSize && !cl->newFBSizePending) {
|
2022-09-18 17:11:43 -04:00
|
|
|
updatingSize = 0;
|
2017-10-14 13:38:05 -04:00
|
|
|
|
2022-09-18 17:11:43 -04:00
|
|
|
allowedX = rfb->width;
|
|
|
|
|
allowedY = rfb->height;
|
2017-10-14 13:38:05 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2022-08-03 03:36:24 +02:00
|
|
|
vnc_blit(int x, int y, int w, int h, int monitor_index)
|
2017-10-14 13:38:05 -04:00
|
|
|
{
|
2023-02-28 23:24:58 -05:00
|
|
|
int row;
|
2017-10-14 20:04:21 -04:00
|
|
|
|
2022-12-30 06:05:57 +01:00
|
|
|
if (monitor_index || (x < 0) || (y < 0) || (w < VNC_MIN_X) || (h < VNC_MIN_Y) || (w > VNC_MAX_X) || (h > VNC_MAX_Y) || (buffer32 == NULL)) {
|
2022-08-03 13:05:21 +06:00
|
|
|
video_blit_complete_monitor(monitor_index);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-09-13 23:19:10 +02:00
|
|
|
|
2022-12-30 04:29:56 +01:00
|
|
|
for (row = 0; row < h; ++row)
|
|
|
|
|
video_copy(&(((uint8_t *) rfb->frameBuffer)[row * 2048 * sizeof(uint32_t)]), &(buffer32->line[y + row][x]), w * sizeof(uint32_t));
|
2021-10-06 02:26:30 +02:00
|
|
|
|
|
|
|
|
if (screenshots)
|
2022-09-18 17:11:43 -04:00
|
|
|
video_screenshot((uint32_t *) rfb->frameBuffer, 0, 0, VNC_MAX_X);
|
2022-02-20 02:26:27 -05:00
|
|
|
|
2022-08-03 13:05:21 +06:00
|
|
|
video_blit_complete_monitor(monitor_index);
|
2017-10-14 13:38:05 -04:00
|
|
|
|
2022-09-18 17:11:43 -04:00
|
|
|
if (!updatingSize)
|
|
|
|
|
rfbMarkRectAsModified(rfb, 0, 0, allowedX, allowedY);
|
2017-10-14 13:38:05 -04:00
|
|
|
}
|
|
|
|
|
|
2017-10-19 04:27:04 -04:00
|
|
|
/* Initialize VNC for operation. */
|
2017-10-14 13:38:05 -04:00
|
|
|
int
|
2017-10-19 04:27:04 -04:00
|
|
|
vnc_init(UNUSED(void *arg))
|
2017-10-14 13:38:05 -04:00
|
|
|
{
|
2022-09-18 17:11:43 -04:00
|
|
|
static char title[128];
|
2017-10-14 13:38:05 -04:00
|
|
|
rfbPixelFormat rpf = {
|
2022-09-18 17:11:43 -04:00
|
|
|
/*
|
|
|
|
|
* Screen format:
|
2022-11-13 16:38:48 -05:00
|
|
|
* 32bpp; 32 depth;
|
|
|
|
|
* little endian;
|
|
|
|
|
* true color;
|
|
|
|
|
* max 255 R/G/B;
|
|
|
|
|
* red shift 16; green shift 8; blue shift 0;
|
|
|
|
|
* padding
|
2022-09-18 17:11:43 -04:00
|
|
|
*/
|
|
|
|
|
32, 32, 0, 1, 255, 255, 255, 16, 8, 0, 0, 0
|
2017-10-14 13:38:05 -04:00
|
|
|
};
|
|
|
|
|
|
2022-08-04 00:21:27 +06:00
|
|
|
plat_pause(1);
|
2022-08-03 13:05:21 +06:00
|
|
|
cgapal_rebuild_monitor(0);
|
2017-12-12 19:18:12 +01:00
|
|
|
|
2017-10-14 20:04:21 -04:00
|
|
|
if (rfb == NULL) {
|
2022-09-18 17:11:43 -04:00
|
|
|
wcstombs(title, ui_window_title(NULL), sizeof(title));
|
|
|
|
|
updatingSize = 0;
|
|
|
|
|
allowedX = scrnsz_x;
|
|
|
|
|
allowedY = scrnsz_y;
|
2022-02-20 02:26:27 -05:00
|
|
|
|
2022-09-18 17:11:43 -04:00
|
|
|
rfb = rfbGetScreen(0, NULL, VNC_MAX_X, VNC_MAX_Y, 8, 3, 4);
|
|
|
|
|
rfb->desktopName = title;
|
|
|
|
|
rfb->frameBuffer = (char *) malloc(VNC_MAX_X * VNC_MAX_Y * 4);
|
2017-10-14 20:04:21 -04:00
|
|
|
|
2022-09-18 17:11:43 -04:00
|
|
|
rfb->serverFormat = rpf;
|
|
|
|
|
rfb->alwaysShared = TRUE;
|
|
|
|
|
rfb->displayHook = vnc_display;
|
|
|
|
|
rfb->ptrAddEvent = vnc_ptrevent;
|
|
|
|
|
rfb->kbdAddEvent = vnc_kbdevent;
|
|
|
|
|
rfb->newClientHook = vnc_newclient;
|
2022-02-20 02:26:27 -05:00
|
|
|
|
2022-09-18 17:11:43 -04:00
|
|
|
/* Set up our current resolution. */
|
|
|
|
|
rfb->width = allowedX;
|
|
|
|
|
rfb->height = allowedY;
|
2022-02-20 02:26:27 -05:00
|
|
|
|
2022-09-18 17:11:43 -04:00
|
|
|
rfbInitServer(rfb);
|
2017-10-14 13:38:05 -04:00
|
|
|
|
2022-09-18 17:11:43 -04:00
|
|
|
rfbRunEventLoop(rfb, -1, TRUE);
|
2017-10-14 13:38:05 -04:00
|
|
|
}
|
2022-02-20 02:26:27 -05:00
|
|
|
|
2017-10-14 20:04:21 -04:00
|
|
|
/* Set up our BLIT handlers. */
|
2017-10-22 03:32:30 +02:00
|
|
|
video_setblit(vnc_blit);
|
2017-10-14 13:38:05 -04:00
|
|
|
|
2017-10-14 20:04:21 -04:00
|
|
|
clients = 0;
|
2017-10-14 13:38:05 -04:00
|
|
|
|
2018-05-21 19:04:05 +02:00
|
|
|
vnc_log("VNC: init complete.\n");
|
2017-10-19 04:27:04 -04:00
|
|
|
|
2022-09-18 17:11:43 -04:00
|
|
|
return (1);
|
2017-10-14 13:38:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
vnc_close(void)
|
|
|
|
|
{
|
2017-12-15 18:47:29 +01:00
|
|
|
video_setblit(NULL);
|
|
|
|
|
|
2017-10-19 04:27:04 -04:00
|
|
|
if (rfb != NULL) {
|
2022-09-18 17:11:43 -04:00
|
|
|
free(rfb->frameBuffer);
|
2017-10-19 04:27:04 -04:00
|
|
|
|
2022-09-18 17:11:43 -04:00
|
|
|
rfbScreenCleanup(rfb);
|
2017-10-19 04:27:04 -04:00
|
|
|
|
2022-09-18 17:11:43 -04:00
|
|
|
rfb = NULL;
|
2017-10-19 04:27:04 -04:00
|
|
|
}
|
2017-10-14 13:38:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
vnc_resize(int x, int y)
|
|
|
|
|
{
|
2017-10-14 20:04:21 -04:00
|
|
|
rfbClientIteratorPtr iterator;
|
2022-09-18 17:11:43 -04:00
|
|
|
rfbClientPtr cl;
|
2017-10-14 20:04:21 -04:00
|
|
|
|
2022-09-18 17:11:43 -04:00
|
|
|
if (rfb == NULL)
|
|
|
|
|
return;
|
2017-10-14 20:04:21 -04:00
|
|
|
|
2017-10-19 04:27:04 -04:00
|
|
|
/* TightVNC doesn't like certain sizes.. */
|
2022-12-30 06:05:57 +01:00
|
|
|
if ((x < VNC_MIN_X) || (x > VNC_MAX_X) || (y < VNC_MIN_Y) || (y > VNC_MAX_Y)) {
|
2022-09-18 17:11:43 -04:00
|
|
|
vnc_log("VNC: invalid resoltion %dx%d requested!\n", x, y);
|
|
|
|
|
return;
|
2017-10-19 04:27:04 -04:00
|
|
|
}
|
|
|
|
|
|
2017-10-14 20:04:21 -04:00
|
|
|
if ((x != rfb->width || y != rfb->height) && x > 160 && y > 0) {
|
2022-09-18 17:11:43 -04:00
|
|
|
vnc_log("VNC: updating resolution: %dx%d\n", x, y);
|
2022-02-20 02:26:27 -05:00
|
|
|
|
2022-09-18 17:11:43 -04:00
|
|
|
allowedX = (rfb->width < x) ? rfb->width : x;
|
|
|
|
|
allowedY = (rfb->width < y) ? rfb->width : y;
|
2022-02-20 02:26:27 -05:00
|
|
|
|
2022-09-18 17:11:43 -04:00
|
|
|
rfb->width = x;
|
|
|
|
|
rfb->height = y;
|
2022-02-20 02:26:27 -05:00
|
|
|
|
2022-09-18 17:11:43 -04:00
|
|
|
iterator = rfbGetClientIterator(rfb);
|
|
|
|
|
while ((cl = rfbClientIteratorNext(iterator)) != NULL) {
|
|
|
|
|
LOCK(cl->updateMutex);
|
|
|
|
|
cl->newFBSizePending = 1;
|
|
|
|
|
UNLOCK(cl->updateMutex);
|
|
|
|
|
}
|
2017-10-14 13:38:05 -04:00
|
|
|
}
|
|
|
|
|
}
|
2022-02-20 02:26:27 -05:00
|
|
|
|
2017-10-19 04:27:04 -04:00
|
|
|
/* Tell them to pause if we have no clients. */
|
2017-10-14 13:38:05 -04:00
|
|
|
int
|
|
|
|
|
vnc_pause(void)
|
|
|
|
|
{
|
2022-09-18 17:11:43 -04:00
|
|
|
return ((clients > 0) ? 0 : 1);
|
2017-10-14 13:38:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
vnc_take_screenshot(wchar_t *fn)
|
|
|
|
|
{
|
2018-05-21 19:04:05 +02:00
|
|
|
vnc_log("VNC: take_screenshot\n");
|
2017-10-14 13:38:05 -04:00
|
|
|
}
|