6 #include "../aseg-paging/keyboard.h"
9 #include "lwip/stats.h"
13 #define SET_PIXEL_FORMAT 0
14 #define SET_ENCODINGS 2
15 #define FB_UPDATE_REQUEST 3
17 #define POINTER_EVENT 5
18 #define CLIENT_CUT_TEXT 6
20 #define RFB_BUF_SIZE 512
22 #define SCREEN_CHUNKS_X 8
23 #define SCREEN_CHUNKS_Y 8
39 struct server_init_message {
42 struct pixel_format fmt;
47 struct fb_update_req {
63 struct key_event_pkt {
70 struct pointer_event_pkt {
77 struct text_event_pkt {
84 struct update_header {
104 char data[RFB_BUF_SIZE];
108 char next_update_incremental;
109 char update_requested;
111 struct fb_update_req client_interest_area;
119 uint32_t checksums[SCREEN_CHUNKS_X][SCREEN_CHUNKS_Y];
125 uint32_t chunk_width;
126 uint32_t chunk_height;
128 uint32_t chunk_lindex;
130 uint32_t chunk_checksum;
132 int chunk_actually_sent;
136 static struct server_init_message server_info;
138 static void init_server_info() {
139 server_info.name_length = htonl(8);
140 memcpy(server_info.name_string, "NetWatch", 8);
143 static void update_server_info() {
145 outputf("RFB: setting fmt %d", fb->curmode.format);
146 server_info.fb_width = htons(fb->curmode.xres);
147 server_info.fb_height = htons(fb->curmode.yres);
148 switch (fb->curmode.format) {
150 server_info.fmt.bpp = 32;
151 server_info.fmt.depth = 24;
152 server_info.fmt.big_endian = 0;
153 server_info.fmt.true_color = 1;
154 server_info.fmt.red_max = htons(255);
155 server_info.fmt.green_max = htons(255);
156 server_info.fmt.blue_max = htons(255);
157 server_info.fmt.red_shift = 0;
158 server_info.fmt.green_shift = 8;
159 server_info.fmt.blue_shift = 16;
162 outputf("RFB: unknown fb fmt %d", fb->curmode.format);
166 outputf("RFB: fb null");
170 static int advance_chunk(struct rfb_state *state) {
172 state->chunk_xnum += 1;
174 if (state->chunk_xnum == SCREEN_CHUNKS_X) {
175 state->chunk_ynum += 1;
176 state->chunk_xnum = 0;
179 if (state->chunk_ynum == SCREEN_CHUNKS_Y) {
180 state->chunk_ynum = 0;
181 state->send_state = SST_IDLE;
182 if (!(state->chunk_actually_sent))
183 state->try_in_a_bit = 2;
190 static void send_fsm(struct tcp_pcb *pcb, struct rfb_state *state) {
191 struct update_header hdr;
193 unsigned char * lptr;
199 switch (state->send_state) {
204 if (state->update_requested) {
205 outputf("RFB send: update requested");
206 state->update_requested = 0;
207 state->chunk_actually_sent = 0;
208 state->send_state = SST_HEADER;
213 /* FALL THROUGH to SST_HEADER */
217 /* Calculate the width and height for this chunk, remembering
218 * that if SCREEN_CHUNKS_[XY] do not evenly divide the width and
219 * height, we may need to have shorter chunks at the edge of
222 state->chunk_width = fb->curmode.xres / SCREEN_CHUNKS_X;
223 if (fb->curmode.xres % SCREEN_CHUNKS_X != 0)
224 state->chunk_width += 1;
225 state->chunk_xpos = state->chunk_width * state->chunk_xnum;
226 totaldim = state->chunk_width * (state->chunk_xnum + 1);
227 if (totaldim > fb->curmode.xres) {
228 state->chunk_width -= (totaldim - fb->curmode.xres);
231 state->chunk_height = fb->curmode.yres / SCREEN_CHUNKS_Y;
232 if (fb->curmode.yres % SCREEN_CHUNKS_Y != 0)
233 state->chunk_height += 1;
234 state->chunk_ypos = state->chunk_height
236 totaldim = state->chunk_height * (state->chunk_ynum + 1);
237 if (totaldim > fb->curmode.yres) {
238 state->chunk_height -= (totaldim - fb->curmode.yres);
241 /* Do we _actually_ need to send this chunk? */
242 if (fb->checksum_rect) {
243 state->chunk_checksum = fb->checksum_rect(state->chunk_xpos, state->chunk_ypos,
244 state->chunk_width, state->chunk_height);
246 if (state->chunk_checksum == state->checksums[state->chunk_xnum][state->chunk_ynum]) {
247 if (advance_chunk(state))
251 /* Checksum gets set in data block, AFTER the data has been sent. */
254 outputf("actually sent");
255 state->chunk_actually_sent = 1;
259 state->chunk_lindex = 0;
260 hdr.nrects = htons(1);
261 hdr.xpos = htons(state->chunk_xpos);
262 hdr.ypos = htons(state->chunk_ypos);
263 hdr.width = htons(state->chunk_width);
264 hdr.height= htons(state->chunk_height);
265 hdr.enctype = htonl(0);
266 lines_left = state->chunk_height;
268 err = tcp_write(pcb, &hdr, sizeof(hdr), TCP_WRITE_FLAG_COPY);
272 outputf("RFB: header send error %d", err);
274 /* Try again later. */
278 state->send_state = SST_DATA;
280 /* FALL THROUGH to SST_DATA */
284 lines_left = state->chunk_height - state->chunk_lindex;
286 if (lines_left == 0) {
287 state->send_state = SST_HEADER;
288 state->checksums[state->chunk_xnum][state->chunk_ynum] = state->chunk_checksum;
289 if (advance_chunk(state))
295 + (fb->curmode.xres * fb->curmode.bytestride
296 * (state->chunk_ypos + state->chunk_lindex))
297 + (state->chunk_xpos * fb->curmode.bytestride);
299 /* The network card can't DMA from video RAM,
300 * so use TCP_WRITE_FLAG_COPY. */
301 err = tcp_write(pcb, lptr,
302 fb->curmode.bytestride * state->chunk_width, TCP_WRITE_FLAG_COPY);
305 state->chunk_lindex += 1;
308 outputf("RFB: send error %d", err);
313 if (tcp_sndbuf(pcb) == 0) {
319 if (tcp_output(pcb) != ERR_OK)
320 outputf("RFB: tcp_output bailed in send_fsm?");
323 static err_t rfb_sent(void *arg, struct tcp_pcb *pcb, uint16_t len) {
324 struct rfb_state *state = arg;
325 send_fsm(pcb, state);
329 static err_t rfb_poll(void *arg, struct tcp_pcb *pcb) {
330 struct rfb_state *state = arg;
331 send_fsm(pcb, state);
332 if (state->try_in_a_bit) {
333 state->try_in_a_bit--;
334 if (!(state->try_in_a_bit)) {
335 state->update_requested = 1;
344 static void close_conn(struct tcp_pcb *pcb, struct rfb_state *state) {
358 static enum fsm_result recv_fsm(struct tcp_pcb *pcb, struct rfb_state *state) {
362 outputf("RFB FSM: st %d rp %d wp %d", state->state, state->readpos,
365 switch(state->state) {
367 if (state->writepos < 12) return NEEDMORE;
369 if (!strncmp(state->data, "RFB 003.003\n", 12)) {
371 } else if (!strncmp(state->data, "RFB 003.005\n", 12)) {
372 /* Spec states that "RFB 003.005", an incorrect value,
373 * should be treated by the server as 3.3. */
375 } else if (!strncmp(state->data, "RFB 003.007\n", 12)) {
377 } else if (!strncmp(state->data, "RFB 003.008\n", 12)) {
380 outputf("RFB: Negotiation fail");
384 outputf("RFB: Negotiated v3.%d", state->version);
386 state->readpos += 12;
387 state->state = ST_CLIENTINIT;
389 /* We support one security type, currently "none".
390 * Send that and SecurityResult. */
391 if (state->version >= 7) {
392 tcp_write(pcb, "\x01\x01\x00\x00\x00\x00", 6, 0);
394 tcp_write(pcb, "\x01\x00\x00\x00\x00", 5, 0);
402 if (state->version >= 7) {
403 /* Ignore the security type and ClientInit */
404 if (state->writepos < 2) return NEEDMORE;
407 /* Just ClientInit */
408 if (state->writepos < 1) return NEEDMORE;
412 state->state = ST_MAIN;
414 outputf("RFB: Sending server info", state->version);
415 tcp_write(pcb, &server_info, sizeof(server_info), TCP_WRITE_FLAG_COPY);
421 if (state->writepos < 1) return NEEDMORE;
423 outputf("RFB: cmd %d", state->data[0]);
424 switch (state->data[0]) {
426 case SET_PIXEL_FORMAT:
428 if (state->writepos < (sizeof(struct pixel_format) + 4))
430 outputf("RFB: SetPixelFormat");
432 struct pixel_format * new_fmt =
433 (struct pixel_format *)(&state->data[4]);
437 state->readpos += sizeof(struct pixel_format) + 4;
441 if (state->writepos < 4) return NEEDMORE;
443 struct set_encs_req * req = (struct set_encs_req *)state->data;
445 pktsize = sizeof(struct set_encs_req) + (4 * ntohs(req->num));
447 outputf("RFB: SetEncodings [%d]", ntohs(req->num));
448 if (state->writepos < pktsize) return NEEDMORE;
450 for (i = 0; i < ntohs(req->num); i++) {
451 outputf("RFB: Encoding: %d", ntohl(req->encodings[i]));
455 state->readpos += pktsize;
458 case FB_UPDATE_REQUEST:
459 if (state->writepos < sizeof(struct fb_update_req))
461 outputf("RFB: UpdateRequest");
463 state->update_requested = 1;
464 memcpy(&state->client_interest_area, state->data,
465 sizeof(struct fb_update_req));
467 state->readpos += sizeof(struct fb_update_req);
471 if (state->writepos < sizeof(struct key_event_pkt))
474 struct key_event_pkt * p = (struct key_event_pkt *)state->data;
476 outputf("RFB: Key: %d (%c)", htonl(p->keysym), (htonl(p->keysym) & 0xFF));
477 kbd_inject_keysym(htonl(p->keysym), p->downflag);
479 state->readpos += sizeof(struct key_event_pkt);
483 if (state->writepos < sizeof(struct pointer_event_pkt))
485 outputf("RFB: Pointer");
489 state->readpos += sizeof(struct pointer_event_pkt);
492 case CLIENT_CUT_TEXT:
493 if (state->writepos < sizeof(struct text_event_pkt))
495 outputf("RFB: Cut Text");
497 struct text_event_pkt * pkt =
498 (struct text_event_pkt *)state->data;
500 if (state->writepos < sizeof(struct text_event_pkt)
506 state->readpos += sizeof(struct text_event_pkt)
511 outputf("RFB: Bad command: %d", state->data[0]);
514 outputf("RFB: Bad state");
519 static err_t rfb_recv(void *arg, struct tcp_pcb *pcb,
520 struct pbuf *p, err_t err) {
521 struct rfb_state *state = arg;
526 outputf("RFB: recv err %d", err);
527 /* FIXME do something better here? */
532 outputf("RFB: Connection closed");
533 close_conn(pcb, state);
537 if (p->tot_len > (RFB_BUF_SIZE - state->writepos)) {
539 outputf("RFB: Overflow!");
540 close_conn(pcb, state);
544 outputf("RFB: Processing %d", p->tot_len);
545 pbuf_copy_partial(p, state->data + state->writepos, p->tot_len, 0);
546 state->writepos += p->tot_len;
548 tcp_recved(pcb, p->tot_len);
552 switch (recv_fsm(pcb, state)) {
554 outputf("RFB FSM: blocking");
559 outputf("RFB FSM: ok");
561 /* Kick off a send. */
562 if (state->send_state == SST_IDLE
563 && state->update_requested) {
564 send_fsm(pcb, state);
567 if (state->readpos == state->writepos) {
573 state->data + state->readpos,
574 state->writepos - state->readpos);
579 outputf("RFB: Protocol error");
580 close_conn(pcb, state);
586 static err_t rfb_accept(void *arg, struct tcp_pcb *pcb, err_t err) {
587 struct rfb_state *state;
589 LWIP_UNUSED_ARG(arg);
590 LWIP_UNUSED_ARG(err);
592 state = (struct rfb_state *)mem_malloc(sizeof(struct rfb_state));
594 memset(state, 0, sizeof(struct rfb_state));
596 state->state = ST_BEGIN;
597 state->send_state = SST_IDLE;
599 /* XXX: update_server_info() should be called from the 64ms timer, and deal
600 * with screen resizes appropriately. */
601 update_server_info();
605 outputf("rfb_accept: out of memory\n");
610 tcp_recv(pcb, rfb_recv);
611 tcp_sent(pcb, rfb_sent);
612 tcp_poll(pcb, rfb_poll, 1);
614 tcp_err(pcb, rfb_err);
616 tcp_write(pcb, "RFB 003.008\n", 12, 0);
628 tcp_bind(pcb, IP_ADDR_ANY, RFB_PORT);
629 pcb = tcp_listen(pcb);
630 tcp_accept(pcb, rfb_accept);