Lines Matching defs:vs

207 static inline uint32_t vnc_has_feature(VncState *vs, int feature) {
208 return (vs->features & (1 << feature));
219 static void vnc_disconnect_start(VncState *vs);
220 static void vnc_disconnect_finish(VncState *vs);
222 static void vnc_colordepth(VncState *vs);
265 static void vnc_update(VncState *vs, int x, int y, int w, int h)
267 struct VncSurface *s = &vs->guest;
292 VncState *vs = vd->clients;
293 while (vs != NULL) {
294 vnc_update(vs, x, y, w, h);
295 vs = vs->next;
299 static void vnc_framebuffer_update(VncState *vs, int x, int y, int w, int h,
302 vnc_write_u16(vs, x);
303 vnc_write_u16(vs, y);
304 vnc_write_u16(vs, w);
305 vnc_write_u16(vs, h);
307 vnc_write_s32(vs, encoding);
343 static void vnc_resize(VncState *vs)
345 DisplayState *ds = vs->ds;
349 if (!vs->guest.ds)
350 vs->guest.ds = qemu_mallocz(sizeof(*vs->guest.ds));
351 if (ds_get_bytes_per_pixel(ds) != vs->guest.ds->pf.bytes_per_pixel)
353 vnc_colordepth(vs);
354 size_changed = ds_get_width(ds) != vs->guest.ds->width ||
355 ds_get_height(ds) != vs->guest.ds->height;
356 *(vs->guest.ds) = *(ds->surface);
358 if (vs->csock != -1 && vnc_has_feature(vs, VNC_FEATURE_RESIZE)) {
359 vnc_write_u8(vs, 0); /* msg id */
360 vnc_write_u8(vs, 0);
361 vnc_write_u16(vs, 1); /* number of rects */
362 vnc_framebuffer_update(vs, 0, 0, ds_get_width(ds), ds_get_height(ds),
364 vnc_flush(vs);
367 memset(vs->guest.dirty, 0xFF, sizeof(vs->guest.dirty));
370 if (!vs->server.ds)
371 vs->server.ds = qemu_mallocz(sizeof(*vs->server.ds));
372 if (vs->server.ds->data)
373 qemu_free(vs->server.ds->data);
374 *(vs->server.ds) = *(ds->surface);
375 vs->server.ds->data = qemu_mallocz(vs->server.ds->linesize *
376 vs->server.ds->height);
377 memset(vs->server.dirty, 0xFF, sizeof(vs->guest.dirty));
383 VncState *vs = vd->clients;
384 while (vs != NULL) {
385 vnc_resize(vs);
386 vs = vs->next;
391 static void vnc_write_pixels_copy(VncState *vs, void *pixels, int size)
393 vnc_write(vs, pixels, size);
397 static void vnc_convert_pixel(VncState *vs, uint8_t *buf, uint32_t v)
401 r = ((((v & vs->server.ds->pf.rmask) >> vs->server.ds->pf.rshift) << vs->clientds.pf.rbits) >>
402 vs->server.ds->pf.rbits);
403 g = ((((v & vs->server.ds->pf.gmask) >> vs->server.ds->pf.gshift) << vs->clientds.pf.gbits) >>
404 vs->server.ds->pf.gbits);
405 b = ((((v & vs->server.ds->pf.bmask) >> vs->server.ds->pf.bshift) << vs->clientds.pf.bbits) >>
406 vs->server.ds->pf.bbits);
407 v = (r << vs->clientds.pf.rshift) |
408 (g << vs->clientds.pf.gshift) |
409 (b << vs->clientds.pf.bshift);
410 switch(vs->clientds.pf.bytes_per_pixel) {
415 if (vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) {
425 if (vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) {
440 static void vnc_write_pixels_generic(VncState *vs, void *pixels1, int size)
444 if (vs->server.ds->pf.bytes_per_pixel == 4) {
449 vnc_convert_pixel(vs, buf, pixels[i]);
450 vnc_write(vs, buf, vs->clientds.pf.bytes_per_pixel);
452 } else if (vs->server.ds->pf.bytes_per_pixel == 2) {
457 vnc_convert_pixel(vs, buf, pixels[i]);
458 vnc_write(vs, buf, vs->clientds.pf.bytes_per_pixel);
460 } else if (vs->server.ds->pf.bytes_per_pixel == 1) {
465 vnc_convert_pixel(vs, buf, pixels[i]);
466 vnc_write(vs, buf, vs->clientds.pf.bytes_per_pixel);
473 static void send_framebuffer_update_raw(VncState *vs, int x, int y, int w, int h)
478 row = vs->server.ds->data + y * ds_get_linesize(vs->ds) + x * ds_get_bytes_per_pixel(vs->ds);
480 vs->write_pixels(vs, row, w * ds_get_bytes_per_pixel(vs->ds));
481 row += ds_get_linesize(vs->ds);
521 static void send_framebuffer_update_hextile(VncState *vs, int x, int y, int w, int h)
527 last_fg = (uint8_t *) qemu_malloc(vs->server.ds->pf.bytes_per_pixel);
528 last_bg = (uint8_t *) qemu_malloc(vs->server.ds->pf.bytes_per_pixel);
532 vs->send_hextile_tile(vs, i, j,
542 static void vnc_zlib_init(VncState *vs)
545 for (i=0; i<(sizeof(vs->zlib_stream) / sizeof(z_stream)); i++)
546 vs->zlib_stream[i].opaque = NULL;
549 static void vnc_zlib_start(VncState *vs)
551 buffer_reset(&vs->zlib);
554 vs->zlib_tmp = vs->output;
555 vs->output = vs->zlib;
558 static int vnc_zlib_stop(VncState *vs, int stream_id)
560 z_streamp zstream = &vs->zlib_stream[stream_id];
564 vs->zlib = vs->output;
565 vs->output = vs->zlib_tmp;
571 if (zstream->opaque != vs) {
575 VNC_DEBUG("VNC: opaque = %p | vs = %p\n", zstream->opaque, vs);
579 err = deflateInit2(zstream, vs->tight_compression, Z_DEFLATED, MAX_WBITS,
587 zstream->opaque = vs;
593 buffer_reserve(&vs->output, vs->zlib.offset + 64);
596 zstream->next_in = vs->zlib.buffer;
597 zstream->avail_in = vs->zlib.offset;
598 zstream->next_out = vs->output.buffer + vs->output.offset;
599 zstream->avail_out = vs->output.capacity - vs->output.offset;
609 vs->output.offset = vs->output.capacity - zstream->avail_out;
613 static void send_framebuffer_update_zlib(VncState *vs, int x, int y, int w, int h)
617 vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_ZLIB);
620 old_offset = vs->output.offset;
621 vnc_write_s32(vs, 0);
624 vnc_zlib_start(vs);
625 send_framebuffer_update_raw(vs, x, y, w, h);
626 bytes_written = vnc_zlib_stop(vs, 0);
632 new_offset = vs->output.offset;
633 vs->output.offset = old_offset;
634 vnc_write_u32(vs, bytes_written);
635 vs->output.offset = new_offset;
638 static void send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
640 switch(vs->vnc_encoding) {
642 send_framebuffer_update_zlib(vs, x, y, w, h);
645 vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_HEXTILE);
646 send_framebuffer_update_hextile(vs, x, y, w, h);
649 vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_RAW);
650 send_framebuffer_update_raw(vs, x, y, w, h);
655 static void vnc_copy(VncState *vs, int src_x, int src_y, int dst_x, int dst_y, int w, int h)
657 vnc_write_u8(vs, 0); /* msg id */
658 vnc_write_u8(vs, 0);
659 vnc_write_u16(vs, 1); /* number of rects */
660 vnc_framebuffer_update(vs, dst_x, dst_y, w, h, VNC_ENCODING_COPYRECT);
661 vnc_write_u16(vs, src_x);
662 vnc_write_u16(vs, src_y);
663 vnc_flush(vs);
669 VncState *vs, *vn;
671 for (vs = vd->clients; vs != NULL; vs = vn) {
672 vn = vs->next;
673 if (vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) {
674 vs->force_update = 1;
675 vnc_update_client(vs);
676 /* vs might be free()ed here */
680 for (vs = vd->clients; vs != NULL; vs = vs->next) {
681 if (vnc_has_feature(vs, VNC_FEATURE_COPYRECT))
682 vnc_copy(vs, src_x, src_y, dst_x, dst_y, w, h);
684 vnc_update(vs, dst_x, dst_y, w, h);
706 VncState *vs = opaque;
707 if (vs->need_update && vs->csock != -1) {
717 if (vs->output.offset && !vs->audio_cap && !vs->force_update) {
719 qemu_mod_timer(vs->timer, qemu_get_clock_ms(rt_clock) + VNC_REFRESH_INTERVAL);
730 vnc_set_bits(width_mask, (ds_get_width(vs->ds) / 16), VNC_DIRTY_WORDS);
731 cmp_bytes = 16 * ds_get_bytes_per_pixel(vs->ds);
732 guest_row = vs->guest.ds->data;
733 server_row = vs->server.ds->data;
734 for (y = 0; y < vs->guest.ds->height; y++) {
735 if (vnc_and_bits(vs->guest.dirty[y], width_mask, VNC_DIRTY_WORDS)) {
743 for (x = 0; x < vs->guest.ds->width;
745 if (!vnc_get_bit(vs->guest.dirty[y], (x / 16)))
747 vnc_clear_bit(vs->guest.dirty[y], (x / 16));
751 vnc_set_bit(vs->server.dirty[y], (x / 16));
755 guest_row += ds_get_linesize(vs->ds);
756 server_row += ds_get_linesize(vs->ds);
759 if (!has_dirty && !vs->audio_cap && !vs->force_update) {
760 qemu_mod_timer(vs->timer, qemu_get_clock_ms(rt_clock) + VNC_REFRESH_INTERVAL);
771 vnc_write_u8(vs, 0); /* msg id */
772 vnc_write_u8(vs, 0);
773 saved_offset = vs->output.offset;
774 vnc_write_u16(vs, 0);
776 for (y = 0; y < vs->server.ds->height; y++) {
779 for (x = 0; x < vs->server.ds->width / 16; x++) {
780 if (vnc_get_bit(vs->server.dirty[y], x)) {
784 vnc_clear_bit(vs->server.dirty[y], x);
787 int h = find_and_clear_dirty_height(&vs->server, y, last_x, x);
788 send_framebuffer_update(vs, last_x * 16, y, (x - last_x) * 16, h);
795 int h = find_and_clear_dirty_height(&vs->server, y, last_x, x);
796 send_framebuffer_update(vs, last_x * 16, y, (x - last_x) * 16, h);
800 vs->output.buffer[saved_offset] = (n_rectangles >> 8) & 0xFF;
801 vs->output.buffer[saved_offset + 1] = n_rectangles & 0xFF;
802 vnc_flush(vs);
803 vs->force_update = 0;
807 if (vs->csock != -1) {
808 qemu_mod_timer(vs->timer, qemu_get_clock_ms(rt_clock) + VNC_REFRESH_INTERVAL);
810 vnc_disconnect_finish(vs);
818 VncState *vs = opaque;
822 vnc_write_u8(vs, 255);
823 vnc_write_u8(vs, 1);
824 vnc_write_u16(vs, 0);
825 vnc_flush(vs);
829 vnc_write_u8(vs, 255);
830 vnc_write_u8(vs, 1);
831 vnc_write_u16(vs, 1);
832 vnc_flush(vs);
843 VncState *vs = opaque;
845 vnc_write_u8(vs, 255);
846 vnc_write_u8(vs, 1);
847 vnc_write_u16(vs, 2);
848 vnc_write_u32(vs, size);
849 vnc_write(vs, buf, size);
850 vnc_flush(vs);
853 static void audio_add(VncState *vs)
858 if (vs->audio_cap) {
867 vs->audio_cap = AUD_add_capture(&vs->as, &ops, vs);
868 if (!vs->audio_cap) {
873 static void audio_del(VncState *vs)
875 if (vs->audio_cap) {
876 AUD_del_capture(vs->audio_cap, vs);
877 vs->audio_cap = NULL;
881 static void vnc_disconnect_start(VncState *vs)
883 if (vs->csock == -1)
885 qemu_set_fd_handler2(vs->csock, NULL, NULL, NULL, NULL);
886 closesocket(vs->csock);
887 vs->csock = -1;
890 static void vnc_disconnect_finish(VncState *vs)
892 qemu_del_timer(vs->timer);
893 qemu_free_timer(vs->timer);
894 if (vs->input.buffer) qemu_free(vs->input.buffer);
895 if (vs->output.buffer) qemu_free(vs->output.buffer);
897 vnc_tls_client_cleanup(vs);
900 vnc_sasl_client_cleanup(vs);
902 audio_del(vs);
905 for (p = vs->vd->clients; p != NULL; p = p->next) {
906 if (p == vs) {
910 vs->vd->clients = p->next;
915 if (!vs->vd->clients)
918 qemu_free(vs->server.ds->data);
919 qemu_free(vs->server.ds);
920 qemu_free(vs->guest.ds);
921 qemu_free(vs);
924 int vnc_client_io_error(VncState *vs, int ret, int last_errno)
942 vnc_disconnect_start(vs);
950 void vnc_client_error(VncState *vs)
953 vnc_disconnect_start(vs);
972 long vnc_client_write_buf(VncState *vs, const uint8_t *data, size_t datalen)
976 if (vs->tls.session) {
977 ret = gnutls_write(vs->tls.session, data, datalen);
987 ret = send(vs->csock, (const void *)data, datalen, 0);
989 return vnc_client_io_error(vs, ret, socket_error());
1003 static long vnc_client_write_plain(VncState *vs)
1009 vs->output.buffer, vs->output.capacity, vs->output.offset,
1010 vs->sasl.waitWriteSSF);
1012 if (vs->sasl.conn &&
1013 vs->sasl.runSSF &&
1014 vs->sasl.waitWriteSSF) {
1015 ret = vnc_client_write_buf(vs, vs->output.buffer, vs->sasl.waitWriteSSF);
1017 vs->sasl.waitWriteSSF -= ret;
1020 ret = vnc_client_write_buf(vs, vs->output.buffer, vs->output.offset);
1024 memmove(vs->output.buffer, vs->output.buffer + ret, (vs->output.offset - ret));
1025 vs->output.offset -= ret;
1027 if (vs->output.offset == 0) {
1028 qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
1043 VncState *vs = opaque;
1046 if (vs->sasl.conn &&
1047 vs->sasl.runSSF &&
1048 !vs->sasl.waitWriteSSF)
1049 ret = vnc_client_write_sasl(vs);
1052 ret = vnc_client_write_plain(vs);
1055 void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting)
1057 vs->read_handler = func;
1058 vs->read_handler_expect = expecting;
1077 long vnc_client_read_buf(VncState *vs, uint8_t *data, size_t datalen)
1081 if (vs->tls.session) {
1082 ret = gnutls_read(vs->tls.session, data, datalen);
1092 ret = recv(vs->csock, (void *)data, datalen, 0);
1094 return vnc_client_io_error(vs, ret, socket_error());
1106 static long vnc_client_read_plain(VncState *vs)
1110 vs->input.buffer, vs->input.capacity, vs->input.offset);
1111 buffer_reserve(&vs->input, 4096);
1112 ret = vnc_client_read_buf(vs, buffer_end(&vs->input), 4096);
1115 vs->input.offset += ret;
1127 VncState *vs = opaque;
1131 if (vs->sasl.conn && vs->sasl.runSSF)
1132 ret = vnc_client_read_sasl(vs);
1135 ret = vnc_client_read_plain(vs);
1137 if (vs->csock == -1)
1138 vnc_disconnect_finish(vs);
1142 while (vs->read_handler && vs->input.offset >= vs->read_handler_expect) {
1143 size_t len = vs->read_handler_expect;
1146 ret = vs->read_handler(vs, vs->input.buffer, len);
1147 if (vs->csock == -1) {
1148 vnc_disconnect_finish(vs);
1153 memmove(vs->input.buffer, vs->input.buffer + len, (vs->input.offset - len));
1154 vs->input.offset -= len;
1156 vs->read_handler_expect = ret;
1161 void vnc_write(VncState *vs, const void *data, size_t len)
1163 buffer_reserve(&vs->output, len);
1165 if (vs->csock != -1 && buffer_empty(&vs->output)) {
1166 qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, vnc_client_write, vs);
1169 buffer_append(&vs->output, data, len);
1172 void vnc_write_s32(VncState *vs, int32_t value)
1174 vnc_write_u32(vs, *(uint32_t *)&value);
1177 void vnc_write_u32(VncState *vs, uint32_t value)
1186 vnc_write(vs, buf, 4);
1189 void vnc_write_u16(VncState *vs, uint16_t value)
1196 vnc_write(vs, buf, 2);
1199 void vnc_write_u8(VncState *vs, uint8_t value)
1201 vnc_write(vs, (char *)&value, 1);
1204 void vnc_flush(VncState *vs)
1206 if (vs->csock != -1 && vs->output.offset)
1207 vnc_client_write(vs);
1232 static void client_cut_text(VncState *vs, size_t len, uint8_t *text)
1236 static void check_pointer_type_change(VncState *vs, int absolute)
1238 if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE) && vs->absolute != absolute) {
1239 vnc_write_u8(vs, 0);
1240 vnc_write_u8(vs, 0);
1241 vnc_write_u16(vs, 1);
1242 vnc_framebuffer_update(vs, absolute, 0,
1243 ds_get_width(vs->ds), ds_get_height(vs->ds),
1245 vnc_flush(vs);
1247 vs->absolute = absolute;
1250 static void pointer_event(VncState *vs, int button_mask, int x, int y)
1266 if (vs->absolute) {
1267 kbd_mouse_event(x * 0x7FFF / (ds_get_width(vs->ds) - 1),
1268 y * 0x7FFF / (ds_get_height(vs->ds) - 1),
1270 } else if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE)) {
1276 if (vs->last_x != -1)
1277 kbd_mouse_event(x - vs->last_x,
1278 y - vs->last_y,
1280 vs->last_x = x;
1281 vs->last_y = y;
1284 check_pointer_type_change(vs, kbd_mouse_is_absolute());
1287 static void reset_keys(VncState *vs)
1291 if (vs->modifiers_state[i]) {
1295 vs->modifiers_state[i] = 0;
1300 static void press_key(VncState *vs, int keysym)
1302 kbd_put_keycode(keysym2scancode(vs->vd->kbd_layout, keysym) & 0x7f);
1303 kbd_put_keycode(keysym2scancode(vs->vd->kbd_layout, keysym) | 0x80);
1306 static void do_key_event(VncState *vs, int down, int keycode, int sym)
1317 vs->modifiers_state[keycode] = 1;
1319 vs->modifiers_state[keycode] = 0;
1322 if (down && vs->modifiers_state[0x1d] && vs->modifiers_state[0x38]) {
1324 reset_keys(vs);
1332 vs->modifiers_state[keycode] ^= 1;
1336 if (keycode_is_keypad(vs->vd->kbd_layout, keycode)) {
1341 if (keysym_is_numlock(vs->vd->kbd_layout, sym & 0xFFFF)) {
1342 if (!vs->modifiers_state[0x45]) {
1343 vs->modifiers_state[0x45] = 1;
1344 press_key(vs, 0xff7f);
1347 if (vs->modifiers_state[0x45]) {
1348 vs->modifiers_state[0x45] = 0;
1349 press_key(vs, 0xff7f);
1364 int numlock = vs->modifiers_state[0x45];
1459 static void key_event(VncState *vs, int down, uint32_t sym)
1466 keycode = keysym2scancode(vs->vd->kbd_layout, sym & 0xFFFF);
1467 do_key_event(vs, down, keycode, sym);
1470 static void ext_key_event(VncState *vs, int down,
1475 key_event(vs, down, sym);
1477 do_key_event(vs, down, keycode, sym);
1480 static void framebuffer_update_request(VncState *vs, int incremental,
1484 if (x_position > ds_get_width(vs->ds))
1485 x_position = ds_get_width(vs->ds);
1486 if (y_position > ds_get_height(vs->ds))
1487 y_position = ds_get_height(vs->ds);
1488 if (x_position + w >= ds_get_width(vs->ds))
1489 w = ds_get_width(vs->ds) - x_position;
1490 if (y_position + h >= ds_get_height(vs->ds))
1491 h = ds_get_height(vs->ds) - y_position;
1494 vs->need_update = 1;
1496 vs->force_update = 1;
1498 vnc_set_bits(vs->guest.dirty[y_position + i],
1499 (ds_get_width(vs->ds) / 16), VNC_DIRTY_WORDS);
1500 vnc_set_bits(vs->server.dirty[y_position + i],
1501 (ds_get_width(vs->ds) / 16), VNC_DIRTY_WORDS);
1506 static void send_ext_key_event_ack(VncState *vs)
1508 vnc_write_u8(vs, 0);
1509 vnc_write_u8(vs, 0);
1510 vnc_write_u16(vs, 1);
1511 vnc_framebuffer_update(vs, 0, 0, ds_get_width(vs->ds), ds_get_height(vs->ds),
1513 vnc_flush(vs);
1516 static void send_ext_audio_ack(VncState *vs)
1518 vnc_write_u8(vs, 0);
1519 vnc_write_u8(vs, 0);
1520 vnc_write_u16(vs, 1);
1521 vnc_framebuffer_update(vs, 0, 0, ds_get_width(vs->ds), ds_get_height(vs->ds),
1523 vnc_flush(vs);
1526 static void set_encodings(VncState *vs, int32_t *encodings, size_t n_encodings)
1531 vnc_zlib_init(vs);
1532 vs->features = 0;
1533 vs->vnc_encoding = 0;
1534 vs->tight_compression = 9;
1535 vs->tight_quality = 9;
1536 vs->absolute = -1;
1542 vs->vnc_encoding = enc;
1545 vs->features |= VNC_FEATURE_COPYRECT_MASK;
1548 vs->features |= VNC_FEATURE_HEXTILE_MASK;
1549 vs->vnc_encoding = enc;
1552 vs->features |= VNC_FEATURE_ZLIB_MASK;
1553 vs->vnc_encoding = enc;
1556 vs->features |= VNC_FEATURE_RESIZE_MASK;
1559 vs->features |= VNC_FEATURE_POINTER_TYPE_CHANGE_MASK;
1562 send_ext_key_event_ack(vs);
1565 send_ext_audio_ack(vs);
1568 vs->features |= VNC_FEATURE_WMVI_MASK;
1571 vs->tight_compression = (enc & 0x0F);
1574 vs->tight_quality = (enc & 0x0F);
1582 check_pointer_type_change(vs, kbd_mouse_is_absolute());
1585 static void set_pixel_conversion(VncState *vs)
1587 if ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) ==
1588 (vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG) &&
1589 !memcmp(&(vs->clientds.pf), &(vs->ds->surface->pf), sizeof(PixelFormat))) {
1590 vs->write_pixels = vnc_write_pixels_copy;
1591 switch (vs->ds->surface->pf.bits_per_pixel) {
1593 vs->send_hextile_tile = send_hextile_tile_8;
1596 vs->send_hextile_tile = send_hextile_tile_16;
1599 vs->send_hextile_tile = send_hextile_tile_32;
1603 vs->write_pixels = vnc_write_pixels_generic;
1604 switch (vs->ds->surface->pf.bits_per_pixel) {
1606 vs->send_hextile_tile = send_hextile_tile_generic_8;
1609 vs->send_hextile_tile = send_hextile_tile_generic_16;
1612 vs->send_hextile_tile = send_hextile_tile_generic_32;
1618 static void set_pixel_format(VncState *vs,
1625 vnc_client_error(vs);
1629 vs->clientds = *(vs->guest.ds);
1630 vs->clientds.pf.rmax = red_max;
1631 count_bits(vs->clientds.pf.rbits, red_max);
1632 vs->clientds.pf.rshift = red_shift;
1633 vs->clientds.pf.rmask = red_max << red_shift;
1634 vs->clientds.pf.gmax = green_max;
1635 count_bits(vs->clientds.pf.gbits, green_max);
1636 vs->clientds.pf.gshift = green_shift;
1637 vs->clientds.pf.gmask = green_max << green_shift;
1638 vs->clientds.pf.bmax = blue_max;
1639 count_bits(vs->clientds.pf.bbits, blue_max);
1640 vs->clientds.pf.bshift = blue_shift;
1641 vs->clientds.pf.bmask = blue_max << blue_shift;
1642 vs->clientds.pf.bits_per_pixel = bits_per_pixel;
1643 vs->clientds.pf.bytes_per_pixel = bits_per_pixel / 8;
1644 vs->clientds.pf.depth = bits_per_pixel == 32 ? 24 : bits_per_pixel;
1645 vs->clientds.flags = big_endian_flag ? QEMU_BIG_ENDIAN_FLAG : 0x00;
1647 set_pixel_conversion(vs);
1653 static void pixel_format_message (VncState *vs) {
1656 vnc_write_u8(vs, vs->ds->surface->pf.bits_per_pixel); /* bits-per-pixel */
1657 vnc_write_u8(vs, vs->ds->surface->pf.depth); /* depth */
1660 vnc_write_u8(vs, 1); /* big-endian-flag */
1662 vnc_write_u8(vs, 0); /* big-endian-flag */
1664 vnc_write_u8(vs, 1); /* true-color-flag */
1665 vnc_write_u16(vs, vs->ds->surface->pf.rmax); /* red-max */
1666 vnc_write_u16(vs, vs->ds->surface->pf.gmax); /* green-max */
1667 vnc_write_u16(vs, vs->ds->surface->pf.bmax); /* blue-max */
1668 vnc_write_u8(vs, vs->ds->surface->pf.rshift); /* red-shift */
1669 vnc_write_u8(vs, vs->ds->surface->pf.gshift); /* green-shift */
1670 vnc_write_u8(vs, vs->ds->surface->pf.bshift); /* blue-shift */
1671 if (vs->ds->surface->pf.bits_per_pixel == 32)
1672 vs->send_hextile_tile = send_hextile_tile_32;
1673 else if (vs->ds->surface->pf.bits_per_pixel == 16)
1674 vs->send_hextile_tile = send_hextile_tile_16;
1675 else if (vs->ds->surface->pf.bits_per_pixel == 8)
1676 vs->send_hextile_tile = send_hextile_tile_8;
1677 vs->clientds = *(vs->ds->surface);
1678 vs->clientds.flags &= ~QEMU_ALLOCATED_FLAG;
1679 vs->write_pixels = vnc_write_pixels_copy;
1681 vnc_write(vs, pad, 3); /* padding */
1689 static void vnc_colordepth(VncState *vs)
1691 if (vnc_has_feature(vs, VNC_FEATURE_WMVI)) {
1693 vnc_write_u8(vs, 0); /* msg id */
1694 vnc_write_u8(vs, 0);
1695 vnc_write_u16(vs, 1); /* number of rects */
1696 vnc_framebuffer_update(vs, 0, 0, ds_get_width(vs->ds),
1697 ds_get_height(vs->ds), VNC_ENCODING_WMVi);
1698 pixel_format_message(vs);
1699 vnc_flush(vs);
1701 set_pixel_conversion(vs);
1705 static int protocol_client_msg(VncState *vs, uint8_t *data, size_t len)
1715 set_pixel_format(vs, read_u8(data, 4), read_u8(data, 5),
1737 set_encodings(vs, (int32_t *)(data + 4), limit);
1743 framebuffer_update_request(vs,
1751 key_event(vs, read_u8(data, 1), read_u32(data, 4));
1757 pointer_event(vs, read_u8(data, 1), read_u16(data, 2), read_u16(data, 4));
1769 client_cut_text(vs, read_u32(data, 4), data + 8);
1780 ext_key_event(vs, read_u16(data, 2),
1789 audio_add(vs);
1792 audio_del(vs);
1798 case 0: vs->as.fmt = AUD_FMT_U8; break;
1799 case 1: vs->as.fmt = AUD_FMT_S8; break;
1800 case 2: vs->as.fmt = AUD_FMT_U16; break;
1801 case 3: vs->as.fmt = AUD_FMT_S16; break;
1802 case 4: vs->as.fmt = AUD_FMT_U32; break;
1803 case 5: vs->as.fmt = AUD_FMT_S32; break;
1806 vnc_client_error(vs);
1809 vs->as.nchannels = read_u8(data, 5);
1810 if (vs->as.nchannels != 1 && vs->as.nchannels != 2) {
1813 vnc_client_error(vs);
1816 vs->as.freq = read_u32(data, 6);
1820 vnc_client_error(vs);
1827 vnc_client_error(vs);
1833 vnc_client_error(vs);
1837 vnc_read_when(vs, protocol_client_msg, 1);
1841 static int protocol_client_init(VncState *vs, uint8_t *data, size_t len)
1846 vnc_write_u16(vs, ds_get_width(vs->ds));
1847 vnc_write_u16(vs, ds_get_height(vs->ds));
1849 pixel_format_message(vs);
1856 vnc_write_u32(vs, size);
1857 vnc_write(vs, buf, size);
1858 vnc_flush(vs);
1860 vnc_read_when(vs, protocol_client_msg, 1);
1865 void start_client_init(VncState *vs)
1867 vnc_read_when(vs, protocol_client_init, 1);
1870 static void make_challenge(VncState *vs)
1876 for (i = 0 ; i < sizeof(vs->challenge) ; i++)
1877 vs->challenge[i] = (int) (256.0*rand()/(RAND_MAX+1.0));
1880 static int protocol_client_auth_vnc(VncState *vs, uint8_t *data, size_t len)
1886 if (!vs->vd->password || !vs->vd->password[0]) {
1888 vnc_write_u32(vs, 1); /* Reject auth */
1889 if (vs->minor >= 8) {
1891 vnc_write_u32(vs, sizeof(err));
1892 vnc_write(vs, err, sizeof(err));
1894 vnc_flush(vs);
1895 vnc_client_error(vs);
1899 memcpy(response, vs->challenge, VNC_AUTH_CHALLENGE_SIZE);
1902 pwlen = strlen(vs->vd->password);
1904 key[i] = i<pwlen ? vs->vd->password[i] : 0;
1909 /* Compare expected vs actual challenge response */
1912 vnc_write_u32(vs, 1); /* Reject auth */
1913 if (vs->minor >= 8) {
1915 vnc_write_u32(vs, sizeof(err));
1916 vnc_write(vs, err, sizeof(err));
1918 vnc_flush(vs);
1919 vnc_client_error(vs);
1922 vnc_write_u32(vs, 0); /* Accept auth */
1923 vnc_flush(vs);
1925 start_client_init(vs);
1930 void start_auth_vnc(VncState *vs)
1932 make_challenge(vs);
1934 vnc_write(vs, vs->challenge, sizeof(vs->challenge));
1935 vnc_flush(vs);
1937 vnc_read_when(vs, protocol_client_auth_vnc, sizeof(vs->challenge));
1941 static int protocol_client_auth(VncState *vs, uint8_t *data, size_t len)
1945 if (data[0] != vs->vd->auth) { /* Reject auth */
1947 vnc_write_u32(vs, 1);
1948 if (vs->minor >= 8) {
1950 vnc_write_u32(vs, sizeof(err));
1951 vnc_write(vs, err, sizeof(err));
1953 vnc_client_error(vs);
1956 switch (vs->vd->auth) {
1959 if (vs->minor >= 8) {
1960 vnc_write_u32(vs, 0); /* Accept auth completion */
1961 vnc_flush(vs);
1963 start_client_init(vs);
1968 start_auth_vnc(vs);
1974 start_auth_vencrypt(vs);
1981 start_auth_sasl(vs);
1986 VNC_DEBUG("Reject auth %d server code bug\n", vs->vd->auth);
1987 vnc_write_u8(vs, 1);
1988 if (vs->minor >= 8) {
1990 vnc_write_u32(vs, sizeof(err));
1991 vnc_write(vs, err, sizeof(err));
1993 vnc_client_error(vs);
1999 static int protocol_version(VncState *vs, uint8_t *version, size_t len)
2006 if (sscanf(local, "RFB %03d.%03d\n", &vs->major, &vs->minor) != 2) {
2008 vnc_client_error(vs);
2011 VNC_DEBUG("Client request protocol version %d.%d\n", vs->major, vs->minor);
2012 if (vs->major != 3 ||
2013 (vs->minor != 3 &&
2014 vs->minor != 4 &&
2015 vs->minor != 5 &&
2016 vs->minor != 7 &&
2017 vs->minor != 8)) {
2019 vnc_write_u32(vs, VNC_AUTH_INVALID);
2020 vnc_flush(vs);
2021 vnc_client_error(vs);
2027 if (vs->minor == 4 || vs->minor == 5)
2028 vs->minor = 3;
2030 if (vs->minor == 3) {
2031 if (vs->vd->auth == VNC_AUTH_NONE) {
2033 vnc_write_u32(vs, vs->vd->auth);
2034 vnc_flush(vs);
2035 start_client_init(vs);
2036 } else if (vs->vd->auth == VNC_AUTH_VNC) {
2038 vnc_write_u32(vs, vs->vd->auth);
2039 vnc_flush(vs);
2040 start_auth_vnc(vs);
2042 VNC_DEBUG("Unsupported auth %d for protocol 3.3\n", vs->vd->auth);
2043 vnc_write_u32(vs, VNC_AUTH_INVALID);
2044 vnc_flush(vs);
2045 vnc_client_error(vs);
2048 VNC_DEBUG("Telling client we support auth %d\n", vs->vd->auth);
2049 vnc_write_u8(vs, 1); /* num auth */
2050 vnc_write_u8(vs, vs->vd->auth);
2051 vnc_read_when(vs, protocol_client_auth, 1);
2052 vnc_flush(vs);
2060 VncState *vs = qemu_mallocz(sizeof(VncState));
2061 vs->csock = csock;
2065 socket_set_nonblock(vs->csock);
2066 qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
2068 vs->vd = vd;
2069 vs->ds = vd->ds;
2070 vs->timer = qemu_new_timer_ms(rt_clock, vnc_update_client, vs);
2071 vs->last_x = -1;
2072 vs->last_y = -1;
2074 vs->as.freq = 44100;
2075 vs->as.nchannels = 2;
2076 vs->as.fmt = AUD_FMT_S16;
2077 vs->as.endianness = 0;
2079 vnc_resize(vs);
2080 vnc_write(vs, "RFB 003.008\n", 12);
2081 vnc_flush(vs);
2082 vnc_read_when(vs, protocol_version, 12);
2083 reset_keys(vs);
2085 vs->next = vd->clients;
2086 vd->clients = vs;
2088 vnc_update_client(vs);
2089 /* vs might be free()ed here */
2094 VncDisplay *vs = opaque;
2101 int csock = accept(vs->lsock, (struct sockaddr *)&addr, &addrlen);
2103 vnc_connect(vs, csock);
2109 VncDisplay *vs = qemu_mallocz(sizeof(*vs));
2113 ds->opaque = vs;
2115 vnc_display = vs;
2117 vs->lsock = -1;
2119 vs->ds = ds;
2122 vs->kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
2124 vs->kbd_layout = init_keyboard_layout(name2keysym, "en-us");
2126 if (!vs->kbd_layout)
2139 VncDisplay *vs = ds ? (VncDisplay *)ds->opaque : vnc_display;
2141 if (!vs)
2143 if (vs->display) {
2144 qemu_free(vs->display);
2145 vs->display = NULL;
2147 if (vs->lsock != -1) {
2148 qemu_set_fd_handler2(vs->lsock, NULL, NULL, NULL, NULL);
2149 close(vs->lsock);
2150 vs->lsock = -1;
2152 vs->auth = VNC_AUTH_INVALID;
2154 vs->subauth = VNC_AUTH_INVALID;
2155 vs->tls.x509verify = 0;
2161 VncDisplay *vs = ds ? (VncDisplay *)ds->opaque : vnc_display;
2163 if (vs->password) {
2164 qemu_free(vs->password);
2165 vs->password = NULL;
2168 if (!(vs->password = qemu_strdup(password)))
2177 VncDisplay *vs = ds ? (VncDisplay *)ds->opaque : vnc_display;
2179 return vnc_socket_local_addr("%s:%s", vs->lsock);
2184 VncDisplay *vs = ds ? (VncDisplay *)ds->opaque : vnc_display;
2204 if (!(vs->display = strdup(display)))
2227 vs->tls.x509verify = 1; /* ...and verify client certs */
2238 if (vnc_tls_set_x509_creds_dir(vs, path) < 0) {
2241 qemu_free(vs->display);
2242 vs->display = NULL;
2248 qemu_free(vs->display);
2249 vs->display = NULL;
2259 if (acl && x509 && vs->tls.x509verify) {
2260 if (!(vs->tls.acl = qemu_acl_init("vnc.x509dname"))) {
2268 if (!(vs->sasl.acl = qemu_acl_init("vnc.username"))) {
2294 vs->auth = VNC_AUTH_VENCRYPT;
2297 vs->subauth = VNC_AUTH_VENCRYPT_X509VNC;
2300 vs->subauth = VNC_AUTH_VENCRYPT_TLSVNC;
2305 vs->auth = VNC_AUTH_VNC;
2307 vs->subauth = VNC_AUTH_INVALID;
2314 vs->auth = VNC_AUTH_VENCRYPT;
2317 vs->subauth = VNC_AUTH_VENCRYPT_X509SASL;
2320 vs->subauth = VNC_AUTH_VENCRYPT_TLSSASL;
2325 vs->auth = VNC_AUTH_SASL;
2327 vs->subauth = VNC_AUTH_INVALID;
2334 vs->auth = VNC_AUTH_VENCRYPT;
2337 vs->subauth = VNC_AUTH_VENCRYPT_X509NONE;
2340 vs->subauth = VNC_AUTH_VENCRYPT_TLSNONE;
2345 vs->auth = VNC_AUTH_NONE;
2347 vs->subauth = VNC_AUTH_INVALID;
2356 free(vs->display);
2357 vs->display = NULL;
2365 vs->lsock = unix_connect(display+5);
2367 vs->lsock = inet_connect(display, SOCK_STREAM);
2368 if (-1 == vs->lsock) {
2369 free(vs->display);
2370 vs->display = NULL;
2373 int csock = vs->lsock;
2374 vs->lsock = -1;
2375 vnc_connect(vs, csock);
2385 vs->lsock = unix_listen(display+5, dpy+5, 256-5);
2387 vs->lsock = inet_listen(display, dpy, 256, SOCK_STREAM, 5900);
2389 if (-1 == vs->lsock) {
2393 free(vs->display);
2394 vs->display = dpy;
2397 return qemu_set_fd_handler2(vs->lsock, NULL, vnc_listen_read, NULL, vs);