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