Statistics
| Branch: | Revision:

root / ui / vnc.h @ 999342a0

History | View | Annotate | Download (15.2 kB)

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 <zlib.h>
39
#include <stdbool.h>
40

    
41
#include "keymaps.h"
42

    
43
// #define _VNC_DEBUG 1
44

    
45
#ifdef _VNC_DEBUG
46
#define VNC_DEBUG(fmt, ...) do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
47
#else
48
#define VNC_DEBUG(fmt, ...) do { } while (0)
49
#endif
50

    
51
/*****************************************************************************
52
 *
53
 * Core data structures
54
 *
55
 *****************************************************************************/
56

    
57
typedef struct Buffer
58
{
59
    size_t capacity;
60
    size_t offset;
61
    uint8_t *buffer;
62
} Buffer;
63

    
64
typedef struct VncState VncState;
65
typedef struct VncJob VncJob;
66
typedef struct VncRect VncRect;
67
typedef struct VncRectEntry VncRectEntry;
68

    
69
typedef int VncReadEvent(VncState *vs, uint8_t *data, size_t len);
70

    
71
typedef void VncWritePixels(VncState *vs, struct PixelFormat *pf, void *data, int size);
72

    
73
typedef void VncSendHextileTile(VncState *vs,
74
                                int x, int y, int w, int h,
75
                                void *last_bg,
76
                                void *last_fg,
77
                                int *has_bg, int *has_fg);
78

    
79
#define VNC_MAX_WIDTH 2560
80
#define VNC_MAX_HEIGHT 2048
81
#define VNC_DIRTY_WORDS (VNC_MAX_WIDTH / (16 * 32))
82

    
83
#define VNC_STAT_RECT  64
84
#define VNC_STAT_COLS (VNC_MAX_WIDTH / VNC_STAT_RECT)
85
#define VNC_STAT_ROWS (VNC_MAX_HEIGHT / VNC_STAT_RECT)
86

    
87
#define VNC_AUTH_CHALLENGE_SIZE 16
88

    
89
typedef struct VncDisplay VncDisplay;
90

    
91
#ifdef CONFIG_VNC_TLS
92
#include "vnc-tls.h"
93
#include "vnc-auth-vencrypt.h"
94
#endif
95
#ifdef CONFIG_VNC_SASL
96
#include "vnc-auth-sasl.h"
97
#endif
98

    
99
struct VncRectStat
100
{
101
    /* time of last 10 updates, to find update frequency */
102
    struct timeval times[10];
103
    int idx;
104

    
105
    double freq;        /* Update frequency (in Hz) */
106
    bool updated;       /* Already updated during this refresh */
107
};
108

    
109
typedef struct VncRectStat VncRectStat;
110

    
111
struct VncSurface
112
{
113
    struct timeval last_freq_check;
114
    uint32_t dirty[VNC_MAX_HEIGHT][VNC_DIRTY_WORDS];
115
    VncRectStat stats[VNC_STAT_ROWS][VNC_STAT_COLS];
116
    DisplaySurface *ds;
117
};
118

    
119
struct VncDisplay
120
{
121
    QTAILQ_HEAD(, VncState) clients;
122
    QEMUTimer *timer;
123
    int timer_interval;
124
    int lsock;
125
    DisplayState *ds;
126
    kbd_layout_t *kbd_layout;
127
    int lock_key_sync;
128
#ifdef CONFIG_VNC_THREAD
129
    QemuMutex mutex;
130
#endif
131

    
132
    QEMUCursor *cursor;
133
    int cursor_msize;
134
    uint8_t *cursor_mask;
135

    
136
    struct VncSurface guest;   /* guest visible surface (aka ds->surface) */
137
    DisplaySurface *server;  /* vnc server surface */
138

    
139
    char *display;
140
    char *password;
141
    time_t expires;
142
    int auth;
143
    bool lossy;
144
#ifdef CONFIG_VNC_TLS
145
    int subauth; /* Used by VeNCrypt */
146
    VncDisplayTLS tls;
147
#endif
148
#ifdef CONFIG_VNC_SASL
149
    VncDisplaySASL sasl;
150
#endif
151
};
152

    
153
typedef struct VncTight {
154
    int type;
155
    uint8_t quality;
156
    uint8_t compression;
157
    uint8_t pixel24;
158
    Buffer tight;
159
    Buffer tmp;
160
    Buffer zlib;
161
    Buffer gradient;
162
#ifdef CONFIG_VNC_JPEG
163
    Buffer jpeg;
164
#endif
165
#ifdef CONFIG_VNC_PNG
166
    Buffer png;
167
#endif
168
    int levels[4];
169
    z_stream stream[4];
170
} VncTight;
171

    
172
typedef struct VncHextile {
173
    VncSendHextileTile *send_tile;
174
} VncHextile;
175

    
176
typedef struct VncZlib {
177
    Buffer zlib;
178
    Buffer tmp;
179
    z_stream stream;
180
    int level;
181
} VncZlib;
182

    
183
#ifdef CONFIG_VNC_THREAD
184
struct VncRect
185
{
186
    int x;
187
    int y;
188
    int w;
189
    int h;
190
};
191

    
192
struct VncRectEntry
193
{
194
    struct VncRect rect;
195
    QLIST_ENTRY(VncRectEntry) next;
196
};
197

    
198
struct VncJob
199
{
200
    VncState *vs;
201

    
202
    QLIST_HEAD(, VncRectEntry) rectangles;
203
    QTAILQ_ENTRY(VncJob) next;
204
};
205
#else
206
struct VncJob
207
{
208
    VncState *vs;
209
    int rectangles;
210
    size_t saved_offset;
211
};
212
#endif
213

    
214
struct VncState
215
{
216
    int csock;
217

    
218
    DisplayState *ds;
219
    uint32_t dirty[VNC_MAX_HEIGHT][VNC_DIRTY_WORDS];
220

    
221
    VncDisplay *vd;
222
    int need_update;
223
    int force_update;
224
    uint32_t features;
225
    int absolute;
226
    int last_x;
227
    int last_y;
228
    int client_width;
229
    int client_height;
230

    
231
    uint32_t vnc_encoding;
232

    
233
    int major;
234
    int minor;
235

    
236
    char challenge[VNC_AUTH_CHALLENGE_SIZE];
237
#ifdef CONFIG_VNC_TLS
238
    VncStateTLS tls;
239
#endif
240
#ifdef CONFIG_VNC_SASL
241
    VncStateSASL sasl;
242
#endif
243

    
244
    QObject *info;
245

    
246
    Buffer output;
247
    Buffer input;
248
    /* current output mode information */
249
    VncWritePixels *write_pixels;
250
    DisplaySurface clientds;
251

    
252
    CaptureVoiceOut *audio_cap;
253
    struct audsettings as;
254

    
255
    VncReadEvent *read_handler;
256
    size_t read_handler_expect;
257
    /* input */
258
    uint8_t modifiers_state[256];
259
    QEMUPutLEDEntry *led;
260

    
261
    bool abort;
262
#ifndef CONFIG_VNC_THREAD
263
    VncJob job;
264
#else
265
    QemuMutex output_mutex;
266
#endif
267

    
268
    /* Encoding specific, if you add something here, don't forget to
269
     *  update vnc_async_encoding_start()
270
     */
271
    VncTight tight;
272
    VncZlib zlib;
273
    VncHextile hextile;
274

    
275

    
276
    Notifier mouse_mode_notifier;
277

    
278
    QTAILQ_ENTRY(VncState) next;
279
};
280

    
281

    
282
/*****************************************************************************
283
 *
284
 * Authentication modes
285
 *
286
 *****************************************************************************/
287

    
288
enum {
289
    VNC_AUTH_INVALID = 0,
290
    VNC_AUTH_NONE = 1,
291
    VNC_AUTH_VNC = 2,
292
    VNC_AUTH_RA2 = 5,
293
    VNC_AUTH_RA2NE = 6,
294
    VNC_AUTH_TIGHT = 16,
295
    VNC_AUTH_ULTRA = 17,
296
    VNC_AUTH_TLS = 18,      /* Supported in GTK-VNC & VINO */
297
    VNC_AUTH_VENCRYPT = 19, /* Supported in GTK-VNC & VeNCrypt */
298
    VNC_AUTH_SASL = 20,     /* Supported in GTK-VNC & VINO */
299
};
300

    
301
enum {
302
    VNC_AUTH_VENCRYPT_PLAIN = 256,
303
    VNC_AUTH_VENCRYPT_TLSNONE = 257,
304
    VNC_AUTH_VENCRYPT_TLSVNC = 258,
305
    VNC_AUTH_VENCRYPT_TLSPLAIN = 259,
306
    VNC_AUTH_VENCRYPT_X509NONE = 260,
307
    VNC_AUTH_VENCRYPT_X509VNC = 261,
308
    VNC_AUTH_VENCRYPT_X509PLAIN = 262,
309
    VNC_AUTH_VENCRYPT_X509SASL = 263,
310
    VNC_AUTH_VENCRYPT_TLSSASL = 264,
311
};
312

    
313

    
314
/*****************************************************************************
315
 *
316
 * Encoding types
317
 *
318
 *****************************************************************************/
319

    
320
#define VNC_ENCODING_RAW                  0x00000000
321
#define VNC_ENCODING_COPYRECT             0x00000001
322
#define VNC_ENCODING_RRE                  0x00000002
323
#define VNC_ENCODING_CORRE                0x00000004
324
#define VNC_ENCODING_HEXTILE              0x00000005
325
#define VNC_ENCODING_ZLIB                 0x00000006
326
#define VNC_ENCODING_TIGHT                0x00000007
327
#define VNC_ENCODING_ZLIBHEX              0x00000008
328
#define VNC_ENCODING_TRLE                 0x0000000f
329
#define VNC_ENCODING_ZRLE                 0x00000010
330
#define VNC_ENCODING_ZYWRLE               0x00000011
331
#define VNC_ENCODING_COMPRESSLEVEL0       0xFFFFFF00 /* -256 */
332
#define VNC_ENCODING_QUALITYLEVEL0        0xFFFFFFE0 /* -32  */
333
#define VNC_ENCODING_XCURSOR              0xFFFFFF10 /* -240 */
334
#define VNC_ENCODING_RICH_CURSOR          0xFFFFFF11 /* -239 */
335
#define VNC_ENCODING_POINTER_POS          0xFFFFFF18 /* -232 */
336
#define VNC_ENCODING_LASTRECT             0xFFFFFF20 /* -224 */
337
#define VNC_ENCODING_DESKTOPRESIZE        0xFFFFFF21 /* -223 */
338
#define VNC_ENCODING_POINTER_TYPE_CHANGE  0XFFFFFEFF /* -257 */
339
#define VNC_ENCODING_EXT_KEY_EVENT        0XFFFFFEFE /* -258 */
340
#define VNC_ENCODING_AUDIO                0XFFFFFEFD /* -259 */
341
#define VNC_ENCODING_TIGHT_PNG            0xFFFFFEFC /* -260 */
342
#define VNC_ENCODING_WMVi                 0x574D5669
343

    
344
/*****************************************************************************
345
 *
346
 * Other tight constants
347
 *
348
 *****************************************************************************/
349

    
350
/*
351
 * Vendors known by TightVNC: standard VNC/RealVNC, TridiaVNC, and TightVNC.
352
 */
353

    
354
#define VNC_TIGHT_CCB_RESET_MASK   (0x0f)
355
#define VNC_TIGHT_CCB_TYPE_MASK    (0x0f << 4)
356
#define VNC_TIGHT_CCB_TYPE_FILL    (0x08 << 4)
357
#define VNC_TIGHT_CCB_TYPE_JPEG    (0x09 << 4)
358
#define VNC_TIGHT_CCB_TYPE_PNG     (0x0A << 4)
359
#define VNC_TIGHT_CCB_BASIC_MAX    (0x07 << 4)
360
#define VNC_TIGHT_CCB_BASIC_ZLIB   (0x03 << 4)
361
#define VNC_TIGHT_CCB_BASIC_FILTER (0x04 << 4)
362

    
363
/*****************************************************************************
364
 *
365
 * Features
366
 *
367
 *****************************************************************************/
368

    
369
#define VNC_FEATURE_RESIZE                   0
370
#define VNC_FEATURE_HEXTILE                  1
371
#define VNC_FEATURE_POINTER_TYPE_CHANGE      2
372
#define VNC_FEATURE_WMVI                     3
373
#define VNC_FEATURE_TIGHT                    4
374
#define VNC_FEATURE_ZLIB                     5
375
#define VNC_FEATURE_COPYRECT                 6
376
#define VNC_FEATURE_RICH_CURSOR              7
377
#define VNC_FEATURE_TIGHT_PNG                8
378

    
379
#define VNC_FEATURE_RESIZE_MASK              (1 << VNC_FEATURE_RESIZE)
380
#define VNC_FEATURE_HEXTILE_MASK             (1 << VNC_FEATURE_HEXTILE)
381
#define VNC_FEATURE_POINTER_TYPE_CHANGE_MASK (1 << VNC_FEATURE_POINTER_TYPE_CHANGE)
382
#define VNC_FEATURE_WMVI_MASK                (1 << VNC_FEATURE_WMVI)
383
#define VNC_FEATURE_TIGHT_MASK               (1 << VNC_FEATURE_TIGHT)
384
#define VNC_FEATURE_ZLIB_MASK                (1 << VNC_FEATURE_ZLIB)
385
#define VNC_FEATURE_COPYRECT_MASK            (1 << VNC_FEATURE_COPYRECT)
386
#define VNC_FEATURE_RICH_CURSOR_MASK         (1 << VNC_FEATURE_RICH_CURSOR)
387
#define VNC_FEATURE_TIGHT_PNG_MASK           (1 << VNC_FEATURE_TIGHT_PNG)
388

    
389

    
390
/* Client -> Server message IDs */
391
#define VNC_MSG_CLIENT_SET_PIXEL_FORMAT           0
392
#define VNC_MSG_CLIENT_SET_ENCODINGS              2
393
#define VNC_MSG_CLIENT_FRAMEBUFFER_UPDATE_REQUEST 3
394
#define VNC_MSG_CLIENT_KEY_EVENT                  4
395
#define VNC_MSG_CLIENT_POINTER_EVENT              5
396
#define VNC_MSG_CLIENT_CUT_TEXT                   6
397
#define VNC_MSG_CLIENT_VMWARE_0                   127
398
#define VNC_MSG_CLIENT_CALL_CONTROL               249
399
#define VNC_MSG_CLIENT_XVP                        250
400
#define VNC_MSG_CLIENT_SET_DESKTOP_SIZE           251
401
#define VNC_MSG_CLIENT_TIGHT                      252
402
#define VNC_MSG_CLIENT_GII                        253
403
#define VNC_MSG_CLIENT_VMWARE_1                   254
404
#define VNC_MSG_CLIENT_QEMU                       255
405

    
406
/* Server -> Client message IDs */
407
#define VNC_MSG_SERVER_FRAMEBUFFER_UPDATE         0
408
#define VNC_MSG_SERVER_SET_COLOUR_MAP_ENTRIES     1
409
#define VNC_MSG_SERVER_BELL                       2
410
#define VNC_MSG_SERVER_CUT_TEXT                   3
411
#define VNC_MSG_SERVER_VMWARE_0                   127
412
#define VNC_MSG_SERVER_CALL_CONTROL               249
413
#define VNC_MSG_SERVER_XVP                        250
414
#define VNC_MSG_SERVER_TIGHT                      252
415
#define VNC_MSG_SERVER_GII                        253
416
#define VNC_MSG_SERVER_VMWARE_1                   254
417
#define VNC_MSG_SERVER_QEMU                       255
418

    
419

    
420

    
421
/* QEMU client -> server message IDs */
422
#define VNC_MSG_CLIENT_QEMU_EXT_KEY_EVENT         0
423
#define VNC_MSG_CLIENT_QEMU_AUDIO                 1
424

    
425
/* QEMU server -> client message IDs */
426
#define VNC_MSG_SERVER_QEMU_AUDIO                 1
427

    
428

    
429

    
430
/* QEMU client -> server audio message IDs */
431
#define VNC_MSG_CLIENT_QEMU_AUDIO_ENABLE          0
432
#define VNC_MSG_CLIENT_QEMU_AUDIO_DISABLE         1
433
#define VNC_MSG_CLIENT_QEMU_AUDIO_SET_FORMAT      2
434

    
435
/* QEMU server -> client audio message IDs */
436
#define VNC_MSG_SERVER_QEMU_AUDIO_END             0
437
#define VNC_MSG_SERVER_QEMU_AUDIO_BEGIN           1
438
#define VNC_MSG_SERVER_QEMU_AUDIO_DATA            2
439

    
440

    
441
/*****************************************************************************
442
 *
443
 * Internal APIs
444
 *
445
 *****************************************************************************/
446

    
447
/* Event loop functions */
448
void vnc_client_read(void *opaque);
449
void vnc_client_write(void *opaque);
450

    
451
long vnc_client_read_buf(VncState *vs, uint8_t *data, size_t datalen);
452
long vnc_client_write_buf(VncState *vs, const uint8_t *data, size_t datalen);
453

    
454
/* Protocol I/O functions */
455
void vnc_write(VncState *vs, const void *data, size_t len);
456
void vnc_write_u32(VncState *vs, uint32_t value);
457
void vnc_write_s32(VncState *vs, int32_t value);
458
void vnc_write_u16(VncState *vs, uint16_t value);
459
void vnc_write_u8(VncState *vs, uint8_t value);
460
void vnc_flush(VncState *vs);
461
void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting);
462

    
463

    
464
/* Buffer I/O functions */
465
uint8_t read_u8(uint8_t *data, size_t offset);
466
uint16_t read_u16(uint8_t *data, size_t offset);
467
int32_t read_s32(uint8_t *data, size_t offset);
468
uint32_t read_u32(uint8_t *data, size_t offset);
469

    
470
/* Protocol stage functions */
471
void vnc_client_error(VncState *vs);
472
int vnc_client_io_error(VncState *vs, int ret, int last_errno);
473

    
474
void start_client_init(VncState *vs);
475
void start_auth_vnc(VncState *vs);
476

    
477
/* Buffer management */
478
void buffer_reserve(Buffer *buffer, size_t len);
479
int buffer_empty(Buffer *buffer);
480
uint8_t *buffer_end(Buffer *buffer);
481
void buffer_reset(Buffer *buffer);
482
void buffer_free(Buffer *buffer);
483
void buffer_append(Buffer *buffer, const void *data, size_t len);
484

    
485

    
486
/* Misc helpers */
487

    
488
char *vnc_socket_local_addr(const char *format, int fd);
489
char *vnc_socket_remote_addr(const char *format, int fd);
490

    
491
static inline uint32_t vnc_has_feature(VncState *vs, int feature) {
492
    return (vs->features & (1 << feature));
493
}
494

    
495
/* Framebuffer */
496
void vnc_framebuffer_update(VncState *vs, int x, int y, int w, int h,
497
                            int32_t encoding);
498

    
499
void vnc_convert_pixel(VncState *vs, uint8_t *buf, uint32_t v);
500
double vnc_update_freq(VncState *vs, int x, int y, int w, int h);
501

    
502
/* Encodings */
503
int vnc_send_framebuffer_update(VncState *vs, int x, int y, int w, int h);
504

    
505
int vnc_raw_send_framebuffer_update(VncState *vs, int x, int y, int w, int h);
506

    
507
int vnc_hextile_send_framebuffer_update(VncState *vs, int x,
508
                                         int y, int w, int h);
509
void vnc_hextile_set_pixel_conversion(VncState *vs, int generic);
510

    
511
void *vnc_zlib_zalloc(void *x, unsigned items, unsigned size);
512
void vnc_zlib_zfree(void *x, void *addr);
513
int vnc_zlib_send_framebuffer_update(VncState *vs, int x, int y, int w, int h);
514
void vnc_zlib_clear(VncState *vs);
515

    
516
int vnc_tight_send_framebuffer_update(VncState *vs, int x, int y, int w, int h);
517
int vnc_tight_png_send_framebuffer_update(VncState *vs, int x, int y,
518
                                          int w, int h);
519
void vnc_tight_clear(VncState *vs);
520

    
521
#endif /* __QEMU_VNC_H */