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

📄 vnc.c

📁 qemu虚拟机代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * 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);struct VncState{    QEMUTimer *timer;    int lsock;    int csock;    DisplayState *ds;    int need_update;    int width;    int height;    uint64_t dirty_row[768];    char *old_data;    int depth;    int has_resize;    int has_hextile;    Buffer output;    Buffer input;    kbd_layout_t *kbd_layout;    VncReadEvent *read_handler;    size_t read_handler_expect;};/* 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 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)	    vs->dirty_row[y] |= (1ULL << ((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){    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;    ds->width = w;    ds->height = h;    ds->linesize = w * vs->depth;    if (vs->csock != -1 && vs->has_resize) {	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;    }}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++) {	vnc_write(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 BPPstatic 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;    uint16_t last_fg16, last_bg16;    uint8_t last_fg8, last_bg8;    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) {	    switch (vs->depth) {	    case 1:		send_hextile_tile_8(vs, i, j, MIN(16, x + w - i), MIN(16, y + h - j),				    &last_bg8, &last_fg8, &has_bg, &has_fg);		break;	    case 2:		send_hextile_tile_16(vs, i, j, MIN(16, x + w - i), MIN(16, y + h - j),				     &last_bg16, &last_fg16, &has_bg, &has_fg);		break;	    case 4:		send_hextile_tile_32(vs, i, j, MIN(16, x + w - i), MIN(16, y + h - j),				     &last_bg32, &last_fg32, &has_bg, &has_fg);		break;	    default:		break;	    }	}    }}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 (!(vs->dirty_row[y + h] & (1ULL << last_x)))	    break;	for (tmp_x = last_x; tmp_x < x; tmp_x++)	    vs->dirty_row[y + h] &= ~(1ULL << 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;	uint64_t width_mask;	int n_rectangles;	int saved_offset;	int has_dirty = 0;	width_mask = (1ULL << (vs->width / 16)) - 1;	if (vs->width == 1024)	    width_mask = ~(0ULL);	/* 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 (vs->dirty_row[y] & width_mask) {		int x;		char *ptr, *old_ptr;		ptr = row;		old_ptr = old_row;		for (x = 0; x < vs->ds->width; x += 16) {		    if (memcmp(old_ptr, ptr, 16 * vs->depth) == 0) {			vs->dirty_row[y] &= ~(1ULL << (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 (vs->dirty_row[y] & (1ULL << x)) {		    if (last_x == -1) {			last_x = x;		    }		    vs->dirty_row[y] &= ~(1ULL << x);		} else {		    if (last_x != -1) {			int h = find_dirty_height(vs, y, last_x, x);			send_framebuffer_update(vs, last_x * 16, y, (x - last_x) * 16, h);			n_rectangles++;		    }		    last_x = -1;		}	    }	    if (last_x != -1) {		int h = find_dirty_height(vs, y, last_x, x);		send_framebuffer_update(vs, last_x * 16, y, (x - last_x) * 16, h);		n_rectangles++;	    }	}	vs->output.buffer[saved_offset] = (n_rectangles >> 8) & 0xFF;	vs->output.buffer[saved_offset + 1] = n_rectangles & 0xFF;	vnc_flush(vs);    }    qemu_mod_timer(vs->timer, qemu_get_clock(rt_clock) + VNC_REFRESH_INTERVAL);}static void vnc_timer_init(VncState *vs){    if (vs->timer == NULL) {	vs->timer = qemu_new_timer(rt_clock, vnc_update_client, vs);	qemu_mod_timer(vs->timer, qemu_get_clock(rt_clock));    }}static void vnc_dpy_refresh(DisplayState *ds){    VncState *vs = ds->opaque;    vnc_timer_init(vs);    vga_hw_update();}static int vnc_listen_poll(void *opaque){    VncState *vs = opaque;    if (vs->csock == -1)	return 1;    return 0;}static void buffer_reserve(Buffer *buffer, size_t len){    if ((buffer->capacity - buffer->offset) < len) {	buffer->capacity += (len + 1024);	buffer->buffer = realloc(buffer->buffer, buffer->capacity);	if (buffer->buffer == NULL) {	    fprintf(stderr, "vnc: out of memory\n");	    exit(1);	}    }}static int buffer_empty(Buffer *buffer){    return buffer->offset == 0;}static char *buffer_end(Buffer *buffer){    return buffer->buffer + buffer->offset;}static void buffer_reset(Buffer *buffer){	buffer->offset = 0;}static void buffer_append(Buffer *buffer, const void *data, size_t len){    memcpy(buffer->buffer + buffer->offset, data, len);    buffer->offset += len;}static int vnc_client_io_error(VncState *vs, int ret, int last_errno){    if (ret == 0 || ret == -1) {	if (ret == -1 && (last_errno == EINTR || last_errno == EAGAIN))	    return 0;	qemu_set_fd_handler2(vs->csock, NULL, NULL, NULL, NULL);	closesocket(vs->csock);	vs->csock = -1;	buffer_reset(&vs->input);	buffer_reset(&vs->output);	vs->need_update = 0;	return 0;    }    return ret;}static void vnc_client_error(VncState *vs){    vnc_client_io_error(vs, -1, EINVAL);}static void vnc_client_write(void *opaque){    ssize_t ret;    VncState *vs = opaque;    ret = send(vs->csock, vs->output.buffer, vs->output.offset, 0);    ret = vnc_client_io_error(vs, ret, socket_error());    if (!ret)	return;    memmove(vs->output.buffer, vs->output.buffer + ret, (vs->output.offset - ret));    vs->output.offset -= ret;    if (vs->output.offset == 0) {	qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);    }}static void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting){    vs->read_handler = func;    vs->read_handler_expect = expecting;}static void vnc_client_read(void *opaque){    VncState *vs = opaque;    ssize_t ret;    buffer_reserve(&vs->input, 4096);

⌨️ 快捷键说明

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