Lines Matching defs:vs

213 static inline uint32_t vnc_has_feature(VncState *vs, int feature) {
214 return (vs->features & (1 << feature));
225 static void vnc_disconnect_start(VncState *vs);
226 static void vnc_disconnect_finish(VncState *vs);
228 static void vnc_colordepth(VncState *vs);
271 static void vnc_update(VncState *vs, int x, int y, int w, int h)
273 struct VncSurface *s = &vs->guest;
298 VncState *vs = vd->clients;
299 while (vs != NULL) {
300 vnc_update(vs, x, y, w, h);
301 vs = vs->next;
305 static void vnc_framebuffer_update(VncState *vs, int x, int y, int w, int h,
308 vnc_write_u16(vs, x);
309 vnc_write_u16(vs, y);
310 vnc_write_u16(vs, w);
311 vnc_write_u16(vs, h);
313 vnc_write_s32(vs, encoding);
349 static void vnc_resize(VncState *vs)
351 DisplayState *ds = vs->ds;
355 if (!vs->guest.ds)
356 vs->guest.ds = qemu_mallocz(sizeof(*vs->guest.ds));
357 if (ds_get_bytes_per_pixel(ds) != vs->guest.ds->pf.bytes_per_pixel)
359 vnc_colordepth(vs);
360 size_changed = ds_get_width(ds) != vs->guest.ds->width ||
361 ds_get_height(ds) != vs->guest.ds->height;
362 *(vs->guest.ds) = *(ds->surface);
364 if (vs->csock != -1 && vnc_has_feature(vs, VNC_FEATURE_RESIZE)) {
365 vnc_write_u8(vs, 0); /* msg id */
366 vnc_write_u8(vs, 0);
367 vnc_write_u16(vs, 1); /* number of rects */
368 vnc_framebuffer_update(vs, 0, 0, ds_get_width(ds), ds_get_height(ds),
370 vnc_flush(vs);
373 memset(vs->guest.dirty, 0xFF, sizeof(vs->guest.dirty));
376 if (!vs->server.ds)
377 vs->server.ds = qemu_mallocz(sizeof(*vs->server.ds));
378 if (vs->server.ds->data)
379 qemu_free(vs->server.ds->data);
380 *(vs->server.ds) = *(ds->surface);
381 vs->server.ds->data = qemu_mallocz(vs->server.ds->linesize *
382 vs->server.ds->height);
383 memset(vs->server.dirty, 0xFF, sizeof(vs->guest.dirty));
389 VncState *vs = vd->clients;
390 while (vs != NULL) {
391 vnc_resize(vs);
392 vs = vs->next;
397 static void vnc_write_pixels_copy(VncState *vs, void *pixels, int size)
399 vnc_write(vs, pixels, size);
403 static void vnc_convert_pixel(VncState *vs, uint8_t *buf, uint32_t v)
407 r = ((((v & vs->server.ds->pf.rmask) >> vs->server.ds->pf.rshift) << vs->clientds.pf.rbits) >>
408 vs->server.ds->pf.rbits);
409 g = ((((v & vs->server.ds->pf.gmask) >> vs->server.ds->pf.gshift) << vs->clientds.pf.gbits) >>
410 vs->server.ds->pf.gbits);
411 b = ((((v & vs->server.ds->pf.bmask) >> vs->server.ds->pf.bshift) << vs->clientds.pf.bbits) >>
412 vs->server.ds->pf.bbits);
413 v = (r << vs->clientds.pf.rshift) |
414 (g << vs->clientds.pf.gshift) |
415 (b << vs->clientds.pf.bshift);
416 switch(vs->clientds.pf.bytes_per_pixel) {
421 if (vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) {
431 if (vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) {
446 static void vnc_write_pixels_generic(VncState *vs, void *pixels1, int size)
450 if (vs->server.ds->pf.bytes_per_pixel == 4) {
455 vnc_convert_pixel(vs, buf, pixels[i]);
456 vnc_write(vs, buf, vs->clientds.pf.bytes_per_pixel);
458 } else if (vs->server.ds->pf.bytes_per_pixel == 2) {
463 vnc_convert_pixel(vs, buf, pixels[i]);
464 vnc_write(vs, buf, vs->clientds.pf.bytes_per_pixel);
466 } else if (vs->server.ds->pf.bytes_per_pixel == 1) {
471 vnc_convert_pixel(vs, buf, pixels[i]);
472 vnc_write(vs, buf, vs->clientds.pf.bytes_per_pixel);
479 static void send_framebuffer_update_raw(VncState *vs, int x, int y, int w, int h)
484 row = vs->server.ds->data + y * ds_get_linesize(vs->ds) + x * ds_get_bytes_per_pixel(vs->ds);
486 vs->write_pixels(vs, row, w * ds_get_bytes_per_pixel(vs->ds));
487 row += ds_get_linesize(vs->ds);
527 static void send_framebuffer_update_hextile(VncState *vs, int x, int y, int w, int h)
533 last_fg = (uint8_t *) qemu_malloc(vs->server.ds->pf.bytes_per_pixel);
534 last_bg = (uint8_t *) qemu_malloc(vs->server.ds->pf.bytes_per_pixel);
538 vs->send_hextile_tile(vs, i, j,
548 static void vnc_zlib_init(VncState *vs)
551 for (i=0; i<(sizeof(vs->zlib_stream) / sizeof(z_stream)); i++)
552 vs->zlib_stream[i].opaque = NULL;
555 static void vnc_zlib_start(VncState *vs)
557 buffer_reset(&vs->zlib);
560 vs->zlib_tmp = vs->output;
561 vs->output = vs->zlib;
564 static int vnc_zlib_stop(VncState *vs, int stream_id)
566 z_streamp zstream = &vs->zlib_stream[stream_id];
570 vs->zlib = vs->output;
571 vs->output = vs->zlib_tmp;
577 if (zstream->opaque != vs) {
581 VNC_DEBUG("VNC: opaque = %p | vs = %p\n", zstream->opaque, vs);
585 err = deflateInit2(zstream, vs->tight_compression, Z_DEFLATED, MAX_WBITS,
593 zstream->opaque = vs;
599 buffer_reserve(&vs->output, vs->zlib.offset + 64);
602 zstream->next_in = vs->zlib.buffer;
603 zstream->avail_in = vs->zlib.offset;
604 zstream->next_out = vs->output.buffer + vs->output.offset;
605 zstream->avail_out = vs->output.capacity - vs->output.offset;
615 vs->output.offset = vs->output.capacity - zstream->avail_out;
619 static void send_framebuffer_update_zlib(VncState *vs, int x, int y, int w, int h)
623 vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_ZLIB);
626 old_offset = vs->output.offset;
627 vnc_write_s32(vs, 0);
630 vnc_zlib_start(vs);
631 send_framebuffer_update_raw(vs, x, y, w, h);
632 bytes_written = vnc_zlib_stop(vs, 0);
638 new_offset = vs->output.offset;
639 vs->output.offset = old_offset;
640 vnc_write_u32(vs, bytes_written);
641 vs->output.offset = new_offset;
644 static void send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
646 switch(vs->vnc_encoding) {
648 send_framebuffer_update_zlib(vs, x, y, w, h);
651 vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_HEXTILE);
652 send_framebuffer_update_hextile(vs, x, y, w, h);
655 vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_RAW);
656 send_framebuffer_update_raw(vs, x, y, w, h);
661 static void vnc_copy(VncState *vs, int src_x, int src_y, int dst_x, int dst_y, int w, int h)
663 vnc_write_u8(vs, 0); /* msg id */
664 vnc_write_u8(vs, 0);
665 vnc_write_u16(vs, 1); /* number of rects */
666 vnc_framebuffer_update(vs, dst_x, dst_y, w, h, VNC_ENCODING_COPYRECT);
667 vnc_write_u16(vs, src_x);
668 vnc_write_u16(vs, src_y);
669 vnc_flush(vs);
675 VncState *vs, *vn;
677 for (vs = vd->clients; vs != NULL; vs = vn) {
678 vn = vs->next;
679 if (vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) {
680 vs->force_update = 1;
681 vnc_update_client(vs);
682 /* vs might be free()ed here */
686 for (vs = vd->clients; vs != NULL; vs = vs->next) {
687 if (vnc_has_feature(vs, VNC_FEATURE_COPYRECT))
688 vnc_copy(vs, src_x, src_y, dst_x, dst_y, w, h);
690 vnc_update(vs, dst_x, dst_y, w, h);
712 VncState *vs = opaque;
713 if (vs->need_update && vs->csock != -1) {
723 if (vs->output.offset && !vs->audio_cap && !vs->force_update) {
725 qemu_mod_timer(vs->timer, qemu_get_clock_ms(rt_clock) + VNC_REFRESH_INTERVAL);
736 vnc_set_bits(width_mask, (ds_get_width(vs->ds) / 16), VNC_DIRTY_WORDS);
737 cmp_bytes = 16 * ds_get_bytes_per_pixel(vs->ds);
738 guest_row = vs->guest.ds->data;
739 server_row = vs->server.ds->data;
740 for (y = 0; y < vs->guest.ds->height; y++) {
741 if (vnc_and_bits(vs->guest.dirty[y], width_mask, VNC_DIRTY_WORDS)) {
749 for (x = 0; x < vs->guest.ds->width;
751 if (!vnc_get_bit(vs->guest.dirty[y], (x / 16)))
753 vnc_clear_bit(vs->guest.dirty[y], (x / 16));
757 vnc_set_bit(vs->server.dirty[y], (x / 16));
761 guest_row += ds_get_linesize(vs->ds);
762 server_row += ds_get_linesize(vs->ds);
765 if (!has_dirty && !vs->audio_cap && !vs->force_update) {
766 qemu_mod_timer(vs->timer, qemu_get_clock_ms(rt_clock) + VNC_REFRESH_INTERVAL);
777 vnc_write_u8(vs, 0); /* msg id */
778 vnc_write_u8(vs, 0);
779 saved_offset = vs->output.offset;
780 vnc_write_u16(vs, 0);
782 for (y = 0; y < vs->server.ds->height; y++) {
785 for (x = 0; x < vs->server.ds->width / 16; x++) {
786 if (vnc_get_bit(vs->server.dirty[y], x)) {
790 vnc_clear_bit(vs->server.dirty[y], x);
793 int h = find_and_clear_dirty_height(&vs->server, y, last_x, x);
794 send_framebuffer_update(vs, last_x * 16, y, (x - last_x) * 16, h);
801 int h = find_and_clear_dirty_height(&vs->server, y, last_x, x);
802 send_framebuffer_update(vs, last_x * 16, y, (x - last_x) * 16, h);
806 vs->output.buffer[saved_offset] = (n_rectangles >> 8) & 0xFF;
807 vs->output.buffer[saved_offset + 1] = n_rectangles & 0xFF;
808 vnc_flush(vs);
809 vs->force_update = 0;
813 if (vs->csock != -1) {
814 qemu_mod_timer(vs->timer, qemu_get_clock_ms(rt_clock) + VNC_REFRESH_INTERVAL);
816 vnc_disconnect_finish(vs);
824 VncState *vs = opaque;
828 vnc_write_u8(vs, 255);
829 vnc_write_u8(vs, 1);
830 vnc_write_u16(vs, 0);
831 vnc_flush(vs);
835 vnc_write_u8(vs, 255);
836 vnc_write_u8(vs, 1);
837 vnc_write_u16(vs, 1);
838 vnc_flush(vs);
849 VncState *vs = opaque;
851 vnc_write_u8(vs, 255);
852 vnc_write_u8(vs, 1);
853 vnc_write_u16(vs, 2);
854 vnc_write_u32(vs, size);
855 vnc_write(vs, buf, size);
856 vnc_flush(vs);
859 static void audio_add(VncState *vs)
864 if (vs->audio_cap) {
873 vs->audio_cap = AUD_add_capture(&vs->as, &ops, vs);
874 if (!vs->audio_cap) {
879 static void audio_del(VncState *vs)
881 if (vs->audio_cap) {
882 AUD_del_capture(vs->audio_cap, vs);
883 vs->audio_cap = NULL;
887 static void vnc_disconnect_start(VncState *vs)
889 if (vs->csock == -1)
891 qemu_set_fd_handler2(vs->csock, NULL, NULL, NULL, NULL);
892 closesocket(vs->csock);
893 vs->csock = -1;
896 static void vnc_disconnect_finish(VncState *vs)
898 qemu_del_timer(vs->timer);
899 qemu_free_timer(vs->timer);
900 if (vs->input.buffer) qemu_free(vs->input.buffer);
901 if (vs->output.buffer) qemu_free(vs->output.buffer);
903 vnc_tls_client_cleanup(vs);
906 vnc_sasl_client_cleanup(vs);
908 audio_del(vs);
911 for (p = vs->vd->clients; p != NULL; p = p->next) {
912 if (p == vs) {
916 vs->vd->clients = p->next;
921 if (!vs->vd->clients)
924 qemu_free(vs->server.ds->data);
925 qemu_free(vs->server.ds);
926 qemu_free(vs->guest.ds);
927 qemu_free(vs);
930 int vnc_client_io_error(VncState *vs, int ret, int last_errno)
948 vnc_disconnect_start(vs);
956 void vnc_client_error(VncState *vs)
959 vnc_disconnect_start(vs);
978 long vnc_client_write_buf(VncState *vs, const uint8_t *data, size_t datalen)
982 if (vs->tls.session) {
983 ret = gnutls_write(vs->tls.session, data, datalen);
993 ret = socket_send(vs->csock, data, datalen);
995 return vnc_client_io_error(vs, ret, socket_error());
1009 static long vnc_client_write_plain(VncState *vs)
1015 vs->output.buffer, vs->output.capacity, vs->output.offset,
1016 vs->sasl.waitWriteSSF);
1018 if (vs->sasl.conn &&
1019 vs->sasl.runSSF &&
1020 vs->sasl.waitWriteSSF) {
1021 ret = vnc_client_write_buf(vs, vs->output.buffer, vs->sasl.waitWriteSSF);
1023 vs->sasl.waitWriteSSF -= ret;
1026 ret = vnc_client_write_buf(vs, vs->output.buffer, vs->output.offset);
1030 memmove(vs->output.buffer, vs->output.buffer + ret, (vs->output.offset - ret));
1031 vs->output.offset -= ret;
1033 if (vs->output.offset == 0) {
1034 qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
1049 VncState *vs = opaque;
1052 if (vs->sasl.conn &&
1053 vs->sasl.runSSF &&
1054 !vs->sasl.waitWriteSSF)
1055 ret = vnc_client_write_sasl(vs);
1058 ret = vnc_client_write_plain(vs);
1061 void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting)
1063 vs->read_handler = func;
1064 vs->read_handler_expect = expecting;
1083 long vnc_client_read_buf(VncState *vs, uint8_t *data, size_t datalen)
1087 if (vs->tls.session) {
1088 ret = gnutls_read(vs->tls.session, data, datalen);
1098 ret = socket_recv(vs->csock, data, datalen);
1100 return vnc_client_io_error(vs, ret, socket_error());
1112 static long vnc_client_read_plain(VncState *vs)
1116 vs->input.buffer, vs->input.capacity, vs->input.offset);
1117 buffer_reserve(&vs->input, 4096);
1118 ret = vnc_client_read_buf(vs, buffer_end(&vs->input), 4096);
1121 vs->input.offset += ret;
1133 VncState *vs = opaque;
1137 if (vs->sasl.conn && vs->sasl.runSSF)
1138 ret = vnc_client_read_sasl(vs);
1141 ret = vnc_client_read_plain(vs);
1143 if (vs->csock == -1)
1144 vnc_disconnect_finish(vs);
1148 while (vs->read_handler && vs->input.offset >= vs->read_handler_expect) {
1149 size_t len = vs->read_handler_expect;
1152 ret = vs->read_handler(vs, vs->input.buffer, len);
1153 if (vs->csock == -1) {
1154 vnc_disconnect_finish(vs);
1159 memmove(vs->input.buffer, vs->input.buffer + len, (vs->input.offset - len));
1160 vs->input.offset -= len;
1162 vs->read_handler_expect = ret;
1167 void vnc_write(VncState *vs, const void *data, size_t len)
1169 buffer_reserve(&vs->output, len);
1171 if (vs->csock != -1 && buffer_empty(&vs->output)) {
1172 qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, vnc_client_write, vs);
1175 buffer_append(&vs->output, data, len);
1178 void vnc_write_s32(VncState *vs, int32_t value)
1180 vnc_write_u32(vs, *(uint32_t *)&value);
1183 void vnc_write_u32(VncState *vs, uint32_t value)
1192 vnc_write(vs, buf, 4);
1195 void vnc_write_u16(VncState *vs, uint16_t value)
1202 vnc_write(vs, buf, 2);
1205 void vnc_write_u8(VncState *vs, uint8_t value)
1207 vnc_write(vs, (char *)&value, 1);
1210 void vnc_flush(VncState *vs)
1212 if (vs->csock != -1 && vs->output.offset)
1213 vnc_client_write(vs);
1238 static void client_cut_text(VncState *vs, size_t len, uint8_t *text)
1242 static void check_pointer_type_change(VncState *vs, int absolute)
1244 if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE) && vs->absolute != absolute) {
1245 vnc_write_u8(vs, 0);
1246 vnc_write_u8(vs, 0);
1247 vnc_write_u16(vs, 1);
1248 vnc_framebuffer_update(vs, absolute, 0,
1249 ds_get_width(vs->ds), ds_get_height(vs->ds),
1251 vnc_flush(vs);
1253 vs->absolute = absolute;
1256 static void pointer_event(VncState *vs, int button_mask, int x, int y)
1272 if (vs->absolute) {
1273 kbd_mouse_event(x * 0x7FFF / (ds_get_width(vs->ds) - 1),
1274 y * 0x7FFF / (ds_get_height(vs->ds) - 1),
1276 } else if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE)) {
1282 if (vs->last_x != -1)
1283 kbd_mouse_event(x - vs->last_x,
1284 y - vs->last_y,
1286 vs->last_x = x;
1287 vs->last_y = y;
1290 check_pointer_type_change(vs, kbd_mouse_is_absolute());
1293 static void reset_keys(VncState *vs)
1297 if (vs->modifiers_state[i]) {
1301 vs->modifiers_state[i] = 0;
1306 static void press_key(VncState *vs, int keysym)
1308 kbd_put_keycode(keysym2scancode(vs->vd->kbd_layout, keysym) & 0x7f);
1309 kbd_put_keycode(keysym2scancode(vs->vd->kbd_layout, keysym) | 0x80);
1312 static void do_key_event(VncState *vs, int down, int keycode, int sym)
1323 vs->modifiers_state[keycode] = 1;
1325 vs->modifiers_state[keycode] = 0;
1328 if (down && vs->modifiers_state[0x1d] && vs->modifiers_state[0x38]) {
1330 reset_keys(vs);
1338 vs->modifiers_state[keycode] ^= 1;
1342 if (keycode_is_keypad(vs->vd->kbd_layout, keycode)) {
1347 if (keysym_is_numlock(vs->vd->kbd_layout, sym & 0xFFFF)) {
1348 if (!vs->modifiers_state[0x45]) {
1349 vs->modifiers_state[0x45] = 1;
1350 press_key(vs, 0xff7f);
1353 if (vs->modifiers_state[0x45]) {
1354 vs->modifiers_state[0x45] = 0;
1355 press_key(vs, 0xff7f);
1370 int numlock = vs->modifiers_state[0x45];
1465 static void key_event(VncState *vs, int down, uint32_t sym)
1472 keycode = keysym2scancode(vs->vd->kbd_layout, sym & 0xFFFF);
1473 do_key_event(vs, down, keycode, sym);
1476 static void ext_key_event(VncState *vs, int down,
1481 key_event(vs, down, sym);
1483 do_key_event(vs, down, keycode, sym);
1486 static void framebuffer_update_request(VncState *vs, int incremental,
1490 if (x_position > ds_get_width(vs->ds))
1491 x_position = ds_get_width(vs->ds);
1492 if (y_position > ds_get_height(vs->ds))
1493 y_position = ds_get_height(vs->ds);
1494 if (x_position + w >= ds_get_width(vs->ds))
1495 w = ds_get_width(vs->ds) - x_position;
1496 if (y_position + h >= ds_get_height(vs->ds))
1497 h = ds_get_height(vs->ds) - y_position;
1500 vs->need_update = 1;
1502 vs->force_update = 1;
1504 vnc_set_bits(vs->guest.dirty[y_position + i],
1505 (ds_get_width(vs->ds) / 16), VNC_DIRTY_WORDS);
1506 vnc_set_bits(vs->server.dirty[y_position + i],
1507 (ds_get_width(vs->ds) / 16), VNC_DIRTY_WORDS);
1512 static void send_ext_key_event_ack(VncState *vs)
1514 vnc_write_u8(vs, 0);
1515 vnc_write_u8(vs, 0);
1516 vnc_write_u16(vs, 1);
1517 vnc_framebuffer_update(vs, 0, 0, ds_get_width(vs->ds), ds_get_height(vs->ds),
1519 vnc_flush(vs);
1522 static void send_ext_audio_ack(VncState *vs)
1524 vnc_write_u8(vs, 0);
1525 vnc_write_u8(vs, 0);
1526 vnc_write_u16(vs, 1);
1527 vnc_framebuffer_update(vs, 0, 0, ds_get_width(vs->ds), ds_get_height(vs->ds),
1529 vnc_flush(vs);
1532 static void set_encodings(VncState *vs, int32_t *encodings, size_t n_encodings)
1537 vnc_zlib_init(vs);
1538 vs->features = 0;
1539 vs->vnc_encoding = 0;
1540 vs->tight_compression = 9;
1541 vs->tight_quality = 9;
1542 vs->absolute = -1;
1548 vs->vnc_encoding = enc;
1551 vs->features |= VNC_FEATURE_COPYRECT_MASK;
1554 vs->features |= VNC_FEATURE_HEXTILE_MASK;
1555 vs->vnc_encoding = enc;
1558 vs->features |= VNC_FEATURE_ZLIB_MASK;
1559 vs->vnc_encoding = enc;
1562 vs->features |= VNC_FEATURE_RESIZE_MASK;
1565 vs->features |= VNC_FEATURE_POINTER_TYPE_CHANGE_MASK;
1568 send_ext_key_event_ack(vs);
1571 send_ext_audio_ack(vs);
1574 vs->features |= VNC_FEATURE_WMVI_MASK;
1577 vs->tight_compression = (enc & 0x0F);
1580 vs->tight_quality = (enc & 0x0F);
1588 check_pointer_type_change(vs, kbd_mouse_is_absolute());
1591 static void set_pixel_conversion(VncState *vs)
1593 if ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) ==
1594 (vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG) &&
1595 !memcmp(&(vs->clientds.pf), &(vs->ds->surface->pf), sizeof(PixelFormat))) {
1596 vs->write_pixels = vnc_write_pixels_copy;
1597 switch (vs->ds->surface->pf.bits_per_pixel) {
1599 vs->send_hextile_tile = send_hextile_tile_8;
1602 vs->send_hextile_tile = send_hextile_tile_16;
1605 vs->send_hextile_tile = send_hextile_tile_32;
1609 vs->write_pixels = vnc_write_pixels_generic;
1610 switch (vs->ds->surface->pf.bits_per_pixel) {
1612 vs->send_hextile_tile = send_hextile_tile_generic_8;
1615 vs->send_hextile_tile = send_hextile_tile_generic_16;
1618 vs->send_hextile_tile = send_hextile_tile_generic_32;
1624 static void set_pixel_format(VncState *vs,
1631 vnc_client_error(vs);
1635 vs->clientds = *(vs->guest.ds);
1636 vs->clientds.pf.rmax = red_max;
1637 count_bits(vs->clientds.pf.rbits, red_max);
1638 vs->clientds.pf.rshift = red_shift;
1639 vs->clientds.pf.rmask = red_max << red_shift;
1640 vs->clientds.pf.gmax = green_max;
1641 count_bits(vs->clientds.pf.gbits, green_max);
1642 vs->clientds.pf.gshift = green_shift;
1643 vs->clientds.pf.gmask = green_max << green_shift;
1644 vs->clientds.pf.bmax = blue_max;
1645 count_bits(vs->clientds.pf.bbits, blue_max);
1646 vs->clientds.pf.bshift = blue_shift;
1647 vs->clientds.pf.bmask = blue_max << blue_shift;
1648 vs->clientds.pf.bits_per_pixel = bits_per_pixel;
1649 vs->clientds.pf.bytes_per_pixel = bits_per_pixel / 8;
1650 vs->clientds.pf.depth = bits_per_pixel == 32 ? 24 : bits_per_pixel;
1651 vs->clientds.flags = big_endian_flag ? QEMU_BIG_ENDIAN_FLAG : 0x00;
1653 set_pixel_conversion(vs);
1659 static void pixel_format_message (VncState *vs) {
1662 vnc_write_u8(vs, vs->ds->surface->pf.bits_per_pixel); /* bits-per-pixel */
1663 vnc_write_u8(vs, vs->ds->surface->pf.depth); /* depth */
1666 vnc_write_u8(vs, 1); /* big-endian-flag */
1668 vnc_write_u8(vs, 0); /* big-endian-flag */
1670 vnc_write_u8(vs, 1); /* true-color-flag */
1671 vnc_write_u16(vs, vs->ds->surface->pf.rmax); /* red-max */
1672 vnc_write_u16(vs, vs->ds->surface->pf.gmax); /* green-max */
1673 vnc_write_u16(vs, vs->ds->surface->pf.bmax); /* blue-max */
1674 vnc_write_u8(vs, vs->ds->surface->pf.rshift); /* red-shift */
1675 vnc_write_u8(vs, vs->ds->surface->pf.gshift); /* green-shift */
1676 vnc_write_u8(vs, vs->ds->surface->pf.bshift); /* blue-shift */
1677 if (vs->ds->surface->pf.bits_per_pixel == 32)
1678 vs->send_hextile_tile = send_hextile_tile_32;
1679 else if (vs->ds->surface->pf.bits_per_pixel == 16)
1680 vs->send_hextile_tile = send_hextile_tile_16;
1681 else if (vs->ds->surface->pf.bits_per_pixel == 8)
1682 vs->send_hextile_tile = send_hextile_tile_8;
1683 vs->clientds = *(vs->ds->surface);
1684 vs->clientds.flags &= ~QEMU_ALLOCATED_FLAG;
1685 vs->write_pixels = vnc_write_pixels_copy;
1687 vnc_write(vs, pad, 3); /* padding */
1695 static void vnc_colordepth(VncState *vs)
1697 if (vnc_has_feature(vs, VNC_FEATURE_WMVI)) {
1699 vnc_write_u8(vs, 0); /* msg id */
1700 vnc_write_u8(vs, 0);
1701 vnc_write_u16(vs, 1); /* number of rects */
1702 vnc_framebuffer_update(vs, 0, 0, ds_get_width(vs->ds),
1703 ds_get_height(vs->ds), VNC_ENCODING_WMVi);
1704 pixel_format_message(vs);
1705 vnc_flush(vs);
1707 set_pixel_conversion(vs);
1711 static int protocol_client_msg(VncState *vs, uint8_t *data, size_t len)
1721 set_pixel_format(vs, read_u8(data, 4), read_u8(data, 5),
1743 set_encodings(vs, (int32_t *)(data + 4), limit);
1749 framebuffer_update_request(vs,
1757 key_event(vs, read_u8(data, 1), read_u32(data, 4));
1763 pointer_event(vs, read_u8(data, 1), read_u16(data, 2), read_u16(data, 4));
1775 client_cut_text(vs, read_u32(data, 4), data + 8);
1786 ext_key_event(vs, read_u16(data, 2),
1795 audio_add(vs);
1798 audio_del(vs);
1804 case 0: vs->as.fmt = AUD_FMT_U8; break;
1805 case 1: vs->as.fmt = AUD_FMT_S8; break;
1806 case 2: vs->as.fmt = AUD_FMT_U16; break;
1807 case 3: vs->as.fmt = AUD_FMT_S16; break;
1808 case 4: vs->as.fmt = AUD_FMT_U32; break;
1809 case 5: vs->as.fmt = AUD_FMT_S32; break;
1812 vnc_client_error(vs);
1815 vs->as.nchannels = read_u8(data, 5);
1816 if (vs->as.nchannels != 1 && vs->as.nchannels != 2) {
1819 vnc_client_error(vs);
1822 vs->as.freq = read_u32(data, 6);
1826 vnc_client_error(vs);
1833 vnc_client_error(vs);
1839 vnc_client_error(vs);
1843 vnc_read_when(vs, protocol_client_msg, 1);
1847 static int protocol_client_init(VncState *vs, uint8_t *data, size_t len)
1852 vnc_write_u16(vs, ds_get_width(vs->ds));
1853 vnc_write_u16(vs, ds_get_height(vs->ds));
1855 pixel_format_message(vs);
1862 vnc_write_u32(vs, size);
1863 vnc_write(vs, buf, size);
1864 vnc_flush(vs);
1866 vnc_read_when(vs, protocol_client_msg, 1);
1871 void start_client_init(VncState *vs)
1873 vnc_read_when(vs, protocol_client_init, 1);
1876 static void make_challenge(VncState *vs)
1882 for (i = 0 ; i < sizeof(vs->challenge) ; i++)
1883 vs->challenge[i] = (int) (256.0*rand()/(RAND_MAX+1.0));
1886 static int protocol_client_auth_vnc(VncState *vs, uint8_t *data, size_t len)
1892 if (!vs->vd->password || !vs->vd->password[0]) {
1894 vnc_write_u32(vs, 1); /* Reject auth */
1895 if (vs->minor >= 8) {
1897 vnc_write_u32(vs, sizeof(err));
1898 vnc_write(vs, err, sizeof(err));
1900 vnc_flush(vs);
1901 vnc_client_error(vs);
1905 memcpy(response, vs->challenge, VNC_AUTH_CHALLENGE_SIZE);
1908 pwlen = strlen(vs->vd->password);
1910 key[i] = i<pwlen ? vs->vd->password[i] : 0;
1915 /* Compare expected vs actual challenge response */
1918 vnc_write_u32(vs, 1); /* Reject auth */
1919 if (vs->minor >= 8) {
1921 vnc_write_u32(vs, sizeof(err));
1922 vnc_write(vs, err, sizeof(err));
1924 vnc_flush(vs);
1925 vnc_client_error(vs);
1928 vnc_write_u32(vs, 0); /* Accept auth */
1929 vnc_flush(vs);
1931 start_client_init(vs);
1936 void start_auth_vnc(VncState *vs)
1938 make_challenge(vs);
1940 vnc_write(vs, vs->challenge, sizeof(vs->challenge));
1941 vnc_flush(vs);
1943 vnc_read_when(vs, protocol_client_auth_vnc, sizeof(vs->challenge));
1947 static int protocol_client_auth(VncState *vs, uint8_t *data, size_t len)
1951 if (data[0] != vs->vd->auth) { /* Reject auth */
1953 vnc_write_u32(vs, 1);
1954 if (vs->minor >= 8) {
1956 vnc_write_u32(vs, sizeof(err));
1957 vnc_write(vs, err, sizeof(err));
1959 vnc_client_error(vs);
1962 switch (vs->vd->auth) {
1965 if (vs->minor >= 8) {
1966 vnc_write_u32(vs, 0); /* Accept auth completion */
1967 vnc_flush(vs);
1969 start_client_init(vs);
1974 start_auth_vnc(vs);
1980 start_auth_vencrypt(vs);
1987 start_auth_sasl(vs);
1992 VNC_DEBUG("Reject auth %d server code bug\n", vs->vd->auth);
1993 vnc_write_u8(vs, 1);
1994 if (vs->minor >= 8) {
1996 vnc_write_u32(vs, sizeof(err));
1997 vnc_write(vs, err, sizeof(err));
1999 vnc_client_error(vs);
2005 static int protocol_version(VncState *vs, uint8_t *version, size_t len)
2012 if (sscanf(local, "RFB %03d.%03d\n", &vs->major, &vs->minor) != 2) {
2014 vnc_client_error(vs);
2017 VNC_DEBUG("Client request protocol version %d.%d\n", vs->major, vs->minor);
2018 if (vs->major != 3 ||
2019 (vs->minor != 3 &&
2020 vs->minor != 4 &&
2021 vs->minor != 5 &&
2022 vs->minor != 7 &&
2023 vs->minor != 8)) {
2025 vnc_write_u32(vs, VNC_AUTH_INVALID);
2026 vnc_flush(vs);
2027 vnc_client_error(vs);
2033 if (vs->minor == 4 || vs->minor == 5)
2034 vs->minor = 3;
2036 if (vs->minor == 3) {
2037 if (vs->vd->auth == VNC_AUTH_NONE) {
2039 vnc_write_u32(vs, vs->vd->auth);
2040 vnc_flush(vs);
2041 start_client_init(vs);
2042 } else if (vs->vd->auth == VNC_AUTH_VNC) {
2044 vnc_write_u32(vs, vs->vd->auth);
2045 vnc_flush(vs);
2046 start_auth_vnc(vs);
2048 VNC_DEBUG("Unsupported auth %d for protocol 3.3\n", vs->vd->auth);
2049 vnc_write_u32(vs, VNC_AUTH_INVALID);
2050 vnc_flush(vs);
2051 vnc_client_error(vs);
2054 VNC_DEBUG("Telling client we support auth %d\n", vs->vd->auth);
2055 vnc_write_u8(vs, 1); /* num auth */
2056 vnc_write_u8(vs, vs->vd->auth);
2057 vnc_read_when(vs, protocol_client_auth, 1);
2058 vnc_flush(vs);
2066 VncState *vs = qemu_mallocz(sizeof(VncState));
2067 vs->csock = csock;
2071 socket_set_nonblock(vs->csock);
2072 qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
2074 vs->vd = vd;
2075 vs->ds = vd->ds;
2076 vs->timer = qemu_new_timer_ms(rt_clock, vnc_update_client, vs);
2077 vs->last_x = -1;
2078 vs->last_y = -1;
2080 vs->as.freq = 44100;
2081 vs->as.nchannels = 2;
2082 vs->as.fmt = AUD_FMT_S16;
2083 vs->as.endianness = 0;
2085 vnc_resize(vs);
2086 vnc_write(vs, "RFB 003.008\n", 12);
2087 vnc_flush(vs);
2088 vnc_read_when(vs, protocol_version, 12);
2089 reset_keys(vs);
2091 vs->next = vd->clients;
2092 vd->clients = vs;
2094 vnc_update_client(vs);
2095 /* vs might be free()ed here */
2100 VncDisplay *vs = opaque;
2105 int csock = socket_accept(vs->lsock, NULL);
2107 vnc_connect(vs, csock);
2113 VncDisplay *vs = qemu_mallocz(sizeof(*vs));
2117 ds->opaque = vs;
2119 vnc_display = vs;
2121 vs->lsock = -1;
2123 vs->ds = ds;
2126 vs->kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
2128 vs->kbd_layout = init_keyboard_layout(name2keysym, "en-us");
2130 if (!vs->kbd_layout)
2143 VncDisplay *vs = ds ? (VncDisplay *)ds->opaque : vnc_display;
2145 if (!vs)
2147 if (vs->display) {
2148 qemu_free(vs->display);
2149 vs->display = NULL;
2151 if (vs->lsock != -1) {
2152 qemu_set_fd_handler2(vs->lsock, NULL, NULL, NULL, NULL);
2153 close(vs->lsock);
2154 vs->lsock = -1;
2156 vs->auth = VNC_AUTH_INVALID;
2158 vs->subauth = VNC_AUTH_INVALID;
2159 vs->tls.x509verify = 0;
2165 VncDisplay *vs = ds ? (VncDisplay *)ds->opaque : vnc_display;
2167 if (vs->password) {
2168 qemu_free(vs->password);
2169 vs->password = NULL;
2172 if (!(vs->password = qemu_strdup(password)))
2181 VncDisplay *vs = ds ? (VncDisplay *)ds->opaque : vnc_display;
2183 return vnc_socket_local_addr("%s:%s", vs->lsock);
2188 VncDisplay *vs = ds ? (VncDisplay *)ds->opaque : vnc_display;
2208 if (!(vs->display = strdup(display)))
2231 vs->tls.x509verify = 1; /* ...and verify client certs */
2242 if (vnc_tls_set_x509_creds_dir(vs, path) < 0) {
2245 qemu_free(vs->display);
2246 vs->display = NULL;
2252 qemu_free(vs->display);
2253 vs->display = NULL;
2263 if (acl && x509 && vs->tls.x509verify) {
2264 if (!(vs->tls.acl = qemu_acl_init("vnc.x509dname"))) {
2272 if (!(vs->sasl.acl = qemu_acl_init("vnc.username"))) {
2298 vs->auth = VNC_AUTH_VENCRYPT;
2301 vs->subauth = VNC_AUTH_VENCRYPT_X509VNC;
2304 vs->subauth = VNC_AUTH_VENCRYPT_TLSVNC;
2309 vs->auth = VNC_AUTH_VNC;
2311 vs->subauth = VNC_AUTH_INVALID;
2318 vs->auth = VNC_AUTH_VENCRYPT;
2321 vs->subauth = VNC_AUTH_VENCRYPT_X509SASL;
2324 vs->subauth = VNC_AUTH_VENCRYPT_TLSSASL;
2329 vs->auth = VNC_AUTH_SASL;
2331 vs->subauth = VNC_AUTH_INVALID;
2338 vs->auth = VNC_AUTH_VENCRYPT;
2341 vs->subauth = VNC_AUTH_VENCRYPT_X509NONE;
2344 vs->subauth = VNC_AUTH_VENCRYPT_TLSNONE;
2349 vs->auth = VNC_AUTH_NONE;
2351 vs->subauth = VNC_AUTH_INVALID;
2360 free(vs->display);
2361 vs->display = NULL;
2369 vs->lsock = unix_connect(display+5);
2371 vs->lsock = inet_connect(display, SOCKET_STREAM);
2372 if (-1 == vs->lsock) {
2373 free(vs->display);
2374 vs->display = NULL;
2377 int csock = vs->lsock;
2378 vs->lsock = -1;
2379 vnc_connect(vs, csock);
2389 vs->lsock = unix_listen(display+5, dpy+5, 256-5);
2391 vs->lsock = inet_listen(display, dpy, 256, SOCKET_STREAM, 5900);
2393 if (-1 == vs->lsock) {
2397 free(vs->display);
2398 vs->display = dpy;
2401 return qemu_set_fd_handler2(vs->lsock, NULL, vnc_listen_read, NULL, vs);