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 64
22 #define SCREEN_CHUNKS_X 16
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_Y][SCREEN_CHUNKS_X];
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;
172 switch (state->send_state) {
175 if (state->update_requested) {
176 outputf("RFB send: update requested");
177 state->update_requested = 0;
178 state->send_state = SST_NEEDS_UPDATE;
183 /* FALL THROUGH to SST_NEEDS_UPDATE */
185 case SST_NEEDS_UPDATE:
187 state->chunk_xnum = 0;
188 state->chunk_ynum = 0;
189 state->chunk_width = 0;
190 state->chunk_height = 0;
191 state->chunk_lindex = 0;
192 state->send_state = SST_SENDING;
194 /* FALL THROUGH to SST_SENDING */
199 lines_left = state->chunk_height - state->chunk_lindex;
201 if (lines_left == 0) {
202 outputf("RFB: (%d [%d], %d [%d]), advancing",
203 state->chunk_xnum, state->chunk_xpos,
204 state->chunk_ynum, state->chunk_ypos);
206 /* Advance to the next chunk if necessary. If
207 * state->chunk_height is zero, then we are
208 * arriving here for the first time from
209 * SST_NEEDS_UPDATE. */
211 if (state->chunk_height != 0) {
212 state->chunk_xnum += 1;
215 if (state->chunk_xnum == SCREEN_CHUNKS_X) {
216 state->chunk_ynum += 1;
217 state->chunk_xnum = 0;
220 if (state->chunk_ynum == SCREEN_CHUNKS_Y) {
221 state->send_state = SST_IDLE;
225 outputf("RFB send: sending header");
227 /* Calculate the width and height for this chunk, remembering
228 * that if SCREEN_CHUNKS_[XY] do not evenly divide the width and
229 * height, we may need to have shorter chunks at the edge of
232 state->chunk_width = fb->curmode.xres / SCREEN_CHUNKS_X;
233 if (fb->curmode.xres % SCREEN_CHUNKS_X != 0)
234 state->chunk_width += 1;
235 state->chunk_xpos = state->chunk_width * state->chunk_xnum;
236 totaldim = state->chunk_width * (state->chunk_xnum + 1);
237 if (totaldim > fb->curmode.xres) {
238 state->chunk_width -= (totaldim - fb->curmode.xres);
241 state->chunk_height = fb->curmode.yres / SCREEN_CHUNKS_Y;
242 if (fb->curmode.yres % SCREEN_CHUNKS_Y != 0)
243 state->chunk_height += 1;
244 state->chunk_ypos = state->chunk_height
246 totaldim = state->chunk_height * (state->chunk_ynum + 1);
247 if (totaldim > fb->curmode.yres) {
248 state->chunk_height -= (totaldim - fb->curmode.yres);
253 hdr.nrects = htons(1);
254 hdr.xpos = htons(state->chunk_xpos);
255 hdr.ypos = htons(state->chunk_ypos);
256 hdr.width = htons(state->chunk_width);
257 hdr.height= htons(state->chunk_height);
258 hdr.enctype = htonl(0);
259 state->chunk_lindex = 0;
260 lines_left = state->chunk_height;
262 err = tcp_write(pcb, &hdr, sizeof(hdr), TCP_WRITE_FLAG_COPY);
266 outputf("RFB: header send error %d", err);
268 /* Crap. Reset chunk_height to 0 so that next time around,
269 * we'll recalculate this chunk (not advance) and try to
270 * send the header again.
272 state->chunk_height = 0;
277 outputf("RFB: (%d [%d], %d [%d]), %d x %d, line %d",
278 state->chunk_xnum, state->chunk_xpos,
279 state->chunk_ynum, state->chunk_ypos,
280 state->chunk_width, state->chunk_height,
281 state->chunk_lindex);
284 + (fb->curmode.xres * fb->curmode.bytestride
285 * (state->chunk_ypos + state->chunk_lindex))
286 + (state->chunk_xpos * fb->curmode.bytestride);
288 /* The network card can't DMA from video RAM,
289 * so use TCP_WRITE_FLAG_COPY. */
290 err = tcp_write(pcb, lptr,
291 fb->curmode.bytestride * state->chunk_width,
292 TCP_WRITE_FLAG_COPY);
295 state->chunk_lindex += 1;
298 } while (err == ERR_OK && state->chunk_lindex < state->chunk_height);
302 outputf("RFB: send error %d", err);
304 outputf("RFB: that's all for now");
308 if (tcp_sndbuf(pcb) == 0) {
316 if (tcp_output(pcb) != ERR_OK)
317 outputf("RFB: tcp_output bailed in send_fsm?");
320 static err_t rfb_sent(void *arg, struct tcp_pcb *pcb, uint16_t len) {
321 struct rfb_state *state = arg;
322 send_fsm(pcb, state);
326 static err_t rfb_poll(void *arg, struct tcp_pcb *pcb) {
327 struct rfb_state *state = arg;
328 send_fsm(pcb, state);
333 static void close_conn(struct tcp_pcb *pcb, struct rfb_state *state) {
347 static enum fsm_result recv_fsm(struct tcp_pcb *pcb, struct rfb_state *state) {
351 outputf("RFB FSM: st %d rp %d wp %d", state->state, state->readpos,
354 switch(state->state) {
356 if (state->writepos < 12) return NEEDMORE;
358 if (!strncmp(state->data, "RFB 003.003\n", 12)) {
360 } else if (!strncmp(state->data, "RFB 003.005\n", 12)) {
361 /* Spec states that "RFB 003.005", an incorrect value,
362 * should be treated by the server as 3.3. */
364 } else if (!strncmp(state->data, "RFB 003.007\n", 12)) {
366 } else if (!strncmp(state->data, "RFB 003.008\n", 12)) {
369 outputf("RFB: Negotiation fail");
373 outputf("RFB: Negotiated v3.%d", state->version);
375 state->readpos += 12;
376 state->state = ST_CLIENTINIT;
378 /* We support one security type, currently "none".
379 * Send that and SecurityResult. */
380 if (state->version >= 7) {
381 tcp_write(pcb, "\x01\x01\x00\x00\x00\x00", 6, 0);
383 tcp_write(pcb, "\x01\x00\x00\x00\x00", 5, 0);
391 if (state->version >= 7) {
392 /* Ignore the security type and ClientInit */
393 if (state->writepos < 2) return NEEDMORE;
396 /* Just ClientInit */
397 if (state->writepos < 1) return NEEDMORE;
401 state->state = ST_MAIN;
403 outputf("RFB: Sending server info", state->version);
404 tcp_write(pcb, &server_info, sizeof(server_info), TCP_WRITE_FLAG_COPY);
410 if (state->writepos < 1) return NEEDMORE;
412 outputf("RFB: cmd %d", state->data[0]);
413 switch (state->data[0]) {
415 case SET_PIXEL_FORMAT:
417 if (state->writepos < (sizeof(struct pixel_format) + 4))
419 outputf("RFB: SetPixelFormat");
421 struct pixel_format * new_fmt =
422 (struct pixel_format *)(&state->data[4]);
426 state->readpos += sizeof(struct pixel_format) + 4;
430 if (state->writepos < 4) return NEEDMORE;
432 struct set_encs_req * req = (struct set_encs_req *)state->data;
434 pktsize = sizeof(struct set_encs_req) + (4 * ntohs(req->num));
436 outputf("RFB: SetEncodings [%d]", ntohs(req->num));
437 if (state->writepos < pktsize) return NEEDMORE;
439 for (i = 0; i < ntohs(req->num); i++) {
440 outputf("RFB: Encoding: %d", ntohl(req->encodings[i]));
444 state->readpos += pktsize;
447 case FB_UPDATE_REQUEST:
448 if (state->writepos < sizeof(struct fb_update_req))
450 outputf("RFB: UpdateRequest");
452 state->update_requested = 1;
453 memcpy(&state->client_interest_area, state->data,
454 sizeof(struct fb_update_req));
456 state->readpos += sizeof(struct fb_update_req);
460 if (state->writepos < sizeof(struct key_event_pkt))
463 struct key_event_pkt * p = (struct key_event_pkt *)state->data;
465 outputf("RFB: Key: %d (%c)", htonl(p->keysym), (htonl(p->keysym) & 0xFF));
466 kbd_inject_keysym(htonl(p->keysym), p->downflag);
468 state->readpos += sizeof(struct key_event_pkt);
472 if (state->writepos < sizeof(struct pointer_event_pkt))
474 outputf("RFB: Pointer");
478 state->readpos += sizeof(struct pointer_event_pkt);
481 case CLIENT_CUT_TEXT:
482 if (state->writepos < sizeof(struct text_event_pkt))
484 outputf("RFB: Cut Text");
486 struct text_event_pkt * pkt =
487 (struct text_event_pkt *)state->data;
489 if (state->writepos < sizeof(struct text_event_pkt)
495 state->readpos += sizeof(struct text_event_pkt)
500 outputf("RFB: Bad command: %d", state->data[0]);
503 outputf("RFB: Bad state");
508 static err_t rfb_recv(void *arg, struct tcp_pcb *pcb,
509 struct pbuf *p, err_t err) {
510 struct rfb_state *state = arg;
515 outputf("RFB: recv err %d", err);
516 /* FIXME do something better here? */
521 outputf("RFB: Connection closed");
522 close_conn(pcb, state);
526 if (p->tot_len > (RFB_BUF_SIZE - state->writepos)) {
528 outputf("RFB: Overflow!");
529 close_conn(pcb, state);
533 outputf("RFB: Processing %d", p->tot_len);
534 pbuf_copy_partial(p, state->data + state->writepos, p->tot_len, 0);
535 state->writepos += p->tot_len;
537 tcp_recved(pcb, p->tot_len);
541 switch (recv_fsm(pcb, state)) {
543 outputf("RFB FSM: blocking");
548 outputf("RFB FSM: ok");
550 /* Kick off a send. */
551 if (state->send_state == SST_IDLE
552 && state->update_requested) {
553 send_fsm(pcb, state);
556 if (state->readpos == state->writepos) {
562 state->data + state->readpos,
563 state->writepos - state->readpos);
568 outputf("RFB: Protocol error");
569 close_conn(pcb, state);
575 static err_t rfb_accept(void *arg, struct tcp_pcb *pcb, err_t err) {
576 struct rfb_state *state;
578 LWIP_UNUSED_ARG(arg);
579 LWIP_UNUSED_ARG(err);
581 state = (struct rfb_state *)mem_malloc(sizeof(struct rfb_state));
583 state->state = ST_BEGIN;
586 state->update_requested = 0;
587 state->send_state = SST_IDLE;
589 /* XXX: update_server_info() should be called from the 64ms timer, and deal
590 * with screen resizes appropriately. */
591 update_server_info();
595 outputf("rfb_accept: out of memory\n");
600 tcp_recv(pcb, rfb_recv);
601 tcp_sent(pcb, rfb_sent);
602 tcp_poll(pcb, rfb_poll, 1);
604 tcp_err(pcb, rfb_err);
606 tcp_write(pcb, "RFB 003.008\n", 12, 0);
618 tcp_bind(pcb, IP_ADDR_ANY, RFB_PORT);
619 pcb = tcp_listen(pcb);
620 tcp_accept(pcb, rfb_accept);