]> Joshua Wise's Git repositories - netwatch.git/blob - net/rfb.c
more rfb work
[netwatch.git] / net / rfb.c
1 #include <stdint.h>
2 #include <minilib.h>
3 #include <output.h>
4 #include <fb.h>
5
6 #include "../aseg-paging/keyboard.h"
7
8 #include "lwip/tcp.h"
9 #include "lwip/stats.h"
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
20 #define RFB_BUF_SIZE    512
21
22 #define SCREEN_CHUNKS_X 4       
23 #define SCREEN_CHUNKS_Y 8
24
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
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
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;
109         char update_requested;
110
111         struct fb_update_req client_interest_area;
112
113         enum {
114                 SST_IDLE,
115                 SST_NEEDS_UPDATE,
116                 SST_SENDING
117         } send_state;
118
119         uint32_t checksums[SCREEN_CHUNKS_X][SCREEN_CHUNKS_Y];
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;
129 };
130
131 static struct server_init_message server_info;
132
133 static void init_server_info() {
134         server_info.name_length = htonl(8);
135         memcpy(server_info.name_string, "NetWatch", 8);
136 }
137
138 static void update_server_info() {
139         if (fb != NULL) {
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) {
144                 case FB_RGB888:
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;
155                         break;
156                 default:
157                         outputf("RFB: unknown fb fmt %d", fb->curmode.format);
158                         break;
159                 }
160         } else {
161                 outputf("RFB: fb null");
162         }
163 }
164
165 static void send_fsm(struct tcp_pcb *pcb, struct rfb_state *state) {
166         struct update_header hdr;
167         int lines_left;
168         char * lptr;
169         uint32_t checksum;
170         int totaldim;
171         err_t err;
172
173         switch (state->send_state) {
174         case SST_IDLE:
175                 /* Nothing to do */
176                 if (state->update_requested) {
177                         outputf("RFB send: update requested");
178                         state->update_requested = 0;
179                         state->send_state = SST_NEEDS_UPDATE;
180                 } else {
181                         break;
182                 }
183         
184                 /* FALL THROUGH to SST_NEEDS_UPDATE */
185
186         case SST_NEEDS_UPDATE:
187
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;
194
195                 /* FALL THROUGH to SST_SENDING */
196
197         case SST_SENDING:
198
199                 while (1) {
200                         lines_left = state->chunk_height - state->chunk_lindex;
201
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. */
207
208                                 if (state->chunk_height != 0) {
209                                         state->chunk_xnum += 1;
210                                 }
211
212                                 if (state->chunk_xnum == SCREEN_CHUNKS_X) {
213                                         state->chunk_ynum += 1;
214                                         state->chunk_xnum = 0;
215                                 }
216
217                                 if (state->chunk_ynum == SCREEN_CHUNKS_Y) {
218                                         state->send_state = SST_IDLE;
219                                         outputf("RFB: Screen update done! %d", state->update_requested);
220                                         break;
221                                 }
222
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
226                                  * the screen. */
227
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);
235                                 }
236
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
241                                                          * state->chunk_ynum;
242                                 totaldim = state->chunk_height * (state->chunk_ynum + 1);
243                                 if (totaldim > fb->curmode.yres) {
244                                         state->chunk_height -= (totaldim - fb->curmode.yres);
245                                 }
246
247                                 if (fb->checksum_rect) {
248                                         checksum = fb->checksum_rect(state->chunk_xpos, state->chunk_ypos,
249                                                                      state->chunk_width, state->chunk_height);
250
251                                         if (checksum == state->checksums[state->chunk_xnum][state->chunk_ynum]) {
252                                                 state->chunk_lindex = state->chunk_height;
253                                                 continue;
254                                         } else {
255                                                 state->checksums[state->chunk_xnum][state->chunk_ynum] = checksum;
256                                         }
257                                 }
258 /*
259                                 outputf("RFB send: sending header");
260 */
261                                 /* Send a header */
262                                 hdr.msgtype = 0;
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;
271
272                                 err = tcp_write(pcb, &hdr, sizeof(hdr), TCP_WRITE_FLAG_COPY);
273
274                                 if (err != ERR_OK) {
275                                         if (err != ERR_MEM)
276                                                 outputf("RFB: header send error %d", err);
277
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. 
281                                          */
282                                         state->chunk_height = 0;
283                                 }
284                         }
285
286                         do {
287                                 lptr = fb->fbaddr
288                                         + (fb->curmode.xres * fb->curmode.bytestride
289                                            * (state->chunk_ypos + state->chunk_lindex))
290                                         + (state->chunk_xpos * fb->curmode.bytestride);
291
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);
297
298                                 if (err == ERR_OK) {
299                                         state->chunk_lindex += 1;
300                                 }
301
302                         } while (err == ERR_OK && state->chunk_lindex < state->chunk_height);
303
304                         if (err != ERR_OK) {
305                                 if (err != ERR_MEM)
306                                         outputf("RFB: send error %d", err);
307
308                                 break;
309                         }
310                                 
311                         if (tcp_sndbuf(pcb) == 0) {
312                                 break;
313                         }
314                 }
315
316                 break;
317         }
318         
319         if (tcp_output(pcb) != ERR_OK)
320                 outputf("RFB: tcp_output bailed in send_fsm?");
321 }
322
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);
326         return ERR_OK;
327 }
328
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);
333 /*
334         stats_display();
335 */
336         return ERR_OK;
337 }
338
339 static void close_conn(struct tcp_pcb *pcb, struct rfb_state *state) {
340         tcp_arg(pcb, NULL);
341         tcp_sent(pcb, NULL);
342         tcp_recv(pcb, NULL);
343         mem_free(state);
344         tcp_close(pcb);
345 }
346
347 enum fsm_result {
348         NEEDMORE,
349         OK,
350         FAIL
351 };
352
353 static enum fsm_result recv_fsm(struct tcp_pcb *pcb, struct rfb_state *state) {
354         int i;
355         int pktsize;
356
357         outputf("RFB FSM: st %d rp %d wp %d", state->state, state->readpos,
358                 state->writepos);
359
360         switch(state->state) {
361         case ST_BEGIN:
362                 if (state->writepos < 12) return NEEDMORE;
363
364                 if (!strncmp(state->data, "RFB 003.003\n", 12)) {
365                         state->version = 3;
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. */
369                         state->version = 3;
370                 } else if (!strncmp(state->data, "RFB 003.007\n", 12)) {
371                         state->version = 7;
372                 } else if (!strncmp(state->data, "RFB 003.008\n", 12)) {
373                         state->version = 8;
374                 } else {
375                         outputf("RFB: Negotiation fail");
376                         return FAIL;
377                 }
378
379                 outputf("RFB: Negotiated v3.%d", state->version);
380
381                 state->readpos += 12;
382                 state->state = ST_CLIENTINIT;
383
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);
388                 } else {
389                         tcp_write(pcb, "\x01\x00\x00\x00\x00", 5, 0);
390                 }
391
392                 tcp_output(pcb);
393
394                 return OK;
395
396         case ST_CLIENTINIT:
397                 if (state->version >= 7) {
398                         /* Ignore the security type and ClientInit */
399                         if (state->writepos < 2) return NEEDMORE;
400                         state->readpos += 2;
401                 } else {
402                         /* Just ClientInit */
403                         if (state->writepos < 1) return NEEDMORE;
404                         state->readpos += 1;
405                 }
406
407                 state->state = ST_MAIN;
408
409                 outputf("RFB: Sending server info", state->version);
410                 tcp_write(pcb, &server_info, sizeof(server_info), TCP_WRITE_FLAG_COPY);
411                 tcp_output(pcb);
412
413                 return OK;
414
415         case ST_MAIN:
416                 if (state->writepos < 1) return NEEDMORE;
417
418                 outputf("RFB: cmd %d", state->data[0]);
419                 switch (state->data[0]) {
420
421                 case SET_PIXEL_FORMAT:
422                         /* SetPixelFormat */
423                         if (state->writepos < (sizeof(struct pixel_format) + 4))
424                                 return NEEDMORE;
425                         outputf("RFB: SetPixelFormat");
426 /*
427                         struct pixel_format * new_fmt =
428                                 (struct pixel_format *)(&state->data[4]);
429 */
430                         /* XXX ... */
431
432                         state->readpos += sizeof(struct pixel_format) + 4;
433                         return OK;
434
435                 case SET_ENCODINGS:
436                         if (state->writepos < 4) return NEEDMORE;
437
438                         struct set_encs_req * req = (struct set_encs_req *)state->data;
439
440                         pktsize = sizeof(struct set_encs_req) + (4 * ntohs(req->num));
441
442                         outputf("RFB: SetEncodings [%d]", ntohs(req->num));
443                         if (state->writepos < pktsize) return NEEDMORE;
444
445                         for (i = 0; i < ntohs(req->num); i++) {
446                                 outputf("RFB: Encoding: %d", ntohl(req->encodings[i]));
447                                 /* XXX ... */
448                         }
449
450                         state->readpos += pktsize;
451                         return OK;
452
453                 case FB_UPDATE_REQUEST:
454                         if (state->writepos < sizeof(struct fb_update_req))
455                                 return NEEDMORE;
456                         outputf("RFB: UpdateRequest");
457
458                         state->update_requested = 1;
459                         memcpy(&state->client_interest_area, state->data,
460                                sizeof(struct fb_update_req)); 
461
462                         state->readpos += sizeof(struct fb_update_req);
463                         return OK;
464
465                 case KEY_EVENT:
466                         if (state->writepos < sizeof(struct key_event_pkt))
467                                 return NEEDMORE;
468
469                         struct key_event_pkt * p = (struct key_event_pkt *)state->data;
470
471                         outputf("RFB: Key: %d (%c)", htonl(p->keysym), (htonl(p->keysym) & 0xFF));
472                         kbd_inject_keysym(htonl(p->keysym), p->downflag);
473
474                         state->readpos += sizeof(struct key_event_pkt);
475                         return OK;
476
477                 case POINTER_EVENT:
478                         if (state->writepos < sizeof(struct pointer_event_pkt))
479                                 return NEEDMORE;
480                         outputf("RFB: Pointer");
481
482                         /* XXX stub */
483
484                         state->readpos += sizeof(struct pointer_event_pkt);
485                         return OK;
486
487                 case CLIENT_CUT_TEXT:
488                         if (state->writepos < sizeof(struct text_event_pkt))
489                                 return NEEDMORE;
490                         outputf("RFB: Cut Text");
491
492                         struct text_event_pkt * pkt =
493                                 (struct text_event_pkt *)state->data;
494
495                         if (state->writepos < sizeof(struct text_event_pkt)
496                                               + pkt->length)
497                                 return NEEDMORE;
498
499                         /* XXX stub */
500
501                         state->readpos += sizeof(struct text_event_pkt)
502                                           + pkt->length;
503                         return OK;
504
505                 default:
506                         outputf("RFB: Bad command: %d", state->data[0]);
507                 }
508         default:
509                 outputf("RFB: Bad state");
510                 return FAIL;
511         }
512 }
513
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;
517
518         if (state == NULL) 
519
520         if (err != ERR_OK) {
521                 outputf("RFB: recv err %d", err);
522                 /* FIXME do something better here? */
523                 return ERR_OK;
524         }
525
526         if (p == NULL) {
527                 outputf("RFB: Connection closed");
528                 close_conn(pcb, state);
529                 return ERR_OK;
530         }
531
532         if (p->tot_len > (RFB_BUF_SIZE - state->writepos)) {
533                 /* Overflow! */
534                 outputf("RFB: Overflow!");
535                 close_conn(pcb, state);
536                 return ERR_OK;
537         }
538
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;
542
543         tcp_recved(pcb, p->tot_len);
544         pbuf_free(p);
545
546         while (1) {
547                 switch (recv_fsm(pcb, state)) {
548                 case NEEDMORE:
549                         outputf("RFB FSM: blocking");
550                         /* Need more data */
551                         return ERR_OK;
552
553                 case OK:
554                         outputf("RFB FSM: ok");
555
556                         /* Kick off a send. */
557                         if (state->send_state == SST_IDLE
558                             && state->update_requested) {
559                                 send_fsm(pcb, state);
560                         }
561
562                         if (state->readpos == state->writepos) {
563                                 state->readpos = 0;
564                                 state->writepos = 0;
565                                 return ERR_OK;
566                         } else {
567                                 memmove(state->data,
568                                         state->data + state->readpos,
569                                         state->writepos - state->readpos);
570                         }
571                         break;
572                 case FAIL:
573                         /* Shit */
574                         outputf("RFB: Protocol error");
575                         close_conn(pcb, state);
576                         return ERR_OK;
577                 }
578         }
579 }       
580                 
581 static err_t rfb_accept(void *arg, struct tcp_pcb *pcb, err_t err) {
582         struct rfb_state *state;
583
584         LWIP_UNUSED_ARG(arg);
585         LWIP_UNUSED_ARG(err);
586
587         state = (struct rfb_state *)mem_malloc(sizeof(struct rfb_state));
588
589         state->state = ST_BEGIN;
590         state->readpos = 0;
591         state->writepos = 0;
592         state->update_requested = 0;
593         state->send_state = SST_IDLE;
594
595         /* XXX: update_server_info() should be called from the 64ms timer, and deal
596          * with screen resizes appropriately. */
597         update_server_info();
598
599         if (!state)
600         {
601                 outputf("rfb_accept: out of memory\n");
602                 return ERR_MEM;
603         }
604
605         tcp_arg(pcb, state);
606         tcp_recv(pcb, rfb_recv);
607         tcp_sent(pcb, rfb_sent);
608         tcp_poll(pcb, rfb_poll, 1);
609 /*
610         tcp_err(pcb, rfb_err);
611 */
612         tcp_write(pcb, "RFB 003.008\n", 12, 0);
613         tcp_output(pcb);
614
615         return ERR_OK;
616 }
617
618 void rfb_init() {
619         struct tcp_pcb *pcb;
620
621         init_server_info();
622
623         pcb = tcp_new();
624         tcp_bind(pcb, IP_ADDR_ANY, RFB_PORT);
625         pcb = tcp_listen(pcb);
626         tcp_accept(pcb, rfb_accept);
627 }
This page took 0.063738 seconds and 4 git commands to generate.