Statistics
| Branch: | Revision:

root / ui / vnc.h @ e31e3694

History | View | Annotate | Download (15.4 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
    uint8_t **lossy_rect; /* Not an Array to avoid costly memcpy in
221
                           * vnc-jobs-async.c */
222

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

    
233
    uint32_t vnc_encoding;
234

    
235
    int major;
236
    int minor;
237

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

    
246
    QObject *info;
247

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

    
254
    CaptureVoiceOut *audio_cap;
255
    struct audsettings as;
256

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

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

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

    
277

    
278
    Notifier mouse_mode_notifier;
279

    
280
    QTAILQ_ENTRY(VncState) next;
281
};
282

    
283

    
284
/*****************************************************************************
285
 *
286
 * Authentication modes
287
 *
288
 *****************************************************************************/
289

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

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

    
315

    
316
/*****************************************************************************
317
 *
318
 * Encoding types
319
 *
320
 *****************************************************************************/
321

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

    
346
/*****************************************************************************
347
 *
348
 * Other tight constants
349
 *
350
 *****************************************************************************/
351

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

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

    
365
/*****************************************************************************
366
 *
367
 * Features
368
 *
369
 *****************************************************************************/
370

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

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

    
391

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

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

    
421

    
422

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

    
427
/* QEMU server -> client message IDs */
428
#define VNC_MSG_SERVER_QEMU_AUDIO                 1
429

    
430

    
431

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

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

    
442

    
443
/*****************************************************************************
444
 *
445
 * Internal APIs
446
 *
447
 *****************************************************************************/
448

    
449
/* Event loop functions */
450
void vnc_client_read(void *opaque);
451
void vnc_client_write(void *opaque);
452

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

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

    
465

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

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

    
476
void start_client_init(VncState *vs);
477
void start_auth_vnc(VncState *vs);
478

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

    
487

    
488
/* Misc helpers */
489

    
490
char *vnc_socket_local_addr(const char *format, int fd);
491
char *vnc_socket_remote_addr(const char *format, int fd);
492

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

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

    
501
void vnc_convert_pixel(VncState *vs, uint8_t *buf, uint32_t v);
502
double vnc_update_freq(VncState *vs, int x, int y, int w, int h);
503
void vnc_sent_lossy_rect(VncState *vs, int x, int y, int w, int h);
504

    
505
/* Encodings */
506
int vnc_send_framebuffer_update(VncState *vs, int x, int y, int w, int h);
507

    
508
int vnc_raw_send_framebuffer_update(VncState *vs, int x, int y, int w, int h);
509

    
510
int vnc_hextile_send_framebuffer_update(VncState *vs, int x,
511
                                         int y, int w, int h);
512
void vnc_hextile_set_pixel_conversion(VncState *vs, int generic);
513

    
514
void *vnc_zlib_zalloc(void *x, unsigned items, unsigned size);
515
void vnc_zlib_zfree(void *x, void *addr);
516
int vnc_zlib_send_framebuffer_update(VncState *vs, int x, int y, int w, int h);
517
void vnc_zlib_clear(VncState *vs);
518

    
519
int vnc_tight_send_framebuffer_update(VncState *vs, int x, int y, int w, int h);
520
int vnc_tight_png_send_framebuffer_update(VncState *vs, int x, int y,
521
                                          int w, int h);
522
void vnc_tight_clear(VncState *vs);
523

    
524
#endif /* __QEMU_VNC_H */