⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vnc.c

📁 qemu性能直逼VMware的仿真器QEMU 的模擬速度約為實機的 25%;約為 Bochs 的 60 倍。Plex86、User-Mode-Linux、VMware 和 Virtual PC 則比
💻 C
📖 第 1 页 / 共 3 页
字号:
/* * 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 "vl.h"#include "qemu_socket.h"#define VNC_REFRESH_INTERVAL (1000 / 30)#include "vnc_keysym.h"#include "keymaps.c"typedef struct Buffer{    size_t capacity;    size_t offset;    char *buffer;} Buffer;typedef struct VncState VncState;typedef int VncReadEvent(VncState *vs, char *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))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;    const char *display;    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;    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);    }    ds->depth = vs->depth * 8;    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;    char *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;    char *src_row;    char *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;	char *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;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -