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) 2009 Red Hat, Inc 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 */ 26 27 #ifndef __QEMU_VNC_H 28 #define __QEMU_VNC_H 29 30 #include "qemu-common.h" 31 #include "qemu-queue.h" 32 #ifdef CONFIG_VNC_THREAD 33 #include "qemu-thread.h" 34 #endif 35 #include "console.h" 36 #include "monitor.h" 37 #include "audio/audio.h" 38 #include "bitmap.h" 39 #include <zlib.h> 40 #include <stdbool.h> 41 42 #include "keymaps.h" 43 #include "vnc-palette.h" 44 #include "vnc-enc-zrle.h" 45 46 // #define _VNC_DEBUG 1 47 48 #ifdef _VNC_DEBUG 49 #define VNC_DEBUG(fmt, ...) do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0) 50 #else 51 #define VNC_DEBUG(fmt, ...) do { } while (0) 52 #endif 53 54 /***************************************************************************** 55 * 56 * Core data structures 57 * 58 *****************************************************************************/ 59 60 typedef struct Buffer 61 { 62 size_t capacity; 63 size_t offset; 64 uint8_t *buffer; 65 } Buffer; 66 67 typedef struct VncState VncState; 68 typedef struct VncJob VncJob; 69 typedef struct VncRect VncRect; 70 typedef struct VncRectEntry VncRectEntry; 71 72 typedef int VncReadEvent(VncState *vs, uint8_t *data, size_t len); 73 74 typedef void VncWritePixels(VncState *vs, struct PixelFormat *pf, void *data, int size); 75 76 typedef void VncSendHextileTile(VncState *vs, 77 int x, int y, int w, int h, 78 void *last_bg, 79 void *last_fg, 80 int *has_bg, int *has_fg); 81 82 #define VNC_MAX_WIDTH 2560 83 #define VNC_MAX_HEIGHT 2048 84 #define VNC_DIRTY_WORDS (VNC_MAX_WIDTH / (16 * BITS_PER_LONG)) 85 86 #define VNC_STAT_RECT 64 87 #define VNC_STAT_COLS (VNC_MAX_WIDTH / VNC_STAT_RECT) 88 #define VNC_STAT_ROWS (VNC_MAX_HEIGHT / VNC_STAT_RECT) 89 90 #define VNC_AUTH_CHALLENGE_SIZE 16 91 92 typedef struct VncDisplay VncDisplay; 93 94 #ifdef CONFIG_VNC_TLS 95 #include "vnc-tls.h" 96 #include "vnc-auth-vencrypt.h" 97 #endif 98 #ifdef CONFIG_VNC_SASL 99 #include "vnc-auth-sasl.h" 100 #endif 101 102 struct VncRectStat 103 { 104 /* time of last 10 updates, to find update frequency */ 105 struct timeval times[10]; 106 int idx; 107 108 double freq; /* Update frequency (in Hz) */ 109 bool updated; /* Already updated during this refresh */ 110 }; 111 112 typedef struct VncRectStat VncRectStat; 113 114 struct VncSurface 115 { 116 struct timeval last_freq_check; 117 unsigned long dirty[VNC_MAX_HEIGHT][VNC_DIRTY_WORDS]; 118 VncRectStat stats[VNC_STAT_ROWS][VNC_STAT_COLS]; 119 DisplaySurface *ds; 120 }; 121 122 struct VncDisplay 123 { 124 QTAILQ_HEAD(, VncState) clients; 125 QEMUTimer *timer; 126 int timer_interval; 127 int lsock; 128 DisplayState *ds; 129 kbd_layout_t *kbd_layout; 130 int lock_key_sync; 131 #ifdef CONFIG_VNC_THREAD 132 QemuMutex mutex; 133 #endif 134 135 QEMUCursor *cursor; 136 int cursor_msize; 137 uint8_t *cursor_mask; 138 139 struct VncSurface guest; /* guest visible surface (aka ds->surface) */ 140 DisplaySurface *server; /* vnc server surface */ 141 142 char *display; 143 char *password; 144 time_t expires; 145 int auth; 146 bool lossy; 147 #ifdef CONFIG_VNC_TLS 148 int subauth; /* Used by VeNCrypt */ 149 VncDisplayTLS tls; 150 #endif 151 #ifdef CONFIG_VNC_SASL 152 VncDisplaySASL sasl; 153 #endif 154 }; 155 156 typedef struct VncTight { 157 int type; 158 uint8_t quality; 159 uint8_t compression; 160 uint8_t pixel24; 161 Buffer tight; 162 Buffer tmp; 163 Buffer zlib; 164 Buffer gradient; 165 #ifdef CONFIG_VNC_JPEG 166 Buffer jpeg; 167 #endif 168 #ifdef CONFIG_VNC_PNG 169 Buffer png; 170 #endif 171 int levels[4]; 172 z_stream stream[4]; 173 } VncTight; 174 175 typedef struct VncHextile { 176 VncSendHextileTile *send_tile; 177 } VncHextile; 178 179 typedef struct VncZlib { 180 Buffer zlib; 181 Buffer tmp; 182 z_stream stream; 183 int level; 184 } VncZlib; 185 186 typedef struct VncZrle { 187 int type; 188 Buffer fb; 189 Buffer zrle; 190 Buffer tmp; 191 Buffer zlib; 192 z_stream stream; 193 VncPalette palette; 194 } VncZrle; 195 196 typedef struct VncZywrle { 197 int buf[VNC_ZRLE_TILE_WIDTH * VNC_ZRLE_TILE_HEIGHT]; 198 } VncZywrle; 199 200 #ifdef CONFIG_VNC_THREAD 201 struct VncRect 202 { 203 int x; 204 int y; 205 int w; 206 int h; 207 }; 208 209 struct VncRectEntry 210 { 211 struct VncRect rect; 212 QLIST_ENTRY(VncRectEntry) next; 213 }; 214 215 struct VncJob 216 { 217 VncState *vs; 218 219 QLIST_HEAD(, VncRectEntry) rectangles; 220 QTAILQ_ENTRY(VncJob) next; 221 }; 222 #else 223 struct VncJob 224 { 225 VncState *vs; 226 int rectangles; 227 size_t saved_offset; 228 }; 229 #endif 230 231 struct VncState 232 { 233 int csock; 234 235 DisplayState *ds; 236 unsigned long dirty[VNC_MAX_HEIGHT][VNC_DIRTY_WORDS]; 237 uint8_t **lossy_rect; /* Not an Array to avoid costly memcpy in 238 * vnc-jobs-async.c */ 239 240 VncDisplay *vd; 241 int need_update; 242 int force_update; 243 uint32_t features; 244 int absolute; 245 int last_x; 246 int last_y; 247 int client_width; 248 int client_height; 249 250 uint32_t vnc_encoding; 251 252 int major; 253 int minor; 254 255 char challenge[VNC_AUTH_CHALLENGE_SIZE]; 256 #ifdef CONFIG_VNC_TLS 257 VncStateTLS tls; 258 #endif 259 #ifdef CONFIG_VNC_SASL 260 VncStateSASL sasl; 261 #endif 262 263 QObject *info; 264 265 Buffer output; 266 Buffer input; 267 /* current output mode information */ 268 VncWritePixels *write_pixels; 269 DisplaySurface clientds; 270 271 CaptureVoiceOut *audio_cap; 272 struct audsettings as; 273 274 VncReadEvent *read_handler; 275 size_t read_handler_expect; 276 /* input */ 277 uint8_t modifiers_state[256]; 278 QEMUPutLEDEntry *led; 279 280 bool abort; 281 #ifndef CONFIG_VNC_THREAD 282 VncJob job; 283 #else 284 QemuMutex output_mutex; 285 #endif 286 287 /* Encoding specific, if you add something here, don't forget to 288 * update vnc_async_encoding_start() 289 */ 290 VncTight tight; 291 VncZlib zlib; 292 VncHextile hextile; 293 VncZrle zrle; 294 VncZywrle zywrle; 295 296 Notifier mouse_mode_notifier; 297 298 QTAILQ_ENTRY(VncState) next; 299 }; 300 301 302 /***************************************************************************** 303 * 304 * Authentication modes 305 * 306 *****************************************************************************/ 307 308 enum { 309 VNC_AUTH_INVALID = 0, 310 VNC_AUTH_NONE = 1, 311 VNC_AUTH_VNC = 2, 312 VNC_AUTH_RA2 = 5, 313 VNC_AUTH_RA2NE = 6, 314 VNC_AUTH_TIGHT = 16, 315 VNC_AUTH_ULTRA = 17, 316 VNC_AUTH_TLS = 18, /* Supported in GTK-VNC & VINO */ 317 VNC_AUTH_VENCRYPT = 19, /* Supported in GTK-VNC & VeNCrypt */ 318 VNC_AUTH_SASL = 20, /* Supported in GTK-VNC & VINO */ 319 }; 320 321 enum { 322 VNC_AUTH_VENCRYPT_PLAIN = 256, 323 VNC_AUTH_VENCRYPT_TLSNONE = 257, 324 VNC_AUTH_VENCRYPT_TLSVNC = 258, 325 VNC_AUTH_VENCRYPT_TLSPLAIN = 259, 326 VNC_AUTH_VENCRYPT_X509NONE = 260, 327 VNC_AUTH_VENCRYPT_X509VNC = 261, 328 VNC_AUTH_VENCRYPT_X509PLAIN = 262, 329 VNC_AUTH_VENCRYPT_X509SASL = 263, 330 VNC_AUTH_VENCRYPT_TLSSASL = 264, 331 }; 332 333 334 /***************************************************************************** 335 * 336 * Encoding types 337 * 338 *****************************************************************************/ 339 340 #define VNC_ENCODING_RAW 0x00000000 341 #define VNC_ENCODING_COPYRECT 0x00000001 342 #define VNC_ENCODING_RRE 0x00000002 343 #define VNC_ENCODING_CORRE 0x00000004 344 #define VNC_ENCODING_HEXTILE 0x00000005 345 #define VNC_ENCODING_ZLIB 0x00000006 346 #define VNC_ENCODING_TIGHT 0x00000007 347 #define VNC_ENCODING_ZLIBHEX 0x00000008 348 #define VNC_ENCODING_TRLE 0x0000000f 349 #define VNC_ENCODING_ZRLE 0x00000010 350 #define VNC_ENCODING_ZYWRLE 0x00000011 351 #define VNC_ENCODING_COMPRESSLEVEL0 0xFFFFFF00 /* -256 */ 352 #define VNC_ENCODING_QUALITYLEVEL0 0xFFFFFFE0 /* -32 */ 353 #define VNC_ENCODING_XCURSOR 0xFFFFFF10 /* -240 */ 354 #define VNC_ENCODING_RICH_CURSOR 0xFFFFFF11 /* -239 */ 355 #define VNC_ENCODING_POINTER_POS 0xFFFFFF18 /* -232 */ 356 #define VNC_ENCODING_LASTRECT 0xFFFFFF20 /* -224 */ 357 #define VNC_ENCODING_DESKTOPRESIZE 0xFFFFFF21 /* -223 */ 358 #define VNC_ENCODING_POINTER_TYPE_CHANGE 0XFFFFFEFF /* -257 */ 359 #define VNC_ENCODING_EXT_KEY_EVENT 0XFFFFFEFE /* -258 */ 360 #define VNC_ENCODING_AUDIO 0XFFFFFEFD /* -259 */ 361 #define VNC_ENCODING_TIGHT_PNG 0xFFFFFEFC /* -260 */ 362 #define VNC_ENCODING_WMVi 0x574D5669 363 364 /***************************************************************************** 365 * 366 * Other tight constants 367 * 368 *****************************************************************************/ 369 370 /* 371 * Vendors known by TightVNC: standard VNC/RealVNC, TridiaVNC, and TightVNC. 372 */ 373 374 #define VNC_TIGHT_CCB_RESET_MASK (0x0f) 375 #define VNC_TIGHT_CCB_TYPE_MASK (0x0f << 4) 376 #define VNC_TIGHT_CCB_TYPE_FILL (0x08 << 4) 377 #define VNC_TIGHT_CCB_TYPE_JPEG (0x09 << 4) 378 #define VNC_TIGHT_CCB_TYPE_PNG (0x0A << 4) 379 #define VNC_TIGHT_CCB_BASIC_MAX (0x07 << 4) 380 #define VNC_TIGHT_CCB_BASIC_ZLIB (0x03 << 4) 381 #define VNC_TIGHT_CCB_BASIC_FILTER (0x04 << 4) 382 383 /***************************************************************************** 384 * 385 * Features 386 * 387 *****************************************************************************/ 388 389 #define VNC_FEATURE_RESIZE 0 390 #define VNC_FEATURE_HEXTILE 1 391 #define VNC_FEATURE_POINTER_TYPE_CHANGE 2 392 #define VNC_FEATURE_WMVI 3 393 #define VNC_FEATURE_TIGHT 4 394 #define VNC_FEATURE_ZLIB 5 395 #define VNC_FEATURE_COPYRECT 6 396 #define VNC_FEATURE_RICH_CURSOR 7 397 #define VNC_FEATURE_TIGHT_PNG 8 398 #define VNC_FEATURE_ZRLE 9 399 #define VNC_FEATURE_ZYWRLE 10 400 401 #define VNC_FEATURE_RESIZE_MASK (1 << VNC_FEATURE_RESIZE) 402 #define VNC_FEATURE_HEXTILE_MASK (1 << VNC_FEATURE_HEXTILE) 403 #define VNC_FEATURE_POINTER_TYPE_CHANGE_MASK (1 << VNC_FEATURE_POINTER_TYPE_CHANGE) 404 #define VNC_FEATURE_WMVI_MASK (1 << VNC_FEATURE_WMVI) 405 #define VNC_FEATURE_TIGHT_MASK (1 << VNC_FEATURE_TIGHT) 406 #define VNC_FEATURE_ZLIB_MASK (1 << VNC_FEATURE_ZLIB) 407 #define VNC_FEATURE_COPYRECT_MASK (1 << VNC_FEATURE_COPYRECT) 408 #define VNC_FEATURE_RICH_CURSOR_MASK (1 << VNC_FEATURE_RICH_CURSOR) 409 #define VNC_FEATURE_TIGHT_PNG_MASK (1 << VNC_FEATURE_TIGHT_PNG) 410 #define VNC_FEATURE_ZRLE_MASK (1 << VNC_FEATURE_ZRLE) 411 #define VNC_FEATURE_ZYWRLE_MASK (1 << VNC_FEATURE_ZYWRLE) 412 413 414 /* Client -> Server message IDs */ 415 #define VNC_MSG_CLIENT_SET_PIXEL_FORMAT 0 416 #define VNC_MSG_CLIENT_SET_ENCODINGS 2 417 #define VNC_MSG_CLIENT_FRAMEBUFFER_UPDATE_REQUEST 3 418 #define VNC_MSG_CLIENT_KEY_EVENT 4 419 #define VNC_MSG_CLIENT_POINTER_EVENT 5 420 #define VNC_MSG_CLIENT_CUT_TEXT 6 421 #define VNC_MSG_CLIENT_VMWARE_0 127 422 #define VNC_MSG_CLIENT_CALL_CONTROL 249 423 #define VNC_MSG_CLIENT_XVP 250 424 #define VNC_MSG_CLIENT_SET_DESKTOP_SIZE 251 425 #define VNC_MSG_CLIENT_TIGHT 252 426 #define VNC_MSG_CLIENT_GII 253 427 #define VNC_MSG_CLIENT_VMWARE_1 254 428 #define VNC_MSG_CLIENT_QEMU 255 429 430 /* Server -> Client message IDs */ 431 #define VNC_MSG_SERVER_FRAMEBUFFER_UPDATE 0 432 #define VNC_MSG_SERVER_SET_COLOUR_MAP_ENTRIES 1 433 #define VNC_MSG_SERVER_BELL 2 434 #define VNC_MSG_SERVER_CUT_TEXT 3 435 #define VNC_MSG_SERVER_VMWARE_0 127 436 #define VNC_MSG_SERVER_CALL_CONTROL 249 437 #define VNC_MSG_SERVER_XVP 250 438 #define VNC_MSG_SERVER_TIGHT 252 439 #define VNC_MSG_SERVER_GII 253 440 #define VNC_MSG_SERVER_VMWARE_1 254 441 #define VNC_MSG_SERVER_QEMU 255 442 443 444 445 /* QEMU client -> server message IDs */ 446 #define VNC_MSG_CLIENT_QEMU_EXT_KEY_EVENT 0 447 #define VNC_MSG_CLIENT_QEMU_AUDIO 1 448 449 /* QEMU server -> client message IDs */ 450 #define VNC_MSG_SERVER_QEMU_AUDIO 1 451 452 453 454 /* QEMU client -> server audio message IDs */ 455 #define VNC_MSG_CLIENT_QEMU_AUDIO_ENABLE 0 456 #define VNC_MSG_CLIENT_QEMU_AUDIO_DISABLE 1 457 #define VNC_MSG_CLIENT_QEMU_AUDIO_SET_FORMAT 2 458 459 /* QEMU server -> client audio message IDs */ 460 #define VNC_MSG_SERVER_QEMU_AUDIO_END 0 461 #define VNC_MSG_SERVER_QEMU_AUDIO_BEGIN 1 462 #define VNC_MSG_SERVER_QEMU_AUDIO_DATA 2 463 464 465 /***************************************************************************** 466 * 467 * Internal APIs 468 * 469 *****************************************************************************/ 470 471 /* Event loop functions */ 472 void vnc_client_read(void *opaque); 473 void vnc_client_write(void *opaque); 474 475 long vnc_client_read_buf(VncState *vs, uint8_t *data, size_t datalen); 476 long vnc_client_write_buf(VncState *vs, const uint8_t *data, size_t datalen); 477 478 /* Protocol I/O functions */ 479 void vnc_write(VncState *vs, const void *data, size_t len); 480 void vnc_write_u32(VncState *vs, uint32_t value); 481 void vnc_write_s32(VncState *vs, int32_t value); 482 void vnc_write_u16(VncState *vs, uint16_t value); 483 void vnc_write_u8(VncState *vs, uint8_t value); 484 void vnc_flush(VncState *vs); 485 void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting); 486 487 488 /* Buffer I/O functions */ 489 uint8_t read_u8(uint8_t *data, size_t offset); 490 uint16_t read_u16(uint8_t *data, size_t offset); 491 int32_t read_s32(uint8_t *data, size_t offset); 492 uint32_t read_u32(uint8_t *data, size_t offset); 493 494 /* Protocol stage functions */ 495 void vnc_client_error(VncState *vs); 496 int vnc_client_io_error(VncState *vs, int ret, int last_errno); 497 498 void start_client_init(VncState *vs); 499 void start_auth_vnc(VncState *vs); 500 501 /* Buffer management */ 502 void buffer_reserve(Buffer *buffer, size_t len); 503 int buffer_empty(Buffer *buffer); 504 uint8_t *buffer_end(Buffer *buffer); 505 void buffer_reset(Buffer *buffer); 506 void buffer_free(Buffer *buffer); 507 void buffer_append(Buffer *buffer, const void *data, size_t len); 508 509 510 /* Misc helpers */ 511 512 char *vnc_socket_local_addr(const char *format, int fd); 513 char *vnc_socket_remote_addr(const char *format, int fd); 514 515 static inline uint32_t vnc_has_feature(VncState *vs, int feature) { 516 return (vs->features & (1 << feature)); 517 } 518 519 /* Framebuffer */ 520 void vnc_framebuffer_update(VncState *vs, int x, int y, int w, int h, 521 int32_t encoding); 522 523 void vnc_convert_pixel(VncState *vs, uint8_t *buf, uint32_t v); 524 double vnc_update_freq(VncState *vs, int x, int y, int w, int h); 525 void vnc_sent_lossy_rect(VncState *vs, int x, int y, int w, int h); 526 527 /* Encodings */ 528 int vnc_send_framebuffer_update(VncState *vs, int x, int y, int w, int h); 529 530 int vnc_raw_send_framebuffer_update(VncState *vs, int x, int y, int w, int h); 531 532 int vnc_hextile_send_framebuffer_update(VncState *vs, int x, 533 int y, int w, int h); 534 void vnc_hextile_set_pixel_conversion(VncState *vs, int generic); 535 536 void *vnc_zlib_zalloc(void *x, unsigned items, unsigned size); 537 void vnc_zlib_zfree(void *x, void *addr); 538 int vnc_zlib_send_framebuffer_update(VncState *vs, int x, int y, int w, int h); 539 void vnc_zlib_clear(VncState *vs); 540 541 int vnc_tight_send_framebuffer_update(VncState *vs, int x, int y, int w, int h); 542 int vnc_tight_png_send_framebuffer_update(VncState *vs, int x, int y, 543 int w, int h); 544 void vnc_tight_clear(VncState *vs); 545 546 int vnc_zrle_send_framebuffer_update(VncState *vs, int x, int y, int w, int h); 547 int vnc_zywrle_send_framebuffer_update(VncState *vs, int x, int y, int w, int h); 548 void vnc_zrle_clear(VncState *vs); 549 550 #endif /* __QEMU_VNC_H */ 551