ia64/xen-unstable

view tools/ioemu/vnc.c @ 10805:2b1a49dd1273

[qemu] Use domain-name in VNC window title.
Set the VNC window title with domain-name.

Signed-off-by: Yang Xiaowei <xiaowei.yang@intel.com>
Signed-off-by: Christian Limpach <Christian.Limpach@xensource.com>
author chris@kneesaa.uk.xensource.com
date Wed Jul 26 14:26:03 2006 +0100 (2006-07-26)
parents 060025203f54
children 3e07ec30c445
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 typedef struct Buffer
36 {
37 size_t capacity;
38 size_t offset;
39 char *buffer;
40 } Buffer;
42 typedef struct VncState VncState;
44 typedef int VncReadEvent(VncState *vs, char *data, size_t len);
46 struct VncState
47 {
48 QEMUTimer *timer;
49 int lsock;
50 int csock;
51 DisplayState *ds;
52 int need_update;
53 int width;
54 int height;
55 uint64_t *dirty_row; /* screen regions which are possibly dirty */
56 int dirty_pixel_shift;
57 uint64_t *update_row; /* outstanding updates */
58 int has_update; /* there's outstanding updates in the
59 * visible area */
60 char *old_data;
61 int depth;
62 int has_resize;
63 int has_hextile;
64 Buffer output;
65 Buffer input;
66 kbd_layout_t *kbd_layout;
68 VncReadEvent *read_handler;
69 size_t read_handler_expect;
71 int visible_x;
72 int visible_y;
73 int visible_w;
74 int visible_h;
76 int slow_client;
77 };
79 #define DIRTY_PIXEL_BITS 64
80 #define X2DP_DOWN(vs, x) ((x) >> (vs)->dirty_pixel_shift)
81 #define X2DP_UP(vs, x) \
82 (((x) + (1ULL << (vs)->dirty_pixel_shift) - 1) >> (vs)->dirty_pixel_shift)
83 #define DP2X(vs, x) ((x) << (vs)->dirty_pixel_shift)
85 /* TODO
86 1) Get the queue working for IO.
87 2) there is some weirdness when using the -S option (the screen is grey
88 and not totally invalidated
89 */
91 static void vnc_write(VncState *vs, const void *data, size_t len);
92 static void vnc_write_u32(VncState *vs, uint32_t value);
93 static void vnc_write_s32(VncState *vs, int32_t value);
94 static void vnc_write_u16(VncState *vs, uint16_t value);
95 static void vnc_write_u8(VncState *vs, uint8_t value);
96 static void vnc_flush(VncState *vs);
97 static void _vnc_update_client(void *opaque);
98 static void vnc_update_client(void *opaque);
99 static void vnc_client_read(void *opaque);
100 static void framebuffer_set_updated(VncState *vs, int x, int y, int w, int h);
102 static void set_bits_in_row(VncState *vs, uint64_t *row,
103 int x, int y, int w, int h)
104 {
105 int x1, x2;
106 uint64_t mask;
108 if (w == 0)
109 return;
111 x1 = X2DP_DOWN(vs, x);
112 x2 = X2DP_UP(vs, x + w);
114 if (X2DP_UP(vs, w) != DIRTY_PIXEL_BITS)
115 mask = ((1ULL << (x2 - x1)) - 1) << x1;
116 else
117 mask = ~(0ULL);
119 h += y;
120 for (; y < h; y++)
121 row[y] |= mask;
122 }
124 static void vnc_dpy_update(DisplayState *ds, int x, int y, int w, int h)
125 {
126 VncState *vs = ds->opaque;
128 set_bits_in_row(vs, vs->dirty_row, x, y, w, h);
129 }
131 static void vnc_framebuffer_update(VncState *vs, int x, int y, int w, int h,
132 int32_t encoding)
133 {
134 vnc_write_u16(vs, x);
135 vnc_write_u16(vs, y);
136 vnc_write_u16(vs, w);
137 vnc_write_u16(vs, h);
139 vnc_write_s32(vs, encoding);
140 }
142 static void vnc_dpy_resize(DisplayState *ds, int w, int h)
143 {
144 VncState *vs = ds->opaque;
145 int o;
147 ds->data = realloc(ds->data, w * h * vs->depth);
148 vs->old_data = realloc(vs->old_data, w * h * vs->depth);
149 vs->dirty_row = realloc(vs->dirty_row, h * sizeof(vs->dirty_row[0]));
150 vs->update_row = realloc(vs->update_row, h * sizeof(vs->dirty_row[0]));
152 if (ds->data == NULL || vs->old_data == NULL ||
153 vs->dirty_row == NULL || vs->update_row == NULL) {
154 fprintf(stderr, "vnc: memory allocation failed\n");
155 exit(1);
156 }
158 if (ds->depth != vs->depth * 8) {
159 ds->depth = vs->depth * 8;
160 set_color_table(ds);
161 }
162 ds->width = w;
163 ds->height = h;
164 ds->linesize = w * vs->depth;
165 if (vs->csock != -1 && vs->has_resize) {
166 vnc_write_u8(vs, 0); /* msg id */
167 vnc_write_u8(vs, 0);
168 vnc_write_u16(vs, 1); /* number of rects */
169 vnc_framebuffer_update(vs, 0, 0, ds->width, ds->height, -223);
170 vnc_flush(vs);
171 vs->width = ds->width;
172 vs->height = ds->height;
173 }
174 vs->dirty_pixel_shift = 0;
175 for (o = DIRTY_PIXEL_BITS; o < ds->width; o *= 2)
176 vs->dirty_pixel_shift++;
177 framebuffer_set_updated(vs, 0, 0, ds->width, ds->height);
178 }
180 static void send_framebuffer_update_raw(VncState *vs, int x, int y, int w, int h)
181 {
182 int i;
183 char *row;
185 vnc_framebuffer_update(vs, x, y, w, h, 0);
187 row = vs->ds->data + y * vs->ds->linesize + x * vs->depth;
188 for (i = 0; i < h; i++) {
189 vnc_write(vs, row, w * vs->depth);
190 row += vs->ds->linesize;
191 }
192 }
194 static void hextile_enc_cord(uint8_t *ptr, int x, int y, int w, int h)
195 {
196 ptr[0] = ((x & 0x0F) << 4) | (y & 0x0F);
197 ptr[1] = (((w - 1) & 0x0F) << 4) | ((h - 1) & 0x0F);
198 }
200 #define BPP 8
201 #include "vnchextile.h"
202 #undef BPP
204 #define BPP 16
205 #include "vnchextile.h"
206 #undef BPP
208 #define BPP 32
209 #include "vnchextile.h"
210 #undef BPP
212 static void send_framebuffer_update_hextile(VncState *vs, int x, int y, int w, int h)
213 {
214 int i, j;
215 int has_fg, has_bg;
216 uint32_t last_fg32, last_bg32;
217 uint16_t last_fg16, last_bg16;
218 uint8_t last_fg8, last_bg8;
220 vnc_framebuffer_update(vs, x, y, w, h, 5);
222 has_fg = has_bg = 0;
223 for (j = y; j < (y + h); j += 16) {
224 for (i = x; i < (x + w); i += 16) {
225 switch (vs->depth) {
226 case 1:
227 send_hextile_tile_8(vs, i, j, MIN(16, x + w - i), MIN(16, y + h - j),
228 &last_bg8, &last_fg8, &has_bg, &has_fg);
229 break;
230 case 2:
231 send_hextile_tile_16(vs, i, j, MIN(16, x + w - i), MIN(16, y + h - j),
232 &last_bg16, &last_fg16, &has_bg, &has_fg);
233 break;
234 case 4:
235 send_hextile_tile_32(vs, i, j, MIN(16, x + w - i), MIN(16, y + h - j),
236 &last_bg32, &last_fg32, &has_bg, &has_fg);
237 break;
238 default:
239 break;
240 }
241 }
242 }
243 }
245 static void send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
246 {
247 if (vs->has_hextile)
248 send_framebuffer_update_hextile(vs, x, y, w, h);
249 else
250 send_framebuffer_update_raw(vs, x, y, w, h);
251 }
253 static void vnc_copy(DisplayState *ds, int src_x, int src_y, int dst_x, int dst_y, int w, int h)
254 {
255 int src, dst;
256 char *src_row;
257 char *dst_row;
258 char *old_row;
259 int y = 0;
260 int pitch = ds->linesize;
261 VncState *vs = ds->opaque;
262 int updating_client = !vs->slow_client;
264 if (src_x < vs->visible_x || src_y < vs->visible_y ||
265 dst_x < vs->visible_x || dst_y < vs->visible_y ||
266 (src_x + w) > (vs->visible_x + vs->visible_w) ||
267 (src_y + h) > (vs->visible_y + vs->visible_h) ||
268 (dst_x + w) > (vs->visible_x + vs->visible_w) ||
269 (dst_y + h) > (vs->visible_y + vs->visible_h))
270 updating_client = 0;
272 if (updating_client) {
273 vs->need_update = 1;
274 _vnc_update_client(vs);
275 }
277 if (dst_y > src_y) {
278 y = h - 1;
279 pitch = -pitch;
280 }
282 src = (ds->linesize * (src_y + y) + vs->depth * src_x);
283 dst = (ds->linesize * (dst_y + y) + vs->depth * dst_x);
285 src_row = ds->data + src;
286 dst_row = ds->data + dst;
287 old_row = vs->old_data + dst;
289 for (y = 0; y < h; y++) {
290 memmove(old_row, src_row, w * vs->depth);
291 memmove(dst_row, src_row, w * vs->depth);
292 src_row += pitch;
293 dst_row += pitch;
294 old_row += pitch;
295 }
297 if (updating_client && vs->csock != -1 && !vs->has_update) {
298 vnc_write_u8(vs, 0); /* msg id */
299 vnc_write_u8(vs, 0);
300 vnc_write_u16(vs, 1); /* number of rects */
301 vnc_framebuffer_update(vs, dst_x, dst_y, w, h, 1);
302 vnc_write_u16(vs, src_x);
303 vnc_write_u16(vs, src_y);
304 vnc_flush(vs);
305 } else
306 framebuffer_set_updated(vs, dst_x, dst_y, w, h);
307 }
309 static int find_update_height(VncState *vs, int y, int maxy, int last_x, int x)
310 {
311 int h;
313 for (h = 1; y + h < maxy; h++) {
314 int tmp_x;
315 if (!(vs->update_row[y + h] & (1ULL << last_x)))
316 break;
317 for (tmp_x = last_x; tmp_x < x; tmp_x++)
318 vs->update_row[y + h] &= ~(1ULL << tmp_x);
319 }
321 return h;
322 }
324 static void _vnc_update_client(void *opaque)
325 {
326 VncState *vs = opaque;
327 int64_t now = qemu_get_clock(rt_clock);
329 if (vs->need_update && vs->csock != -1) {
330 int y;
331 char *row;
332 char *old_row;
333 uint64_t width_mask;
334 int n_rectangles;
335 int saved_offset;
336 int maxx, maxy;
337 int tile_bytes = vs->depth * DP2X(vs, 1);
339 if (vs->width != DP2X(vs, DIRTY_PIXEL_BITS))
340 width_mask = (1ULL << X2DP_UP(vs, vs->ds->width)) - 1;
341 else
342 width_mask = ~(0ULL);
344 /* Walk through the dirty map and eliminate tiles that
345 really aren't dirty */
346 row = vs->ds->data;
347 old_row = vs->old_data;
349 for (y = 0; y < vs->ds->height; y++) {
350 if (vs->dirty_row[y] & width_mask) {
351 int x;
352 char *ptr, *old_ptr;
354 ptr = row;
355 old_ptr = old_row;
357 for (x = 0; x < X2DP_UP(vs, vs->ds->width); x++) {
358 if (vs->dirty_row[y] & (1ULL << x)) {
359 if (memcmp(old_ptr, ptr, tile_bytes)) {
360 vs->has_update = 1;
361 vs->update_row[y] |= (1ULL << x);
362 memcpy(old_ptr, ptr, tile_bytes);
363 }
364 vs->dirty_row[y] &= ~(1ULL << x);
365 }
367 ptr += tile_bytes;
368 old_ptr += tile_bytes;
369 }
370 }
372 row += vs->ds->linesize;
373 old_row += vs->ds->linesize;
374 }
376 if (!vs->has_update || vs->visible_y >= vs->ds->height ||
377 vs->visible_x >= vs->ds->width)
378 goto out;
380 /* Count rectangles */
381 n_rectangles = 0;
382 vnc_write_u8(vs, 0); /* msg id */
383 vnc_write_u8(vs, 0);
384 saved_offset = vs->output.offset;
385 vnc_write_u16(vs, 0);
387 maxy = vs->visible_y + vs->visible_h;
388 if (maxy > vs->ds->height)
389 maxy = vs->ds->height;
390 maxx = vs->visible_x + vs->visible_w;
391 if (maxx > vs->ds->width)
392 maxx = vs->ds->width;
394 for (y = vs->visible_y; y < maxy; y++) {
395 int x;
396 int last_x = -1;
397 for (x = X2DP_DOWN(vs, vs->visible_x);
398 x < X2DP_UP(vs, maxx); x++) {
399 if (vs->update_row[y] & (1ULL << x)) {
400 if (last_x == -1)
401 last_x = x;
402 vs->update_row[y] &= ~(1ULL << x);
403 } else {
404 if (last_x != -1) {
405 int h = find_update_height(vs, y, maxy, last_x, x);
406 send_framebuffer_update(vs, DP2X(vs, last_x), y,
407 DP2X(vs, (x - last_x)), h);
408 n_rectangles++;
409 }
410 last_x = -1;
411 }
412 }
413 if (last_x != -1) {
414 int h = find_update_height(vs, y, maxy, last_x, x);
415 send_framebuffer_update(vs, DP2X(vs, last_x), y,
416 DP2X(vs, (x - last_x)), h);
417 n_rectangles++;
418 }
419 }
420 vs->output.buffer[saved_offset] = (n_rectangles >> 8) & 0xFF;
421 vs->output.buffer[saved_offset + 1] = n_rectangles & 0xFF;
423 vs->has_update = 0;
424 vs->need_update = 0;
425 vnc_flush(vs);
426 vs->slow_client = 0;
427 } else
428 vs->slow_client = 1;
430 out:
431 qemu_mod_timer(vs->timer, now + VNC_REFRESH_INTERVAL);
432 }
434 static void vnc_update_client(void *opaque)
435 {
436 VncState *vs = opaque;
438 vs->ds->dpy_refresh(vs->ds);
439 _vnc_update_client(vs);
440 }
442 static void vnc_timer_init(VncState *vs)
443 {
444 if (vs->timer == NULL) {
445 vs->timer = qemu_new_timer(rt_clock, vnc_update_client, vs);
446 qemu_mod_timer(vs->timer, qemu_get_clock(rt_clock));
447 }
448 }
450 static void vnc_dpy_refresh(DisplayState *ds)
451 {
452 vga_hw_update();
453 }
455 static int vnc_listen_poll(void *opaque)
456 {
457 VncState *vs = opaque;
458 if (vs->csock == -1)
459 return 1;
460 return 0;
461 }
463 static void buffer_reserve(Buffer *buffer, size_t len)
464 {
465 if ((buffer->capacity - buffer->offset) < len) {
466 buffer->capacity += (len + 1024);
467 buffer->buffer = realloc(buffer->buffer, buffer->capacity);
468 if (buffer->buffer == NULL) {
469 fprintf(stderr, "vnc: out of memory\n");
470 exit(1);
471 }
472 }
473 }
475 static int buffer_empty(Buffer *buffer)
476 {
477 return buffer->offset == 0;
478 }
480 static char *buffer_end(Buffer *buffer)
481 {
482 return buffer->buffer + buffer->offset;
483 }
485 static void buffer_reset(Buffer *buffer)
486 {
487 buffer->offset = 0;
488 }
490 static void buffer_append(Buffer *buffer, const void *data, size_t len)
491 {
492 memcpy(buffer->buffer + buffer->offset, data, len);
493 buffer->offset += len;
494 }
496 static int vnc_client_io_error(VncState *vs, int ret, int last_errno)
497 {
498 if (ret == 0 || ret == -1) {
499 if (ret == -1 && (last_errno == EINTR || last_errno == EAGAIN))
500 return 0;
502 qemu_set_fd_handler2(vs->csock, NULL, NULL, NULL, NULL);
503 closesocket(vs->csock);
504 vs->csock = -1;
505 buffer_reset(&vs->input);
506 buffer_reset(&vs->output);
507 vs->need_update = 0;
508 return 0;
509 }
510 return ret;
511 }
513 static void vnc_client_error(VncState *vs)
514 {
515 vnc_client_io_error(vs, -1, EINVAL);
516 }
518 static void vnc_client_write(void *opaque)
519 {
520 ssize_t ret;
521 VncState *vs = opaque;
523 ret = send(vs->csock, vs->output.buffer, vs->output.offset, 0);
524 ret = vnc_client_io_error(vs, ret, socket_error());
525 if (!ret)
526 return;
528 memmove(vs->output.buffer, vs->output.buffer + ret,
529 vs->output.offset - ret);
530 vs->output.offset -= ret;
532 if (vs->output.offset == 0)
533 qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
534 }
536 static void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting)
537 {
538 vs->read_handler = func;
539 vs->read_handler_expect = expecting;
540 }
542 static void vnc_client_read(void *opaque)
543 {
544 VncState *vs = opaque;
545 ssize_t ret;
547 buffer_reserve(&vs->input, 4096);
549 ret = recv(vs->csock, buffer_end(&vs->input), 4096, 0);
550 ret = vnc_client_io_error(vs, ret, socket_error());
551 if (!ret)
552 return;
554 vs->input.offset += ret;
556 while (vs->read_handler && vs->input.offset >= vs->read_handler_expect) {
557 size_t len = vs->read_handler_expect;
558 int ret;
560 ret = vs->read_handler(vs, vs->input.buffer, len);
561 if (vs->csock == -1)
562 return;
564 if (!ret) {
565 memmove(vs->input.buffer, vs->input.buffer + len,
566 vs->input.offset - len);
567 vs->input.offset -= len;
568 } else
569 vs->read_handler_expect = ret;
570 }
571 }
573 static void vnc_write(VncState *vs, const void *data, size_t len)
574 {
575 buffer_reserve(&vs->output, len);
577 if (buffer_empty(&vs->output))
578 qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read,
579 vnc_client_write, vs);
581 buffer_append(&vs->output, data, len);
582 }
584 static void vnc_write_s32(VncState *vs, int32_t value)
585 {
586 vnc_write_u32(vs, *(uint32_t *)&value);
587 }
589 static void vnc_write_u32(VncState *vs, uint32_t value)
590 {
591 uint8_t buf[4];
593 buf[0] = (value >> 24) & 0xFF;
594 buf[1] = (value >> 16) & 0xFF;
595 buf[2] = (value >> 8) & 0xFF;
596 buf[3] = value & 0xFF;
598 vnc_write(vs, buf, 4);
599 }
601 static void vnc_write_u16(VncState *vs, uint16_t value)
602 {
603 char buf[2];
605 buf[0] = (value >> 8) & 0xFF;
606 buf[1] = value & 0xFF;
608 vnc_write(vs, buf, 2);
609 }
611 static void vnc_write_u8(VncState *vs, uint8_t value)
612 {
613 vnc_write(vs, (char *)&value, 1);
614 }
616 static void vnc_flush(VncState *vs)
617 {
618 if (vs->output.offset)
619 vnc_client_write(vs);
620 }
622 static uint8_t read_u8(char *data, size_t offset)
623 {
624 return data[offset];
625 }
627 static uint16_t read_u16(char *data, size_t offset)
628 {
629 return ((data[offset] & 0xFF) << 8) | (data[offset + 1] & 0xFF);
630 }
632 static int32_t read_s32(char *data, size_t offset)
633 {
634 return (int32_t)((data[offset] << 24) | (data[offset + 1] << 16) |
635 (data[offset + 2] << 8) | data[offset + 3]);
636 }
638 static uint32_t read_u32(char *data, size_t offset)
639 {
640 return ((data[offset] << 24) | (data[offset + 1] << 16) |
641 (data[offset + 2] << 8) | data[offset + 3]);
642 }
644 static void client_cut_text(VncState *vs, size_t len, char *text)
645 {
646 }
648 static void pointer_event(VncState *vs, int button_mask, int x, int y)
649 {
650 int buttons = 0;
651 int dz = 0;
653 if (button_mask & 0x01)
654 buttons |= MOUSE_EVENT_LBUTTON;
655 if (button_mask & 0x02)
656 buttons |= MOUSE_EVENT_MBUTTON;
657 if (button_mask & 0x04)
658 buttons |= MOUSE_EVENT_RBUTTON;
659 if (button_mask & 0x08)
660 dz = -1;
661 if (button_mask & 0x10)
662 dz = 1;
664 if (kbd_mouse_is_absolute()) {
665 kbd_mouse_event(x * 0x7FFF / vs->ds->width,
666 y * 0x7FFF / vs->ds->height,
667 dz, buttons);
668 } else {
669 static int last_x = -1;
670 static int last_y = -1;
672 if (last_x != -1)
673 kbd_mouse_event(x - last_x, y - last_y, dz, buttons);
675 last_x = x;
676 last_y = y;
677 }
678 }
680 static void do_key_event(VncState *vs, int down, uint32_t sym)
681 {
682 int keycode;
684 keycode = keysym2scancode(vs->kbd_layout, sym & 0xFFFF);
686 if (keycode & 0x80)
687 kbd_put_keycode(0xe0);
688 if (down)
689 kbd_put_keycode(keycode & 0x7f);
690 else
691 kbd_put_keycode(keycode | 0x80);
692 }
694 static void key_event(VncState *vs, int down, uint32_t sym)
695 {
696 if (sym >= 'A' && sym <= 'Z')
697 sym = sym - 'A' + 'a';
698 do_key_event(vs, down, sym);
699 }
701 static void framebuffer_set_updated(VncState *vs, int x, int y, int w, int h)
702 {
704 set_bits_in_row(vs, vs->update_row, x, y, w, h);
706 vs->has_update = 1;
707 }
709 static void framebuffer_update_request(VncState *vs, int incremental,
710 int x_position, int y_position,
711 int w, int h)
712 {
713 vs->need_update = 1;
714 if (!incremental)
715 framebuffer_set_updated(vs, x_position, y_position, w, h);
716 vs->visible_x = x_position;
717 vs->visible_y = y_position;
718 vs->visible_w = w;
719 vs->visible_h = h;
720 }
722 static void set_encodings(VncState *vs, int32_t *encodings, size_t n_encodings)
723 {
724 int i;
726 vs->has_hextile = 0;
727 vs->has_resize = 0;
728 vs->ds->dpy_copy = NULL;
730 for (i = n_encodings - 1; i >= 0; i--) {
731 switch (encodings[i]) {
732 case 0: /* Raw */
733 vs->has_hextile = 0;
734 break;
735 case 1: /* CopyRect */
736 vs->ds->dpy_copy = vnc_copy;
737 break;
738 case 5: /* Hextile */
739 vs->has_hextile = 1;
740 break;
741 case -223: /* DesktopResize */
742 vs->has_resize = 1;
743 break;
744 default:
745 break;
746 }
747 }
748 }
750 static void set_pixel_format(VncState *vs,
751 int bits_per_pixel, int depth,
752 int big_endian_flag, int true_color_flag,
753 int red_max, int green_max, int blue_max,
754 int red_shift, int green_shift, int blue_shift)
755 {
756 switch (bits_per_pixel) {
757 case 32:
758 case 24:
759 vs->depth = 4;
760 break;
761 case 16:
762 vs->depth = 2;
763 break;
764 case 8:
765 vs->depth = 1;
766 break;
767 default:
768 vnc_client_error(vs);
769 break;
770 }
772 if (!true_color_flag)
773 vnc_client_error(vs);
775 vnc_dpy_resize(vs->ds, vs->ds->width, vs->ds->height);
777 vga_hw_invalidate();
778 vga_hw_update();
779 }
781 static int protocol_client_msg(VncState *vs, char *data, size_t len)
782 {
783 int i;
784 uint16_t limit;
786 switch (data[0]) {
787 case 0:
788 if (len == 1)
789 return 20;
791 set_pixel_format(vs, read_u8(data, 4), read_u8(data, 5),
792 read_u8(data, 6), read_u8(data, 7),
793 read_u16(data, 8), read_u16(data, 10),
794 read_u16(data, 12), read_u8(data, 14),
795 read_u8(data, 15), read_u8(data, 16));
796 break;
797 case 2:
798 if (len == 1)
799 return 4;
801 if (len == 4)
802 return 4 + (read_u16(data, 2) * 4);
804 limit = read_u16(data, 2);
805 for (i = 0; i < limit; i++) {
806 int32_t val = read_s32(data, 4 + (i * 4));
807 memcpy(data + 4 + (i * 4), &val, sizeof(val));
808 }
810 set_encodings(vs, (int32_t *)(data + 4), limit);
811 break;
812 case 3:
813 if (len == 1)
814 return 10;
816 framebuffer_update_request(vs,
817 read_u8(data, 1), read_u16(data, 2), read_u16(data, 4),
818 read_u16(data, 6), read_u16(data, 8));
819 break;
820 case 4:
821 if (len == 1)
822 return 8;
824 key_event(vs, read_u8(data, 1), read_u32(data, 4));
825 break;
826 case 5:
827 if (len == 1)
828 return 6;
830 pointer_event(vs, read_u8(data, 1), read_u16(data, 2), read_u16(data, 4));
831 break;
832 case 6:
833 if (len == 1)
834 return 8;
836 if (len == 8)
837 return 8 + read_u32(data, 4);
839 client_cut_text(vs, read_u32(data, 4), data + 8);
840 break;
841 default:
842 printf("Msg: %d\n", data[0]);
843 vnc_client_error(vs);
844 break;
845 }
847 vnc_read_when(vs, protocol_client_msg, 1);
848 return 0;
849 }
851 static int protocol_client_init(VncState *vs, char *data, size_t len)
852 {
853 size_t l;
854 char pad[3] = { 0, 0, 0 };
856 vs->width = vs->ds->width;
857 vs->height = vs->ds->height;
858 vnc_write_u16(vs, vs->ds->width);
859 vnc_write_u16(vs, vs->ds->height);
861 vnc_write_u8(vs, vs->depth * 8); /* bits-per-pixel */
862 vnc_write_u8(vs, vs->depth * 8); /* depth */
863 vnc_write_u8(vs, 0); /* big-endian-flag */
864 vnc_write_u8(vs, 1); /* true-color-flag */
865 if (vs->depth == 4) {
866 vnc_write_u16(vs, 0xFF); /* red-max */
867 vnc_write_u16(vs, 0xFF); /* green-max */
868 vnc_write_u16(vs, 0xFF); /* blue-max */
869 vnc_write_u8(vs, 16); /* red-shift */
870 vnc_write_u8(vs, 8); /* green-shift */
871 vnc_write_u8(vs, 0); /* blue-shift */
872 } else if (vs->depth == 2) {
873 vnc_write_u16(vs, 31); /* red-max */
874 vnc_write_u16(vs, 63); /* green-max */
875 vnc_write_u16(vs, 31); /* blue-max */
876 vnc_write_u8(vs, 11); /* red-shift */
877 vnc_write_u8(vs, 5); /* green-shift */
878 vnc_write_u8(vs, 0); /* blue-shift */
879 } else if (vs->depth == 1) {
880 vnc_write_u16(vs, 3); /* red-max */
881 vnc_write_u16(vs, 7); /* green-max */
882 vnc_write_u16(vs, 3); /* blue-max */
883 vnc_write_u8(vs, 5); /* red-shift */
884 vnc_write_u8(vs, 2); /* green-shift */
885 vnc_write_u8(vs, 0); /* blue-shift */
886 }
888 vnc_write(vs, pad, 3); /* padding */
890 l = strlen(domain_name);
891 vnc_write_u32(vs, l);
892 vnc_write(vs, domain_name, l);
894 vnc_flush(vs);
896 vnc_read_when(vs, protocol_client_msg, 1);
898 return 0;
899 }
901 static int protocol_version(VncState *vs, char *version, size_t len)
902 {
903 char local[13];
904 int maj, min;
906 memcpy(local, version, 12);
907 local[12] = 0;
909 if (sscanf(local, "RFB %03d.%03d\n", &maj, &min) != 2) {
910 vnc_client_error(vs);
911 return 0;
912 }
914 vnc_write_u32(vs, 1); /* None */
915 vnc_flush(vs);
917 vnc_read_when(vs, protocol_client_init, 1);
919 return 0;
920 }
922 static void vnc_listen_read(void *opaque)
923 {
924 VncState *vs = opaque;
925 struct sockaddr_in addr;
926 socklen_t addrlen = sizeof(addr);
928 vs->csock = accept(vs->lsock, (struct sockaddr *)&addr, &addrlen);
929 if (vs->csock != -1) {
930 socket_set_nonblock(vs->csock);
931 qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, opaque);
932 vnc_write(vs, "RFB 003.003\n", 12);
933 vnc_flush(vs);
934 vnc_read_when(vs, protocol_version, 12);
935 framebuffer_set_updated(vs, 0, 0, vs->ds->width, vs->ds->height);
936 vs->has_resize = 0;
937 vs->has_hextile = 0;
938 vs->ds->dpy_copy = NULL;
939 vnc_timer_init(vs);
940 }
941 }
943 void vnc_display_init(DisplayState *ds, int display)
944 {
945 struct sockaddr_in addr;
946 int reuse_addr, ret;
947 VncState *vs;
949 vs = qemu_mallocz(sizeof(VncState));
950 if (!vs)
951 exit(1);
953 ds->opaque = vs;
955 vs->lsock = -1;
956 vs->csock = -1;
957 vs->depth = 4;
959 vs->ds = ds;
961 if (!keyboard_layout)
962 keyboard_layout = "en-us";
964 vs->kbd_layout = init_keyboard_layout(keyboard_layout);
965 if (!vs->kbd_layout)
966 exit(1);
968 vs->lsock = socket(PF_INET, SOCK_STREAM, 0);
969 if (vs->lsock == -1) {
970 fprintf(stderr, "Could not create socket\n");
971 exit(1);
972 }
974 addr.sin_family = AF_INET;
975 addr.sin_port = htons(5900 + display);
976 memset(&addr.sin_addr, 0, sizeof(addr.sin_addr));
978 reuse_addr = 1;
979 ret = setsockopt(vs->lsock, SOL_SOCKET, SO_REUSEADDR,
980 (const char *)&reuse_addr, sizeof(reuse_addr));
981 if (ret == -1) {
982 fprintf(stderr, "setsockopt() failed\n");
983 exit(1);
984 }
986 if (bind(vs->lsock, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
987 fprintf(stderr, "bind() failed\n");
988 exit(1);
989 }
991 if (listen(vs->lsock, 1) == -1) {
992 fprintf(stderr, "listen() failed\n");
993 exit(1);
994 }
996 ret = qemu_set_fd_handler2(vs->lsock, vnc_listen_poll, vnc_listen_read,
997 NULL, vs);
998 if (ret == -1)
999 exit(1);
1001 vs->ds->data = NULL;
1002 vs->ds->dpy_update = vnc_dpy_update;
1003 vs->ds->dpy_resize = vnc_dpy_resize;
1004 vs->ds->dpy_refresh = vnc_dpy_refresh;
1006 vnc_dpy_resize(vs->ds, 640, 400);
1009 int vnc_start_viewer(int port)
1011 int pid;
1012 char s[16];
1014 sprintf(s, ":%d", port);
1016 switch (pid = fork()) {
1017 case -1:
1018 fprintf(stderr, "vncviewer failed fork\n");
1019 exit(1);
1021 case 0: /* child */
1022 execlp("vncviewer", "vncviewer", s, 0);
1023 fprintf(stderr, "vncviewer execlp failed\n");
1024 exit(1);
1026 default:
1027 return pid;