3 #include <sys/socket.h>
4 #include <netinet/in.h>
5 #include <netinet/tcp.h>
8 #define VNC_REFRESH_INTERVAL (1000 / 30)
10 #include "vnc_keysym.h"
20 typedef struct VncState VncState;
22 typedef int VncReadEvent(VncState *vs, char *data, size_t len);
33 uint64_t dirty_row[768];
40 kbd_layout_t *kbd_layout;
42 VncReadEvent *read_handler;
43 size_t read_handler_expect;
47 1) Get the queue working for IO.
48 2) there is some weirdness when using the -S option (the screen is grey
49 and not totally invalidated
53 static void vnc_write(VncState *vs, const void *data, size_t len);
54 static void vnc_write_u32(VncState *vs, uint32_t value);
55 static void vnc_write_s32(VncState *vs, int32_t value);
56 static void vnc_write_u16(VncState *vs, uint16_t value);
57 static void vnc_write_u8(VncState *vs, uint8_t value);
58 static void vnc_flush(VncState *vs);
59 static void vnc_update_client(void *opaque);
60 static void vnc_client_read(void *opaque);
62 static void vnc_dpy_update(DisplayState *ds, int x, int y, int w, int h)
64 VncState *vs = ds->opaque;
70 for (i = 0; i < w; i += 16)
71 vs->dirty_row[y] |= (1ULL << ((x + i) / 16));
74 static void vnc_framebuffer_update(VncState *vs, int x, int y, int w, int h,
82 vnc_write_s32(vs, encoding);
85 static void vnc_dpy_resize(DisplayState *ds, int w, int h)
87 VncState *vs = ds->opaque;
89 ds->data = realloc(ds->data, w * h * vs->depth);
90 vs->old_data = realloc(vs->old_data, w * h * vs->depth);
92 if (ds->data == NULL || vs->old_data == NULL) {
93 fprintf(stderr, "vnc: memory allocation failed\n");
97 ds->depth = vs->depth * 8;
100 ds->linesize = w * vs->depth;
101 if (vs->csock != -1 && vs->has_resize) {
102 vnc_write_u8(vs, 0); /* msg id */
104 vnc_write_u16(vs, 1); /* number of rects */
105 vnc_framebuffer_update(vs, 0, 0, ds->width, ds->height, -223);
107 vs->width = ds->width;
108 vs->height = ds->height;
112 static void send_framebuffer_update_raw(VncState *vs, int x, int y, int w, int h)
117 vnc_framebuffer_update(vs, x, y, w, h, 0);
119 row = vs->ds->data + y * vs->ds->linesize + x * vs->depth;
120 for (i = 0; i < h; i++) {
121 vnc_write(vs, row, w * vs->depth);
122 row += vs->ds->linesize;
126 static void hextile_enc_cord(uint8_t *ptr, int x, int y, int w, int h)
128 ptr[0] = ((x & 0x0F) << 4) | (y & 0x0F);
129 ptr[1] = (((w - 1) & 0x0F) << 4) | ((h - 1) & 0x0F);
133 #include "vnchextile.h"
137 #include "vnchextile.h"
141 #include "vnchextile.h"
144 static void send_framebuffer_update_hextile(VncState *vs, int x, int y, int w, int h)
148 uint32_t last_fg32, last_bg32;
149 uint16_t last_fg16, last_bg16;
150 uint8_t last_fg8, last_bg8;
152 vnc_framebuffer_update(vs, x, y, w, h, 5);
155 for (j = y; j < (y + h); j += 16) {
156 for (i = x; i < (x + w); i += 16) {
159 send_hextile_tile_8(vs, i, j, MIN(16, x + w - i), MIN(16, y + h - j),
160 &last_bg8, &last_fg8, &has_bg, &has_fg);
163 send_hextile_tile_16(vs, i, j, MIN(16, x + w - i), MIN(16, y + h - j),
164 &last_bg16, &last_fg16, &has_bg, &has_fg);
167 send_hextile_tile_32(vs, i, j, MIN(16, x + w - i), MIN(16, y + h - j),
168 &last_bg32, &last_fg32, &has_bg, &has_fg);
177 static void send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
180 send_framebuffer_update_hextile(vs, x, y, w, h);
182 send_framebuffer_update_raw(vs, x, y, w, h);
185 static void vnc_copy(DisplayState *ds, int src_x, int src_y, int dst_x, int dst_y, int w, int h)
192 int pitch = ds->linesize;
193 VncState *vs = ds->opaque;
195 vnc_update_client(vs);
202 src = (ds->linesize * (src_y + y) + vs->depth * src_x);
203 dst = (ds->linesize * (dst_y + y) + vs->depth * dst_x);
205 src_row = ds->data + src;
206 dst_row = ds->data + dst;
207 old_row = vs->old_data + dst;
209 for (y = 0; y < h; y++) {
210 memmove(old_row, src_row, w * vs->depth);
211 memmove(dst_row, src_row, w * vs->depth);
217 vnc_write_u8(vs, 0); /* msg id */
219 vnc_write_u16(vs, 1); /* number of rects */
220 vnc_framebuffer_update(vs, dst_x, dst_y, w, h, 1);
221 vnc_write_u16(vs, src_x);
222 vnc_write_u16(vs, src_y);
226 static int find_dirty_height(VncState *vs, int y, int last_x, int x)
230 for (h = 1; h < (vs->height - y); h++) {
232 if (!(vs->dirty_row[y + h] & (1ULL << last_x)))
234 for (tmp_x = last_x; tmp_x < x; tmp_x++)
235 vs->dirty_row[y + h] &= ~(1ULL << tmp_x);
241 static void vnc_update_client(void *opaque)
243 VncState *vs = opaque;
245 if (vs->need_update && vs->csock != -1) {
254 width_mask = (1ULL << (vs->width / 16)) - 1;
256 if (vs->width == 1024)
257 width_mask = ~(0ULL);
259 /* Walk through the dirty map and eliminate tiles that
260 really aren't dirty */
262 old_row = vs->old_data;
264 for (y = 0; y < vs->height; y++) {
265 if (vs->dirty_row[y] & width_mask) {
272 for (x = 0; x < vs->ds->width; x += 16) {
273 if (memcmp(old_ptr, ptr, 16 * vs->depth) == 0) {
274 vs->dirty_row[y] &= ~(1ULL << (x / 16));
277 memcpy(old_ptr, ptr, 16 * vs->depth);
280 ptr += 16 * vs->depth;
281 old_ptr += 16 * vs->depth;
285 row += vs->ds->linesize;
286 old_row += vs->ds->linesize;
290 qemu_mod_timer(vs->timer, qemu_get_clock(rt_clock) + VNC_REFRESH_INTERVAL);
294 /* Count rectangles */
296 vnc_write_u8(vs, 0); /* msg id */
298 saved_offset = vs->output.offset;
299 vnc_write_u16(vs, 0);
301 for (y = 0; y < vs->height; y++) {
304 for (x = 0; x < vs->width / 16; x++) {
305 if (vs->dirty_row[y] & (1ULL << x)) {
309 vs->dirty_row[y] &= ~(1ULL << x);
312 int h = find_dirty_height(vs, y, last_x, x);
313 send_framebuffer_update(vs, last_x * 16, y, (x - last_x) * 16, h);
320 int h = find_dirty_height(vs, y, last_x, x);
321 send_framebuffer_update(vs, last_x * 16, y, (x - last_x) * 16, h);
325 vs->output.buffer[saved_offset] = (n_rectangles >> 8) & 0xFF;
326 vs->output.buffer[saved_offset + 1] = n_rectangles & 0xFF;
330 qemu_mod_timer(vs->timer, qemu_get_clock(rt_clock) + VNC_REFRESH_INTERVAL);
333 static void vnc_timer_init(VncState *vs)
335 if (vs->timer == NULL) {
336 vs->timer = qemu_new_timer(rt_clock, vnc_update_client, vs);
337 qemu_mod_timer(vs->timer, qemu_get_clock(rt_clock));
341 static void vnc_dpy_refresh(DisplayState *ds)
343 VncState *vs = ds->opaque;
348 static int vnc_listen_poll(void *opaque)
350 VncState *vs = opaque;
356 static void buffer_reserve(Buffer *buffer, size_t len)
358 if ((buffer->capacity - buffer->offset) < len) {
359 buffer->capacity += (len + 1024);
360 buffer->buffer = realloc(buffer->buffer, buffer->capacity);
361 if (buffer->buffer == NULL) {
362 fprintf(stderr, "vnc: out of memory\n");
368 static int buffer_empty(Buffer *buffer)
370 return buffer->offset == 0;
373 static char *buffer_end(Buffer *buffer)
375 return buffer->buffer + buffer->offset;
378 static void buffer_reset(Buffer *buffer)
383 static void buffer_append(Buffer *buffer, const void *data, size_t len)
385 memcpy(buffer->buffer + buffer->offset, data, len);
386 buffer->offset += len;
389 static int vnc_client_io_error(VncState *vs, int ret)
391 if (ret == 0 || ret == -1) {
392 if (ret == -1 && (errno == EINTR || errno == EAGAIN))
395 qemu_set_fd_handler2(vs->csock, NULL, NULL, NULL, NULL);
398 buffer_reset(&vs->input);
399 buffer_reset(&vs->output);
406 static void vnc_client_error(VncState *vs)
409 vnc_client_io_error(vs, -1);
412 static void vnc_client_write(void *opaque)
415 VncState *vs = opaque;
417 ret = write(vs->csock, vs->output.buffer, vs->output.offset);
418 ret = vnc_client_io_error(vs, ret);
422 memmove(vs->output.buffer, vs->output.buffer + ret, (vs->output.offset - ret));
423 vs->output.offset -= ret;
425 if (vs->output.offset == 0) {
426 qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
430 static void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting)
432 vs->read_handler = func;
433 vs->read_handler_expect = expecting;
436 static void vnc_client_read(void *opaque)
438 VncState *vs = opaque;
441 buffer_reserve(&vs->input, 4096);
443 ret = read(vs->csock, buffer_end(&vs->input), 4096);
444 ret = vnc_client_io_error(vs, ret);
448 vs->input.offset += ret;
450 while (vs->read_handler && vs->input.offset >= vs->read_handler_expect) {
451 size_t len = vs->read_handler_expect;
454 ret = vs->read_handler(vs, vs->input.buffer, len);
459 memmove(vs->input.buffer, vs->input.buffer + len, (vs->input.offset - len));
460 vs->input.offset -= len;
462 vs->read_handler_expect = ret;
467 static void vnc_write(VncState *vs, const void *data, size_t len)
469 buffer_reserve(&vs->output, len);
471 if (buffer_empty(&vs->output)) {
472 qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, vnc_client_write, vs);
475 buffer_append(&vs->output, data, len);
478 static void vnc_write_s32(VncState *vs, int32_t value)
480 vnc_write_u32(vs, *(uint32_t *)&value);
483 static void vnc_write_u32(VncState *vs, uint32_t value)
487 buf[0] = (value >> 24) & 0xFF;
488 buf[1] = (value >> 16) & 0xFF;
489 buf[2] = (value >> 8) & 0xFF;
490 buf[3] = value & 0xFF;
492 vnc_write(vs, buf, 4);
495 static void vnc_write_u16(VncState *vs, uint16_t value)
499 buf[0] = (value >> 8) & 0xFF;
500 buf[1] = value & 0xFF;
502 vnc_write(vs, buf, 2);
505 static void vnc_write_u8(VncState *vs, uint8_t value)
507 vnc_write(vs, (char *)&value, 1);
510 static void vnc_flush(VncState *vs)
512 if (vs->output.offset)
513 vnc_client_write(vs);
516 static uint8_t read_u8(char *data, size_t offset)
521 static uint16_t read_u16(char *data, size_t offset)
523 return ((data[offset] & 0xFF) << 8) | (data[offset + 1] & 0xFF);
526 static int32_t read_s32(char *data, size_t offset)
528 return (int32_t)((data[offset] << 24) | (data[offset + 1] << 16) |
529 (data[offset + 2] << 8) | data[offset + 3]);
532 static uint32_t read_u32(char *data, size_t offset)
534 return ((data[offset] << 24) | (data[offset + 1] << 16) |
535 (data[offset + 2] << 8) | data[offset + 3]);
538 static void client_cut_text(VncState *vs, size_t len, char *text)
542 static void pointer_event(VncState *vs, int button_mask, int x, int y)
547 if (button_mask & 0x01)
548 buttons |= MOUSE_EVENT_LBUTTON;
549 if (button_mask & 0x02)
550 buttons |= MOUSE_EVENT_MBUTTON;
551 if (button_mask & 0x04)
552 buttons |= MOUSE_EVENT_RBUTTON;
553 if (button_mask & 0x08)
555 if (button_mask & 0x10)
558 if (kbd_mouse_is_absolute()) {
559 kbd_mouse_event(x * 0x7FFF / vs->ds->width,
560 y * 0x7FFF / vs->ds->height,
563 static int last_x = -1;
564 static int last_y = -1;
567 kbd_mouse_event(x - last_x, y - last_y, dz, buttons);
574 static void key_event(VncState *vs, int down, uint32_t sym)
578 keycode = keysym2scancode(vs->kbd_layout, sym & 0xFFFF);
581 kbd_put_keycode(0xe0);
583 kbd_put_keycode(keycode & 0x7f);
585 kbd_put_keycode(keycode | 0x80);
588 static void framebuffer_update_request(VncState *vs, int incremental,
589 int x_position, int y_position,
595 char *old_row = vs->old_data + y_position * vs->ds->linesize;
597 for (i = 0; i < h; i++) {
598 vs->dirty_row[y_position + i] = (1ULL << (vs->ds->width / 16)) - 1;
599 if (vs->ds->width == 1024) {
600 vs->dirty_row[y_position + i] = ~(0ULL);
602 memset(old_row, 42, vs->ds->width * vs->depth);
603 old_row += vs->ds->linesize;
608 static void set_encodings(VncState *vs, int32_t *encodings, size_t n_encodings)
614 vs->ds->dpy_copy = NULL;
616 for (i = n_encodings - 1; i >= 0; i--) {
617 switch (encodings[i]) {
621 case 1: /* CopyRect */
622 vs->ds->dpy_copy = vnc_copy;
624 case 5: /* Hextile */
627 case -223: /* DesktopResize */
636 static void set_pixel_format(VncState *vs,
637 int bits_per_pixel, int depth,
638 int big_endian_flag, int true_color_flag,
639 int red_max, int green_max, int blue_max,
640 int red_shift, int green_shift, int blue_shift)
642 switch (bits_per_pixel) {
654 vnc_client_error(vs);
658 if (!true_color_flag)
659 vnc_client_error(vs);
661 vnc_dpy_resize(vs->ds, vs->ds->width, vs->ds->height);
662 memset(vs->dirty_row, 0xFF, sizeof(vs->dirty_row));
663 memset(vs->old_data, 42, vs->ds->linesize * vs->ds->height);
669 static int protocol_client_msg(VncState *vs, char *data, size_t len)
679 set_pixel_format(vs, read_u8(data, 4), read_u8(data, 5),
680 read_u8(data, 6), read_u8(data, 7),
681 read_u16(data, 8), read_u16(data, 10),
682 read_u16(data, 12), read_u8(data, 14),
683 read_u8(data, 15), read_u8(data, 16));
690 return 4 + (read_u16(data, 2) * 4);
692 limit = read_u16(data, 2);
693 for (i = 0; i < limit; i++) {
694 int32_t val = read_s32(data, 4 + (i * 4));
695 memcpy(data + 4 + (i * 4), &val, sizeof(val));
698 set_encodings(vs, (int32_t *)(data + 4), limit);
704 framebuffer_update_request(vs,
705 read_u8(data, 1), read_u16(data, 2), read_u16(data, 4),
706 read_u16(data, 6), read_u16(data, 8));
712 key_event(vs, read_u8(data, 1), read_u32(data, 4));
718 pointer_event(vs, read_u8(data, 1), read_u16(data, 2), read_u16(data, 4));
725 return 8 + read_u32(data, 4);
727 client_cut_text(vs, read_u32(data, 4), data + 8);
730 printf("Msg: %d\n", data[0]);
731 vnc_client_error(vs);
735 vnc_read_when(vs, protocol_client_msg, 1);
739 static int protocol_client_init(VncState *vs, char *data, size_t len)
741 char pad[3] = { 0, 0, 0 };
743 vs->width = vs->ds->width;
744 vs->height = vs->ds->height;
745 vnc_write_u16(vs, vs->ds->width);
746 vnc_write_u16(vs, vs->ds->height);
748 vnc_write_u8(vs, vs->depth * 8); /* bits-per-pixel */
749 vnc_write_u8(vs, vs->depth * 8); /* depth */
750 vnc_write_u8(vs, 0); /* big-endian-flag */
751 vnc_write_u8(vs, 1); /* true-color-flag */
752 if (vs->depth == 4) {
753 vnc_write_u16(vs, 0xFF); /* red-max */
754 vnc_write_u16(vs, 0xFF); /* green-max */
755 vnc_write_u16(vs, 0xFF); /* blue-max */
756 vnc_write_u8(vs, 16); /* red-shift */
757 vnc_write_u8(vs, 8); /* green-shift */
758 vnc_write_u8(vs, 0); /* blue-shift */
759 } else if (vs->depth == 2) {
760 vnc_write_u16(vs, 31); /* red-max */
761 vnc_write_u16(vs, 63); /* green-max */
762 vnc_write_u16(vs, 31); /* blue-max */
763 vnc_write_u8(vs, 11); /* red-shift */
764 vnc_write_u8(vs, 5); /* green-shift */
765 vnc_write_u8(vs, 0); /* blue-shift */
766 } else if (vs->depth == 1) {
767 vnc_write_u16(vs, 3); /* red-max */
768 vnc_write_u16(vs, 7); /* green-max */
769 vnc_write_u16(vs, 3); /* blue-max */
770 vnc_write_u8(vs, 5); /* red-shift */
771 vnc_write_u8(vs, 2); /* green-shift */
772 vnc_write_u8(vs, 0); /* blue-shift */
775 vnc_write(vs, pad, 3); /* padding */
777 vnc_write_u32(vs, 4);
778 vnc_write(vs, "QEMU", 4);
781 vnc_read_when(vs, protocol_client_msg, 1);
786 static int protocol_version(VncState *vs, char *version, size_t len)
791 memcpy(local, version, 12);
794 if (sscanf(local, "RFB %03d.%03d\n", &maj, &min) != 2) {
795 vnc_client_error(vs);
799 vnc_write_u32(vs, 1); /* None */
802 vnc_read_when(vs, protocol_client_init, 1);
807 static void vnc_listen_read(void *opaque)
809 VncState *vs = opaque;
810 struct sockaddr_in addr;
811 socklen_t addrlen = sizeof(addr);
813 vs->csock = accept(vs->lsock, (struct sockaddr *)&addr, &addrlen);
814 if (vs->csock != -1) {
815 fcntl(vs->csock, F_SETFL, O_NONBLOCK);
816 qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, opaque);
817 vnc_write(vs, "RFB 003.003\n", 12);
819 vnc_read_when(vs, protocol_version, 12);
820 memset(vs->old_data, 0, vs->ds->linesize * vs->ds->height);
821 memset(vs->dirty_row, 0xFF, sizeof(vs->dirty_row));
824 vs->ds->dpy_copy = NULL;
828 void vnc_display_init(DisplayState *ds, int display)
830 struct sockaddr_in addr;
834 vs = qemu_mallocz(sizeof(VncState));
846 if (!keyboard_layout)
847 keyboard_layout = "en-us";
849 vs->kbd_layout = init_keyboard_layout(keyboard_layout);
853 vs->lsock = socket(PF_INET, SOCK_STREAM, 0);
854 if (vs->lsock == -1) {
855 fprintf(stderr, "Could not create socket\n");
859 addr.sin_family = AF_INET;
860 addr.sin_port = htons(5900 + display);
861 memset(&addr.sin_addr, 0, sizeof(addr.sin_addr));
864 ret = setsockopt(vs->lsock, SOL_SOCKET, SO_REUSEADDR,
865 &reuse_addr, sizeof(reuse_addr));
867 fprintf(stderr, "setsockopt() failed\n");
871 if (bind(vs->lsock, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
872 fprintf(stderr, "bind() failed\n");
876 if (listen(vs->lsock, 1) == -1) {
877 fprintf(stderr, "listen() failed\n");
881 ret = qemu_set_fd_handler2(vs->lsock, vnc_listen_poll, vnc_listen_read, NULL, vs);
887 vs->ds->dpy_update = vnc_dpy_update;
888 vs->ds->dpy_resize = vnc_dpy_resize;
889 vs->ds->dpy_refresh = vnc_dpy_refresh;
891 memset(vs->dirty_row, 0xFF, sizeof(vs->dirty_row));
893 vnc_dpy_resize(vs->ds, 640, 400);