source: mainline/uspace/srv/hid/rfb/rfb.c@ 7c3fb9b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7c3fb9b was 7c3fb9b, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix block comment formatting (ccheck).

  • Property mode set to 100644
File size: 21.8 KB
Line 
1/*
2 * Copyright (c) 2013 Martin Sucha
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <errno.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <fibril_synch.h>
33#include <inet/addr.h>
34#include <inet/endpoint.h>
35#include <inet/tcp.h>
36#include <inttypes.h>
37#include <str.h>
38#include <str_error.h>
39#include <byteorder.h>
40#include <macros.h>
41#include <io/log.h>
42
43#include "rfb.h"
44
45static void rfb_new_conn(tcp_listener_t *, tcp_conn_t *);
46
47static tcp_listen_cb_t listen_cb = {
48 .new_conn = rfb_new_conn
49};
50
51static tcp_cb_t conn_cb = {
52 .connected = NULL
53};
54
55/** Buffer for receiving the request. */
56#define BUFFER_SIZE 1024
57
58static char rbuf[BUFFER_SIZE];
59static size_t rbuf_out;
60static size_t rbuf_in;
61
62
63/** Receive one character (with buffering) */
64static errno_t recv_char(tcp_conn_t *conn, char *c)
65{
66 size_t nrecv;
67 errno_t rc;
68
69 if (rbuf_out == rbuf_in) {
70 rbuf_out = 0;
71 rbuf_in = 0;
72
73 rc = tcp_conn_recv_wait(conn, rbuf, BUFFER_SIZE, &nrecv);
74 if (rc != EOK)
75 return rc;
76
77 rbuf_in = nrecv;
78 }
79
80 *c = rbuf[rbuf_out++];
81 return EOK;
82}
83
84/** Receive count characters (with buffering) */
85static errno_t __attribute__((warn_unused_result))
86recv_chars(tcp_conn_t *conn, char *c, size_t count)
87{
88 for (size_t i = 0; i < count; i++) {
89 errno_t rc = recv_char(conn, c);
90 if (rc != EOK)
91 return rc;
92 c++;
93 }
94 return EOK;
95}
96
97static errno_t recv_skip_chars(tcp_conn_t *conn, size_t count)
98{
99 for (size_t i = 0; i < count; i++) {
100 char c;
101 errno_t rc = recv_char(conn, &c);
102 if (rc != EOK)
103 return rc;
104 }
105 return EOK;
106}
107
108static void rfb_pixel_format_to_be(rfb_pixel_format_t *src, rfb_pixel_format_t *dst)
109{
110 dst->r_max = host2uint16_t_be(src->r_max);
111 dst->g_max = host2uint16_t_be(src->g_max);
112 dst->b_max = host2uint16_t_be(src->b_max);
113}
114
115static void rfb_pixel_format_to_host(rfb_pixel_format_t *src, rfb_pixel_format_t *dst)
116{
117 dst->r_max = uint16_t_be2host(src->r_max);
118 dst->g_max = uint16_t_be2host(src->g_max);
119 dst->b_max = uint16_t_be2host(src->b_max);
120}
121
122static void rfb_server_init_to_be(rfb_server_init_t *src, rfb_server_init_t *dst)
123{
124 dst->width = host2uint16_t_be(src->width);
125 dst->height = host2uint16_t_be(src->height);
126 rfb_pixel_format_to_be(&src->pixel_format, &dst->pixel_format);
127 dst->name_length = host2uint32_t_be(src->name_length);
128}
129
130static void rfb_set_encodings_to_host(rfb_set_encodings_t *src, rfb_set_encodings_t *dst)
131{
132 dst->count = uint16_t_be2host(src->count);
133}
134
135static void rfb_framebuffer_update_request_to_host(rfb_framebuffer_update_request_t *src,
136 rfb_framebuffer_update_request_t *dst)
137{
138 dst->x = uint16_t_be2host(src->x);
139 dst->y = uint16_t_be2host(src->x);
140 dst->width = uint16_t_be2host(src->width);
141 dst->height = uint16_t_be2host(src->height);
142}
143
144static void rfb_framebuffer_update_to_be(rfb_framebuffer_update_t *src,
145 rfb_framebuffer_update_t *dst)
146{
147 dst->rect_count = host2uint16_t_be(src->rect_count);
148}
149
150static void rfb_rectangle_to_be(rfb_rectangle_t *src, rfb_rectangle_t *dst)
151{
152 dst->x = host2uint16_t_be(src->x);
153 dst->y = host2uint16_t_be(src->y);
154 dst->width = host2uint16_t_be(src->width);
155 dst->height = host2uint16_t_be(src->height);
156 dst->enctype = host2uint32_t_be(src->enctype);
157}
158
159static void rfb_key_event_to_host(rfb_key_event_t *src, rfb_key_event_t *dst)
160{
161 dst->key = uint32_t_be2host(src->key);
162}
163
164static void rfb_pointer_event_to_host(rfb_pointer_event_t *src, rfb_pointer_event_t *dst)
165{
166 dst->x = uint16_t_be2host(src->x);
167 dst->y = uint16_t_be2host(src->y);
168}
169
170static void rfb_client_cut_text_to_host(rfb_client_cut_text_t *src,
171 rfb_client_cut_text_t *dst)
172{
173 dst->length = uint32_t_be2host(src->length);
174}
175
176errno_t rfb_init(rfb_t *rfb, uint16_t width, uint16_t height, const char *name)
177{
178 memset(rfb, 0, sizeof(rfb_t));
179 fibril_mutex_initialize(&rfb->lock);
180
181 rfb_pixel_format_t *pf = &rfb->pixel_format;
182 pf->bpp = 32;
183 pf->depth = 24;
184 pf->big_endian = 1;
185 pf->true_color = 1;
186 pf->r_max = 255;
187 pf->g_max = 255;
188 pf->b_max = 255;
189 pf->r_shift = 0;
190 pf->g_shift = 8;
191 pf->b_shift = 16;
192
193 rfb->name = str_dup(name);
194 rfb->supports_trle = false;
195
196 return rfb_set_size(rfb, width, height);
197}
198
199errno_t rfb_set_size(rfb_t *rfb, uint16_t width, uint16_t height)
200{
201 size_t new_size = width * height * sizeof(pixel_t);
202 void *pixbuf = malloc(new_size);
203 if (pixbuf == NULL)
204 return ENOMEM;
205
206 free(rfb->framebuffer.data);
207 rfb->framebuffer.data = pixbuf;
208 rfb->framebuffer.width = width;
209 rfb->framebuffer.height = height;
210 rfb->width = width;
211 rfb->height = height;
212
213 /* Fill with white */
214 memset(rfb->framebuffer.data, 255, new_size);
215
216 return EOK;
217}
218
219static errno_t __attribute__((warn_unused_result))
220recv_message(tcp_conn_t *conn, char type, void *buf, size_t size)
221{
222 memcpy(buf, &type, 1);
223 return recv_chars(conn, ((char *) buf) + 1, size - 1);
224}
225
226static uint32_t rfb_scale_channel(uint8_t val, uint32_t max)
227{
228 return val * max / 255;
229}
230
231static void rfb_encode_index(rfb_t *rfb, uint8_t *buf, pixel_t pixel)
232{
233 int first_free_index = -1;
234 for (size_t i = 0; i < 256; i++) {
235 bool free = ALPHA(rfb->palette[i]) == 0;
236 if (free && first_free_index == -1) {
237 first_free_index = i;
238 } else if (!free && RED(rfb->palette[i]) == RED(pixel) &&
239 GREEN(rfb->palette[i]) == GREEN(pixel) &&
240 BLUE(rfb->palette[i]) == BLUE(pixel)) {
241 *buf = i;
242 return;
243 }
244 }
245
246 if (first_free_index != -1) {
247 rfb->palette[first_free_index] = PIXEL(255, RED(pixel), GREEN(pixel),
248 BLUE(pixel));
249 rfb->palette_used = max(rfb->palette_used, (unsigned) first_free_index + 1);
250 *buf = first_free_index;
251 return;
252 }
253
254 /* TODO find nearest color index. We are lazy so return index 0 for now */
255 *buf = 0;
256}
257
258static void rfb_encode_true_color(rfb_pixel_format_t *pf, void *buf,
259 pixel_t pixel)
260{
261 uint32_t pix = 0;
262 pix |= rfb_scale_channel(RED(pixel), pf->r_max) << pf->r_shift;
263 pix |= rfb_scale_channel(GREEN(pixel), pf->g_max) << pf->g_shift;
264 pix |= rfb_scale_channel(BLUE(pixel), pf->b_max) << pf->b_shift;
265
266 if (pf->bpp == 8) {
267 uint8_t pix8 = pix;
268 memcpy(buf, &pix8, 1);
269 } else if (pf->bpp == 16) {
270 uint16_t pix16 = pix;
271 if (pf->big_endian) {
272 pix16 = host2uint16_t_be(pix16);
273 } else {
274 pix16 = host2uint16_t_le(pix16);
275 }
276 memcpy(buf, &pix16, 2);
277 } else if (pf->bpp == 32) {
278 if (pf->big_endian) {
279 pix = host2uint32_t_be(pix);
280 } else {
281 pix = host2uint32_t_le(pix);
282 }
283 memcpy(buf, &pix, 4);
284 }
285}
286
287static void rfb_encode_pixel(rfb_t *rfb, void *buf, pixel_t pixel)
288{
289 if (rfb->pixel_format.true_color) {
290 rfb_encode_true_color(&rfb->pixel_format, buf, pixel);
291 } else {
292 rfb_encode_index(rfb, buf, pixel);
293 }
294}
295
296static void rfb_set_color_map_entries_to_be(rfb_set_color_map_entries_t *src,
297 rfb_set_color_map_entries_t *dst)
298{
299 dst->first_color = host2uint16_t_be(src->first_color);
300 dst->color_count = host2uint16_t_be(src->color_count);
301}
302
303static void rfb_color_map_entry_to_be(rfb_color_map_entry_t *src,
304 rfb_color_map_entry_t *dst)
305{
306 dst->red = host2uint16_t_be(src->red);
307 dst->green = host2uint16_t_be(src->green);
308 dst->blue = host2uint16_t_be(src->blue);
309}
310
311static void *rfb_send_palette_message(rfb_t *rfb, size_t *psize)
312{
313 size_t size = sizeof(rfb_set_color_map_entries_t) +
314 rfb->palette_used * sizeof(rfb_color_map_entry_t);
315
316 void *buf = malloc(size);
317 if (buf == NULL)
318 return NULL;
319
320 void *pos = buf;
321
322 rfb_set_color_map_entries_t *scme = pos;
323 scme->message_type = RFB_SMSG_SET_COLOR_MAP_ENTRIES;
324 scme->first_color = 0;
325 scme->color_count = rfb->palette_used;
326 rfb_set_color_map_entries_to_be(scme, scme);
327 pos += sizeof(rfb_set_color_map_entries_t);
328
329 rfb_color_map_entry_t *entries = pos;
330 for (unsigned i = 0; i < rfb->palette_used; i++) {
331 entries[i].red = 65535 * RED(rfb->palette[i]) / 255;
332 entries[i].green = 65535 * GREEN(rfb->palette[i]) / 255;
333 entries[i].blue = 65535 * BLUE(rfb->palette[i]) / 255;
334 rfb_color_map_entry_to_be(&entries[i], &entries[i]);
335 }
336
337 *psize = size;
338 return buf;
339}
340
341static size_t rfb_rect_encode_raw(rfb_t *rfb, rfb_rectangle_t *rect, void *buf)
342{
343 size_t pixel_size = rfb->pixel_format.bpp / 8;
344 size_t size = (rect->width * rect->height * pixel_size);
345
346 if (buf == NULL)
347 return size;
348
349 for (uint16_t y = 0; y < rect->height; y++) {
350 for (uint16_t x = 0; x < rect->width; x++) {
351 pixel_t pixel = pixelmap_get_pixel(&rfb->framebuffer,
352 x + rect->x, y + rect->y);
353 rfb_encode_pixel(rfb, buf, pixel);
354 buf += pixel_size;
355 }
356 }
357
358 return size;
359}
360
361typedef enum {
362 COMP_NONE,
363 COMP_SKIP_START,
364 COMP_SKIP_END
365} cpixel_compress_type_t;
366
367typedef struct {
368 size_t size;
369 cpixel_compress_type_t compress_type;
370} cpixel_ctx_t;
371
372static void cpixel_context_init(cpixel_ctx_t *ctx, rfb_pixel_format_t *pixel_format)
373{
374 ctx->size = pixel_format->bpp / 8;
375 ctx->compress_type = COMP_NONE;
376
377 if (pixel_format->bpp == 32 && pixel_format->depth <= 24) {
378 uint32_t mask = 0;
379 mask |= pixel_format->r_max << pixel_format->r_shift;
380 mask |= pixel_format->g_max << pixel_format->g_shift;
381 mask |= pixel_format->b_max << pixel_format->b_shift;
382
383 if (pixel_format->big_endian) {
384 mask = host2uint32_t_be(mask);
385 } else {
386 mask = host2uint32_t_le(mask);
387 }
388
389 uint8_t *mask_data = (uint8_t *) &mask;
390 if (mask_data[0] == 0) {
391 ctx->compress_type = COMP_SKIP_START;
392 ctx->size = 3;
393 } else if (mask_data[3] == 0) {
394 ctx->compress_type = COMP_SKIP_END;
395 ctx->size = 3;
396 }
397 }
398}
399
400static void cpixel_encode(rfb_t *rfb, cpixel_ctx_t *cpixel, void *buf,
401 pixel_t pixel)
402{
403 uint8_t data[4];
404 rfb_encode_pixel(rfb, data, pixel);
405
406 switch (cpixel->compress_type) {
407 case COMP_NONE:
408 case COMP_SKIP_END:
409 memcpy(buf, data, cpixel->size);
410 break;
411 case COMP_SKIP_START:
412 memcpy(buf, data + 1, cpixel->size);
413 }
414}
415
416static size_t rfb_tile_encode_raw(rfb_t *rfb, cpixel_ctx_t *cpixel,
417 rfb_rectangle_t *tile, void *buf)
418{
419 size_t size = tile->width * tile->height * cpixel->size;
420 if (buf == NULL)
421 return size;
422
423 for (uint16_t y = tile->y; y < tile->y + tile->height; y++) {
424 for (uint16_t x = tile->x; x < tile->x + tile->width; x++) {
425 pixel_t pixel = pixelmap_get_pixel(&rfb->framebuffer, x, y);
426 cpixel_encode(rfb, cpixel, buf, pixel);
427 }
428 }
429
430 return size;
431}
432
433static errno_t rfb_tile_encode_solid(rfb_t *rfb, cpixel_ctx_t *cpixel,
434 rfb_rectangle_t *tile, void *buf, size_t *size)
435{
436 /* Check if it is single color */
437 pixel_t the_color = pixelmap_get_pixel(&rfb->framebuffer, tile->x, tile->y);
438 for (uint16_t y = tile->y; y < tile->y + tile->height; y++) {
439 for (uint16_t x = tile->x; x < tile->x + tile->width; x++) {
440 if (pixelmap_get_pixel(&rfb->framebuffer, x, y) != the_color)
441 return EINVAL;
442 }
443 }
444
445 /* OK, encode it */
446 if (buf)
447 cpixel_encode(rfb, cpixel, buf, the_color);
448 *size = cpixel->size;
449 return EOK;
450}
451
452static size_t rfb_rect_encode_trle(rfb_t *rfb, rfb_rectangle_t *rect, void *buf)
453{
454 cpixel_ctx_t cpixel;
455 cpixel_context_init(&cpixel, &rfb->pixel_format);
456
457 size_t size = 0;
458 for (uint16_t y = 0; y < rect->height; y += 16) {
459 for (uint16_t x = 0; x < rect->width; x += 16) {
460 rfb_rectangle_t tile = {
461 .x = x,
462 .y = y,
463 .width = (x + 16 <= rect->width ? 16 : rect->width - x),
464 .height = (y + 16 <= rect->height ? 16 : rect->height - y)
465 };
466
467 size += 1;
468 uint8_t *tile_enctype_ptr = buf;
469 if (buf)
470 buf += 1;
471
472 uint8_t tile_enctype = RFB_TILE_ENCODING_SOLID;
473 size_t tile_size;
474 errno_t rc = rfb_tile_encode_solid(rfb, &cpixel, &tile, buf,
475 &tile_size);
476 if (rc != EOK) {
477 tile_size = rfb_tile_encode_raw(rfb, &cpixel, &tile, buf);
478 tile_enctype = RFB_TILE_ENCODING_RAW;
479 }
480
481 if (buf) {
482 *tile_enctype_ptr = tile_enctype;
483 buf += tile_size;
484 }
485 }
486 }
487 return size;
488}
489
490static errno_t rfb_send_framebuffer_update(rfb_t *rfb, tcp_conn_t *conn,
491 bool incremental)
492{
493 fibril_mutex_lock(&rfb->lock);
494 if (!incremental || !rfb->damage_valid) {
495 rfb->damage_rect.x = 0;
496 rfb->damage_rect.y = 0;
497 rfb->damage_rect.width = rfb->width;
498 rfb->damage_rect.height = rfb->height;
499 }
500
501
502 /* We send only single raw rectangle right now */
503 size_t buf_size = sizeof(rfb_framebuffer_update_t) +
504 sizeof(rfb_rectangle_t) * 1 +
505 rfb_rect_encode_raw(rfb, &rfb->damage_rect, NULL);
506
507 void *buf = malloc(buf_size);
508 if (buf == NULL) {
509 fibril_mutex_unlock(&rfb->lock);
510 return ENOMEM;
511 }
512 memset(buf, 0, buf_size);
513
514 void *pos = buf;
515 rfb_framebuffer_update_t *fbu = buf;
516 fbu->message_type = RFB_SMSG_FRAMEBUFFER_UPDATE;
517 fbu->rect_count = 1;
518 rfb_framebuffer_update_to_be(fbu, fbu);
519 pos += sizeof(rfb_framebuffer_update_t);
520
521 rfb_rectangle_t *rect = pos;
522 pos += sizeof(rfb_rectangle_t);
523
524 *rect = rfb->damage_rect;
525
526 if (rfb->supports_trle) {
527 rect->enctype = RFB_ENCODING_TRLE;
528 pos += rfb_rect_encode_trle(rfb, rect, pos);
529 } else {
530 rect->enctype = RFB_ENCODING_RAW;
531 pos += rfb_rect_encode_raw(rfb, rect, pos);
532 }
533 rfb_rectangle_to_be(rect, rect);
534
535 rfb->damage_valid = false;
536
537 size_t send_palette_size = 0;
538 void *send_palette = NULL;
539
540 if (!rfb->pixel_format.true_color) {
541 send_palette = rfb_send_palette_message(rfb, &send_palette_size);
542 if (send_palette == NULL) {
543 free(buf);
544 fibril_mutex_unlock(&rfb->lock);
545 return ENOMEM;
546 }
547 }
548
549 fibril_mutex_unlock(&rfb->lock);
550
551 if (!rfb->pixel_format.true_color) {
552 errno_t rc = tcp_conn_send(conn, send_palette, send_palette_size);
553 if (rc != EOK) {
554 free(buf);
555 return rc;
556 }
557 }
558
559 errno_t rc = tcp_conn_send(conn, buf, buf_size);
560 free(buf);
561
562 return rc;
563}
564
565static errno_t rfb_set_pixel_format(rfb_t *rfb, rfb_pixel_format_t *pixel_format)
566{
567 rfb->pixel_format = *pixel_format;
568 if (rfb->pixel_format.true_color) {
569 free(rfb->palette);
570 rfb->palette = NULL;
571 rfb->palette_used = 0;
572 log_msg(LOG_DEFAULT, LVL_DEBUG,
573 "changed pixel format to %d-bit true color (%x<<%d, %x<<%d, %x<<%d)",
574 pixel_format->depth, pixel_format->r_max, pixel_format->r_shift,
575 pixel_format->g_max, pixel_format->g_shift, pixel_format->b_max,
576 pixel_format->b_shift);
577 } else {
578 if (rfb->palette == NULL) {
579 rfb->palette = malloc(sizeof(pixel_t) * 256);
580 if (rfb->palette == NULL)
581 return ENOMEM;
582 memset(rfb->palette, 0, sizeof(pixel_t) * 256);
583 rfb->palette_used = 0;
584 }
585 log_msg(LOG_DEFAULT, LVL_DEBUG, "changed pixel format to %d-bit palette",
586 pixel_format->depth);
587 }
588 return EOK;
589}
590
591static void rfb_socket_connection(rfb_t *rfb, tcp_conn_t *conn)
592{
593 /* Version handshake */
594 errno_t rc = tcp_conn_send(conn, "RFB 003.008\n", 12);
595 if (rc != EOK) {
596 log_msg(LOG_DEFAULT, LVL_WARN, "Failed sending server version: %s",
597 str_error(rc));
598 return;
599 }
600
601 char client_version[12];
602 rc = recv_chars(conn, client_version, 12);
603 if (rc != EOK) {
604 log_msg(LOG_DEFAULT, LVL_WARN, "Failed receiving client version: %s",
605 str_error(rc));
606 return;
607 }
608
609 if (memcmp(client_version, "RFB 003.008\n", 12) != 0) {
610 log_msg(LOG_DEFAULT, LVL_WARN, "Client version is not RFB 3.8");
611 return;
612 }
613
614 /*
615 * Security handshake
616 * 1 security type supported, which is 1 - None
617 */
618 char sec_types[2];
619 sec_types[0] = 1; /* length */
620 sec_types[1] = RFB_SECURITY_NONE;
621 rc = tcp_conn_send(conn, sec_types, 2);
622 if (rc != EOK) {
623 log_msg(LOG_DEFAULT, LVL_WARN,
624 "Failed sending security handshake: %s", str_error(rc));
625 return;
626 }
627
628 char selected_sec_type = 0;
629 rc = recv_char(conn, &selected_sec_type);
630 if (rc != EOK) {
631 log_msg(LOG_DEFAULT, LVL_WARN, "Failed receiving security type: %s",
632 str_error(rc));
633 return;
634 }
635 if (selected_sec_type != RFB_SECURITY_NONE) {
636 log_msg(LOG_DEFAULT, LVL_WARN,
637 "Client selected security type other than none");
638 return;
639 }
640 uint32_t security_result = RFB_SECURITY_HANDSHAKE_OK;
641 rc = tcp_conn_send(conn, &security_result, sizeof(uint32_t));
642 if (rc != EOK) {
643 log_msg(LOG_DEFAULT, LVL_WARN, "Failed sending security result: %s",
644 str_error(rc));
645 return;
646 }
647
648 /* Client init */
649 char shared_flag;
650 rc = recv_char(conn, &shared_flag);
651 if (rc != EOK) {
652 log_msg(LOG_DEFAULT, LVL_WARN, "Failed receiving client init: %s",
653 str_error(rc));
654 return;
655 }
656
657 /* Server init */
658 fibril_mutex_lock(&rfb->lock);
659 size_t name_length = str_length(rfb->name);
660 size_t msg_length = sizeof(rfb_server_init_t) + name_length;
661 rfb_server_init_t *server_init = malloc(msg_length);
662 if (server_init == NULL) {
663 log_msg(LOG_DEFAULT, LVL_WARN, "Cannot allocate memory for server init");
664 fibril_mutex_unlock(&rfb->lock);
665 return;
666 }
667 server_init->width = rfb->width;
668 server_init->height = rfb->height;
669 server_init->pixel_format = rfb->pixel_format;
670 server_init->name_length = name_length;
671 rfb_server_init_to_be(server_init, server_init);
672 memcpy(server_init->name, rfb->name, name_length);
673 fibril_mutex_unlock(&rfb->lock);
674 rc = tcp_conn_send(conn, server_init, msg_length);
675 if (rc != EOK) {
676 log_msg(LOG_DEFAULT, LVL_WARN, "Failed sending server init: %s",
677 str_error(rc));
678 return;
679 }
680
681 while (true) {
682 char message_type = 0;
683 rc = recv_char(conn, &message_type);
684 if (rc != EOK) {
685 log_msg(LOG_DEFAULT, LVL_WARN,
686 "Failed receiving client message type: %s",
687 str_error(rc));
688 return;
689 }
690
691 rfb_set_pixel_format_t spf;
692 rfb_set_encodings_t se;
693 rfb_framebuffer_update_request_t fbur;
694 rfb_key_event_t ke;
695 rfb_pointer_event_t pe;
696 rfb_client_cut_text_t cct;
697 switch (message_type) {
698 case RFB_CMSG_SET_PIXEL_FORMAT:
699 rc = recv_message(conn, message_type, &spf, sizeof(spf));
700 if (rc != EOK) {
701 log_msg(LOG_DEFAULT, LVL_WARN,
702 "Failed receiving client message: %s",
703 str_error(rc));
704 return;
705 }
706 rfb_pixel_format_to_host(&spf.pixel_format, &spf.pixel_format);
707 log_msg(LOG_DEFAULT, LVL_DEBUG2, "Received SetPixelFormat message");
708 fibril_mutex_lock(&rfb->lock);
709 rc = rfb_set_pixel_format(rfb, &spf.pixel_format);
710 fibril_mutex_unlock(&rfb->lock);
711 if (rc != EOK)
712 return;
713 break;
714 case RFB_CMSG_SET_ENCODINGS:
715 rc = recv_message(conn, message_type, &se, sizeof(se));
716 if (rc != EOK) {
717 log_msg(LOG_DEFAULT, LVL_WARN,
718 "Failed receiving client message: %s",
719 str_error(rc));
720 return;
721 }
722 rfb_set_encodings_to_host(&se, &se);
723 log_msg(LOG_DEFAULT, LVL_DEBUG2, "Received SetEncodings message");
724 for (uint16_t i = 0; i < se.count; i++) {
725 int32_t encoding = 0;
726 rc = recv_chars(conn, (char *) &encoding, sizeof(int32_t));
727 if (rc != EOK)
728 return;
729 encoding = uint32_t_be2host(encoding);
730 if (encoding == RFB_ENCODING_TRLE) {
731 log_msg(LOG_DEFAULT, LVL_DEBUG,
732 "Client supports TRLE encoding");
733 rfb->supports_trle = true;
734 }
735 }
736 break;
737 case RFB_CMSG_FRAMEBUFFER_UPDATE_REQUEST:
738 rc = recv_message(conn, message_type, &fbur, sizeof(fbur));
739 if (rc != EOK) {
740 log_msg(LOG_DEFAULT, LVL_WARN,
741 "Failed receiving client message: %s",
742 str_error(rc));
743 return;
744 }
745 rfb_framebuffer_update_request_to_host(&fbur, &fbur);
746 log_msg(LOG_DEFAULT, LVL_DEBUG2,
747 "Received FramebufferUpdateRequest message");
748 rfb_send_framebuffer_update(rfb, conn, fbur.incremental);
749 break;
750 case RFB_CMSG_KEY_EVENT:
751 rc = recv_message(conn, message_type, &ke, sizeof(ke));
752 if (rc != EOK) {
753 log_msg(LOG_DEFAULT, LVL_WARN,
754 "Failed receiving client message: %s",
755 str_error(rc));
756 return;
757 }
758 rfb_key_event_to_host(&ke, &ke);
759 log_msg(LOG_DEFAULT, LVL_DEBUG2, "Received KeyEvent message");
760 break;
761 case RFB_CMSG_POINTER_EVENT:
762 rc = recv_message(conn, message_type, &pe, sizeof(pe));
763 if (rc != EOK) {
764 log_msg(LOG_DEFAULT, LVL_WARN,
765 "Failed receiving client message: %s",
766 str_error(rc));
767 return;
768 }
769 rfb_pointer_event_to_host(&pe, &pe);
770 log_msg(LOG_DEFAULT, LVL_DEBUG2, "Received PointerEvent message");
771 break;
772 case RFB_CMSG_CLIENT_CUT_TEXT:
773 rc = recv_message(conn, message_type, &cct, sizeof(cct));
774 if (rc != EOK) {
775 log_msg(LOG_DEFAULT, LVL_WARN,
776 "Failed receiving client message: %s",
777 str_error(rc));
778 return;
779 }
780 rfb_client_cut_text_to_host(&cct, &cct);
781 log_msg(LOG_DEFAULT, LVL_DEBUG2, "Received ClientCutText message");
782 recv_skip_chars(conn, cct.length);
783 break;
784 default:
785 log_msg(LOG_DEFAULT, LVL_WARN,
786 "Invalid client message type encountered");
787 return;
788 }
789 }
790}
791
792errno_t rfb_listen(rfb_t *rfb, uint16_t port)
793{
794 tcp_t *tcp = NULL;
795 tcp_listener_t *lst = NULL;
796 inet_ep_t ep;
797 errno_t rc;
798
799 rc = tcp_create(&tcp);
800 if (rc != EOK) {
801 log_msg(LOG_DEFAULT, LVL_ERROR, "Error initializing TCP.");
802 goto error;
803 }
804
805 inet_ep_init(&ep);
806 ep.port = port;
807
808 rc = tcp_listener_create(tcp, &ep, &listen_cb, rfb, &conn_cb, rfb,
809 &lst);
810 if (rc != EOK) {
811 log_msg(LOG_DEFAULT, LVL_ERROR, "Error creating listener.");
812 goto error;
813 }
814
815 rfb->tcp = tcp;
816 rfb->lst = lst;
817
818 return EOK;
819error:
820 tcp_listener_destroy(lst);
821 tcp_destroy(tcp);
822 return rc;
823}
824
825static void rfb_new_conn(tcp_listener_t *lst, tcp_conn_t *conn)
826{
827 rfb_t *rfb = (rfb_t *)tcp_listener_userptr(lst);
828 log_msg(LOG_DEFAULT, LVL_DEBUG, "Connection accepted");
829
830 rbuf_out = 0;
831 rbuf_in = 0;
832
833 rfb_socket_connection(rfb, conn);
834}
Note: See TracBrowser for help on using the repository browser.