Commit 24236869fbff210b356b6626e6e9e9eadc8a976c
1 parent
a46e4035
VNC server (Anthony Liguori)
git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@1869 c046a42c-6fe2-441c-8c8c-71466251a162
Showing
10 changed files
with
1477 additions
and
4 deletions
Changelog
Makefile.target
... | ... | @@ -357,6 +357,7 @@ endif |
357 | 357 | ifdef CONFIG_SDL |
358 | 358 | VL_OBJS+=sdl.o |
359 | 359 | endif |
360 | +VL_OBJS+=vnc.o | |
360 | 361 | ifdef CONFIG_COCOA |
361 | 362 | VL_OBJS+=cocoa.o |
362 | 363 | COCOA_LIBS=-F/System/Library/Frameworks -framework Cocoa -framework IOKit |
... | ... | @@ -409,6 +410,9 @@ cocoa.o: cocoa.m |
409 | 410 | sdl.o: sdl.c keymaps.c sdl_keysym.h |
410 | 411 | $(CC) $(CFLAGS) $(DEFINES) $(SDL_CFLAGS) -c -o $@ $< |
411 | 412 | |
413 | +vnc.o: vnc.c keymaps.c sdl_keysym.h vnchextile.h | |
414 | + $(CC) $(CFLAGS) $(DEFINES) -c -o $@ $< | |
415 | + | |
412 | 416 | sdlaudio.o: sdlaudio.c |
413 | 417 | $(CC) $(CFLAGS) $(DEFINES) $(SDL_CFLAGS) -c -o $@ $< |
414 | 418 | ... | ... |
hw/cirrus_vga.c
... | ... | @@ -644,15 +644,90 @@ static int cirrus_bitblt_videotovideo_patterncopy(CirrusVGAState * s) |
644 | 644 | (s->cirrus_blt_srcaddr & ~7)); |
645 | 645 | } |
646 | 646 | |
647 | -static int cirrus_bitblt_videotovideo_copy(CirrusVGAState * s) | |
647 | +static void cirrus_do_copy(CirrusVGAState *s, int dst, int src, int w, int h) | |
648 | 648 | { |
649 | + int sx, sy; | |
650 | + int dx, dy; | |
651 | + int width, height; | |
652 | + int depth; | |
653 | + int notify = 0; | |
654 | + | |
655 | + depth = s->get_bpp((VGAState *)s) / 8; | |
656 | + s->get_resolution((VGAState *)s, &width, &height); | |
657 | + | |
658 | + /* extra x, y */ | |
659 | + sx = (src % (width * depth)) / depth; | |
660 | + sy = (src / (width * depth)); | |
661 | + dx = (dst % (width *depth)) / depth; | |
662 | + dy = (dst / (width * depth)); | |
663 | + | |
664 | + /* normalize width */ | |
665 | + w /= depth; | |
666 | + | |
667 | + /* if we're doing a backward copy, we have to adjust | |
668 | + our x/y to be the upper left corner (instead of the lower | |
669 | + right corner) */ | |
670 | + if (s->cirrus_blt_dstpitch < 0) { | |
671 | + sx -= (s->cirrus_blt_width / depth) - 1; | |
672 | + dx -= (s->cirrus_blt_width / depth) - 1; | |
673 | + sy -= s->cirrus_blt_height - 1; | |
674 | + dy -= s->cirrus_blt_height - 1; | |
675 | + } | |
676 | + | |
677 | + /* are we in the visible portion of memory? */ | |
678 | + if (sx >= 0 && sy >= 0 && dx >= 0 && dy >= 0 && | |
679 | + (sx + w) <= width && (sy + h) <= height && | |
680 | + (dx + w) <= width && (dy + h) <= height) { | |
681 | + notify = 1; | |
682 | + } | |
683 | + | |
684 | + /* make to sure only copy if it's a plain copy ROP */ | |
685 | + if (*s->cirrus_rop != cirrus_bitblt_rop_fwd_src && | |
686 | + *s->cirrus_rop != cirrus_bitblt_rop_bkwd_src) | |
687 | + notify = 0; | |
688 | + | |
689 | + /* we have to flush all pending changes so that the copy | |
690 | + is generated at the appropriate moment in time */ | |
691 | + if (notify) | |
692 | + vga_hw_update(); | |
693 | + | |
649 | 694 | (*s->cirrus_rop) (s, s->vram_ptr + s->cirrus_blt_dstaddr, |
650 | 695 | s->vram_ptr + s->cirrus_blt_srcaddr, |
651 | 696 | s->cirrus_blt_dstpitch, s->cirrus_blt_srcpitch, |
652 | 697 | s->cirrus_blt_width, s->cirrus_blt_height); |
653 | - cirrus_invalidate_region(s, s->cirrus_blt_dstaddr, | |
654 | - s->cirrus_blt_dstpitch, s->cirrus_blt_width, | |
655 | - s->cirrus_blt_height); | |
698 | + | |
699 | + if (notify) | |
700 | + s->ds->dpy_copy(s->ds, | |
701 | + sx, sy, dx, dy, | |
702 | + s->cirrus_blt_width / depth, | |
703 | + s->cirrus_blt_height); | |
704 | + | |
705 | + /* we don't have to notify the display that this portion has | |
706 | + changed since dpy_copy implies this */ | |
707 | + | |
708 | + if (!notify) | |
709 | + cirrus_invalidate_region(s, s->cirrus_blt_dstaddr, | |
710 | + s->cirrus_blt_dstpitch, s->cirrus_blt_width, | |
711 | + s->cirrus_blt_height); | |
712 | +} | |
713 | + | |
714 | +static int cirrus_bitblt_videotovideo_copy(CirrusVGAState * s) | |
715 | +{ | |
716 | + if (s->ds->dpy_copy) { | |
717 | + cirrus_do_copy(s, s->cirrus_blt_dstaddr - s->start_addr, | |
718 | + s->cirrus_blt_srcaddr - s->start_addr, | |
719 | + s->cirrus_blt_width, s->cirrus_blt_height); | |
720 | + } else { | |
721 | + (*s->cirrus_rop) (s, s->vram_ptr + s->cirrus_blt_dstaddr, | |
722 | + s->vram_ptr + s->cirrus_blt_srcaddr, | |
723 | + s->cirrus_blt_dstpitch, s->cirrus_blt_srcpitch, | |
724 | + s->cirrus_blt_width, s->cirrus_blt_height); | |
725 | + | |
726 | + cirrus_invalidate_region(s, s->cirrus_blt_dstaddr, | |
727 | + s->cirrus_blt_dstpitch, s->cirrus_blt_width, | |
728 | + s->cirrus_blt_height); | |
729 | + } | |
730 | + | |
656 | 731 | return 1; |
657 | 732 | } |
658 | 733 | ... | ... |
keymaps.c
... | ... | @@ -99,8 +99,10 @@ static kbd_layout_t *parse_keyboard_layout(const char *language, |
99 | 99 | "Warning: Could not assign keysym %s (0x%x) because of memory constraints.\n", |
100 | 100 | line, keysym); |
101 | 101 | } else { |
102 | +#if 0 | |
102 | 103 | fprintf(stderr, "Setting %d: %d,%d\n", |
103 | 104 | k->extra_count, keysym, keycode); |
105 | +#endif | |
104 | 106 | k->keysym2keycode_extra[k->extra_count]. |
105 | 107 | keysym = keysym; |
106 | 108 | k->keysym2keycode_extra[k->extra_count]. | ... | ... |
qemu-doc.texi
... | ... | @@ -185,6 +185,13 @@ command line application. The emulated serial port is redirected on |
185 | 185 | the console. Therefore, you can still use QEMU to debug a Linux kernel |
186 | 186 | with a serial console. |
187 | 187 | |
188 | +@item -vnc d | |
189 | + | |
190 | +Normally, QEMU uses SDL to display the VGA output. With this option, | |
191 | +you can have QEMU listen on VNC display d and redirect the VGA display | |
192 | +over the VNC session. It is very useful to enable the usb tablet device | |
193 | +when using this option (option @option{-usbdevice tablet}). | |
194 | + | |
188 | 195 | @item -k language |
189 | 196 | |
190 | 197 | Use keyboard layout @var{language} (for example @code{fr} for | ... | ... |
vl.c
... | ... | @@ -149,6 +149,7 @@ USBPort *vm_usb_ports[MAX_VM_USB_PORTS]; |
149 | 149 | USBDevice *vm_usb_hub; |
150 | 150 | static VLANState *first_vlan; |
151 | 151 | int smp_cpus = 1; |
152 | +int vnc_display = -1; | |
152 | 153 | #if defined(TARGET_SPARC) |
153 | 154 | #define MAX_CPUS 16 |
154 | 155 | #elif defined(TARGET_I386) |
... | ... | @@ -4638,6 +4639,7 @@ void help(void) |
4638 | 4639 | " (default is CL-GD5446 PCI VGA)\n" |
4639 | 4640 | #endif |
4640 | 4641 | "-loadvm file start right away with a saved state (loadvm in monitor)\n" |
4642 | + "-vnc display start a VNC server on display\n" | |
4641 | 4643 | "\n" |
4642 | 4644 | "During emulation, the following keys are useful:\n" |
4643 | 4645 | "ctrl-alt-f toggle full screen\n" |
... | ... | @@ -4721,6 +4723,7 @@ enum { |
4721 | 4723 | QEMU_OPTION_usb, |
4722 | 4724 | QEMU_OPTION_usbdevice, |
4723 | 4725 | QEMU_OPTION_smp, |
4726 | + QEMU_OPTION_vnc, | |
4724 | 4727 | }; |
4725 | 4728 | |
4726 | 4729 | typedef struct QEMUOption { |
... | ... | @@ -4788,6 +4791,7 @@ const QEMUOption qemu_options[] = { |
4788 | 4791 | { "win2k-hack", 0, QEMU_OPTION_win2k_hack }, |
4789 | 4792 | { "usbdevice", HAS_ARG, QEMU_OPTION_usbdevice }, |
4790 | 4793 | { "smp", HAS_ARG, QEMU_OPTION_smp }, |
4794 | + { "vnc", HAS_ARG, QEMU_OPTION_vnc }, | |
4791 | 4795 | |
4792 | 4796 | /* temporary options */ |
4793 | 4797 | { "usb", 0, QEMU_OPTION_usb }, |
... | ... | @@ -5386,6 +5390,13 @@ int main(int argc, char **argv) |
5386 | 5390 | exit(1); |
5387 | 5391 | } |
5388 | 5392 | break; |
5393 | + case QEMU_OPTION_vnc: | |
5394 | + vnc_display = atoi(optarg); | |
5395 | + if (vnc_display < 0) { | |
5396 | + fprintf(stderr, "Invalid VNC display\n"); | |
5397 | + exit(1); | |
5398 | + } | |
5399 | + break; | |
5389 | 5400 | } |
5390 | 5401 | } |
5391 | 5402 | } |
... | ... | @@ -5551,6 +5562,8 @@ int main(int argc, char **argv) |
5551 | 5562 | /* terminal init */ |
5552 | 5563 | if (nographic) { |
5553 | 5564 | dumb_display_init(ds); |
5565 | + } if (vnc_display != -1) { | |
5566 | + vnc_display_init(ds, vnc_display); | |
5554 | 5567 | } else { |
5555 | 5568 | #if defined(CONFIG_SDL) |
5556 | 5569 | sdl_display_init(ds, full_screen); | ... | ... |
vl.h
... | ... | @@ -82,6 +82,13 @@ static inline char *realpath(const char *path, char *resolved_path) |
82 | 82 | #define tostring(s) #s |
83 | 83 | #endif |
84 | 84 | |
85 | +#ifndef MIN | |
86 | +#define MIN(a, b) (((a) < (b)) ? (a) : (b)) | |
87 | +#endif | |
88 | +#ifndef MAX | |
89 | +#define MAX(a, b) (((a) > (b)) ? (a) : (b)) | |
90 | +#endif | |
91 | + | |
85 | 92 | /* vl.c */ |
86 | 93 | uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c); |
87 | 94 | |
... | ... | @@ -672,9 +679,12 @@ struct DisplayState { |
672 | 679 | int depth; |
673 | 680 | int width; |
674 | 681 | int height; |
682 | + void *opaque; | |
683 | + | |
675 | 684 | void (*dpy_update)(struct DisplayState *s, int x, int y, int w, int h); |
676 | 685 | void (*dpy_resize)(struct DisplayState *s, int w, int h); |
677 | 686 | void (*dpy_refresh)(struct DisplayState *s); |
687 | + void (*dpy_copy)(struct DisplayState *s, int src_x, int src_y, int dst_x, int dst_y, int w, int h); | |
678 | 688 | }; |
679 | 689 | |
680 | 690 | static inline void dpy_update(DisplayState *s, int x, int y, int w, int h) |
... | ... | @@ -703,6 +713,9 @@ void sdl_display_init(DisplayState *ds, int full_screen); |
703 | 713 | /* cocoa.m */ |
704 | 714 | void cocoa_display_init(DisplayState *ds, int full_screen); |
705 | 715 | |
716 | +/* vnc.c */ | |
717 | +void vnc_display_init(DisplayState *ds, int display); | |
718 | + | |
706 | 719 | /* ide.c */ |
707 | 720 | #define MAX_DISKS 4 |
708 | 721 | ... | ... |
vnc.c
0 โ 100644
1 | +#include "vl.h" | |
2 | + | |
3 | +#include <sys/socket.h> | |
4 | +#include <netinet/in.h> | |
5 | +#include <netinet/tcp.h> | |
6 | +#include <fcntl.h> | |
7 | + | |
8 | +#define VNC_REFRESH_INTERVAL (1000 / 30) | |
9 | + | |
10 | +#include "vnc_keysym.h" | |
11 | +#include "keymaps.c" | |
12 | + | |
13 | +typedef struct Buffer | |
14 | +{ | |
15 | + size_t capacity; | |
16 | + size_t offset; | |
17 | + char *buffer; | |
18 | +} Buffer; | |
19 | + | |
20 | +typedef struct VncState VncState; | |
21 | + | |
22 | +typedef int VncReadEvent(VncState *vs, char *data, size_t len); | |
23 | + | |
24 | +struct VncState | |
25 | +{ | |
26 | + QEMUTimer *timer; | |
27 | + int lsock; | |
28 | + int csock; | |
29 | + DisplayState *ds; | |
30 | + int need_update; | |
31 | + int width; | |
32 | + int height; | |
33 | + uint64_t dirty_row[768]; | |
34 | + char *old_data; | |
35 | + int depth; | |
36 | + int has_resize; | |
37 | + int has_hextile; | |
38 | + Buffer output; | |
39 | + Buffer input; | |
40 | + kbd_layout_t *kbd_layout; | |
41 | + | |
42 | + VncReadEvent *read_handler; | |
43 | + size_t read_handler_expect; | |
44 | +}; | |
45 | + | |
46 | +/* TODO | |
47 | + 1) Get the queue working for IO. | |
48 | + 2) there is some weirdness when using the -S option (the screen is grey | |
49 | + and not totally invalidated | |
50 | + 3) resolutions > 1024 | |
51 | +*/ | |
52 | + | |
53 | +static void vnc_write(VncState *vs, const void *data, size_t len); | |
54 | +static void vnc_write_u32(VncState *vs, uint32_t value); | |
55 | +static void vnc_write_s32(VncState *vs, int32_t value); | |
56 | +static void vnc_write_u16(VncState *vs, uint16_t value); | |
57 | +static void vnc_write_u8(VncState *vs, uint8_t value); | |
58 | +static void vnc_flush(VncState *vs); | |
59 | +static void vnc_update_client(void *opaque); | |
60 | +static void vnc_client_read(void *opaque); | |
61 | + | |
62 | +static void vnc_dpy_update(DisplayState *ds, int x, int y, int w, int h) | |
63 | +{ | |
64 | + VncState *vs = ds->opaque; | |
65 | + int i; | |
66 | + | |
67 | + h += y; | |
68 | + | |
69 | + for (; y < h; y++) | |
70 | + for (i = 0; i < w; i += 16) | |
71 | + vs->dirty_row[y] |= (1ULL << ((x + i) / 16)); | |
72 | +} | |
73 | + | |
74 | +static void vnc_framebuffer_update(VncState *vs, int x, int y, int w, int h, | |
75 | + int32_t encoding) | |
76 | +{ | |
77 | + vnc_write_u16(vs, x); | |
78 | + vnc_write_u16(vs, y); | |
79 | + vnc_write_u16(vs, w); | |
80 | + vnc_write_u16(vs, h); | |
81 | + | |
82 | + vnc_write_s32(vs, encoding); | |
83 | +} | |
84 | + | |
85 | +static void vnc_dpy_resize(DisplayState *ds, int w, int h) | |
86 | +{ | |
87 | + VncState *vs = ds->opaque; | |
88 | + | |
89 | + ds->data = realloc(ds->data, w * h * vs->depth); | |
90 | + vs->old_data = realloc(vs->old_data, w * h * vs->depth); | |
91 | + | |
92 | + if (ds->data == NULL || vs->old_data == NULL) { | |
93 | + fprintf(stderr, "vnc: memory allocation failed\n"); | |
94 | + exit(1); | |
95 | + } | |
96 | + | |
97 | + ds->depth = vs->depth * 8; | |
98 | + ds->width = w; | |
99 | + ds->height = h; | |
100 | + ds->linesize = w * vs->depth; | |
101 | + if (vs->csock != -1 && vs->has_resize) { | |
102 | + vnc_write_u8(vs, 0); /* msg id */ | |
103 | + vnc_write_u8(vs, 0); | |
104 | + vnc_write_u16(vs, 1); /* number of rects */ | |
105 | + vnc_framebuffer_update(vs, 0, 0, ds->width, ds->height, -223); | |
106 | + vnc_flush(vs); | |
107 | + vs->width = ds->width; | |
108 | + vs->height = ds->height; | |
109 | + } | |
110 | +} | |
111 | + | |
112 | +static void send_framebuffer_update_raw(VncState *vs, int x, int y, int w, int h) | |
113 | +{ | |
114 | + int i; | |
115 | + char *row; | |
116 | + | |
117 | + vnc_framebuffer_update(vs, x, y, w, h, 0); | |
118 | + | |
119 | + row = vs->ds->data + y * vs->ds->linesize + x * vs->depth; | |
120 | + for (i = 0; i < h; i++) { | |
121 | + vnc_write(vs, row, w * vs->depth); | |
122 | + row += vs->ds->linesize; | |
123 | + } | |
124 | +} | |
125 | + | |
126 | +static void hextile_enc_cord(uint8_t *ptr, int x, int y, int w, int h) | |
127 | +{ | |
128 | + ptr[0] = ((x & 0x0F) << 4) | (y & 0x0F); | |
129 | + ptr[1] = (((w - 1) & 0x0F) << 4) | ((h - 1) & 0x0F); | |
130 | +} | |
131 | + | |
132 | +#define BPP 8 | |
133 | +#include "vnchextile.h" | |
134 | +#undef BPP | |
135 | + | |
136 | +#define BPP 16 | |
137 | +#include "vnchextile.h" | |
138 | +#undef BPP | |
139 | + | |
140 | +#define BPP 32 | |
141 | +#include "vnchextile.h" | |
142 | +#undef BPP | |
143 | + | |
144 | +static void send_framebuffer_update_hextile(VncState *vs, int x, int y, int w, int h) | |
145 | +{ | |
146 | + int i, j; | |
147 | + int has_fg, has_bg; | |
148 | + uint32_t last_fg32, last_bg32; | |
149 | + uint16_t last_fg16, last_bg16; | |
150 | + uint8_t last_fg8, last_bg8; | |
151 | + | |
152 | + vnc_framebuffer_update(vs, x, y, w, h, 5); | |
153 | + | |
154 | + has_fg = has_bg = 0; | |
155 | + for (j = y; j < (y + h); j += 16) { | |
156 | + for (i = x; i < (x + w); i += 16) { | |
157 | + switch (vs->depth) { | |
158 | + case 1: | |
159 | + send_hextile_tile_8(vs, i, j, MIN(16, x + w - i), MIN(16, y + h - j), | |
160 | + &last_bg8, &last_fg8, &has_bg, &has_fg); | |
161 | + break; | |
162 | + case 2: | |
163 | + send_hextile_tile_16(vs, i, j, MIN(16, x + w - i), MIN(16, y + h - j), | |
164 | + &last_bg16, &last_fg16, &has_bg, &has_fg); | |
165 | + break; | |
166 | + case 4: | |
167 | + send_hextile_tile_32(vs, i, j, MIN(16, x + w - i), MIN(16, y + h - j), | |
168 | + &last_bg32, &last_fg32, &has_bg, &has_fg); | |
169 | + break; | |
170 | + default: | |
171 | + break; | |
172 | + } | |
173 | + } | |
174 | + } | |
175 | +} | |
176 | + | |
177 | +static void send_framebuffer_update(VncState *vs, int x, int y, int w, int h) | |
178 | +{ | |
179 | + if (vs->has_hextile) | |
180 | + send_framebuffer_update_hextile(vs, x, y, w, h); | |
181 | + else | |
182 | + send_framebuffer_update_raw(vs, x, y, w, h); | |
183 | +} | |
184 | + | |
185 | +static void vnc_copy(DisplayState *ds, int src_x, int src_y, int dst_x, int dst_y, int w, int h) | |
186 | +{ | |
187 | + int src, dst; | |
188 | + char *src_row; | |
189 | + char *dst_row; | |
190 | + char *old_row; | |
191 | + int y = 0; | |
192 | + int pitch = ds->linesize; | |
193 | + VncState *vs = ds->opaque; | |
194 | + | |
195 | + vnc_update_client(vs); | |
196 | + | |
197 | + if (dst_y > src_y) { | |
198 | + y = h - 1; | |
199 | + pitch = -pitch; | |
200 | + } | |
201 | + | |
202 | + src = (ds->linesize * (src_y + y) + vs->depth * src_x); | |
203 | + dst = (ds->linesize * (dst_y + y) + vs->depth * dst_x); | |
204 | + | |
205 | + src_row = ds->data + src; | |
206 | + dst_row = ds->data + dst; | |
207 | + old_row = vs->old_data + dst; | |
208 | + | |
209 | + for (y = 0; y < h; y++) { | |
210 | + memmove(old_row, src_row, w * vs->depth); | |
211 | + memmove(dst_row, src_row, w * vs->depth); | |
212 | + src_row += pitch; | |
213 | + dst_row += pitch; | |
214 | + old_row += pitch; | |
215 | + } | |
216 | + | |
217 | + vnc_write_u8(vs, 0); /* msg id */ | |
218 | + vnc_write_u8(vs, 0); | |
219 | + vnc_write_u16(vs, 1); /* number of rects */ | |
220 | + vnc_framebuffer_update(vs, dst_x, dst_y, w, h, 1); | |
221 | + vnc_write_u16(vs, src_x); | |
222 | + vnc_write_u16(vs, src_y); | |
223 | + vnc_flush(vs); | |
224 | +} | |
225 | + | |
226 | +static int find_dirty_height(VncState *vs, int y, int last_x, int x) | |
227 | +{ | |
228 | + int h; | |
229 | + | |
230 | + for (h = 1; h < (vs->height - y); h++) { | |
231 | + int tmp_x; | |
232 | + if (!(vs->dirty_row[y + h] & (1ULL << last_x))) | |
233 | + break; | |
234 | + for (tmp_x = last_x; tmp_x < x; tmp_x++) | |
235 | + vs->dirty_row[y + h] &= ~(1ULL << tmp_x); | |
236 | + } | |
237 | + | |
238 | + return h; | |
239 | +} | |
240 | + | |
241 | +static void vnc_update_client(void *opaque) | |
242 | +{ | |
243 | + VncState *vs = opaque; | |
244 | + | |
245 | + if (vs->need_update && vs->csock != -1) { | |
246 | + int y; | |
247 | + char *row; | |
248 | + char *old_row; | |
249 | + uint64_t width_mask; | |
250 | + int n_rectangles; | |
251 | + int saved_offset; | |
252 | + int has_dirty = 0; | |
253 | + | |
254 | + width_mask = (1ULL << (vs->width / 16)) - 1; | |
255 | + | |
256 | + if (vs->width == 1024) | |
257 | + width_mask = ~(0ULL); | |
258 | + | |
259 | + /* Walk through the dirty map and eliminate tiles that | |
260 | + really aren't dirty */ | |
261 | + row = vs->ds->data; | |
262 | + old_row = vs->old_data; | |
263 | + | |
264 | + for (y = 0; y < vs->height; y++) { | |
265 | + if (vs->dirty_row[y] & width_mask) { | |
266 | + int x; | |
267 | + char *ptr, *old_ptr; | |
268 | + | |
269 | + ptr = row; | |
270 | + old_ptr = old_row; | |
271 | + | |
272 | + for (x = 0; x < vs->ds->width; x += 16) { | |
273 | + if (memcmp(old_ptr, ptr, 16 * vs->depth) == 0) { | |
274 | + vs->dirty_row[y] &= ~(1ULL << (x / 16)); | |
275 | + } else { | |
276 | + has_dirty = 1; | |
277 | + memcpy(old_ptr, ptr, 16 * vs->depth); | |
278 | + } | |
279 | + | |
280 | + ptr += 16 * vs->depth; | |
281 | + old_ptr += 16 * vs->depth; | |
282 | + } | |
283 | + } | |
284 | + | |
285 | + row += vs->ds->linesize; | |
286 | + old_row += vs->ds->linesize; | |
287 | + } | |
288 | + | |
289 | + if (!has_dirty) { | |
290 | + qemu_mod_timer(vs->timer, qemu_get_clock(rt_clock) + VNC_REFRESH_INTERVAL); | |
291 | + return; | |
292 | + } | |
293 | + | |
294 | + /* Count rectangles */ | |
295 | + n_rectangles = 0; | |
296 | + vnc_write_u8(vs, 0); /* msg id */ | |
297 | + vnc_write_u8(vs, 0); | |
298 | + saved_offset = vs->output.offset; | |
299 | + vnc_write_u16(vs, 0); | |
300 | + | |
301 | + for (y = 0; y < vs->height; y++) { | |
302 | + int x; | |
303 | + int last_x = -1; | |
304 | + for (x = 0; x < vs->width / 16; x++) { | |
305 | + if (vs->dirty_row[y] & (1ULL << x)) { | |
306 | + if (last_x == -1) { | |
307 | + last_x = x; | |
308 | + } | |
309 | + vs->dirty_row[y] &= ~(1ULL << x); | |
310 | + } else { | |
311 | + if (last_x != -1) { | |
312 | + int h = find_dirty_height(vs, y, last_x, x); | |
313 | + send_framebuffer_update(vs, last_x * 16, y, (x - last_x) * 16, h); | |
314 | + n_rectangles++; | |
315 | + } | |
316 | + last_x = -1; | |
317 | + } | |
318 | + } | |
319 | + if (last_x != -1) { | |
320 | + int h = find_dirty_height(vs, y, last_x, x); | |
321 | + send_framebuffer_update(vs, last_x * 16, y, (x - last_x) * 16, h); | |
322 | + n_rectangles++; | |
323 | + } | |
324 | + } | |
325 | + vs->output.buffer[saved_offset] = (n_rectangles >> 8) & 0xFF; | |
326 | + vs->output.buffer[saved_offset + 1] = n_rectangles & 0xFF; | |
327 | + vnc_flush(vs); | |
328 | + | |
329 | + } | |
330 | + qemu_mod_timer(vs->timer, qemu_get_clock(rt_clock) + VNC_REFRESH_INTERVAL); | |
331 | +} | |
332 | + | |
333 | +static void vnc_timer_init(VncState *vs) | |
334 | +{ | |
335 | + if (vs->timer == NULL) { | |
336 | + vs->timer = qemu_new_timer(rt_clock, vnc_update_client, vs); | |
337 | + qemu_mod_timer(vs->timer, qemu_get_clock(rt_clock)); | |
338 | + } | |
339 | +} | |
340 | + | |
341 | +static void vnc_dpy_refresh(DisplayState *ds) | |
342 | +{ | |
343 | + VncState *vs = ds->opaque; | |
344 | + vnc_timer_init(vs); | |
345 | + vga_hw_update(); | |
346 | +} | |
347 | + | |
348 | +static int vnc_listen_poll(void *opaque) | |
349 | +{ | |
350 | + VncState *vs = opaque; | |
351 | + if (vs->csock == -1) | |
352 | + return 1; | |
353 | + return 0; | |
354 | +} | |
355 | + | |
356 | +static void buffer_reserve(Buffer *buffer, size_t len) | |
357 | +{ | |
358 | + if ((buffer->capacity - buffer->offset) < len) { | |
359 | + buffer->capacity += (len + 1024); | |
360 | + buffer->buffer = realloc(buffer->buffer, buffer->capacity); | |
361 | + if (buffer->buffer == NULL) { | |
362 | + fprintf(stderr, "vnc: out of memory\n"); | |
363 | + exit(1); | |
364 | + } | |
365 | + } | |
366 | +} | |
367 | + | |
368 | +static int buffer_empty(Buffer *buffer) | |
369 | +{ | |
370 | + return buffer->offset == 0; | |
371 | +} | |
372 | + | |
373 | +static char *buffer_end(Buffer *buffer) | |
374 | +{ | |
375 | + return buffer->buffer + buffer->offset; | |
376 | +} | |
377 | + | |
378 | +static void buffer_reset(Buffer *buffer) | |
379 | +{ | |
380 | + buffer->offset = 0; | |
381 | +} | |
382 | + | |
383 | +static void buffer_append(Buffer *buffer, const void *data, size_t len) | |
384 | +{ | |
385 | + memcpy(buffer->buffer + buffer->offset, data, len); | |
386 | + buffer->offset += len; | |
387 | +} | |
388 | + | |
389 | +static int vnc_client_io_error(VncState *vs, int ret) | |
390 | +{ | |
391 | + if (ret == 0 || ret == -1) { | |
392 | + if (ret == -1 && (errno == EINTR || errno == EAGAIN)) | |
393 | + return 0; | |
394 | + | |
395 | + qemu_set_fd_handler2(vs->csock, NULL, NULL, NULL, NULL); | |
396 | + close(vs->csock); | |
397 | + vs->csock = -1; | |
398 | + buffer_reset(&vs->input); | |
399 | + buffer_reset(&vs->output); | |
400 | + vs->need_update = 0; | |
401 | + return 0; | |
402 | + } | |
403 | + return ret; | |
404 | +} | |
405 | + | |
406 | +static void vnc_client_error(VncState *vs) | |
407 | +{ | |
408 | + errno = EINVAL; | |
409 | + vnc_client_io_error(vs, -1); | |
410 | +} | |
411 | + | |
412 | +static void vnc_client_write(void *opaque) | |
413 | +{ | |
414 | + ssize_t ret; | |
415 | + VncState *vs = opaque; | |
416 | + | |
417 | + ret = write(vs->csock, vs->output.buffer, vs->output.offset); | |
418 | + ret = vnc_client_io_error(vs, ret); | |
419 | + if (!ret) | |
420 | + return; | |
421 | + | |
422 | + memmove(vs->output.buffer, vs->output.buffer + ret, (vs->output.offset - ret)); | |
423 | + vs->output.offset -= ret; | |
424 | + | |
425 | + if (vs->output.offset == 0) { | |
426 | + qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs); | |
427 | + } | |
428 | +} | |
429 | + | |
430 | +static void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting) | |
431 | +{ | |
432 | + vs->read_handler = func; | |
433 | + vs->read_handler_expect = expecting; | |
434 | +} | |
435 | + | |
436 | +static void vnc_client_read(void *opaque) | |
437 | +{ | |
438 | + VncState *vs = opaque; | |
439 | + ssize_t ret; | |
440 | + | |
441 | + buffer_reserve(&vs->input, 4096); | |
442 | + | |
443 | + ret = read(vs->csock, buffer_end(&vs->input), 4096); | |
444 | + ret = vnc_client_io_error(vs, ret); | |
445 | + if (!ret) | |
446 | + return; | |
447 | + | |
448 | + vs->input.offset += ret; | |
449 | + | |
450 | + while (vs->read_handler && vs->input.offset >= vs->read_handler_expect) { | |
451 | + size_t len = vs->read_handler_expect; | |
452 | + int ret; | |
453 | + | |
454 | + ret = vs->read_handler(vs, vs->input.buffer, len); | |
455 | + if (vs->csock == -1) | |
456 | + return; | |
457 | + | |
458 | + if (!ret) { | |
459 | + memmove(vs->input.buffer, vs->input.buffer + len, (vs->input.offset - len)); | |
460 | + vs->input.offset -= len; | |
461 | + } else { | |
462 | + vs->read_handler_expect = ret; | |
463 | + } | |
464 | + } | |
465 | +} | |
466 | + | |
467 | +static void vnc_write(VncState *vs, const void *data, size_t len) | |
468 | +{ | |
469 | + buffer_reserve(&vs->output, len); | |
470 | + | |
471 | + if (buffer_empty(&vs->output)) { | |
472 | + qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, vnc_client_write, vs); | |
473 | + } | |
474 | + | |
475 | + buffer_append(&vs->output, data, len); | |
476 | +} | |
477 | + | |
478 | +static void vnc_write_s32(VncState *vs, int32_t value) | |
479 | +{ | |
480 | + vnc_write_u32(vs, *(uint32_t *)&value); | |
481 | +} | |
482 | + | |
483 | +static void vnc_write_u32(VncState *vs, uint32_t value) | |
484 | +{ | |
485 | + uint8_t buf[4]; | |
486 | + | |
487 | + buf[0] = (value >> 24) & 0xFF; | |
488 | + buf[1] = (value >> 16) & 0xFF; | |
489 | + buf[2] = (value >> 8) & 0xFF; | |
490 | + buf[3] = value & 0xFF; | |
491 | + | |
492 | + vnc_write(vs, buf, 4); | |
493 | +} | |
494 | + | |
495 | +static void vnc_write_u16(VncState *vs, uint16_t value) | |
496 | +{ | |
497 | + char buf[2]; | |
498 | + | |
499 | + buf[0] = (value >> 8) & 0xFF; | |
500 | + buf[1] = value & 0xFF; | |
501 | + | |
502 | + vnc_write(vs, buf, 2); | |
503 | +} | |
504 | + | |
505 | +static void vnc_write_u8(VncState *vs, uint8_t value) | |
506 | +{ | |
507 | + vnc_write(vs, (char *)&value, 1); | |
508 | +} | |
509 | + | |
510 | +static void vnc_flush(VncState *vs) | |
511 | +{ | |
512 | + if (vs->output.offset) | |
513 | + vnc_client_write(vs); | |
514 | +} | |
515 | + | |
516 | +static uint8_t read_u8(char *data, size_t offset) | |
517 | +{ | |
518 | + return data[offset]; | |
519 | +} | |
520 | + | |
521 | +static uint16_t read_u16(char *data, size_t offset) | |
522 | +{ | |
523 | + return ((data[offset] & 0xFF) << 8) | (data[offset + 1] & 0xFF); | |
524 | +} | |
525 | + | |
526 | +static int32_t read_s32(char *data, size_t offset) | |
527 | +{ | |
528 | + return (int32_t)((data[offset] << 24) | (data[offset + 1] << 16) | | |
529 | + (data[offset + 2] << 8) | data[offset + 3]); | |
530 | +} | |
531 | + | |
532 | +static uint32_t read_u32(char *data, size_t offset) | |
533 | +{ | |
534 | + return ((data[offset] << 24) | (data[offset + 1] << 16) | | |
535 | + (data[offset + 2] << 8) | data[offset + 3]); | |
536 | +} | |
537 | + | |
538 | +static void client_cut_text(VncState *vs, size_t len, char *text) | |
539 | +{ | |
540 | +} | |
541 | + | |
542 | +static void pointer_event(VncState *vs, int button_mask, int x, int y) | |
543 | +{ | |
544 | + int buttons = 0; | |
545 | + int dz = 0; | |
546 | + | |
547 | + if (button_mask & 0x01) | |
548 | + buttons |= MOUSE_EVENT_LBUTTON; | |
549 | + if (button_mask & 0x02) | |
550 | + buttons |= MOUSE_EVENT_MBUTTON; | |
551 | + if (button_mask & 0x04) | |
552 | + buttons |= MOUSE_EVENT_RBUTTON; | |
553 | + if (button_mask & 0x08) | |
554 | + dz = -1; | |
555 | + if (button_mask & 0x10) | |
556 | + dz = 1; | |
557 | + | |
558 | + if (kbd_mouse_is_absolute()) { | |
559 | + kbd_mouse_event(x * 0x7FFF / vs->ds->width, | |
560 | + y * 0x7FFF / vs->ds->height, | |
561 | + dz, buttons); | |
562 | + } else { | |
563 | + static int last_x = -1; | |
564 | + static int last_y = -1; | |
565 | + | |
566 | + if (last_x != -1) | |
567 | + kbd_mouse_event(x - last_x, y - last_y, dz, buttons); | |
568 | + | |
569 | + last_x = x; | |
570 | + last_y = y; | |
571 | + } | |
572 | +} | |
573 | + | |
574 | +static void key_event(VncState *vs, int down, uint32_t sym) | |
575 | +{ | |
576 | + int keycode; | |
577 | + | |
578 | + keycode = keysym2scancode(vs->kbd_layout, sym & 0xFFFF); | |
579 | + | |
580 | + if (keycode & 0x80) | |
581 | + kbd_put_keycode(0xe0); | |
582 | + if (down) | |
583 | + kbd_put_keycode(keycode & 0x7f); | |
584 | + else | |
585 | + kbd_put_keycode(keycode | 0x80); | |
586 | +} | |
587 | + | |
588 | +static void framebuffer_update_request(VncState *vs, int incremental, | |
589 | + int x_position, int y_position, | |
590 | + int w, int h) | |
591 | +{ | |
592 | + int i; | |
593 | + vs->need_update = 1; | |
594 | + if (!incremental) { | |
595 | + char *old_row = vs->old_data + y_position * vs->ds->linesize; | |
596 | + | |
597 | + for (i = 0; i < h; i++) { | |
598 | + vs->dirty_row[y_position + i] = (1ULL << (vs->ds->width / 16)) - 1; | |
599 | + if (vs->ds->width == 1024) { | |
600 | + vs->dirty_row[y_position + i] = ~(0ULL); | |
601 | + } | |
602 | + memset(old_row, 42, vs->ds->width * vs->depth); | |
603 | + old_row += vs->ds->linesize; | |
604 | + } | |
605 | + } | |
606 | +} | |
607 | + | |
608 | +static void set_encodings(VncState *vs, int32_t *encodings, size_t n_encodings) | |
609 | +{ | |
610 | + int i; | |
611 | + | |
612 | + vs->has_hextile = 0; | |
613 | + vs->has_resize = 0; | |
614 | + vs->ds->dpy_copy = NULL; | |
615 | + | |
616 | + for (i = n_encodings - 1; i >= 0; i--) { | |
617 | + switch (encodings[i]) { | |
618 | + case 0: /* Raw */ | |
619 | + vs->has_hextile = 0; | |
620 | + break; | |
621 | + case 1: /* CopyRect */ | |
622 | + vs->ds->dpy_copy = vnc_copy; | |
623 | + break; | |
624 | + case 5: /* Hextile */ | |
625 | + vs->has_hextile = 1; | |
626 | + break; | |
627 | + case -223: /* DesktopResize */ | |
628 | + vs->has_resize = 1; | |
629 | + break; | |
630 | + default: | |
631 | + break; | |
632 | + } | |
633 | + } | |
634 | +} | |
635 | + | |
636 | +static void set_pixel_format(VncState *vs, | |
637 | + int bits_per_pixel, int depth, | |
638 | + int big_endian_flag, int true_color_flag, | |
639 | + int red_max, int green_max, int blue_max, | |
640 | + int red_shift, int green_shift, int blue_shift) | |
641 | +{ | |
642 | + switch (bits_per_pixel) { | |
643 | + case 32: | |
644 | + case 24: | |
645 | + vs->depth = 4; | |
646 | + break; | |
647 | + case 16: | |
648 | + vs->depth = 2; | |
649 | + break; | |
650 | + case 8: | |
651 | + vs->depth = 1; | |
652 | + break; | |
653 | + default: | |
654 | + vnc_client_error(vs); | |
655 | + break; | |
656 | + } | |
657 | + | |
658 | + if (!true_color_flag) | |
659 | + vnc_client_error(vs); | |
660 | + | |
661 | + vnc_dpy_resize(vs->ds, vs->ds->width, vs->ds->height); | |
662 | + memset(vs->dirty_row, 0xFF, sizeof(vs->dirty_row)); | |
663 | + memset(vs->old_data, 42, vs->ds->linesize * vs->ds->height); | |
664 | + | |
665 | + vga_hw_invalidate(); | |
666 | + vga_hw_update(); | |
667 | +} | |
668 | + | |
669 | +static int protocol_client_msg(VncState *vs, char *data, size_t len) | |
670 | +{ | |
671 | + int i; | |
672 | + uint16_t limit; | |
673 | + | |
674 | + switch (data[0]) { | |
675 | + case 0: | |
676 | + if (len == 1) | |
677 | + return 20; | |
678 | + | |
679 | + set_pixel_format(vs, read_u8(data, 4), read_u8(data, 5), | |
680 | + read_u8(data, 6), read_u8(data, 7), | |
681 | + read_u16(data, 8), read_u16(data, 10), | |
682 | + read_u16(data, 12), read_u8(data, 14), | |
683 | + read_u8(data, 15), read_u8(data, 16)); | |
684 | + break; | |
685 | + case 2: | |
686 | + if (len == 1) | |
687 | + return 4; | |
688 | + | |
689 | + if (len == 4) | |
690 | + return 4 + (read_u16(data, 2) * 4); | |
691 | + | |
692 | + limit = read_u16(data, 2); | |
693 | + for (i = 0; i < limit; i++) { | |
694 | + int32_t val = read_s32(data, 4 + (i * 4)); | |
695 | + memcpy(data + 4 + (i * 4), &val, sizeof(val)); | |
696 | + } | |
697 | + | |
698 | + set_encodings(vs, (int32_t *)(data + 4), limit); | |
699 | + break; | |
700 | + case 3: | |
701 | + if (len == 1) | |
702 | + return 10; | |
703 | + | |
704 | + framebuffer_update_request(vs, | |
705 | + read_u8(data, 1), read_u16(data, 2), read_u16(data, 4), | |
706 | + read_u16(data, 6), read_u16(data, 8)); | |
707 | + break; | |
708 | + case 4: | |
709 | + if (len == 1) | |
710 | + return 8; | |
711 | + | |
712 | + key_event(vs, read_u8(data, 1), read_u32(data, 4)); | |
713 | + break; | |
714 | + case 5: | |
715 | + if (len == 1) | |
716 | + return 6; | |
717 | + | |
718 | + pointer_event(vs, read_u8(data, 1), read_u16(data, 2), read_u16(data, 4)); | |
719 | + break; | |
720 | + case 6: | |
721 | + if (len == 1) | |
722 | + return 8; | |
723 | + | |
724 | + if (len == 8) | |
725 | + return 8 + read_u32(data, 4); | |
726 | + | |
727 | + client_cut_text(vs, read_u32(data, 4), data + 8); | |
728 | + break; | |
729 | + default: | |
730 | + printf("Msg: %d\n", data[0]); | |
731 | + vnc_client_error(vs); | |
732 | + break; | |
733 | + } | |
734 | + | |
735 | + vnc_read_when(vs, protocol_client_msg, 1); | |
736 | + return 0; | |
737 | +} | |
738 | + | |
739 | +static int protocol_client_init(VncState *vs, char *data, size_t len) | |
740 | +{ | |
741 | + char pad[3] = { 0, 0, 0 }; | |
742 | + | |
743 | + vs->width = vs->ds->width; | |
744 | + vs->height = vs->ds->height; | |
745 | + vnc_write_u16(vs, vs->ds->width); | |
746 | + vnc_write_u16(vs, vs->ds->height); | |
747 | + | |
748 | + vnc_write_u8(vs, vs->depth * 8); /* bits-per-pixel */ | |
749 | + vnc_write_u8(vs, vs->depth * 8); /* depth */ | |
750 | + vnc_write_u8(vs, 0); /* big-endian-flag */ | |
751 | + vnc_write_u8(vs, 1); /* true-color-flag */ | |
752 | + if (vs->depth == 4) { | |
753 | + vnc_write_u16(vs, 0xFF); /* red-max */ | |
754 | + vnc_write_u16(vs, 0xFF); /* green-max */ | |
755 | + vnc_write_u16(vs, 0xFF); /* blue-max */ | |
756 | + vnc_write_u8(vs, 16); /* red-shift */ | |
757 | + vnc_write_u8(vs, 8); /* green-shift */ | |
758 | + vnc_write_u8(vs, 0); /* blue-shift */ | |
759 | + } else if (vs->depth == 2) { | |
760 | + vnc_write_u16(vs, 31); /* red-max */ | |
761 | + vnc_write_u16(vs, 63); /* green-max */ | |
762 | + vnc_write_u16(vs, 31); /* blue-max */ | |
763 | + vnc_write_u8(vs, 11); /* red-shift */ | |
764 | + vnc_write_u8(vs, 5); /* green-shift */ | |
765 | + vnc_write_u8(vs, 0); /* blue-shift */ | |
766 | + } else if (vs->depth == 1) { | |
767 | + vnc_write_u16(vs, 3); /* red-max */ | |
768 | + vnc_write_u16(vs, 7); /* green-max */ | |
769 | + vnc_write_u16(vs, 3); /* blue-max */ | |
770 | + vnc_write_u8(vs, 5); /* red-shift */ | |
771 | + vnc_write_u8(vs, 2); /* green-shift */ | |
772 | + vnc_write_u8(vs, 0); /* blue-shift */ | |
773 | + } | |
774 | + | |
775 | + vnc_write(vs, pad, 3); /* padding */ | |
776 | + | |
777 | + vnc_write_u32(vs, 4); | |
778 | + vnc_write(vs, "QEMU", 4); | |
779 | + vnc_flush(vs); | |
780 | + | |
781 | + vnc_read_when(vs, protocol_client_msg, 1); | |
782 | + | |
783 | + return 0; | |
784 | +} | |
785 | + | |
786 | +static int protocol_version(VncState *vs, char *version, size_t len) | |
787 | +{ | |
788 | + char local[13]; | |
789 | + int maj, min; | |
790 | + | |
791 | + memcpy(local, version, 12); | |
792 | + local[12] = 0; | |
793 | + | |
794 | + if (sscanf(local, "RFB %03d.%03d\n", &maj, &min) != 2) { | |
795 | + vnc_client_error(vs); | |
796 | + return 0; | |
797 | + } | |
798 | + | |
799 | + vnc_write_u32(vs, 1); /* None */ | |
800 | + vnc_flush(vs); | |
801 | + | |
802 | + vnc_read_when(vs, protocol_client_init, 1); | |
803 | + | |
804 | + return 0; | |
805 | +} | |
806 | + | |
807 | +static void vnc_listen_read(void *opaque) | |
808 | +{ | |
809 | + VncState *vs = opaque; | |
810 | + struct sockaddr_in addr; | |
811 | + socklen_t addrlen = sizeof(addr); | |
812 | + | |
813 | + vs->csock = accept(vs->lsock, (struct sockaddr *)&addr, &addrlen); | |
814 | + if (vs->csock != -1) { | |
815 | + fcntl(vs->csock, F_SETFL, O_NONBLOCK); | |
816 | + qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, opaque); | |
817 | + vnc_write(vs, "RFB 003.003\n", 12); | |
818 | + vnc_flush(vs); | |
819 | + vnc_read_when(vs, protocol_version, 12); | |
820 | + memset(vs->old_data, 0, vs->ds->linesize * vs->ds->height); | |
821 | + memset(vs->dirty_row, 0xFF, sizeof(vs->dirty_row)); | |
822 | + vs->has_resize = 0; | |
823 | + vs->has_hextile = 0; | |
824 | + vs->ds->dpy_copy = NULL; | |
825 | + } | |
826 | +} | |
827 | + | |
828 | +void vnc_display_init(DisplayState *ds, int display) | |
829 | +{ | |
830 | + struct sockaddr_in addr; | |
831 | + int reuse_addr, ret; | |
832 | + VncState *vs; | |
833 | + | |
834 | + vs = qemu_mallocz(sizeof(VncState)); | |
835 | + if (!vs) | |
836 | + exit(1); | |
837 | + | |
838 | + ds->opaque = vs; | |
839 | + | |
840 | + vs->lsock = -1; | |
841 | + vs->csock = -1; | |
842 | + vs->depth = 4; | |
843 | + | |
844 | + vs->ds = ds; | |
845 | + | |
846 | + if (!keyboard_layout) | |
847 | + keyboard_layout = "en-us"; | |
848 | + | |
849 | + vs->kbd_layout = init_keyboard_layout(keyboard_layout); | |
850 | + if (!vs->kbd_layout) | |
851 | + exit(1); | |
852 | + | |
853 | + vs->lsock = socket(PF_INET, SOCK_STREAM, 0); | |
854 | + if (vs->lsock == -1) { | |
855 | + fprintf(stderr, "Could not create socket\n"); | |
856 | + exit(1); | |
857 | + } | |
858 | + | |
859 | + addr.sin_family = AF_INET; | |
860 | + addr.sin_port = htons(5900 + display); | |
861 | + memset(&addr.sin_addr, 0, sizeof(addr.sin_addr)); | |
862 | + | |
863 | + reuse_addr = 1; | |
864 | + ret = setsockopt(vs->lsock, SOL_SOCKET, SO_REUSEADDR, | |
865 | + &reuse_addr, sizeof(reuse_addr)); | |
866 | + if (ret == -1) { | |
867 | + fprintf(stderr, "setsockopt() failed\n"); | |
868 | + exit(1); | |
869 | + } | |
870 | + | |
871 | + if (bind(vs->lsock, (struct sockaddr *)&addr, sizeof(addr)) == -1) { | |
872 | + fprintf(stderr, "bind() failed\n"); | |
873 | + exit(1); | |
874 | + } | |
875 | + | |
876 | + if (listen(vs->lsock, 1) == -1) { | |
877 | + fprintf(stderr, "listen() failed\n"); | |
878 | + exit(1); | |
879 | + } | |
880 | + | |
881 | + ret = qemu_set_fd_handler2(vs->lsock, vnc_listen_poll, vnc_listen_read, NULL, vs); | |
882 | + if (ret == -1) { | |
883 | + exit(1); | |
884 | + } | |
885 | + | |
886 | + vs->ds->data = NULL; | |
887 | + vs->ds->dpy_update = vnc_dpy_update; | |
888 | + vs->ds->dpy_resize = vnc_dpy_resize; | |
889 | + vs->ds->dpy_refresh = vnc_dpy_refresh; | |
890 | + | |
891 | + memset(vs->dirty_row, 0xFF, sizeof(vs->dirty_row)); | |
892 | + | |
893 | + vnc_dpy_resize(vs->ds, 640, 400); | |
894 | +} | ... | ... |
vnc_keysym.h
0 โ 100644
1 | +typedef struct { | |
2 | + const char* name; | |
3 | + int keysym; | |
4 | +} name2keysym_t; | |
5 | +static name2keysym_t name2keysym[]={ | |
6 | +/* ascii */ | |
7 | + { "space", 0x020}, | |
8 | + { "exclam", 0x021}, | |
9 | + { "quotedbl", 0x022}, | |
10 | + { "numbersign", 0x023}, | |
11 | + { "dollar", 0x024}, | |
12 | + { "percent", 0x025}, | |
13 | + { "ampersand", 0x026}, | |
14 | + { "apostrophe", 0x027}, | |
15 | + { "parenleft", 0x028}, | |
16 | + { "parenright", 0x029}, | |
17 | + { "asterisk", 0x02a}, | |
18 | + { "plus", 0x02b}, | |
19 | + { "comma", 0x02c}, | |
20 | + { "minus", 0x02d}, | |
21 | + { "period", 0x02e}, | |
22 | + { "slash", 0x02f}, | |
23 | + { "0", 0x030}, | |
24 | + { "1", 0x031}, | |
25 | + { "2", 0x032}, | |
26 | + { "3", 0x033}, | |
27 | + { "4", 0x034}, | |
28 | + { "5", 0x035}, | |
29 | + { "6", 0x036}, | |
30 | + { "7", 0x037}, | |
31 | + { "8", 0x038}, | |
32 | + { "9", 0x039}, | |
33 | + { "colon", 0x03a}, | |
34 | + { "semicolon", 0x03b}, | |
35 | + { "less", 0x03c}, | |
36 | + { "equal", 0x03d}, | |
37 | + { "greater", 0x03e}, | |
38 | + { "question", 0x03f}, | |
39 | + { "at", 0x040}, | |
40 | + { "A", 0x041}, | |
41 | + { "B", 0x042}, | |
42 | + { "C", 0x043}, | |
43 | + { "D", 0x044}, | |
44 | + { "E", 0x045}, | |
45 | + { "F", 0x046}, | |
46 | + { "G", 0x047}, | |
47 | + { "H", 0x048}, | |
48 | + { "I", 0x049}, | |
49 | + { "J", 0x04a}, | |
50 | + { "K", 0x04b}, | |
51 | + { "L", 0x04c}, | |
52 | + { "M", 0x04d}, | |
53 | + { "N", 0x04e}, | |
54 | + { "O", 0x04f}, | |
55 | + { "P", 0x050}, | |
56 | + { "Q", 0x051}, | |
57 | + { "R", 0x052}, | |
58 | + { "S", 0x053}, | |
59 | + { "T", 0x054}, | |
60 | + { "U", 0x055}, | |
61 | + { "V", 0x056}, | |
62 | + { "W", 0x057}, | |
63 | + { "X", 0x058}, | |
64 | + { "Y", 0x059}, | |
65 | + { "Z", 0x05a}, | |
66 | + { "bracketleft", 0x05b}, | |
67 | + { "backslash", 0x05c}, | |
68 | + { "bracketright", 0x05d}, | |
69 | + { "asciicircum", 0x05e}, | |
70 | + { "underscore", 0x05f}, | |
71 | + { "grave", 0x060}, | |
72 | + { "a", 0x061}, | |
73 | + { "b", 0x062}, | |
74 | + { "c", 0x063}, | |
75 | + { "d", 0x064}, | |
76 | + { "e", 0x065}, | |
77 | + { "f", 0x066}, | |
78 | + { "g", 0x067}, | |
79 | + { "h", 0x068}, | |
80 | + { "i", 0x069}, | |
81 | + { "j", 0x06a}, | |
82 | + { "k", 0x06b}, | |
83 | + { "l", 0x06c}, | |
84 | + { "m", 0x06d}, | |
85 | + { "n", 0x06e}, | |
86 | + { "o", 0x06f}, | |
87 | + { "p", 0x070}, | |
88 | + { "q", 0x071}, | |
89 | + { "r", 0x072}, | |
90 | + { "s", 0x073}, | |
91 | + { "t", 0x074}, | |
92 | + { "u", 0x075}, | |
93 | + { "v", 0x076}, | |
94 | + { "w", 0x077}, | |
95 | + { "x", 0x078}, | |
96 | + { "y", 0x079}, | |
97 | + { "z", 0x07a}, | |
98 | + { "braceleft", 0x07b}, | |
99 | + { "bar", 0x07c}, | |
100 | + { "braceright", 0x07d}, | |
101 | + { "asciitilde", 0x07e}, | |
102 | + | |
103 | +/* latin 1 extensions */ | |
104 | +{ "nobreakspace", 0x0a0}, | |
105 | +{ "exclamdown", 0x0a1}, | |
106 | +{ "cent", 0x0a2}, | |
107 | +{ "sterling", 0x0a3}, | |
108 | +{ "currency", 0x0a4}, | |
109 | +{ "yen", 0x0a5}, | |
110 | +{ "brokenbar", 0x0a6}, | |
111 | +{ "section", 0x0a7}, | |
112 | +{ "diaeresis", 0x0a8}, | |
113 | +{ "copyright", 0x0a9}, | |
114 | +{ "ordfeminine", 0x0aa}, | |
115 | +{ "guillemotleft", 0x0ab}, | |
116 | +{ "notsign", 0x0ac}, | |
117 | +{ "hyphen", 0x0ad}, | |
118 | +{ "registered", 0x0ae}, | |
119 | +{ "macron", 0x0af}, | |
120 | +{ "degree", 0x0b0}, | |
121 | +{ "plusminus", 0x0b1}, | |
122 | +{ "twosuperior", 0x0b2}, | |
123 | +{ "threesuperior", 0x0b3}, | |
124 | +{ "acute", 0x0b4}, | |
125 | +{ "mu", 0x0b5}, | |
126 | +{ "paragraph", 0x0b6}, | |
127 | +{ "periodcentered", 0x0b7}, | |
128 | +{ "cedilla", 0x0b8}, | |
129 | +{ "onesuperior", 0x0b9}, | |
130 | +{ "masculine", 0x0ba}, | |
131 | +{ "guillemotright", 0x0bb}, | |
132 | +{ "onequarter", 0x0bc}, | |
133 | +{ "onehalf", 0x0bd}, | |
134 | +{ "threequarters", 0x0be}, | |
135 | +{ "questiondown", 0x0bf}, | |
136 | +{ "Agrave", 0x0c0}, | |
137 | +{ "Aacute", 0x0c1}, | |
138 | +{ "Acircumflex", 0x0c2}, | |
139 | +{ "Atilde", 0x0c3}, | |
140 | +{ "Adiaeresis", 0x0c4}, | |
141 | +{ "Aring", 0x0c5}, | |
142 | +{ "AE", 0x0c6}, | |
143 | +{ "Ccedilla", 0x0c7}, | |
144 | +{ "Egrave", 0x0c8}, | |
145 | +{ "Eacute", 0x0c9}, | |
146 | +{ "Ecircumflex", 0x0ca}, | |
147 | +{ "Ediaeresis", 0x0cb}, | |
148 | +{ "Igrave", 0x0cc}, | |
149 | +{ "Iacute", 0x0cd}, | |
150 | +{ "Icircumflex", 0x0ce}, | |
151 | +{ "Idiaeresis", 0x0cf}, | |
152 | +{ "ETH", 0x0d0}, | |
153 | +{ "Eth", 0x0d0}, | |
154 | +{ "Ntilde", 0x0d1}, | |
155 | +{ "Ograve", 0x0d2}, | |
156 | +{ "Oacute", 0x0d3}, | |
157 | +{ "Ocircumflex", 0x0d4}, | |
158 | +{ "Otilde", 0x0d5}, | |
159 | +{ "Odiaeresis", 0x0d6}, | |
160 | +{ "multiply", 0x0d7}, | |
161 | +{ "Ooblique", 0x0d8}, | |
162 | +{ "Oslash", 0x0d8}, | |
163 | +{ "Ugrave", 0x0d9}, | |
164 | +{ "Uacute", 0x0da}, | |
165 | +{ "Ucircumflex", 0x0db}, | |
166 | +{ "Udiaeresis", 0x0dc}, | |
167 | +{ "Yacute", 0x0dd}, | |
168 | +{ "THORN", 0x0de}, | |
169 | +{ "Thorn", 0x0de}, | |
170 | +{ "ssharp", 0x0df}, | |
171 | +{ "agrave", 0x0e0}, | |
172 | +{ "aacute", 0x0e1}, | |
173 | +{ "acircumflex", 0x0e2}, | |
174 | +{ "atilde", 0x0e3}, | |
175 | +{ "adiaeresis", 0x0e4}, | |
176 | +{ "aring", 0x0e5}, | |
177 | +{ "ae", 0x0e6}, | |
178 | +{ "ccedilla", 0x0e7}, | |
179 | +{ "egrave", 0x0e8}, | |
180 | +{ "eacute", 0x0e9}, | |
181 | +{ "ecircumflex", 0x0ea}, | |
182 | +{ "ediaeresis", 0x0eb}, | |
183 | +{ "igrave", 0x0ec}, | |
184 | +{ "iacute", 0x0ed}, | |
185 | +{ "icircumflex", 0x0ee}, | |
186 | +{ "idiaeresis", 0x0ef}, | |
187 | +{ "eth", 0x0f0}, | |
188 | +{ "ntilde", 0x0f1}, | |
189 | +{ "ograve", 0x0f2}, | |
190 | +{ "oacute", 0x0f3}, | |
191 | +{ "ocircumflex", 0x0f4}, | |
192 | +{ "otilde", 0x0f5}, | |
193 | +{ "odiaeresis", 0x0f6}, | |
194 | +{ "division", 0x0f7}, | |
195 | +{ "oslash", 0x0f8}, | |
196 | +{ "ooblique", 0x0f8}, | |
197 | +{ "ugrave", 0x0f9}, | |
198 | +{ "uacute", 0x0fa}, | |
199 | +{ "ucircumflex", 0x0fb}, | |
200 | +{ "udiaeresis", 0x0fc}, | |
201 | +{ "yacute", 0x0fd}, | |
202 | +{ "thorn", 0x0fe}, | |
203 | +{ "ydiaeresis", 0x0ff}, | |
204 | +{"EuroSign", 0x20ac}, /* XK_EuroSign */ | |
205 | + | |
206 | + /* modifiers */ | |
207 | +{"Control_L", 0xffe3}, /* XK_Control_L */ | |
208 | +{"Control_R", 0xffe4}, /* XK_Control_R */ | |
209 | +{"Alt_L", 0xffe9}, /* XK_Alt_L */ | |
210 | +{"Alt_R", 0xffea}, /* XK_Alt_R */ | |
211 | +{"Caps_Lock", 0xffe5}, /* XK_Caps_Lock */ | |
212 | +{"Meta_L", 0xffe7}, /* XK_Meta_L */ | |
213 | +{"Meta_R", 0xffe8}, /* XK_Meta_R */ | |
214 | +{"Shift_L", 0xffe1}, /* XK_Shift_L */ | |
215 | +{"Shift_R", 0xffe2}, /* XK_Shift_R */ | |
216 | +{"Super_L", 0xffeb}, /* XK_Super_L */ | |
217 | +{"Super_R", 0xffec}, /* XK_Super_R */ | |
218 | + | |
219 | + /* special keys */ | |
220 | +{"BackSpace", 0xff08}, /* XK_BackSpace */ | |
221 | +{"Tab", 0xff09}, /* XK_Tab */ | |
222 | +{"Return", 0xff0d}, /* XK_Return */ | |
223 | +{"Right", 0xff53}, /* XK_Right */ | |
224 | +{"Left", 0xff51}, /* XK_Left */ | |
225 | +{"Up", 0xff52}, /* XK_Up */ | |
226 | +{"Down", 0xff54}, /* XK_Down */ | |
227 | +{"Page_Down", 0xff56}, /* XK_Page_Down */ | |
228 | +{"Page_Up", 0xff55}, /* XK_Page_Up */ | |
229 | +{"Insert", 0xff63}, /* XK_Insert */ | |
230 | +{"Delete", 0xffff}, /* XK_Delete */ | |
231 | +{"Home", 0xff50}, /* XK_Home */ | |
232 | +{"End", 0xff57}, /* XK_End */ | |
233 | +{"Scroll_Lock", 0xff14}, /* XK_Scroll_Lock */ | |
234 | +{"F1", 0xffbe}, /* XK_F1 */ | |
235 | +{"F2", 0xffbf}, /* XK_F2 */ | |
236 | +{"F3", 0xffc0}, /* XK_F3 */ | |
237 | +{"F4", 0xffc1}, /* XK_F4 */ | |
238 | +{"F5", 0xffc2}, /* XK_F5 */ | |
239 | +{"F6", 0xffc3}, /* XK_F6 */ | |
240 | +{"F7", 0xffc4}, /* XK_F7 */ | |
241 | +{"F8", 0xffc5}, /* XK_F8 */ | |
242 | +{"F9", 0xffc6}, /* XK_F9 */ | |
243 | +{"F10", 0xffc7}, /* XK_F10 */ | |
244 | +{"F11", 0xffc8}, /* XK_F11 */ | |
245 | +{"F12", 0xffc9}, /* XK_F12 */ | |
246 | +{"F13", 0xffca}, /* XK_F13 */ | |
247 | +{"F14", 0xffcb}, /* XK_F14 */ | |
248 | +{"F15", 0xffcc}, /* XK_F15 */ | |
249 | +{"Sys_Req", 0xff15}, /* XK_Sys_Req */ | |
250 | +{"KP_0", 0xffb0}, /* XK_KP_0 */ | |
251 | +{"KP_1", 0xffb1}, /* XK_KP_1 */ | |
252 | +{"KP_2", 0xffb2}, /* XK_KP_2 */ | |
253 | +{"KP_3", 0xffb3}, /* XK_KP_3 */ | |
254 | +{"KP_4", 0xffb4}, /* XK_KP_4 */ | |
255 | +{"KP_5", 0xffb5}, /* XK_KP_5 */ | |
256 | +{"KP_6", 0xffb6}, /* XK_KP_6 */ | |
257 | +{"KP_7", 0xffb7}, /* XK_KP_7 */ | |
258 | +{"KP_8", 0xffb8}, /* XK_KP_8 */ | |
259 | +{"KP_9", 0xffb9}, /* XK_KP_9 */ | |
260 | +{"KP_Add", 0xffab}, /* XK_KP_Add */ | |
261 | +{"KP_Decimal", 0xffae}, /* XK_KP_Decimal */ | |
262 | +{"KP_Divide", 0xffaf}, /* XK_KP_Divide */ | |
263 | +{"KP_Enter", 0xff8d}, /* XK_KP_Enter */ | |
264 | +{"KP_Equal", 0xffbd}, /* XK_KP_Equal */ | |
265 | +{"KP_Multiply", 0xffaa}, /* XK_KP_Multiply */ | |
266 | +{"KP_Subtract", 0xffad}, /* XK_KP_Subtract */ | |
267 | +{"help", 0xff6a}, /* XK_Help */ | |
268 | +{"Menu", 0xff67}, /* XK_Menu */ | |
269 | +{"Print", 0xff61}, /* XK_Print */ | |
270 | +{"Mode_switch", 0xff7e}, /* XK_Mode_switch */ | |
271 | +{"Num_Lock", 0xff7f}, /* XK_Num_Lock */ | |
272 | +{"Pause", 0xff13}, /* XK_Pause */ | |
273 | +{"Escape", 0xff1b}, /* XK_Escape */ | |
274 | +{0,0}, | |
275 | +}; | ... | ... |
vnchextile.h
0 โ 100644
1 | +#define CONCAT_I(a, b) a ## b | |
2 | +#define CONCAT(a, b) CONCAT_I(a, b) | |
3 | +#define pixel_t CONCAT(uint, CONCAT(BPP, _t)) | |
4 | + | |
5 | +static void CONCAT(send_hextile_tile_, BPP)(VncState *vs, | |
6 | + int x, int y, int w, int h, | |
7 | + pixel_t *last_bg, pixel_t *last_fg, | |
8 | + int *has_bg, int *has_fg) | |
9 | +{ | |
10 | + char *row = (vs->ds->data + y * vs->ds->linesize + x * vs->depth); | |
11 | + pixel_t *irow = (pixel_t *)row; | |
12 | + int j, i; | |
13 | + pixel_t bg = 0; | |
14 | + pixel_t fg = 0; | |
15 | + int n_colors = 0; | |
16 | + int bg_count = 0; | |
17 | + int fg_count = 0; | |
18 | + int flags = 0; | |
19 | + uint8_t data[(sizeof(pixel_t) + 2) * 16 * 16]; | |
20 | + int n_data = 0; | |
21 | + int n_subtiles = 0; | |
22 | + | |
23 | + for (j = 0; j < h; j++) { | |
24 | + for (i = 0; i < w; i++) { | |
25 | + switch (n_colors) { | |
26 | + case 0: | |
27 | + bg = irow[i]; | |
28 | + n_colors = 1; | |
29 | + break; | |
30 | + case 1: | |
31 | + if (irow[i] != bg) { | |
32 | + fg = irow[i]; | |
33 | + n_colors = 2; | |
34 | + } | |
35 | + break; | |
36 | + case 2: | |
37 | + if (irow[i] != bg && irow[i] != fg) { | |
38 | + n_colors = 3; | |
39 | + } else { | |
40 | + if (irow[i] == bg) | |
41 | + bg_count++; | |
42 | + else if (irow[i] == fg) | |
43 | + fg_count++; | |
44 | + } | |
45 | + break; | |
46 | + default: | |
47 | + break; | |
48 | + } | |
49 | + } | |
50 | + if (n_colors > 2) | |
51 | + break; | |
52 | + irow += vs->ds->linesize / sizeof(pixel_t); | |
53 | + } | |
54 | + | |
55 | + if (n_colors > 1 && fg_count > bg_count) { | |
56 | + pixel_t tmp = fg; | |
57 | + fg = bg; | |
58 | + bg = tmp; | |
59 | + } | |
60 | + | |
61 | + if (!*has_bg || *last_bg != bg) { | |
62 | + flags |= 0x02; | |
63 | + *has_bg = 1; | |
64 | + *last_bg = bg; | |
65 | + } | |
66 | + | |
67 | + if (!*has_fg || *last_fg != fg) { | |
68 | + flags |= 0x04; | |
69 | + *has_fg = 1; | |
70 | + *last_fg = fg; | |
71 | + } | |
72 | + | |
73 | + switch (n_colors) { | |
74 | + case 1: | |
75 | + n_data = 0; | |
76 | + break; | |
77 | + case 2: | |
78 | + flags |= 0x08; | |
79 | + | |
80 | + irow = (pixel_t *)row; | |
81 | + | |
82 | + for (j = 0; j < h; j++) { | |
83 | + int min_x = -1; | |
84 | + for (i = 0; i < w; i++) { | |
85 | + if (irow[i] == fg) { | |
86 | + if (min_x == -1) | |
87 | + min_x = i; | |
88 | + } else if (min_x != -1) { | |
89 | + hextile_enc_cord(data + n_data, min_x, j, i - min_x, 1); | |
90 | + n_data += 2; | |
91 | + n_subtiles++; | |
92 | + min_x = -1; | |
93 | + } | |
94 | + } | |
95 | + if (min_x != -1) { | |
96 | + hextile_enc_cord(data + n_data, min_x, j, i - min_x, 1); | |
97 | + n_data += 2; | |
98 | + n_subtiles++; | |
99 | + } | |
100 | + irow += vs->ds->linesize / sizeof(pixel_t); | |
101 | + } | |
102 | + break; | |
103 | + case 3: | |
104 | + flags |= 0x18; | |
105 | + | |
106 | + irow = (pixel_t *)row; | |
107 | + | |
108 | + if (!*has_bg || *last_bg != bg) | |
109 | + flags |= 0x02; | |
110 | + | |
111 | + for (j = 0; j < h; j++) { | |
112 | + int has_color = 0; | |
113 | + int min_x = -1; | |
114 | + pixel_t color; | |
115 | + | |
116 | + for (i = 0; i < w; i++) { | |
117 | + if (!has_color) { | |
118 | + if (irow[i] == bg) | |
119 | + continue; | |
120 | + color = irow[i]; | |
121 | + min_x = i; | |
122 | + has_color = 1; | |
123 | + } else if (irow[i] != color) { | |
124 | + has_color = 0; | |
125 | + | |
126 | + memcpy(data + n_data, &color, sizeof(color)); | |
127 | + hextile_enc_cord(data + n_data + sizeof(pixel_t), min_x, j, i - min_x, 1); | |
128 | + n_data += 2 + sizeof(pixel_t); | |
129 | + n_subtiles++; | |
130 | + | |
131 | + min_x = -1; | |
132 | + if (irow[i] != bg) { | |
133 | + color = irow[i]; | |
134 | + min_x = i; | |
135 | + has_color = 1; | |
136 | + } | |
137 | + } | |
138 | + } | |
139 | + if (has_color) { | |
140 | + memcpy(data + n_data, &color, sizeof(color)); | |
141 | + hextile_enc_cord(data + n_data + sizeof(pixel_t), min_x, j, i - min_x, 1); | |
142 | + n_data += 2 + sizeof(pixel_t); | |
143 | + n_subtiles++; | |
144 | + } | |
145 | + irow += vs->ds->linesize / sizeof(pixel_t); | |
146 | + } | |
147 | + | |
148 | + /* A SubrectsColoured subtile invalidates the foreground color */ | |
149 | + *has_fg = 0; | |
150 | + if (n_data > (w * h * sizeof(pixel_t))) { | |
151 | + n_colors = 4; | |
152 | + flags = 0x01; | |
153 | + *has_bg = 0; | |
154 | + | |
155 | + /* we really don't have to invalidate either the bg or fg | |
156 | + but we've lost the old values. oh well. */ | |
157 | + } | |
158 | + default: | |
159 | + break; | |
160 | + } | |
161 | + | |
162 | + if (n_colors > 3) { | |
163 | + flags = 0x01; | |
164 | + *has_fg = 0; | |
165 | + *has_bg = 0; | |
166 | + n_colors = 4; | |
167 | + } | |
168 | + | |
169 | + vnc_write_u8(vs, flags); | |
170 | + if (n_colors < 4) { | |
171 | + if (flags & 0x02) | |
172 | + vnc_write(vs, last_bg, sizeof(pixel_t)); | |
173 | + if (flags & 0x04) | |
174 | + vnc_write(vs, last_fg, sizeof(pixel_t)); | |
175 | + if (n_subtiles) { | |
176 | + vnc_write_u8(vs, n_subtiles); | |
177 | + vnc_write(vs, data, n_data); | |
178 | + } | |
179 | + } else { | |
180 | + for (j = 0; j < h; j++) { | |
181 | + vnc_write(vs, row, w * vs->depth); | |
182 | + row += vs->ds->linesize; | |
183 | + } | |
184 | + } | |
185 | +} | |
186 | + | |
187 | +#undef pixel_t | |
188 | +#undef CONCAT_I | |
189 | +#undef CONCAT | ... | ... |