2 #include "qemu_socket.h"
4 #define VNC_REFRESH_INTERVAL (1000 / 30)
6 #include "vnc_keysym.h"
16 typedef struct VncState VncState;
18 typedef int VncReadEvent(VncState *vs, char *data, size_t len);
29 uint64_t dirty_row[768];
36 kbd_layout_t *kbd_layout;
38 VncReadEvent *read_handler;
39 size_t read_handler_expect;
43 1) Get the queue working for IO.
44 2) there is some weirdness when using the -S option (the screen is grey
45 and not totally invalidated
49 static void vnc_write(VncState *vs, const void *data, size_t len);
50 static void vnc_write_u32(VncState *vs, uint32_t value);
51 static void vnc_write_s32(VncState *vs, int32_t value);
52 static void vnc_write_u16(VncState *vs, uint16_t value);
53 static void vnc_write_u8(VncState *vs, uint8_t value);
54 static void vnc_flush(VncState *vs);
55 static void vnc_update_client(void *opaque);
56 static void vnc_client_read(void *opaque);
58 static void vnc_dpy_update(DisplayState *ds, int x, int y, int w, int h)
60 VncState *vs = ds->opaque;
66 for (i = 0; i < w; i += 16)
67 vs->dirty_row[y] |= (1ULL << ((x + i) / 16));
70 static void vnc_framebuffer_update(VncState *vs, int x, int y, int w, int h,
78 vnc_write_s32(vs, encoding);
81 static void vnc_dpy_resize(DisplayState *ds, int w, int h)
83 VncState *vs = ds->opaque;
85 ds->data = realloc(ds->data, w * h * vs->depth);
86 vs->old_data = realloc(vs->old_data, w * h * vs->depth);
88 if (ds->data == NULL || vs->old_data == NULL) {
89 fprintf(stderr, "vnc: memory allocation failed\n");
93 ds->depth = vs->depth * 8;
96 ds->linesize = w * vs->depth;
97 if (vs->csock != -1 && vs->has_resize) {
98 vnc_write_u8(vs, 0); /* msg id */
100 vnc_write_u16(vs, 1); /* number of rects */
101 vnc_framebuffer_update(vs, 0, 0, ds->width, ds->height, -223);
103 vs->width = ds->width;
104 vs->height = ds->height;
108 static void send_framebuffer_update_raw(VncState *vs, int x, int y, int w, int h)
113 vnc_framebuffer_update(vs, x, y, w, h, 0);
115 row = vs->ds->data + y * vs->ds->linesize + x * vs->depth;
116 for (i = 0; i < h; i++) {
117 vnc_write(vs, row, w * vs->depth);
118 row += vs->ds->linesize;
122 static void hextile_enc_cord(uint8_t *ptr, int x, int y, int w, int h)
124 ptr[0] = ((x & 0x0F) << 4) | (y & 0x0F);
125 ptr[1] = (((w - 1) & 0x0F) << 4) | ((h - 1) & 0x0F);
129 #include "vnchextile.h"
133 #include "vnchextile.h"
137 #include "vnchextile.h"
140 static void send_framebuffer_update_hextile(VncState *vs, int x, int y, int w, int h)
144 uint32_t last_fg32, last_bg32;
145 uint16_t last_fg16, last_bg16;
146 uint8_t last_fg8, last_bg8;
148 vnc_framebuffer_update(vs, x, y, w, h, 5);
151 for (j = y; j < (y + h); j += 16) {
152 for (i = x; i < (x + w); i += 16) {
155 send_hextile_tile_8(vs, i, j, MIN(16, x + w - i), MIN(16, y + h - j),
156 &last_bg8, &last_fg8, &has_bg, &has_fg);
159 send_hextile_tile_16(vs, i, j, MIN(16, x + w - i), MIN(16, y + h - j),
160 &last_bg16, &last_fg16, &has_bg, &has_fg);
163 send_hextile_tile_32(vs, i, j, MIN(16, x + w - i), MIN(16, y + h - j),
164 &last_bg32, &last_fg32, &has_bg, &has_fg);
173 static void send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
176 send_framebuffer_update_hextile(vs, x, y, w, h);
178 send_framebuffer_update_raw(vs, x, y, w, h);
181 static void vnc_copy(DisplayState *ds, int src_x, int src_y, int dst_x, int dst_y, int w, int h)
188 int pitch = ds->linesize;
189 VncState *vs = ds->opaque;
191 vnc_update_client(vs);
198 src = (ds->linesize * (src_y + y) + vs->depth * src_x);
199 dst = (ds->linesize * (dst_y + y) + vs->depth * dst_x);
201 src_row = ds->data + src;
202 dst_row = ds->data + dst;
203 old_row = vs->old_data + dst;
205 for (y = 0; y < h; y++) {
206 memmove(old_row, src_row, w * vs->depth);
207 memmove(dst_row, src_row, w * vs->depth);
213 vnc_write_u8(vs, 0); /* msg id */
215 vnc_write_u16(vs, 1); /* number of rects */
216 vnc_framebuffer_update(vs, dst_x, dst_y, w, h, 1);
217 vnc_write_u16(vs, src_x);
218 vnc_write_u16(vs, src_y);
222 static int find_dirty_height(VncState *vs, int y, int last_x, int x)
226 for (h = 1; h < (vs->height - y); h++) {
228 if (!(vs->dirty_row[y + h] & (1ULL << last_x)))
230 for (tmp_x = last_x; tmp_x < x; tmp_x++)
231 vs->dirty_row[y + h] &= ~(1ULL << tmp_x);
237 static void vnc_update_client(void *opaque)
239 VncState *vs = opaque;
241 if (vs->need_update && vs->csock != -1) {
250 width_mask = (1ULL << (vs->width / 16)) - 1;
252 if (vs->width == 1024)
253 width_mask = ~(0ULL);
255 /* Walk through the dirty map and eliminate tiles that
256 really aren't dirty */
258 old_row = vs->old_data;
260 for (y = 0; y < vs->height; y++) {
261 if (vs->dirty_row[y] & width_mask) {
268 for (x = 0; x < vs->ds->width; x += 16) {
269 if (memcmp(old_ptr, ptr, 16 * vs->depth) == 0) {
270 vs->dirty_row[y] &= ~(1ULL << (x / 16));
273 memcpy(old_ptr, ptr, 16 * vs->depth);
276 ptr += 16 * vs->depth;
277 old_ptr += 16 * vs->depth;
281 row += vs->ds->linesize;
282 old_row += vs->ds->linesize;
286 qemu_mod_timer(vs->timer, qemu_get_clock(rt_clock) + VNC_REFRESH_INTERVAL);
290 /* Count rectangles */
292 vnc_write_u8(vs, 0); /* msg id */
294 saved_offset = vs->output.offset;
295 vnc_write_u16(vs, 0);
297 for (y = 0; y < vs->height; y++) {
300 for (x = 0; x < vs->width / 16; x++) {
301 if (vs->dirty_row[y] & (1ULL << x)) {
305 vs->dirty_row[y] &= ~(1ULL << x);
308 int h = find_dirty_height(vs, y, last_x, x);
309 send_framebuffer_update(vs, last_x * 16, y, (x - last_x) * 16, h);
316 int h = find_dirty_height(vs, y, last_x, x);
317 send_framebuffer_update(vs, last_x * 16, y, (x - last_x) * 16, h);
321 vs->output.buffer[saved_offset] = (n_rectangles >> 8) & 0xFF;
322 vs->output.buffer[saved_offset + 1] = n_rectangles & 0xFF;
326 qemu_mod_timer(vs->timer, qemu_get_clock(rt_clock) + VNC_REFRESH_INTERVAL);
329 static void vnc_timer_init(VncState *vs)
331 if (vs->timer == NULL) {
332 vs->timer = qemu_new_timer(rt_clock, vnc_update_client, vs);
333 qemu_mod_timer(vs->timer, qemu_get_clock(rt_clock));
337 static void vnc_dpy_refresh(DisplayState *ds)
339 VncState *vs = ds->opaque;
344 static int vnc_listen_poll(void *opaque)
346 VncState *vs = opaque;
352 static void buffer_reserve(Buffer *buffer, size_t len)
354 if ((buffer->capacity - buffer->offset) < len) {
355 buffer->capacity += (len + 1024);
356 buffer->buffer = realloc(buffer->buffer, buffer->capacity);
357 if (buffer->buffer == NULL) {
358 fprintf(stderr, "vnc: out of memory\n");
364 static int buffer_empty(Buffer *buffer)
366 return buffer->offset == 0;
369 static char *buffer_end(Buffer *buffer)
371 return buffer->buffer + buffer->offset;
374 static void buffer_reset(Buffer *buffer)
379 static void buffer_append(Buffer *buffer, const void *data, size_t len)
381 memcpy(buffer->buffer + buffer->offset, data, len);
382 buffer->offset += len;
385 static int vnc_client_io_error(VncState *vs, int ret, int last_errno)
387 if (ret == 0 || ret == -1) {
388 if (ret == -1 && (last_errno == EINTR || last_errno == EAGAIN))
391 qemu_set_fd_handler2(vs->csock, NULL, NULL, NULL, NULL);
392 closesocket(vs->csock);
394 buffer_reset(&vs->input);
395 buffer_reset(&vs->output);
402 static void vnc_client_error(VncState *vs)
404 vnc_client_io_error(vs, -1, EINVAL);
407 static void vnc_client_write(void *opaque)
410 VncState *vs = opaque;
412 ret = send(vs->csock, vs->output.buffer, vs->output.offset, 0);
413 ret = vnc_client_io_error(vs, ret, socket_error());
417 memmove(vs->output.buffer, vs->output.buffer + ret, (vs->output.offset - ret));
418 vs->output.offset -= ret;
420 if (vs->output.offset == 0) {
421 qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
425 static void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting)
427 vs->read_handler = func;
428 vs->read_handler_expect = expecting;
431 static void vnc_client_read(void *opaque)
433 VncState *vs = opaque;
436 buffer_reserve(&vs->input, 4096);
438 ret = recv(vs->csock, buffer_end(&vs->input), 4096, 0);
439 ret = vnc_client_io_error(vs, ret, socket_error());
443 vs->input.offset += ret;
445 while (vs->read_handler && vs->input.offset >= vs->read_handler_expect) {
446 size_t len = vs->read_handler_expect;
449 ret = vs->read_handler(vs, vs->input.buffer, len);
454 memmove(vs->input.buffer, vs->input.buffer + len, (vs->input.offset - len));
455 vs->input.offset -= len;
457 vs->read_handler_expect = ret;
462 static void vnc_write(VncState *vs, const void *data, size_t len)
464 buffer_reserve(&vs->output, len);
466 if (buffer_empty(&vs->output)) {
467 qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, vnc_client_write, vs);
470 buffer_append(&vs->output, data, len);
473 static void vnc_write_s32(VncState *vs, int32_t value)
475 vnc_write_u32(vs, *(uint32_t *)&value);
478 static void vnc_write_u32(VncState *vs, uint32_t value)
482 buf[0] = (value >> 24) & 0xFF;
483 buf[1] = (value >> 16) & 0xFF;
484 buf[2] = (value >> 8) & 0xFF;
485 buf[3] = value & 0xFF;
487 vnc_write(vs, buf, 4);
490 static void vnc_write_u16(VncState *vs, uint16_t value)
494 buf[0] = (value >> 8) & 0xFF;
495 buf[1] = value & 0xFF;
497 vnc_write(vs, buf, 2);
500 static void vnc_write_u8(VncState *vs, uint8_t value)
502 vnc_write(vs, (char *)&value, 1);
505 static void vnc_flush(VncState *vs)
507 if (vs->output.offset)
508 vnc_client_write(vs);
511 static uint8_t read_u8(char *data, size_t offset)
516 static uint16_t read_u16(char *data, size_t offset)
518 return ((data[offset] & 0xFF) << 8) | (data[offset + 1] & 0xFF);
521 static int32_t read_s32(char *data, size_t offset)
523 return (int32_t)((data[offset] << 24) | (data[offset + 1] << 16) |
524 (data[offset + 2] << 8) | data[offset + 3]);
527 static uint32_t read_u32(char *data, size_t offset)
529 return ((data[offset] << 24) | (data[offset + 1] << 16) |
530 (data[offset + 2] << 8) | data[offset + 3]);
533 static void client_cut_text(VncState *vs, size_t len, char *text)
537 static void pointer_event(VncState *vs, int button_mask, int x, int y)
542 if (button_mask & 0x01)
543 buttons |= MOUSE_EVENT_LBUTTON;
544 if (button_mask & 0x02)
545 buttons |= MOUSE_EVENT_MBUTTON;
546 if (button_mask & 0x04)
547 buttons |= MOUSE_EVENT_RBUTTON;
548 if (button_mask & 0x08)
550 if (button_mask & 0x10)
553 if (kbd_mouse_is_absolute()) {
554 kbd_mouse_event(x * 0x7FFF / vs->ds->width,
555 y * 0x7FFF / vs->ds->height,
558 static int last_x = -1;
559 static int last_y = -1;
562 kbd_mouse_event(x - last_x, y - last_y, dz, buttons);
569 static void key_event(VncState *vs, int down, uint32_t sym)
573 keycode = keysym2scancode(vs->kbd_layout, sym & 0xFFFF);
576 kbd_put_keycode(0xe0);
578 kbd_put_keycode(keycode & 0x7f);
580 kbd_put_keycode(keycode | 0x80);
583 static void framebuffer_update_request(VncState *vs, int incremental,
584 int x_position, int y_position,
590 char *old_row = vs->old_data + y_position * vs->ds->linesize;
592 for (i = 0; i < h; i++) {
593 vs->dirty_row[y_position + i] = (1ULL << (vs->ds->width / 16)) - 1;
594 if (vs->ds->width == 1024) {
595 vs->dirty_row[y_position + i] = ~(0ULL);
597 memset(old_row, 42, vs->ds->width * vs->depth);
598 old_row += vs->ds->linesize;
603 static void set_encodings(VncState *vs, int32_t *encodings, size_t n_encodings)
609 vs->ds->dpy_copy = NULL;
611 for (i = n_encodings - 1; i >= 0; i--) {
612 switch (encodings[i]) {
616 case 1: /* CopyRect */
617 vs->ds->dpy_copy = vnc_copy;
619 case 5: /* Hextile */
622 case -223: /* DesktopResize */
631 static void set_pixel_format(VncState *vs,
632 int bits_per_pixel, int depth,
633 int big_endian_flag, int true_color_flag,
634 int red_max, int green_max, int blue_max,
635 int red_shift, int green_shift, int blue_shift)
637 switch (bits_per_pixel) {
649 vnc_client_error(vs);
653 if (!true_color_flag)
654 vnc_client_error(vs);
656 vnc_dpy_resize(vs->ds, vs->ds->width, vs->ds->height);
657 memset(vs->dirty_row, 0xFF, sizeof(vs->dirty_row));
658 memset(vs->old_data, 42, vs->ds->linesize * vs->ds->height);
664 static int protocol_client_msg(VncState *vs, char *data, size_t len)
674 set_pixel_format(vs, read_u8(data, 4), read_u8(data, 5),
675 read_u8(data, 6), read_u8(data, 7),
676 read_u16(data, 8), read_u16(data, 10),
677 read_u16(data, 12), read_u8(data, 14),
678 read_u8(data, 15), read_u8(data, 16));
685 return 4 + (read_u16(data, 2) * 4);
687 limit = read_u16(data, 2);
688 for (i = 0; i < limit; i++) {
689 int32_t val = read_s32(data, 4 + (i * 4));
690 memcpy(data + 4 + (i * 4), &val, sizeof(val));
693 set_encodings(vs, (int32_t *)(data + 4), limit);
699 framebuffer_update_request(vs,
700 read_u8(data, 1), read_u16(data, 2), read_u16(data, 4),
701 read_u16(data, 6), read_u16(data, 8));
707 key_event(vs, read_u8(data, 1), read_u32(data, 4));
713 pointer_event(vs, read_u8(data, 1), read_u16(data, 2), read_u16(data, 4));
720 return 8 + read_u32(data, 4);
722 client_cut_text(vs, read_u32(data, 4), data + 8);
725 printf("Msg: %d\n", data[0]);
726 vnc_client_error(vs);
730 vnc_read_when(vs, protocol_client_msg, 1);
734 static int protocol_client_init(VncState *vs, char *data, size_t len)
736 char pad[3] = { 0, 0, 0 };
738 vs->width = vs->ds->width;
739 vs->height = vs->ds->height;
740 vnc_write_u16(vs, vs->ds->width);
741 vnc_write_u16(vs, vs->ds->height);
743 vnc_write_u8(vs, vs->depth * 8); /* bits-per-pixel */
744 vnc_write_u8(vs, vs->depth * 8); /* depth */
745 vnc_write_u8(vs, 0); /* big-endian-flag */
746 vnc_write_u8(vs, 1); /* true-color-flag */
747 if (vs->depth == 4) {
748 vnc_write_u16(vs, 0xFF); /* red-max */
749 vnc_write_u16(vs, 0xFF); /* green-max */
750 vnc_write_u16(vs, 0xFF); /* blue-max */
751 vnc_write_u8(vs, 16); /* red-shift */
752 vnc_write_u8(vs, 8); /* green-shift */
753 vnc_write_u8(vs, 0); /* blue-shift */
754 } else if (vs->depth == 2) {
755 vnc_write_u16(vs, 31); /* red-max */
756 vnc_write_u16(vs, 63); /* green-max */
757 vnc_write_u16(vs, 31); /* blue-max */
758 vnc_write_u8(vs, 11); /* red-shift */
759 vnc_write_u8(vs, 5); /* green-shift */
760 vnc_write_u8(vs, 0); /* blue-shift */
761 } else if (vs->depth == 1) {
762 vnc_write_u16(vs, 3); /* red-max */
763 vnc_write_u16(vs, 7); /* green-max */
764 vnc_write_u16(vs, 3); /* blue-max */
765 vnc_write_u8(vs, 5); /* red-shift */
766 vnc_write_u8(vs, 2); /* green-shift */
767 vnc_write_u8(vs, 0); /* blue-shift */
770 vnc_write(vs, pad, 3); /* padding */
772 vnc_write_u32(vs, 4);
773 vnc_write(vs, "QEMU", 4);
776 vnc_read_when(vs, protocol_client_msg, 1);
781 static int protocol_version(VncState *vs, char *version, size_t len)
786 memcpy(local, version, 12);
789 if (sscanf(local, "RFB %03d.%03d\n", &maj, &min) != 2) {
790 vnc_client_error(vs);
794 vnc_write_u32(vs, 1); /* None */
797 vnc_read_when(vs, protocol_client_init, 1);
802 static void vnc_listen_read(void *opaque)
804 VncState *vs = opaque;
805 struct sockaddr_in addr;
806 socklen_t addrlen = sizeof(addr);
808 vs->csock = accept(vs->lsock, (struct sockaddr *)&addr, &addrlen);
809 if (vs->csock != -1) {
810 socket_set_nonblock(vs->csock);
811 qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, opaque);
812 vnc_write(vs, "RFB 003.003\n", 12);
814 vnc_read_when(vs, protocol_version, 12);
815 memset(vs->old_data, 0, vs->ds->linesize * vs->ds->height);
816 memset(vs->dirty_row, 0xFF, sizeof(vs->dirty_row));
819 vs->ds->dpy_copy = NULL;
823 void vnc_display_init(DisplayState *ds, int display)
825 struct sockaddr_in addr;
829 vs = qemu_mallocz(sizeof(VncState));
841 if (!keyboard_layout)
842 keyboard_layout = "en-us";
844 vs->kbd_layout = init_keyboard_layout(keyboard_layout);
848 vs->lsock = socket(PF_INET, SOCK_STREAM, 0);
849 if (vs->lsock == -1) {
850 fprintf(stderr, "Could not create socket\n");
854 addr.sin_family = AF_INET;
855 addr.sin_port = htons(5900 + display);
856 memset(&addr.sin_addr, 0, sizeof(addr.sin_addr));
859 ret = setsockopt(vs->lsock, SOL_SOCKET, SO_REUSEADDR,
860 (const char *)&reuse_addr, sizeof(reuse_addr));
862 fprintf(stderr, "setsockopt() failed\n");
866 if (bind(vs->lsock, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
867 fprintf(stderr, "bind() failed\n");
871 if (listen(vs->lsock, 1) == -1) {
872 fprintf(stderr, "listen() failed\n");
876 ret = qemu_set_fd_handler2(vs->lsock, vnc_listen_poll, vnc_listen_read, NULL, vs);
882 vs->ds->dpy_update = vnc_dpy_update;
883 vs->ds->dpy_resize = vnc_dpy_resize;
884 vs->ds->dpy_refresh = vnc_dpy_refresh;
886 memset(vs->dirty_row, 0xFF, sizeof(vs->dirty_row));
888 vnc_dpy_resize(vs->ds, 640, 400);