2 * QEMU VNC display driver
4 * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
5 * Copyright (C) 2006 Fabrice Bellard
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 #include "qemu_socket.h"
29 #define VNC_REFRESH_INTERVAL (1000 / 30)
31 #include "vnc_keysym.h"
41 typedef struct VncState VncState
;
43 typedef int VncReadEvent(VncState
*vs
, char *data
, size_t len
);
45 typedef void VncWritePixels(VncState
*vs
, void *data
, int size
);
47 typedef void VncSendHextileTile(VncState
*vs
,
48 int x
, int y
, int w
, int h
,
51 int *has_bg
, int *has_fg
);
62 uint64_t dirty_row
[768];
64 int depth
; /* internal VNC frame buffer byte per pixel */
69 kbd_layout_t
*kbd_layout
;
70 /* current output mode information */
71 VncWritePixels
*write_pixels
;
72 VncSendHextileTile
*send_hextile_tile
;
73 int pix_bpp
, pix_big_endian
;
74 int red_shift
, red_max
, red_shift1
;
75 int green_shift
, green_max
, green_shift1
;
76 int blue_shift
, blue_max
, blue_shift1
;
78 VncReadEvent
*read_handler
;
79 size_t read_handler_expect
;
83 1) Get the queue working for IO.
84 2) there is some weirdness when using the -S option (the screen is grey
85 and not totally invalidated
89 static void vnc_write(VncState
*vs
, const void *data
, size_t len
);
90 static void vnc_write_u32(VncState
*vs
, uint32_t value
);
91 static void vnc_write_s32(VncState
*vs
, int32_t value
);
92 static void vnc_write_u16(VncState
*vs
, uint16_t value
);
93 static void vnc_write_u8(VncState
*vs
, uint8_t value
);
94 static void vnc_flush(VncState
*vs
);
95 static void vnc_update_client(void *opaque
);
96 static void vnc_client_read(void *opaque
);
98 static void vnc_dpy_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
100 VncState
*vs
= ds
->opaque
;
106 for (i
= 0; i
< w
; i
+= 16)
107 vs
->dirty_row
[y
] |= (1ULL << ((x
+ i
) / 16));
110 static void vnc_framebuffer_update(VncState
*vs
, int x
, int y
, int w
, int h
,
113 vnc_write_u16(vs
, x
);
114 vnc_write_u16(vs
, y
);
115 vnc_write_u16(vs
, w
);
116 vnc_write_u16(vs
, h
);
118 vnc_write_s32(vs
, encoding
);
121 static void vnc_dpy_resize(DisplayState
*ds
, int w
, int h
)
123 VncState
*vs
= ds
->opaque
;
125 ds
->data
= realloc(ds
->data
, w
* h
* vs
->depth
);
126 vs
->old_data
= realloc(vs
->old_data
, w
* h
* vs
->depth
);
128 if (ds
->data
== NULL
|| vs
->old_data
== NULL
) {
129 fprintf(stderr
, "vnc: memory allocation failed\n");
133 ds
->depth
= vs
->depth
* 8;
136 ds
->linesize
= w
* vs
->depth
;
137 if (vs
->csock
!= -1 && vs
->has_resize
) {
138 vnc_write_u8(vs
, 0); /* msg id */
140 vnc_write_u16(vs
, 1); /* number of rects */
141 vnc_framebuffer_update(vs
, 0, 0, ds
->width
, ds
->height
, -223);
143 vs
->width
= ds
->width
;
144 vs
->height
= ds
->height
;
149 static void vnc_write_pixels_copy(VncState
*vs
, void *pixels
, int size
)
151 vnc_write(vs
, pixels
, size
);
154 /* slowest but generic code. */
155 static void vnc_convert_pixel(VncState
*vs
, uint8_t *buf
, uint32_t v
)
157 unsigned int r
, g
, b
;
159 r
= (v
>> vs
->red_shift1
) & vs
->red_max
;
160 g
= (v
>> vs
->green_shift1
) & vs
->green_max
;
161 b
= (v
>> vs
->blue_shift1
) & vs
->blue_max
;
162 v
= (r
<< vs
->red_shift
) |
163 (g
<< vs
->green_shift
) |
164 (b
<< vs
->blue_shift
);
165 switch(vs
->pix_bpp
) {
170 if (vs
->pix_big_endian
) {
180 if (vs
->pix_big_endian
) {
195 static void vnc_write_pixels_generic(VncState
*vs
, void *pixels1
, int size
)
197 uint32_t *pixels
= pixels1
;
202 for(i
= 0; i
< n
; i
++) {
203 vnc_convert_pixel(vs
, buf
, pixels
[i
]);
204 vnc_write(vs
, buf
, vs
->pix_bpp
);
208 static void send_framebuffer_update_raw(VncState
*vs
, int x
, int y
, int w
, int h
)
213 vnc_framebuffer_update(vs
, x
, y
, w
, h
, 0);
215 row
= vs
->ds
->data
+ y
* vs
->ds
->linesize
+ x
* vs
->depth
;
216 for (i
= 0; i
< h
; i
++) {
217 vs
->write_pixels(vs
, row
, w
* vs
->depth
);
218 row
+= vs
->ds
->linesize
;
222 static void hextile_enc_cord(uint8_t *ptr
, int x
, int y
, int w
, int h
)
224 ptr
[0] = ((x
& 0x0F) << 4) | (y
& 0x0F);
225 ptr
[1] = (((w
- 1) & 0x0F) << 4) | ((h
- 1) & 0x0F);
229 #include "vnchextile.h"
233 #include "vnchextile.h"
237 #include "vnchextile.h"
242 #include "vnchextile.h"
246 static void send_framebuffer_update_hextile(VncState
*vs
, int x
, int y
, int w
, int h
)
250 uint32_t last_fg32
, last_bg32
;
252 vnc_framebuffer_update(vs
, x
, y
, w
, h
, 5);
255 for (j
= y
; j
< (y
+ h
); j
+= 16) {
256 for (i
= x
; i
< (x
+ w
); i
+= 16) {
257 vs
->send_hextile_tile(vs
, i
, j
,
258 MIN(16, x
+ w
- i
), MIN(16, y
+ h
- j
),
259 &last_bg32
, &last_fg32
, &has_bg
, &has_fg
);
264 static void send_framebuffer_update(VncState
*vs
, int x
, int y
, int w
, int h
)
267 send_framebuffer_update_hextile(vs
, x
, y
, w
, h
);
269 send_framebuffer_update_raw(vs
, x
, y
, w
, h
);
272 static void vnc_copy(DisplayState
*ds
, int src_x
, int src_y
, int dst_x
, int dst_y
, int w
, int h
)
279 int pitch
= ds
->linesize
;
280 VncState
*vs
= ds
->opaque
;
282 vnc_update_client(vs
);
289 src
= (ds
->linesize
* (src_y
+ y
) + vs
->depth
* src_x
);
290 dst
= (ds
->linesize
* (dst_y
+ y
) + vs
->depth
* dst_x
);
292 src_row
= ds
->data
+ src
;
293 dst_row
= ds
->data
+ dst
;
294 old_row
= vs
->old_data
+ dst
;
296 for (y
= 0; y
< h
; y
++) {
297 memmove(old_row
, src_row
, w
* vs
->depth
);
298 memmove(dst_row
, src_row
, w
* vs
->depth
);
304 vnc_write_u8(vs
, 0); /* msg id */
306 vnc_write_u16(vs
, 1); /* number of rects */
307 vnc_framebuffer_update(vs
, dst_x
, dst_y
, w
, h
, 1);
308 vnc_write_u16(vs
, src_x
);
309 vnc_write_u16(vs
, src_y
);
313 static int find_dirty_height(VncState
*vs
, int y
, int last_x
, int x
)
317 for (h
= 1; h
< (vs
->height
- y
); h
++) {
319 if (!(vs
->dirty_row
[y
+ h
] & (1ULL << last_x
)))
321 for (tmp_x
= last_x
; tmp_x
< x
; tmp_x
++)
322 vs
->dirty_row
[y
+ h
] &= ~(1ULL << tmp_x
);
328 static void vnc_update_client(void *opaque
)
330 VncState
*vs
= opaque
;
332 if (vs
->need_update
&& vs
->csock
!= -1) {
341 width_mask
= (1ULL << (vs
->width
/ 16)) - 1;
343 if (vs
->width
== 1024)
344 width_mask
= ~(0ULL);
346 /* Walk through the dirty map and eliminate tiles that
347 really aren't dirty */
349 old_row
= vs
->old_data
;
351 for (y
= 0; y
< vs
->height
; y
++) {
352 if (vs
->dirty_row
[y
] & width_mask
) {
359 for (x
= 0; x
< vs
->ds
->width
; x
+= 16) {
360 if (memcmp(old_ptr
, ptr
, 16 * vs
->depth
) == 0) {
361 vs
->dirty_row
[y
] &= ~(1ULL << (x
/ 16));
364 memcpy(old_ptr
, ptr
, 16 * vs
->depth
);
367 ptr
+= 16 * vs
->depth
;
368 old_ptr
+= 16 * vs
->depth
;
372 row
+= vs
->ds
->linesize
;
373 old_row
+= vs
->ds
->linesize
;
377 qemu_mod_timer(vs
->timer
, qemu_get_clock(rt_clock
) + VNC_REFRESH_INTERVAL
);
381 /* Count rectangles */
383 vnc_write_u8(vs
, 0); /* msg id */
385 saved_offset
= vs
->output
.offset
;
386 vnc_write_u16(vs
, 0);
388 for (y
= 0; y
< vs
->height
; y
++) {
391 for (x
= 0; x
< vs
->width
/ 16; x
++) {
392 if (vs
->dirty_row
[y
] & (1ULL << x
)) {
396 vs
->dirty_row
[y
] &= ~(1ULL << x
);
399 int h
= find_dirty_height(vs
, y
, last_x
, x
);
400 send_framebuffer_update(vs
, last_x
* 16, y
, (x
- last_x
) * 16, h
);
407 int h
= find_dirty_height(vs
, y
, last_x
, x
);
408 send_framebuffer_update(vs
, last_x
* 16, y
, (x
- last_x
) * 16, h
);
412 vs
->output
.buffer
[saved_offset
] = (n_rectangles
>> 8) & 0xFF;
413 vs
->output
.buffer
[saved_offset
+ 1] = n_rectangles
& 0xFF;
417 qemu_mod_timer(vs
->timer
, qemu_get_clock(rt_clock
) + VNC_REFRESH_INTERVAL
);
420 static void vnc_timer_init(VncState
*vs
)
422 if (vs
->timer
== NULL
) {
423 vs
->timer
= qemu_new_timer(rt_clock
, vnc_update_client
, vs
);
424 qemu_mod_timer(vs
->timer
, qemu_get_clock(rt_clock
));
428 static void vnc_dpy_refresh(DisplayState
*ds
)
430 VncState
*vs
= ds
->opaque
;
435 static int vnc_listen_poll(void *opaque
)
437 VncState
*vs
= opaque
;
443 static void buffer_reserve(Buffer
*buffer
, size_t len
)
445 if ((buffer
->capacity
- buffer
->offset
) < len
) {
446 buffer
->capacity
+= (len
+ 1024);
447 buffer
->buffer
= realloc(buffer
->buffer
, buffer
->capacity
);
448 if (buffer
->buffer
== NULL
) {
449 fprintf(stderr
, "vnc: out of memory\n");
455 static int buffer_empty(Buffer
*buffer
)
457 return buffer
->offset
== 0;
460 static char *buffer_end(Buffer
*buffer
)
462 return buffer
->buffer
+ buffer
->offset
;
465 static void buffer_reset(Buffer
*buffer
)
470 static void buffer_append(Buffer
*buffer
, const void *data
, size_t len
)
472 memcpy(buffer
->buffer
+ buffer
->offset
, data
, len
);
473 buffer
->offset
+= len
;
476 static int vnc_client_io_error(VncState
*vs
, int ret
, int last_errno
)
478 if (ret
== 0 || ret
== -1) {
479 if (ret
== -1 && (last_errno
== EINTR
|| last_errno
== EAGAIN
))
482 qemu_set_fd_handler2(vs
->csock
, NULL
, NULL
, NULL
, NULL
);
483 closesocket(vs
->csock
);
485 buffer_reset(&vs
->input
);
486 buffer_reset(&vs
->output
);
493 static void vnc_client_error(VncState
*vs
)
495 vnc_client_io_error(vs
, -1, EINVAL
);
498 static void vnc_client_write(void *opaque
)
501 VncState
*vs
= opaque
;
503 ret
= send(vs
->csock
, vs
->output
.buffer
, vs
->output
.offset
, 0);
504 ret
= vnc_client_io_error(vs
, ret
, socket_error());
508 memmove(vs
->output
.buffer
, vs
->output
.buffer
+ ret
, (vs
->output
.offset
- ret
));
509 vs
->output
.offset
-= ret
;
511 if (vs
->output
.offset
== 0) {
512 qemu_set_fd_handler2(vs
->csock
, NULL
, vnc_client_read
, NULL
, vs
);
516 static void vnc_read_when(VncState
*vs
, VncReadEvent
*func
, size_t expecting
)
518 vs
->read_handler
= func
;
519 vs
->read_handler_expect
= expecting
;
522 static void vnc_client_read(void *opaque
)
524 VncState
*vs
= opaque
;
527 buffer_reserve(&vs
->input
, 4096);
529 ret
= recv(vs
->csock
, buffer_end(&vs
->input
), 4096, 0);
530 ret
= vnc_client_io_error(vs
, ret
, socket_error());
534 vs
->input
.offset
+= ret
;
536 while (vs
->read_handler
&& vs
->input
.offset
>= vs
->read_handler_expect
) {
537 size_t len
= vs
->read_handler_expect
;
540 ret
= vs
->read_handler(vs
, vs
->input
.buffer
, len
);
545 memmove(vs
->input
.buffer
, vs
->input
.buffer
+ len
, (vs
->input
.offset
- len
));
546 vs
->input
.offset
-= len
;
548 vs
->read_handler_expect
= ret
;
553 static void vnc_write(VncState
*vs
, const void *data
, size_t len
)
555 buffer_reserve(&vs
->output
, len
);
557 if (buffer_empty(&vs
->output
)) {
558 qemu_set_fd_handler2(vs
->csock
, NULL
, vnc_client_read
, vnc_client_write
, vs
);
561 buffer_append(&vs
->output
, data
, len
);
564 static void vnc_write_s32(VncState
*vs
, int32_t value
)
566 vnc_write_u32(vs
, *(uint32_t *)&value
);
569 static void vnc_write_u32(VncState
*vs
, uint32_t value
)
573 buf
[0] = (value
>> 24) & 0xFF;
574 buf
[1] = (value
>> 16) & 0xFF;
575 buf
[2] = (value
>> 8) & 0xFF;
576 buf
[3] = value
& 0xFF;
578 vnc_write(vs
, buf
, 4);
581 static void vnc_write_u16(VncState
*vs
, uint16_t value
)
585 buf
[0] = (value
>> 8) & 0xFF;
586 buf
[1] = value
& 0xFF;
588 vnc_write(vs
, buf
, 2);
591 static void vnc_write_u8(VncState
*vs
, uint8_t value
)
593 vnc_write(vs
, (char *)&value
, 1);
596 static void vnc_flush(VncState
*vs
)
598 if (vs
->output
.offset
)
599 vnc_client_write(vs
);
602 static uint8_t read_u8(char *data
, size_t offset
)
607 static uint16_t read_u16(char *data
, size_t offset
)
609 return ((data
[offset
] & 0xFF) << 8) | (data
[offset
+ 1] & 0xFF);
612 static int32_t read_s32(char *data
, size_t offset
)
614 return (int32_t)((data
[offset
] << 24) | (data
[offset
+ 1] << 16) |
615 (data
[offset
+ 2] << 8) | data
[offset
+ 3]);
618 static uint32_t read_u32(char *data
, size_t offset
)
620 return ((data
[offset
] << 24) | (data
[offset
+ 1] << 16) |
621 (data
[offset
+ 2] << 8) | data
[offset
+ 3]);
624 static void client_cut_text(VncState
*vs
, size_t len
, char *text
)
628 static void pointer_event(VncState
*vs
, int button_mask
, int x
, int y
)
633 if (button_mask
& 0x01)
634 buttons
|= MOUSE_EVENT_LBUTTON
;
635 if (button_mask
& 0x02)
636 buttons
|= MOUSE_EVENT_MBUTTON
;
637 if (button_mask
& 0x04)
638 buttons
|= MOUSE_EVENT_RBUTTON
;
639 if (button_mask
& 0x08)
641 if (button_mask
& 0x10)
644 if (kbd_mouse_is_absolute()) {
645 kbd_mouse_event(x
* 0x7FFF / vs
->ds
->width
,
646 y
* 0x7FFF / vs
->ds
->height
,
649 static int last_x
= -1;
650 static int last_y
= -1;
653 kbd_mouse_event(x
- last_x
, y
- last_y
, dz
, buttons
);
660 static void do_key_event(VncState
*vs
, int down
, uint32_t sym
)
664 keycode
= keysym2scancode(vs
->kbd_layout
, sym
& 0xFFFF);
667 kbd_put_keycode(0xe0);
669 kbd_put_keycode(keycode
& 0x7f);
671 kbd_put_keycode(keycode
| 0x80);
674 static void key_event(VncState
*vs
, int down
, uint32_t sym
)
676 if (sym
>= 'A' && sym
<= 'Z')
677 sym
= sym
- 'A' + 'a';
678 do_key_event(vs
, down
, sym
);
681 static void framebuffer_update_request(VncState
*vs
, int incremental
,
682 int x_position
, int y_position
,
688 char *old_row
= vs
->old_data
+ y_position
* vs
->ds
->linesize
;
690 for (i
= 0; i
< h
; i
++) {
691 vs
->dirty_row
[y_position
+ i
] = (1ULL << (vs
->ds
->width
/ 16)) - 1;
692 if (vs
->ds
->width
== 1024) {
693 vs
->dirty_row
[y_position
+ i
] = ~(0ULL);
695 memset(old_row
, 42, vs
->ds
->width
* vs
->depth
);
696 old_row
+= vs
->ds
->linesize
;
701 static void set_encodings(VncState
*vs
, int32_t *encodings
, size_t n_encodings
)
707 vs
->ds
->dpy_copy
= NULL
;
709 for (i
= n_encodings
- 1; i
>= 0; i
--) {
710 switch (encodings
[i
]) {
714 case 1: /* CopyRect */
715 vs
->ds
->dpy_copy
= vnc_copy
;
717 case 5: /* Hextile */
720 case -223: /* DesktopResize */
729 static int compute_nbits(unsigned int val
)
740 static void set_pixel_format(VncState
*vs
,
741 int bits_per_pixel
, int depth
,
742 int big_endian_flag
, int true_color_flag
,
743 int red_max
, int green_max
, int blue_max
,
744 int red_shift
, int green_shift
, int blue_shift
)
746 int host_big_endian_flag
;
748 #ifdef WORDS_BIGENDIAN
749 host_big_endian_flag
= 1;
751 host_big_endian_flag
= 0;
753 if (!true_color_flag
) {
755 vnc_client_error(vs
);
758 if (bits_per_pixel
== 32 &&
759 host_big_endian_flag
== big_endian_flag
&&
760 red_max
== 0xff && green_max
== 0xff && blue_max
== 0xff &&
761 red_shift
== 16 && green_shift
== 8 && blue_shift
== 0) {
763 vs
->write_pixels
= vnc_write_pixels_copy
;
764 vs
->send_hextile_tile
= send_hextile_tile_32
;
766 if (bits_per_pixel
== 16 &&
767 host_big_endian_flag
== big_endian_flag
&&
768 red_max
== 31 && green_max
== 63 && blue_max
== 31 &&
769 red_shift
== 11 && green_shift
== 5 && blue_shift
== 0) {
771 vs
->write_pixels
= vnc_write_pixels_copy
;
772 vs
->send_hextile_tile
= send_hextile_tile_16
;
774 if (bits_per_pixel
== 8 &&
775 red_max
== 7 && green_max
== 7 && blue_max
== 3 &&
776 red_shift
== 5 && green_shift
== 2 && blue_shift
== 0) {
778 vs
->write_pixels
= vnc_write_pixels_copy
;
779 vs
->send_hextile_tile
= send_hextile_tile_8
;
782 /* generic and slower case */
783 if (bits_per_pixel
!= 8 &&
784 bits_per_pixel
!= 16 &&
785 bits_per_pixel
!= 32)
788 vs
->red_shift
= red_shift
;
789 vs
->red_max
= red_max
;
790 vs
->red_shift1
= 24 - compute_nbits(red_max
);
791 vs
->green_shift
= green_shift
;
792 vs
->green_max
= green_max
;
793 vs
->green_shift1
= 16 - compute_nbits(green_max
);
794 vs
->blue_shift
= blue_shift
;
795 vs
->blue_max
= blue_max
;
796 vs
->blue_shift1
= 8 - compute_nbits(blue_max
);
797 vs
->pix_bpp
= bits_per_pixel
/ 8;
798 vs
->pix_big_endian
= big_endian_flag
;
799 vs
->write_pixels
= vnc_write_pixels_generic
;
800 vs
->send_hextile_tile
= send_hextile_tile_generic
;
803 vnc_dpy_resize(vs
->ds
, vs
->ds
->width
, vs
->ds
->height
);
804 memset(vs
->dirty_row
, 0xFF, sizeof(vs
->dirty_row
));
805 memset(vs
->old_data
, 42, vs
->ds
->linesize
* vs
->ds
->height
);
811 static int protocol_client_msg(VncState
*vs
, char *data
, size_t len
)
821 set_pixel_format(vs
, read_u8(data
, 4), read_u8(data
, 5),
822 read_u8(data
, 6), read_u8(data
, 7),
823 read_u16(data
, 8), read_u16(data
, 10),
824 read_u16(data
, 12), read_u8(data
, 14),
825 read_u8(data
, 15), read_u8(data
, 16));
832 return 4 + (read_u16(data
, 2) * 4);
834 limit
= read_u16(data
, 2);
835 for (i
= 0; i
< limit
; i
++) {
836 int32_t val
= read_s32(data
, 4 + (i
* 4));
837 memcpy(data
+ 4 + (i
* 4), &val
, sizeof(val
));
840 set_encodings(vs
, (int32_t *)(data
+ 4), limit
);
846 framebuffer_update_request(vs
,
847 read_u8(data
, 1), read_u16(data
, 2), read_u16(data
, 4),
848 read_u16(data
, 6), read_u16(data
, 8));
854 key_event(vs
, read_u8(data
, 1), read_u32(data
, 4));
860 pointer_event(vs
, read_u8(data
, 1), read_u16(data
, 2), read_u16(data
, 4));
867 return 8 + read_u32(data
, 4);
869 client_cut_text(vs
, read_u32(data
, 4), data
+ 8);
872 printf("Msg: %d\n", data
[0]);
873 vnc_client_error(vs
);
877 vnc_read_when(vs
, protocol_client_msg
, 1);
881 static int protocol_client_init(VncState
*vs
, char *data
, size_t len
)
883 char pad
[3] = { 0, 0, 0 };
885 vs
->width
= vs
->ds
->width
;
886 vs
->height
= vs
->ds
->height
;
887 vnc_write_u16(vs
, vs
->ds
->width
);
888 vnc_write_u16(vs
, vs
->ds
->height
);
890 vnc_write_u8(vs
, vs
->depth
* 8); /* bits-per-pixel */
891 vnc_write_u8(vs
, vs
->depth
* 8); /* depth */
892 #ifdef WORDS_BIGENDIAN
893 vnc_write_u8(vs
, 1); /* big-endian-flag */
895 vnc_write_u8(vs
, 0); /* big-endian-flag */
897 vnc_write_u8(vs
, 1); /* true-color-flag */
898 if (vs
->depth
== 4) {
899 vnc_write_u16(vs
, 0xFF); /* red-max */
900 vnc_write_u16(vs
, 0xFF); /* green-max */
901 vnc_write_u16(vs
, 0xFF); /* blue-max */
902 vnc_write_u8(vs
, 16); /* red-shift */
903 vnc_write_u8(vs
, 8); /* green-shift */
904 vnc_write_u8(vs
, 0); /* blue-shift */
905 vs
->send_hextile_tile
= send_hextile_tile_32
;
906 } else if (vs
->depth
== 2) {
907 vnc_write_u16(vs
, 31); /* red-max */
908 vnc_write_u16(vs
, 63); /* green-max */
909 vnc_write_u16(vs
, 31); /* blue-max */
910 vnc_write_u8(vs
, 11); /* red-shift */
911 vnc_write_u8(vs
, 5); /* green-shift */
912 vnc_write_u8(vs
, 0); /* blue-shift */
913 vs
->send_hextile_tile
= send_hextile_tile_16
;
914 } else if (vs
->depth
== 1) {
915 /* XXX: change QEMU pixel 8 bit pixel format to match the VNC one ? */
916 vnc_write_u16(vs
, 7); /* red-max */
917 vnc_write_u16(vs
, 7); /* green-max */
918 vnc_write_u16(vs
, 3); /* blue-max */
919 vnc_write_u8(vs
, 5); /* red-shift */
920 vnc_write_u8(vs
, 2); /* green-shift */
921 vnc_write_u8(vs
, 0); /* blue-shift */
922 vs
->send_hextile_tile
= send_hextile_tile_8
;
924 vs
->write_pixels
= vnc_write_pixels_copy
;
926 vnc_write(vs
, pad
, 3); /* padding */
928 vnc_write_u32(vs
, 4);
929 vnc_write(vs
, "QEMU", 4);
932 vnc_read_when(vs
, protocol_client_msg
, 1);
937 static int protocol_version(VncState
*vs
, char *version
, size_t len
)
942 memcpy(local
, version
, 12);
945 if (sscanf(local
, "RFB %03d.%03d\n", &maj
, &min
) != 2) {
946 vnc_client_error(vs
);
950 vnc_write_u32(vs
, 1); /* None */
953 vnc_read_when(vs
, protocol_client_init
, 1);
958 static void vnc_listen_read(void *opaque
)
960 VncState
*vs
= opaque
;
961 struct sockaddr_in addr
;
962 socklen_t addrlen
= sizeof(addr
);
964 vs
->csock
= accept(vs
->lsock
, (struct sockaddr
*)&addr
, &addrlen
);
965 if (vs
->csock
!= -1) {
966 socket_set_nonblock(vs
->csock
);
967 qemu_set_fd_handler2(vs
->csock
, NULL
, vnc_client_read
, NULL
, opaque
);
968 vnc_write(vs
, "RFB 003.003\n", 12);
970 vnc_read_when(vs
, protocol_version
, 12);
971 memset(vs
->old_data
, 0, vs
->ds
->linesize
* vs
->ds
->height
);
972 memset(vs
->dirty_row
, 0xFF, sizeof(vs
->dirty_row
));
975 vs
->ds
->dpy_copy
= NULL
;
979 void vnc_display_init(DisplayState
*ds
, int display
)
981 struct sockaddr_in addr
;
985 vs
= qemu_mallocz(sizeof(VncState
));
997 if (!keyboard_layout
)
998 keyboard_layout
= "en-us";
1000 vs
->kbd_layout
= init_keyboard_layout(keyboard_layout
);
1001 if (!vs
->kbd_layout
)
1004 vs
->lsock
= socket(PF_INET
, SOCK_STREAM
, 0);
1005 if (vs
->lsock
== -1) {
1006 fprintf(stderr
, "Could not create socket\n");
1010 addr
.sin_family
= AF_INET
;
1011 addr
.sin_port
= htons(5900 + display
);
1012 memset(&addr
.sin_addr
, 0, sizeof(addr
.sin_addr
));
1015 ret
= setsockopt(vs
->lsock
, SOL_SOCKET
, SO_REUSEADDR
,
1016 (const char *)&reuse_addr
, sizeof(reuse_addr
));
1018 fprintf(stderr
, "setsockopt() failed\n");
1022 if (bind(vs
->lsock
, (struct sockaddr
*)&addr
, sizeof(addr
)) == -1) {
1023 fprintf(stderr
, "bind() failed\n");
1027 if (listen(vs
->lsock
, 1) == -1) {
1028 fprintf(stderr
, "listen() failed\n");
1032 ret
= qemu_set_fd_handler2(vs
->lsock
, vnc_listen_poll
, vnc_listen_read
, NULL
, vs
);
1037 vs
->ds
->data
= NULL
;
1038 vs
->ds
->dpy_update
= vnc_dpy_update
;
1039 vs
->ds
->dpy_resize
= vnc_dpy_resize
;
1040 vs
->ds
->dpy_refresh
= vnc_dpy_refresh
;
1042 memset(vs
->dirty_row
, 0xFF, sizeof(vs
->dirty_row
));
1044 vnc_dpy_resize(vs
->ds
, 640, 400);