📄 vnc.c
字号:
for (y = 0; y < vs->height; y++) { if (vnc_and_bits(vs->dirty_row[y], width_mask, VNC_DIRTY_WORDS)) { 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) { 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); 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){ long 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; long ret; buffer_reserve(&vs->input, 4096); ret = recv(vs->csock, buffer_end(&vs->input), 4096, 0); ret = vnc_client_io_error(vs, ret, socket_error()); if (!ret) return; vs->input.offset += ret; while (vs->read_handler && vs->input.offset >= vs->read_handler_expect) { size_t len = vs->read_handler_expect; int ret; ret = vs->read_handler(vs, vs->input.buffer, len); if (vs->csock == -1) return; if (!ret) { memmove(vs->input.buffer, vs->input.buffer + len, (vs->input.offset - len)); vs->input.offset -= len; } else { vs->read_handler_expect = ret; } }}static void vnc_write(VncState *vs, const void *data, size_t len){ buffer_reserve(&vs->output, len); if (buffer_empty(&vs->output)) { qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, vnc_client_write, vs); } buffer_append(&vs->output, data, len);}static void vnc_write_s32(VncState *vs, int32_t value){ vnc_write_u32(vs, *(uint32_t *)&value);}static void vnc_write_u32(VncState *vs, uint32_t value){ uint8_t buf[4]; buf[0] = (value >> 24) & 0xFF; buf[1] = (value >> 16) & 0xFF; buf[2] = (value >> 8) & 0xFF; buf[3] = value & 0xFF; vnc_write(vs, buf, 4);}static void vnc_write_u16(VncState *vs, uint16_t value){ uint8_t buf[2]; buf[0] = (value >> 8) & 0xFF; buf[1] = value & 0xFF; vnc_write(vs, buf, 2);}static void vnc_write_u8(VncState *vs, uint8_t value){ vnc_write(vs, (char *)&value, 1);}static void vnc_flush(VncState *vs){ if (vs->output.offset) vnc_client_write(vs);}static uint8_t read_u8(uint8_t *data, size_t offset){ return data[offset];}static uint16_t read_u16(uint8_t *data, size_t offset){ return ((data[offset] & 0xFF) << 8) | (data[offset + 1] & 0xFF);}static int32_t read_s32(uint8_t *data, size_t offset){ return (int32_t)((data[offset] << 24) | (data[offset + 1] << 16) | (data[offset + 2] << 8) | data[offset + 3]);}static uint32_t read_u32(uint8_t *data, size_t offset){ return ((data[offset] << 24) | (data[offset + 1] << 16) | (data[offset + 2] << 8) | data[offset + 3]);}static void client_cut_text(VncState *vs, size_t len, char *text){}static void check_pointer_type_change(VncState *vs, int absolute){ if (vs->has_pointer_type_change && vs->absolute != absolute) { vnc_write_u8(vs, 0); vnc_write_u8(vs, 0); vnc_write_u16(vs, 1); vnc_framebuffer_update(vs, absolute, 0, vs->ds->width, vs->ds->height, -257); vnc_flush(vs); } vs->absolute = absolute;}static void pointer_event(VncState *vs, int button_mask, int x, int y){ int buttons = 0; int dz = 0; if (button_mask & 0x01) buttons |= MOUSE_EVENT_LBUTTON; if (button_mask & 0x02) buttons |= MOUSE_EVENT_MBUTTON; if (button_mask & 0x04) buttons |= MOUSE_EVENT_RBUTTON; if (button_mask & 0x08) dz = -1; if (button_mask & 0x10) dz = 1; if (vs->absolute) { kbd_mouse_event(x * 0x7FFF / vs->ds->width, y * 0x7FFF / vs->ds->height, dz, buttons); } else if (vs->has_pointer_type_change) { x -= 0x7FFF; y -= 0x7FFF; kbd_mouse_event(x, y, dz, buttons); } else { if (vs->last_x != -1) kbd_mouse_event(x - vs->last_x, y - vs->last_y, dz, buttons); vs->last_x = x; vs->last_y = y; } check_pointer_type_change(vs, kbd_mouse_is_absolute());}static void reset_keys(VncState *vs){ int i; for(i = 0; i < 256; i++) { if (vs->modifiers_state[i]) { if (i & 0x80) kbd_put_keycode(0xe0); kbd_put_keycode(i | 0x80); vs->modifiers_state[i] = 0; } }}static void do_key_event(VncState *vs, int down, uint32_t sym){ int keycode; keycode = keysym2scancode(vs->kbd_layout, sym & 0xFFFF); /* QEMU console switch */ switch(keycode) { case 0x2a: /* Left Shift */ case 0x36: /* Right Shift */ case 0x1d: /* Left CTRL */ case 0x9d: /* Right CTRL */ case 0x38: /* Left ALT */ case 0xb8: /* Right ALT */ if (down) vs->modifiers_state[keycode] = 1; else vs->modifiers_state[keycode] = 0; break; case 0x02 ... 0x0a: /* '1' to '9' keys */ if (down && vs->modifiers_state[0x1d] && vs->modifiers_state[0x38]) { /* Reset the modifiers sent to the current console */ reset_keys(vs); console_select(keycode - 0x02); return; } break; } if (is_graphic_console()) { if (keycode & 0x80) kbd_put_keycode(0xe0); if (down) kbd_put_keycode(keycode & 0x7f); else kbd_put_keycode(keycode | 0x80); } else { /* QEMU console emulation */ if (down) { switch (keycode) { case 0x2a: /* Left Shift */ case 0x36: /* Right Shift */ case 0x1d: /* Left CTRL */ case 0x9d: /* Right CTRL */ case 0x38: /* Left ALT */ case 0xb8: /* Right ALT */ break; case 0xc8: kbd_put_keysym(QEMU_KEY_UP); break; case 0xd0: kbd_put_keysym(QEMU_KEY_DOWN); break; case 0xcb: kbd_put_keysym(QEMU_KEY_LEFT); break; case 0xcd: kbd_put_keysym(QEMU_KEY_RIGHT); break; case 0xd3: kbd_put_keysym(QEMU_KEY_DELETE); break; case 0xc7: kbd_put_keysym(QEMU_KEY_HOME); break; case 0xcf: kbd_put_keysym(QEMU_KEY_END); break; case 0xc9: kbd_put_keysym(QEMU_KEY_PAGEUP); break; case 0xd1: kbd_put_keysym(QEMU_KEY_PAGEDOWN); break; default: kbd_put_keysym(sym); break; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -