]>
Commit | Line | Data |
---|---|---|
7da36fbd JP |
1 | #include <stdint.h> |
2 | #include <minilib.h> | |
3 | #include <output.h> | |
4 | #include <fb.h> | |
5 | ||
ec949d20 JP |
6 | #include "../aseg-paging/keyboard.h" |
7 | ||
7da36fbd | 8 | #include "lwip/tcp.h" |
b59a6e50 | 9 | #include "lwip/stats.h" |
7da36fbd JP |
10 | |
11 | #include "rfb.h" | |
12 | ||
13 | #define SET_PIXEL_FORMAT 0 | |
14 | #define SET_ENCODINGS 2 | |
15 | #define FB_UPDATE_REQUEST 3 | |
16 | #define KEY_EVENT 4 | |
17 | #define POINTER_EVENT 5 | |
18 | #define CLIENT_CUT_TEXT 6 | |
19 | ||
99a4c846 | 20 | #define RFB_BUF_SIZE 512 |
7da36fbd | 21 | |
d9b82f8c | 22 | #define SCREEN_CHUNKS_X 8 |
b496c2b1 JP |
23 | #define SCREEN_CHUNKS_Y 8 |
24 | ||
7da36fbd JP |
25 | struct pixel_format { |
26 | uint8_t bpp; | |
27 | uint8_t depth; | |
28 | uint8_t big_endian; | |
29 | uint8_t true_color; | |
30 | uint16_t red_max; | |
31 | uint16_t green_max; | |
32 | uint16_t blue_max; | |
33 | uint8_t red_shift; | |
34 | uint8_t green_shift; | |
35 | uint8_t blue_shift; | |
36 | uint8_t padding[3]; | |
37 | }; | |
38 | ||
39 | struct server_init_message { | |
40 | uint16_t fb_width; | |
41 | uint16_t fb_height; | |
42 | struct pixel_format fmt; | |
43 | uint32_t name_length; | |
44 | char name_string[8]; | |
45 | }; | |
46 | ||
47 | struct fb_update_req { | |
48 | uint8_t msgtype; | |
49 | uint8_t incremental; | |
50 | uint16_t xpos; | |
51 | uint16_t ypos; | |
52 | uint16_t width; | |
53 | uint16_t height; | |
54 | }; | |
55 | ||
56 | struct set_encs_req { | |
57 | uint8_t msgtype; | |
58 | uint8_t padding; | |
59 | uint16_t num; | |
60 | int32_t encodings[]; | |
61 | }; | |
62 | ||
63 | struct key_event_pkt { | |
64 | uint8_t msgtype; | |
65 | uint8_t downflag; | |
66 | uint8_t pad[2]; | |
67 | uint32_t keysym; | |
68 | }; | |
69 | ||
70 | struct pointer_event_pkt { | |
71 | uint8_t msgtype; | |
72 | uint8_t button_mask; | |
73 | uint16_t x; | |
74 | uint16_t y; | |
75 | }; | |
76 | ||
77 | struct text_event_pkt { | |
78 | uint8_t msgtype; | |
79 | uint8_t padding[3]; | |
80 | uint32_t length; | |
81 | char text[]; | |
82 | }; | |
83 | ||
8ceb0515 JP |
84 | struct update_header { |
85 | uint8_t msgtype; | |
86 | uint8_t padding; | |
87 | uint16_t nrects; | |
88 | uint16_t xpos; | |
89 | uint16_t ypos; | |
90 | uint16_t width; | |
91 | uint16_t height; | |
92 | int32_t enctype; | |
93 | }; | |
94 | ||
7da36fbd JP |
95 | struct rfb_state { |
96 | enum { | |
97 | ST_BEGIN, | |
98 | ST_CLIENTINIT, | |
99 | ST_MAIN | |
100 | } state; | |
101 | int version; | |
102 | int encs_remaining; | |
103 | ||
104 | char data[RFB_BUF_SIZE]; | |
105 | int readpos; | |
106 | int writepos; | |
107 | ||
108 | char next_update_incremental; | |
8ceb0515 JP |
109 | char update_requested; |
110 | ||
7da36fbd JP |
111 | struct fb_update_req client_interest_area; |
112 | ||
8ceb0515 JP |
113 | enum { |
114 | SST_IDLE, | |
d9b82f8c JP |
115 | SST_HEADER, |
116 | SST_DATA | |
8ceb0515 JP |
117 | } send_state; |
118 | ||
99a4c846 | 119 | uint32_t checksums[SCREEN_CHUNKS_X][SCREEN_CHUNKS_Y]; |
b496c2b1 JP |
120 | |
121 | uint32_t chunk_xnum; | |
122 | uint32_t chunk_ynum; | |
123 | uint32_t chunk_xpos; | |
124 | uint32_t chunk_ypos; | |
125 | uint32_t chunk_width; | |
126 | uint32_t chunk_height; | |
127 | ||
128 | uint32_t chunk_lindex; | |
d136cf4c JW |
129 | |
130 | uint32_t chunk_checksum; | |
d9b82f8c JP |
131 | |
132 | uint32_t chunk_actually_sent; | |
7da36fbd JP |
133 | }; |
134 | ||
135 | static struct server_init_message server_info; | |
136 | ||
137 | static void init_server_info() { | |
80726808 | 138 | server_info.name_length = htonl(8); |
7da36fbd JP |
139 | memcpy(server_info.name_string, "NetWatch", 8); |
140 | } | |
141 | ||
142 | static void update_server_info() { | |
143 | if (fb != NULL) { | |
80726808 JP |
144 | outputf("RFB: setting fmt %d", fb->curmode.format); |
145 | server_info.fb_width = htons(fb->curmode.xres); | |
146 | server_info.fb_height = htons(fb->curmode.yres); | |
7da36fbd JP |
147 | switch (fb->curmode.format) { |
148 | case FB_RGB888: | |
149 | server_info.fmt.bpp = 32; | |
150 | server_info.fmt.depth = 24; | |
151 | server_info.fmt.big_endian = 0; | |
152 | server_info.fmt.true_color = 1; | |
80726808 JP |
153 | server_info.fmt.red_max = htons(255); |
154 | server_info.fmt.green_max = htons(255); | |
155 | server_info.fmt.blue_max = htons(255); | |
7da36fbd JP |
156 | server_info.fmt.red_shift = 0; |
157 | server_info.fmt.green_shift = 8; | |
158 | server_info.fmt.blue_shift = 16; | |
159 | break; | |
160 | default: | |
161 | outputf("RFB: unknown fb fmt %d", fb->curmode.format); | |
162 | break; | |
163 | } | |
80726808 JP |
164 | } else { |
165 | outputf("RFB: fb null"); | |
7da36fbd JP |
166 | } |
167 | } | |
168 | ||
d9b82f8c JP |
169 | static int advance_chunk(struct rfb_state *state) { |
170 | ||
171 | state->chunk_xnum += 1; | |
172 | ||
173 | if (state->chunk_xnum == SCREEN_CHUNKS_X) { | |
174 | state->chunk_ynum += 1; | |
175 | state->chunk_xnum = 0; | |
176 | } | |
177 | ||
178 | if (state->chunk_ynum == SCREEN_CHUNKS_Y) { | |
179 | state->chunk_ynum = 0; | |
180 | state->send_state = SST_IDLE; | |
181 | if (!(state->chunk_actually_sent)) | |
182 | state->update_requested = 1; | |
183 | return 1; | |
184 | } | |
185 | ||
186 | return 0; | |
187 | } | |
188 | ||
8ceb0515 JP |
189 | static void send_fsm(struct tcp_pcb *pcb, struct rfb_state *state) { |
190 | struct update_header hdr; | |
b496c2b1 | 191 | int lines_left; |
d9b82f8c | 192 | unsigned char * lptr; |
b496c2b1 | 193 | int totaldim; |
8ceb0515 JP |
194 | err_t err; |
195 | ||
d9b82f8c | 196 | while(1) { |
b496c2b1 | 197 | |
d9b82f8c | 198 | switch (state->send_state) { |
b496c2b1 | 199 | |
d9b82f8c JP |
200 | case SST_IDLE: |
201 | /* Nothing to do */ | |
8ceb0515 | 202 | |
d9b82f8c JP |
203 | if (state->update_requested) { |
204 | outputf("RFB send: update requested"); | |
205 | state->update_requested = 0; | |
206 | state->chunk_actually_sent = 0; | |
207 | state->send_state = SST_HEADER; | |
208 | } else { | |
209 | return; | |
210 | } | |
211 | ||
212 | /* FALL THROUGH to SST_HEADER */ | |
213 | ||
214 | case SST_HEADER: | |
215 | ||
216 | /* Calculate the width and height for this chunk, remembering | |
217 | * that if SCREEN_CHUNKS_[XY] do not evenly divide the width and | |
218 | * height, we may need to have shorter chunks at the edge of | |
219 | * the screen. */ | |
220 | ||
221 | state->chunk_width = fb->curmode.xres / SCREEN_CHUNKS_X; | |
222 | if (fb->curmode.xres % SCREEN_CHUNKS_X != 0) | |
223 | state->chunk_width += 1; | |
224 | state->chunk_xpos = state->chunk_width * state->chunk_xnum; | |
225 | totaldim = state->chunk_width * (state->chunk_xnum + 1); | |
226 | if (totaldim > fb->curmode.xres) { | |
227 | state->chunk_width -= (totaldim - fb->curmode.xres); | |
228 | } | |
b496c2b1 | 229 | |
d9b82f8c JP |
230 | state->chunk_height = fb->curmode.yres / SCREEN_CHUNKS_Y; |
231 | if (fb->curmode.yres % SCREEN_CHUNKS_Y != 0) | |
232 | state->chunk_height += 1; | |
233 | state->chunk_ypos = state->chunk_height | |
234 | * state->chunk_ynum; | |
235 | totaldim = state->chunk_height * (state->chunk_ynum + 1); | |
236 | if (totaldim > fb->curmode.yres) { | |
237 | state->chunk_height -= (totaldim - fb->curmode.yres); | |
238 | } | |
8ceb0515 | 239 | |
d9b82f8c JP |
240 | outputf("rfb send: (%d [%d], %d [%d]) %d x %d", |
241 | state->chunk_xnum, state->chunk_xpos, | |
242 | state->chunk_ynum, state->chunk_ypos, | |
243 | state->chunk_width, state->chunk_height); | |
244 | ||
245 | /* Do we _actually_ need to send this chunk? */ | |
246 | if (fb->checksum_rect) { | |
d136cf4c JW |
247 | state->chunk_checksum = fb->checksum_rect(state->chunk_xpos, state->chunk_ypos, |
248 | state->chunk_width, state->chunk_height); | |
d9b82f8c | 249 | |
d136cf4c JW |
250 | if (state->chunk_checksum == state->checksums[state->chunk_xnum][state->chunk_ynum]) { |
251 | outputf("!!!!!!! SKIPPING: %08x", state->chunk_checksum); | |
d9b82f8c JP |
252 | if (advance_chunk(state)) |
253 | return; | |
254 | continue; | |
d9b82f8c | 255 | } |
d136cf4c | 256 | /* Checksum gets set in data block, AFTER the data has been sent. */ |
d9b82f8c | 257 | } |
b496c2b1 | 258 | |
d9b82f8c JP |
259 | outputf("actually sent"); |
260 | state->chunk_actually_sent = 1; | |
b496c2b1 | 261 | |
d9b82f8c JP |
262 | /* Send a header */ |
263 | hdr.msgtype = 0; | |
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; | |
b496c2b1 | 272 | |
d9b82f8c | 273 | err = tcp_write(pcb, &hdr, sizeof(hdr), TCP_WRITE_FLAG_COPY); |
b496c2b1 | 274 | |
d9b82f8c JP |
275 | if (err != ERR_OK) { |
276 | if (err != ERR_MEM) | |
277 | outputf("RFB: header send error %d", err); | |
b496c2b1 | 278 | |
d9b82f8c JP |
279 | /* Try again later. */ |
280 | return; | |
281 | } | |
b496c2b1 | 282 | |
d9b82f8c | 283 | state->send_state = SST_DATA; |
b496c2b1 | 284 | |
d9b82f8c | 285 | /* FALL THROUGH to SST_DATA */ |
99a4c846 | 286 | |
d9b82f8c | 287 | case SST_DATA: |
8ceb0515 | 288 | |
d9b82f8c | 289 | lines_left = state->chunk_height - state->chunk_lindex; |
b496c2b1 | 290 | |
d9b82f8c JP |
291 | if (lines_left == 0) { |
292 | state->send_state = SST_HEADER; | |
d136cf4c | 293 | state->checksums[state->chunk_xnum][state->chunk_ynum] = state->chunk_checksum; |
d9b82f8c JP |
294 | if (advance_chunk(state)) |
295 | return; | |
296 | break; | |
297 | } | |
b496c2b1 | 298 | |
d9b82f8c JP |
299 | lptr = fb->fbaddr |
300 | + (fb->curmode.xres * fb->curmode.bytestride | |
301 | * (state->chunk_ypos + state->chunk_lindex)) | |
302 | + (state->chunk_xpos * fb->curmode.bytestride); | |
b496c2b1 | 303 | |
d9b82f8c JP |
304 | /* The network card can't DMA from video RAM, |
305 | * so use TCP_WRITE_FLAG_COPY. */ | |
306 | err = tcp_write(pcb, lptr, | |
307 | fb->curmode.bytestride * state->chunk_width, TCP_WRITE_FLAG_COPY); | |
bbfab433 | 308 | |
d9b82f8c JP |
309 | if (err == ERR_OK) { |
310 | state->chunk_lindex += 1; | |
311 | } else { | |
8cc40c4d JW |
312 | if (err != ERR_MEM) |
313 | outputf("RFB: send error %d", err); | |
b496c2b1 | 314 | |
d9b82f8c | 315 | return; |
bbfab433 | 316 | } |
b496c2b1 | 317 | |
bbfab433 | 318 | if (tcp_sndbuf(pcb) == 0) { |
d9b82f8c | 319 | return; |
bbfab433 | 320 | } |
8ceb0515 | 321 | } |
8ceb0515 | 322 | } |
075bbc71 JW |
323 | |
324 | if (tcp_output(pcb) != ERR_OK) | |
075bbc71 | 325 | outputf("RFB: tcp_output bailed in send_fsm?"); |
8ceb0515 JP |
326 | } |
327 | ||
328 | static err_t rfb_sent(void *arg, struct tcp_pcb *pcb, uint16_t len) { | |
329 | struct rfb_state *state = arg; | |
330 | send_fsm(pcb, state); | |
331 | return ERR_OK; | |
80726808 JP |
332 | } |
333 | ||
b59a6e50 JP |
334 | static err_t rfb_poll(void *arg, struct tcp_pcb *pcb) { |
335 | struct rfb_state *state = arg; | |
336 | send_fsm(pcb, state); | |
99a4c846 | 337 | /* |
b59a6e50 | 338 | stats_display(); |
99a4c846 | 339 | */ |
b59a6e50 JP |
340 | return ERR_OK; |
341 | } | |
342 | ||
7da36fbd JP |
343 | static void close_conn(struct tcp_pcb *pcb, struct rfb_state *state) { |
344 | tcp_arg(pcb, NULL); | |
345 | tcp_sent(pcb, NULL); | |
346 | tcp_recv(pcb, NULL); | |
347 | mem_free(state); | |
348 | tcp_close(pcb); | |
349 | } | |
350 | ||
351 | enum fsm_result { | |
352 | NEEDMORE, | |
353 | OK, | |
354 | FAIL | |
355 | }; | |
356 | ||
357 | static enum fsm_result recv_fsm(struct tcp_pcb *pcb, struct rfb_state *state) { | |
80726808 JP |
358 | int i; |
359 | int pktsize; | |
360 | ||
361 | outputf("RFB FSM: st %d rp %d wp %d", state->state, state->readpos, | |
362 | state->writepos); | |
7da36fbd JP |
363 | |
364 | switch(state->state) { | |
365 | case ST_BEGIN: | |
366 | if (state->writepos < 12) return NEEDMORE; | |
367 | ||
368 | if (!strncmp(state->data, "RFB 003.003\n", 12)) { | |
369 | state->version = 3; | |
370 | } else if (!strncmp(state->data, "RFB 003.005\n", 12)) { | |
371 | /* Spec states that "RFB 003.005", an incorrect value, | |
372 | * should be treated by the server as 3.3. */ | |
373 | state->version = 3; | |
374 | } else if (!strncmp(state->data, "RFB 003.007\n", 12)) { | |
375 | state->version = 7; | |
376 | } else if (!strncmp(state->data, "RFB 003.008\n", 12)) { | |
377 | state->version = 8; | |
378 | } else { | |
379 | outputf("RFB: Negotiation fail"); | |
380 | return FAIL; | |
381 | } | |
382 | ||
383 | outputf("RFB: Negotiated v3.%d", state->version); | |
384 | ||
385 | state->readpos += 12; | |
386 | state->state = ST_CLIENTINIT; | |
387 | ||
80726808 JP |
388 | /* We support one security type, currently "none". |
389 | * Send that and SecurityResult. */ | |
7da36fbd | 390 | if (state->version >= 7) { |
80726808 | 391 | tcp_write(pcb, "\x01\x01\x00\x00\x00\x00", 6, 0); |
7da36fbd | 392 | } else { |
80726808 | 393 | tcp_write(pcb, "\x01\x00\x00\x00\x00", 5, 0); |
7da36fbd JP |
394 | } |
395 | ||
7da36fbd JP |
396 | tcp_output(pcb); |
397 | ||
398 | return OK; | |
399 | ||
400 | case ST_CLIENTINIT: | |
80726808 JP |
401 | if (state->version >= 7) { |
402 | /* Ignore the security type and ClientInit */ | |
403 | if (state->writepos < 2) return NEEDMORE; | |
404 | state->readpos += 2; | |
405 | } else { | |
406 | /* Just ClientInit */ | |
407 | if (state->writepos < 1) return NEEDMORE; | |
408 | state->readpos += 1; | |
409 | } | |
410 | ||
7da36fbd JP |
411 | state->state = ST_MAIN; |
412 | ||
80726808 | 413 | outputf("RFB: Sending server info", state->version); |
075bbc71 | 414 | tcp_write(pcb, &server_info, sizeof(server_info), TCP_WRITE_FLAG_COPY); |
7da36fbd JP |
415 | tcp_output(pcb); |
416 | ||
417 | return OK; | |
418 | ||
419 | case ST_MAIN: | |
420 | if (state->writepos < 1) return NEEDMORE; | |
421 | ||
80726808 | 422 | outputf("RFB: cmd %d", state->data[0]); |
7da36fbd JP |
423 | switch (state->data[0]) { |
424 | ||
425 | case SET_PIXEL_FORMAT: | |
426 | /* SetPixelFormat */ | |
427 | if (state->writepos < (sizeof(struct pixel_format) + 4)) | |
428 | return NEEDMORE; | |
80726808 | 429 | outputf("RFB: SetPixelFormat"); |
7da36fbd JP |
430 | /* |
431 | struct pixel_format * new_fmt = | |
432 | (struct pixel_format *)(&state->data[4]); | |
433 | */ | |
434 | /* XXX ... */ | |
435 | ||
436 | state->readpos += sizeof(struct pixel_format) + 4; | |
437 | return OK; | |
438 | ||
439 | case SET_ENCODINGS: | |
440 | if (state->writepos < 4) return NEEDMORE; | |
441 | ||
442 | struct set_encs_req * req = (struct set_encs_req *)state->data; | |
443 | ||
80726808 | 444 | pktsize = sizeof(struct set_encs_req) + (4 * ntohs(req->num)); |
7da36fbd | 445 | |
80726808 JP |
446 | outputf("RFB: SetEncodings [%d]", ntohs(req->num)); |
447 | if (state->writepos < pktsize) return NEEDMORE; | |
448 | ||
449 | for (i = 0; i < ntohs(req->num); i++) { | |
450 | outputf("RFB: Encoding: %d", ntohl(req->encodings[i])); | |
451 | /* XXX ... */ | |
80726808 JP |
452 | } |
453 | ||
454 | state->readpos += pktsize; | |
7da36fbd JP |
455 | return OK; |
456 | ||
457 | case FB_UPDATE_REQUEST: | |
458 | if (state->writepos < sizeof(struct fb_update_req)) | |
459 | return NEEDMORE; | |
80726808 | 460 | outputf("RFB: UpdateRequest"); |
7da36fbd | 461 | |
8ceb0515 | 462 | state->update_requested = 1; |
7da36fbd JP |
463 | memcpy(&state->client_interest_area, state->data, |
464 | sizeof(struct fb_update_req)); | |
465 | ||
466 | state->readpos += sizeof(struct fb_update_req); | |
467 | return OK; | |
468 | ||
469 | case KEY_EVENT: | |
470 | if (state->writepos < sizeof(struct key_event_pkt)) | |
471 | return NEEDMORE; | |
472 | ||
ec949d20 | 473 | struct key_event_pkt * p = (struct key_event_pkt *)state->data; |
dadcd4fc JP |
474 | |
475 | outputf("RFB: Key: %d (%c)", htonl(p->keysym), (htonl(p->keysym) & 0xFF)); | |
ec949d20 | 476 | kbd_inject_keysym(htonl(p->keysym), p->downflag); |
7da36fbd JP |
477 | |
478 | state->readpos += sizeof(struct key_event_pkt); | |
479 | return OK; | |
480 | ||
481 | case POINTER_EVENT: | |
482 | if (state->writepos < sizeof(struct pointer_event_pkt)) | |
483 | return NEEDMORE; | |
80726808 | 484 | outputf("RFB: Pointer"); |
7da36fbd JP |
485 | |
486 | /* XXX stub */ | |
487 | ||
488 | state->readpos += sizeof(struct pointer_event_pkt); | |
489 | return OK; | |
490 | ||
491 | case CLIENT_CUT_TEXT: | |
492 | if (state->writepos < sizeof(struct text_event_pkt)) | |
493 | return NEEDMORE; | |
80726808 | 494 | outputf("RFB: Cut Text"); |
7da36fbd JP |
495 | |
496 | struct text_event_pkt * pkt = | |
497 | (struct text_event_pkt *)state->data; | |
498 | ||
499 | if (state->writepos < sizeof(struct text_event_pkt) | |
d9b82f8c | 500 | + pkt->length) |
7da36fbd JP |
501 | return NEEDMORE; |
502 | ||
503 | /* XXX stub */ | |
504 | ||
505 | state->readpos += sizeof(struct text_event_pkt) | |
d9b82f8c | 506 | + pkt->length; |
7da36fbd JP |
507 | return OK; |
508 | ||
509 | default: | |
510 | outputf("RFB: Bad command: %d", state->data[0]); | |
511 | } | |
512 | default: | |
513 | outputf("RFB: Bad state"); | |
514 | return FAIL; | |
515 | } | |
516 | } | |
517 | ||
518 | static err_t rfb_recv(void *arg, struct tcp_pcb *pcb, | |
d9b82f8c | 519 | struct pbuf *p, err_t err) { |
7da36fbd JP |
520 | struct rfb_state *state = arg; |
521 | ||
522 | if (state == NULL) | |
523 | ||
524 | if (err != ERR_OK) { | |
525 | outputf("RFB: recv err %d", err); | |
526 | /* FIXME do something better here? */ | |
527 | return ERR_OK; | |
528 | } | |
529 | ||
530 | if (p == NULL) { | |
531 | outputf("RFB: Connection closed"); | |
532 | close_conn(pcb, state); | |
533 | return ERR_OK; | |
534 | } | |
535 | ||
536 | if (p->tot_len > (RFB_BUF_SIZE - state->writepos)) { | |
537 | /* Overflow! */ | |
538 | outputf("RFB: Overflow!"); | |
539 | close_conn(pcb, state); | |
540 | return ERR_OK; | |
541 | } | |
542 | ||
543 | outputf("RFB: Processing %d", p->tot_len); | |
544 | pbuf_copy_partial(p, state->data + state->writepos, p->tot_len, 0); | |
80726808 JP |
545 | state->writepos += p->tot_len; |
546 | ||
7da36fbd JP |
547 | tcp_recved(pcb, p->tot_len); |
548 | pbuf_free(p); | |
549 | ||
550 | while (1) { | |
551 | switch (recv_fsm(pcb, state)) { | |
552 | case NEEDMORE: | |
80726808 | 553 | outputf("RFB FSM: blocking"); |
7da36fbd JP |
554 | /* Need more data */ |
555 | return ERR_OK; | |
556 | ||
557 | case OK: | |
80726808 JP |
558 | outputf("RFB FSM: ok"); |
559 | ||
03bcc4db | 560 | /* Kick off a send. */ |
8ceb0515 JP |
561 | if (state->send_state == SST_IDLE |
562 | && state->update_requested) { | |
563 | send_fsm(pcb, state); | |
80726808 JP |
564 | } |
565 | ||
7da36fbd JP |
566 | if (state->readpos == state->writepos) { |
567 | state->readpos = 0; | |
568 | state->writepos = 0; | |
569 | return ERR_OK; | |
570 | } else { | |
571 | memmove(state->data, | |
d9b82f8c JP |
572 | state->data + state->readpos, |
573 | state->writepos - state->readpos); | |
7da36fbd JP |
574 | } |
575 | break; | |
576 | case FAIL: | |
577 | /* Shit */ | |
578 | outputf("RFB: Protocol error"); | |
579 | close_conn(pcb, state); | |
580 | return ERR_OK; | |
581 | } | |
582 | } | |
583 | } | |
584 | ||
585 | static err_t rfb_accept(void *arg, struct tcp_pcb *pcb, err_t err) { | |
586 | struct rfb_state *state; | |
587 | ||
588 | LWIP_UNUSED_ARG(arg); | |
589 | LWIP_UNUSED_ARG(err); | |
590 | ||
591 | state = (struct rfb_state *)mem_malloc(sizeof(struct rfb_state)); | |
592 | ||
80726808 JP |
593 | state->state = ST_BEGIN; |
594 | state->readpos = 0; | |
595 | state->writepos = 0; | |
8ceb0515 JP |
596 | state->update_requested = 0; |
597 | state->send_state = SST_IDLE; | |
9eee8073 JP |
598 | state->chunk_xnum = 0; |
599 | state->chunk_ynum = 0; | |
d9b82f8c | 600 | memset(state->checksums, 0, sizeof(state->checksums)); |
80726808 | 601 | |
7da36fbd JP |
602 | /* XXX: update_server_info() should be called from the 64ms timer, and deal |
603 | * with screen resizes appropriately. */ | |
604 | update_server_info(); | |
605 | ||
606 | if (!state) | |
607 | { | |
608 | outputf("rfb_accept: out of memory\n"); | |
609 | return ERR_MEM; | |
610 | } | |
611 | ||
612 | tcp_arg(pcb, state); | |
613 | tcp_recv(pcb, rfb_recv); | |
8ceb0515 | 614 | tcp_sent(pcb, rfb_sent); |
b59a6e50 | 615 | tcp_poll(pcb, rfb_poll, 1); |
7da36fbd JP |
616 | /* |
617 | tcp_err(pcb, rfb_err); | |
7da36fbd JP |
618 | */ |
619 | tcp_write(pcb, "RFB 003.008\n", 12, 0); | |
620 | tcp_output(pcb); | |
621 | ||
622 | return ERR_OK; | |
623 | } | |
624 | ||
625 | void rfb_init() { | |
626 | struct tcp_pcb *pcb; | |
627 | ||
628 | init_server_info(); | |
629 | ||
630 | pcb = tcp_new(); | |
631 | tcp_bind(pcb, IP_ADDR_ANY, RFB_PORT); | |
632 | pcb = tcp_listen(pcb); | |
633 | tcp_accept(pcb, rfb_accept); | |
634 | } |