ia64/xen-unstable

view tools/ioemu/vnc.c @ 11029:bd04004865ba

[qemu] Add vncunused option.
If the port used for the requested display number is in use, try
additional ports until a free port is found.

Signed-off-by: Christian Limpach <Christian.Limpach@xensource.com>
author chris@kneesaa.uk.xensource.com
date Wed Aug 09 15:03:38 2006 +0100 (2006-08-09)
parents 08a11694b109
children c742b2ae920c
line source
1 /*
2 * QEMU VNC display driver
3 *
4 * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
5 * Copyright (C) 2006 Fabrice Bellard
6 * Copyright (C) 2006 Christian Limpach <Christian.Limpach@xensource.com>
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
27 #include "vl.h"
28 #include "qemu_socket.h"
30 #define VNC_REFRESH_INTERVAL (1000 / 30)
32 #include "vnc_keysym.h"
33 #include "keymaps.c"
35 #define XK_MISCELLANY
36 #define XK_LATIN1
37 #include <X11/keysymdef.h>
39 typedef struct Buffer
40 {
41 size_t capacity;
42 size_t offset;
43 char *buffer;
44 } Buffer;
46 typedef struct VncState VncState;
48 typedef int VncReadEvent(VncState *vs, char *data, size_t len);
50 typedef void VncWritePixels(VncState *vs, void *data, int size);
52 typedef void VncSendHextileTile(VncState *vs,
53 int x, int y, int w, int h,
54 uint32_t *last_bg,
55 uint32_t *last_fg,
56 int *has_bg, int *has_fg);
58 #if 0
59 #define VNC_MAX_WIDTH 2048
60 #define VNC_MAX_HEIGHT 2048
61 #define VNC_DIRTY_WORDS (VNC_MAX_WIDTH / (16 * 32))
62 #endif
64 struct VncState
65 {
66 QEMUTimer *timer;
67 int lsock;
68 int csock;
69 DisplayState *ds;
70 int need_update;
71 int width;
72 int height;
73 uint64_t *dirty_row; /* screen regions which are possibly dirty */
74 int dirty_pixel_shift;
75 uint64_t *update_row; /* outstanding updates */
76 int has_update; /* there's outstanding updates in the
77 * visible area */
78 char *old_data;
79 int depth; /* internal VNC frame buffer byte per pixel */
80 int has_resize;
81 int has_hextile;
82 Buffer output;
83 Buffer input;
84 kbd_layout_t *kbd_layout;
85 /* current output mode information */
86 VncWritePixels *write_pixels;
87 VncSendHextileTile *send_hextile_tile;
88 int pix_bpp, pix_big_endian;
89 int red_shift, red_max, red_shift1;
90 int green_shift, green_max, green_shift1;
91 int blue_shift, blue_max, blue_shift1;
93 VncReadEvent *read_handler;
94 size_t read_handler_expect;
96 int visible_x;
97 int visible_y;
98 int visible_w;
99 int visible_h;
101 int slow_client;
103 int ctl_keys; /* Ctrl+Alt starts calibration */
104 };
106 #define DIRTY_PIXEL_BITS 64
107 #define X2DP_DOWN(vs, x) ((x) >> (vs)->dirty_pixel_shift)
108 #define X2DP_UP(vs, x) \
109 (((x) + (1ULL << (vs)->dirty_pixel_shift) - 1) >> (vs)->dirty_pixel_shift)
110 #define DP2X(vs, x) ((x) << (vs)->dirty_pixel_shift)
112 /* TODO
113 1) Get the queue working for IO.
114 2) there is some weirdness when using the -S option (the screen is grey
115 and not totally invalidated
116 */
118 static void vnc_write(VncState *vs, const void *data, size_t len);
119 static void vnc_write_u32(VncState *vs, uint32_t value);
120 static void vnc_write_s32(VncState *vs, int32_t value);
121 static void vnc_write_u16(VncState *vs, uint16_t value);
122 static void vnc_write_u8(VncState *vs, uint8_t value);
123 static void vnc_flush(VncState *vs);
124 static void _vnc_update_client(void *opaque);
125 static void vnc_update_client(void *opaque);
126 static void vnc_client_read(void *opaque);
127 static void framebuffer_set_updated(VncState *vs, int x, int y, int w, int h);
129 #if 0
130 static inline void vnc_set_bit(uint32_t *d, int k)
131 {
132 d[k >> 5] |= 1 << (k & 0x1f);
133 }
135 static inline void vnc_clear_bit(uint32_t *d, int k)
136 {
137 d[k >> 5] &= ~(1 << (k & 0x1f));
138 }
140 static inline void vnc_set_bits(uint32_t *d, int n, int nb_words)
141 {
142 int j;
144 j = 0;
145 while (n >= 32) {
146 d[j++] = -1;
147 n -= 32;
148 }
149 if (n > 0)
150 d[j++] = (1 << n) - 1;
151 while (j < nb_words)
152 d[j++] = 0;
153 }
155 static inline int vnc_get_bit(const uint32_t *d, int k)
156 {
157 return (d[k >> 5] >> (k & 0x1f)) & 1;
158 }
160 static inline int vnc_and_bits(const uint32_t *d1, const uint32_t *d2,
161 int nb_words)
162 {
163 int i;
164 for(i = 0; i < nb_words; i++) {
165 if ((d1[i] & d2[i]) != 0)
166 return 1;
167 }
168 return 0;
169 }
170 #endif
172 static void set_bits_in_row(VncState *vs, uint64_t *row,
173 int x, int y, int w, int h)
174 {
175 int x1, x2;
176 uint64_t mask;
178 if (w == 0)
179 return;
181 x1 = X2DP_DOWN(vs, x);
182 x2 = X2DP_UP(vs, x + w);
184 if (X2DP_UP(vs, w) != DIRTY_PIXEL_BITS)
185 mask = ((1ULL << (x2 - x1)) - 1) << x1;
186 else
187 mask = ~(0ULL);
189 h += y;
190 for (; y < h; y++)
191 row[y] |= mask;
192 }
194 static void vnc_dpy_update(DisplayState *ds, int x, int y, int w, int h)
195 {
196 VncState *vs = ds->opaque;
198 set_bits_in_row(vs, vs->dirty_row, x, y, w, h);
199 }
201 static void vnc_framebuffer_update(VncState *vs, int x, int y, int w, int h,
202 int32_t encoding)
203 {
204 vnc_write_u16(vs, x);
205 vnc_write_u16(vs, y);
206 vnc_write_u16(vs, w);
207 vnc_write_u16(vs, h);
209 vnc_write_s32(vs, encoding);
210 }
212 static void vnc_dpy_resize(DisplayState *ds, int w, int h)
213 {
214 VncState *vs = ds->opaque;
215 int o;
217 ds->data = realloc(ds->data, w * h * vs->depth);
218 vs->old_data = realloc(vs->old_data, w * h * vs->depth);
219 vs->dirty_row = realloc(vs->dirty_row, h * sizeof(vs->dirty_row[0]));
220 vs->update_row = realloc(vs->update_row, h * sizeof(vs->dirty_row[0]));
222 if (ds->data == NULL || vs->old_data == NULL ||
223 vs->dirty_row == NULL || vs->update_row == NULL) {
224 fprintf(stderr, "vnc: memory allocation failed\n");
225 exit(1);
226 }
228 if (ds->depth != vs->depth * 8) {
229 ds->depth = vs->depth * 8;
230 set_color_table(ds);
231 }
232 ds->width = w;
233 ds->height = h;
234 ds->linesize = w * vs->depth;
235 if (vs->csock != -1 && vs->has_resize) {
236 vnc_write_u8(vs, 0); /* msg id */
237 vnc_write_u8(vs, 0);
238 vnc_write_u16(vs, 1); /* number of rects */
239 vnc_framebuffer_update(vs, 0, 0, ds->width, ds->height, -223);
240 vnc_flush(vs);
241 vs->width = ds->width;
242 vs->height = ds->height;
243 }
244 vs->dirty_pixel_shift = 0;
245 for (o = DIRTY_PIXEL_BITS; o < ds->width; o *= 2)
246 vs->dirty_pixel_shift++;
247 framebuffer_set_updated(vs, 0, 0, ds->width, ds->height);
248 }
250 /* fastest code */
251 static void vnc_write_pixels_copy(VncState *vs, void *pixels, int size)
252 {
253 vnc_write(vs, pixels, size);
254 }
256 /* slowest but generic code. */
257 static void vnc_convert_pixel(VncState *vs, uint8_t *buf, uint32_t v)
258 {
259 unsigned int r, g, b;
261 r = (v >> vs->red_shift1) & vs->red_max;
262 g = (v >> vs->green_shift1) & vs->green_max;
263 b = (v >> vs->blue_shift1) & vs->blue_max;
264 v = (r << vs->red_shift) |
265 (g << vs->green_shift) |
266 (b << vs->blue_shift);
267 switch(vs->pix_bpp) {
268 case 1:
269 buf[0] = v;
270 break;
271 case 2:
272 if (vs->pix_big_endian) {
273 buf[0] = v >> 8;
274 buf[1] = v;
275 } else {
276 buf[1] = v >> 8;
277 buf[0] = v;
278 }
279 break;
280 default:
281 case 4:
282 if (vs->pix_big_endian) {
283 buf[0] = v >> 24;
284 buf[1] = v >> 16;
285 buf[2] = v >> 8;
286 buf[3] = v;
287 } else {
288 buf[3] = v >> 24;
289 buf[2] = v >> 16;
290 buf[1] = v >> 8;
291 buf[0] = v;
292 }
293 break;
294 }
295 }
297 static void vnc_write_pixels_generic(VncState *vs, void *pixels1, int size)
298 {
299 uint32_t *pixels = pixels1;
300 uint8_t buf[4];
301 int n, i;
303 n = size >> 2;
304 for(i = 0; i < n; i++) {
305 vnc_convert_pixel(vs, buf, pixels[i]);
306 vnc_write(vs, buf, vs->pix_bpp);
307 }
308 }
310 static void send_framebuffer_update_raw(VncState *vs, int x, int y, int w, int h)
311 {
312 int i;
313 char *row;
315 vnc_framebuffer_update(vs, x, y, w, h, 0);
317 row = vs->ds->data + y * vs->ds->linesize + x * vs->depth;
318 for (i = 0; i < h; i++) {
319 vs->write_pixels(vs, row, w * vs->depth);
320 row += vs->ds->linesize;
321 }
322 }
324 static void hextile_enc_cord(uint8_t *ptr, int x, int y, int w, int h)
325 {
326 ptr[0] = ((x & 0x0F) << 4) | (y & 0x0F);
327 ptr[1] = (((w - 1) & 0x0F) << 4) | ((h - 1) & 0x0F);
328 }
330 #define BPP 8
331 #include "vnchextile.h"
332 #undef BPP
334 #define BPP 16
335 #include "vnchextile.h"
336 #undef BPP
338 #define BPP 32
339 #include "vnchextile.h"
340 #undef BPP
342 #define GENERIC
343 #define BPP 32
344 #include "vnchextile.h"
345 #undef BPP
346 #undef GENERIC
348 static void send_framebuffer_update_hextile(VncState *vs, int x, int y, int w, int h)
349 {
350 int i, j;
351 int has_fg, has_bg;
352 uint32_t last_fg32, last_bg32;
354 vnc_framebuffer_update(vs, x, y, w, h, 5);
356 has_fg = has_bg = 0;
357 for (j = y; j < (y + h); j += 16) {
358 for (i = x; i < (x + w); i += 16) {
359 vs->send_hextile_tile(vs, i, j,
360 MIN(16, x + w - i), MIN(16, y + h - j),
361 &last_bg32, &last_fg32, &has_bg, &has_fg);
362 }
363 }
364 }
366 static void send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
367 {
368 if (vs->has_hextile)
369 send_framebuffer_update_hextile(vs, x, y, w, h);
370 else
371 send_framebuffer_update_raw(vs, x, y, w, h);
372 }
374 static void vnc_copy(DisplayState *ds, int src_x, int src_y, int dst_x, int dst_y, int w, int h)
375 {
376 int src, dst;
377 char *src_row;
378 char *dst_row;
379 char *old_row;
380 int y = 0;
381 int pitch = ds->linesize;
382 VncState *vs = ds->opaque;
383 int updating_client = !vs->slow_client;
385 if (src_x < vs->visible_x || src_y < vs->visible_y ||
386 dst_x < vs->visible_x || dst_y < vs->visible_y ||
387 (src_x + w) > (vs->visible_x + vs->visible_w) ||
388 (src_y + h) > (vs->visible_y + vs->visible_h) ||
389 (dst_x + w) > (vs->visible_x + vs->visible_w) ||
390 (dst_y + h) > (vs->visible_y + vs->visible_h))
391 updating_client = 0;
393 if (updating_client) {
394 vs->need_update = 1;
395 _vnc_update_client(vs);
396 }
398 if (dst_y > src_y) {
399 y = h - 1;
400 pitch = -pitch;
401 }
403 src = (ds->linesize * (src_y + y) + vs->depth * src_x);
404 dst = (ds->linesize * (dst_y + y) + vs->depth * dst_x);
406 src_row = ds->data + src;
407 dst_row = ds->data + dst;
408 old_row = vs->old_data + dst;
410 for (y = 0; y < h; y++) {
411 memmove(old_row, src_row, w * vs->depth);
412 memmove(dst_row, src_row, w * vs->depth);
413 src_row += pitch;
414 dst_row += pitch;
415 old_row += pitch;
416 }
418 if (updating_client && vs->csock != -1 && !vs->has_update) {
419 vnc_write_u8(vs, 0); /* msg id */
420 vnc_write_u8(vs, 0);
421 vnc_write_u16(vs, 1); /* number of rects */
422 vnc_framebuffer_update(vs, dst_x, dst_y, w, h, 1);
423 vnc_write_u16(vs, src_x);
424 vnc_write_u16(vs, src_y);
425 vnc_flush(vs);
426 } else
427 framebuffer_set_updated(vs, dst_x, dst_y, w, h);
428 }
430 static int find_update_height(VncState *vs, int y, int maxy, int last_x, int x)
431 {
432 int h;
434 for (h = 1; y + h < maxy; h++) {
435 int tmp_x;
436 if (!(vs->update_row[y + h] & (1ULL << last_x)))
437 break;
438 for (tmp_x = last_x; tmp_x < x; tmp_x++)
439 vs->update_row[y + h] &= ~(1ULL << tmp_x);
440 }
442 return h;
443 }
445 static void _vnc_update_client(void *opaque)
446 {
447 VncState *vs = opaque;
448 int64_t now = qemu_get_clock(rt_clock);
450 if (vs->need_update && vs->csock != -1) {
451 int y;
452 char *row;
453 char *old_row;
454 uint64_t width_mask;
455 int n_rectangles;
456 int saved_offset;
457 int maxx, maxy;
458 int tile_bytes = vs->depth * DP2X(vs, 1);
460 if (vs->width != DP2X(vs, DIRTY_PIXEL_BITS))
461 width_mask = (1ULL << X2DP_UP(vs, vs->ds->width)) - 1;
462 else
463 width_mask = ~(0ULL);
465 /* Walk through the dirty map and eliminate tiles that
466 really aren't dirty */
467 row = vs->ds->data;
468 old_row = vs->old_data;
470 for (y = 0; y < vs->ds->height; y++) {
471 if (vs->dirty_row[y] & width_mask) {
472 int x;
473 char *ptr, *old_ptr;
475 ptr = row;
476 old_ptr = old_row;
478 for (x = 0; x < X2DP_UP(vs, vs->ds->width); x++) {
479 if (vs->dirty_row[y] & (1ULL << x)) {
480 if (memcmp(old_ptr, ptr, tile_bytes)) {
481 vs->has_update = 1;
482 vs->update_row[y] |= (1ULL << x);
483 memcpy(old_ptr, ptr, tile_bytes);
484 }
485 vs->dirty_row[y] &= ~(1ULL << x);
486 }
488 ptr += tile_bytes;
489 old_ptr += tile_bytes;
490 }
491 }
493 row += vs->ds->linesize;
494 old_row += vs->ds->linesize;
495 }
497 if (!vs->has_update || vs->visible_y >= vs->ds->height ||
498 vs->visible_x >= vs->ds->width)
499 goto out;
501 /* Count rectangles */
502 n_rectangles = 0;
503 vnc_write_u8(vs, 0); /* msg id */
504 vnc_write_u8(vs, 0);
505 saved_offset = vs->output.offset;
506 vnc_write_u16(vs, 0);
508 maxy = vs->visible_y + vs->visible_h;
509 if (maxy > vs->ds->height)
510 maxy = vs->ds->height;
511 maxx = vs->visible_x + vs->visible_w;
512 if (maxx > vs->ds->width)
513 maxx = vs->ds->width;
515 for (y = vs->visible_y; y < maxy; y++) {
516 int x;
517 int last_x = -1;
518 for (x = X2DP_DOWN(vs, vs->visible_x);
519 x < X2DP_UP(vs, maxx); x++) {
520 if (vs->update_row[y] & (1ULL << x)) {
521 if (last_x == -1)
522 last_x = x;
523 vs->update_row[y] &= ~(1ULL << x);
524 } else {
525 if (last_x != -1) {
526 int h = find_update_height(vs, y, maxy, last_x, x);
527 send_framebuffer_update(vs, DP2X(vs, last_x), y,
528 DP2X(vs, (x - last_x)), h);
529 n_rectangles++;
530 }
531 last_x = -1;
532 }
533 }
534 if (last_x != -1) {
535 int h = find_update_height(vs, y, maxy, last_x, x);
536 send_framebuffer_update(vs, DP2X(vs, last_x), y,
537 DP2X(vs, (x - last_x)), h);
538 n_rectangles++;
539 }
540 }
541 vs->output.buffer[saved_offset] = (n_rectangles >> 8) & 0xFF;
542 vs->output.buffer[saved_offset + 1] = n_rectangles & 0xFF;
544 vs->has_update = 0;
545 vs->need_update = 0;
546 vnc_flush(vs);
547 vs->slow_client = 0;
548 } else
549 vs->slow_client = 1;
551 out:
552 qemu_mod_timer(vs->timer, now + VNC_REFRESH_INTERVAL);
553 }
555 static void vnc_update_client(void *opaque)
556 {
557 VncState *vs = opaque;
559 vs->ds->dpy_refresh(vs->ds);
560 _vnc_update_client(vs);
561 }
563 static void vnc_timer_init(VncState *vs)
564 {
565 if (vs->timer == NULL) {
566 vs->timer = qemu_new_timer(rt_clock, vnc_update_client, vs);
567 qemu_mod_timer(vs->timer, qemu_get_clock(rt_clock));
568 }
569 }
571 static void vnc_dpy_refresh(DisplayState *ds)
572 {
573 vga_hw_update();
574 }
576 static int vnc_listen_poll(void *opaque)
577 {
578 VncState *vs = opaque;
579 if (vs->csock == -1)
580 return 1;
581 return 0;
582 }
584 static void buffer_reserve(Buffer *buffer, size_t len)
585 {
586 if ((buffer->capacity - buffer->offset) < len) {
587 buffer->capacity += (len + 1024);
588 buffer->buffer = realloc(buffer->buffer, buffer->capacity);
589 if (buffer->buffer == NULL) {
590 fprintf(stderr, "vnc: out of memory\n");
591 exit(1);
592 }
593 }
594 }
596 static int buffer_empty(Buffer *buffer)
597 {
598 return buffer->offset == 0;
599 }
601 static char *buffer_end(Buffer *buffer)
602 {
603 return buffer->buffer + buffer->offset;
604 }
606 static void buffer_reset(Buffer *buffer)
607 {
608 buffer->offset = 0;
609 }
611 static void buffer_append(Buffer *buffer, const void *data, size_t len)
612 {
613 memcpy(buffer->buffer + buffer->offset, data, len);
614 buffer->offset += len;
615 }
617 static int vnc_client_io_error(VncState *vs, int ret, int last_errno)
618 {
619 if (ret == 0 || ret == -1) {
620 if (ret == -1 && (last_errno == EINTR || last_errno == EAGAIN))
621 return 0;
623 qemu_set_fd_handler2(vs->csock, NULL, NULL, NULL, NULL);
624 closesocket(vs->csock);
625 vs->csock = -1;
626 buffer_reset(&vs->input);
627 buffer_reset(&vs->output);
628 vs->need_update = 0;
629 return 0;
630 }
631 return ret;
632 }
634 static void vnc_client_error(VncState *vs)
635 {
636 vnc_client_io_error(vs, -1, EINVAL);
637 }
639 static void vnc_client_write(void *opaque)
640 {
641 long ret;
642 VncState *vs = opaque;
644 ret = send(vs->csock, vs->output.buffer, vs->output.offset, 0);
645 ret = vnc_client_io_error(vs, ret, socket_error());
646 if (!ret)
647 return;
649 memmove(vs->output.buffer, vs->output.buffer + ret,
650 vs->output.offset - ret);
651 vs->output.offset -= ret;
653 if (vs->output.offset == 0)
654 qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
655 }
657 static void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting)
658 {
659 vs->read_handler = func;
660 vs->read_handler_expect = expecting;
661 }
663 static void vnc_client_read(void *opaque)
664 {
665 VncState *vs = opaque;
666 long ret;
668 buffer_reserve(&vs->input, 4096);
670 ret = recv(vs->csock, buffer_end(&vs->input), 4096, 0);
671 ret = vnc_client_io_error(vs, ret, socket_error());
672 if (!ret)
673 return;
675 vs->input.offset += ret;
677 while (vs->read_handler && vs->input.offset >= vs->read_handler_expect) {
678 size_t len = vs->read_handler_expect;
679 int ret;
681 ret = vs->read_handler(vs, vs->input.buffer, len);
682 if (vs->csock == -1)
683 return;
685 if (!ret) {
686 memmove(vs->input.buffer, vs->input.buffer + len,
687 vs->input.offset - len);
688 vs->input.offset -= len;
689 } else
690 vs->read_handler_expect = ret;
691 }
692 }
694 static void vnc_write(VncState *vs, const void *data, size_t len)
695 {
696 buffer_reserve(&vs->output, len);
698 if (buffer_empty(&vs->output))
699 qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read,
700 vnc_client_write, vs);
702 buffer_append(&vs->output, data, len);
703 }
705 static void vnc_write_s32(VncState *vs, int32_t value)
706 {
707 vnc_write_u32(vs, *(uint32_t *)&value);
708 }
710 static void vnc_write_u32(VncState *vs, uint32_t value)
711 {
712 uint8_t buf[4];
714 buf[0] = (value >> 24) & 0xFF;
715 buf[1] = (value >> 16) & 0xFF;
716 buf[2] = (value >> 8) & 0xFF;
717 buf[3] = value & 0xFF;
719 vnc_write(vs, buf, 4);
720 }
722 static void vnc_write_u16(VncState *vs, uint16_t value)
723 {
724 char buf[2];
726 buf[0] = (value >> 8) & 0xFF;
727 buf[1] = value & 0xFF;
729 vnc_write(vs, buf, 2);
730 }
732 static void vnc_write_u8(VncState *vs, uint8_t value)
733 {
734 vnc_write(vs, (char *)&value, 1);
735 }
737 static void vnc_flush(VncState *vs)
738 {
739 if (vs->output.offset)
740 vnc_client_write(vs);
741 }
743 static uint8_t read_u8(char *data, size_t offset)
744 {
745 return data[offset];
746 }
748 static uint16_t read_u16(char *data, size_t offset)
749 {
750 return ((data[offset] & 0xFF) << 8) | (data[offset + 1] & 0xFF);
751 }
753 static int32_t read_s32(char *data, size_t offset)
754 {
755 return (int32_t)((data[offset] << 24) | (data[offset + 1] << 16) |
756 (data[offset + 2] << 8) | data[offset + 3]);
757 }
759 static uint32_t read_u32(char *data, size_t offset)
760 {
761 return ((data[offset] << 24) | (data[offset + 1] << 16) |
762 (data[offset + 2] << 8) | data[offset + 3]);
763 }
765 static void client_cut_text(VncState *vs, size_t len, char *text)
766 {
767 }
769 static void pointer_event(VncState *vs, int button_mask, int x, int y)
770 {
771 int buttons = 0;
772 int dz = 0;
774 if (button_mask & 0x01)
775 buttons |= MOUSE_EVENT_LBUTTON;
776 if (button_mask & 0x02)
777 buttons |= MOUSE_EVENT_MBUTTON;
778 if (button_mask & 0x04)
779 buttons |= MOUSE_EVENT_RBUTTON;
780 if (button_mask & 0x08)
781 dz = -1;
782 if (button_mask & 0x10)
783 dz = 1;
785 if (kbd_mouse_is_absolute()) {
786 kbd_mouse_event(x * 0x7FFF / vs->ds->width,
787 y * 0x7FFF / vs->ds->height,
788 dz, buttons);
789 } else {
790 static int last_x = -1;
791 static int last_y = -1;
793 if (last_x != -1)
794 kbd_mouse_event(x - last_x, y - last_y, dz, buttons);
796 last_x = x;
797 last_y = y;
798 }
799 }
801 static void do_key_event(VncState *vs, int down, uint32_t sym)
802 {
803 sym &= 0xFFFF;
805 if (is_graphic_console()) {
806 int keycode;
808 keycode = keysym2scancode(vs->kbd_layout, sym);
809 if (keycode & 0x80)
810 kbd_put_keycode(0xe0);
811 if (down)
812 kbd_put_keycode(keycode & 0x7f);
813 else
814 kbd_put_keycode(keycode | 0x80);
815 } else if (down) {
816 int qemu_keysym = 0;
818 if (sym <= 128) /* normal ascii */
819 qemu_keysym = sym;
820 else {
821 switch (sym) {
822 case XK_Up: qemu_keysym = QEMU_KEY_UP; break;
823 case XK_Down: qemu_keysym = QEMU_KEY_DOWN; break;
824 case XK_Left: qemu_keysym = QEMU_KEY_LEFT; break;
825 case XK_Right: qemu_keysym = QEMU_KEY_RIGHT; break;
826 case XK_Home: qemu_keysym = QEMU_KEY_HOME; break;
827 case XK_End: qemu_keysym = QEMU_KEY_END; break;
828 case XK_Page_Up: qemu_keysym = QEMU_KEY_PAGEUP; break;
829 case XK_Page_Down: qemu_keysym = QEMU_KEY_PAGEDOWN; break;
830 case XK_BackSpace: qemu_keysym = QEMU_KEY_BACKSPACE; break;
831 case XK_Delete: qemu_keysym = QEMU_KEY_DELETE; break;
832 case XK_Return:
833 case XK_Linefeed: qemu_keysym = sym; break;
834 default: break;
835 }
836 }
837 if (qemu_keysym != 0)
838 kbd_put_keysym(qemu_keysym);
839 }
841 if (down) {
842 switch (sym) {
843 case XK_Control_L:
844 vs->ctl_keys |= 1;
845 break;
847 case XK_Alt_L:
848 vs->ctl_keys |= 2;
849 break;
851 default:
852 break;
853 }
854 } else {
855 switch (sym) {
856 case XK_Control_L:
857 vs->ctl_keys &= ~1;
858 break;
860 case XK_Alt_L:
861 vs->ctl_keys &= ~2;
862 break;
864 case XK_1 ... XK_9:
865 if ((vs->ctl_keys & 3) != 3)
866 break;
868 console_select(sym - XK_1);
869 if (is_graphic_console()) {
870 /* tell the vga console to redisplay itself */
871 vga_hw_invalidate();
872 vnc_dpy_update(vs->ds, 0, 0, vs->ds->width, vs->ds->height);
873 }
874 break;
875 }
876 }
877 }
879 static void key_event(VncState *vs, int down, uint32_t sym)
880 {
881 if (sym >= 'A' && sym <= 'Z')
882 sym = sym - 'A' + 'a';
883 do_key_event(vs, down, sym);
884 }
886 static void framebuffer_set_updated(VncState *vs, int x, int y, int w, int h)
887 {
889 set_bits_in_row(vs, vs->update_row, x, y, w, h);
891 vs->has_update = 1;
892 }
894 static void framebuffer_update_request(VncState *vs, int incremental,
895 int x_position, int y_position,
896 int w, int h)
897 {
898 vs->need_update = 1;
899 if (!incremental)
900 framebuffer_set_updated(vs, x_position, y_position, w, h);
901 vs->visible_x = x_position;
902 vs->visible_y = y_position;
903 vs->visible_w = w;
904 vs->visible_h = h;
905 }
907 static void set_encodings(VncState *vs, int32_t *encodings, size_t n_encodings)
908 {
909 int i;
911 vs->has_hextile = 0;
912 vs->has_resize = 0;
913 vs->ds->dpy_copy = NULL;
915 for (i = n_encodings - 1; i >= 0; i--) {
916 switch (encodings[i]) {
917 case 0: /* Raw */
918 vs->has_hextile = 0;
919 break;
920 case 1: /* CopyRect */
921 vs->ds->dpy_copy = vnc_copy;
922 break;
923 case 5: /* Hextile */
924 vs->has_hextile = 1;
925 break;
926 case -223: /* DesktopResize */
927 vs->has_resize = 1;
928 break;
929 default:
930 break;
931 }
932 }
933 }
935 static int compute_nbits(unsigned int val)
936 {
937 int n;
938 n = 0;
939 while (val != 0) {
940 n++;
941 val >>= 1;
942 }
943 return n;
944 }
946 static void set_pixel_format(VncState *vs,
947 int bits_per_pixel, int depth,
948 int big_endian_flag, int true_color_flag,
949 int red_max, int green_max, int blue_max,
950 int red_shift, int green_shift, int blue_shift)
951 {
952 int host_big_endian_flag;
954 #ifdef WORDS_BIGENDIAN
955 host_big_endian_flag = 1;
956 #else
957 host_big_endian_flag = 0;
958 #endif
959 if (!true_color_flag) {
960 fail:
961 vnc_client_error(vs);
962 return;
963 }
964 if (bits_per_pixel == 32 &&
965 host_big_endian_flag == big_endian_flag &&
966 red_max == 0xff && green_max == 0xff && blue_max == 0xff &&
967 red_shift == 16 && green_shift == 8 && blue_shift == 0) {
968 vs->depth = 4;
969 vs->write_pixels = vnc_write_pixels_copy;
970 vs->send_hextile_tile = send_hextile_tile_32;
971 } else
972 if (bits_per_pixel == 16 &&
973 host_big_endian_flag == big_endian_flag &&
974 red_max == 31 && green_max == 63 && blue_max == 31 &&
975 red_shift == 11 && green_shift == 5 && blue_shift == 0) {
976 vs->depth = 2;
977 vs->write_pixels = vnc_write_pixels_copy;
978 vs->send_hextile_tile = send_hextile_tile_16;
979 } else
980 if (bits_per_pixel == 8 &&
981 red_max == 7 && green_max == 7 && blue_max == 3 &&
982 red_shift == 5 && green_shift == 2 && blue_shift == 0) {
983 vs->depth = 1;
984 vs->write_pixels = vnc_write_pixels_copy;
985 vs->send_hextile_tile = send_hextile_tile_8;
986 } else
987 {
988 /* generic and slower case */
989 if (bits_per_pixel != 8 &&
990 bits_per_pixel != 16 &&
991 bits_per_pixel != 32)
992 goto fail;
993 vs->depth = 4;
994 vs->red_shift = red_shift;
995 vs->red_max = red_max;
996 vs->red_shift1 = 24 - compute_nbits(red_max);
997 vs->green_shift = green_shift;
998 vs->green_max = green_max;
999 vs->green_shift1 = 16 - compute_nbits(green_max);
1000 vs->blue_shift = blue_shift;
1001 vs->blue_max = blue_max;
1002 vs->blue_shift1 = 8 - compute_nbits(blue_max);
1003 vs->pix_bpp = bits_per_pixel / 8;
1004 vs->pix_big_endian = big_endian_flag;
1005 vs->write_pixels = vnc_write_pixels_generic;
1006 vs->send_hextile_tile = send_hextile_tile_generic;
1009 vnc_dpy_resize(vs->ds, vs->ds->width, vs->ds->height);
1011 vga_hw_invalidate();
1012 vga_hw_update();
1015 static int protocol_client_msg(VncState *vs, char *data, size_t len)
1017 int i;
1018 uint16_t limit;
1020 switch (data[0]) {
1021 case 0:
1022 if (len == 1)
1023 return 20;
1025 set_pixel_format(vs, read_u8(data, 4), read_u8(data, 5),
1026 read_u8(data, 6), read_u8(data, 7),
1027 read_u16(data, 8), read_u16(data, 10),
1028 read_u16(data, 12), read_u8(data, 14),
1029 read_u8(data, 15), read_u8(data, 16));
1030 break;
1031 case 2:
1032 if (len == 1)
1033 return 4;
1035 if (len == 4)
1036 return 4 + (read_u16(data, 2) * 4);
1038 limit = read_u16(data, 2);
1039 for (i = 0; i < limit; i++) {
1040 int32_t val = read_s32(data, 4 + (i * 4));
1041 memcpy(data + 4 + (i * 4), &val, sizeof(val));
1044 set_encodings(vs, (int32_t *)(data + 4), limit);
1045 break;
1046 case 3:
1047 if (len == 1)
1048 return 10;
1050 framebuffer_update_request(vs,
1051 read_u8(data, 1), read_u16(data, 2), read_u16(data, 4),
1052 read_u16(data, 6), read_u16(data, 8));
1053 break;
1054 case 4:
1055 if (len == 1)
1056 return 8;
1058 key_event(vs, read_u8(data, 1), read_u32(data, 4));
1059 break;
1060 case 5:
1061 if (len == 1)
1062 return 6;
1064 pointer_event(vs, read_u8(data, 1), read_u16(data, 2), read_u16(data, 4));
1065 break;
1066 case 6:
1067 if (len == 1)
1068 return 8;
1070 if (len == 8)
1071 return 8 + read_u32(data, 4);
1073 client_cut_text(vs, read_u32(data, 4), data + 8);
1074 break;
1075 default:
1076 printf("Msg: %d\n", data[0]);
1077 vnc_client_error(vs);
1078 break;
1081 vnc_read_when(vs, protocol_client_msg, 1);
1082 return 0;
1085 static int protocol_client_init(VncState *vs, char *data, size_t len)
1087 size_t l;
1088 char pad[3] = { 0, 0, 0 };
1090 vs->width = vs->ds->width;
1091 vs->height = vs->ds->height;
1092 vnc_write_u16(vs, vs->ds->width);
1093 vnc_write_u16(vs, vs->ds->height);
1095 vnc_write_u8(vs, vs->depth * 8); /* bits-per-pixel */
1096 vnc_write_u8(vs, vs->depth * 8); /* depth */
1097 #ifdef WORDS_BIGENDIAN
1098 vnc_write_u8(vs, 1); /* big-endian-flag */
1099 #else
1100 vnc_write_u8(vs, 0); /* big-endian-flag */
1101 #endif
1102 vnc_write_u8(vs, 1); /* true-color-flag */
1103 if (vs->depth == 4) {
1104 vnc_write_u16(vs, 0xFF); /* red-max */
1105 vnc_write_u16(vs, 0xFF); /* green-max */
1106 vnc_write_u16(vs, 0xFF); /* blue-max */
1107 vnc_write_u8(vs, 16); /* red-shift */
1108 vnc_write_u8(vs, 8); /* green-shift */
1109 vnc_write_u8(vs, 0); /* blue-shift */
1110 vs->send_hextile_tile = send_hextile_tile_32;
1111 } else if (vs->depth == 2) {
1112 vnc_write_u16(vs, 31); /* red-max */
1113 vnc_write_u16(vs, 63); /* green-max */
1114 vnc_write_u16(vs, 31); /* blue-max */
1115 vnc_write_u8(vs, 11); /* red-shift */
1116 vnc_write_u8(vs, 5); /* green-shift */
1117 vnc_write_u8(vs, 0); /* blue-shift */
1118 vs->send_hextile_tile = send_hextile_tile_16;
1119 } else if (vs->depth == 1) {
1120 /* XXX: change QEMU pixel 8 bit pixel format to match the VNC one ? */
1121 vnc_write_u16(vs, 7); /* red-max */
1122 vnc_write_u16(vs, 7); /* green-max */
1123 vnc_write_u16(vs, 3); /* blue-max */
1124 vnc_write_u8(vs, 5); /* red-shift */
1125 vnc_write_u8(vs, 2); /* green-shift */
1126 vnc_write_u8(vs, 0); /* blue-shift */
1127 vs->send_hextile_tile = send_hextile_tile_8;
1129 vs->write_pixels = vnc_write_pixels_copy;
1131 vnc_write(vs, pad, 3); /* padding */
1133 l = strlen(domain_name);
1134 vnc_write_u32(vs, l);
1135 vnc_write(vs, domain_name, l);
1137 vnc_flush(vs);
1139 vnc_read_when(vs, protocol_client_msg, 1);
1141 return 0;
1144 static int protocol_version(VncState *vs, char *version, size_t len)
1146 char local[13];
1147 int maj, min;
1149 memcpy(local, version, 12);
1150 local[12] = 0;
1152 if (sscanf(local, "RFB %03d.%03d\n", &maj, &min) != 2) {
1153 vnc_client_error(vs);
1154 return 0;
1157 vnc_write_u32(vs, 1); /* None */
1158 vnc_flush(vs);
1160 vnc_read_when(vs, protocol_client_init, 1);
1162 return 0;
1165 static void vnc_listen_read(void *opaque)
1167 VncState *vs = opaque;
1168 struct sockaddr_in addr;
1169 socklen_t addrlen = sizeof(addr);
1171 vs->csock = accept(vs->lsock, (struct sockaddr *)&addr, &addrlen);
1172 if (vs->csock != -1) {
1173 socket_set_nonblock(vs->csock);
1174 qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, opaque);
1175 vnc_write(vs, "RFB 003.003\n", 12);
1176 vnc_flush(vs);
1177 vnc_read_when(vs, protocol_version, 12);
1178 framebuffer_set_updated(vs, 0, 0, vs->ds->width, vs->ds->height);
1179 vs->has_resize = 0;
1180 vs->has_hextile = 0;
1181 vs->ds->dpy_copy = NULL;
1182 vnc_timer_init(vs);
1186 int vnc_display_init(DisplayState *ds, int display, int find_unused)
1188 struct sockaddr_in addr;
1189 int reuse_addr, ret;
1190 VncState *vs;
1192 vs = qemu_mallocz(sizeof(VncState));
1193 if (!vs)
1194 exit(1);
1196 ds->opaque = vs;
1198 vs->lsock = -1;
1199 vs->csock = -1;
1200 vs->depth = 4;
1202 vs->ds = ds;
1204 if (!keyboard_layout)
1205 keyboard_layout = "en-us";
1207 vs->kbd_layout = init_keyboard_layout(keyboard_layout);
1208 if (!vs->kbd_layout)
1209 exit(1);
1211 vs->lsock = socket(PF_INET, SOCK_STREAM, 0);
1212 if (vs->lsock == -1) {
1213 fprintf(stderr, "Could not create socket\n");
1214 exit(1);
1217 reuse_addr = 1;
1218 ret = setsockopt(vs->lsock, SOL_SOCKET, SO_REUSEADDR,
1219 (const char *)&reuse_addr, sizeof(reuse_addr));
1220 if (ret == -1) {
1221 fprintf(stderr, "setsockopt() failed\n");
1222 exit(1);
1225 retry:
1226 addr.sin_family = AF_INET;
1227 addr.sin_port = htons(5900 + display);
1228 memset(&addr.sin_addr, 0, sizeof(addr.sin_addr));
1230 if (bind(vs->lsock, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
1231 if (find_unused && errno == EADDRINUSE) {
1232 display++;
1233 goto retry;
1235 fprintf(stderr, "bind() failed\n");
1236 exit(1);
1239 if (listen(vs->lsock, 1) == -1) {
1240 fprintf(stderr, "listen() failed\n");
1241 exit(1);
1244 ret = qemu_set_fd_handler2(vs->lsock, vnc_listen_poll, vnc_listen_read,
1245 NULL, vs);
1246 if (ret == -1)
1247 exit(1);
1249 vs->ds->data = NULL;
1250 vs->ds->dpy_update = vnc_dpy_update;
1251 vs->ds->dpy_resize = vnc_dpy_resize;
1252 vs->ds->dpy_refresh = vnc_dpy_refresh;
1254 vnc_dpy_resize(vs->ds, 640, 400);
1256 return display;
1259 int vnc_start_viewer(int port)
1261 int pid;
1262 char s[16];
1264 sprintf(s, ":%d", port);
1266 switch (pid = fork()) {
1267 case -1:
1268 fprintf(stderr, "vncviewer failed fork\n");
1269 exit(1);
1271 case 0: /* child */
1272 execlp("vncviewer", "vncviewer", s, 0);
1273 fprintf(stderr, "vncviewer execlp failed\n");
1274 exit(1);
1276 default:
1277 return pid;