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 4
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;
131 static struct server_init_message server_info;
133 static void init_server_info() {
134 server_info.name_length = htonl(8);
135 memcpy(server_info.name_string, "NetWatch", 8);
138 static void update_server_info() {
140 outputf("RFB: setting fmt %d", fb->curmode.format);
141 server_info.fb_width = htons(fb->curmode.xres);
142 server_info.fb_height = htons(fb->curmode.yres);
143 switch (fb->curmode.format) {
145 server_info.fmt.bpp = 32;
146 server_info.fmt.depth = 24;
147 server_info.fmt.big_endian = 0;
148 server_info.fmt.true_color = 1;
149 server_info.fmt.red_max = htons(255);
150 server_info.fmt.green_max = htons(255);
151 server_info.fmt.blue_max = htons(255);
152 server_info.fmt.red_shift = 0;
153 server_info.fmt.green_shift = 8;
154 server_info.fmt.blue_shift = 16;
157 outputf("RFB: unknown fb fmt %d", fb->curmode.format);
161 outputf("RFB: fb null");
165 static void send_fsm(struct tcp_pcb *pcb, struct rfb_state *state) {
166 struct update_header hdr;
173 switch (state->send_state) {
176 if (state->update_requested) {
177 outputf("RFB send: update requested");
178 state->update_requested = 0;
179 state->send_state = SST_NEEDS_UPDATE;
184 /* FALL THROUGH to SST_NEEDS_UPDATE */
186 case SST_NEEDS_UPDATE:
188 state->chunk_xnum = 0;
189 state->chunk_ynum = 0;
190 state->chunk_width = 0;
191 state->chunk_height = 0;
192 state->chunk_lindex = 0;
193 state->send_state = SST_SENDING;
195 /* FALL THROUGH to SST_SENDING */
200 lines_left = state->chunk_height - state->chunk_lindex;
202 if (lines_left == 0) {
203 /* Advance to the next chunk if necessary. If
204 * state->chunk_height is zero, then we are
205 * arriving here for the first time from
206 * SST_NEEDS_UPDATE. */
208 if (state->chunk_height != 0) {
209 state->chunk_xnum += 1;
212 if (state->chunk_xnum == SCREEN_CHUNKS_X) {
213 state->chunk_ynum += 1;
214 state->chunk_xnum = 0;
217 if (state->chunk_ynum == SCREEN_CHUNKS_Y) {
218 state->send_state = SST_IDLE;
219 outputf("RFB: Screen update done! %d", state->update_requested);
223 /* Calculate the width and height for this chunk, remembering
224 * that if SCREEN_CHUNKS_[XY] do not evenly divide the width and
225 * height, we may need to have shorter chunks at the edge of
228 state->chunk_width = fb->curmode.xres / SCREEN_CHUNKS_X;
229 if (fb->curmode.xres % SCREEN_CHUNKS_X != 0)
230 state->chunk_width += 1;
231 state->chunk_xpos = state->chunk_width * state->chunk_xnum;
232 totaldim = state->chunk_width * (state->chunk_xnum + 1);
233 if (totaldim > fb->curmode.xres) {
234 state->chunk_width -= (totaldim - fb->curmode.xres);
237 state->chunk_height = fb->curmode.yres / SCREEN_CHUNKS_Y;
238 if (fb->curmode.yres % SCREEN_CHUNKS_Y != 0)
239 state->chunk_height += 1;
240 state->chunk_ypos = state->chunk_height
242 totaldim = state->chunk_height * (state->chunk_ynum + 1);
243 if (totaldim > fb->curmode.yres) {
244 state->chunk_height -= (totaldim - fb->curmode.yres);
247 if (fb->checksum_rect) {
248 checksum = fb->checksum_rect(state->chunk_xpos, state->chunk_ypos,
249 state->chunk_width, state->chunk_height);
251 if (checksum == state->checksums[state->chunk_xnum][state->chunk_ynum]) {
252 state->chunk_lindex = state->chunk_height;
255 state->checksums[state->chunk_xnum][state->chunk_ynum] = checksum;
259 outputf("RFB send: sending header");
263 state->chunk_lindex = 0;
264 hdr.nrects = htons(1);
265 hdr.xpos = htons(state->chunk_xpos);
266 hdr.ypos = htons(state->chunk_ypos);
267 hdr.width = htons(state->chunk_width);
268 hdr.height= htons(state->chunk_height);
269 hdr.enctype = htonl(0);
270 lines_left = state->chunk_height;
272 err = tcp_write(pcb, &hdr, sizeof(hdr), TCP_WRITE_FLAG_COPY);
276 outputf("RFB: header send error %d", err);
278 /* Crap. Reset chunk_height to 0 so that next time around,
279 * we'll recalculate this chunk (not advance) and try to
280 * send the header again.
282 state->chunk_height = 0;
288 + (fb->curmode.xres * fb->curmode.bytestride
289 * (state->chunk_ypos + state->chunk_lindex))
290 + (state->chunk_xpos * fb->curmode.bytestride);
292 /* The network card can't DMA from video RAM,
293 * so use TCP_WRITE_FLAG_COPY. */
294 err = tcp_write(pcb, lptr,
295 fb->curmode.bytestride * state->chunk_width,
296 TCP_WRITE_FLAG_COPY);
299 state->chunk_lindex += 1;
302 } while (err == ERR_OK && state->chunk_lindex < state->chunk_height);
306 outputf("RFB: send error %d", err);
311 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 state->update_requested = 1;
332 send_fsm(pcb, state);
339 static void close_conn(struct tcp_pcb *pcb, struct rfb_state *state) {
353 static enum fsm_result recv_fsm(struct tcp_pcb *pcb, struct rfb_state *state) {
357 outputf("RFB FSM: st %d rp %d wp %d", state->state, state->readpos,
360 switch(state->state) {
362 if (state->writepos < 12) return NEEDMORE;
364 if (!strncmp(state->data, "RFB 003.003\n", 12)) {
366 } else if (!strncmp(state->data, "RFB 003.005\n", 12)) {
367 /* Spec states that "RFB 003.005", an incorrect value,
368 * should be treated by the server as 3.3. */
370 } else if (!strncmp(state->data, "RFB 003.007\n", 12)) {
372 } else if (!strncmp(state->data, "RFB 003.008\n", 12)) {
375 outputf("RFB: Negotiation fail");
379 outputf("RFB: Negotiated v3.%d", state->version);
381 state->readpos += 12;
382 state->state = ST_CLIENTINIT;
384 /* We support one security type, currently "none".
385 * Send that and SecurityResult. */
386 if (state->version >= 7) {
387 tcp_write(pcb, "\x01\x01\x00\x00\x00\x00", 6, 0);
389 tcp_write(pcb, "\x01\x00\x00\x00\x00", 5, 0);
397 if (state->version >= 7) {
398 /* Ignore the security type and ClientInit */
399 if (state->writepos < 2) return NEEDMORE;
402 /* Just ClientInit */
403 if (state->writepos < 1) return NEEDMORE;
407 state->state = ST_MAIN;
409 outputf("RFB: Sending server info", state->version);
410 tcp_write(pcb, &server_info, sizeof(server_info), TCP_WRITE_FLAG_COPY);
416 if (state->writepos < 1) return NEEDMORE;
418 outputf("RFB: cmd %d", state->data[0]);
419 switch (state->data[0]) {
421 case SET_PIXEL_FORMAT:
423 if (state->writepos < (sizeof(struct pixel_format) + 4))
425 outputf("RFB: SetPixelFormat");
427 struct pixel_format * new_fmt =
428 (struct pixel_format *)(&state->data[4]);
432 state->readpos += sizeof(struct pixel_format) + 4;
436 if (state->writepos < 4) return NEEDMORE;
438 struct set_encs_req * req = (struct set_encs_req *)state->data;
440 pktsize = sizeof(struct set_encs_req) + (4 * ntohs(req->num));
442 outputf("RFB: SetEncodings [%d]", ntohs(req->num));
443 if (state->writepos < pktsize) return NEEDMORE;
445 for (i = 0; i < ntohs(req->num); i++) {
446 outputf("RFB: Encoding: %d", ntohl(req->encodings[i]));
450 state->readpos += pktsize;
453 case FB_UPDATE_REQUEST:
454 if (state->writepos < sizeof(struct fb_update_req))
456 outputf("RFB: UpdateRequest");
458 state->update_requested = 1;
459 memcpy(&state->client_interest_area, state->data,
460 sizeof(struct fb_update_req));
462 state->readpos += sizeof(struct fb_update_req);
466 if (state->writepos < sizeof(struct key_event_pkt))
469 struct key_event_pkt * p = (struct key_event_pkt *)state->data;
471 outputf("RFB: Key: %d (%c)", htonl(p->keysym), (htonl(p->keysym) & 0xFF));
472 kbd_inject_keysym(htonl(p->keysym), p->downflag);
474 state->readpos += sizeof(struct key_event_pkt);
478 if (state->writepos < sizeof(struct pointer_event_pkt))
480 outputf("RFB: Pointer");
484 state->readpos += sizeof(struct pointer_event_pkt);
487 case CLIENT_CUT_TEXT:
488 if (state->writepos < sizeof(struct text_event_pkt))
490 outputf("RFB: Cut Text");
492 struct text_event_pkt * pkt =
493 (struct text_event_pkt *)state->data;
495 if (state->writepos < sizeof(struct text_event_pkt)
501 state->readpos += sizeof(struct text_event_pkt)
506 outputf("RFB: Bad command: %d", state->data[0]);
509 outputf("RFB: Bad state");
514 static err_t rfb_recv(void *arg, struct tcp_pcb *pcb,
515 struct pbuf *p, err_t err) {
516 struct rfb_state *state = arg;
521 outputf("RFB: recv err %d", err);
522 /* FIXME do something better here? */
527 outputf("RFB: Connection closed");
528 close_conn(pcb, state);
532 if (p->tot_len > (RFB_BUF_SIZE - state->writepos)) {
534 outputf("RFB: Overflow!");
535 close_conn(pcb, state);
539 outputf("RFB: Processing %d", p->tot_len);
540 pbuf_copy_partial(p, state->data + state->writepos, p->tot_len, 0);
541 state->writepos += p->tot_len;
543 tcp_recved(pcb, p->tot_len);
547 switch (recv_fsm(pcb, state)) {
549 outputf("RFB FSM: blocking");
554 outputf("RFB FSM: ok");
556 /* Kick off a send. */
557 if (state->send_state == SST_IDLE
558 && state->update_requested) {
559 send_fsm(pcb, state);
562 if (state->readpos == state->writepos) {
568 state->data + state->readpos,
569 state->writepos - state->readpos);
574 outputf("RFB: Protocol error");
575 close_conn(pcb, state);
581 static err_t rfb_accept(void *arg, struct tcp_pcb *pcb, err_t err) {
582 struct rfb_state *state;
584 LWIP_UNUSED_ARG(arg);
585 LWIP_UNUSED_ARG(err);
587 state = (struct rfb_state *)mem_malloc(sizeof(struct rfb_state));
589 state->state = ST_BEGIN;
592 state->update_requested = 0;
593 state->send_state = SST_IDLE;
595 /* XXX: update_server_info() should be called from the 64ms timer, and deal
596 * with screen resizes appropriately. */
597 update_server_info();
601 outputf("rfb_accept: out of memory\n");
606 tcp_recv(pcb, rfb_recv);
607 tcp_sent(pcb, rfb_sent);
608 tcp_poll(pcb, rfb_poll, 1);
610 tcp_err(pcb, rfb_err);
612 tcp_write(pcb, "RFB 003.008\n", 12, 0);
624 tcp_bind(pcb, IP_ADDR_ANY, RFB_PORT);
625 pcb = tcp_listen(pcb);
626 tcp_accept(pcb, rfb_accept);