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