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_actually_sent;
133 static struct server_init_message server_info;
135 static void init_server_info() {
136 server_info.name_length = htonl(8);
137 memcpy(server_info.name_string, "NetWatch", 8);
140 static void update_server_info() {
142 outputf("RFB: setting fmt %d", fb->curmode.format);
143 server_info.fb_width = htons(fb->curmode.xres);
144 server_info.fb_height = htons(fb->curmode.yres);
145 switch (fb->curmode.format) {
147 server_info.fmt.bpp = 32;
148 server_info.fmt.depth = 24;
149 server_info.fmt.big_endian = 0;
150 server_info.fmt.true_color = 1;
151 server_info.fmt.red_max = htons(255);
152 server_info.fmt.green_max = htons(255);
153 server_info.fmt.blue_max = htons(255);
154 server_info.fmt.red_shift = 0;
155 server_info.fmt.green_shift = 8;
156 server_info.fmt.blue_shift = 16;
159 outputf("RFB: unknown fb fmt %d", fb->curmode.format);
163 outputf("RFB: fb null");
167 static int advance_chunk(struct rfb_state *state) {
169 state->chunk_xnum += 1;
171 if (state->chunk_xnum == SCREEN_CHUNKS_X) {
172 state->chunk_ynum += 1;
173 state->chunk_xnum = 0;
176 if (state->chunk_ynum == SCREEN_CHUNKS_Y) {
177 state->chunk_ynum = 0;
178 state->send_state = SST_IDLE;
179 if (!(state->chunk_actually_sent))
180 state->update_requested = 1;
187 static void send_fsm(struct tcp_pcb *pcb, struct rfb_state *state) {
188 struct update_header hdr;
190 unsigned char * lptr;
197 switch (state->send_state) {
202 if (state->update_requested) {
203 outputf("RFB send: update requested");
204 state->update_requested = 0;
205 state->chunk_actually_sent = 0;
206 state->send_state = SST_HEADER;
211 /* FALL THROUGH to SST_HEADER */
215 /* Calculate the width and height for this chunk, remembering
216 * that if SCREEN_CHUNKS_[XY] do not evenly divide the width and
217 * height, we may need to have shorter chunks at the edge of
220 state->chunk_width = fb->curmode.xres / SCREEN_CHUNKS_X;
221 if (fb->curmode.xres % SCREEN_CHUNKS_X != 0)
222 state->chunk_width += 1;
223 state->chunk_xpos = state->chunk_width * state->chunk_xnum;
224 totaldim = state->chunk_width * (state->chunk_xnum + 1);
225 if (totaldim > fb->curmode.xres) {
226 state->chunk_width -= (totaldim - fb->curmode.xres);
229 state->chunk_height = fb->curmode.yres / SCREEN_CHUNKS_Y;
230 if (fb->curmode.yres % SCREEN_CHUNKS_Y != 0)
231 state->chunk_height += 1;
232 state->chunk_ypos = state->chunk_height
234 totaldim = state->chunk_height * (state->chunk_ynum + 1);
235 if (totaldim > fb->curmode.yres) {
236 state->chunk_height -= (totaldim - fb->curmode.yres);
239 outputf("rfb send: (%d [%d], %d [%d]) %d x %d",
240 state->chunk_xnum, state->chunk_xpos,
241 state->chunk_ynum, state->chunk_ypos,
242 state->chunk_width, state->chunk_height);
244 /* Do we _actually_ need to send this chunk? */
245 if (fb->checksum_rect) {
246 checksum = fb->checksum_rect(state->chunk_xpos, state->chunk_ypos,
247 state->chunk_width, state->chunk_height);
249 if (checksum == state->checksums[state->chunk_xnum][state->chunk_ynum]) {
250 outputf("!!!!!!! SKIPPING: %08x", checksum);
251 if (advance_chunk(state))
255 state->checksums[state->chunk_xnum][state->chunk_ynum] = checksum;
259 outputf("actually sent");
260 state->chunk_actually_sent = 1;
264 state->chunk_lindex = 0;
265 hdr.nrects = htons(1);
266 hdr.xpos = htons(state->chunk_xpos);
267 hdr.ypos = htons(state->chunk_ypos);
268 hdr.width = htons(state->chunk_width);
269 hdr.height= htons(state->chunk_height);
270 hdr.enctype = htonl(0);
271 lines_left = state->chunk_height;
273 err = tcp_write(pcb, &hdr, sizeof(hdr), TCP_WRITE_FLAG_COPY);
277 outputf("RFB: header send error %d", err);
279 /* Try again later. */
283 state->send_state = SST_DATA;
285 /* FALL THROUGH to SST_DATA */
289 lines_left = state->chunk_height - state->chunk_lindex;
291 if (lines_left == 0) {
292 state->send_state = SST_HEADER;
293 if (advance_chunk(state))
299 + (fb->curmode.xres * fb->curmode.bytestride
300 * (state->chunk_ypos + state->chunk_lindex))
301 + (state->chunk_xpos * fb->curmode.bytestride);
303 /* The network card can't DMA from video RAM,
304 * so use TCP_WRITE_FLAG_COPY. */
305 err = tcp_write(pcb, lptr,
306 fb->curmode.bytestride * state->chunk_width, TCP_WRITE_FLAG_COPY);
309 state->chunk_lindex += 1;
312 outputf("RFB: send error %d", err);
317 if (tcp_sndbuf(pcb) == 0) {
323 if (tcp_output(pcb) != ERR_OK)
324 outputf("RFB: tcp_output bailed in send_fsm?");
327 static err_t rfb_sent(void *arg, struct tcp_pcb *pcb, uint16_t len) {
328 struct rfb_state *state = arg;
329 send_fsm(pcb, state);
333 static err_t rfb_poll(void *arg, struct tcp_pcb *pcb) {
334 struct rfb_state *state = arg;
335 send_fsm(pcb, state);
342 static void close_conn(struct tcp_pcb *pcb, struct rfb_state *state) {
356 static enum fsm_result recv_fsm(struct tcp_pcb *pcb, struct rfb_state *state) {
360 outputf("RFB FSM: st %d rp %d wp %d", state->state, state->readpos,
363 switch(state->state) {
365 if (state->writepos < 12) return NEEDMORE;
367 if (!strncmp(state->data, "RFB 003.003\n", 12)) {
369 } else if (!strncmp(state->data, "RFB 003.005\n", 12)) {
370 /* Spec states that "RFB 003.005", an incorrect value,
371 * should be treated by the server as 3.3. */
373 } else if (!strncmp(state->data, "RFB 003.007\n", 12)) {
375 } else if (!strncmp(state->data, "RFB 003.008\n", 12)) {
378 outputf("RFB: Negotiation fail");
382 outputf("RFB: Negotiated v3.%d", state->version);
384 state->readpos += 12;
385 state->state = ST_CLIENTINIT;
387 /* We support one security type, currently "none".
388 * Send that and SecurityResult. */
389 if (state->version >= 7) {
390 tcp_write(pcb, "\x01\x01\x00\x00\x00\x00", 6, 0);
392 tcp_write(pcb, "\x01\x00\x00\x00\x00", 5, 0);
400 if (state->version >= 7) {
401 /* Ignore the security type and ClientInit */
402 if (state->writepos < 2) return NEEDMORE;
405 /* Just ClientInit */
406 if (state->writepos < 1) return NEEDMORE;
410 state->state = ST_MAIN;
412 outputf("RFB: Sending server info", state->version);
413 tcp_write(pcb, &server_info, sizeof(server_info), TCP_WRITE_FLAG_COPY);
419 if (state->writepos < 1) return NEEDMORE;
421 outputf("RFB: cmd %d", state->data[0]);
422 switch (state->data[0]) {
424 case SET_PIXEL_FORMAT:
426 if (state->writepos < (sizeof(struct pixel_format) + 4))
428 outputf("RFB: SetPixelFormat");
430 struct pixel_format * new_fmt =
431 (struct pixel_format *)(&state->data[4]);
435 state->readpos += sizeof(struct pixel_format) + 4;
439 if (state->writepos < 4) return NEEDMORE;
441 struct set_encs_req * req = (struct set_encs_req *)state->data;
443 pktsize = sizeof(struct set_encs_req) + (4 * ntohs(req->num));
445 outputf("RFB: SetEncodings [%d]", ntohs(req->num));
446 if (state->writepos < pktsize) return NEEDMORE;
448 for (i = 0; i < ntohs(req->num); i++) {
449 outputf("RFB: Encoding: %d", ntohl(req->encodings[i]));
453 state->readpos += pktsize;
456 case FB_UPDATE_REQUEST:
457 if (state->writepos < sizeof(struct fb_update_req))
459 outputf("RFB: UpdateRequest");
461 state->update_requested = 1;
462 memcpy(&state->client_interest_area, state->data,
463 sizeof(struct fb_update_req));
465 state->readpos += sizeof(struct fb_update_req);
469 if (state->writepos < sizeof(struct key_event_pkt))
472 struct key_event_pkt * p = (struct key_event_pkt *)state->data;
474 outputf("RFB: Key: %d (%c)", htonl(p->keysym), (htonl(p->keysym) & 0xFF));
475 kbd_inject_keysym(htonl(p->keysym), p->downflag);
477 state->readpos += sizeof(struct key_event_pkt);
481 if (state->writepos < sizeof(struct pointer_event_pkt))
483 outputf("RFB: Pointer");
487 state->readpos += sizeof(struct pointer_event_pkt);
490 case CLIENT_CUT_TEXT:
491 if (state->writepos < sizeof(struct text_event_pkt))
493 outputf("RFB: Cut Text");
495 struct text_event_pkt * pkt =
496 (struct text_event_pkt *)state->data;
498 if (state->writepos < sizeof(struct text_event_pkt)
504 state->readpos += sizeof(struct text_event_pkt)
509 outputf("RFB: Bad command: %d", state->data[0]);
512 outputf("RFB: Bad state");
517 static err_t rfb_recv(void *arg, struct tcp_pcb *pcb,
518 struct pbuf *p, err_t err) {
519 struct rfb_state *state = arg;
524 outputf("RFB: recv err %d", err);
525 /* FIXME do something better here? */
530 outputf("RFB: Connection closed");
531 close_conn(pcb, state);
535 if (p->tot_len > (RFB_BUF_SIZE - state->writepos)) {
537 outputf("RFB: Overflow!");
538 close_conn(pcb, state);
542 outputf("RFB: Processing %d", p->tot_len);
543 pbuf_copy_partial(p, state->data + state->writepos, p->tot_len, 0);
544 state->writepos += p->tot_len;
546 tcp_recved(pcb, p->tot_len);
550 switch (recv_fsm(pcb, state)) {
552 outputf("RFB FSM: blocking");
557 outputf("RFB FSM: ok");
559 /* Kick off a send. */
560 if (state->send_state == SST_IDLE
561 && state->update_requested) {
562 send_fsm(pcb, state);
565 if (state->readpos == state->writepos) {
571 state->data + state->readpos,
572 state->writepos - state->readpos);
577 outputf("RFB: Protocol error");
578 close_conn(pcb, state);
584 static err_t rfb_accept(void *arg, struct tcp_pcb *pcb, err_t err) {
585 struct rfb_state *state;
587 LWIP_UNUSED_ARG(arg);
588 LWIP_UNUSED_ARG(err);
590 state = (struct rfb_state *)mem_malloc(sizeof(struct rfb_state));
592 state->state = ST_BEGIN;
595 state->update_requested = 0;
596 state->send_state = SST_IDLE;
597 memset(state->checksums, 0, sizeof(state->checksums));
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);