-
Notifications
You must be signed in to change notification settings - Fork 8
/
vnc.c
173 lines (145 loc) · 4.27 KB
/
vnc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <errno.h>
#include <stdio.h>
#include "main.h"
#include "vnc.h"
#include "framebuffer.h"
static const struct frontend_ops fops = {
.alloc = vnc_alloc,
.start = vnc_start,
.free = vnc_free,
.update = vnc_update,
.draw_string = vnc_draw_string,
};
static const struct frontend_arg fargs[] = {
{ .name = "port", .configure = vnc_configure_port },
{ .name = "font", .configure = vnc_configure_font },
{ .name = "flickerfree", .configure = vnc_configure_flicker },
{ .name = "", .configure = NULL },
};
DECLARE_FRONTEND_SIG_ARGS(front_vnc, "VNC server frontend", &fops, fargs);
static void set_shared(struct vnc* vnc, bool shared) {
vnc->server->alwaysShared = shared ? TRUE : FALSE;
vnc->server->neverShared = shared ? FALSE : TRUE;
}
static void pre_display_cb(struct _rfbClientRec* client) {
struct vnc* vnc = client->screen->screenData;
if(vnc->front.sync_overlay_draw) {
pthread_mutex_lock(&vnc->draw_lock);
draw_overlays(vnc->fb_overlay);
}
}
static void post_display_cb(struct _rfbClientRec* client, int result) {
struct vnc* vnc = client->screen->screenData;
if(vnc->front.sync_overlay_draw) {
pthread_mutex_unlock(&vnc->draw_lock);
}
}
int vnc_alloc(struct frontend** ret, struct fb* fb, void* priv) {
int err = 0;
struct vnc* vnc = calloc(1, sizeof(struct vnc));
struct fb_size* size;
if(!vnc) {
err = -ENOMEM;
goto fail;
}
pthread_mutex_init(&vnc->draw_lock, NULL);
vnc->fb = fb;
size = fb_get_size(fb);
if((err = fb_alloc(&vnc->fb_overlay, size->width, size->height))) {
goto fail_vnc;
}
vnc->server = rfbGetScreen(NULL, NULL, size->width, size->height, 8, 3, 4);
if(!vnc->server) {
err = -ENOMEM;
goto fail_fb;
}
vnc->server->bitsPerPixel = 32;
vnc->server->depth = 24;
rfbPixelFormat* format = &vnc->server->serverFormat;
format->depth = 24;
format->redMax = 0xFF;
format->greenMax = 0xFF;
format->blueMax = 0xFF;
format->redShift = 24;
format->greenShift = 16;
format->blueShift = 8;
vnc->server->displayHook = pre_display_cb;
vnc->server->displayFinishedHook = post_display_cb;
vnc->server->screenData = vnc;
vnc->server->frameBuffer = (char *)vnc->fb_overlay->pixels;
vnc->server->desktopName = "shoreline";
set_shared(vnc, true);
*ret = &vnc->front;
return 0;
fail_fb:
fb_free(vnc->fb_overlay);
fail_vnc:
free(vnc);
fail:
return err;
};
int vnc_start(struct frontend* front) {
struct vnc* vnc = container_of(front, struct vnc, front);
rfbInitServer(vnc->server);
rfbRunEventLoop(vnc->server, -1, TRUE);
return 0;
}
void vnc_free(struct frontend* front) {
struct vnc* vnc = container_of(front, struct vnc, front);
rfbShutdownServer(vnc->server, TRUE);
rfbScreenCleanup(vnc->server);
free(vnc);
}
int vnc_update(struct frontend* front) {
struct vnc* vnc = container_of(front, struct vnc, front);
if(front->sync_overlay_draw) {
pthread_mutex_lock(&vnc->draw_lock);
}
fb_copy(vnc->fb_overlay, vnc->fb);
if(front->sync_overlay_draw) {
pthread_mutex_unlock(&vnc->draw_lock);
}
rfbMarkRectAsModified(vnc->server, 0, 0, vnc->fb->size.width, vnc->fb->size.height);
return !rfbIsActive(vnc->server);
}
int vnc_draw_string(struct frontend* front, unsigned x, unsigned y, char* str) {
int space, width;
struct vnc* vnc = container_of(front, struct vnc, front);
if(vnc->font) {
space = rfbWidthOfString(vnc->font, " ");
width = rfbWidthOfString(vnc->font, str);
rfbFillRect(vnc->server, x, y, x + width + 2 * space, y + VNC_FONT_HEIGHT + 4, 0x00000000);
rfbDrawString(vnc->server, vnc->font, x + space, y + VNC_FONT_HEIGHT + 2, str, 0xffffffff);
}
return 0;
}
int vnc_configure_port(struct frontend* front, char* value) {
struct vnc* vnc = container_of(front, struct vnc, front);
if(!value) {
return -EINVAL;
}
int port = atoi(value);
if(port < 0 || port > 65535) {
return -EINVAL;
}
vnc->server->port = vnc->server->ipv6port = port;
vnc->server->autoPort = FALSE;
return 0;
}
int vnc_configure_font(struct frontend* front, char* value) {
struct vnc* vnc = container_of(front, struct vnc, front);
if(!value) {
return -EINVAL;
}
vnc->font = rfbLoadConsoleFont(value);
if(!vnc->font) {
return -EINVAL;
}
return 0;
}
int vnc_configure_flicker(struct frontend* front, char* value) {
struct vnc* vnc = container_of(front, struct vnc, front);
front->sync_overlay_draw = true;
set_shared(vnc, false);
return 0;
}