?? vnc.c
字號:
/* * QEMU VNC display driver * * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws> * Copyright (C) 2006 Fabrice Bellard * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */#include "qemu-common.h"#include "console.h"#include "sysemu.h"#include "qemu_socket.h"#include "qemu-timer.h"#define VNC_REFRESH_INTERVAL (1000 / 30)#include "vnc_keysym.h"#include "keymaps.c"#include "d3des.h"#if CONFIG_VNC_TLS#include <gnutls/gnutls.h>#include <gnutls/x509.h>#endif /* CONFIG_VNC_TLS */// #define _VNC_DEBUG 1#if _VNC_DEBUG#define VNC_DEBUG(fmt, ...) do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)#if CONFIG_VNC_TLS && _VNC_DEBUG >= 2/* Very verbose, so only enabled for _VNC_DEBUG >= 2 */static void vnc_debug_gnutls_log(int level, const char* str) { VNC_DEBUG("%d %s", level, str);}#endif /* CONFIG_VNC_TLS && _VNC_DEBUG */#else#define VNC_DEBUG(fmt, ...) do { } while (0)#endiftypedef struct Buffer{ size_t capacity; size_t offset; uint8_t *buffer;} Buffer;typedef struct VncState VncState;typedef int VncReadEvent(VncState *vs, uint8_t *data, size_t len);typedef void VncWritePixels(VncState *vs, void *data, int size);typedef void VncSendHextileTile(VncState *vs, int x, int y, int w, int h, uint32_t *last_bg, uint32_t *last_fg, int *has_bg, int *has_fg);#define VNC_MAX_WIDTH 2048#define VNC_MAX_HEIGHT 2048#define VNC_DIRTY_WORDS (VNC_MAX_WIDTH / (16 * 32))#define VNC_AUTH_CHALLENGE_SIZE 16enum { VNC_AUTH_INVALID = 0, VNC_AUTH_NONE = 1, VNC_AUTH_VNC = 2, VNC_AUTH_RA2 = 5, VNC_AUTH_RA2NE = 6, VNC_AUTH_TIGHT = 16, VNC_AUTH_ULTRA = 17, VNC_AUTH_TLS = 18, VNC_AUTH_VENCRYPT = 19};#if CONFIG_VNC_TLSenum { VNC_WIREMODE_CLEAR, VNC_WIREMODE_TLS,};enum { VNC_AUTH_VENCRYPT_PLAIN = 256, VNC_AUTH_VENCRYPT_TLSNONE = 257, VNC_AUTH_VENCRYPT_TLSVNC = 258, VNC_AUTH_VENCRYPT_TLSPLAIN = 259, VNC_AUTH_VENCRYPT_X509NONE = 260, VNC_AUTH_VENCRYPT_X509VNC = 261, VNC_AUTH_VENCRYPT_X509PLAIN = 262,};#if CONFIG_VNC_TLS#define X509_CA_CERT_FILE "ca-cert.pem"#define X509_CA_CRL_FILE "ca-crl.pem"#define X509_SERVER_KEY_FILE "server-key.pem"#define X509_SERVER_CERT_FILE "server-cert.pem"#endif#endif /* CONFIG_VNC_TLS */struct VncState{ QEMUTimer *timer; int lsock; int csock; DisplayState *ds; int need_update; int width; int height; uint32_t dirty_row[VNC_MAX_HEIGHT][VNC_DIRTY_WORDS]; char *old_data; int depth; /* internal VNC frame buffer byte per pixel */ int has_resize; int has_hextile; int has_pointer_type_change; int absolute; int last_x; int last_y; int major; int minor; char *display; char *password; int auth;#if CONFIG_VNC_TLS int subauth; int x509verify; char *x509cacert; char *x509cacrl; char *x509cert; char *x509key;#endif char challenge[VNC_AUTH_CHALLENGE_SIZE];#if CONFIG_VNC_TLS int wiremode; gnutls_session_t tls_session;#endif Buffer output; Buffer input; kbd_layout_t *kbd_layout; /* current output mode information */ VncWritePixels *write_pixels; VncSendHextileTile *send_hextile_tile; int pix_bpp, pix_big_endian; int red_shift, red_max, red_shift1; int green_shift, green_max, green_shift1; int blue_shift, blue_max, blue_shift1; VncReadEvent *read_handler; size_t read_handler_expect; /* input */ uint8_t modifiers_state[256];};static VncState *vnc_state; /* needed for info vnc */void do_info_vnc(void){ if (vnc_state == NULL) term_printf("VNC server disabled\n"); else { term_printf("VNC server active on: "); term_print_filename(vnc_state->display); term_printf("\n"); if (vnc_state->csock == -1) term_printf("No client connected\n"); else term_printf("Client connected\n"); }}/* TODO 1) Get the queue working for IO. 2) there is some weirdness when using the -S option (the screen is grey and not totally invalidated 3) resolutions > 1024*/static void vnc_write(VncState *vs, const void *data, size_t len);static void vnc_write_u32(VncState *vs, uint32_t value);static void vnc_write_s32(VncState *vs, int32_t value);static void vnc_write_u16(VncState *vs, uint16_t value);static void vnc_write_u8(VncState *vs, uint8_t value);static void vnc_flush(VncState *vs);static void vnc_update_client(void *opaque);static void vnc_client_read(void *opaque);static inline void vnc_set_bit(uint32_t *d, int k){ d[k >> 5] |= 1 << (k & 0x1f);}static inline void vnc_clear_bit(uint32_t *d, int k){ d[k >> 5] &= ~(1 << (k & 0x1f));}static inline void vnc_set_bits(uint32_t *d, int n, int nb_words){ int j; j = 0; while (n >= 32) { d[j++] = -1; n -= 32; } if (n > 0) d[j++] = (1 << n) - 1; while (j < nb_words) d[j++] = 0;}static inline int vnc_get_bit(const uint32_t *d, int k){ return (d[k >> 5] >> (k & 0x1f)) & 1;}static inline int vnc_and_bits(const uint32_t *d1, const uint32_t *d2, int nb_words){ int i; for(i = 0; i < nb_words; i++) { if ((d1[i] & d2[i]) != 0) return 1; } return 0;}static void vnc_dpy_update(DisplayState *ds, int x, int y, int w, int h){ VncState *vs = ds->opaque; int i; h += y; /* round x down to ensure the loop only spans one 16-pixel block per, iteration. otherwise, if (x % 16) != 0, the last iteration may span two 16-pixel blocks but we only mark the first as dirty */ w += (x % 16); x -= (x % 16); for (; y < h; y++) for (i = 0; i < w; i += 16) vnc_set_bit(vs->dirty_row[y], (x + i) / 16);}static void vnc_framebuffer_update(VncState *vs, int x, int y, int w, int h, int32_t encoding){ vnc_write_u16(vs, x); vnc_write_u16(vs, y); vnc_write_u16(vs, w); vnc_write_u16(vs, h); vnc_write_s32(vs, encoding);}static void vnc_dpy_resize(DisplayState *ds, int w, int h){ int size_changed; VncState *vs = ds->opaque; ds->data = realloc(ds->data, w * h * vs->depth); vs->old_data = realloc(vs->old_data, w * h * vs->depth); if (ds->data == NULL || vs->old_data == NULL) { fprintf(stderr, "vnc: memory allocation failed\n"); exit(1); } if (ds->depth != vs->depth * 8) { ds->depth = vs->depth * 8; console_color_init(ds); } size_changed = ds->width != w || ds->height != h; ds->width = w; ds->height = h; ds->linesize = w * vs->depth; if (vs->csock != -1 && vs->has_resize && size_changed) { vnc_write_u8(vs, 0); /* msg id */ vnc_write_u8(vs, 0); vnc_write_u16(vs, 1); /* number of rects */ vnc_framebuffer_update(vs, 0, 0, ds->width, ds->height, -223); vnc_flush(vs); vs->width = ds->width; vs->height = ds->height; }}/* fastest code */static void vnc_write_pixels_copy(VncState *vs, void *pixels, int size){ vnc_write(vs, pixels, size);}/* slowest but generic code. */static void vnc_convert_pixel(VncState *vs, uint8_t *buf, uint32_t v){ unsigned int r, g, b; r = (v >> vs->red_shift1) & vs->red_max; g = (v >> vs->green_shift1) & vs->green_max; b = (v >> vs->blue_shift1) & vs->blue_max; v = (r << vs->red_shift) | (g << vs->green_shift) | (b << vs->blue_shift); switch(vs->pix_bpp) { case 1: buf[0] = v; break; case 2: if (vs->pix_big_endian) { buf[0] = v >> 8; buf[1] = v; } else { buf[1] = v >> 8; buf[0] = v; } break; default: case 4: if (vs->pix_big_endian) { buf[0] = v >> 24; buf[1] = v >> 16; buf[2] = v >> 8; buf[3] = v; } else { buf[3] = v >> 24; buf[2] = v >> 16; buf[1] = v >> 8; buf[0] = v; } break; }}static void vnc_write_pixels_generic(VncState *vs, void *pixels1, int size){ uint32_t *pixels = pixels1; uint8_t buf[4]; int n, i; n = size >> 2; for(i = 0; i < n; i++) { vnc_convert_pixel(vs, buf, pixels[i]); vnc_write(vs, buf, vs->pix_bpp); }}static void send_framebuffer_update_raw(VncState *vs, int x, int y, int w, int h){ int i; uint8_t *row; vnc_framebuffer_update(vs, x, y, w, h, 0); row = vs->ds->data + y * vs->ds->linesize + x * vs->depth; for (i = 0; i < h; i++) { vs->write_pixels(vs, row, w * vs->depth); row += vs->ds->linesize; }}static void hextile_enc_cord(uint8_t *ptr, int x, int y, int w, int h){ ptr[0] = ((x & 0x0F) << 4) | (y & 0x0F); ptr[1] = (((w - 1) & 0x0F) << 4) | ((h - 1) & 0x0F);}#define BPP 8#include "vnchextile.h"#undef BPP#define BPP 16#include "vnchextile.h"#undef BPP#define BPP 32#include "vnchextile.h"#undef BPP#define GENERIC#define BPP 32#include "vnchextile.h"#undef BPP#undef GENERICstatic void send_framebuffer_update_hextile(VncState *vs, int x, int y, int w, int h){ int i, j; int has_fg, has_bg; uint32_t last_fg32, last_bg32; vnc_framebuffer_update(vs, x, y, w, h, 5); has_fg = has_bg = 0; for (j = y; j < (y + h); j += 16) { for (i = x; i < (x + w); i += 16) { vs->send_hextile_tile(vs, i, j, MIN(16, x + w - i), MIN(16, y + h - j), &last_bg32, &last_fg32, &has_bg, &has_fg); } }}static void send_framebuffer_update(VncState *vs, int x, int y, int w, int h){ if (vs->has_hextile) send_framebuffer_update_hextile(vs, x, y, w, h); else send_framebuffer_update_raw(vs, x, y, w, h);}static void vnc_copy(DisplayState *ds, int src_x, int src_y, int dst_x, int dst_y, int w, int h){ int src, dst; uint8_t *src_row; uint8_t *dst_row; char *old_row; int y = 0; int pitch = ds->linesize; VncState *vs = ds->opaque; vnc_update_client(vs); if (dst_y > src_y) { y = h - 1; pitch = -pitch; } src = (ds->linesize * (src_y + y) + vs->depth * src_x); dst = (ds->linesize * (dst_y + y) + vs->depth * dst_x); src_row = ds->data + src; dst_row = ds->data + dst; old_row = vs->old_data + dst; for (y = 0; y < h; y++) { memmove(old_row, src_row, w * vs->depth); memmove(dst_row, src_row, w * vs->depth); src_row += pitch; dst_row += pitch; old_row += pitch; } vnc_write_u8(vs, 0); /* msg id */ vnc_write_u8(vs, 0); vnc_write_u16(vs, 1); /* number of rects */ vnc_framebuffer_update(vs, dst_x, dst_y, w, h, 1); vnc_write_u16(vs, src_x); vnc_write_u16(vs, src_y); vnc_flush(vs);}static int find_dirty_height(VncState *vs, int y, int last_x, int x){ int h; for (h = 1; h < (vs->height - y); h++) { int tmp_x; if (!vnc_get_bit(vs->dirty_row[y + h], last_x)) break; for (tmp_x = last_x; tmp_x < x; tmp_x++) vnc_clear_bit(vs->dirty_row[y + h], tmp_x); } return h;}static void vnc_update_client(void *opaque){ VncState *vs = opaque; if (vs->need_update && vs->csock != -1) { int y; uint8_t *row; char *old_row; uint32_t width_mask[VNC_DIRTY_WORDS]; int n_rectangles; int saved_offset; int has_dirty = 0; vnc_set_bits(width_mask, (vs->width / 16), VNC_DIRTY_WORDS); /* Walk through the dirty map and eliminate tiles that really aren't dirty */ row = vs->ds->data; old_row = vs->old_data; for (y = 0; y < vs->height; y++) { if (vnc_and_bits(vs->dirty_row[y], width_mask, VNC_DIRTY_WORDS)) { int x; uint8_t *ptr; char *old_ptr; ptr = row; old_ptr = (char*)old_row; for (x = 0; x < vs->ds->width; x += 16) { if (memcmp(old_ptr, ptr, 16 * vs->depth) == 0) { vnc_clear_bit(vs->dirty_row[y], (x / 16)); } else { has_dirty = 1; memcpy(old_ptr, ptr, 16 * vs->depth); } ptr += 16 * vs->depth; old_ptr += 16 * vs->depth; } } row += vs->ds->linesize; old_row += vs->ds->linesize; } if (!has_dirty) { qemu_mod_timer(vs->timer, qemu_get_clock(rt_clock) + VNC_REFRESH_INTERVAL); return; } /* Count rectangles */ n_rectangles = 0; vnc_write_u8(vs, 0); /* msg id */ vnc_write_u8(vs, 0); saved_offset = vs->output.offset; vnc_write_u16(vs, 0); for (y = 0; y < vs->height; y++) { int x; int last_x = -1; for (x = 0; x < vs->width / 16; x++) { if (vnc_get_bit(vs->dirty_row[y], x)) { if (last_x == -1) { last_x = x; } vnc_clear_bit(vs->dirty_row[y], x); } else { if (last_x != -1) { int h = find_dirty_height(vs, y, last_x, x);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -