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