ia64/xen-unstable

view tools/ioemu/vnc.c @ 10807:3e07ec30c445

[qemu] Add access to the monito vt over VNC.
Recognize ctrl+alt+1/2/3 to switch among screens.

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:52:47 2006 +0100 (2006-07-26)
parents 2b1a49dd1273
children 21918b22746e 08a11694b109
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 struct VncState
51 {
52 QEMUTimer *timer;
53 int lsock;
54 int csock;
55 DisplayState *ds;
56 int need_update;
57 int width;
58 int height;
59 uint64_t *dirty_row; /* screen regions which are possibly dirty */
60 int dirty_pixel_shift;
61 uint64_t *update_row; /* outstanding updates */
62 int has_update; /* there's outstanding updates in the
63 * visible area */
64 char *old_data;
65 int depth;
66 int has_resize;
67 int has_hextile;
68 Buffer output;
69 Buffer input;
70 kbd_layout_t *kbd_layout;
71 int ctl_keys; /* Ctrl+Alt starts calibration */
73 VncReadEvent *read_handler;
74 size_t read_handler_expect;
76 int visible_x;
77 int visible_y;
78 int visible_w;
79 int visible_h;
81 int slow_client;
82 };
84 #define DIRTY_PIXEL_BITS 64
85 #define X2DP_DOWN(vs, x) ((x) >> (vs)->dirty_pixel_shift)
86 #define X2DP_UP(vs, x) \
87 (((x) + (1ULL << (vs)->dirty_pixel_shift) - 1) >> (vs)->dirty_pixel_shift)
88 #define DP2X(vs, x) ((x) << (vs)->dirty_pixel_shift)
90 /* TODO
91 1) Get the queue working for IO.
92 2) there is some weirdness when using the -S option (the screen is grey
93 and not totally invalidated
94 */
96 static void vnc_write(VncState *vs, const void *data, size_t len);
97 static void vnc_write_u32(VncState *vs, uint32_t value);
98 static void vnc_write_s32(VncState *vs, int32_t value);
99 static void vnc_write_u16(VncState *vs, uint16_t value);
100 static void vnc_write_u8(VncState *vs, uint8_t value);
101 static void vnc_flush(VncState *vs);
102 static void _vnc_update_client(void *opaque);
103 static void vnc_update_client(void *opaque);
104 static void vnc_client_read(void *opaque);
105 static void framebuffer_set_updated(VncState *vs, int x, int y, int w, int h);
107 static void set_bits_in_row(VncState *vs, uint64_t *row,
108 int x, int y, int w, int h)
109 {
110 int x1, x2;
111 uint64_t mask;
113 if (w == 0)
114 return;
116 x1 = X2DP_DOWN(vs, x);
117 x2 = X2DP_UP(vs, x + w);
119 if (X2DP_UP(vs, w) != DIRTY_PIXEL_BITS)
120 mask = ((1ULL << (x2 - x1)) - 1) << x1;
121 else
122 mask = ~(0ULL);
124 h += y;
125 for (; y < h; y++)
126 row[y] |= mask;
127 }
129 static void vnc_dpy_update(DisplayState *ds, int x, int y, int w, int h)
130 {
131 VncState *vs = ds->opaque;
133 set_bits_in_row(vs, vs->dirty_row, x, y, w, h);
134 }
136 static void vnc_framebuffer_update(VncState *vs, int x, int y, int w, int h,
137 int32_t encoding)
138 {
139 vnc_write_u16(vs, x);
140 vnc_write_u16(vs, y);
141 vnc_write_u16(vs, w);
142 vnc_write_u16(vs, h);
144 vnc_write_s32(vs, encoding);
145 }
147 static void vnc_dpy_resize(DisplayState *ds, int w, int h)
148 {
149 VncState *vs = ds->opaque;
150 int o;
152 ds->data = realloc(ds->data, w * h * vs->depth);
153 vs->old_data = realloc(vs->old_data, w * h * vs->depth);
154 vs->dirty_row = realloc(vs->dirty_row, h * sizeof(vs->dirty_row[0]));
155 vs->update_row = realloc(vs->update_row, h * sizeof(vs->dirty_row[0]));
157 if (ds->data == NULL || vs->old_data == NULL ||
158 vs->dirty_row == NULL || vs->update_row == NULL) {
159 fprintf(stderr, "vnc: memory allocation failed\n");
160 exit(1);
161 }
163 if (ds->depth != vs->depth * 8) {
164 ds->depth = vs->depth * 8;
165 set_color_table(ds);
166 }
167 ds->width = w;
168 ds->height = h;
169 ds->linesize = w * vs->depth;
170 if (vs->csock != -1 && vs->has_resize) {
171 vnc_write_u8(vs, 0); /* msg id */
172 vnc_write_u8(vs, 0);
173 vnc_write_u16(vs, 1); /* number of rects */
174 vnc_framebuffer_update(vs, 0, 0, ds->width, ds->height, -223);
175 vnc_flush(vs);
176 vs->width = ds->width;
177 vs->height = ds->height;
178 }
179 vs->dirty_pixel_shift = 0;
180 for (o = DIRTY_PIXEL_BITS; o < ds->width; o *= 2)
181 vs->dirty_pixel_shift++;
182 framebuffer_set_updated(vs, 0, 0, ds->width, ds->height);
183 }
185 static void send_framebuffer_update_raw(VncState *vs, int x, int y, int w, int h)
186 {
187 int i;
188 char *row;
190 vnc_framebuffer_update(vs, x, y, w, h, 0);
192 row = vs->ds->data + y * vs->ds->linesize + x * vs->depth;
193 for (i = 0; i < h; i++) {
194 vnc_write(vs, row, w * vs->depth);
195 row += vs->ds->linesize;
196 }
197 }
199 static void hextile_enc_cord(uint8_t *ptr, int x, int y, int w, int h)
200 {
201 ptr[0] = ((x & 0x0F) << 4) | (y & 0x0F);
202 ptr[1] = (((w - 1) & 0x0F) << 4) | ((h - 1) & 0x0F);
203 }
205 #define BPP 8
206 #include "vnchextile.h"
207 #undef BPP
209 #define BPP 16
210 #include "vnchextile.h"
211 #undef BPP
213 #define BPP 32
214 #include "vnchextile.h"
215 #undef BPP
217 static void send_framebuffer_update_hextile(VncState *vs, int x, int y, int w, int h)
218 {
219 int i, j;
220 int has_fg, has_bg;
221 uint32_t last_fg32, last_bg32;
222 uint16_t last_fg16, last_bg16;
223 uint8_t last_fg8, last_bg8;
225 vnc_framebuffer_update(vs, x, y, w, h, 5);
227 has_fg = has_bg = 0;
228 for (j = y; j < (y + h); j += 16) {
229 for (i = x; i < (x + w); i += 16) {
230 switch (vs->depth) {
231 case 1:
232 send_hextile_tile_8(vs, i, j, MIN(16, x + w - i), MIN(16, y + h - j),
233 &last_bg8, &last_fg8, &has_bg, &has_fg);
234 break;
235 case 2:
236 send_hextile_tile_16(vs, i, j, MIN(16, x + w - i), MIN(16, y + h - j),
237 &last_bg16, &last_fg16, &has_bg, &has_fg);
238 break;
239 case 4:
240 send_hextile_tile_32(vs, i, j, MIN(16, x + w - i), MIN(16, y + h - j),
241 &last_bg32, &last_fg32, &has_bg, &has_fg);
242 break;
243 default:
244 break;
245 }
246 }
247 }
248 }
250 static void send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
251 {
252 if (vs->has_hextile)
253 send_framebuffer_update_hextile(vs, x, y, w, h);
254 else
255 send_framebuffer_update_raw(vs, x, y, w, h);
256 }
258 static void vnc_copy(DisplayState *ds, int src_x, int src_y, int dst_x, int dst_y, int w, int h)
259 {
260 int src, dst;
261 char *src_row;
262 char *dst_row;
263 char *old_row;
264 int y = 0;
265 int pitch = ds->linesize;
266 VncState *vs = ds->opaque;
267 int updating_client = !vs->slow_client;
269 if (src_x < vs->visible_x || src_y < vs->visible_y ||
270 dst_x < vs->visible_x || dst_y < vs->visible_y ||
271 (src_x + w) > (vs->visible_x + vs->visible_w) ||
272 (src_y + h) > (vs->visible_y + vs->visible_h) ||
273 (dst_x + w) > (vs->visible_x + vs->visible_w) ||
274 (dst_y + h) > (vs->visible_y + vs->visible_h))
275 updating_client = 0;
277 if (updating_client) {
278 vs->need_update = 1;
279 _vnc_update_client(vs);
280 }
282 if (dst_y > src_y) {
283 y = h - 1;
284 pitch = -pitch;
285 }
287 src = (ds->linesize * (src_y + y) + vs->depth * src_x);
288 dst = (ds->linesize * (dst_y + y) + vs->depth * dst_x);
290 src_row = ds->data + src;
291 dst_row = ds->data + dst;
292 old_row = vs->old_data + dst;
294 for (y = 0; y < h; y++) {
295 memmove(old_row, src_row, w * vs->depth);
296 memmove(dst_row, src_row, w * vs->depth);
297 src_row += pitch;
298 dst_row += pitch;
299 old_row += pitch;
300 }
302 if (updating_client && vs->csock != -1 && !vs->has_update) {
303 vnc_write_u8(vs, 0); /* msg id */
304 vnc_write_u8(vs, 0);
305 vnc_write_u16(vs, 1); /* number of rects */
306 vnc_framebuffer_update(vs, dst_x, dst_y, w, h, 1);
307 vnc_write_u16(vs, src_x);
308 vnc_write_u16(vs, src_y);
309 vnc_flush(vs);
310 } else
311 framebuffer_set_updated(vs, dst_x, dst_y, w, h);
312 }
314 static int find_update_height(VncState *vs, int y, int maxy, int last_x, int x)
315 {
316 int h;
318 for (h = 1; y + h < maxy; h++) {
319 int tmp_x;
320 if (!(vs->update_row[y + h] & (1ULL << last_x)))
321 break;
322 for (tmp_x = last_x; tmp_x < x; tmp_x++)
323 vs->update_row[y + h] &= ~(1ULL << tmp_x);
324 }
326 return h;
327 }
329 static void _vnc_update_client(void *opaque)
330 {
331 VncState *vs = opaque;
332 int64_t now = qemu_get_clock(rt_clock);
334 if (vs->need_update && vs->csock != -1) {
335 int y;
336 char *row;
337 char *old_row;
338 uint64_t width_mask;
339 int n_rectangles;
340 int saved_offset;
341 int maxx, maxy;
342 int tile_bytes = vs->depth * DP2X(vs, 1);
344 if (vs->width != DP2X(vs, DIRTY_PIXEL_BITS))
345 width_mask = (1ULL << X2DP_UP(vs, vs->ds->width)) - 1;
346 else
347 width_mask = ~(0ULL);
349 /* Walk through the dirty map and eliminate tiles that
350 really aren't dirty */
351 row = vs->ds->data;
352 old_row = vs->old_data;
354 for (y = 0; y < vs->ds->height; y++) {
355 if (vs->dirty_row[y] & width_mask) {
356 int x;
357 char *ptr, *old_ptr;
359 ptr = row;
360 old_ptr = old_row;
362 for (x = 0; x < X2DP_UP(vs, vs->ds->width); x++) {
363 if (vs->dirty_row[y] & (1ULL << x)) {
364 if (memcmp(old_ptr, ptr, tile_bytes)) {
365 vs->has_update = 1;
366 vs->update_row[y] |= (1ULL << x);
367 memcpy(old_ptr, ptr, tile_bytes);
368 }
369 vs->dirty_row[y] &= ~(1ULL << x);
370 }
372 ptr += tile_bytes;
373 old_ptr += tile_bytes;
374 }
375 }
377 row += vs->ds->linesize;
378 old_row += vs->ds->linesize;
379 }
381 if (!vs->has_update || vs->visible_y >= vs->ds->height ||
382 vs->visible_x >= vs->ds->width)
383 goto out;
385 /* Count rectangles */
386 n_rectangles = 0;
387 vnc_write_u8(vs, 0); /* msg id */
388 vnc_write_u8(vs, 0);
389 saved_offset = vs->output.offset;
390 vnc_write_u16(vs, 0);
392 maxy = vs->visible_y + vs->visible_h;
393 if (maxy > vs->ds->height)
394 maxy = vs->ds->height;
395 maxx = vs->visible_x + vs->visible_w;
396 if (maxx > vs->ds->width)
397 maxx = vs->ds->width;
399 for (y = vs->visible_y; y < maxy; y++) {
400 int x;
401 int last_x = -1;
402 for (x = X2DP_DOWN(vs, vs->visible_x);
403 x < X2DP_UP(vs, maxx); x++) {
404 if (vs->update_row[y] & (1ULL << x)) {
405 if (last_x == -1)
406 last_x = x;
407 vs->update_row[y] &= ~(1ULL << x);
408 } else {
409 if (last_x != -1) {
410 int h = find_update_height(vs, y, maxy, last_x, x);
411 send_framebuffer_update(vs, DP2X(vs, last_x), y,
412 DP2X(vs, (x - last_x)), h);
413 n_rectangles++;
414 }
415 last_x = -1;
416 }
417 }
418 if (last_x != -1) {
419 int h = find_update_height(vs, y, maxy, last_x, x);
420 send_framebuffer_update(vs, DP2X(vs, last_x), y,
421 DP2X(vs, (x - last_x)), h);
422 n_rectangles++;
423 }
424 }
425 vs->output.buffer[saved_offset] = (n_rectangles >> 8) & 0xFF;
426 vs->output.buffer[saved_offset + 1] = n_rectangles & 0xFF;
428 vs->has_update = 0;
429 vs->need_update = 0;
430 vnc_flush(vs);
431 vs->slow_client = 0;
432 } else
433 vs->slow_client = 1;
435 out:
436 qemu_mod_timer(vs->timer, now + VNC_REFRESH_INTERVAL);
437 }
439 static void vnc_update_client(void *opaque)
440 {
441 VncState *vs = opaque;
443 vs->ds->dpy_refresh(vs->ds);
444 _vnc_update_client(vs);
445 }
447 static void vnc_timer_init(VncState *vs)
448 {
449 if (vs->timer == NULL) {
450 vs->timer = qemu_new_timer(rt_clock, vnc_update_client, vs);
451 qemu_mod_timer(vs->timer, qemu_get_clock(rt_clock));
452 }
453 }
455 static void vnc_dpy_refresh(DisplayState *ds)
456 {
457 vga_hw_update();
458 }
460 static int vnc_listen_poll(void *opaque)
461 {
462 VncState *vs = opaque;
463 if (vs->csock == -1)
464 return 1;
465 return 0;
466 }
468 static void buffer_reserve(Buffer *buffer, size_t len)
469 {
470 if ((buffer->capacity - buffer->offset) < len) {
471 buffer->capacity += (len + 1024);
472 buffer->buffer = realloc(buffer->buffer, buffer->capacity);
473 if (buffer->buffer == NULL) {
474 fprintf(stderr, "vnc: out of memory\n");
475 exit(1);
476 }
477 }
478 }
480 static int buffer_empty(Buffer *buffer)
481 {
482 return buffer->offset == 0;
483 }
485 static char *buffer_end(Buffer *buffer)
486 {
487 return buffer->buffer + buffer->offset;
488 }
490 static void buffer_reset(Buffer *buffer)
491 {
492 buffer->offset = 0;
493 }
495 static void buffer_append(Buffer *buffer, const void *data, size_t len)
496 {
497 memcpy(buffer->buffer + buffer->offset, data, len);
498 buffer->offset += len;
499 }
501 static int vnc_client_io_error(VncState *vs, int ret, int last_errno)
502 {
503 if (ret == 0 || ret == -1) {
504 if (ret == -1 && (last_errno == EINTR || last_errno == EAGAIN))
505 return 0;
507 qemu_set_fd_handler2(vs->csock, NULL, NULL, NULL, NULL);
508 closesocket(vs->csock);
509 vs->csock = -1;
510 buffer_reset(&vs->input);
511 buffer_reset(&vs->output);
512 vs->need_update = 0;
513 return 0;
514 }
515 return ret;
516 }
518 static void vnc_client_error(VncState *vs)
519 {
520 vnc_client_io_error(vs, -1, EINVAL);
521 }
523 static void vnc_client_write(void *opaque)
524 {
525 ssize_t ret;
526 VncState *vs = opaque;
528 ret = send(vs->csock, vs->output.buffer, vs->output.offset, 0);
529 ret = vnc_client_io_error(vs, ret, socket_error());
530 if (!ret)
531 return;
533 memmove(vs->output.buffer, vs->output.buffer + ret,
534 vs->output.offset - ret);
535 vs->output.offset -= ret;
537 if (vs->output.offset == 0)
538 qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
539 }
541 static void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting)
542 {
543 vs->read_handler = func;
544 vs->read_handler_expect = expecting;
545 }
547 static void vnc_client_read(void *opaque)
548 {
549 VncState *vs = opaque;
550 ssize_t ret;
552 buffer_reserve(&vs->input, 4096);
554 ret = recv(vs->csock, buffer_end(&vs->input), 4096, 0);
555 ret = vnc_client_io_error(vs, ret, socket_error());
556 if (!ret)
557 return;
559 vs->input.offset += ret;
561 while (vs->read_handler && vs->input.offset >= vs->read_handler_expect) {
562 size_t len = vs->read_handler_expect;
563 int ret;
565 ret = vs->read_handler(vs, vs->input.buffer, len);
566 if (vs->csock == -1)
567 return;
569 if (!ret) {
570 memmove(vs->input.buffer, vs->input.buffer + len,
571 vs->input.offset - len);
572 vs->input.offset -= len;
573 } else
574 vs->read_handler_expect = ret;
575 }
576 }
578 static void vnc_write(VncState *vs, const void *data, size_t len)
579 {
580 buffer_reserve(&vs->output, len);
582 if (buffer_empty(&vs->output))
583 qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read,
584 vnc_client_write, vs);
586 buffer_append(&vs->output, data, len);
587 }
589 static void vnc_write_s32(VncState *vs, int32_t value)
590 {
591 vnc_write_u32(vs, *(uint32_t *)&value);
592 }
594 static void vnc_write_u32(VncState *vs, uint32_t value)
595 {
596 uint8_t buf[4];
598 buf[0] = (value >> 24) & 0xFF;
599 buf[1] = (value >> 16) & 0xFF;
600 buf[2] = (value >> 8) & 0xFF;
601 buf[3] = value & 0xFF;
603 vnc_write(vs, buf, 4);
604 }
606 static void vnc_write_u16(VncState *vs, uint16_t value)
607 {
608 char buf[2];
610 buf[0] = (value >> 8) & 0xFF;
611 buf[1] = value & 0xFF;
613 vnc_write(vs, buf, 2);
614 }
616 static void vnc_write_u8(VncState *vs, uint8_t value)
617 {
618 vnc_write(vs, (char *)&value, 1);
619 }
621 static void vnc_flush(VncState *vs)
622 {
623 if (vs->output.offset)
624 vnc_client_write(vs);
625 }
627 static uint8_t read_u8(char *data, size_t offset)
628 {
629 return data[offset];
630 }
632 static uint16_t read_u16(char *data, size_t offset)
633 {
634 return ((data[offset] & 0xFF) << 8) | (data[offset + 1] & 0xFF);
635 }
637 static int32_t read_s32(char *data, size_t offset)
638 {
639 return (int32_t)((data[offset] << 24) | (data[offset + 1] << 16) |
640 (data[offset + 2] << 8) | data[offset + 3]);
641 }
643 static uint32_t read_u32(char *data, size_t offset)
644 {
645 return ((data[offset] << 24) | (data[offset + 1] << 16) |
646 (data[offset + 2] << 8) | data[offset + 3]);
647 }
649 static void client_cut_text(VncState *vs, size_t len, char *text)
650 {
651 }
653 static void pointer_event(VncState *vs, int button_mask, int x, int y)
654 {
655 int buttons = 0;
656 int dz = 0;
658 if (button_mask & 0x01)
659 buttons |= MOUSE_EVENT_LBUTTON;
660 if (button_mask & 0x02)
661 buttons |= MOUSE_EVENT_MBUTTON;
662 if (button_mask & 0x04)
663 buttons |= MOUSE_EVENT_RBUTTON;
664 if (button_mask & 0x08)
665 dz = -1;
666 if (button_mask & 0x10)
667 dz = 1;
669 if (kbd_mouse_is_absolute()) {
670 kbd_mouse_event(x * 0x7FFF / vs->ds->width,
671 y * 0x7FFF / vs->ds->height,
672 dz, buttons);
673 } else {
674 static int last_x = -1;
675 static int last_y = -1;
677 if (last_x != -1)
678 kbd_mouse_event(x - last_x, y - last_y, dz, buttons);
680 last_x = x;
681 last_y = y;
682 }
683 }
685 static void do_key_event(VncState *vs, int down, uint32_t sym)
686 {
687 sym &= 0xFFFF;
689 if (is_graphic_console()) {
690 int keycode;
692 keycode = keysym2scancode(vs->kbd_layout, sym);
693 if (keycode & 0x80)
694 kbd_put_keycode(0xe0);
695 if (down)
696 kbd_put_keycode(keycode & 0x7f);
697 else
698 kbd_put_keycode(keycode | 0x80);
699 } else if (down) {
700 int qemu_keysym = 0;
702 if (sym <= 128) /* normal ascii */
703 qemu_keysym = sym;
704 else {
705 switch (sym) {
706 case XK_Up: qemu_keysym = QEMU_KEY_UP; break;
707 case XK_Down: qemu_keysym = QEMU_KEY_DOWN; break;
708 case XK_Left: qemu_keysym = QEMU_KEY_LEFT; break;
709 case XK_Right: qemu_keysym = QEMU_KEY_RIGHT; break;
710 case XK_Home: qemu_keysym = QEMU_KEY_HOME; break;
711 case XK_End: qemu_keysym = QEMU_KEY_END; break;
712 case XK_Page_Up: qemu_keysym = QEMU_KEY_PAGEUP; break;
713 case XK_Page_Down: qemu_keysym = QEMU_KEY_PAGEDOWN; break;
714 case XK_BackSpace: qemu_keysym = QEMU_KEY_BACKSPACE; break;
715 case XK_Delete: qemu_keysym = QEMU_KEY_DELETE; break;
716 case XK_Return:
717 case XK_Linefeed: qemu_keysym = sym; break;
718 default: break;
719 }
720 }
721 if (qemu_keysym != 0)
722 kbd_put_keysym(qemu_keysym);
723 }
725 if (down) {
726 switch (sym) {
727 case XK_Control_L:
728 vs->ctl_keys |= 1;
729 break;
731 case XK_Alt_L:
732 vs->ctl_keys |= 2;
733 break;
735 default:
736 break;
737 }
738 } else {
739 switch (sym) {
740 case XK_Control_L:
741 vs->ctl_keys &= ~1;
742 break;
744 case XK_Alt_L:
745 vs->ctl_keys &= ~2;
746 break;
748 case XK_1 ... XK_9:
749 if ((vs->ctl_keys & 3) != 3)
750 break;
752 console_select(sym - XK_1);
753 if (is_graphic_console()) {
754 /* tell the vga console to redisplay itself */
755 vga_hw_invalidate();
756 vnc_dpy_update(vs->ds, 0, 0, vs->ds->width, vs->ds->height);
757 }
758 break;
759 }
760 }
761 }
763 static void key_event(VncState *vs, int down, uint32_t sym)
764 {
765 if (sym >= 'A' && sym <= 'Z')
766 sym = sym - 'A' + 'a';
767 do_key_event(vs, down, sym);
768 }
770 static void framebuffer_set_updated(VncState *vs, int x, int y, int w, int h)
771 {
773 set_bits_in_row(vs, vs->update_row, x, y, w, h);
775 vs->has_update = 1;
776 }
778 static void framebuffer_update_request(VncState *vs, int incremental,
779 int x_position, int y_position,
780 int w, int h)
781 {
782 vs->need_update = 1;
783 if (!incremental)
784 framebuffer_set_updated(vs, x_position, y_position, w, h);
785 vs->visible_x = x_position;
786 vs->visible_y = y_position;
787 vs->visible_w = w;
788 vs->visible_h = h;
789 }
791 static void set_encodings(VncState *vs, int32_t *encodings, size_t n_encodings)
792 {
793 int i;
795 vs->has_hextile = 0;
796 vs->has_resize = 0;
797 vs->ds->dpy_copy = NULL;
799 for (i = n_encodings - 1; i >= 0; i--) {
800 switch (encodings[i]) {
801 case 0: /* Raw */
802 vs->has_hextile = 0;
803 break;
804 case 1: /* CopyRect */
805 vs->ds->dpy_copy = vnc_copy;
806 break;
807 case 5: /* Hextile */
808 vs->has_hextile = 1;
809 break;
810 case -223: /* DesktopResize */
811 vs->has_resize = 1;
812 break;
813 default:
814 break;
815 }
816 }
817 }
819 static void set_pixel_format(VncState *vs,
820 int bits_per_pixel, int depth,
821 int big_endian_flag, int true_color_flag,
822 int red_max, int green_max, int blue_max,
823 int red_shift, int green_shift, int blue_shift)
824 {
825 switch (bits_per_pixel) {
826 case 32:
827 case 24:
828 vs->depth = 4;
829 break;
830 case 16:
831 vs->depth = 2;
832 break;
833 case 8:
834 vs->depth = 1;
835 break;
836 default:
837 vnc_client_error(vs);
838 break;
839 }
841 if (!true_color_flag)
842 vnc_client_error(vs);
844 vnc_dpy_resize(vs->ds, vs->ds->width, vs->ds->height);
846 vga_hw_invalidate();
847 vga_hw_update();
848 }
850 static int protocol_client_msg(VncState *vs, char *data, size_t len)
851 {
852 int i;
853 uint16_t limit;
855 switch (data[0]) {
856 case 0:
857 if (len == 1)
858 return 20;
860 set_pixel_format(vs, read_u8(data, 4), read_u8(data, 5),
861 read_u8(data, 6), read_u8(data, 7),
862 read_u16(data, 8), read_u16(data, 10),
863 read_u16(data, 12), read_u8(data, 14),
864 read_u8(data, 15), read_u8(data, 16));
865 break;
866 case 2:
867 if (len == 1)
868 return 4;
870 if (len == 4)
871 return 4 + (read_u16(data, 2) * 4);
873 limit = read_u16(data, 2);
874 for (i = 0; i < limit; i++) {
875 int32_t val = read_s32(data, 4 + (i * 4));
876 memcpy(data + 4 + (i * 4), &val, sizeof(val));
877 }
879 set_encodings(vs, (int32_t *)(data + 4), limit);
880 break;
881 case 3:
882 if (len == 1)
883 return 10;
885 framebuffer_update_request(vs,
886 read_u8(data, 1), read_u16(data, 2), read_u16(data, 4),
887 read_u16(data, 6), read_u16(data, 8));
888 break;
889 case 4:
890 if (len == 1)
891 return 8;
893 key_event(vs, read_u8(data, 1), read_u32(data, 4));
894 break;
895 case 5:
896 if (len == 1)
897 return 6;
899 pointer_event(vs, read_u8(data, 1), read_u16(data, 2), read_u16(data, 4));
900 break;
901 case 6:
902 if (len == 1)
903 return 8;
905 if (len == 8)
906 return 8 + read_u32(data, 4);
908 client_cut_text(vs, read_u32(data, 4), data + 8);
909 break;
910 default:
911 printf("Msg: %d\n", data[0]);
912 vnc_client_error(vs);
913 break;
914 }
916 vnc_read_when(vs, protocol_client_msg, 1);
917 return 0;
918 }
920 static int protocol_client_init(VncState *vs, char *data, size_t len)
921 {
922 size_t l;
923 char pad[3] = { 0, 0, 0 };
925 vs->width = vs->ds->width;
926 vs->height = vs->ds->height;
927 vnc_write_u16(vs, vs->ds->width);
928 vnc_write_u16(vs, vs->ds->height);
930 vnc_write_u8(vs, vs->depth * 8); /* bits-per-pixel */
931 vnc_write_u8(vs, vs->depth * 8); /* depth */
932 vnc_write_u8(vs, 0); /* big-endian-flag */
933 vnc_write_u8(vs, 1); /* true-color-flag */
934 if (vs->depth == 4) {
935 vnc_write_u16(vs, 0xFF); /* red-max */
936 vnc_write_u16(vs, 0xFF); /* green-max */
937 vnc_write_u16(vs, 0xFF); /* blue-max */
938 vnc_write_u8(vs, 16); /* red-shift */
939 vnc_write_u8(vs, 8); /* green-shift */
940 vnc_write_u8(vs, 0); /* blue-shift */
941 } else if (vs->depth == 2) {
942 vnc_write_u16(vs, 31); /* red-max */
943 vnc_write_u16(vs, 63); /* green-max */
944 vnc_write_u16(vs, 31); /* blue-max */
945 vnc_write_u8(vs, 11); /* red-shift */
946 vnc_write_u8(vs, 5); /* green-shift */
947 vnc_write_u8(vs, 0); /* blue-shift */
948 } else if (vs->depth == 1) {
949 vnc_write_u16(vs, 3); /* red-max */
950 vnc_write_u16(vs, 7); /* green-max */
951 vnc_write_u16(vs, 3); /* blue-max */
952 vnc_write_u8(vs, 5); /* red-shift */
953 vnc_write_u8(vs, 2); /* green-shift */
954 vnc_write_u8(vs, 0); /* blue-shift */
955 }
957 vnc_write(vs, pad, 3); /* padding */
959 l = strlen(domain_name);
960 vnc_write_u32(vs, l);
961 vnc_write(vs, domain_name, l);
963 vnc_flush(vs);
965 vnc_read_when(vs, protocol_client_msg, 1);
967 return 0;
968 }
970 static int protocol_version(VncState *vs, char *version, size_t len)
971 {
972 char local[13];
973 int maj, min;
975 memcpy(local, version, 12);
976 local[12] = 0;
978 if (sscanf(local, "RFB %03d.%03d\n", &maj, &min) != 2) {
979 vnc_client_error(vs);
980 return 0;
981 }
983 vnc_write_u32(vs, 1); /* None */
984 vnc_flush(vs);
986 vnc_read_when(vs, protocol_client_init, 1);
988 return 0;
989 }
991 static void vnc_listen_read(void *opaque)
992 {
993 VncState *vs = opaque;
994 struct sockaddr_in addr;
995 socklen_t addrlen = sizeof(addr);
997 vs->csock = accept(vs->lsock, (struct sockaddr *)&addr, &addrlen);
998 if (vs->csock != -1) {
999 socket_set_nonblock(vs->csock);
1000 qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, opaque);
1001 vnc_write(vs, "RFB 003.003\n", 12);
1002 vnc_flush(vs);
1003 vnc_read_when(vs, protocol_version, 12);
1004 framebuffer_set_updated(vs, 0, 0, vs->ds->width, vs->ds->height);
1005 vs->has_resize = 0;
1006 vs->has_hextile = 0;
1007 vs->ds->dpy_copy = NULL;
1008 vnc_timer_init(vs);
1012 void vnc_display_init(DisplayState *ds, int display)
1014 struct sockaddr_in addr;
1015 int reuse_addr, ret;
1016 VncState *vs;
1018 vs = qemu_mallocz(sizeof(VncState));
1019 if (!vs)
1020 exit(1);
1022 ds->opaque = vs;
1024 vs->lsock = -1;
1025 vs->csock = -1;
1026 vs->depth = 4;
1028 vs->ds = ds;
1030 if (!keyboard_layout)
1031 keyboard_layout = "en-us";
1033 vs->kbd_layout = init_keyboard_layout(keyboard_layout);
1034 if (!vs->kbd_layout)
1035 exit(1);
1037 vs->lsock = socket(PF_INET, SOCK_STREAM, 0);
1038 if (vs->lsock == -1) {
1039 fprintf(stderr, "Could not create socket\n");
1040 exit(1);
1043 addr.sin_family = AF_INET;
1044 addr.sin_port = htons(5900 + display);
1045 memset(&addr.sin_addr, 0, sizeof(addr.sin_addr));
1047 reuse_addr = 1;
1048 ret = setsockopt(vs->lsock, SOL_SOCKET, SO_REUSEADDR,
1049 (const char *)&reuse_addr, sizeof(reuse_addr));
1050 if (ret == -1) {
1051 fprintf(stderr, "setsockopt() failed\n");
1052 exit(1);
1055 if (bind(vs->lsock, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
1056 fprintf(stderr, "bind() failed\n");
1057 exit(1);
1060 if (listen(vs->lsock, 1) == -1) {
1061 fprintf(stderr, "listen() failed\n");
1062 exit(1);
1065 ret = qemu_set_fd_handler2(vs->lsock, vnc_listen_poll, vnc_listen_read,
1066 NULL, vs);
1067 if (ret == -1)
1068 exit(1);
1070 vs->ds->data = NULL;
1071 vs->ds->dpy_update = vnc_dpy_update;
1072 vs->ds->dpy_resize = vnc_dpy_resize;
1073 vs->ds->dpy_refresh = vnc_dpy_refresh;
1075 vnc_dpy_resize(vs->ds, 640, 400);
1078 int vnc_start_viewer(int port)
1080 int pid;
1081 char s[16];
1083 sprintf(s, ":%d", port);
1085 switch (pid = fork()) {
1086 case -1:
1087 fprintf(stderr, "vncviewer failed fork\n");
1088 exit(1);
1090 case 0: /* child */
1091 execlp("vncviewer", "vncviewer", s, 0);
1092 fprintf(stderr, "vncviewer execlp failed\n");
1093 exit(1);
1095 default:
1096 return pid;