root / vnc.c @ c5d6edc3
History | View | Annotate | Download (26.3 kB)
1 | 7d510b8c | bellard | /*
|
---|---|---|---|
2 | 7d510b8c | bellard | * QEMU VNC display driver
|
3 | 7d510b8c | bellard | *
|
4 | 7d510b8c | bellard | * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
|
5 | 7d510b8c | bellard | * Copyright (C) 2006 Fabrice Bellard
|
6 | 7d510b8c | bellard | *
|
7 | 7d510b8c | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
8 | 7d510b8c | bellard | * of this software and associated documentation files (the "Software"), to deal
|
9 | 7d510b8c | bellard | * in the Software without restriction, including without limitation the rights
|
10 | 7d510b8c | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11 | 7d510b8c | bellard | * copies of the Software, and to permit persons to whom the Software is
|
12 | 7d510b8c | bellard | * furnished to do so, subject to the following conditions:
|
13 | 7d510b8c | bellard | *
|
14 | 7d510b8c | bellard | * The above copyright notice and this permission notice shall be included in
|
15 | 7d510b8c | bellard | * all copies or substantial portions of the Software.
|
16 | 7d510b8c | bellard | *
|
17 | 7d510b8c | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
18 | 7d510b8c | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
19 | 7d510b8c | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
20 | 7d510b8c | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
21 | 7d510b8c | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
22 | 7d510b8c | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
23 | 7d510b8c | bellard | * THE SOFTWARE.
|
24 | 7d510b8c | bellard | */
|
25 | 7d510b8c | bellard | |
26 | 24236869 | bellard | #include "vl.h" |
27 | 6ca957f0 | bellard | #include "qemu_socket.h" |
28 | 24236869 | bellard | |
29 | 24236869 | bellard | #define VNC_REFRESH_INTERVAL (1000 / 30) |
30 | 24236869 | bellard | |
31 | 24236869 | bellard | #include "vnc_keysym.h" |
32 | 24236869 | bellard | #include "keymaps.c" |
33 | 24236869 | bellard | |
34 | 24236869 | bellard | typedef struct Buffer |
35 | 24236869 | bellard | { |
36 | 24236869 | bellard | size_t capacity; |
37 | 24236869 | bellard | size_t offset; |
38 | 24236869 | bellard | char *buffer;
|
39 | 24236869 | bellard | } Buffer; |
40 | 24236869 | bellard | |
41 | 24236869 | bellard | typedef struct VncState VncState; |
42 | 24236869 | bellard | |
43 | 24236869 | bellard | typedef int VncReadEvent(VncState *vs, char *data, size_t len); |
44 | 24236869 | bellard | |
45 | 3512779a | bellard | typedef void VncWritePixels(VncState *vs, void *data, int size); |
46 | 3512779a | bellard | |
47 | 3512779a | bellard | typedef void VncSendHextileTile(VncState *vs, |
48 | 3512779a | bellard | int x, int y, int w, int h, |
49 | 3512779a | bellard | uint32_t *last_bg, |
50 | 3512779a | bellard | uint32_t *last_fg, |
51 | 3512779a | bellard | int *has_bg, int *has_fg); |
52 | 3512779a | bellard | |
53 | 99589bdc | bellard | #define VNC_MAX_WIDTH 2048 |
54 | 99589bdc | bellard | #define VNC_MAX_HEIGHT 2048 |
55 | 99589bdc | bellard | #define VNC_DIRTY_WORDS (VNC_MAX_WIDTH / (16 * 32)) |
56 | 99589bdc | bellard | |
57 | 24236869 | bellard | struct VncState
|
58 | 24236869 | bellard | { |
59 | 24236869 | bellard | QEMUTimer *timer; |
60 | 24236869 | bellard | int lsock;
|
61 | 24236869 | bellard | int csock;
|
62 | 24236869 | bellard | DisplayState *ds; |
63 | 24236869 | bellard | int need_update;
|
64 | 24236869 | bellard | int width;
|
65 | 24236869 | bellard | int height;
|
66 | 99589bdc | bellard | uint32_t dirty_row[VNC_MAX_HEIGHT][VNC_DIRTY_WORDS]; |
67 | 24236869 | bellard | char *old_data;
|
68 | 3512779a | bellard | int depth; /* internal VNC frame buffer byte per pixel */ |
69 | 24236869 | bellard | int has_resize;
|
70 | 24236869 | bellard | int has_hextile;
|
71 | 24236869 | bellard | Buffer output; |
72 | 24236869 | bellard | Buffer input; |
73 | 24236869 | bellard | kbd_layout_t *kbd_layout; |
74 | 3512779a | bellard | /* current output mode information */
|
75 | 3512779a | bellard | VncWritePixels *write_pixels; |
76 | 3512779a | bellard | VncSendHextileTile *send_hextile_tile; |
77 | 3512779a | bellard | int pix_bpp, pix_big_endian;
|
78 | 3512779a | bellard | int red_shift, red_max, red_shift1;
|
79 | 3512779a | bellard | int green_shift, green_max, green_shift1;
|
80 | 3512779a | bellard | int blue_shift, blue_max, blue_shift1;
|
81 | 24236869 | bellard | |
82 | 24236869 | bellard | VncReadEvent *read_handler; |
83 | 24236869 | bellard | size_t read_handler_expect; |
84 | 24236869 | bellard | }; |
85 | 24236869 | bellard | |
86 | 24236869 | bellard | /* TODO
|
87 | 24236869 | bellard | 1) Get the queue working for IO.
|
88 | 24236869 | bellard | 2) there is some weirdness when using the -S option (the screen is grey
|
89 | 24236869 | bellard | and not totally invalidated
|
90 | 24236869 | bellard | 3) resolutions > 1024
|
91 | 24236869 | bellard | */
|
92 | 24236869 | bellard | |
93 | 24236869 | bellard | static void vnc_write(VncState *vs, const void *data, size_t len); |
94 | 24236869 | bellard | static void vnc_write_u32(VncState *vs, uint32_t value); |
95 | 24236869 | bellard | static void vnc_write_s32(VncState *vs, int32_t value); |
96 | 24236869 | bellard | static void vnc_write_u16(VncState *vs, uint16_t value); |
97 | 24236869 | bellard | static void vnc_write_u8(VncState *vs, uint8_t value); |
98 | 24236869 | bellard | static void vnc_flush(VncState *vs); |
99 | 24236869 | bellard | static void vnc_update_client(void *opaque); |
100 | 24236869 | bellard | static void vnc_client_read(void *opaque); |
101 | 24236869 | bellard | |
102 | 99589bdc | bellard | static inline void vnc_set_bit(uint32_t *d, int k) |
103 | 99589bdc | bellard | { |
104 | 99589bdc | bellard | d[k >> 5] |= 1 << (k & 0x1f); |
105 | 99589bdc | bellard | } |
106 | 99589bdc | bellard | |
107 | 99589bdc | bellard | static inline void vnc_clear_bit(uint32_t *d, int k) |
108 | 99589bdc | bellard | { |
109 | 99589bdc | bellard | d[k >> 5] &= ~(1 << (k & 0x1f)); |
110 | 99589bdc | bellard | } |
111 | 99589bdc | bellard | |
112 | 99589bdc | bellard | static inline void vnc_set_bits(uint32_t *d, int n, int nb_words) |
113 | 99589bdc | bellard | { |
114 | 99589bdc | bellard | int j;
|
115 | 99589bdc | bellard | |
116 | 99589bdc | bellard | j = 0;
|
117 | 99589bdc | bellard | while (n >= 32) { |
118 | 99589bdc | bellard | d[j++] = -1;
|
119 | 99589bdc | bellard | n -= 32;
|
120 | 99589bdc | bellard | } |
121 | 99589bdc | bellard | if (n > 0) |
122 | 99589bdc | bellard | d[j++] = (1 << n) - 1; |
123 | 99589bdc | bellard | while (j < nb_words)
|
124 | 99589bdc | bellard | d[j++] = 0;
|
125 | 99589bdc | bellard | } |
126 | 99589bdc | bellard | |
127 | 99589bdc | bellard | static inline int vnc_get_bit(const uint32_t *d, int k) |
128 | 99589bdc | bellard | { |
129 | 99589bdc | bellard | return (d[k >> 5] >> (k & 0x1f)) & 1; |
130 | 99589bdc | bellard | } |
131 | 99589bdc | bellard | |
132 | 99589bdc | bellard | static inline int vnc_and_bits(const uint32_t *d1, const uint32_t *d2, |
133 | 99589bdc | bellard | int nb_words)
|
134 | 99589bdc | bellard | { |
135 | 99589bdc | bellard | int i;
|
136 | 99589bdc | bellard | for(i = 0; i < nb_words; i++) { |
137 | 99589bdc | bellard | if ((d1[i] & d2[i]) != 0) |
138 | 99589bdc | bellard | return 1; |
139 | 99589bdc | bellard | } |
140 | 99589bdc | bellard | return 0; |
141 | 99589bdc | bellard | } |
142 | 99589bdc | bellard | |
143 | 24236869 | bellard | static void vnc_dpy_update(DisplayState *ds, int x, int y, int w, int h) |
144 | 24236869 | bellard | { |
145 | 24236869 | bellard | VncState *vs = ds->opaque; |
146 | 24236869 | bellard | int i;
|
147 | 24236869 | bellard | |
148 | 24236869 | bellard | h += y; |
149 | 24236869 | bellard | |
150 | 24236869 | bellard | for (; y < h; y++)
|
151 | 24236869 | bellard | for (i = 0; i < w; i += 16) |
152 | 99589bdc | bellard | vnc_set_bit(vs->dirty_row[y], (x + i) / 16);
|
153 | 24236869 | bellard | } |
154 | 24236869 | bellard | |
155 | 24236869 | bellard | static void vnc_framebuffer_update(VncState *vs, int x, int y, int w, int h, |
156 | 24236869 | bellard | int32_t encoding) |
157 | 24236869 | bellard | { |
158 | 24236869 | bellard | vnc_write_u16(vs, x); |
159 | 24236869 | bellard | vnc_write_u16(vs, y); |
160 | 24236869 | bellard | vnc_write_u16(vs, w); |
161 | 24236869 | bellard | vnc_write_u16(vs, h); |
162 | 24236869 | bellard | |
163 | 24236869 | bellard | vnc_write_s32(vs, encoding); |
164 | 24236869 | bellard | } |
165 | 24236869 | bellard | |
166 | 24236869 | bellard | static void vnc_dpy_resize(DisplayState *ds, int w, int h) |
167 | 24236869 | bellard | { |
168 | 24236869 | bellard | VncState *vs = ds->opaque; |
169 | 24236869 | bellard | |
170 | 24236869 | bellard | ds->data = realloc(ds->data, w * h * vs->depth); |
171 | 24236869 | bellard | vs->old_data = realloc(vs->old_data, w * h * vs->depth); |
172 | 24236869 | bellard | |
173 | 24236869 | bellard | if (ds->data == NULL || vs->old_data == NULL) { |
174 | 24236869 | bellard | fprintf(stderr, "vnc: memory allocation failed\n");
|
175 | 24236869 | bellard | exit(1);
|
176 | 24236869 | bellard | } |
177 | 24236869 | bellard | |
178 | 24236869 | bellard | ds->depth = vs->depth * 8;
|
179 | 24236869 | bellard | ds->width = w; |
180 | 24236869 | bellard | ds->height = h; |
181 | 24236869 | bellard | ds->linesize = w * vs->depth; |
182 | 24236869 | bellard | if (vs->csock != -1 && vs->has_resize) { |
183 | 24236869 | bellard | vnc_write_u8(vs, 0); /* msg id */ |
184 | 24236869 | bellard | vnc_write_u8(vs, 0);
|
185 | 24236869 | bellard | vnc_write_u16(vs, 1); /* number of rects */ |
186 | 24236869 | bellard | vnc_framebuffer_update(vs, 0, 0, ds->width, ds->height, -223); |
187 | 24236869 | bellard | vnc_flush(vs); |
188 | 24236869 | bellard | vs->width = ds->width; |
189 | 24236869 | bellard | vs->height = ds->height; |
190 | 24236869 | bellard | } |
191 | 24236869 | bellard | } |
192 | 24236869 | bellard | |
193 | 3512779a | bellard | /* fastest code */
|
194 | 3512779a | bellard | static void vnc_write_pixels_copy(VncState *vs, void *pixels, int size) |
195 | 3512779a | bellard | { |
196 | 3512779a | bellard | vnc_write(vs, pixels, size); |
197 | 3512779a | bellard | } |
198 | 3512779a | bellard | |
199 | 3512779a | bellard | /* slowest but generic code. */
|
200 | 3512779a | bellard | static void vnc_convert_pixel(VncState *vs, uint8_t *buf, uint32_t v) |
201 | 3512779a | bellard | { |
202 | 3512779a | bellard | unsigned int r, g, b; |
203 | 3512779a | bellard | |
204 | 3512779a | bellard | r = (v >> vs->red_shift1) & vs->red_max; |
205 | 3512779a | bellard | g = (v >> vs->green_shift1) & vs->green_max; |
206 | 3512779a | bellard | b = (v >> vs->blue_shift1) & vs->blue_max; |
207 | 3512779a | bellard | v = (r << vs->red_shift) | |
208 | 3512779a | bellard | (g << vs->green_shift) | |
209 | 3512779a | bellard | (b << vs->blue_shift); |
210 | 3512779a | bellard | switch(vs->pix_bpp) {
|
211 | 3512779a | bellard | case 1: |
212 | 3512779a | bellard | buf[0] = v;
|
213 | 3512779a | bellard | break;
|
214 | 3512779a | bellard | case 2: |
215 | 3512779a | bellard | if (vs->pix_big_endian) {
|
216 | 3512779a | bellard | buf[0] = v >> 8; |
217 | 3512779a | bellard | buf[1] = v;
|
218 | 3512779a | bellard | } else {
|
219 | 3512779a | bellard | buf[1] = v >> 8; |
220 | 3512779a | bellard | buf[0] = v;
|
221 | 3512779a | bellard | } |
222 | 3512779a | bellard | break;
|
223 | 3512779a | bellard | default:
|
224 | 3512779a | bellard | case 4: |
225 | 3512779a | bellard | if (vs->pix_big_endian) {
|
226 | 3512779a | bellard | buf[0] = v >> 24; |
227 | 3512779a | bellard | buf[1] = v >> 16; |
228 | 3512779a | bellard | buf[2] = v >> 8; |
229 | 3512779a | bellard | buf[3] = v;
|
230 | 3512779a | bellard | } else {
|
231 | 3512779a | bellard | buf[3] = v >> 24; |
232 | 3512779a | bellard | buf[2] = v >> 16; |
233 | 3512779a | bellard | buf[1] = v >> 8; |
234 | 3512779a | bellard | buf[0] = v;
|
235 | 3512779a | bellard | } |
236 | 3512779a | bellard | break;
|
237 | 3512779a | bellard | } |
238 | 3512779a | bellard | } |
239 | 3512779a | bellard | |
240 | 3512779a | bellard | static void vnc_write_pixels_generic(VncState *vs, void *pixels1, int size) |
241 | 3512779a | bellard | { |
242 | 3512779a | bellard | uint32_t *pixels = pixels1; |
243 | 3512779a | bellard | uint8_t buf[4];
|
244 | 3512779a | bellard | int n, i;
|
245 | 3512779a | bellard | |
246 | 3512779a | bellard | n = size >> 2;
|
247 | 3512779a | bellard | for(i = 0; i < n; i++) { |
248 | 3512779a | bellard | vnc_convert_pixel(vs, buf, pixels[i]); |
249 | 3512779a | bellard | vnc_write(vs, buf, vs->pix_bpp); |
250 | 3512779a | bellard | } |
251 | 3512779a | bellard | } |
252 | 3512779a | bellard | |
253 | 24236869 | bellard | static void send_framebuffer_update_raw(VncState *vs, int x, int y, int w, int h) |
254 | 24236869 | bellard | { |
255 | 24236869 | bellard | int i;
|
256 | 24236869 | bellard | char *row;
|
257 | 24236869 | bellard | |
258 | 24236869 | bellard | vnc_framebuffer_update(vs, x, y, w, h, 0);
|
259 | 24236869 | bellard | |
260 | 24236869 | bellard | row = vs->ds->data + y * vs->ds->linesize + x * vs->depth; |
261 | 24236869 | bellard | for (i = 0; i < h; i++) { |
262 | 3512779a | bellard | vs->write_pixels(vs, row, w * vs->depth); |
263 | 24236869 | bellard | row += vs->ds->linesize; |
264 | 24236869 | bellard | } |
265 | 24236869 | bellard | } |
266 | 24236869 | bellard | |
267 | 24236869 | bellard | static void hextile_enc_cord(uint8_t *ptr, int x, int y, int w, int h) |
268 | 24236869 | bellard | { |
269 | 24236869 | bellard | ptr[0] = ((x & 0x0F) << 4) | (y & 0x0F); |
270 | 24236869 | bellard | ptr[1] = (((w - 1) & 0x0F) << 4) | ((h - 1) & 0x0F); |
271 | 24236869 | bellard | } |
272 | 24236869 | bellard | |
273 | 24236869 | bellard | #define BPP 8 |
274 | 24236869 | bellard | #include "vnchextile.h" |
275 | 24236869 | bellard | #undef BPP
|
276 | 24236869 | bellard | |
277 | 24236869 | bellard | #define BPP 16 |
278 | 24236869 | bellard | #include "vnchextile.h" |
279 | 24236869 | bellard | #undef BPP
|
280 | 24236869 | bellard | |
281 | 24236869 | bellard | #define BPP 32 |
282 | 24236869 | bellard | #include "vnchextile.h" |
283 | 24236869 | bellard | #undef BPP
|
284 | 24236869 | bellard | |
285 | 3512779a | bellard | #define GENERIC
|
286 | 3512779a | bellard | #define BPP 32 |
287 | 3512779a | bellard | #include "vnchextile.h" |
288 | 3512779a | bellard | #undef BPP
|
289 | 3512779a | bellard | #undef GENERIC
|
290 | 3512779a | bellard | |
291 | 24236869 | bellard | static void send_framebuffer_update_hextile(VncState *vs, int x, int y, int w, int h) |
292 | 24236869 | bellard | { |
293 | 24236869 | bellard | int i, j;
|
294 | 24236869 | bellard | int has_fg, has_bg;
|
295 | 24236869 | bellard | uint32_t last_fg32, last_bg32; |
296 | 24236869 | bellard | |
297 | 24236869 | bellard | vnc_framebuffer_update(vs, x, y, w, h, 5);
|
298 | 24236869 | bellard | |
299 | 24236869 | bellard | has_fg = has_bg = 0;
|
300 | 24236869 | bellard | for (j = y; j < (y + h); j += 16) { |
301 | 24236869 | bellard | for (i = x; i < (x + w); i += 16) { |
302 | 3512779a | bellard | vs->send_hextile_tile(vs, i, j, |
303 | 3512779a | bellard | MIN(16, x + w - i), MIN(16, y + h - j), |
304 | 3512779a | bellard | &last_bg32, &last_fg32, &has_bg, &has_fg); |
305 | 24236869 | bellard | } |
306 | 24236869 | bellard | } |
307 | 24236869 | bellard | } |
308 | 24236869 | bellard | |
309 | 24236869 | bellard | static void send_framebuffer_update(VncState *vs, int x, int y, int w, int h) |
310 | 24236869 | bellard | { |
311 | 24236869 | bellard | if (vs->has_hextile)
|
312 | 24236869 | bellard | send_framebuffer_update_hextile(vs, x, y, w, h); |
313 | 24236869 | bellard | else
|
314 | 24236869 | bellard | send_framebuffer_update_raw(vs, x, y, w, h); |
315 | 24236869 | bellard | } |
316 | 24236869 | bellard | |
317 | 24236869 | bellard | static void vnc_copy(DisplayState *ds, int src_x, int src_y, int dst_x, int dst_y, int w, int h) |
318 | 24236869 | bellard | { |
319 | 24236869 | bellard | int src, dst;
|
320 | 24236869 | bellard | char *src_row;
|
321 | 24236869 | bellard | char *dst_row;
|
322 | 24236869 | bellard | char *old_row;
|
323 | 24236869 | bellard | int y = 0; |
324 | 24236869 | bellard | int pitch = ds->linesize;
|
325 | 24236869 | bellard | VncState *vs = ds->opaque; |
326 | 24236869 | bellard | |
327 | 24236869 | bellard | vnc_update_client(vs); |
328 | 24236869 | bellard | |
329 | 24236869 | bellard | if (dst_y > src_y) {
|
330 | 24236869 | bellard | y = h - 1;
|
331 | 24236869 | bellard | pitch = -pitch; |
332 | 24236869 | bellard | } |
333 | 24236869 | bellard | |
334 | 24236869 | bellard | src = (ds->linesize * (src_y + y) + vs->depth * src_x); |
335 | 24236869 | bellard | dst = (ds->linesize * (dst_y + y) + vs->depth * dst_x); |
336 | 24236869 | bellard | |
337 | 24236869 | bellard | src_row = ds->data + src; |
338 | 24236869 | bellard | dst_row = ds->data + dst; |
339 | 24236869 | bellard | old_row = vs->old_data + dst; |
340 | 24236869 | bellard | |
341 | 24236869 | bellard | for (y = 0; y < h; y++) { |
342 | 24236869 | bellard | memmove(old_row, src_row, w * vs->depth); |
343 | 24236869 | bellard | memmove(dst_row, src_row, w * vs->depth); |
344 | 24236869 | bellard | src_row += pitch; |
345 | 24236869 | bellard | dst_row += pitch; |
346 | 24236869 | bellard | old_row += pitch; |
347 | 24236869 | bellard | } |
348 | 24236869 | bellard | |
349 | 24236869 | bellard | vnc_write_u8(vs, 0); /* msg id */ |
350 | 24236869 | bellard | vnc_write_u8(vs, 0);
|
351 | 24236869 | bellard | vnc_write_u16(vs, 1); /* number of rects */ |
352 | 24236869 | bellard | vnc_framebuffer_update(vs, dst_x, dst_y, w, h, 1);
|
353 | 24236869 | bellard | vnc_write_u16(vs, src_x); |
354 | 24236869 | bellard | vnc_write_u16(vs, src_y); |
355 | 24236869 | bellard | vnc_flush(vs); |
356 | 24236869 | bellard | } |
357 | 24236869 | bellard | |
358 | 24236869 | bellard | static int find_dirty_height(VncState *vs, int y, int last_x, int x) |
359 | 24236869 | bellard | { |
360 | 24236869 | bellard | int h;
|
361 | 24236869 | bellard | |
362 | 24236869 | bellard | for (h = 1; h < (vs->height - y); h++) { |
363 | 24236869 | bellard | int tmp_x;
|
364 | 99589bdc | bellard | if (!vnc_get_bit(vs->dirty_row[y + h], last_x))
|
365 | 24236869 | bellard | break;
|
366 | 24236869 | bellard | for (tmp_x = last_x; tmp_x < x; tmp_x++)
|
367 | 99589bdc | bellard | vnc_clear_bit(vs->dirty_row[y + h], tmp_x); |
368 | 24236869 | bellard | } |
369 | 24236869 | bellard | |
370 | 24236869 | bellard | return h;
|
371 | 24236869 | bellard | } |
372 | 24236869 | bellard | |
373 | 24236869 | bellard | static void vnc_update_client(void *opaque) |
374 | 24236869 | bellard | { |
375 | 24236869 | bellard | VncState *vs = opaque; |
376 | 24236869 | bellard | |
377 | 24236869 | bellard | if (vs->need_update && vs->csock != -1) { |
378 | 24236869 | bellard | int y;
|
379 | 24236869 | bellard | char *row;
|
380 | 24236869 | bellard | char *old_row;
|
381 | 99589bdc | bellard | uint32_t width_mask[VNC_DIRTY_WORDS]; |
382 | 24236869 | bellard | int n_rectangles;
|
383 | 24236869 | bellard | int saved_offset;
|
384 | 24236869 | bellard | int has_dirty = 0; |
385 | 24236869 | bellard | |
386 | 99589bdc | bellard | vnc_set_bits(width_mask, (vs->width / 16), VNC_DIRTY_WORDS);
|
387 | 24236869 | bellard | |
388 | 24236869 | bellard | /* Walk through the dirty map and eliminate tiles that
|
389 | 24236869 | bellard | really aren't dirty */
|
390 | 24236869 | bellard | row = vs->ds->data; |
391 | 24236869 | bellard | old_row = vs->old_data; |
392 | 24236869 | bellard | |
393 | 24236869 | bellard | for (y = 0; y < vs->height; y++) { |
394 | 99589bdc | bellard | if (vnc_and_bits(vs->dirty_row[y], width_mask, VNC_DIRTY_WORDS)) {
|
395 | 24236869 | bellard | int x;
|
396 | 24236869 | bellard | char *ptr, *old_ptr;
|
397 | 24236869 | bellard | |
398 | 24236869 | bellard | ptr = row; |
399 | 24236869 | bellard | old_ptr = old_row; |
400 | 24236869 | bellard | |
401 | 24236869 | bellard | for (x = 0; x < vs->ds->width; x += 16) { |
402 | 24236869 | bellard | if (memcmp(old_ptr, ptr, 16 * vs->depth) == 0) { |
403 | 99589bdc | bellard | vnc_clear_bit(vs->dirty_row[y], (x / 16));
|
404 | 24236869 | bellard | } else {
|
405 | 24236869 | bellard | has_dirty = 1;
|
406 | 24236869 | bellard | memcpy(old_ptr, ptr, 16 * vs->depth);
|
407 | 24236869 | bellard | } |
408 | 24236869 | bellard | |
409 | 24236869 | bellard | ptr += 16 * vs->depth;
|
410 | 24236869 | bellard | old_ptr += 16 * vs->depth;
|
411 | 24236869 | bellard | } |
412 | 24236869 | bellard | } |
413 | 24236869 | bellard | |
414 | 24236869 | bellard | row += vs->ds->linesize; |
415 | 24236869 | bellard | old_row += vs->ds->linesize; |
416 | 24236869 | bellard | } |
417 | 24236869 | bellard | |
418 | 24236869 | bellard | if (!has_dirty) {
|
419 | 24236869 | bellard | qemu_mod_timer(vs->timer, qemu_get_clock(rt_clock) + VNC_REFRESH_INTERVAL); |
420 | 24236869 | bellard | return;
|
421 | 24236869 | bellard | } |
422 | 24236869 | bellard | |
423 | 24236869 | bellard | /* Count rectangles */
|
424 | 24236869 | bellard | n_rectangles = 0;
|
425 | 24236869 | bellard | vnc_write_u8(vs, 0); /* msg id */ |
426 | 24236869 | bellard | vnc_write_u8(vs, 0);
|
427 | 24236869 | bellard | saved_offset = vs->output.offset; |
428 | 24236869 | bellard | vnc_write_u16(vs, 0);
|
429 | 24236869 | bellard | |
430 | 24236869 | bellard | for (y = 0; y < vs->height; y++) { |
431 | 24236869 | bellard | int x;
|
432 | 24236869 | bellard | int last_x = -1; |
433 | 24236869 | bellard | for (x = 0; x < vs->width / 16; x++) { |
434 | 99589bdc | bellard | if (vnc_get_bit(vs->dirty_row[y], x)) {
|
435 | 24236869 | bellard | if (last_x == -1) { |
436 | 24236869 | bellard | last_x = x; |
437 | 24236869 | bellard | } |
438 | 99589bdc | bellard | vnc_clear_bit(vs->dirty_row[y], x); |
439 | 24236869 | bellard | } else {
|
440 | 24236869 | bellard | if (last_x != -1) { |
441 | 24236869 | bellard | int h = find_dirty_height(vs, y, last_x, x);
|
442 | 24236869 | bellard | send_framebuffer_update(vs, last_x * 16, y, (x - last_x) * 16, h); |
443 | 24236869 | bellard | n_rectangles++; |
444 | 24236869 | bellard | } |
445 | 24236869 | bellard | last_x = -1;
|
446 | 24236869 | bellard | } |
447 | 24236869 | bellard | } |
448 | 24236869 | bellard | if (last_x != -1) { |
449 | 24236869 | bellard | int h = find_dirty_height(vs, y, last_x, x);
|
450 | 24236869 | bellard | send_framebuffer_update(vs, last_x * 16, y, (x - last_x) * 16, h); |
451 | 24236869 | bellard | n_rectangles++; |
452 | 24236869 | bellard | } |
453 | 24236869 | bellard | } |
454 | 24236869 | bellard | vs->output.buffer[saved_offset] = (n_rectangles >> 8) & 0xFF; |
455 | 24236869 | bellard | vs->output.buffer[saved_offset + 1] = n_rectangles & 0xFF; |
456 | 24236869 | bellard | vnc_flush(vs); |
457 | 24236869 | bellard | |
458 | 24236869 | bellard | } |
459 | 24236869 | bellard | qemu_mod_timer(vs->timer, qemu_get_clock(rt_clock) + VNC_REFRESH_INTERVAL); |
460 | 24236869 | bellard | } |
461 | 24236869 | bellard | |
462 | 24236869 | bellard | static void vnc_timer_init(VncState *vs) |
463 | 24236869 | bellard | { |
464 | 24236869 | bellard | if (vs->timer == NULL) { |
465 | 24236869 | bellard | vs->timer = qemu_new_timer(rt_clock, vnc_update_client, vs); |
466 | 24236869 | bellard | qemu_mod_timer(vs->timer, qemu_get_clock(rt_clock)); |
467 | 24236869 | bellard | } |
468 | 24236869 | bellard | } |
469 | 24236869 | bellard | |
470 | 24236869 | bellard | static void vnc_dpy_refresh(DisplayState *ds) |
471 | 24236869 | bellard | { |
472 | 24236869 | bellard | VncState *vs = ds->opaque; |
473 | 24236869 | bellard | vnc_timer_init(vs); |
474 | 24236869 | bellard | vga_hw_update(); |
475 | 24236869 | bellard | } |
476 | 24236869 | bellard | |
477 | 24236869 | bellard | static int vnc_listen_poll(void *opaque) |
478 | 24236869 | bellard | { |
479 | 24236869 | bellard | VncState *vs = opaque; |
480 | 24236869 | bellard | if (vs->csock == -1) |
481 | 24236869 | bellard | return 1; |
482 | 24236869 | bellard | return 0; |
483 | 24236869 | bellard | } |
484 | 24236869 | bellard | |
485 | 24236869 | bellard | static void buffer_reserve(Buffer *buffer, size_t len) |
486 | 24236869 | bellard | { |
487 | 24236869 | bellard | if ((buffer->capacity - buffer->offset) < len) {
|
488 | 24236869 | bellard | buffer->capacity += (len + 1024);
|
489 | 24236869 | bellard | buffer->buffer = realloc(buffer->buffer, buffer->capacity); |
490 | 24236869 | bellard | if (buffer->buffer == NULL) { |
491 | 24236869 | bellard | fprintf(stderr, "vnc: out of memory\n");
|
492 | 24236869 | bellard | exit(1);
|
493 | 24236869 | bellard | } |
494 | 24236869 | bellard | } |
495 | 24236869 | bellard | } |
496 | 24236869 | bellard | |
497 | 24236869 | bellard | static int buffer_empty(Buffer *buffer) |
498 | 24236869 | bellard | { |
499 | 24236869 | bellard | return buffer->offset == 0; |
500 | 24236869 | bellard | } |
501 | 24236869 | bellard | |
502 | 24236869 | bellard | static char *buffer_end(Buffer *buffer) |
503 | 24236869 | bellard | { |
504 | 24236869 | bellard | return buffer->buffer + buffer->offset;
|
505 | 24236869 | bellard | } |
506 | 24236869 | bellard | |
507 | 24236869 | bellard | static void buffer_reset(Buffer *buffer) |
508 | 24236869 | bellard | { |
509 | 24236869 | bellard | buffer->offset = 0;
|
510 | 24236869 | bellard | } |
511 | 24236869 | bellard | |
512 | 24236869 | bellard | static void buffer_append(Buffer *buffer, const void *data, size_t len) |
513 | 24236869 | bellard | { |
514 | 24236869 | bellard | memcpy(buffer->buffer + buffer->offset, data, len); |
515 | 24236869 | bellard | buffer->offset += len; |
516 | 24236869 | bellard | } |
517 | 24236869 | bellard | |
518 | 6ca957f0 | bellard | static int vnc_client_io_error(VncState *vs, int ret, int last_errno) |
519 | 24236869 | bellard | { |
520 | 24236869 | bellard | if (ret == 0 || ret == -1) { |
521 | 6ca957f0 | bellard | if (ret == -1 && (last_errno == EINTR || last_errno == EAGAIN)) |
522 | 24236869 | bellard | return 0; |
523 | 24236869 | bellard | |
524 | 24236869 | bellard | qemu_set_fd_handler2(vs->csock, NULL, NULL, NULL, NULL); |
525 | 6ca957f0 | bellard | closesocket(vs->csock); |
526 | 24236869 | bellard | vs->csock = -1;
|
527 | 24236869 | bellard | buffer_reset(&vs->input); |
528 | 24236869 | bellard | buffer_reset(&vs->output); |
529 | 24236869 | bellard | vs->need_update = 0;
|
530 | 24236869 | bellard | return 0; |
531 | 24236869 | bellard | } |
532 | 24236869 | bellard | return ret;
|
533 | 24236869 | bellard | } |
534 | 24236869 | bellard | |
535 | 24236869 | bellard | static void vnc_client_error(VncState *vs) |
536 | 24236869 | bellard | { |
537 | 6ca957f0 | bellard | vnc_client_io_error(vs, -1, EINVAL);
|
538 | 24236869 | bellard | } |
539 | 24236869 | bellard | |
540 | 24236869 | bellard | static void vnc_client_write(void *opaque) |
541 | 24236869 | bellard | { |
542 | ceb5caaf | bellard | long ret;
|
543 | 24236869 | bellard | VncState *vs = opaque; |
544 | 24236869 | bellard | |
545 | 6ca957f0 | bellard | ret = send(vs->csock, vs->output.buffer, vs->output.offset, 0);
|
546 | 6ca957f0 | bellard | ret = vnc_client_io_error(vs, ret, socket_error()); |
547 | 24236869 | bellard | if (!ret)
|
548 | 24236869 | bellard | return;
|
549 | 24236869 | bellard | |
550 | 24236869 | bellard | memmove(vs->output.buffer, vs->output.buffer + ret, (vs->output.offset - ret)); |
551 | 24236869 | bellard | vs->output.offset -= ret; |
552 | 24236869 | bellard | |
553 | 24236869 | bellard | if (vs->output.offset == 0) { |
554 | 24236869 | bellard | qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs); |
555 | 24236869 | bellard | } |
556 | 24236869 | bellard | } |
557 | 24236869 | bellard | |
558 | 24236869 | bellard | static void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting) |
559 | 24236869 | bellard | { |
560 | 24236869 | bellard | vs->read_handler = func; |
561 | 24236869 | bellard | vs->read_handler_expect = expecting; |
562 | 24236869 | bellard | } |
563 | 24236869 | bellard | |
564 | 24236869 | bellard | static void vnc_client_read(void *opaque) |
565 | 24236869 | bellard | { |
566 | 24236869 | bellard | VncState *vs = opaque; |
567 | ceb5caaf | bellard | long ret;
|
568 | 24236869 | bellard | |
569 | 24236869 | bellard | buffer_reserve(&vs->input, 4096);
|
570 | 24236869 | bellard | |
571 | 6ca957f0 | bellard | ret = recv(vs->csock, buffer_end(&vs->input), 4096, 0); |
572 | 6ca957f0 | bellard | ret = vnc_client_io_error(vs, ret, socket_error()); |
573 | 24236869 | bellard | if (!ret)
|
574 | 24236869 | bellard | return;
|
575 | 24236869 | bellard | |
576 | 24236869 | bellard | vs->input.offset += ret; |
577 | 24236869 | bellard | |
578 | 24236869 | bellard | while (vs->read_handler && vs->input.offset >= vs->read_handler_expect) {
|
579 | 24236869 | bellard | size_t len = vs->read_handler_expect; |
580 | 24236869 | bellard | int ret;
|
581 | 24236869 | bellard | |
582 | 24236869 | bellard | ret = vs->read_handler(vs, vs->input.buffer, len); |
583 | 24236869 | bellard | if (vs->csock == -1) |
584 | 24236869 | bellard | return;
|
585 | 24236869 | bellard | |
586 | 24236869 | bellard | if (!ret) {
|
587 | 24236869 | bellard | memmove(vs->input.buffer, vs->input.buffer + len, (vs->input.offset - len)); |
588 | 24236869 | bellard | vs->input.offset -= len; |
589 | 24236869 | bellard | } else {
|
590 | 24236869 | bellard | vs->read_handler_expect = ret; |
591 | 24236869 | bellard | } |
592 | 24236869 | bellard | } |
593 | 24236869 | bellard | } |
594 | 24236869 | bellard | |
595 | 24236869 | bellard | static void vnc_write(VncState *vs, const void *data, size_t len) |
596 | 24236869 | bellard | { |
597 | 24236869 | bellard | buffer_reserve(&vs->output, len); |
598 | 24236869 | bellard | |
599 | 24236869 | bellard | if (buffer_empty(&vs->output)) {
|
600 | 24236869 | bellard | qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, vnc_client_write, vs);
|
601 | 24236869 | bellard | } |
602 | 24236869 | bellard | |
603 | 24236869 | bellard | buffer_append(&vs->output, data, len); |
604 | 24236869 | bellard | } |
605 | 24236869 | bellard | |
606 | 24236869 | bellard | static void vnc_write_s32(VncState *vs, int32_t value) |
607 | 24236869 | bellard | { |
608 | 24236869 | bellard | vnc_write_u32(vs, *(uint32_t *)&value); |
609 | 24236869 | bellard | } |
610 | 24236869 | bellard | |
611 | 24236869 | bellard | static void vnc_write_u32(VncState *vs, uint32_t value) |
612 | 24236869 | bellard | { |
613 | 24236869 | bellard | uint8_t buf[4];
|
614 | 24236869 | bellard | |
615 | 24236869 | bellard | buf[0] = (value >> 24) & 0xFF; |
616 | 24236869 | bellard | buf[1] = (value >> 16) & 0xFF; |
617 | 24236869 | bellard | buf[2] = (value >> 8) & 0xFF; |
618 | 24236869 | bellard | buf[3] = value & 0xFF; |
619 | 24236869 | bellard | |
620 | 24236869 | bellard | vnc_write(vs, buf, 4);
|
621 | 24236869 | bellard | } |
622 | 24236869 | bellard | |
623 | 24236869 | bellard | static void vnc_write_u16(VncState *vs, uint16_t value) |
624 | 24236869 | bellard | { |
625 | 24236869 | bellard | char buf[2]; |
626 | 24236869 | bellard | |
627 | 24236869 | bellard | buf[0] = (value >> 8) & 0xFF; |
628 | 24236869 | bellard | buf[1] = value & 0xFF; |
629 | 24236869 | bellard | |
630 | 24236869 | bellard | vnc_write(vs, buf, 2);
|
631 | 24236869 | bellard | } |
632 | 24236869 | bellard | |
633 | 24236869 | bellard | static void vnc_write_u8(VncState *vs, uint8_t value) |
634 | 24236869 | bellard | { |
635 | 24236869 | bellard | vnc_write(vs, (char *)&value, 1); |
636 | 24236869 | bellard | } |
637 | 24236869 | bellard | |
638 | 24236869 | bellard | static void vnc_flush(VncState *vs) |
639 | 24236869 | bellard | { |
640 | 24236869 | bellard | if (vs->output.offset)
|
641 | 24236869 | bellard | vnc_client_write(vs); |
642 | 24236869 | bellard | } |
643 | 24236869 | bellard | |
644 | 24236869 | bellard | static uint8_t read_u8(char *data, size_t offset) |
645 | 24236869 | bellard | { |
646 | 24236869 | bellard | return data[offset];
|
647 | 24236869 | bellard | } |
648 | 24236869 | bellard | |
649 | 24236869 | bellard | static uint16_t read_u16(char *data, size_t offset) |
650 | 24236869 | bellard | { |
651 | 24236869 | bellard | return ((data[offset] & 0xFF) << 8) | (data[offset + 1] & 0xFF); |
652 | 24236869 | bellard | } |
653 | 24236869 | bellard | |
654 | 24236869 | bellard | static int32_t read_s32(char *data, size_t offset) |
655 | 24236869 | bellard | { |
656 | 24236869 | bellard | return (int32_t)((data[offset] << 24) | (data[offset + 1] << 16) | |
657 | 24236869 | bellard | (data[offset + 2] << 8) | data[offset + 3]); |
658 | 24236869 | bellard | } |
659 | 24236869 | bellard | |
660 | 24236869 | bellard | static uint32_t read_u32(char *data, size_t offset) |
661 | 24236869 | bellard | { |
662 | 24236869 | bellard | return ((data[offset] << 24) | (data[offset + 1] << 16) | |
663 | 24236869 | bellard | (data[offset + 2] << 8) | data[offset + 3]); |
664 | 24236869 | bellard | } |
665 | 24236869 | bellard | |
666 | 24236869 | bellard | static void client_cut_text(VncState *vs, size_t len, char *text) |
667 | 24236869 | bellard | { |
668 | 24236869 | bellard | } |
669 | 24236869 | bellard | |
670 | 24236869 | bellard | static void pointer_event(VncState *vs, int button_mask, int x, int y) |
671 | 24236869 | bellard | { |
672 | 24236869 | bellard | int buttons = 0; |
673 | 24236869 | bellard | int dz = 0; |
674 | 24236869 | bellard | |
675 | 24236869 | bellard | if (button_mask & 0x01) |
676 | 24236869 | bellard | buttons |= MOUSE_EVENT_LBUTTON; |
677 | 24236869 | bellard | if (button_mask & 0x02) |
678 | 24236869 | bellard | buttons |= MOUSE_EVENT_MBUTTON; |
679 | 24236869 | bellard | if (button_mask & 0x04) |
680 | 24236869 | bellard | buttons |= MOUSE_EVENT_RBUTTON; |
681 | 24236869 | bellard | if (button_mask & 0x08) |
682 | 24236869 | bellard | dz = -1;
|
683 | 24236869 | bellard | if (button_mask & 0x10) |
684 | 24236869 | bellard | dz = 1;
|
685 | 24236869 | bellard | |
686 | 24236869 | bellard | if (kbd_mouse_is_absolute()) {
|
687 | 24236869 | bellard | kbd_mouse_event(x * 0x7FFF / vs->ds->width,
|
688 | 24236869 | bellard | y * 0x7FFF / vs->ds->height,
|
689 | 24236869 | bellard | dz, buttons); |
690 | 24236869 | bellard | } else {
|
691 | 24236869 | bellard | static int last_x = -1; |
692 | 24236869 | bellard | static int last_y = -1; |
693 | 24236869 | bellard | |
694 | 24236869 | bellard | if (last_x != -1) |
695 | 24236869 | bellard | kbd_mouse_event(x - last_x, y - last_y, dz, buttons); |
696 | 24236869 | bellard | |
697 | 24236869 | bellard | last_x = x; |
698 | 24236869 | bellard | last_y = y; |
699 | 24236869 | bellard | } |
700 | 24236869 | bellard | } |
701 | 24236869 | bellard | |
702 | bdbd7676 | bellard | static void do_key_event(VncState *vs, int down, uint32_t sym) |
703 | 24236869 | bellard | { |
704 | 24236869 | bellard | int keycode;
|
705 | 24236869 | bellard | |
706 | 24236869 | bellard | keycode = keysym2scancode(vs->kbd_layout, sym & 0xFFFF);
|
707 | 24236869 | bellard | |
708 | 24236869 | bellard | if (keycode & 0x80) |
709 | 24236869 | bellard | kbd_put_keycode(0xe0);
|
710 | 24236869 | bellard | if (down)
|
711 | 24236869 | bellard | kbd_put_keycode(keycode & 0x7f);
|
712 | 24236869 | bellard | else
|
713 | 24236869 | bellard | kbd_put_keycode(keycode | 0x80);
|
714 | 24236869 | bellard | } |
715 | 24236869 | bellard | |
716 | bdbd7676 | bellard | static void key_event(VncState *vs, int down, uint32_t sym) |
717 | bdbd7676 | bellard | { |
718 | bdbd7676 | bellard | if (sym >= 'A' && sym <= 'Z') |
719 | bdbd7676 | bellard | sym = sym - 'A' + 'a'; |
720 | bdbd7676 | bellard | do_key_event(vs, down, sym); |
721 | bdbd7676 | bellard | } |
722 | bdbd7676 | bellard | |
723 | 24236869 | bellard | static void framebuffer_update_request(VncState *vs, int incremental, |
724 | 24236869 | bellard | int x_position, int y_position, |
725 | 24236869 | bellard | int w, int h) |
726 | 24236869 | bellard | { |
727 | 24236869 | bellard | int i;
|
728 | 24236869 | bellard | vs->need_update = 1;
|
729 | 24236869 | bellard | if (!incremental) {
|
730 | 24236869 | bellard | char *old_row = vs->old_data + y_position * vs->ds->linesize;
|
731 | 24236869 | bellard | |
732 | 24236869 | bellard | for (i = 0; i < h; i++) { |
733 | 99589bdc | bellard | vnc_set_bits(vs->dirty_row[y_position + i], |
734 | 99589bdc | bellard | (vs->ds->width / 16), VNC_DIRTY_WORDS);
|
735 | 24236869 | bellard | memset(old_row, 42, vs->ds->width * vs->depth);
|
736 | 24236869 | bellard | old_row += vs->ds->linesize; |
737 | 24236869 | bellard | } |
738 | 24236869 | bellard | } |
739 | 24236869 | bellard | } |
740 | 24236869 | bellard | |
741 | 24236869 | bellard | static void set_encodings(VncState *vs, int32_t *encodings, size_t n_encodings) |
742 | 24236869 | bellard | { |
743 | 24236869 | bellard | int i;
|
744 | 24236869 | bellard | |
745 | 24236869 | bellard | vs->has_hextile = 0;
|
746 | 24236869 | bellard | vs->has_resize = 0;
|
747 | 24236869 | bellard | vs->ds->dpy_copy = NULL;
|
748 | 24236869 | bellard | |
749 | 24236869 | bellard | for (i = n_encodings - 1; i >= 0; i--) { |
750 | 24236869 | bellard | switch (encodings[i]) {
|
751 | 24236869 | bellard | case 0: /* Raw */ |
752 | 24236869 | bellard | vs->has_hextile = 0;
|
753 | 24236869 | bellard | break;
|
754 | 24236869 | bellard | case 1: /* CopyRect */ |
755 | 24236869 | bellard | vs->ds->dpy_copy = vnc_copy; |
756 | 24236869 | bellard | break;
|
757 | 24236869 | bellard | case 5: /* Hextile */ |
758 | 24236869 | bellard | vs->has_hextile = 1;
|
759 | 24236869 | bellard | break;
|
760 | 24236869 | bellard | case -223: /* DesktopResize */ |
761 | 24236869 | bellard | vs->has_resize = 1;
|
762 | 24236869 | bellard | break;
|
763 | 24236869 | bellard | default:
|
764 | 24236869 | bellard | break;
|
765 | 24236869 | bellard | } |
766 | 24236869 | bellard | } |
767 | 24236869 | bellard | } |
768 | 24236869 | bellard | |
769 | 3512779a | bellard | static int compute_nbits(unsigned int val) |
770 | 3512779a | bellard | { |
771 | 3512779a | bellard | int n;
|
772 | 3512779a | bellard | n = 0;
|
773 | 3512779a | bellard | while (val != 0) { |
774 | 3512779a | bellard | n++; |
775 | 3512779a | bellard | val >>= 1;
|
776 | 3512779a | bellard | } |
777 | 3512779a | bellard | return n;
|
778 | 3512779a | bellard | } |
779 | 3512779a | bellard | |
780 | 24236869 | bellard | static void set_pixel_format(VncState *vs, |
781 | 24236869 | bellard | int bits_per_pixel, int depth, |
782 | 24236869 | bellard | int big_endian_flag, int true_color_flag, |
783 | 24236869 | bellard | int red_max, int green_max, int blue_max, |
784 | 24236869 | bellard | int red_shift, int green_shift, int blue_shift) |
785 | 24236869 | bellard | { |
786 | 3512779a | bellard | int host_big_endian_flag;
|
787 | 24236869 | bellard | |
788 | 3512779a | bellard | #ifdef WORDS_BIGENDIAN
|
789 | 3512779a | bellard | host_big_endian_flag = 1;
|
790 | 3512779a | bellard | #else
|
791 | 3512779a | bellard | host_big_endian_flag = 0;
|
792 | 3512779a | bellard | #endif
|
793 | 3512779a | bellard | if (!true_color_flag) {
|
794 | 3512779a | bellard | fail:
|
795 | 24236869 | bellard | vnc_client_error(vs); |
796 | 3512779a | bellard | return;
|
797 | 3512779a | bellard | } |
798 | 3512779a | bellard | if (bits_per_pixel == 32 && |
799 | 3512779a | bellard | host_big_endian_flag == big_endian_flag && |
800 | 3512779a | bellard | red_max == 0xff && green_max == 0xff && blue_max == 0xff && |
801 | 3512779a | bellard | red_shift == 16 && green_shift == 8 && blue_shift == 0) { |
802 | 3512779a | bellard | vs->depth = 4;
|
803 | 3512779a | bellard | vs->write_pixels = vnc_write_pixels_copy; |
804 | 3512779a | bellard | vs->send_hextile_tile = send_hextile_tile_32; |
805 | 3512779a | bellard | } else
|
806 | 3512779a | bellard | if (bits_per_pixel == 16 && |
807 | 3512779a | bellard | host_big_endian_flag == big_endian_flag && |
808 | 3512779a | bellard | red_max == 31 && green_max == 63 && blue_max == 31 && |
809 | 3512779a | bellard | red_shift == 11 && green_shift == 5 && blue_shift == 0) { |
810 | 3512779a | bellard | vs->depth = 2;
|
811 | 3512779a | bellard | vs->write_pixels = vnc_write_pixels_copy; |
812 | 3512779a | bellard | vs->send_hextile_tile = send_hextile_tile_16; |
813 | 3512779a | bellard | } else
|
814 | 3512779a | bellard | if (bits_per_pixel == 8 && |
815 | 3512779a | bellard | red_max == 7 && green_max == 7 && blue_max == 3 && |
816 | 3512779a | bellard | red_shift == 5 && green_shift == 2 && blue_shift == 0) { |
817 | 3512779a | bellard | vs->depth = 1;
|
818 | 3512779a | bellard | vs->write_pixels = vnc_write_pixels_copy; |
819 | 3512779a | bellard | vs->send_hextile_tile = send_hextile_tile_8; |
820 | 3512779a | bellard | } else
|
821 | 3512779a | bellard | { |
822 | 3512779a | bellard | /* generic and slower case */
|
823 | 3512779a | bellard | if (bits_per_pixel != 8 && |
824 | 3512779a | bellard | bits_per_pixel != 16 &&
|
825 | 3512779a | bellard | bits_per_pixel != 32)
|
826 | 3512779a | bellard | goto fail;
|
827 | 3512779a | bellard | vs->depth = 4;
|
828 | 3512779a | bellard | vs->red_shift = red_shift; |
829 | 3512779a | bellard | vs->red_max = red_max; |
830 | 3512779a | bellard | vs->red_shift1 = 24 - compute_nbits(red_max);
|
831 | 3512779a | bellard | vs->green_shift = green_shift; |
832 | 3512779a | bellard | vs->green_max = green_max; |
833 | 3512779a | bellard | vs->green_shift1 = 16 - compute_nbits(green_max);
|
834 | 3512779a | bellard | vs->blue_shift = blue_shift; |
835 | 3512779a | bellard | vs->blue_max = blue_max; |
836 | 3512779a | bellard | vs->blue_shift1 = 8 - compute_nbits(blue_max);
|
837 | 3512779a | bellard | vs->pix_bpp = bits_per_pixel / 8;
|
838 | 3512779a | bellard | vs->pix_big_endian = big_endian_flag; |
839 | 3512779a | bellard | vs->write_pixels = vnc_write_pixels_generic; |
840 | 3512779a | bellard | vs->send_hextile_tile = send_hextile_tile_generic; |
841 | 3512779a | bellard | } |
842 | 24236869 | bellard | |
843 | 24236869 | bellard | vnc_dpy_resize(vs->ds, vs->ds->width, vs->ds->height); |
844 | 24236869 | bellard | memset(vs->dirty_row, 0xFF, sizeof(vs->dirty_row)); |
845 | 24236869 | bellard | memset(vs->old_data, 42, vs->ds->linesize * vs->ds->height);
|
846 | 24236869 | bellard | |
847 | 24236869 | bellard | vga_hw_invalidate(); |
848 | 24236869 | bellard | vga_hw_update(); |
849 | 24236869 | bellard | } |
850 | 24236869 | bellard | |
851 | 24236869 | bellard | static int protocol_client_msg(VncState *vs, char *data, size_t len) |
852 | 24236869 | bellard | { |
853 | 24236869 | bellard | int i;
|
854 | 24236869 | bellard | uint16_t limit; |
855 | 24236869 | bellard | |
856 | 24236869 | bellard | switch (data[0]) { |
857 | 24236869 | bellard | case 0: |
858 | 24236869 | bellard | if (len == 1) |
859 | 24236869 | bellard | return 20; |
860 | 24236869 | bellard | |
861 | 24236869 | bellard | set_pixel_format(vs, read_u8(data, 4), read_u8(data, 5), |
862 | 24236869 | bellard | read_u8(data, 6), read_u8(data, 7), |
863 | 24236869 | bellard | read_u16(data, 8), read_u16(data, 10), |
864 | 24236869 | bellard | read_u16(data, 12), read_u8(data, 14), |
865 | 24236869 | bellard | read_u8(data, 15), read_u8(data, 16)); |
866 | 24236869 | bellard | break;
|
867 | 24236869 | bellard | case 2: |
868 | 24236869 | bellard | if (len == 1) |
869 | 24236869 | bellard | return 4; |
870 | 24236869 | bellard | |
871 | 24236869 | bellard | if (len == 4) |
872 | 24236869 | bellard | return 4 + (read_u16(data, 2) * 4); |
873 | 24236869 | bellard | |
874 | 24236869 | bellard | limit = read_u16(data, 2);
|
875 | 24236869 | bellard | for (i = 0; i < limit; i++) { |
876 | 24236869 | bellard | int32_t val = read_s32(data, 4 + (i * 4)); |
877 | 24236869 | bellard | memcpy(data + 4 + (i * 4), &val, sizeof(val)); |
878 | 24236869 | bellard | } |
879 | 24236869 | bellard | |
880 | 24236869 | bellard | set_encodings(vs, (int32_t *)(data + 4), limit);
|
881 | 24236869 | bellard | break;
|
882 | 24236869 | bellard | case 3: |
883 | 24236869 | bellard | if (len == 1) |
884 | 24236869 | bellard | return 10; |
885 | 24236869 | bellard | |
886 | 24236869 | bellard | framebuffer_update_request(vs, |
887 | 24236869 | bellard | read_u8(data, 1), read_u16(data, 2), read_u16(data, 4), |
888 | 24236869 | bellard | read_u16(data, 6), read_u16(data, 8)); |
889 | 24236869 | bellard | break;
|
890 | 24236869 | bellard | case 4: |
891 | 24236869 | bellard | if (len == 1) |
892 | 24236869 | bellard | return 8; |
893 | 24236869 | bellard | |
894 | 24236869 | bellard | key_event(vs, read_u8(data, 1), read_u32(data, 4)); |
895 | 24236869 | bellard | break;
|
896 | 24236869 | bellard | case 5: |
897 | 24236869 | bellard | if (len == 1) |
898 | 24236869 | bellard | return 6; |
899 | 24236869 | bellard | |
900 | 24236869 | bellard | pointer_event(vs, read_u8(data, 1), read_u16(data, 2), read_u16(data, 4)); |
901 | 24236869 | bellard | break;
|
902 | 24236869 | bellard | case 6: |
903 | 24236869 | bellard | if (len == 1) |
904 | 24236869 | bellard | return 8; |
905 | 24236869 | bellard | |
906 | 24236869 | bellard | if (len == 8) |
907 | 24236869 | bellard | return 8 + read_u32(data, 4); |
908 | 24236869 | bellard | |
909 | 24236869 | bellard | client_cut_text(vs, read_u32(data, 4), data + 8); |
910 | 24236869 | bellard | break;
|
911 | 24236869 | bellard | default:
|
912 | 24236869 | bellard | printf("Msg: %d\n", data[0]); |
913 | 24236869 | bellard | vnc_client_error(vs); |
914 | 24236869 | bellard | break;
|
915 | 24236869 | bellard | } |
916 | 24236869 | bellard | |
917 | 24236869 | bellard | vnc_read_when(vs, protocol_client_msg, 1);
|
918 | 24236869 | bellard | return 0; |
919 | 24236869 | bellard | } |
920 | 24236869 | bellard | |
921 | 24236869 | bellard | static int protocol_client_init(VncState *vs, char *data, size_t len) |
922 | 24236869 | bellard | { |
923 | 24236869 | bellard | char pad[3] = { 0, 0, 0 }; |
924 | 24236869 | bellard | |
925 | 24236869 | bellard | vs->width = vs->ds->width; |
926 | 24236869 | bellard | vs->height = vs->ds->height; |
927 | 24236869 | bellard | vnc_write_u16(vs, vs->ds->width); |
928 | 24236869 | bellard | vnc_write_u16(vs, vs->ds->height); |
929 | 24236869 | bellard | |
930 | 24236869 | bellard | vnc_write_u8(vs, vs->depth * 8); /* bits-per-pixel */ |
931 | 24236869 | bellard | vnc_write_u8(vs, vs->depth * 8); /* depth */ |
932 | 3512779a | bellard | #ifdef WORDS_BIGENDIAN
|
933 | 3512779a | bellard | vnc_write_u8(vs, 1); /* big-endian-flag */ |
934 | 3512779a | bellard | #else
|
935 | 24236869 | bellard | vnc_write_u8(vs, 0); /* big-endian-flag */ |
936 | 3512779a | bellard | #endif
|
937 | 24236869 | bellard | vnc_write_u8(vs, 1); /* true-color-flag */ |
938 | 24236869 | bellard | if (vs->depth == 4) { |
939 | 24236869 | bellard | vnc_write_u16(vs, 0xFF); /* red-max */ |
940 | 24236869 | bellard | vnc_write_u16(vs, 0xFF); /* green-max */ |
941 | 24236869 | bellard | vnc_write_u16(vs, 0xFF); /* blue-max */ |
942 | 24236869 | bellard | vnc_write_u8(vs, 16); /* red-shift */ |
943 | 24236869 | bellard | vnc_write_u8(vs, 8); /* green-shift */ |
944 | 24236869 | bellard | vnc_write_u8(vs, 0); /* blue-shift */ |
945 | 3512779a | bellard | vs->send_hextile_tile = send_hextile_tile_32; |
946 | 24236869 | bellard | } else if (vs->depth == 2) { |
947 | 24236869 | bellard | vnc_write_u16(vs, 31); /* red-max */ |
948 | 24236869 | bellard | vnc_write_u16(vs, 63); /* green-max */ |
949 | 24236869 | bellard | vnc_write_u16(vs, 31); /* blue-max */ |
950 | 24236869 | bellard | vnc_write_u8(vs, 11); /* red-shift */ |
951 | 24236869 | bellard | vnc_write_u8(vs, 5); /* green-shift */ |
952 | 24236869 | bellard | vnc_write_u8(vs, 0); /* blue-shift */ |
953 | 3512779a | bellard | vs->send_hextile_tile = send_hextile_tile_16; |
954 | 24236869 | bellard | } else if (vs->depth == 1) { |
955 | 3512779a | bellard | /* XXX: change QEMU pixel 8 bit pixel format to match the VNC one ? */
|
956 | 3512779a | bellard | vnc_write_u16(vs, 7); /* red-max */ |
957 | 24236869 | bellard | vnc_write_u16(vs, 7); /* green-max */ |
958 | 24236869 | bellard | vnc_write_u16(vs, 3); /* blue-max */ |
959 | 24236869 | bellard | vnc_write_u8(vs, 5); /* red-shift */ |
960 | 24236869 | bellard | vnc_write_u8(vs, 2); /* green-shift */ |
961 | 24236869 | bellard | vnc_write_u8(vs, 0); /* blue-shift */ |
962 | 3512779a | bellard | vs->send_hextile_tile = send_hextile_tile_8; |
963 | 24236869 | bellard | } |
964 | 3512779a | bellard | vs->write_pixels = vnc_write_pixels_copy; |
965 | 24236869 | bellard | |
966 | 24236869 | bellard | vnc_write(vs, pad, 3); /* padding */ |
967 | 24236869 | bellard | |
968 | 24236869 | bellard | vnc_write_u32(vs, 4);
|
969 | 24236869 | bellard | vnc_write(vs, "QEMU", 4); |
970 | 24236869 | bellard | vnc_flush(vs); |
971 | 24236869 | bellard | |
972 | 24236869 | bellard | vnc_read_when(vs, protocol_client_msg, 1);
|
973 | 24236869 | bellard | |
974 | 24236869 | bellard | return 0; |
975 | 24236869 | bellard | } |
976 | 24236869 | bellard | |
977 | 24236869 | bellard | static int protocol_version(VncState *vs, char *version, size_t len) |
978 | 24236869 | bellard | { |
979 | 24236869 | bellard | char local[13]; |
980 | 24236869 | bellard | int maj, min;
|
981 | 24236869 | bellard | |
982 | 24236869 | bellard | memcpy(local, version, 12);
|
983 | 24236869 | bellard | local[12] = 0; |
984 | 24236869 | bellard | |
985 | 24236869 | bellard | if (sscanf(local, "RFB %03d.%03d\n", &maj, &min) != 2) { |
986 | 24236869 | bellard | vnc_client_error(vs); |
987 | 24236869 | bellard | return 0; |
988 | 24236869 | bellard | } |
989 | 24236869 | bellard | |
990 | 24236869 | bellard | vnc_write_u32(vs, 1); /* None */ |
991 | 24236869 | bellard | vnc_flush(vs); |
992 | 24236869 | bellard | |
993 | 24236869 | bellard | vnc_read_when(vs, protocol_client_init, 1);
|
994 | 24236869 | bellard | |
995 | 24236869 | bellard | return 0; |
996 | 24236869 | bellard | } |
997 | 24236869 | bellard | |
998 | 24236869 | bellard | static void vnc_listen_read(void *opaque) |
999 | 24236869 | bellard | { |
1000 | 24236869 | bellard | VncState *vs = opaque; |
1001 | 24236869 | bellard | struct sockaddr_in addr;
|
1002 | 24236869 | bellard | socklen_t addrlen = sizeof(addr);
|
1003 | 24236869 | bellard | |
1004 | 24236869 | bellard | vs->csock = accept(vs->lsock, (struct sockaddr *)&addr, &addrlen);
|
1005 | 24236869 | bellard | if (vs->csock != -1) { |
1006 | 6ca957f0 | bellard | socket_set_nonblock(vs->csock); |
1007 | 24236869 | bellard | qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, opaque); |
1008 | 24236869 | bellard | vnc_write(vs, "RFB 003.003\n", 12); |
1009 | 24236869 | bellard | vnc_flush(vs); |
1010 | 24236869 | bellard | vnc_read_when(vs, protocol_version, 12);
|
1011 | 24236869 | bellard | memset(vs->old_data, 0, vs->ds->linesize * vs->ds->height);
|
1012 | 24236869 | bellard | memset(vs->dirty_row, 0xFF, sizeof(vs->dirty_row)); |
1013 | 24236869 | bellard | vs->has_resize = 0;
|
1014 | 24236869 | bellard | vs->has_hextile = 0;
|
1015 | 24236869 | bellard | vs->ds->dpy_copy = NULL;
|
1016 | 24236869 | bellard | } |
1017 | 24236869 | bellard | } |
1018 | 24236869 | bellard | |
1019 | 24236869 | bellard | void vnc_display_init(DisplayState *ds, int display) |
1020 | 24236869 | bellard | { |
1021 | 24236869 | bellard | struct sockaddr_in addr;
|
1022 | 24236869 | bellard | int reuse_addr, ret;
|
1023 | 24236869 | bellard | VncState *vs; |
1024 | 24236869 | bellard | |
1025 | 24236869 | bellard | vs = qemu_mallocz(sizeof(VncState));
|
1026 | 24236869 | bellard | if (!vs)
|
1027 | 24236869 | bellard | exit(1);
|
1028 | 24236869 | bellard | |
1029 | 24236869 | bellard | ds->opaque = vs; |
1030 | 24236869 | bellard | |
1031 | 24236869 | bellard | vs->lsock = -1;
|
1032 | 24236869 | bellard | vs->csock = -1;
|
1033 | 24236869 | bellard | vs->depth = 4;
|
1034 | 24236869 | bellard | |
1035 | 24236869 | bellard | vs->ds = ds; |
1036 | 24236869 | bellard | |
1037 | 24236869 | bellard | if (!keyboard_layout)
|
1038 | 24236869 | bellard | keyboard_layout = "en-us";
|
1039 | 24236869 | bellard | |
1040 | 24236869 | bellard | vs->kbd_layout = init_keyboard_layout(keyboard_layout); |
1041 | 24236869 | bellard | if (!vs->kbd_layout)
|
1042 | 24236869 | bellard | exit(1);
|
1043 | 24236869 | bellard | |
1044 | 24236869 | bellard | vs->lsock = socket(PF_INET, SOCK_STREAM, 0);
|
1045 | 24236869 | bellard | if (vs->lsock == -1) { |
1046 | 24236869 | bellard | fprintf(stderr, "Could not create socket\n");
|
1047 | 24236869 | bellard | exit(1);
|
1048 | 24236869 | bellard | } |
1049 | 24236869 | bellard | |
1050 | 24236869 | bellard | addr.sin_family = AF_INET; |
1051 | 24236869 | bellard | addr.sin_port = htons(5900 + display);
|
1052 | 24236869 | bellard | memset(&addr.sin_addr, 0, sizeof(addr.sin_addr)); |
1053 | 24236869 | bellard | |
1054 | 24236869 | bellard | reuse_addr = 1;
|
1055 | 24236869 | bellard | ret = setsockopt(vs->lsock, SOL_SOCKET, SO_REUSEADDR, |
1056 | 6ca957f0 | bellard | (const char *)&reuse_addr, sizeof(reuse_addr)); |
1057 | 24236869 | bellard | if (ret == -1) { |
1058 | 24236869 | bellard | fprintf(stderr, "setsockopt() failed\n");
|
1059 | 24236869 | bellard | exit(1);
|
1060 | 24236869 | bellard | } |
1061 | 24236869 | bellard | |
1062 | 24236869 | bellard | if (bind(vs->lsock, (struct sockaddr *)&addr, sizeof(addr)) == -1) { |
1063 | 24236869 | bellard | fprintf(stderr, "bind() failed\n");
|
1064 | 24236869 | bellard | exit(1);
|
1065 | 24236869 | bellard | } |
1066 | 24236869 | bellard | |
1067 | 24236869 | bellard | if (listen(vs->lsock, 1) == -1) { |
1068 | 24236869 | bellard | fprintf(stderr, "listen() failed\n");
|
1069 | 24236869 | bellard | exit(1);
|
1070 | 24236869 | bellard | } |
1071 | 24236869 | bellard | |
1072 | 24236869 | bellard | ret = qemu_set_fd_handler2(vs->lsock, vnc_listen_poll, vnc_listen_read, NULL, vs);
|
1073 | 24236869 | bellard | if (ret == -1) { |
1074 | 24236869 | bellard | exit(1);
|
1075 | 24236869 | bellard | } |
1076 | 24236869 | bellard | |
1077 | 24236869 | bellard | vs->ds->data = NULL;
|
1078 | 24236869 | bellard | vs->ds->dpy_update = vnc_dpy_update; |
1079 | 24236869 | bellard | vs->ds->dpy_resize = vnc_dpy_resize; |
1080 | 24236869 | bellard | vs->ds->dpy_refresh = vnc_dpy_refresh; |
1081 | 24236869 | bellard | |
1082 | 24236869 | bellard | memset(vs->dirty_row, 0xFF, sizeof(vs->dirty_row)); |
1083 | 24236869 | bellard | |
1084 | 24236869 | bellard | vnc_dpy_resize(vs->ds, 640, 400); |
1085 | 24236869 | bellard | } |