root / vnc.c @ 5a1e8ffb
History | View | Annotate | Download (31.4 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 | 564c337e | bellard | int has_pointer_type_change;
|
72 | 564c337e | bellard | int absolute;
|
73 | 564c337e | bellard | int last_x;
|
74 | 564c337e | bellard | int last_y;
|
75 | 564c337e | bellard | |
76 | a9ce8590 | bellard | const char *display; |
77 | a9ce8590 | bellard | |
78 | 24236869 | bellard | Buffer output; |
79 | 24236869 | bellard | Buffer input; |
80 | 24236869 | bellard | kbd_layout_t *kbd_layout; |
81 | 3512779a | bellard | /* current output mode information */
|
82 | 3512779a | bellard | VncWritePixels *write_pixels; |
83 | 3512779a | bellard | VncSendHextileTile *send_hextile_tile; |
84 | 3512779a | bellard | int pix_bpp, pix_big_endian;
|
85 | 3512779a | bellard | int red_shift, red_max, red_shift1;
|
86 | 3512779a | bellard | int green_shift, green_max, green_shift1;
|
87 | 3512779a | bellard | int blue_shift, blue_max, blue_shift1;
|
88 | 24236869 | bellard | |
89 | 24236869 | bellard | VncReadEvent *read_handler; |
90 | 24236869 | bellard | size_t read_handler_expect; |
91 | 64f5a135 | bellard | /* input */
|
92 | 64f5a135 | bellard | uint8_t modifiers_state[256];
|
93 | 24236869 | bellard | }; |
94 | 24236869 | bellard | |
95 | a9ce8590 | bellard | static VncState *vnc_state; /* needed for info vnc */ |
96 | a9ce8590 | bellard | |
97 | a9ce8590 | bellard | void do_info_vnc(void) |
98 | a9ce8590 | bellard | { |
99 | a9ce8590 | bellard | if (vnc_state == NULL) |
100 | a9ce8590 | bellard | term_printf("VNC server disabled\n");
|
101 | a9ce8590 | bellard | else {
|
102 | a9ce8590 | bellard | term_printf("VNC server active on: ");
|
103 | a9ce8590 | bellard | term_print_filename(vnc_state->display); |
104 | a9ce8590 | bellard | term_printf("\n");
|
105 | a9ce8590 | bellard | |
106 | a9ce8590 | bellard | if (vnc_state->csock == -1) |
107 | a9ce8590 | bellard | term_printf("No client connected\n");
|
108 | a9ce8590 | bellard | else
|
109 | a9ce8590 | bellard | term_printf("Client connected\n");
|
110 | a9ce8590 | bellard | } |
111 | a9ce8590 | bellard | } |
112 | a9ce8590 | bellard | |
113 | 24236869 | bellard | /* TODO
|
114 | 24236869 | bellard | 1) Get the queue working for IO.
|
115 | 24236869 | bellard | 2) there is some weirdness when using the -S option (the screen is grey
|
116 | 24236869 | bellard | and not totally invalidated
|
117 | 24236869 | bellard | 3) resolutions > 1024
|
118 | 24236869 | bellard | */
|
119 | 24236869 | bellard | |
120 | 24236869 | bellard | static void vnc_write(VncState *vs, const void *data, size_t len); |
121 | 24236869 | bellard | static void vnc_write_u32(VncState *vs, uint32_t value); |
122 | 24236869 | bellard | static void vnc_write_s32(VncState *vs, int32_t value); |
123 | 24236869 | bellard | static void vnc_write_u16(VncState *vs, uint16_t value); |
124 | 24236869 | bellard | static void vnc_write_u8(VncState *vs, uint8_t value); |
125 | 24236869 | bellard | static void vnc_flush(VncState *vs); |
126 | 24236869 | bellard | static void vnc_update_client(void *opaque); |
127 | 24236869 | bellard | static void vnc_client_read(void *opaque); |
128 | 24236869 | bellard | |
129 | 99589bdc | bellard | static inline void vnc_set_bit(uint32_t *d, int k) |
130 | 99589bdc | bellard | { |
131 | 99589bdc | bellard | d[k >> 5] |= 1 << (k & 0x1f); |
132 | 99589bdc | bellard | } |
133 | 99589bdc | bellard | |
134 | 99589bdc | bellard | static inline void vnc_clear_bit(uint32_t *d, int k) |
135 | 99589bdc | bellard | { |
136 | 99589bdc | bellard | d[k >> 5] &= ~(1 << (k & 0x1f)); |
137 | 99589bdc | bellard | } |
138 | 99589bdc | bellard | |
139 | 99589bdc | bellard | static inline void vnc_set_bits(uint32_t *d, int n, int nb_words) |
140 | 99589bdc | bellard | { |
141 | 99589bdc | bellard | int j;
|
142 | 99589bdc | bellard | |
143 | 99589bdc | bellard | j = 0;
|
144 | 99589bdc | bellard | while (n >= 32) { |
145 | 99589bdc | bellard | d[j++] = -1;
|
146 | 99589bdc | bellard | n -= 32;
|
147 | 99589bdc | bellard | } |
148 | 99589bdc | bellard | if (n > 0) |
149 | 99589bdc | bellard | d[j++] = (1 << n) - 1; |
150 | 99589bdc | bellard | while (j < nb_words)
|
151 | 99589bdc | bellard | d[j++] = 0;
|
152 | 99589bdc | bellard | } |
153 | 99589bdc | bellard | |
154 | 99589bdc | bellard | static inline int vnc_get_bit(const uint32_t *d, int k) |
155 | 99589bdc | bellard | { |
156 | 99589bdc | bellard | return (d[k >> 5] >> (k & 0x1f)) & 1; |
157 | 99589bdc | bellard | } |
158 | 99589bdc | bellard | |
159 | 99589bdc | bellard | static inline int vnc_and_bits(const uint32_t *d1, const uint32_t *d2, |
160 | 99589bdc | bellard | int nb_words)
|
161 | 99589bdc | bellard | { |
162 | 99589bdc | bellard | int i;
|
163 | 99589bdc | bellard | for(i = 0; i < nb_words; i++) { |
164 | 99589bdc | bellard | if ((d1[i] & d2[i]) != 0) |
165 | 99589bdc | bellard | return 1; |
166 | 99589bdc | bellard | } |
167 | 99589bdc | bellard | return 0; |
168 | 99589bdc | bellard | } |
169 | 99589bdc | bellard | |
170 | 24236869 | bellard | static void vnc_dpy_update(DisplayState *ds, int x, int y, int w, int h) |
171 | 24236869 | bellard | { |
172 | 24236869 | bellard | VncState *vs = ds->opaque; |
173 | 24236869 | bellard | int i;
|
174 | 24236869 | bellard | |
175 | 24236869 | bellard | h += y; |
176 | 24236869 | bellard | |
177 | 24236869 | bellard | for (; y < h; y++)
|
178 | 24236869 | bellard | for (i = 0; i < w; i += 16) |
179 | 99589bdc | bellard | vnc_set_bit(vs->dirty_row[y], (x + i) / 16);
|
180 | 24236869 | bellard | } |
181 | 24236869 | bellard | |
182 | 24236869 | bellard | static void vnc_framebuffer_update(VncState *vs, int x, int y, int w, int h, |
183 | 24236869 | bellard | int32_t encoding) |
184 | 24236869 | bellard | { |
185 | 24236869 | bellard | vnc_write_u16(vs, x); |
186 | 24236869 | bellard | vnc_write_u16(vs, y); |
187 | 24236869 | bellard | vnc_write_u16(vs, w); |
188 | 24236869 | bellard | vnc_write_u16(vs, h); |
189 | 24236869 | bellard | |
190 | 24236869 | bellard | vnc_write_s32(vs, encoding); |
191 | 24236869 | bellard | } |
192 | 24236869 | bellard | |
193 | 24236869 | bellard | static void vnc_dpy_resize(DisplayState *ds, int w, int h) |
194 | 24236869 | bellard | { |
195 | 73e14b62 | ths | int size_changed;
|
196 | 24236869 | bellard | VncState *vs = ds->opaque; |
197 | 24236869 | bellard | |
198 | 24236869 | bellard | ds->data = realloc(ds->data, w * h * vs->depth); |
199 | 24236869 | bellard | vs->old_data = realloc(vs->old_data, w * h * vs->depth); |
200 | 24236869 | bellard | |
201 | 24236869 | bellard | if (ds->data == NULL || vs->old_data == NULL) { |
202 | 24236869 | bellard | fprintf(stderr, "vnc: memory allocation failed\n");
|
203 | 24236869 | bellard | exit(1);
|
204 | 24236869 | bellard | } |
205 | 24236869 | bellard | |
206 | 24236869 | bellard | ds->depth = vs->depth * 8;
|
207 | 73e14b62 | ths | size_changed = ds->width != w || ds->height != h; |
208 | 24236869 | bellard | ds->width = w; |
209 | 24236869 | bellard | ds->height = h; |
210 | 24236869 | bellard | ds->linesize = w * vs->depth; |
211 | 73e14b62 | ths | if (vs->csock != -1 && vs->has_resize && size_changed) { |
212 | 24236869 | bellard | vnc_write_u8(vs, 0); /* msg id */ |
213 | 24236869 | bellard | vnc_write_u8(vs, 0);
|
214 | 24236869 | bellard | vnc_write_u16(vs, 1); /* number of rects */ |
215 | 24236869 | bellard | vnc_framebuffer_update(vs, 0, 0, ds->width, ds->height, -223); |
216 | 24236869 | bellard | vnc_flush(vs); |
217 | 24236869 | bellard | vs->width = ds->width; |
218 | 24236869 | bellard | vs->height = ds->height; |
219 | 24236869 | bellard | } |
220 | 24236869 | bellard | } |
221 | 24236869 | bellard | |
222 | 3512779a | bellard | /* fastest code */
|
223 | 3512779a | bellard | static void vnc_write_pixels_copy(VncState *vs, void *pixels, int size) |
224 | 3512779a | bellard | { |
225 | 3512779a | bellard | vnc_write(vs, pixels, size); |
226 | 3512779a | bellard | } |
227 | 3512779a | bellard | |
228 | 3512779a | bellard | /* slowest but generic code. */
|
229 | 3512779a | bellard | static void vnc_convert_pixel(VncState *vs, uint8_t *buf, uint32_t v) |
230 | 3512779a | bellard | { |
231 | 3512779a | bellard | unsigned int r, g, b; |
232 | 3512779a | bellard | |
233 | 3512779a | bellard | r = (v >> vs->red_shift1) & vs->red_max; |
234 | 3512779a | bellard | g = (v >> vs->green_shift1) & vs->green_max; |
235 | 3512779a | bellard | b = (v >> vs->blue_shift1) & vs->blue_max; |
236 | 3512779a | bellard | v = (r << vs->red_shift) | |
237 | 3512779a | bellard | (g << vs->green_shift) | |
238 | 3512779a | bellard | (b << vs->blue_shift); |
239 | 3512779a | bellard | switch(vs->pix_bpp) {
|
240 | 3512779a | bellard | case 1: |
241 | 3512779a | bellard | buf[0] = v;
|
242 | 3512779a | bellard | break;
|
243 | 3512779a | bellard | case 2: |
244 | 3512779a | bellard | if (vs->pix_big_endian) {
|
245 | 3512779a | bellard | buf[0] = v >> 8; |
246 | 3512779a | bellard | buf[1] = v;
|
247 | 3512779a | bellard | } else {
|
248 | 3512779a | bellard | buf[1] = v >> 8; |
249 | 3512779a | bellard | buf[0] = v;
|
250 | 3512779a | bellard | } |
251 | 3512779a | bellard | break;
|
252 | 3512779a | bellard | default:
|
253 | 3512779a | bellard | case 4: |
254 | 3512779a | bellard | if (vs->pix_big_endian) {
|
255 | 3512779a | bellard | buf[0] = v >> 24; |
256 | 3512779a | bellard | buf[1] = v >> 16; |
257 | 3512779a | bellard | buf[2] = v >> 8; |
258 | 3512779a | bellard | buf[3] = v;
|
259 | 3512779a | bellard | } else {
|
260 | 3512779a | bellard | buf[3] = v >> 24; |
261 | 3512779a | bellard | buf[2] = v >> 16; |
262 | 3512779a | bellard | buf[1] = v >> 8; |
263 | 3512779a | bellard | buf[0] = v;
|
264 | 3512779a | bellard | } |
265 | 3512779a | bellard | break;
|
266 | 3512779a | bellard | } |
267 | 3512779a | bellard | } |
268 | 3512779a | bellard | |
269 | 3512779a | bellard | static void vnc_write_pixels_generic(VncState *vs, void *pixels1, int size) |
270 | 3512779a | bellard | { |
271 | 3512779a | bellard | uint32_t *pixels = pixels1; |
272 | 3512779a | bellard | uint8_t buf[4];
|
273 | 3512779a | bellard | int n, i;
|
274 | 3512779a | bellard | |
275 | 3512779a | bellard | n = size >> 2;
|
276 | 3512779a | bellard | for(i = 0; i < n; i++) { |
277 | 3512779a | bellard | vnc_convert_pixel(vs, buf, pixels[i]); |
278 | 3512779a | bellard | vnc_write(vs, buf, vs->pix_bpp); |
279 | 3512779a | bellard | } |
280 | 3512779a | bellard | } |
281 | 3512779a | bellard | |
282 | 24236869 | bellard | static void send_framebuffer_update_raw(VncState *vs, int x, int y, int w, int h) |
283 | 24236869 | bellard | { |
284 | 24236869 | bellard | int i;
|
285 | 24236869 | bellard | char *row;
|
286 | 24236869 | bellard | |
287 | 24236869 | bellard | vnc_framebuffer_update(vs, x, y, w, h, 0);
|
288 | 24236869 | bellard | |
289 | 24236869 | bellard | row = vs->ds->data + y * vs->ds->linesize + x * vs->depth; |
290 | 24236869 | bellard | for (i = 0; i < h; i++) { |
291 | 3512779a | bellard | vs->write_pixels(vs, row, w * vs->depth); |
292 | 24236869 | bellard | row += vs->ds->linesize; |
293 | 24236869 | bellard | } |
294 | 24236869 | bellard | } |
295 | 24236869 | bellard | |
296 | 24236869 | bellard | static void hextile_enc_cord(uint8_t *ptr, int x, int y, int w, int h) |
297 | 24236869 | bellard | { |
298 | 24236869 | bellard | ptr[0] = ((x & 0x0F) << 4) | (y & 0x0F); |
299 | 24236869 | bellard | ptr[1] = (((w - 1) & 0x0F) << 4) | ((h - 1) & 0x0F); |
300 | 24236869 | bellard | } |
301 | 24236869 | bellard | |
302 | 24236869 | bellard | #define BPP 8 |
303 | 24236869 | bellard | #include "vnchextile.h" |
304 | 24236869 | bellard | #undef BPP
|
305 | 24236869 | bellard | |
306 | 24236869 | bellard | #define BPP 16 |
307 | 24236869 | bellard | #include "vnchextile.h" |
308 | 24236869 | bellard | #undef BPP
|
309 | 24236869 | bellard | |
310 | 24236869 | bellard | #define BPP 32 |
311 | 24236869 | bellard | #include "vnchextile.h" |
312 | 24236869 | bellard | #undef BPP
|
313 | 24236869 | bellard | |
314 | 3512779a | bellard | #define GENERIC
|
315 | 3512779a | bellard | #define BPP 32 |
316 | 3512779a | bellard | #include "vnchextile.h" |
317 | 3512779a | bellard | #undef BPP
|
318 | 3512779a | bellard | #undef GENERIC
|
319 | 3512779a | bellard | |
320 | 24236869 | bellard | static void send_framebuffer_update_hextile(VncState *vs, int x, int y, int w, int h) |
321 | 24236869 | bellard | { |
322 | 24236869 | bellard | int i, j;
|
323 | 24236869 | bellard | int has_fg, has_bg;
|
324 | 24236869 | bellard | uint32_t last_fg32, last_bg32; |
325 | 24236869 | bellard | |
326 | 24236869 | bellard | vnc_framebuffer_update(vs, x, y, w, h, 5);
|
327 | 24236869 | bellard | |
328 | 24236869 | bellard | has_fg = has_bg = 0;
|
329 | 24236869 | bellard | for (j = y; j < (y + h); j += 16) { |
330 | 24236869 | bellard | for (i = x; i < (x + w); i += 16) { |
331 | 3512779a | bellard | vs->send_hextile_tile(vs, i, j, |
332 | 3512779a | bellard | MIN(16, x + w - i), MIN(16, y + h - j), |
333 | 3512779a | bellard | &last_bg32, &last_fg32, &has_bg, &has_fg); |
334 | 24236869 | bellard | } |
335 | 24236869 | bellard | } |
336 | 24236869 | bellard | } |
337 | 24236869 | bellard | |
338 | 24236869 | bellard | static void send_framebuffer_update(VncState *vs, int x, int y, int w, int h) |
339 | 24236869 | bellard | { |
340 | 24236869 | bellard | if (vs->has_hextile)
|
341 | 24236869 | bellard | send_framebuffer_update_hextile(vs, x, y, w, h); |
342 | 24236869 | bellard | else
|
343 | 24236869 | bellard | send_framebuffer_update_raw(vs, x, y, w, h); |
344 | 24236869 | bellard | } |
345 | 24236869 | bellard | |
346 | 24236869 | bellard | static void vnc_copy(DisplayState *ds, int src_x, int src_y, int dst_x, int dst_y, int w, int h) |
347 | 24236869 | bellard | { |
348 | 24236869 | bellard | int src, dst;
|
349 | 24236869 | bellard | char *src_row;
|
350 | 24236869 | bellard | char *dst_row;
|
351 | 24236869 | bellard | char *old_row;
|
352 | 24236869 | bellard | int y = 0; |
353 | 24236869 | bellard | int pitch = ds->linesize;
|
354 | 24236869 | bellard | VncState *vs = ds->opaque; |
355 | 24236869 | bellard | |
356 | 24236869 | bellard | vnc_update_client(vs); |
357 | 24236869 | bellard | |
358 | 24236869 | bellard | if (dst_y > src_y) {
|
359 | 24236869 | bellard | y = h - 1;
|
360 | 24236869 | bellard | pitch = -pitch; |
361 | 24236869 | bellard | } |
362 | 24236869 | bellard | |
363 | 24236869 | bellard | src = (ds->linesize * (src_y + y) + vs->depth * src_x); |
364 | 24236869 | bellard | dst = (ds->linesize * (dst_y + y) + vs->depth * dst_x); |
365 | 24236869 | bellard | |
366 | 24236869 | bellard | src_row = ds->data + src; |
367 | 24236869 | bellard | dst_row = ds->data + dst; |
368 | 24236869 | bellard | old_row = vs->old_data + dst; |
369 | 24236869 | bellard | |
370 | 24236869 | bellard | for (y = 0; y < h; y++) { |
371 | 24236869 | bellard | memmove(old_row, src_row, w * vs->depth); |
372 | 24236869 | bellard | memmove(dst_row, src_row, w * vs->depth); |
373 | 24236869 | bellard | src_row += pitch; |
374 | 24236869 | bellard | dst_row += pitch; |
375 | 24236869 | bellard | old_row += pitch; |
376 | 24236869 | bellard | } |
377 | 24236869 | bellard | |
378 | 24236869 | bellard | vnc_write_u8(vs, 0); /* msg id */ |
379 | 24236869 | bellard | vnc_write_u8(vs, 0);
|
380 | 24236869 | bellard | vnc_write_u16(vs, 1); /* number of rects */ |
381 | 24236869 | bellard | vnc_framebuffer_update(vs, dst_x, dst_y, w, h, 1);
|
382 | 24236869 | bellard | vnc_write_u16(vs, src_x); |
383 | 24236869 | bellard | vnc_write_u16(vs, src_y); |
384 | 24236869 | bellard | vnc_flush(vs); |
385 | 24236869 | bellard | } |
386 | 24236869 | bellard | |
387 | 24236869 | bellard | static int find_dirty_height(VncState *vs, int y, int last_x, int x) |
388 | 24236869 | bellard | { |
389 | 24236869 | bellard | int h;
|
390 | 24236869 | bellard | |
391 | 24236869 | bellard | for (h = 1; h < (vs->height - y); h++) { |
392 | 24236869 | bellard | int tmp_x;
|
393 | 99589bdc | bellard | if (!vnc_get_bit(vs->dirty_row[y + h], last_x))
|
394 | 24236869 | bellard | break;
|
395 | 24236869 | bellard | for (tmp_x = last_x; tmp_x < x; tmp_x++)
|
396 | 99589bdc | bellard | vnc_clear_bit(vs->dirty_row[y + h], tmp_x); |
397 | 24236869 | bellard | } |
398 | 24236869 | bellard | |
399 | 24236869 | bellard | return h;
|
400 | 24236869 | bellard | } |
401 | 24236869 | bellard | |
402 | 24236869 | bellard | static void vnc_update_client(void *opaque) |
403 | 24236869 | bellard | { |
404 | 24236869 | bellard | VncState *vs = opaque; |
405 | 24236869 | bellard | |
406 | 24236869 | bellard | if (vs->need_update && vs->csock != -1) { |
407 | 24236869 | bellard | int y;
|
408 | 24236869 | bellard | char *row;
|
409 | 24236869 | bellard | char *old_row;
|
410 | 99589bdc | bellard | uint32_t width_mask[VNC_DIRTY_WORDS]; |
411 | 24236869 | bellard | int n_rectangles;
|
412 | 24236869 | bellard | int saved_offset;
|
413 | 24236869 | bellard | int has_dirty = 0; |
414 | 24236869 | bellard | |
415 | 99589bdc | bellard | vnc_set_bits(width_mask, (vs->width / 16), VNC_DIRTY_WORDS);
|
416 | 24236869 | bellard | |
417 | 24236869 | bellard | /* Walk through the dirty map and eliminate tiles that
|
418 | 24236869 | bellard | really aren't dirty */
|
419 | 24236869 | bellard | row = vs->ds->data; |
420 | 24236869 | bellard | old_row = vs->old_data; |
421 | 24236869 | bellard | |
422 | 24236869 | bellard | for (y = 0; y < vs->height; y++) { |
423 | 99589bdc | bellard | if (vnc_and_bits(vs->dirty_row[y], width_mask, VNC_DIRTY_WORDS)) {
|
424 | 24236869 | bellard | int x;
|
425 | 24236869 | bellard | char *ptr, *old_ptr;
|
426 | 24236869 | bellard | |
427 | 24236869 | bellard | ptr = row; |
428 | 24236869 | bellard | old_ptr = old_row; |
429 | 24236869 | bellard | |
430 | 24236869 | bellard | for (x = 0; x < vs->ds->width; x += 16) { |
431 | 24236869 | bellard | if (memcmp(old_ptr, ptr, 16 * vs->depth) == 0) { |
432 | 99589bdc | bellard | vnc_clear_bit(vs->dirty_row[y], (x / 16));
|
433 | 24236869 | bellard | } else {
|
434 | 24236869 | bellard | has_dirty = 1;
|
435 | 24236869 | bellard | memcpy(old_ptr, ptr, 16 * vs->depth);
|
436 | 24236869 | bellard | } |
437 | 24236869 | bellard | |
438 | 24236869 | bellard | ptr += 16 * vs->depth;
|
439 | 24236869 | bellard | old_ptr += 16 * vs->depth;
|
440 | 24236869 | bellard | } |
441 | 24236869 | bellard | } |
442 | 24236869 | bellard | |
443 | 24236869 | bellard | row += vs->ds->linesize; |
444 | 24236869 | bellard | old_row += vs->ds->linesize; |
445 | 24236869 | bellard | } |
446 | 24236869 | bellard | |
447 | 24236869 | bellard | if (!has_dirty) {
|
448 | 24236869 | bellard | qemu_mod_timer(vs->timer, qemu_get_clock(rt_clock) + VNC_REFRESH_INTERVAL); |
449 | 24236869 | bellard | return;
|
450 | 24236869 | bellard | } |
451 | 24236869 | bellard | |
452 | 24236869 | bellard | /* Count rectangles */
|
453 | 24236869 | bellard | n_rectangles = 0;
|
454 | 24236869 | bellard | vnc_write_u8(vs, 0); /* msg id */ |
455 | 24236869 | bellard | vnc_write_u8(vs, 0);
|
456 | 24236869 | bellard | saved_offset = vs->output.offset; |
457 | 24236869 | bellard | vnc_write_u16(vs, 0);
|
458 | 24236869 | bellard | |
459 | 24236869 | bellard | for (y = 0; y < vs->height; y++) { |
460 | 24236869 | bellard | int x;
|
461 | 24236869 | bellard | int last_x = -1; |
462 | 24236869 | bellard | for (x = 0; x < vs->width / 16; x++) { |
463 | 99589bdc | bellard | if (vnc_get_bit(vs->dirty_row[y], x)) {
|
464 | 24236869 | bellard | if (last_x == -1) { |
465 | 24236869 | bellard | last_x = x; |
466 | 24236869 | bellard | } |
467 | 99589bdc | bellard | vnc_clear_bit(vs->dirty_row[y], x); |
468 | 24236869 | bellard | } else {
|
469 | 24236869 | bellard | if (last_x != -1) { |
470 | 24236869 | bellard | int h = find_dirty_height(vs, y, last_x, x);
|
471 | 24236869 | bellard | send_framebuffer_update(vs, last_x * 16, y, (x - last_x) * 16, h); |
472 | 24236869 | bellard | n_rectangles++; |
473 | 24236869 | bellard | } |
474 | 24236869 | bellard | last_x = -1;
|
475 | 24236869 | bellard | } |
476 | 24236869 | bellard | } |
477 | 24236869 | bellard | if (last_x != -1) { |
478 | 24236869 | bellard | int h = find_dirty_height(vs, y, last_x, x);
|
479 | 24236869 | bellard | send_framebuffer_update(vs, last_x * 16, y, (x - last_x) * 16, h); |
480 | 24236869 | bellard | n_rectangles++; |
481 | 24236869 | bellard | } |
482 | 24236869 | bellard | } |
483 | 24236869 | bellard | vs->output.buffer[saved_offset] = (n_rectangles >> 8) & 0xFF; |
484 | 24236869 | bellard | vs->output.buffer[saved_offset + 1] = n_rectangles & 0xFF; |
485 | 24236869 | bellard | vnc_flush(vs); |
486 | 24236869 | bellard | |
487 | 24236869 | bellard | } |
488 | 24236869 | bellard | qemu_mod_timer(vs->timer, qemu_get_clock(rt_clock) + VNC_REFRESH_INTERVAL); |
489 | 24236869 | bellard | } |
490 | 24236869 | bellard | |
491 | 24236869 | bellard | static void vnc_timer_init(VncState *vs) |
492 | 24236869 | bellard | { |
493 | 24236869 | bellard | if (vs->timer == NULL) { |
494 | 24236869 | bellard | vs->timer = qemu_new_timer(rt_clock, vnc_update_client, vs); |
495 | 24236869 | bellard | qemu_mod_timer(vs->timer, qemu_get_clock(rt_clock)); |
496 | 24236869 | bellard | } |
497 | 24236869 | bellard | } |
498 | 24236869 | bellard | |
499 | 24236869 | bellard | static void vnc_dpy_refresh(DisplayState *ds) |
500 | 24236869 | bellard | { |
501 | 24236869 | bellard | VncState *vs = ds->opaque; |
502 | 24236869 | bellard | vnc_timer_init(vs); |
503 | 24236869 | bellard | vga_hw_update(); |
504 | 24236869 | bellard | } |
505 | 24236869 | bellard | |
506 | 24236869 | bellard | static int vnc_listen_poll(void *opaque) |
507 | 24236869 | bellard | { |
508 | 24236869 | bellard | VncState *vs = opaque; |
509 | 24236869 | bellard | if (vs->csock == -1) |
510 | 24236869 | bellard | return 1; |
511 | 24236869 | bellard | return 0; |
512 | 24236869 | bellard | } |
513 | 24236869 | bellard | |
514 | 24236869 | bellard | static void buffer_reserve(Buffer *buffer, size_t len) |
515 | 24236869 | bellard | { |
516 | 24236869 | bellard | if ((buffer->capacity - buffer->offset) < len) {
|
517 | 24236869 | bellard | buffer->capacity += (len + 1024);
|
518 | 24236869 | bellard | buffer->buffer = realloc(buffer->buffer, buffer->capacity); |
519 | 24236869 | bellard | if (buffer->buffer == NULL) { |
520 | 24236869 | bellard | fprintf(stderr, "vnc: out of memory\n");
|
521 | 24236869 | bellard | exit(1);
|
522 | 24236869 | bellard | } |
523 | 24236869 | bellard | } |
524 | 24236869 | bellard | } |
525 | 24236869 | bellard | |
526 | 24236869 | bellard | static int buffer_empty(Buffer *buffer) |
527 | 24236869 | bellard | { |
528 | 24236869 | bellard | return buffer->offset == 0; |
529 | 24236869 | bellard | } |
530 | 24236869 | bellard | |
531 | 24236869 | bellard | static char *buffer_end(Buffer *buffer) |
532 | 24236869 | bellard | { |
533 | 24236869 | bellard | return buffer->buffer + buffer->offset;
|
534 | 24236869 | bellard | } |
535 | 24236869 | bellard | |
536 | 24236869 | bellard | static void buffer_reset(Buffer *buffer) |
537 | 24236869 | bellard | { |
538 | 24236869 | bellard | buffer->offset = 0;
|
539 | 24236869 | bellard | } |
540 | 24236869 | bellard | |
541 | 24236869 | bellard | static void buffer_append(Buffer *buffer, const void *data, size_t len) |
542 | 24236869 | bellard | { |
543 | 24236869 | bellard | memcpy(buffer->buffer + buffer->offset, data, len); |
544 | 24236869 | bellard | buffer->offset += len; |
545 | 24236869 | bellard | } |
546 | 24236869 | bellard | |
547 | 6ca957f0 | bellard | static int vnc_client_io_error(VncState *vs, int ret, int last_errno) |
548 | 24236869 | bellard | { |
549 | 24236869 | bellard | if (ret == 0 || ret == -1) { |
550 | 6ca957f0 | bellard | if (ret == -1 && (last_errno == EINTR || last_errno == EAGAIN)) |
551 | 24236869 | bellard | return 0; |
552 | 24236869 | bellard | |
553 | 24236869 | bellard | qemu_set_fd_handler2(vs->csock, NULL, NULL, NULL, NULL); |
554 | 6ca957f0 | bellard | closesocket(vs->csock); |
555 | 24236869 | bellard | vs->csock = -1;
|
556 | 24236869 | bellard | buffer_reset(&vs->input); |
557 | 24236869 | bellard | buffer_reset(&vs->output); |
558 | 24236869 | bellard | vs->need_update = 0;
|
559 | 24236869 | bellard | return 0; |
560 | 24236869 | bellard | } |
561 | 24236869 | bellard | return ret;
|
562 | 24236869 | bellard | } |
563 | 24236869 | bellard | |
564 | 24236869 | bellard | static void vnc_client_error(VncState *vs) |
565 | 24236869 | bellard | { |
566 | 6ca957f0 | bellard | vnc_client_io_error(vs, -1, EINVAL);
|
567 | 24236869 | bellard | } |
568 | 24236869 | bellard | |
569 | 24236869 | bellard | static void vnc_client_write(void *opaque) |
570 | 24236869 | bellard | { |
571 | ceb5caaf | bellard | long ret;
|
572 | 24236869 | bellard | VncState *vs = opaque; |
573 | 24236869 | bellard | |
574 | 6ca957f0 | bellard | ret = send(vs->csock, vs->output.buffer, vs->output.offset, 0);
|
575 | 6ca957f0 | bellard | ret = vnc_client_io_error(vs, ret, socket_error()); |
576 | 24236869 | bellard | if (!ret)
|
577 | 24236869 | bellard | return;
|
578 | 24236869 | bellard | |
579 | 24236869 | bellard | memmove(vs->output.buffer, vs->output.buffer + ret, (vs->output.offset - ret)); |
580 | 24236869 | bellard | vs->output.offset -= ret; |
581 | 24236869 | bellard | |
582 | 24236869 | bellard | if (vs->output.offset == 0) { |
583 | 24236869 | bellard | qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs); |
584 | 24236869 | bellard | } |
585 | 24236869 | bellard | } |
586 | 24236869 | bellard | |
587 | 24236869 | bellard | static void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting) |
588 | 24236869 | bellard | { |
589 | 24236869 | bellard | vs->read_handler = func; |
590 | 24236869 | bellard | vs->read_handler_expect = expecting; |
591 | 24236869 | bellard | } |
592 | 24236869 | bellard | |
593 | 24236869 | bellard | static void vnc_client_read(void *opaque) |
594 | 24236869 | bellard | { |
595 | 24236869 | bellard | VncState *vs = opaque; |
596 | ceb5caaf | bellard | long ret;
|
597 | 24236869 | bellard | |
598 | 24236869 | bellard | buffer_reserve(&vs->input, 4096);
|
599 | 24236869 | bellard | |
600 | 6ca957f0 | bellard | ret = recv(vs->csock, buffer_end(&vs->input), 4096, 0); |
601 | 6ca957f0 | bellard | ret = vnc_client_io_error(vs, ret, socket_error()); |
602 | 24236869 | bellard | if (!ret)
|
603 | 24236869 | bellard | return;
|
604 | 24236869 | bellard | |
605 | 24236869 | bellard | vs->input.offset += ret; |
606 | 24236869 | bellard | |
607 | 24236869 | bellard | while (vs->read_handler && vs->input.offset >= vs->read_handler_expect) {
|
608 | 24236869 | bellard | size_t len = vs->read_handler_expect; |
609 | 24236869 | bellard | int ret;
|
610 | 24236869 | bellard | |
611 | 24236869 | bellard | ret = vs->read_handler(vs, vs->input.buffer, len); |
612 | 24236869 | bellard | if (vs->csock == -1) |
613 | 24236869 | bellard | return;
|
614 | 24236869 | bellard | |
615 | 24236869 | bellard | if (!ret) {
|
616 | 24236869 | bellard | memmove(vs->input.buffer, vs->input.buffer + len, (vs->input.offset - len)); |
617 | 24236869 | bellard | vs->input.offset -= len; |
618 | 24236869 | bellard | } else {
|
619 | 24236869 | bellard | vs->read_handler_expect = ret; |
620 | 24236869 | bellard | } |
621 | 24236869 | bellard | } |
622 | 24236869 | bellard | } |
623 | 24236869 | bellard | |
624 | 24236869 | bellard | static void vnc_write(VncState *vs, const void *data, size_t len) |
625 | 24236869 | bellard | { |
626 | 24236869 | bellard | buffer_reserve(&vs->output, len); |
627 | 24236869 | bellard | |
628 | 24236869 | bellard | if (buffer_empty(&vs->output)) {
|
629 | 24236869 | bellard | qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, vnc_client_write, vs);
|
630 | 24236869 | bellard | } |
631 | 24236869 | bellard | |
632 | 24236869 | bellard | buffer_append(&vs->output, data, len); |
633 | 24236869 | bellard | } |
634 | 24236869 | bellard | |
635 | 24236869 | bellard | static void vnc_write_s32(VncState *vs, int32_t value) |
636 | 24236869 | bellard | { |
637 | 24236869 | bellard | vnc_write_u32(vs, *(uint32_t *)&value); |
638 | 24236869 | bellard | } |
639 | 24236869 | bellard | |
640 | 24236869 | bellard | static void vnc_write_u32(VncState *vs, uint32_t value) |
641 | 24236869 | bellard | { |
642 | 24236869 | bellard | uint8_t buf[4];
|
643 | 24236869 | bellard | |
644 | 24236869 | bellard | buf[0] = (value >> 24) & 0xFF; |
645 | 24236869 | bellard | buf[1] = (value >> 16) & 0xFF; |
646 | 24236869 | bellard | buf[2] = (value >> 8) & 0xFF; |
647 | 24236869 | bellard | buf[3] = value & 0xFF; |
648 | 24236869 | bellard | |
649 | 24236869 | bellard | vnc_write(vs, buf, 4);
|
650 | 24236869 | bellard | } |
651 | 24236869 | bellard | |
652 | 24236869 | bellard | static void vnc_write_u16(VncState *vs, uint16_t value) |
653 | 24236869 | bellard | { |
654 | 64f5a135 | bellard | uint8_t buf[2];
|
655 | 24236869 | bellard | |
656 | 24236869 | bellard | buf[0] = (value >> 8) & 0xFF; |
657 | 24236869 | bellard | buf[1] = value & 0xFF; |
658 | 24236869 | bellard | |
659 | 24236869 | bellard | vnc_write(vs, buf, 2);
|
660 | 24236869 | bellard | } |
661 | 24236869 | bellard | |
662 | 24236869 | bellard | static void vnc_write_u8(VncState *vs, uint8_t value) |
663 | 24236869 | bellard | { |
664 | 24236869 | bellard | vnc_write(vs, (char *)&value, 1); |
665 | 24236869 | bellard | } |
666 | 24236869 | bellard | |
667 | 24236869 | bellard | static void vnc_flush(VncState *vs) |
668 | 24236869 | bellard | { |
669 | 24236869 | bellard | if (vs->output.offset)
|
670 | 24236869 | bellard | vnc_client_write(vs); |
671 | 24236869 | bellard | } |
672 | 24236869 | bellard | |
673 | 64f5a135 | bellard | static uint8_t read_u8(uint8_t *data, size_t offset)
|
674 | 24236869 | bellard | { |
675 | 24236869 | bellard | return data[offset];
|
676 | 24236869 | bellard | } |
677 | 24236869 | bellard | |
678 | 64f5a135 | bellard | static uint16_t read_u16(uint8_t *data, size_t offset)
|
679 | 24236869 | bellard | { |
680 | 24236869 | bellard | return ((data[offset] & 0xFF) << 8) | (data[offset + 1] & 0xFF); |
681 | 24236869 | bellard | } |
682 | 24236869 | bellard | |
683 | 64f5a135 | bellard | static int32_t read_s32(uint8_t *data, size_t offset)
|
684 | 24236869 | bellard | { |
685 | 24236869 | bellard | return (int32_t)((data[offset] << 24) | (data[offset + 1] << 16) | |
686 | 24236869 | bellard | (data[offset + 2] << 8) | data[offset + 3]); |
687 | 24236869 | bellard | } |
688 | 24236869 | bellard | |
689 | 64f5a135 | bellard | static uint32_t read_u32(uint8_t *data, size_t offset)
|
690 | 24236869 | bellard | { |
691 | 24236869 | bellard | return ((data[offset] << 24) | (data[offset + 1] << 16) | |
692 | 24236869 | bellard | (data[offset + 2] << 8) | data[offset + 3]); |
693 | 24236869 | bellard | } |
694 | 24236869 | bellard | |
695 | 24236869 | bellard | static void client_cut_text(VncState *vs, size_t len, char *text) |
696 | 24236869 | bellard | { |
697 | 24236869 | bellard | } |
698 | 24236869 | bellard | |
699 | 564c337e | bellard | static void check_pointer_type_change(VncState *vs, int absolute) |
700 | 564c337e | bellard | { |
701 | 564c337e | bellard | if (vs->has_pointer_type_change && vs->absolute != absolute) {
|
702 | 564c337e | bellard | vnc_write_u8(vs, 0);
|
703 | 564c337e | bellard | vnc_write_u8(vs, 0);
|
704 | 564c337e | bellard | vnc_write_u16(vs, 1);
|
705 | 564c337e | bellard | vnc_framebuffer_update(vs, absolute, 0,
|
706 | 564c337e | bellard | vs->ds->width, vs->ds->height, -257);
|
707 | 564c337e | bellard | vnc_flush(vs); |
708 | 564c337e | bellard | } |
709 | 564c337e | bellard | vs->absolute = absolute; |
710 | 564c337e | bellard | } |
711 | 564c337e | bellard | |
712 | 24236869 | bellard | static void pointer_event(VncState *vs, int button_mask, int x, int y) |
713 | 24236869 | bellard | { |
714 | 24236869 | bellard | int buttons = 0; |
715 | 24236869 | bellard | int dz = 0; |
716 | 24236869 | bellard | |
717 | 24236869 | bellard | if (button_mask & 0x01) |
718 | 24236869 | bellard | buttons |= MOUSE_EVENT_LBUTTON; |
719 | 24236869 | bellard | if (button_mask & 0x02) |
720 | 24236869 | bellard | buttons |= MOUSE_EVENT_MBUTTON; |
721 | 24236869 | bellard | if (button_mask & 0x04) |
722 | 24236869 | bellard | buttons |= MOUSE_EVENT_RBUTTON; |
723 | 24236869 | bellard | if (button_mask & 0x08) |
724 | 24236869 | bellard | dz = -1;
|
725 | 24236869 | bellard | if (button_mask & 0x10) |
726 | 24236869 | bellard | dz = 1;
|
727 | 564c337e | bellard | |
728 | 564c337e | bellard | if (vs->absolute) {
|
729 | 24236869 | bellard | kbd_mouse_event(x * 0x7FFF / vs->ds->width,
|
730 | 24236869 | bellard | y * 0x7FFF / vs->ds->height,
|
731 | 24236869 | bellard | dz, buttons); |
732 | 564c337e | bellard | } else if (vs->has_pointer_type_change) { |
733 | 564c337e | bellard | x -= 0x7FFF;
|
734 | 564c337e | bellard | y -= 0x7FFF;
|
735 | 24236869 | bellard | |
736 | 564c337e | bellard | kbd_mouse_event(x, y, dz, buttons); |
737 | 564c337e | bellard | } else {
|
738 | 564c337e | bellard | if (vs->last_x != -1) |
739 | 564c337e | bellard | kbd_mouse_event(x - vs->last_x, |
740 | 564c337e | bellard | y - vs->last_y, |
741 | 564c337e | bellard | dz, buttons); |
742 | 564c337e | bellard | vs->last_x = x; |
743 | 564c337e | bellard | vs->last_y = y; |
744 | 24236869 | bellard | } |
745 | 564c337e | bellard | |
746 | 564c337e | bellard | check_pointer_type_change(vs, kbd_mouse_is_absolute()); |
747 | 24236869 | bellard | } |
748 | 24236869 | bellard | |
749 | 64f5a135 | bellard | static void reset_keys(VncState *vs) |
750 | 64f5a135 | bellard | { |
751 | 64f5a135 | bellard | int i;
|
752 | 64f5a135 | bellard | for(i = 0; i < 256; i++) { |
753 | 64f5a135 | bellard | if (vs->modifiers_state[i]) {
|
754 | 64f5a135 | bellard | if (i & 0x80) |
755 | 64f5a135 | bellard | kbd_put_keycode(0xe0);
|
756 | 64f5a135 | bellard | kbd_put_keycode(i | 0x80);
|
757 | 64f5a135 | bellard | vs->modifiers_state[i] = 0;
|
758 | 64f5a135 | bellard | } |
759 | 64f5a135 | bellard | } |
760 | 64f5a135 | bellard | } |
761 | 64f5a135 | bellard | |
762 | bdbd7676 | bellard | static void do_key_event(VncState *vs, int down, uint32_t sym) |
763 | 24236869 | bellard | { |
764 | 24236869 | bellard | int keycode;
|
765 | 24236869 | bellard | |
766 | 24236869 | bellard | keycode = keysym2scancode(vs->kbd_layout, sym & 0xFFFF);
|
767 | 64f5a135 | bellard | |
768 | 64f5a135 | bellard | /* QEMU console switch */
|
769 | 64f5a135 | bellard | switch(keycode) {
|
770 | 64f5a135 | bellard | case 0x2a: /* Left Shift */ |
771 | 64f5a135 | bellard | case 0x36: /* Right Shift */ |
772 | 64f5a135 | bellard | case 0x1d: /* Left CTRL */ |
773 | 64f5a135 | bellard | case 0x9d: /* Right CTRL */ |
774 | 64f5a135 | bellard | case 0x38: /* Left ALT */ |
775 | 64f5a135 | bellard | case 0xb8: /* Right ALT */ |
776 | 64f5a135 | bellard | if (down)
|
777 | 64f5a135 | bellard | vs->modifiers_state[keycode] = 1;
|
778 | 64f5a135 | bellard | else
|
779 | 64f5a135 | bellard | vs->modifiers_state[keycode] = 0;
|
780 | 64f5a135 | bellard | break;
|
781 | 64f5a135 | bellard | case 0x02 ... 0x0a: /* '1' to '9' keys */ |
782 | 64f5a135 | bellard | if (down && vs->modifiers_state[0x1d] && vs->modifiers_state[0x38]) { |
783 | 64f5a135 | bellard | /* Reset the modifiers sent to the current console */
|
784 | 64f5a135 | bellard | reset_keys(vs); |
785 | 64f5a135 | bellard | console_select(keycode - 0x02);
|
786 | 64f5a135 | bellard | return;
|
787 | 64f5a135 | bellard | } |
788 | 64f5a135 | bellard | break;
|
789 | 64f5a135 | bellard | } |
790 | 24236869 | bellard | |
791 | 64f5a135 | bellard | if (is_graphic_console()) {
|
792 | 64f5a135 | bellard | if (keycode & 0x80) |
793 | 64f5a135 | bellard | kbd_put_keycode(0xe0);
|
794 | 64f5a135 | bellard | if (down)
|
795 | 64f5a135 | bellard | kbd_put_keycode(keycode & 0x7f);
|
796 | 64f5a135 | bellard | else
|
797 | 64f5a135 | bellard | kbd_put_keycode(keycode | 0x80);
|
798 | 64f5a135 | bellard | } else {
|
799 | 64f5a135 | bellard | /* QEMU console emulation */
|
800 | 64f5a135 | bellard | if (down) {
|
801 | 64f5a135 | bellard | switch (keycode) {
|
802 | 64f5a135 | bellard | case 0x2a: /* Left Shift */ |
803 | 64f5a135 | bellard | case 0x36: /* Right Shift */ |
804 | 64f5a135 | bellard | case 0x1d: /* Left CTRL */ |
805 | 64f5a135 | bellard | case 0x9d: /* Right CTRL */ |
806 | 64f5a135 | bellard | case 0x38: /* Left ALT */ |
807 | 64f5a135 | bellard | case 0xb8: /* Right ALT */ |
808 | 64f5a135 | bellard | break;
|
809 | 64f5a135 | bellard | case 0xc8: |
810 | 64f5a135 | bellard | kbd_put_keysym(QEMU_KEY_UP); |
811 | 64f5a135 | bellard | break;
|
812 | 64f5a135 | bellard | case 0xd0: |
813 | 64f5a135 | bellard | kbd_put_keysym(QEMU_KEY_DOWN); |
814 | 64f5a135 | bellard | break;
|
815 | 64f5a135 | bellard | case 0xcb: |
816 | 64f5a135 | bellard | kbd_put_keysym(QEMU_KEY_LEFT); |
817 | 64f5a135 | bellard | break;
|
818 | 64f5a135 | bellard | case 0xcd: |
819 | 64f5a135 | bellard | kbd_put_keysym(QEMU_KEY_RIGHT); |
820 | 64f5a135 | bellard | break;
|
821 | 64f5a135 | bellard | case 0xd3: |
822 | 64f5a135 | bellard | kbd_put_keysym(QEMU_KEY_DELETE); |
823 | 64f5a135 | bellard | break;
|
824 | 64f5a135 | bellard | case 0xc7: |
825 | 64f5a135 | bellard | kbd_put_keysym(QEMU_KEY_HOME); |
826 | 64f5a135 | bellard | break;
|
827 | 64f5a135 | bellard | case 0xcf: |
828 | 64f5a135 | bellard | kbd_put_keysym(QEMU_KEY_END); |
829 | 64f5a135 | bellard | break;
|
830 | 64f5a135 | bellard | case 0xc9: |
831 | 64f5a135 | bellard | kbd_put_keysym(QEMU_KEY_PAGEUP); |
832 | 64f5a135 | bellard | break;
|
833 | 64f5a135 | bellard | case 0xd1: |
834 | 64f5a135 | bellard | kbd_put_keysym(QEMU_KEY_PAGEDOWN); |
835 | 64f5a135 | bellard | break;
|
836 | 64f5a135 | bellard | default:
|
837 | 64f5a135 | bellard | kbd_put_keysym(sym); |
838 | 64f5a135 | bellard | break;
|
839 | 64f5a135 | bellard | } |
840 | 64f5a135 | bellard | } |
841 | 64f5a135 | bellard | } |
842 | 24236869 | bellard | } |
843 | 24236869 | bellard | |
844 | bdbd7676 | bellard | static void key_event(VncState *vs, int down, uint32_t sym) |
845 | bdbd7676 | bellard | { |
846 | bdbd7676 | bellard | if (sym >= 'A' && sym <= 'Z') |
847 | bdbd7676 | bellard | sym = sym - 'A' + 'a'; |
848 | bdbd7676 | bellard | do_key_event(vs, down, sym); |
849 | bdbd7676 | bellard | } |
850 | bdbd7676 | bellard | |
851 | 24236869 | bellard | static void framebuffer_update_request(VncState *vs, int incremental, |
852 | 24236869 | bellard | int x_position, int y_position, |
853 | 24236869 | bellard | int w, int h) |
854 | 24236869 | bellard | { |
855 | cf2d385c | ths | if (x_position > vs->ds->width)
|
856 | cf2d385c | ths | x_position = vs->ds->width; |
857 | cf2d385c | ths | if (y_position > vs->ds->height)
|
858 | cf2d385c | ths | y_position = vs->ds->height; |
859 | cf2d385c | ths | if (x_position + w >= vs->ds->width)
|
860 | cf2d385c | ths | w = vs->ds->width - x_position; |
861 | cf2d385c | ths | if (y_position + h >= vs->ds->height)
|
862 | cf2d385c | ths | h = vs->ds->height - y_position; |
863 | cf2d385c | ths | |
864 | 24236869 | bellard | int i;
|
865 | 24236869 | bellard | vs->need_update = 1;
|
866 | 24236869 | bellard | if (!incremental) {
|
867 | 24236869 | bellard | char *old_row = vs->old_data + y_position * vs->ds->linesize;
|
868 | 24236869 | bellard | |
869 | 24236869 | bellard | for (i = 0; i < h; i++) { |
870 | 99589bdc | bellard | vnc_set_bits(vs->dirty_row[y_position + i], |
871 | 99589bdc | bellard | (vs->ds->width / 16), VNC_DIRTY_WORDS);
|
872 | 24236869 | bellard | memset(old_row, 42, vs->ds->width * vs->depth);
|
873 | 24236869 | bellard | old_row += vs->ds->linesize; |
874 | 24236869 | bellard | } |
875 | 24236869 | bellard | } |
876 | 24236869 | bellard | } |
877 | 24236869 | bellard | |
878 | 24236869 | bellard | static void set_encodings(VncState *vs, int32_t *encodings, size_t n_encodings) |
879 | 24236869 | bellard | { |
880 | 24236869 | bellard | int i;
|
881 | 24236869 | bellard | |
882 | 24236869 | bellard | vs->has_hextile = 0;
|
883 | 24236869 | bellard | vs->has_resize = 0;
|
884 | 564c337e | bellard | vs->has_pointer_type_change = 0;
|
885 | 564c337e | bellard | vs->absolute = -1;
|
886 | 24236869 | bellard | vs->ds->dpy_copy = NULL;
|
887 | 24236869 | bellard | |
888 | 24236869 | bellard | for (i = n_encodings - 1; i >= 0; i--) { |
889 | 24236869 | bellard | switch (encodings[i]) {
|
890 | 24236869 | bellard | case 0: /* Raw */ |
891 | 24236869 | bellard | vs->has_hextile = 0;
|
892 | 24236869 | bellard | break;
|
893 | 24236869 | bellard | case 1: /* CopyRect */ |
894 | 24236869 | bellard | vs->ds->dpy_copy = vnc_copy; |
895 | 24236869 | bellard | break;
|
896 | 24236869 | bellard | case 5: /* Hextile */ |
897 | 24236869 | bellard | vs->has_hextile = 1;
|
898 | 24236869 | bellard | break;
|
899 | 24236869 | bellard | case -223: /* DesktopResize */ |
900 | 24236869 | bellard | vs->has_resize = 1;
|
901 | 24236869 | bellard | break;
|
902 | 564c337e | bellard | case -257: |
903 | 564c337e | bellard | vs->has_pointer_type_change = 1;
|
904 | 564c337e | bellard | break;
|
905 | 24236869 | bellard | default:
|
906 | 24236869 | bellard | break;
|
907 | 24236869 | bellard | } |
908 | 24236869 | bellard | } |
909 | 564c337e | bellard | |
910 | 564c337e | bellard | check_pointer_type_change(vs, kbd_mouse_is_absolute()); |
911 | 24236869 | bellard | } |
912 | 24236869 | bellard | |
913 | 3512779a | bellard | static int compute_nbits(unsigned int val) |
914 | 3512779a | bellard | { |
915 | 3512779a | bellard | int n;
|
916 | 3512779a | bellard | n = 0;
|
917 | 3512779a | bellard | while (val != 0) { |
918 | 3512779a | bellard | n++; |
919 | 3512779a | bellard | val >>= 1;
|
920 | 3512779a | bellard | } |
921 | 3512779a | bellard | return n;
|
922 | 3512779a | bellard | } |
923 | 3512779a | bellard | |
924 | 24236869 | bellard | static void set_pixel_format(VncState *vs, |
925 | 24236869 | bellard | int bits_per_pixel, int depth, |
926 | 24236869 | bellard | int big_endian_flag, int true_color_flag, |
927 | 24236869 | bellard | int red_max, int green_max, int blue_max, |
928 | 24236869 | bellard | int red_shift, int green_shift, int blue_shift) |
929 | 24236869 | bellard | { |
930 | 3512779a | bellard | int host_big_endian_flag;
|
931 | 24236869 | bellard | |
932 | 3512779a | bellard | #ifdef WORDS_BIGENDIAN
|
933 | 3512779a | bellard | host_big_endian_flag = 1;
|
934 | 3512779a | bellard | #else
|
935 | 3512779a | bellard | host_big_endian_flag = 0;
|
936 | 3512779a | bellard | #endif
|
937 | 3512779a | bellard | if (!true_color_flag) {
|
938 | 3512779a | bellard | fail:
|
939 | 24236869 | bellard | vnc_client_error(vs); |
940 | 3512779a | bellard | return;
|
941 | 3512779a | bellard | } |
942 | 3512779a | bellard | if (bits_per_pixel == 32 && |
943 | 3512779a | bellard | host_big_endian_flag == big_endian_flag && |
944 | 3512779a | bellard | red_max == 0xff && green_max == 0xff && blue_max == 0xff && |
945 | 3512779a | bellard | red_shift == 16 && green_shift == 8 && blue_shift == 0) { |
946 | 3512779a | bellard | vs->depth = 4;
|
947 | 3512779a | bellard | vs->write_pixels = vnc_write_pixels_copy; |
948 | 3512779a | bellard | vs->send_hextile_tile = send_hextile_tile_32; |
949 | 3512779a | bellard | } else
|
950 | 3512779a | bellard | if (bits_per_pixel == 16 && |
951 | 3512779a | bellard | host_big_endian_flag == big_endian_flag && |
952 | 3512779a | bellard | red_max == 31 && green_max == 63 && blue_max == 31 && |
953 | 3512779a | bellard | red_shift == 11 && green_shift == 5 && blue_shift == 0) { |
954 | 3512779a | bellard | vs->depth = 2;
|
955 | 3512779a | bellard | vs->write_pixels = vnc_write_pixels_copy; |
956 | 3512779a | bellard | vs->send_hextile_tile = send_hextile_tile_16; |
957 | 3512779a | bellard | } else
|
958 | 3512779a | bellard | if (bits_per_pixel == 8 && |
959 | 3512779a | bellard | red_max == 7 && green_max == 7 && blue_max == 3 && |
960 | 3512779a | bellard | red_shift == 5 && green_shift == 2 && blue_shift == 0) { |
961 | 3512779a | bellard | vs->depth = 1;
|
962 | 3512779a | bellard | vs->write_pixels = vnc_write_pixels_copy; |
963 | 3512779a | bellard | vs->send_hextile_tile = send_hextile_tile_8; |
964 | 3512779a | bellard | } else
|
965 | 3512779a | bellard | { |
966 | 3512779a | bellard | /* generic and slower case */
|
967 | 3512779a | bellard | if (bits_per_pixel != 8 && |
968 | 3512779a | bellard | bits_per_pixel != 16 &&
|
969 | 3512779a | bellard | bits_per_pixel != 32)
|
970 | 3512779a | bellard | goto fail;
|
971 | 3512779a | bellard | vs->depth = 4;
|
972 | 3512779a | bellard | vs->red_shift = red_shift; |
973 | 3512779a | bellard | vs->red_max = red_max; |
974 | 3512779a | bellard | vs->red_shift1 = 24 - compute_nbits(red_max);
|
975 | 3512779a | bellard | vs->green_shift = green_shift; |
976 | 3512779a | bellard | vs->green_max = green_max; |
977 | 3512779a | bellard | vs->green_shift1 = 16 - compute_nbits(green_max);
|
978 | 3512779a | bellard | vs->blue_shift = blue_shift; |
979 | 3512779a | bellard | vs->blue_max = blue_max; |
980 | 3512779a | bellard | vs->blue_shift1 = 8 - compute_nbits(blue_max);
|
981 | 3512779a | bellard | vs->pix_bpp = bits_per_pixel / 8;
|
982 | 3512779a | bellard | vs->pix_big_endian = big_endian_flag; |
983 | 3512779a | bellard | vs->write_pixels = vnc_write_pixels_generic; |
984 | 3512779a | bellard | vs->send_hextile_tile = send_hextile_tile_generic; |
985 | 3512779a | bellard | } |
986 | 24236869 | bellard | |
987 | 24236869 | bellard | vnc_dpy_resize(vs->ds, vs->ds->width, vs->ds->height); |
988 | 24236869 | bellard | memset(vs->dirty_row, 0xFF, sizeof(vs->dirty_row)); |
989 | 24236869 | bellard | memset(vs->old_data, 42, vs->ds->linesize * vs->ds->height);
|
990 | 24236869 | bellard | |
991 | 24236869 | bellard | vga_hw_invalidate(); |
992 | 24236869 | bellard | vga_hw_update(); |
993 | 24236869 | bellard | } |
994 | 24236869 | bellard | |
995 | 24236869 | bellard | static int protocol_client_msg(VncState *vs, char *data, size_t len) |
996 | 24236869 | bellard | { |
997 | 24236869 | bellard | int i;
|
998 | 24236869 | bellard | uint16_t limit; |
999 | 24236869 | bellard | |
1000 | 24236869 | bellard | switch (data[0]) { |
1001 | 24236869 | bellard | case 0: |
1002 | 24236869 | bellard | if (len == 1) |
1003 | 24236869 | bellard | return 20; |
1004 | 24236869 | bellard | |
1005 | 24236869 | bellard | set_pixel_format(vs, read_u8(data, 4), read_u8(data, 5), |
1006 | 24236869 | bellard | read_u8(data, 6), read_u8(data, 7), |
1007 | 24236869 | bellard | read_u16(data, 8), read_u16(data, 10), |
1008 | 24236869 | bellard | read_u16(data, 12), read_u8(data, 14), |
1009 | 24236869 | bellard | read_u8(data, 15), read_u8(data, 16)); |
1010 | 24236869 | bellard | break;
|
1011 | 24236869 | bellard | case 2: |
1012 | 24236869 | bellard | if (len == 1) |
1013 | 24236869 | bellard | return 4; |
1014 | 24236869 | bellard | |
1015 | 24236869 | bellard | if (len == 4) |
1016 | 24236869 | bellard | return 4 + (read_u16(data, 2) * 4); |
1017 | 24236869 | bellard | |
1018 | 24236869 | bellard | limit = read_u16(data, 2);
|
1019 | 24236869 | bellard | for (i = 0; i < limit; i++) { |
1020 | 24236869 | bellard | int32_t val = read_s32(data, 4 + (i * 4)); |
1021 | 24236869 | bellard | memcpy(data + 4 + (i * 4), &val, sizeof(val)); |
1022 | 24236869 | bellard | } |
1023 | 24236869 | bellard | |
1024 | 24236869 | bellard | set_encodings(vs, (int32_t *)(data + 4), limit);
|
1025 | 24236869 | bellard | break;
|
1026 | 24236869 | bellard | case 3: |
1027 | 24236869 | bellard | if (len == 1) |
1028 | 24236869 | bellard | return 10; |
1029 | 24236869 | bellard | |
1030 | 24236869 | bellard | framebuffer_update_request(vs, |
1031 | 24236869 | bellard | read_u8(data, 1), read_u16(data, 2), read_u16(data, 4), |
1032 | 24236869 | bellard | read_u16(data, 6), read_u16(data, 8)); |
1033 | 24236869 | bellard | break;
|
1034 | 24236869 | bellard | case 4: |
1035 | 24236869 | bellard | if (len == 1) |
1036 | 24236869 | bellard | return 8; |
1037 | 24236869 | bellard | |
1038 | 24236869 | bellard | key_event(vs, read_u8(data, 1), read_u32(data, 4)); |
1039 | 24236869 | bellard | break;
|
1040 | 24236869 | bellard | case 5: |
1041 | 24236869 | bellard | if (len == 1) |
1042 | 24236869 | bellard | return 6; |
1043 | 24236869 | bellard | |
1044 | 24236869 | bellard | pointer_event(vs, read_u8(data, 1), read_u16(data, 2), read_u16(data, 4)); |
1045 | 24236869 | bellard | break;
|
1046 | 24236869 | bellard | case 6: |
1047 | 24236869 | bellard | if (len == 1) |
1048 | 24236869 | bellard | return 8; |
1049 | 24236869 | bellard | |
1050 | 24236869 | bellard | if (len == 8) |
1051 | 24236869 | bellard | return 8 + read_u32(data, 4); |
1052 | 24236869 | bellard | |
1053 | 24236869 | bellard | client_cut_text(vs, read_u32(data, 4), data + 8); |
1054 | 24236869 | bellard | break;
|
1055 | 24236869 | bellard | default:
|
1056 | 24236869 | bellard | printf("Msg: %d\n", data[0]); |
1057 | 24236869 | bellard | vnc_client_error(vs); |
1058 | 24236869 | bellard | break;
|
1059 | 24236869 | bellard | } |
1060 | 24236869 | bellard | |
1061 | 24236869 | bellard | vnc_read_when(vs, protocol_client_msg, 1);
|
1062 | 24236869 | bellard | return 0; |
1063 | 24236869 | bellard | } |
1064 | 24236869 | bellard | |
1065 | 24236869 | bellard | static int protocol_client_init(VncState *vs, char *data, size_t len) |
1066 | 24236869 | bellard | { |
1067 | 24236869 | bellard | char pad[3] = { 0, 0, 0 }; |
1068 | c35734b2 | ths | char buf[1024]; |
1069 | c35734b2 | ths | int size;
|
1070 | 24236869 | bellard | |
1071 | 24236869 | bellard | vs->width = vs->ds->width; |
1072 | 24236869 | bellard | vs->height = vs->ds->height; |
1073 | 24236869 | bellard | vnc_write_u16(vs, vs->ds->width); |
1074 | 24236869 | bellard | vnc_write_u16(vs, vs->ds->height); |
1075 | 24236869 | bellard | |
1076 | 24236869 | bellard | vnc_write_u8(vs, vs->depth * 8); /* bits-per-pixel */ |
1077 | 24236869 | bellard | vnc_write_u8(vs, vs->depth * 8); /* depth */ |
1078 | 3512779a | bellard | #ifdef WORDS_BIGENDIAN
|
1079 | 3512779a | bellard | vnc_write_u8(vs, 1); /* big-endian-flag */ |
1080 | 3512779a | bellard | #else
|
1081 | 24236869 | bellard | vnc_write_u8(vs, 0); /* big-endian-flag */ |
1082 | 3512779a | bellard | #endif
|
1083 | 24236869 | bellard | vnc_write_u8(vs, 1); /* true-color-flag */ |
1084 | 24236869 | bellard | if (vs->depth == 4) { |
1085 | 24236869 | bellard | vnc_write_u16(vs, 0xFF); /* red-max */ |
1086 | 24236869 | bellard | vnc_write_u16(vs, 0xFF); /* green-max */ |
1087 | 24236869 | bellard | vnc_write_u16(vs, 0xFF); /* blue-max */ |
1088 | 24236869 | bellard | vnc_write_u8(vs, 16); /* red-shift */ |
1089 | 24236869 | bellard | vnc_write_u8(vs, 8); /* green-shift */ |
1090 | 24236869 | bellard | vnc_write_u8(vs, 0); /* blue-shift */ |
1091 | 3512779a | bellard | vs->send_hextile_tile = send_hextile_tile_32; |
1092 | 24236869 | bellard | } else if (vs->depth == 2) { |
1093 | 24236869 | bellard | vnc_write_u16(vs, 31); /* red-max */ |
1094 | 24236869 | bellard | vnc_write_u16(vs, 63); /* green-max */ |
1095 | 24236869 | bellard | vnc_write_u16(vs, 31); /* blue-max */ |
1096 | 24236869 | bellard | vnc_write_u8(vs, 11); /* red-shift */ |
1097 | 24236869 | bellard | vnc_write_u8(vs, 5); /* green-shift */ |
1098 | 24236869 | bellard | vnc_write_u8(vs, 0); /* blue-shift */ |
1099 | 3512779a | bellard | vs->send_hextile_tile = send_hextile_tile_16; |
1100 | 24236869 | bellard | } else if (vs->depth == 1) { |
1101 | 3512779a | bellard | /* XXX: change QEMU pixel 8 bit pixel format to match the VNC one ? */
|
1102 | 3512779a | bellard | vnc_write_u16(vs, 7); /* red-max */ |
1103 | 24236869 | bellard | vnc_write_u16(vs, 7); /* green-max */ |
1104 | 24236869 | bellard | vnc_write_u16(vs, 3); /* blue-max */ |
1105 | 24236869 | bellard | vnc_write_u8(vs, 5); /* red-shift */ |
1106 | 24236869 | bellard | vnc_write_u8(vs, 2); /* green-shift */ |
1107 | 24236869 | bellard | vnc_write_u8(vs, 0); /* blue-shift */ |
1108 | 3512779a | bellard | vs->send_hextile_tile = send_hextile_tile_8; |
1109 | 24236869 | bellard | } |
1110 | 3512779a | bellard | vs->write_pixels = vnc_write_pixels_copy; |
1111 | 24236869 | bellard | |
1112 | 24236869 | bellard | vnc_write(vs, pad, 3); /* padding */ |
1113 | 24236869 | bellard | |
1114 | c35734b2 | ths | if (qemu_name)
|
1115 | c35734b2 | ths | size = snprintf(buf, sizeof(buf), "QEMU (%s)", qemu_name); |
1116 | c35734b2 | ths | else
|
1117 | c35734b2 | ths | size = snprintf(buf, sizeof(buf), "QEMU"); |
1118 | c35734b2 | ths | |
1119 | c35734b2 | ths | vnc_write_u32(vs, size); |
1120 | c35734b2 | ths | vnc_write(vs, buf, size); |
1121 | 24236869 | bellard | vnc_flush(vs); |
1122 | 24236869 | bellard | |
1123 | 24236869 | bellard | vnc_read_when(vs, protocol_client_msg, 1);
|
1124 | 24236869 | bellard | |
1125 | 24236869 | bellard | return 0; |
1126 | 24236869 | bellard | } |
1127 | 24236869 | bellard | |
1128 | 24236869 | bellard | static int protocol_version(VncState *vs, char *version, size_t len) |
1129 | 24236869 | bellard | { |
1130 | 24236869 | bellard | char local[13]; |
1131 | 24236869 | bellard | int maj, min;
|
1132 | 24236869 | bellard | |
1133 | 24236869 | bellard | memcpy(local, version, 12);
|
1134 | 24236869 | bellard | local[12] = 0; |
1135 | 24236869 | bellard | |
1136 | 24236869 | bellard | if (sscanf(local, "RFB %03d.%03d\n", &maj, &min) != 2) { |
1137 | 24236869 | bellard | vnc_client_error(vs); |
1138 | 24236869 | bellard | return 0; |
1139 | 24236869 | bellard | } |
1140 | 24236869 | bellard | |
1141 | 24236869 | bellard | vnc_write_u32(vs, 1); /* None */ |
1142 | 24236869 | bellard | vnc_flush(vs); |
1143 | 24236869 | bellard | |
1144 | 24236869 | bellard | vnc_read_when(vs, protocol_client_init, 1);
|
1145 | 24236869 | bellard | |
1146 | 24236869 | bellard | return 0; |
1147 | 24236869 | bellard | } |
1148 | 24236869 | bellard | |
1149 | 24236869 | bellard | static void vnc_listen_read(void *opaque) |
1150 | 24236869 | bellard | { |
1151 | 24236869 | bellard | VncState *vs = opaque; |
1152 | 24236869 | bellard | struct sockaddr_in addr;
|
1153 | 24236869 | bellard | socklen_t addrlen = sizeof(addr);
|
1154 | 24236869 | bellard | |
1155 | 24236869 | bellard | vs->csock = accept(vs->lsock, (struct sockaddr *)&addr, &addrlen);
|
1156 | 24236869 | bellard | if (vs->csock != -1) { |
1157 | 6ca957f0 | bellard | socket_set_nonblock(vs->csock); |
1158 | 24236869 | bellard | qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, opaque); |
1159 | 24236869 | bellard | vnc_write(vs, "RFB 003.003\n", 12); |
1160 | 24236869 | bellard | vnc_flush(vs); |
1161 | 24236869 | bellard | vnc_read_when(vs, protocol_version, 12);
|
1162 | 24236869 | bellard | memset(vs->old_data, 0, vs->ds->linesize * vs->ds->height);
|
1163 | 24236869 | bellard | memset(vs->dirty_row, 0xFF, sizeof(vs->dirty_row)); |
1164 | 24236869 | bellard | vs->has_resize = 0;
|
1165 | 24236869 | bellard | vs->has_hextile = 0;
|
1166 | 24236869 | bellard | vs->ds->dpy_copy = NULL;
|
1167 | 24236869 | bellard | } |
1168 | 24236869 | bellard | } |
1169 | 24236869 | bellard | |
1170 | 73fc9742 | ths | extern int parse_host_port(struct sockaddr_in *saddr, const char *str); |
1171 | 73fc9742 | ths | |
1172 | 73fc9742 | ths | void vnc_display_init(DisplayState *ds, const char *arg) |
1173 | 24236869 | bellard | { |
1174 | 73fc9742 | ths | struct sockaddr *addr;
|
1175 | 73fc9742 | ths | struct sockaddr_in iaddr;
|
1176 | 73fc9742 | ths | #ifndef _WIN32
|
1177 | 73fc9742 | ths | struct sockaddr_un uaddr;
|
1178 | 73fc9742 | ths | #endif
|
1179 | 24236869 | bellard | int reuse_addr, ret;
|
1180 | 73fc9742 | ths | socklen_t addrlen; |
1181 | 73fc9742 | ths | const char *p; |
1182 | 24236869 | bellard | VncState *vs; |
1183 | 24236869 | bellard | |
1184 | 24236869 | bellard | vs = qemu_mallocz(sizeof(VncState));
|
1185 | 24236869 | bellard | if (!vs)
|
1186 | 24236869 | bellard | exit(1);
|
1187 | 24236869 | bellard | |
1188 | 24236869 | bellard | ds->opaque = vs; |
1189 | a9ce8590 | bellard | vnc_state = vs; |
1190 | a9ce8590 | bellard | vs->display = arg; |
1191 | 24236869 | bellard | |
1192 | 24236869 | bellard | vs->lsock = -1;
|
1193 | 24236869 | bellard | vs->csock = -1;
|
1194 | 24236869 | bellard | vs->depth = 4;
|
1195 | 564c337e | bellard | vs->last_x = -1;
|
1196 | 564c337e | bellard | vs->last_y = -1;
|
1197 | 24236869 | bellard | |
1198 | 24236869 | bellard | vs->ds = ds; |
1199 | 24236869 | bellard | |
1200 | 24236869 | bellard | if (!keyboard_layout)
|
1201 | 24236869 | bellard | keyboard_layout = "en-us";
|
1202 | 24236869 | bellard | |
1203 | 24236869 | bellard | vs->kbd_layout = init_keyboard_layout(keyboard_layout); |
1204 | 24236869 | bellard | if (!vs->kbd_layout)
|
1205 | 24236869 | bellard | exit(1);
|
1206 | 24236869 | bellard | |
1207 | 73fc9742 | ths | vs->ds->data = NULL;
|
1208 | 73fc9742 | ths | vs->ds->dpy_update = vnc_dpy_update; |
1209 | 73fc9742 | ths | vs->ds->dpy_resize = vnc_dpy_resize; |
1210 | 73fc9742 | ths | vs->ds->dpy_refresh = vnc_dpy_refresh; |
1211 | 73fc9742 | ths | |
1212 | 73fc9742 | ths | memset(vs->dirty_row, 0xFF, sizeof(vs->dirty_row)); |
1213 | 24236869 | bellard | |
1214 | 73fc9742 | ths | vnc_dpy_resize(vs->ds, 640, 400); |
1215 | 24236869 | bellard | |
1216 | 73fc9742 | ths | #ifndef _WIN32
|
1217 | 73fc9742 | ths | if (strstart(arg, "unix:", &p)) { |
1218 | 73fc9742 | ths | addr = (struct sockaddr *)&uaddr;
|
1219 | 73fc9742 | ths | addrlen = sizeof(uaddr);
|
1220 | 73fc9742 | ths | |
1221 | 73fc9742 | ths | vs->lsock = socket(PF_UNIX, SOCK_STREAM, 0);
|
1222 | 73fc9742 | ths | if (vs->lsock == -1) { |
1223 | 73fc9742 | ths | fprintf(stderr, "Could not create socket\n");
|
1224 | 73fc9742 | ths | exit(1);
|
1225 | 73fc9742 | ths | } |
1226 | 73fc9742 | ths | |
1227 | 73fc9742 | ths | uaddr.sun_family = AF_UNIX; |
1228 | 73fc9742 | ths | memset(uaddr.sun_path, 0, 108); |
1229 | 73fc9742 | ths | snprintf(uaddr.sun_path, 108, "%s", p); |
1230 | 73fc9742 | ths | |
1231 | 73fc9742 | ths | unlink(uaddr.sun_path); |
1232 | 73fc9742 | ths | } else
|
1233 | 73fc9742 | ths | #endif
|
1234 | 73fc9742 | ths | { |
1235 | 73fc9742 | ths | addr = (struct sockaddr *)&iaddr;
|
1236 | 73fc9742 | ths | addrlen = sizeof(iaddr);
|
1237 | 73fc9742 | ths | |
1238 | 73fc9742 | ths | vs->lsock = socket(PF_INET, SOCK_STREAM, 0);
|
1239 | 73fc9742 | ths | if (vs->lsock == -1) { |
1240 | 73fc9742 | ths | fprintf(stderr, "Could not create socket\n");
|
1241 | 73fc9742 | ths | exit(1);
|
1242 | 73fc9742 | ths | } |
1243 | 73fc9742 | ths | |
1244 | 73fc9742 | ths | if (parse_host_port(&iaddr, arg) < 0) { |
1245 | 73fc9742 | ths | fprintf(stderr, "Could not parse VNC address\n");
|
1246 | 73fc9742 | ths | exit(1);
|
1247 | 73fc9742 | ths | } |
1248 | 73fc9742 | ths | |
1249 | 73fc9742 | ths | iaddr.sin_port = htons(ntohs(iaddr.sin_port) + 5900);
|
1250 | 73fc9742 | ths | |
1251 | 73fc9742 | ths | reuse_addr = 1;
|
1252 | 73fc9742 | ths | ret = setsockopt(vs->lsock, SOL_SOCKET, SO_REUSEADDR, |
1253 | 73fc9742 | ths | (const char *)&reuse_addr, sizeof(reuse_addr)); |
1254 | 73fc9742 | ths | if (ret == -1) { |
1255 | 73fc9742 | ths | fprintf(stderr, "setsockopt() failed\n");
|
1256 | 73fc9742 | ths | exit(1);
|
1257 | 73fc9742 | ths | } |
1258 | 24236869 | bellard | } |
1259 | 24236869 | bellard | |
1260 | 73fc9742 | ths | if (bind(vs->lsock, addr, addrlen) == -1) { |
1261 | 24236869 | bellard | fprintf(stderr, "bind() failed\n");
|
1262 | 24236869 | bellard | exit(1);
|
1263 | 24236869 | bellard | } |
1264 | 24236869 | bellard | |
1265 | 24236869 | bellard | if (listen(vs->lsock, 1) == -1) { |
1266 | 24236869 | bellard | fprintf(stderr, "listen() failed\n");
|
1267 | 24236869 | bellard | exit(1);
|
1268 | 24236869 | bellard | } |
1269 | 24236869 | bellard | |
1270 | 24236869 | bellard | ret = qemu_set_fd_handler2(vs->lsock, vnc_listen_poll, vnc_listen_read, NULL, vs);
|
1271 | 24236869 | bellard | if (ret == -1) { |
1272 | 24236869 | bellard | exit(1);
|
1273 | 24236869 | bellard | } |
1274 | 24236869 | bellard | } |