~ubuntu-branches/ubuntu/raring/virtualbox-ose/raring

« back to all changes in this revision

Viewing changes to src/VBox/Main/ConsoleVRDPServer.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Felix Geyer
  • Date: 2011-01-30 23:27:25 UTC
  • mfrom: (0.3.12 upstream)
  • Revision ID: james.westby@ubuntu.com-20110130232725-2ouajjd2ggdet0zd
Tags: 4.0.2-dfsg-1ubuntu1
* Merge from Debian unstable, remaining changes:
  - Add Apport hook.
    - debian/virtualbox-ose.files/source_virtualbox-ose.py
    - debian/virtualbox-ose.install
  - Drop *-source packages.
* Drop ubuntu-01-fix-build-gcc45.patch, fixed upstream.
* Drop ubuntu-02-as-needed.patch, added to the Debian package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: ConsoleVRDPServer.cpp $ */
2
 
/** @file
3
 
 * VBox Console VRDP Helper class
4
 
 */
5
 
 
6
 
/*
7
 
 * Copyright (C) 2006-2010 Oracle Corporation
8
 
 *
9
 
 * This file is part of VirtualBox Open Source Edition (OSE), as
10
 
 * available from http://www.virtualbox.org. This file is free software;
11
 
 * you can redistribute it and/or modify it under the terms of the GNU
12
 
 * General Public License (GPL) as published by the Free Software
13
 
 * Foundation, in version 2 as it comes in the "COPYING" file of the
14
 
 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15
 
 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16
 
 */
17
 
 
18
 
#include "ConsoleVRDPServer.h"
19
 
#include "ConsoleImpl.h"
20
 
#include "DisplayImpl.h"
21
 
#include "KeyboardImpl.h"
22
 
#include "MouseImpl.h"
23
 
 
24
 
#include "AutoCaller.h"
25
 
#include "Logging.h"
26
 
 
27
 
#include <iprt/asm.h>
28
 
#include <iprt/ldr.h>
29
 
#include <iprt/param.h>
30
 
#include <iprt/path.h>
31
 
#include <iprt/alloca.h>
32
 
 
33
 
#include <VBox/err.h>
34
 
#ifdef VBOX_WITH_VRDP
35
 
#include <VBox/VRDPOrders.h>
36
 
#endif /* VBOX_WITH_VRDP */
37
 
 
38
 
class VRDPConsoleCallback :
39
 
    VBOX_SCRIPTABLE_IMPL(IConsoleCallback)
40
 
{
41
 
public:
42
 
    VRDPConsoleCallback(ConsoleVRDPServer *server)
43
 
        : m_server(server)
44
 
    {
45
 
#ifndef VBOX_WITH_XPCOM
46
 
        refcnt = 0;
47
 
#endif /* !VBOX_WITH_XPCOM */
48
 
    }
49
 
 
50
 
    virtual ~VRDPConsoleCallback() {}
51
 
 
52
 
    NS_DECL_ISUPPORTS
53
 
 
54
 
#ifndef VBOX_WITH_XPCOM
55
 
    STDMETHOD_(ULONG, AddRef)() {
56
 
        return ::InterlockedIncrement(&refcnt);
57
 
    }
58
 
    STDMETHOD_(ULONG, Release)()
59
 
    {
60
 
        long cnt = ::InterlockedDecrement(&refcnt);
61
 
        if (cnt == 0)
62
 
            delete this;
63
 
        return cnt;
64
 
    }
65
 
    STDMETHOD(QueryInterface)(REFIID riid , void **ppObj)
66
 
    {
67
 
        if (riid == IID_IUnknown) {
68
 
            *ppObj = this;
69
 
            AddRef();
70
 
            return S_OK;
71
 
        }
72
 
        if (riid == IID_IConsoleCallback) {
73
 
            *ppObj = this;
74
 
            AddRef();
75
 
            return S_OK;
76
 
        }
77
 
        *ppObj = NULL;
78
 
        return E_NOINTERFACE;
79
 
    }
80
 
#endif /* !VBOX_WITH_XPCOM */
81
 
 
82
 
 
83
 
    STDMETHOD(OnMousePointerShapeChange)(BOOL visible, BOOL alpha, ULONG xHot, ULONG yHot,
84
 
                                         ULONG width, ULONG height, ComSafeArrayIn(BYTE,shape));
85
 
 
86
 
    STDMETHOD(OnMouseCapabilityChange)(BOOL supportsAbsolute, BOOL supportsRelative, BOOL needsHostCursor)
87
 
    {
88
 
        if (m_server)
89
 
        {
90
 
            m_server->NotifyAbsoluteMouse(!!supportsAbsolute);
91
 
        }
92
 
        return S_OK;
93
 
    }
94
 
 
95
 
    STDMETHOD(OnKeyboardLedsChange)(BOOL fNumLock, BOOL fCapsLock, BOOL fScrollLock)
96
 
    {
97
 
        if (m_server)
98
 
        {
99
 
            m_server->NotifyKeyboardLedsChange(fNumLock, fCapsLock, fScrollLock);
100
 
        }
101
 
        return S_OK;
102
 
    }
103
 
 
104
 
    STDMETHOD(OnStateChange)(MachineState_T machineState)
105
 
    {
106
 
        return S_OK;
107
 
    }
108
 
 
109
 
    STDMETHOD(OnAdditionsStateChange)()
110
 
    {
111
 
        return S_OK;
112
 
    }
113
 
 
114
 
    STDMETHOD(OnMediumChange)(IMediumAttachment *aAttachment)
115
 
    {
116
 
        return S_OK;
117
 
    }
118
 
 
119
 
    STDMETHOD(OnCPUChange)(ULONG aCPU, BOOL aRemove)
120
 
    {
121
 
        return S_OK;
122
 
    }
123
 
 
124
 
    STDMETHOD(OnNetworkAdapterChange)(INetworkAdapter *aNetworkAdapter)
125
 
    {
126
 
        return S_OK;
127
 
    }
128
 
 
129
 
    STDMETHOD(OnSerialPortChange)(ISerialPort *aSerialPort)
130
 
    {
131
 
        return S_OK;
132
 
    }
133
 
 
134
 
    STDMETHOD(OnParallelPortChange)(IParallelPort *aParallelPort)
135
 
    {
136
 
        return S_OK;
137
 
    }
138
 
 
139
 
    STDMETHOD(OnStorageControllerChange)()
140
 
    {
141
 
        return S_OK;
142
 
    }
143
 
 
144
 
    STDMETHOD(OnVRDPServerChange)()
145
 
    {
146
 
        return S_OK;
147
 
    }
148
 
 
149
 
    STDMETHOD(OnRemoteDisplayInfoChange)()
150
 
    {
151
 
        return S_OK;
152
 
    }
153
 
 
154
 
    STDMETHOD(OnUSBControllerChange)()
155
 
    {
156
 
        return S_OK;
157
 
    }
158
 
 
159
 
    STDMETHOD(OnUSBDeviceStateChange)(IUSBDevice *aDevice, BOOL aAttached,
160
 
                                      IVirtualBoxErrorInfo *aError)
161
 
    {
162
 
        return S_OK;
163
 
    }
164
 
 
165
 
    STDMETHOD(OnSharedFolderChange)(Scope_T aScope)
166
 
    {
167
 
        return S_OK;
168
 
    }
169
 
 
170
 
    STDMETHOD(OnRuntimeError)(BOOL fatal, IN_BSTR id, IN_BSTR message)
171
 
    {
172
 
        return S_OK;
173
 
    }
174
 
 
175
 
    STDMETHOD(OnCanShowWindow)(BOOL *canShow)
176
 
    {
177
 
        if (!canShow)
178
 
            return E_POINTER;
179
 
        /* we don't manage window activation here: always agree */
180
 
        *canShow = TRUE;
181
 
        return S_OK;
182
 
    }
183
 
 
184
 
    STDMETHOD(OnShowWindow)(ULONG64 *winId)
185
 
    {
186
 
        if (!winId)
187
 
            return E_POINTER;
188
 
        /* we don't manage window activation here */
189
 
        *winId = 0;
190
 
        return S_OK;
191
 
    }
192
 
 
193
 
private:
194
 
    ConsoleVRDPServer *m_server;
195
 
#ifndef VBOX_WITH_XPCOM
196
 
    long refcnt;
197
 
#endif /* !VBOX_WITH_XPCOM */
198
 
};
199
 
 
200
 
#ifdef VBOX_WITH_XPCOM
201
 
#include <nsMemory.h>
202
 
NS_DECL_CLASSINFO(VRDPConsoleCallback)
203
 
NS_IMPL_THREADSAFE_ISUPPORTS1_CI(VRDPConsoleCallback, IConsoleCallback)
204
 
#endif /* VBOX_WITH_XPCOM */
205
 
 
206
 
#ifdef DEBUG_sunlover
207
 
#define LOGDUMPPTR Log
208
 
void dumpPointer(const uint8_t *pu8Shape, uint32_t width, uint32_t height, bool fXorMaskRGB32)
209
 
{
210
 
    unsigned i;
211
 
 
212
 
    const uint8_t *pu8And = pu8Shape;
213
 
 
214
 
    for (i = 0; i < height; i++)
215
 
    {
216
 
        unsigned j;
217
 
        LOGDUMPPTR(("%p: ", pu8And));
218
 
        for (j = 0; j < (width + 7) / 8; j++)
219
 
        {
220
 
            unsigned k;
221
 
            for (k = 0; k < 8; k++)
222
 
            {
223
 
                LOGDUMPPTR(("%d", ((*pu8And) & (1 << (7 - k)))? 1: 0));
224
 
            }
225
 
 
226
 
            pu8And++;
227
 
        }
228
 
        LOGDUMPPTR(("\n"));
229
 
    }
230
 
 
231
 
    if (fXorMaskRGB32)
232
 
    {
233
 
        uint32_t *pu32Xor = (uint32_t*)(pu8Shape + ((((width + 7) / 8) * height + 3) & ~3));
234
 
 
235
 
        for (i = 0; i < height; i++)
236
 
        {
237
 
            unsigned j;
238
 
            LOGDUMPPTR(("%p: ", pu32Xor));
239
 
            for (j = 0; j < width; j++)
240
 
            {
241
 
                LOGDUMPPTR(("%08X", *pu32Xor++));
242
 
            }
243
 
            LOGDUMPPTR(("\n"));
244
 
        }
245
 
    }
246
 
    else
247
 
    {
248
 
        /* RDP 24 bit RGB mask. */
249
 
        uint8_t *pu8Xor = (uint8_t*)(pu8Shape + ((((width + 7) / 8) * height + 3) & ~3));
250
 
        for (i = 0; i < height; i++)
251
 
        {
252
 
            unsigned j;
253
 
            LOGDUMPPTR(("%p: ", pu8Xor));
254
 
            for (j = 0; j < width; j++)
255
 
            {
256
 
                LOGDUMPPTR(("%02X%02X%02X", pu8Xor[2], pu8Xor[1], pu8Xor[0]));
257
 
                pu8Xor += 3;
258
 
            }
259
 
            LOGDUMPPTR(("\n"));
260
 
        }
261
 
    }
262
 
}
263
 
#else
264
 
#define dumpPointer(a, b, c, d) do {} while (0)
265
 
#endif /* DEBUG_sunlover */
266
 
 
267
 
static void findTopLeftBorder(const uint8_t *pu8AndMask, const uint8_t *pu8XorMask, uint32_t width, uint32_t height, uint32_t *pxSkip, uint32_t *pySkip)
268
 
{
269
 
    /*
270
 
     * Find the top border of the AND mask. First assign to special value.
271
 
     */
272
 
    uint32_t ySkipAnd = ~0;
273
 
 
274
 
    const uint8_t *pu8And = pu8AndMask;
275
 
    const uint32_t cbAndRow = (width + 7) / 8;
276
 
    const uint8_t maskLastByte = (uint8_t)( 0xFF << (cbAndRow * 8 - width) );
277
 
 
278
 
    Assert(cbAndRow > 0);
279
 
 
280
 
    unsigned y;
281
 
    unsigned x;
282
 
 
283
 
    for (y = 0; y < height && ySkipAnd == ~(uint32_t)0; y++, pu8And += cbAndRow)
284
 
    {
285
 
        /* For each complete byte in the row. */
286
 
        for (x = 0; x < cbAndRow - 1; x++)
287
 
        {
288
 
            if (pu8And[x] != 0xFF)
289
 
            {
290
 
                ySkipAnd = y;
291
 
                break;
292
 
            }
293
 
        }
294
 
 
295
 
        if (ySkipAnd == ~(uint32_t)0)
296
 
        {
297
 
            /* Last byte. */
298
 
            if ((pu8And[cbAndRow - 1] & maskLastByte) != maskLastByte)
299
 
            {
300
 
                ySkipAnd = y;
301
 
            }
302
 
        }
303
 
    }
304
 
 
305
 
    if (ySkipAnd == ~(uint32_t)0)
306
 
    {
307
 
        ySkipAnd = 0;
308
 
    }
309
 
 
310
 
    /*
311
 
     * Find the left border of the AND mask.
312
 
     */
313
 
    uint32_t xSkipAnd = ~0;
314
 
 
315
 
    /* For all bit columns. */
316
 
    for (x = 0; x < width && xSkipAnd == ~(uint32_t)0; x++)
317
 
    {
318
 
        pu8And = pu8AndMask + x/8;       /* Currently checking byte. */
319
 
        uint8_t mask = 1 << (7 - x%8); /* Currently checking bit in the byte. */
320
 
 
321
 
        for (y = ySkipAnd; y < height; y++, pu8And += cbAndRow)
322
 
        {
323
 
            if ((*pu8And & mask) == 0)
324
 
            {
325
 
                xSkipAnd = x;
326
 
                break;
327
 
            }
328
 
        }
329
 
    }
330
 
 
331
 
    if (xSkipAnd == ~(uint32_t)0)
332
 
    {
333
 
        xSkipAnd = 0;
334
 
    }
335
 
 
336
 
    /*
337
 
     * Find the XOR mask top border.
338
 
     */
339
 
    uint32_t ySkipXor = ~0;
340
 
 
341
 
    uint32_t *pu32XorStart = (uint32_t *)pu8XorMask;
342
 
 
343
 
    uint32_t *pu32Xor = pu32XorStart;
344
 
 
345
 
    for (y = 0; y < height && ySkipXor == ~(uint32_t)0; y++, pu32Xor += width)
346
 
    {
347
 
        for (x = 0; x < width; x++)
348
 
        {
349
 
            if (pu32Xor[x] != 0)
350
 
            {
351
 
                ySkipXor = y;
352
 
                break;
353
 
            }
354
 
        }
355
 
    }
356
 
 
357
 
    if (ySkipXor == ~(uint32_t)0)
358
 
    {
359
 
        ySkipXor = 0;
360
 
    }
361
 
 
362
 
    /*
363
 
     * Find the left border of the XOR mask.
364
 
     */
365
 
    uint32_t xSkipXor = ~(uint32_t)0;
366
 
 
367
 
    /* For all columns. */
368
 
    for (x = 0; x < width && xSkipXor == ~(uint32_t)0; x++)
369
 
    {
370
 
        pu32Xor = pu32XorStart + x;    /* Currently checking dword. */
371
 
 
372
 
        for (y = ySkipXor; y < height; y++, pu32Xor += width)
373
 
        {
374
 
            if (*pu32Xor != 0)
375
 
            {
376
 
                xSkipXor = x;
377
 
                break;
378
 
            }
379
 
        }
380
 
    }
381
 
 
382
 
    if (xSkipXor == ~(uint32_t)0)
383
 
    {
384
 
        xSkipXor = 0;
385
 
    }
386
 
 
387
 
    *pxSkip = RT_MIN(xSkipAnd, xSkipXor);
388
 
    *pySkip = RT_MIN(ySkipAnd, ySkipXor);
389
 
}
390
 
 
391
 
/* Generate an AND mask for alpha pointers here, because
392
 
 * guest driver does not do that correctly for Vista pointers.
393
 
 * Similar fix, changing the alpha threshold, could be applied
394
 
 * for the guest driver, but then additions reinstall would be
395
 
 * necessary, which we try to avoid.
396
 
 */
397
 
static void mousePointerGenerateANDMask(uint8_t *pu8DstAndMask, int cbDstAndMask, const uint8_t *pu8SrcAlpha, int w, int h)
398
 
{
399
 
    memset(pu8DstAndMask, 0xFF, cbDstAndMask);
400
 
 
401
 
    int y;
402
 
    for (y = 0; y < h; y++)
403
 
    {
404
 
        uint8_t bitmask = 0x80;
405
 
 
406
 
        int x;
407
 
        for (x = 0; x < w; x++, bitmask >>= 1)
408
 
        {
409
 
            if (bitmask == 0)
410
 
            {
411
 
                bitmask = 0x80;
412
 
            }
413
 
 
414
 
            /* Whether alpha channel value is not transparent enough for the pixel to be seen. */
415
 
            if (pu8SrcAlpha[x * 4 + 3] > 0x7f)
416
 
            {
417
 
                pu8DstAndMask[x / 8] &= ~bitmask;
418
 
            }
419
 
        }
420
 
 
421
 
        /* Point to next source and dest scans. */
422
 
        pu8SrcAlpha += w * 4;
423
 
        pu8DstAndMask += (w + 7) / 8;
424
 
    }
425
 
}
426
 
 
427
 
STDMETHODIMP VRDPConsoleCallback::OnMousePointerShapeChange(BOOL visible,
428
 
                                                            BOOL alpha,
429
 
                                                            ULONG xHot,
430
 
                                                            ULONG yHot,
431
 
                                                            ULONG width,
432
 
                                                            ULONG height,
433
 
                                                            ComSafeArrayIn(BYTE,inShape))
434
 
{
435
 
    LogSunlover(("VRDPConsoleCallback::OnMousePointerShapeChange: %d, %d, %lux%lu, @%lu,%lu\n", visible, alpha, width, height, xHot, yHot));
436
 
 
437
 
    if (m_server)
438
 
    {
439
 
        com::SafeArray <BYTE> aShape(ComSafeArrayInArg (inShape));
440
 
        if (aShape.size() == 0)
441
 
        {
442
 
            if (!visible)
443
 
            {
444
 
                m_server->MousePointerHide();
445
 
            }
446
 
        }
447
 
        else if (width != 0 && height != 0)
448
 
        {
449
 
            /* Pointer consists of 1 bpp AND and 24 BPP XOR masks.
450
 
             * 'shape' AND mask followed by XOR mask.
451
 
             * XOR mask contains 32 bit (lsb)BGR0(msb) values.
452
 
             *
453
 
             * We convert this to RDP color format which consist of
454
 
             * one bpp AND mask and 24 BPP (BGR) color XOR image.
455
 
             *
456
 
             * RDP clients expect 8 aligned width and height of
457
 
             * pointer (preferably 32x32).
458
 
             *
459
 
             * They even contain bugs which do not appear for
460
 
             * 32x32 pointers but would appear for a 41x32 one.
461
 
             *
462
 
             * So set pointer size to 32x32. This can be done safely
463
 
             * because most pointers are 32x32.
464
 
             */
465
 
            uint8_t* shape = aShape.raw();
466
 
 
467
 
            dumpPointer(shape, width, height, true);
468
 
 
469
 
            int cbDstAndMask = (((width + 7) / 8) * height + 3) & ~3;
470
 
 
471
 
            uint8_t *pu8AndMask = shape;
472
 
            uint8_t *pu8XorMask = shape + cbDstAndMask;
473
 
 
474
 
            if (alpha)
475
 
            {
476
 
                pu8AndMask = (uint8_t*)alloca(cbDstAndMask);
477
 
 
478
 
                mousePointerGenerateANDMask(pu8AndMask, cbDstAndMask, pu8XorMask, width, height);
479
 
            }
480
 
 
481
 
            /* Windows guest alpha pointers are wider than 32 pixels.
482
 
             * Try to find out the top-left border of the pointer and
483
 
             * then copy only meaningful bits. All complete top rows
484
 
             * and all complete left columns where (AND == 1 && XOR == 0)
485
 
             * are skipped. Hot spot is adjusted.
486
 
             */
487
 
            uint32_t ySkip = 0; /* How many rows to skip at the top. */
488
 
            uint32_t xSkip = 0; /* How many columns to skip at the left. */
489
 
 
490
 
            findTopLeftBorder(pu8AndMask, pu8XorMask, width, height, &xSkip, &ySkip);
491
 
 
492
 
            /* Must not skip the hot spot. */
493
 
            xSkip = RT_MIN(xSkip, xHot);
494
 
            ySkip = RT_MIN(ySkip, yHot);
495
 
 
496
 
            /*
497
 
             * Compute size and allocate memory for the pointer.
498
 
             */
499
 
            const uint32_t dstwidth = 32;
500
 
            const uint32_t dstheight = 32;
501
 
 
502
 
            VRDPCOLORPOINTER *pointer = NULL;
503
 
 
504
 
            uint32_t dstmaskwidth = (dstwidth + 7) / 8;
505
 
 
506
 
            uint32_t rdpmaskwidth = dstmaskwidth;
507
 
            uint32_t rdpmasklen = dstheight * rdpmaskwidth;
508
 
 
509
 
            uint32_t rdpdatawidth = dstwidth * 3;
510
 
            uint32_t rdpdatalen = dstheight * rdpdatawidth;
511
 
 
512
 
            pointer = (VRDPCOLORPOINTER *)RTMemTmpAlloc(sizeof(VRDPCOLORPOINTER) + rdpmasklen + rdpdatalen);
513
 
 
514
 
            if (pointer)
515
 
            {
516
 
                uint8_t *maskarray = (uint8_t*)pointer + sizeof(VRDPCOLORPOINTER);
517
 
                uint8_t *dataarray = maskarray + rdpmasklen;
518
 
 
519
 
                memset(maskarray, 0xFF, rdpmasklen);
520
 
                memset(dataarray, 0x00, rdpdatalen);
521
 
 
522
 
                uint32_t srcmaskwidth = (width + 7) / 8;
523
 
                uint32_t srcdatawidth = width * 4;
524
 
 
525
 
                /* Copy AND mask. */
526
 
                uint8_t *src = pu8AndMask + ySkip * srcmaskwidth;
527
 
                uint8_t *dst = maskarray + (dstheight - 1) * rdpmaskwidth;
528
 
 
529
 
                uint32_t minheight = RT_MIN(height - ySkip, dstheight);
530
 
                uint32_t minwidth = RT_MIN(width - xSkip, dstwidth);
531
 
 
532
 
                unsigned x, y;
533
 
 
534
 
                for (y = 0; y < minheight; y++)
535
 
                {
536
 
                    for (x = 0; x < minwidth; x++)
537
 
                    {
538
 
                        uint32_t byteIndex = (x + xSkip) / 8;
539
 
                        uint32_t bitIndex = (x + xSkip) % 8;
540
 
 
541
 
                        bool bit = (src[byteIndex] & (1 << (7 - bitIndex))) != 0;
542
 
 
543
 
                        if (!bit)
544
 
                        {
545
 
                            byteIndex = x / 8;
546
 
                            bitIndex = x % 8;
547
 
 
548
 
                            dst[byteIndex] &= ~(1 << (7 - bitIndex));
549
 
                        }
550
 
                    }
551
 
 
552
 
                    src += srcmaskwidth;
553
 
                    dst -= rdpmaskwidth;
554
 
                }
555
 
 
556
 
                /* Point src to XOR mask */
557
 
                src = pu8XorMask + ySkip * srcdatawidth;
558
 
                dst = dataarray + (dstheight - 1) * rdpdatawidth;
559
 
 
560
 
                for (y = 0; y < minheight ; y++)
561
 
                {
562
 
                    for (x = 0; x < minwidth; x++)
563
 
                    {
564
 
                        memcpy(dst + x * 3, &src[4 * (x + xSkip)], 3);
565
 
                    }
566
 
 
567
 
                    src += srcdatawidth;
568
 
                    dst -= rdpdatawidth;
569
 
                }
570
 
 
571
 
                pointer->u16HotX = (uint16_t)(xHot - xSkip);
572
 
                pointer->u16HotY = (uint16_t)(yHot - ySkip);
573
 
 
574
 
                pointer->u16Width = (uint16_t)dstwidth;
575
 
                pointer->u16Height = (uint16_t)dstheight;
576
 
 
577
 
                pointer->u16MaskLen = (uint16_t)rdpmasklen;
578
 
                pointer->u16DataLen = (uint16_t)rdpdatalen;
579
 
 
580
 
                dumpPointer((uint8_t*)pointer + sizeof(*pointer), dstwidth, dstheight, false);
581
 
 
582
 
                m_server->MousePointerUpdate(pointer);
583
 
 
584
 
                RTMemTmpFree(pointer);
585
 
            }
586
 
        }
587
 
    }
588
 
 
589
 
    return S_OK;
590
 
}
591
 
 
592
 
 
593
 
// ConsoleVRDPServer
594
 
////////////////////////////////////////////////////////////////////////////////
595
 
 
596
 
#ifdef VBOX_WITH_VRDP
597
 
RTLDRMOD ConsoleVRDPServer::mVRDPLibrary;
598
 
 
599
 
PFNVRDPCREATESERVER ConsoleVRDPServer::mpfnVRDPCreateServer = NULL;
600
 
 
601
 
VRDPENTRYPOINTS_1 *ConsoleVRDPServer::mpEntryPoints = NULL;
602
 
 
603
 
VRDPCALLBACKS_1 ConsoleVRDPServer::mCallbacks =
604
 
{
605
 
    { VRDP_INTERFACE_VERSION_1, sizeof(VRDPCALLBACKS_1) },
606
 
    ConsoleVRDPServer::VRDPCallbackQueryProperty,
607
 
    ConsoleVRDPServer::VRDPCallbackClientLogon,
608
 
    ConsoleVRDPServer::VRDPCallbackClientConnect,
609
 
    ConsoleVRDPServer::VRDPCallbackClientDisconnect,
610
 
    ConsoleVRDPServer::VRDPCallbackIntercept,
611
 
    ConsoleVRDPServer::VRDPCallbackUSB,
612
 
    ConsoleVRDPServer::VRDPCallbackClipboard,
613
 
    ConsoleVRDPServer::VRDPCallbackFramebufferQuery,
614
 
    ConsoleVRDPServer::VRDPCallbackFramebufferLock,
615
 
    ConsoleVRDPServer::VRDPCallbackFramebufferUnlock,
616
 
    ConsoleVRDPServer::VRDPCallbackInput,
617
 
    ConsoleVRDPServer::VRDPCallbackVideoModeHint
618
 
};
619
 
 
620
 
DECLCALLBACK(int)  ConsoleVRDPServer::VRDPCallbackQueryProperty(void *pvCallback, uint32_t index, void *pvBuffer, uint32_t cbBuffer, uint32_t *pcbOut)
621
 
{
622
 
    ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
623
 
 
624
 
    int rc = VERR_NOT_SUPPORTED;
625
 
 
626
 
    switch (index)
627
 
    {
628
 
        case VRDP_QP_NETWORK_PORT:
629
 
        {
630
 
            /* This is obsolete, the VRDP server uses VRDP_QP_NETWORK_PORT_RANGE instead. */
631
 
            ULONG port = 0;
632
 
 
633
 
            if (cbBuffer >= sizeof(uint32_t))
634
 
            {
635
 
                *(uint32_t *)pvBuffer = (uint32_t)port;
636
 
                rc = VINF_SUCCESS;
637
 
            }
638
 
            else
639
 
            {
640
 
                rc = VINF_BUFFER_OVERFLOW;
641
 
            }
642
 
 
643
 
            *pcbOut = sizeof(uint32_t);
644
 
        } break;
645
 
 
646
 
        case VRDP_QP_NETWORK_ADDRESS:
647
 
        {
648
 
            com::Bstr bstr;
649
 
            server->mConsole->getVRDPServer()->COMGETTER(NetAddress)(bstr.asOutParam());
650
 
 
651
 
            /* The server expects UTF8. */
652
 
            com::Utf8Str address = bstr;
653
 
 
654
 
            size_t cbAddress = address.length() + 1;
655
 
 
656
 
            if (cbAddress >= 0x10000)
657
 
            {
658
 
                /* More than 64K seems to be an  invalid address. */
659
 
                rc = VERR_TOO_MUCH_DATA;
660
 
                break;
661
 
            }
662
 
 
663
 
            if ((size_t)cbBuffer >= cbAddress)
664
 
            {
665
 
                if (cbAddress > 0)
666
 
                {
667
 
                    if (address.raw())
668
 
                    {
669
 
                        memcpy(pvBuffer, address.raw(), cbAddress);
670
 
                    }
671
 
                    else
672
 
                    {
673
 
                        /* The value is an empty string. */
674
 
                        *(uint8_t *)pvBuffer = 0;
675
 
                    }
676
 
                }
677
 
 
678
 
                rc = VINF_SUCCESS;
679
 
            }
680
 
            else
681
 
            {
682
 
                rc = VINF_BUFFER_OVERFLOW;
683
 
            }
684
 
 
685
 
            *pcbOut = (uint32_t)cbAddress;
686
 
        } break;
687
 
 
688
 
        case VRDP_QP_NUMBER_MONITORS:
689
 
        {
690
 
            ULONG cMonitors = 1;
691
 
 
692
 
            server->mConsole->machine()->COMGETTER(MonitorCount)(&cMonitors);
693
 
 
694
 
            if (cbBuffer >= sizeof(uint32_t))
695
 
            {
696
 
                *(uint32_t *)pvBuffer = (uint32_t)cMonitors;
697
 
                rc = VINF_SUCCESS;
698
 
            }
699
 
            else
700
 
            {
701
 
                rc = VINF_BUFFER_OVERFLOW;
702
 
            }
703
 
 
704
 
            *pcbOut = sizeof(uint32_t);
705
 
        } break;
706
 
 
707
 
        case VRDP_QP_NETWORK_PORT_RANGE:
708
 
        {
709
 
            com::Bstr bstr;
710
 
            HRESULT hrc = server->mConsole->getVRDPServer()->COMGETTER(Ports)(bstr.asOutParam());
711
 
 
712
 
            if (hrc != S_OK)
713
 
            {
714
 
                bstr = "";
715
 
            }
716
 
 
717
 
            if (bstr == "0")
718
 
            {
719
 
                bstr = "3389";
720
 
            }
721
 
 
722
 
            /* The server expects UTF8. */
723
 
            com::Utf8Str portRange = bstr;
724
 
 
725
 
            size_t cbPortRange = portRange.length () + 1;
726
 
 
727
 
            if (cbPortRange >= 0x10000)
728
 
            {
729
 
                /* More than 64K seems to be an  invalid port range string. */
730
 
                rc = VERR_TOO_MUCH_DATA;
731
 
                break;
732
 
            }
733
 
 
734
 
            if ((size_t)cbBuffer >= cbPortRange)
735
 
            {
736
 
                if (cbPortRange > 0)
737
 
                {
738
 
                    if (portRange.raw())
739
 
                    {
740
 
                        memcpy(pvBuffer, portRange.raw(), cbPortRange);
741
 
                    }
742
 
                    else
743
 
                    {
744
 
                        /* The value is an empty string. */
745
 
                        *(uint8_t *)pvBuffer = 0;
746
 
                    }
747
 
                }
748
 
 
749
 
                rc = VINF_SUCCESS;
750
 
            }
751
 
            else
752
 
            {
753
 
                rc = VINF_BUFFER_OVERFLOW;
754
 
            }
755
 
 
756
 
            *pcbOut = (uint32_t)cbPortRange;
757
 
        } break;
758
 
 
759
 
#ifdef VBOX_WITH_VRDP_VIDEO_CHANNEL
760
 
        case VRDP_QP_VIDEO_CHANNEL:
761
 
        {
762
 
            BOOL fVideoEnabled = FALSE;
763
 
 
764
 
            server->mConsole->getVRDPServer()->COMGETTER(VideoChannel)(&fVideoEnabled);
765
 
 
766
 
            if (cbBuffer >= sizeof(uint32_t))
767
 
            {
768
 
                *(uint32_t *)pvBuffer = (uint32_t)fVideoEnabled;
769
 
                rc = VINF_SUCCESS;
770
 
            }
771
 
            else
772
 
            {
773
 
                rc = VINF_BUFFER_OVERFLOW;
774
 
            }
775
 
 
776
 
            *pcbOut = sizeof(uint32_t);
777
 
        } break;
778
 
 
779
 
        case VRDP_QP_VIDEO_CHANNEL_QUALITY:
780
 
        {
781
 
            ULONG ulQuality = 0;
782
 
 
783
 
            server->mConsole->getVRDPServer()->COMGETTER(VideoChannelQuality)(&ulQuality);
784
 
 
785
 
            if (cbBuffer >= sizeof(uint32_t))
786
 
            {
787
 
                *(uint32_t *)pvBuffer = (uint32_t)ulQuality;
788
 
                rc = VINF_SUCCESS;
789
 
            }
790
 
            else
791
 
            {
792
 
                rc = VINF_BUFFER_OVERFLOW;
793
 
            }
794
 
 
795
 
            *pcbOut = sizeof(uint32_t);
796
 
        } break;
797
 
 
798
 
        case VRDP_QP_VIDEO_CHANNEL_SUNFLSH:
799
 
        {
800
 
            ULONG ulSunFlsh = 1;
801
 
 
802
 
            com::Bstr bstr;
803
 
            HRESULT hrc = server->mConsole->machine ()->GetExtraData(Bstr("VRDP/SunFlsh"), bstr.asOutParam());
804
 
            if (hrc == S_OK && !bstr.isEmpty())
805
 
            {
806
 
                com::Utf8Str sunFlsh = bstr;
807
 
                if (!sunFlsh.isEmpty())
808
 
                {
809
 
                    ulSunFlsh = sunFlsh.toUInt32();
810
 
                }
811
 
            }
812
 
 
813
 
            if (cbBuffer >= sizeof(uint32_t))
814
 
            {
815
 
                *(uint32_t *)pvBuffer = (uint32_t)ulSunFlsh;
816
 
                rc = VINF_SUCCESS;
817
 
            }
818
 
            else
819
 
            {
820
 
                rc = VINF_BUFFER_OVERFLOW;
821
 
            }
822
 
 
823
 
            *pcbOut = sizeof(uint32_t);
824
 
        } break;
825
 
#endif /* VBOX_WITH_VRDP_VIDEO_CHANNEL */
826
 
 
827
 
        case VRDP_QP_FEATURE:
828
 
        {
829
 
            if (cbBuffer < sizeof(VRDPFEATURE))
830
 
            {
831
 
                rc = VERR_INVALID_PARAMETER;
832
 
                break;
833
 
            }
834
 
 
835
 
            size_t cbInfo = cbBuffer - RT_OFFSETOF(VRDPFEATURE, achInfo);
836
 
 
837
 
            VRDPFEATURE *pFeature = (VRDPFEATURE *)pvBuffer;
838
 
 
839
 
            size_t cchInfo = 0;
840
 
            rc = RTStrNLenEx(pFeature->achInfo, cbInfo, &cchInfo);
841
 
 
842
 
            if (RT_FAILURE(rc))
843
 
            {
844
 
                rc = VERR_INVALID_PARAMETER;
845
 
                break;
846
 
            }
847
 
 
848
 
            /* features are mapped to "VRDP/Feature/NAME" extra data. */
849
 
            com::Utf8Str extraData("VRDP/Feature/");
850
 
            extraData.append(com::Utf8Str(pFeature->achInfo));
851
 
 
852
 
            com::Bstr bstrValue;
853
 
 
854
 
            /* @todo these features should be per client. */
855
 
            NOREF(pFeature->u32ClientId);
856
 
 
857
 
            if (   RTStrICmp(pFeature->achInfo, "Client/DisableDisplay") == 0
858
 
                || RTStrICmp(pFeature->achInfo, "Client/DisableInput") == 0
859
 
                || RTStrICmp(pFeature->achInfo, "Client/DisableAudio") == 0
860
 
                || RTStrICmp(pFeature->achInfo, "Client/DisableUSB") == 0
861
 
                || RTStrICmp(pFeature->achInfo, "Client/DisableClipboard") == 0
862
 
               )
863
 
            {
864
 
                HRESULT hrc = server->mConsole->machine ()->GetExtraData(com::Bstr(extraData), bstrValue.asOutParam());
865
 
                if (hrc == S_OK && !bstrValue.isEmpty())
866
 
                {
867
 
                    rc = VINF_SUCCESS;
868
 
                }
869
 
            }
870
 
            else
871
 
            {
872
 
                rc = VERR_NOT_SUPPORTED;
873
 
            }
874
 
 
875
 
            /* Copy the value string to the callers buffer. */
876
 
            if (rc == VINF_SUCCESS)
877
 
            {
878
 
                com::Utf8Str value = bstrValue;
879
 
 
880
 
                size_t cb = value.length() + 1;
881
 
 
882
 
                if ((size_t)cbInfo >= cb)
883
 
                {
884
 
                    memcpy(pFeature->achInfo, value.c_str(), cb);
885
 
                }
886
 
                else
887
 
                {
888
 
                    rc = VINF_BUFFER_OVERFLOW;
889
 
                }
890
 
 
891
 
                *pcbOut = (uint32_t)cb;
892
 
            }
893
 
        } break;
894
 
 
895
 
        case VRDP_SP_NETWORK_BIND_PORT:
896
 
        {
897
 
            if (cbBuffer != sizeof(uint32_t))
898
 
            {
899
 
                rc = VERR_INVALID_PARAMETER;
900
 
                break;
901
 
            }
902
 
 
903
 
            ULONG port = *(uint32_t *)pvBuffer;
904
 
 
905
 
            server->mVRDPBindPort = port;
906
 
 
907
 
            rc = VINF_SUCCESS;
908
 
 
909
 
            if (pcbOut)
910
 
            {
911
 
                *pcbOut = sizeof(uint32_t);
912
 
            }
913
 
 
914
 
            server->mConsole->onRemoteDisplayInfoChange();
915
 
        } break;
916
 
 
917
 
        default:
918
 
            break;
919
 
    }
920
 
 
921
 
    return rc;
922
 
}
923
 
 
924
 
DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackClientLogon(void *pvCallback, uint32_t u32ClientId, const char *pszUser, const char *pszPassword, const char *pszDomain)
925
 
{
926
 
    ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
927
 
 
928
 
    return server->mConsole->VRDPClientLogon (u32ClientId, pszUser, pszPassword, pszDomain);
929
 
}
930
 
 
931
 
DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackClientConnect (void *pvCallback, uint32_t u32ClientId)
932
 
{
933
 
    ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
934
 
 
935
 
    server->mConsole->VRDPClientConnect (u32ClientId);
936
 
}
937
 
 
938
 
DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackClientDisconnect (void *pvCallback, uint32_t u32ClientId, uint32_t fu32Intercepted)
939
 
{
940
 
    ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
941
 
 
942
 
    server->mConsole->VRDPClientDisconnect (u32ClientId, fu32Intercepted);
943
 
}
944
 
 
945
 
DECLCALLBACK(int)  ConsoleVRDPServer::VRDPCallbackIntercept (void *pvCallback, uint32_t u32ClientId, uint32_t fu32Intercept, void **ppvIntercept)
946
 
{
947
 
    ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
948
 
 
949
 
    LogFlowFunc(("%x\n", fu32Intercept));
950
 
 
951
 
    int rc = VERR_NOT_SUPPORTED;
952
 
 
953
 
    switch (fu32Intercept)
954
 
    {
955
 
        case VRDP_CLIENT_INTERCEPT_AUDIO:
956
 
        {
957
 
            server->mConsole->VRDPInterceptAudio (u32ClientId);
958
 
            if (ppvIntercept)
959
 
            {
960
 
                *ppvIntercept = server;
961
 
            }
962
 
            rc = VINF_SUCCESS;
963
 
        } break;
964
 
 
965
 
        case VRDP_CLIENT_INTERCEPT_USB:
966
 
        {
967
 
            server->mConsole->VRDPInterceptUSB (u32ClientId, ppvIntercept);
968
 
            rc = VINF_SUCCESS;
969
 
        } break;
970
 
 
971
 
        case VRDP_CLIENT_INTERCEPT_CLIPBOARD:
972
 
        {
973
 
            server->mConsole->VRDPInterceptClipboard (u32ClientId);
974
 
            if (ppvIntercept)
975
 
            {
976
 
                *ppvIntercept = server;
977
 
            }
978
 
            rc = VINF_SUCCESS;
979
 
        } break;
980
 
 
981
 
        default:
982
 
            break;
983
 
    }
984
 
 
985
 
    return rc;
986
 
}
987
 
 
988
 
DECLCALLBACK(int)  ConsoleVRDPServer::VRDPCallbackUSB (void *pvCallback, void *pvIntercept, uint32_t u32ClientId, uint8_t u8Code, const void *pvRet, uint32_t cbRet)
989
 
{
990
 
#ifdef VBOX_WITH_USB
991
 
    return USBClientResponseCallback (pvIntercept, u32ClientId, u8Code, pvRet, cbRet);
992
 
#else
993
 
    return VERR_NOT_SUPPORTED;
994
 
#endif
995
 
}
996
 
 
997
 
DECLCALLBACK(int)  ConsoleVRDPServer::VRDPCallbackClipboard (void *pvCallback, void *pvIntercept, uint32_t u32ClientId, uint32_t u32Function, uint32_t u32Format, const void *pvData, uint32_t cbData)
998
 
{
999
 
    return ClipboardCallback (pvIntercept, u32ClientId, u32Function, u32Format, pvData, cbData);
1000
 
}
1001
 
 
1002
 
DECLCALLBACK(bool) ConsoleVRDPServer::VRDPCallbackFramebufferQuery (void *pvCallback, unsigned uScreenId, VRDPFRAMEBUFFERINFO *pInfo)
1003
 
{
1004
 
    ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1005
 
 
1006
 
    bool fAvailable = false;
1007
 
 
1008
 
    IFramebuffer *pfb = NULL;
1009
 
    LONG xOrigin = 0;
1010
 
    LONG yOrigin = 0;
1011
 
 
1012
 
    server->mConsole->getDisplay ()->GetFramebuffer (uScreenId, &pfb, &xOrigin, &yOrigin);
1013
 
 
1014
 
    if (pfb)
1015
 
    {
1016
 
        pfb->Lock ();
1017
 
 
1018
 
        /* Query framebuffer parameters. */
1019
 
        ULONG lineSize = 0;
1020
 
        pfb->COMGETTER(BytesPerLine) (&lineSize);
1021
 
 
1022
 
        ULONG bitsPerPixel = 0;
1023
 
        pfb->COMGETTER(BitsPerPixel) (&bitsPerPixel);
1024
 
 
1025
 
        BYTE *address = NULL;
1026
 
        pfb->COMGETTER(Address) (&address);
1027
 
 
1028
 
        ULONG height = 0;
1029
 
        pfb->COMGETTER(Height) (&height);
1030
 
 
1031
 
        ULONG width = 0;
1032
 
        pfb->COMGETTER(Width) (&width);
1033
 
 
1034
 
        /* Now fill the information as requested by the caller. */
1035
 
        pInfo->pu8Bits = address;
1036
 
        pInfo->xOrigin = xOrigin;
1037
 
        pInfo->yOrigin = yOrigin;
1038
 
        pInfo->cWidth = width;
1039
 
        pInfo->cHeight = height;
1040
 
        pInfo->cBitsPerPixel = bitsPerPixel;
1041
 
        pInfo->cbLine = lineSize;
1042
 
 
1043
 
        pfb->Unlock ();
1044
 
 
1045
 
        fAvailable = true;
1046
 
    }
1047
 
 
1048
 
    if (server->maFramebuffers[uScreenId])
1049
 
    {
1050
 
        server->maFramebuffers[uScreenId]->Release ();
1051
 
    }
1052
 
    server->maFramebuffers[uScreenId] = pfb;
1053
 
 
1054
 
    return fAvailable;
1055
 
}
1056
 
 
1057
 
DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackFramebufferLock (void *pvCallback, unsigned uScreenId)
1058
 
{
1059
 
    ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1060
 
 
1061
 
    if (server->maFramebuffers[uScreenId])
1062
 
    {
1063
 
        server->maFramebuffers[uScreenId]->Lock ();
1064
 
    }
1065
 
}
1066
 
 
1067
 
DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackFramebufferUnlock (void *pvCallback, unsigned uScreenId)
1068
 
{
1069
 
    ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1070
 
 
1071
 
    if (server->maFramebuffers[uScreenId])
1072
 
    {
1073
 
        server->maFramebuffers[uScreenId]->Unlock ();
1074
 
    }
1075
 
}
1076
 
 
1077
 
static void fixKbdLockStatus (VRDPInputSynch *pInputSynch, IKeyboard *pKeyboard)
1078
 
{
1079
 
    if (   pInputSynch->cGuestNumLockAdaptions
1080
 
        && (pInputSynch->fGuestNumLock != pInputSynch->fClientNumLock))
1081
 
    {
1082
 
        pInputSynch->cGuestNumLockAdaptions--;
1083
 
        pKeyboard->PutScancode(0x45);
1084
 
        pKeyboard->PutScancode(0x45 | 0x80);
1085
 
    }
1086
 
    if (   pInputSynch->cGuestCapsLockAdaptions
1087
 
        && (pInputSynch->fGuestCapsLock != pInputSynch->fClientCapsLock))
1088
 
    {
1089
 
        pInputSynch->cGuestCapsLockAdaptions--;
1090
 
        pKeyboard->PutScancode(0x3a);
1091
 
        pKeyboard->PutScancode(0x3a | 0x80);
1092
 
    }
1093
 
}
1094
 
 
1095
 
DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackInput (void *pvCallback, int type, const void *pvInput, unsigned cbInput)
1096
 
{
1097
 
    ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1098
 
    Console *pConsole = server->mConsole;
1099
 
 
1100
 
    switch (type)
1101
 
    {
1102
 
        case VRDP_INPUT_SCANCODE:
1103
 
        {
1104
 
            if (cbInput == sizeof (VRDPINPUTSCANCODE))
1105
 
            {
1106
 
                IKeyboard *pKeyboard = pConsole->getKeyboard ();
1107
 
 
1108
 
                const VRDPINPUTSCANCODE *pInputScancode = (VRDPINPUTSCANCODE *)pvInput;
1109
 
 
1110
 
                /* Track lock keys. */
1111
 
                if (pInputScancode->uScancode == 0x45)
1112
 
                {
1113
 
                    server->m_InputSynch.fClientNumLock = !server->m_InputSynch.fClientNumLock;
1114
 
                }
1115
 
                else if (pInputScancode->uScancode == 0x3a)
1116
 
                {
1117
 
                    server->m_InputSynch.fClientCapsLock = !server->m_InputSynch.fClientCapsLock;
1118
 
                }
1119
 
                else if (pInputScancode->uScancode == 0x46)
1120
 
                {
1121
 
                    server->m_InputSynch.fClientScrollLock = !server->m_InputSynch.fClientScrollLock;
1122
 
                }
1123
 
                else if ((pInputScancode->uScancode & 0x80) == 0)
1124
 
                {
1125
 
                    /* Key pressed. */
1126
 
                    fixKbdLockStatus (&server->m_InputSynch, pKeyboard);
1127
 
                }
1128
 
 
1129
 
                pKeyboard->PutScancode((LONG)pInputScancode->uScancode);
1130
 
            }
1131
 
        } break;
1132
 
 
1133
 
        case VRDP_INPUT_POINT:
1134
 
        {
1135
 
            if (cbInput == sizeof (VRDPINPUTPOINT))
1136
 
            {
1137
 
                const VRDPINPUTPOINT *pInputPoint = (VRDPINPUTPOINT *)pvInput;
1138
 
 
1139
 
                int mouseButtons = 0;
1140
 
                int iWheel = 0;
1141
 
 
1142
 
                if (pInputPoint->uButtons & VRDP_INPUT_POINT_BUTTON1)
1143
 
                {
1144
 
                    mouseButtons |= MouseButtonState_LeftButton;
1145
 
                }
1146
 
                if (pInputPoint->uButtons & VRDP_INPUT_POINT_BUTTON2)
1147
 
                {
1148
 
                    mouseButtons |= MouseButtonState_RightButton;
1149
 
                }
1150
 
                if (pInputPoint->uButtons & VRDP_INPUT_POINT_BUTTON3)
1151
 
                {
1152
 
                    mouseButtons |= MouseButtonState_MiddleButton;
1153
 
                }
1154
 
                if (pInputPoint->uButtons & VRDP_INPUT_POINT_WHEEL_UP)
1155
 
                {
1156
 
                    mouseButtons |= MouseButtonState_WheelUp;
1157
 
                    iWheel = -1;
1158
 
                }
1159
 
                if (pInputPoint->uButtons & VRDP_INPUT_POINT_WHEEL_DOWN)
1160
 
                {
1161
 
                    mouseButtons |= MouseButtonState_WheelDown;
1162
 
                    iWheel = 1;
1163
 
                }
1164
 
 
1165
 
                if (server->m_fGuestWantsAbsolute)
1166
 
                {
1167
 
                    pConsole->getMouse()->PutMouseEventAbsolute (pInputPoint->x + 1, pInputPoint->y + 1, iWheel, 0 /* Horizontal wheel */, mouseButtons);
1168
 
                } else
1169
 
                {
1170
 
                    pConsole->getMouse()->PutMouseEvent (pInputPoint->x - server->m_mousex,
1171
 
                                                         pInputPoint->y - server->m_mousey,
1172
 
                                                         iWheel, 0 /* Horizontal wheel */, mouseButtons);
1173
 
                    server->m_mousex = pInputPoint->x;
1174
 
                    server->m_mousey = pInputPoint->y;
1175
 
                }
1176
 
            }
1177
 
        } break;
1178
 
 
1179
 
        case VRDP_INPUT_CAD:
1180
 
        {
1181
 
            pConsole->getKeyboard ()->PutCAD();
1182
 
        } break;
1183
 
 
1184
 
        case VRDP_INPUT_RESET:
1185
 
        {
1186
 
            pConsole->Reset();
1187
 
        } break;
1188
 
 
1189
 
        case VRDP_INPUT_SYNCH:
1190
 
        {
1191
 
            if (cbInput == sizeof (VRDPINPUTSYNCH))
1192
 
            {
1193
 
                IKeyboard *pKeyboard = pConsole->getKeyboard ();
1194
 
 
1195
 
                const VRDPINPUTSYNCH *pInputSynch = (VRDPINPUTSYNCH *)pvInput;
1196
 
 
1197
 
                server->m_InputSynch.fClientNumLock    = (pInputSynch->uLockStatus & VRDP_INPUT_SYNCH_NUMLOCK) != 0;
1198
 
                server->m_InputSynch.fClientCapsLock   = (pInputSynch->uLockStatus & VRDP_INPUT_SYNCH_CAPITAL) != 0;
1199
 
                server->m_InputSynch.fClientScrollLock = (pInputSynch->uLockStatus & VRDP_INPUT_SYNCH_SCROLL) != 0;
1200
 
 
1201
 
                /* The client initiated synchronization. Always make the guest to reflect the client state.
1202
 
                 * Than means, when the guest changes the state itself, it is forced to return to the client
1203
 
                 * state.
1204
 
                 */
1205
 
                if (server->m_InputSynch.fClientNumLock != server->m_InputSynch.fGuestNumLock)
1206
 
                {
1207
 
                    server->m_InputSynch.cGuestNumLockAdaptions = 2;
1208
 
                }
1209
 
 
1210
 
                if (server->m_InputSynch.fClientCapsLock != server->m_InputSynch.fGuestCapsLock)
1211
 
                {
1212
 
                    server->m_InputSynch.cGuestCapsLockAdaptions = 2;
1213
 
                }
1214
 
 
1215
 
                fixKbdLockStatus (&server->m_InputSynch, pKeyboard);
1216
 
            }
1217
 
        } break;
1218
 
 
1219
 
        default:
1220
 
            break;
1221
 
    }
1222
 
}
1223
 
 
1224
 
DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackVideoModeHint (void *pvCallback, unsigned cWidth, unsigned cHeight, unsigned cBitsPerPixel, unsigned uScreenId)
1225
 
{
1226
 
    ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1227
 
 
1228
 
    server->mConsole->getDisplay()->SetVideoModeHint(cWidth, cHeight, cBitsPerPixel, uScreenId);
1229
 
}
1230
 
#endif /* VBOX_WITH_VRDP */
1231
 
 
1232
 
ConsoleVRDPServer::ConsoleVRDPServer (Console *console)
1233
 
{
1234
 
    mConsole = console;
1235
 
 
1236
 
    int rc = RTCritSectInit(&mCritSect);
1237
 
    AssertRC(rc);
1238
 
 
1239
 
    mcClipboardRefs = 0;
1240
 
    mpfnClipboardCallback = NULL;
1241
 
 
1242
 
#ifdef VBOX_WITH_USB
1243
 
    mUSBBackends.pHead = NULL;
1244
 
    mUSBBackends.pTail = NULL;
1245
 
 
1246
 
    mUSBBackends.thread = NIL_RTTHREAD;
1247
 
    mUSBBackends.fThreadRunning = false;
1248
 
    mUSBBackends.event = 0;
1249
 
#endif
1250
 
 
1251
 
#ifdef VBOX_WITH_VRDP
1252
 
    mhServer = 0;
1253
 
 
1254
 
    m_fGuestWantsAbsolute = false;
1255
 
    m_mousex = 0;
1256
 
    m_mousey = 0;
1257
 
 
1258
 
    m_InputSynch.cGuestNumLockAdaptions = 2;
1259
 
    m_InputSynch.cGuestCapsLockAdaptions = 2;
1260
 
 
1261
 
    m_InputSynch.fGuestNumLock    = false;
1262
 
    m_InputSynch.fGuestCapsLock   = false;
1263
 
    m_InputSynch.fGuestScrollLock = false;
1264
 
 
1265
 
    m_InputSynch.fClientNumLock    = false;
1266
 
    m_InputSynch.fClientCapsLock   = false;
1267
 
    m_InputSynch.fClientScrollLock = false;
1268
 
 
1269
 
    memset (maFramebuffers, 0, sizeof (maFramebuffers));
1270
 
 
1271
 
    mConsoleCallback = new VRDPConsoleCallback(this);
1272
 
    mConsoleCallback->AddRef();
1273
 
    console->RegisterCallback(mConsoleCallback);
1274
 
 
1275
 
    mVRDPBindPort = -1;
1276
 
#endif /* VBOX_WITH_VRDP */
1277
 
 
1278
 
    mAuthLibrary = 0;
1279
 
}
1280
 
 
1281
 
ConsoleVRDPServer::~ConsoleVRDPServer ()
1282
 
{
1283
 
    Stop ();
1284
 
 
1285
 
#ifdef VBOX_WITH_VRDP
1286
 
    if (mConsoleCallback)
1287
 
    {
1288
 
        mConsole->UnregisterCallback(mConsoleCallback);
1289
 
        mConsoleCallback->Release();
1290
 
        mConsoleCallback = NULL;
1291
 
    }
1292
 
 
1293
 
    unsigned i;
1294
 
    for (i = 0; i < RT_ELEMENTS(maFramebuffers); i++)
1295
 
    {
1296
 
        if (maFramebuffers[i])
1297
 
        {
1298
 
            maFramebuffers[i]->Release ();
1299
 
            maFramebuffers[i] = NULL;
1300
 
        }
1301
 
    }
1302
 
#endif /* VBOX_WITH_VRDP */
1303
 
 
1304
 
    if (RTCritSectIsInitialized (&mCritSect))
1305
 
    {
1306
 
        RTCritSectDelete (&mCritSect);
1307
 
        memset (&mCritSect, 0, sizeof (mCritSect));
1308
 
    }
1309
 
}
1310
 
 
1311
 
int ConsoleVRDPServer::Launch (void)
1312
 
{
1313
 
    LogFlowThisFunc(("\n"));
1314
 
#ifdef VBOX_WITH_VRDP
1315
 
    int rc = VINF_SUCCESS;
1316
 
    IVRDPServer *vrdpserver = mConsole->getVRDPServer ();
1317
 
    Assert(vrdpserver);
1318
 
    BOOL vrdpEnabled = FALSE;
1319
 
 
1320
 
    HRESULT rc2 = vrdpserver->COMGETTER(Enabled) (&vrdpEnabled);
1321
 
    AssertComRC(rc2);
1322
 
 
1323
 
    if (SUCCEEDED(rc2) && vrdpEnabled)
1324
 
    {
1325
 
        if (loadVRDPLibrary ())
1326
 
        {
1327
 
            rc = mpfnVRDPCreateServer (&mCallbacks.header, this, (VRDPINTERFACEHDR **)&mpEntryPoints, &mhServer);
1328
 
 
1329
 
            if (RT_SUCCESS(rc))
1330
 
            {
1331
 
#ifdef VBOX_WITH_USB
1332
 
                remoteUSBThreadStart ();
1333
 
#endif /* VBOX_WITH_USB */
1334
 
            }
1335
 
            else if (rc != VERR_NET_ADDRESS_IN_USE)
1336
 
                AssertMsgFailed(("Could not start VRDP server: rc = %Rrc\n", rc));
1337
 
        }
1338
 
        else
1339
 
        {
1340
 
            AssertMsgFailed(("Could not load the VRDP library\n"));
1341
 
            rc = VERR_FILE_NOT_FOUND;
1342
 
        }
1343
 
    }
1344
 
#else
1345
 
    int rc = VERR_NOT_SUPPORTED;
1346
 
    LogRel(("VRDP: this version does not include the VRDP server.\n"));
1347
 
#endif /* VBOX_WITH_VRDP */
1348
 
    return rc;
1349
 
}
1350
 
 
1351
 
void ConsoleVRDPServer::EnableConnections (void)
1352
 
{
1353
 
#ifdef VBOX_WITH_VRDP
1354
 
    if (mpEntryPoints && mhServer)
1355
 
    {
1356
 
        mpEntryPoints->VRDPEnableConnections (mhServer, true);
1357
 
    }
1358
 
#endif /* VBOX_WITH_VRDP */
1359
 
}
1360
 
 
1361
 
void ConsoleVRDPServer::DisconnectClient (uint32_t u32ClientId, bool fReconnect)
1362
 
{
1363
 
#ifdef VBOX_WITH_VRDP
1364
 
    if (mpEntryPoints && mhServer)
1365
 
    {
1366
 
        mpEntryPoints->VRDPDisconnect (mhServer, u32ClientId, fReconnect);
1367
 
    }
1368
 
#endif /* VBOX_WITH_VRDP */
1369
 
}
1370
 
 
1371
 
void ConsoleVRDPServer::MousePointerUpdate (const VRDPCOLORPOINTER *pPointer)
1372
 
{
1373
 
#ifdef VBOX_WITH_VRDP
1374
 
    if (mpEntryPoints && mhServer)
1375
 
    {
1376
 
        mpEntryPoints->VRDPColorPointer (mhServer, pPointer);
1377
 
    }
1378
 
#endif /* VBOX_WITH_VRDP */
1379
 
}
1380
 
 
1381
 
void ConsoleVRDPServer::MousePointerHide (void)
1382
 
{
1383
 
#ifdef VBOX_WITH_VRDP
1384
 
    if (mpEntryPoints && mhServer)
1385
 
    {
1386
 
        mpEntryPoints->VRDPHidePointer (mhServer);
1387
 
    }
1388
 
#endif /* VBOX_WITH_VRDP */
1389
 
}
1390
 
 
1391
 
void ConsoleVRDPServer::Stop (void)
1392
 
{
1393
 
    Assert(VALID_PTR(this)); /** @todo r=bird: there are(/was) some odd cases where this buster was invalid on
1394
 
                              * linux. Just remove this when it's 100% sure that problem has been fixed. */
1395
 
#ifdef VBOX_WITH_VRDP
1396
 
    if (mhServer)
1397
 
    {
1398
 
        HVRDPSERVER hServer = mhServer;
1399
 
 
1400
 
        /* Reset the handle to avoid further calls to the server. */
1401
 
        mhServer = 0;
1402
 
 
1403
 
        if (mpEntryPoints && hServer)
1404
 
        {
1405
 
            mpEntryPoints->VRDPDestroy (hServer);
1406
 
        }
1407
 
    }
1408
 
#endif /* VBOX_WITH_VRDP */
1409
 
 
1410
 
#ifdef VBOX_WITH_USB
1411
 
    remoteUSBThreadStop ();
1412
 
#endif /* VBOX_WITH_USB */
1413
 
 
1414
 
    mpfnAuthEntry = NULL;
1415
 
    mpfnAuthEntry2 = NULL;
1416
 
 
1417
 
    if (mAuthLibrary)
1418
 
    {
1419
 
        RTLdrClose(mAuthLibrary);
1420
 
        mAuthLibrary = 0;
1421
 
    }
1422
 
}
1423
 
 
1424
 
/* Worker thread for Remote USB. The thread polls the clients for
1425
 
 * the list of attached USB devices.
1426
 
 * The thread is also responsible for attaching/detaching devices
1427
 
 * to/from the VM.
1428
 
 *
1429
 
 * It is expected that attaching/detaching is not a frequent operation.
1430
 
 *
1431
 
 * The thread is always running when the VRDP server is active.
1432
 
 *
1433
 
 * The thread scans backends and requests the device list every 2 seconds.
1434
 
 *
1435
 
 * When device list is available, the thread calls the Console to process it.
1436
 
 *
1437
 
 */
1438
 
#define VRDP_DEVICE_LIST_PERIOD_MS (2000)
1439
 
 
1440
 
#ifdef VBOX_WITH_USB
1441
 
static DECLCALLBACK(int) threadRemoteUSB (RTTHREAD self, void *pvUser)
1442
 
{
1443
 
    ConsoleVRDPServer *pOwner = (ConsoleVRDPServer *)pvUser;
1444
 
 
1445
 
    LogFlow(("Console::threadRemoteUSB: start. owner = %p.\n", pOwner));
1446
 
 
1447
 
    pOwner->notifyRemoteUSBThreadRunning (self);
1448
 
 
1449
 
    while (pOwner->isRemoteUSBThreadRunning ())
1450
 
    {
1451
 
        RemoteUSBBackend *pRemoteUSBBackend = NULL;
1452
 
 
1453
 
        while ((pRemoteUSBBackend = pOwner->usbBackendGetNext (pRemoteUSBBackend)) != NULL)
1454
 
        {
1455
 
            pRemoteUSBBackend->PollRemoteDevices ();
1456
 
        }
1457
 
 
1458
 
        pOwner->waitRemoteUSBThreadEvent (VRDP_DEVICE_LIST_PERIOD_MS);
1459
 
 
1460
 
        LogFlow(("Console::threadRemoteUSB: iteration. owner = %p.\n", pOwner));
1461
 
    }
1462
 
 
1463
 
    return VINF_SUCCESS;
1464
 
}
1465
 
 
1466
 
void ConsoleVRDPServer::notifyRemoteUSBThreadRunning (RTTHREAD thread)
1467
 
{
1468
 
    mUSBBackends.thread = thread;
1469
 
    mUSBBackends.fThreadRunning = true;
1470
 
    int rc = RTThreadUserSignal (thread);
1471
 
    AssertRC(rc);
1472
 
}
1473
 
 
1474
 
bool ConsoleVRDPServer::isRemoteUSBThreadRunning (void)
1475
 
{
1476
 
    return mUSBBackends.fThreadRunning;
1477
 
}
1478
 
 
1479
 
void ConsoleVRDPServer::waitRemoteUSBThreadEvent (RTMSINTERVAL cMillies)
1480
 
{
1481
 
    int rc = RTSemEventWait (mUSBBackends.event, cMillies);
1482
 
    Assert (RT_SUCCESS(rc) || rc == VERR_TIMEOUT);
1483
 
    NOREF(rc);
1484
 
}
1485
 
 
1486
 
void ConsoleVRDPServer::remoteUSBThreadStart (void)
1487
 
{
1488
 
    int rc = RTSemEventCreate (&mUSBBackends.event);
1489
 
 
1490
 
    if (RT_FAILURE(rc))
1491
 
    {
1492
 
        AssertFailed ();
1493
 
        mUSBBackends.event = 0;
1494
 
    }
1495
 
 
1496
 
    if (RT_SUCCESS(rc))
1497
 
    {
1498
 
        rc = RTThreadCreate (&mUSBBackends.thread, threadRemoteUSB, this, 65536,
1499
 
                             RTTHREADTYPE_VRDP_IO, RTTHREADFLAGS_WAITABLE, "remote usb");
1500
 
    }
1501
 
 
1502
 
    if (RT_FAILURE(rc))
1503
 
    {
1504
 
        LogRel(("Warning: could not start the remote USB thread, rc = %Rrc!!!\n", rc));
1505
 
        mUSBBackends.thread = NIL_RTTHREAD;
1506
 
    }
1507
 
    else
1508
 
    {
1509
 
        /* Wait until the thread is ready. */
1510
 
        rc = RTThreadUserWait (mUSBBackends.thread, 60000);
1511
 
        AssertRC(rc);
1512
 
        Assert (mUSBBackends.fThreadRunning || RT_FAILURE(rc));
1513
 
    }
1514
 
}
1515
 
 
1516
 
void ConsoleVRDPServer::remoteUSBThreadStop (void)
1517
 
{
1518
 
    mUSBBackends.fThreadRunning = false;
1519
 
 
1520
 
    if (mUSBBackends.thread != NIL_RTTHREAD)
1521
 
    {
1522
 
        Assert (mUSBBackends.event != 0);
1523
 
 
1524
 
        RTSemEventSignal (mUSBBackends.event);
1525
 
 
1526
 
        int rc = RTThreadWait (mUSBBackends.thread, 60000, NULL);
1527
 
        AssertRC(rc);
1528
 
 
1529
 
        mUSBBackends.thread = NIL_RTTHREAD;
1530
 
    }
1531
 
 
1532
 
    if (mUSBBackends.event)
1533
 
    {
1534
 
        RTSemEventDestroy (mUSBBackends.event);
1535
 
        mUSBBackends.event = 0;
1536
 
    }
1537
 
}
1538
 
#endif /* VBOX_WITH_USB */
1539
 
 
1540
 
VRDPAuthResult ConsoleVRDPServer::Authenticate (const Guid &uuid, VRDPAuthGuestJudgement guestJudgement,
1541
 
                                                const char *pszUser, const char *pszPassword, const char *pszDomain,
1542
 
                                                uint32_t u32ClientId)
1543
 
{
1544
 
    VRDPAUTHUUID rawuuid;
1545
 
 
1546
 
    memcpy (rawuuid, ((Guid &)uuid).ptr (), sizeof (rawuuid));
1547
 
 
1548
 
    LogFlow(("ConsoleVRDPServer::Authenticate: uuid = %RTuuid, guestJudgement = %d, pszUser = %s, pszPassword = %s, pszDomain = %s, u32ClientId = %d\n",
1549
 
             rawuuid, guestJudgement, pszUser, pszPassword, pszDomain, u32ClientId));
1550
 
 
1551
 
    /*
1552
 
     * Called only from VRDP input thread. So thread safety is not required.
1553
 
     */
1554
 
 
1555
 
    if (!mAuthLibrary)
1556
 
    {
1557
 
        /* Load the external authentication library. */
1558
 
 
1559
 
        ComPtr<IMachine> machine;
1560
 
        mConsole->COMGETTER(Machine)(machine.asOutParam());
1561
 
 
1562
 
        ComPtr<IVirtualBox> virtualBox;
1563
 
        machine->COMGETTER(Parent)(virtualBox.asOutParam());
1564
 
 
1565
 
        ComPtr<ISystemProperties> systemProperties;
1566
 
        virtualBox->COMGETTER(SystemProperties)(systemProperties.asOutParam());
1567
 
 
1568
 
        Bstr authLibrary;
1569
 
        systemProperties->COMGETTER(RemoteDisplayAuthLibrary)(authLibrary.asOutParam());
1570
 
 
1571
 
        Utf8Str filename = authLibrary;
1572
 
 
1573
 
        LogRel(("VRDPAUTH: ConsoleVRDPServer::Authenticate: loading external authentication library '%ls'\n", authLibrary.raw()));
1574
 
 
1575
 
        int rc;
1576
 
        if (RTPathHavePath(filename.raw()))
1577
 
            rc = RTLdrLoad(filename.raw(), &mAuthLibrary);
1578
 
        else
1579
 
            rc = RTLdrLoadAppPriv(filename.raw(), &mAuthLibrary);
1580
 
 
1581
 
        if (RT_FAILURE(rc))
1582
 
            LogRel(("VRDPAUTH: Failed to load external authentication library. Error code: %Rrc\n", rc));
1583
 
 
1584
 
        if (RT_SUCCESS(rc))
1585
 
        {
1586
 
            /* Get the entry point. */
1587
 
            mpfnAuthEntry2 = NULL;
1588
 
            int rc2 = RTLdrGetSymbol(mAuthLibrary, "VRDPAuth2", (void**)&mpfnAuthEntry2);
1589
 
            if (RT_FAILURE(rc2))
1590
 
            {
1591
 
                if (rc2 != VERR_SYMBOL_NOT_FOUND)
1592
 
                {
1593
 
                    LogRel(("VRDPAUTH: Could not resolve import '%s'. Error code: %Rrc\n", "VRDPAuth2", rc2));
1594
 
                }
1595
 
                rc = rc2;
1596
 
            }
1597
 
 
1598
 
            /* Get the entry point. */
1599
 
            mpfnAuthEntry = NULL;
1600
 
            rc2 = RTLdrGetSymbol(mAuthLibrary, "VRDPAuth", (void**)&mpfnAuthEntry);
1601
 
            if (RT_FAILURE(rc2))
1602
 
            {
1603
 
                if (rc2 != VERR_SYMBOL_NOT_FOUND)
1604
 
                {
1605
 
                    LogRel(("VRDPAUTH: Could not resolve import '%s'. Error code: %Rrc\n", "VRDPAuth", rc2));
1606
 
                }
1607
 
                rc = rc2;
1608
 
            }
1609
 
 
1610
 
            if (mpfnAuthEntry2 || mpfnAuthEntry)
1611
 
            {
1612
 
                LogRel(("VRDPAUTH: Using entry point '%s'.\n", mpfnAuthEntry2? "VRDPAuth2": "VRDPAuth"));
1613
 
                rc = VINF_SUCCESS;
1614
 
            }
1615
 
        }
1616
 
 
1617
 
        if (RT_FAILURE(rc))
1618
 
        {
1619
 
            mConsole->reportAuthLibraryError(filename.raw(), rc);
1620
 
 
1621
 
            mpfnAuthEntry = NULL;
1622
 
            mpfnAuthEntry2 = NULL;
1623
 
 
1624
 
            if (mAuthLibrary)
1625
 
            {
1626
 
                RTLdrClose(mAuthLibrary);
1627
 
                mAuthLibrary = 0;
1628
 
            }
1629
 
 
1630
 
            return VRDPAuthAccessDenied;
1631
 
        }
1632
 
    }
1633
 
 
1634
 
    Assert (mAuthLibrary && (mpfnAuthEntry || mpfnAuthEntry2));
1635
 
 
1636
 
    VRDPAuthResult result = mpfnAuthEntry2?
1637
 
                                mpfnAuthEntry2 (&rawuuid, guestJudgement, pszUser, pszPassword, pszDomain, true, u32ClientId):
1638
 
                                mpfnAuthEntry (&rawuuid, guestJudgement, pszUser, pszPassword, pszDomain);
1639
 
 
1640
 
    switch (result)
1641
 
    {
1642
 
        case VRDPAuthAccessDenied:
1643
 
            LogRel(("VRDPAUTH: external authentication module returned 'access denied'\n"));
1644
 
            break;
1645
 
        case VRDPAuthAccessGranted:
1646
 
            LogRel(("VRDPAUTH: external authentication module returned 'access granted'\n"));
1647
 
            break;
1648
 
        case VRDPAuthDelegateToGuest:
1649
 
            LogRel(("VRDPAUTH: external authentication module returned 'delegate request to guest'\n"));
1650
 
            break;
1651
 
        default:
1652
 
            LogRel(("VRDPAUTH: external authentication module returned incorrect return code %d\n", result));
1653
 
            result = VRDPAuthAccessDenied;
1654
 
    }
1655
 
 
1656
 
    LogFlow(("ConsoleVRDPServer::Authenticate: result = %d\n", result));
1657
 
 
1658
 
    return result;
1659
 
}
1660
 
 
1661
 
void ConsoleVRDPServer::AuthDisconnect (const Guid &uuid, uint32_t u32ClientId)
1662
 
{
1663
 
    VRDPAUTHUUID rawuuid;
1664
 
 
1665
 
    memcpy (rawuuid, ((Guid &)uuid).ptr (), sizeof (rawuuid));
1666
 
 
1667
 
    LogFlow(("ConsoleVRDPServer::AuthDisconnect: uuid = %RTuuid, u32ClientId = %d\n",
1668
 
             rawuuid, u32ClientId));
1669
 
 
1670
 
    Assert (mAuthLibrary && (mpfnAuthEntry || mpfnAuthEntry2));
1671
 
 
1672
 
    if (mpfnAuthEntry2)
1673
 
        mpfnAuthEntry2 (&rawuuid, VRDPAuthGuestNotAsked, NULL, NULL, NULL, false, u32ClientId);
1674
 
}
1675
 
 
1676
 
int ConsoleVRDPServer::lockConsoleVRDPServer (void)
1677
 
{
1678
 
    int rc = RTCritSectEnter (&mCritSect);
1679
 
    AssertRC(rc);
1680
 
    return rc;
1681
 
}
1682
 
 
1683
 
void ConsoleVRDPServer::unlockConsoleVRDPServer (void)
1684
 
{
1685
 
    RTCritSectLeave (&mCritSect);
1686
 
}
1687
 
 
1688
 
DECLCALLBACK(int) ConsoleVRDPServer::ClipboardCallback (void *pvCallback,
1689
 
                                                        uint32_t u32ClientId,
1690
 
                                                        uint32_t u32Function,
1691
 
                                                        uint32_t u32Format,
1692
 
                                                        const void *pvData,
1693
 
                                                        uint32_t cbData)
1694
 
{
1695
 
    LogFlowFunc(("pvCallback = %p, u32ClientId = %d, u32Function = %d, u32Format = 0x%08X, pvData = %p, cbData = %d\n",
1696
 
                 pvCallback, u32ClientId, u32Function, u32Format, pvData, cbData));
1697
 
 
1698
 
    int rc = VINF_SUCCESS;
1699
 
 
1700
 
    ConsoleVRDPServer *pServer = static_cast <ConsoleVRDPServer *>(pvCallback);
1701
 
 
1702
 
    NOREF(u32ClientId);
1703
 
 
1704
 
    switch (u32Function)
1705
 
    {
1706
 
        case VRDP_CLIPBOARD_FUNCTION_FORMAT_ANNOUNCE:
1707
 
        {
1708
 
            if (pServer->mpfnClipboardCallback)
1709
 
            {
1710
 
                pServer->mpfnClipboardCallback (VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE,
1711
 
                                                u32Format,
1712
 
                                                (void *)pvData,
1713
 
                                                cbData);
1714
 
            }
1715
 
        } break;
1716
 
 
1717
 
        case VRDP_CLIPBOARD_FUNCTION_DATA_READ:
1718
 
        {
1719
 
            if (pServer->mpfnClipboardCallback)
1720
 
            {
1721
 
                pServer->mpfnClipboardCallback (VBOX_CLIPBOARD_EXT_FN_DATA_READ,
1722
 
                                                u32Format,
1723
 
                                                (void *)pvData,
1724
 
                                                cbData);
1725
 
            }
1726
 
        } break;
1727
 
 
1728
 
        default:
1729
 
            rc = VERR_NOT_SUPPORTED;
1730
 
    }
1731
 
 
1732
 
    return rc;
1733
 
}
1734
 
 
1735
 
DECLCALLBACK(int) ConsoleVRDPServer::ClipboardServiceExtension (void *pvExtension,
1736
 
                                                                uint32_t u32Function,
1737
 
                                                                void *pvParms,
1738
 
                                                                uint32_t cbParms)
1739
 
{
1740
 
    LogFlowFunc(("pvExtension = %p, u32Function = %d, pvParms = %p, cbParms = %d\n",
1741
 
                 pvExtension, u32Function, pvParms, cbParms));
1742
 
 
1743
 
    int rc = VINF_SUCCESS;
1744
 
 
1745
 
#ifdef VBOX_WITH_VRDP
1746
 
    ConsoleVRDPServer *pServer = static_cast <ConsoleVRDPServer *>(pvExtension);
1747
 
 
1748
 
    VBOXCLIPBOARDEXTPARMS *pParms = (VBOXCLIPBOARDEXTPARMS *)pvParms;
1749
 
 
1750
 
    switch (u32Function)
1751
 
    {
1752
 
        case VBOX_CLIPBOARD_EXT_FN_SET_CALLBACK:
1753
 
        {
1754
 
            pServer->mpfnClipboardCallback = pParms->u.pfnCallback;
1755
 
        } break;
1756
 
 
1757
 
        case VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE:
1758
 
        {
1759
 
            /* The guest announces clipboard formats. This must be delivered to all clients. */
1760
 
            if (mpEntryPoints && pServer->mhServer)
1761
 
            {
1762
 
                mpEntryPoints->VRDPClipboard (pServer->mhServer,
1763
 
                                              VRDP_CLIPBOARD_FUNCTION_FORMAT_ANNOUNCE,
1764
 
                                              pParms->u32Format,
1765
 
                                              NULL,
1766
 
                                              0,
1767
 
                                              NULL);
1768
 
            }
1769
 
        } break;
1770
 
 
1771
 
        case VBOX_CLIPBOARD_EXT_FN_DATA_READ:
1772
 
        {
1773
 
            /* The clipboard service expects that the pvData buffer will be filled
1774
 
             * with clipboard data. The server returns the data from the client that
1775
 
             * announced the requested format most recently.
1776
 
             */
1777
 
            if (mpEntryPoints && pServer->mhServer)
1778
 
            {
1779
 
                mpEntryPoints->VRDPClipboard (pServer->mhServer,
1780
 
                                              VRDP_CLIPBOARD_FUNCTION_DATA_READ,
1781
 
                                              pParms->u32Format,
1782
 
                                              pParms->u.pvData,
1783
 
                                              pParms->cbData,
1784
 
                                              &pParms->cbData);
1785
 
            }
1786
 
        } break;
1787
 
 
1788
 
        case VBOX_CLIPBOARD_EXT_FN_DATA_WRITE:
1789
 
        {
1790
 
            if (mpEntryPoints && pServer->mhServer)
1791
 
            {
1792
 
                mpEntryPoints->VRDPClipboard (pServer->mhServer,
1793
 
                                              VRDP_CLIPBOARD_FUNCTION_DATA_WRITE,
1794
 
                                              pParms->u32Format,
1795
 
                                              pParms->u.pvData,
1796
 
                                              pParms->cbData,
1797
 
                                              NULL);
1798
 
            }
1799
 
        } break;
1800
 
 
1801
 
        default:
1802
 
            rc = VERR_NOT_SUPPORTED;
1803
 
    }
1804
 
#endif /* VBOX_WITH_VRDP */
1805
 
 
1806
 
    return rc;
1807
 
}
1808
 
 
1809
 
void ConsoleVRDPServer::ClipboardCreate (uint32_t u32ClientId)
1810
 
{
1811
 
    int rc = lockConsoleVRDPServer ();
1812
 
 
1813
 
    if (RT_SUCCESS(rc))
1814
 
    {
1815
 
        if (mcClipboardRefs == 0)
1816
 
        {
1817
 
            rc = HGCMHostRegisterServiceExtension (&mhClipboard, "VBoxSharedClipboard", ClipboardServiceExtension, this);
1818
 
 
1819
 
            if (RT_SUCCESS(rc))
1820
 
            {
1821
 
                mcClipboardRefs++;
1822
 
            }
1823
 
        }
1824
 
 
1825
 
        unlockConsoleVRDPServer ();
1826
 
    }
1827
 
}
1828
 
 
1829
 
void ConsoleVRDPServer::ClipboardDelete (uint32_t u32ClientId)
1830
 
{
1831
 
    int rc = lockConsoleVRDPServer ();
1832
 
 
1833
 
    if (RT_SUCCESS(rc))
1834
 
    {
1835
 
        mcClipboardRefs--;
1836
 
 
1837
 
        if (mcClipboardRefs == 0)
1838
 
        {
1839
 
            HGCMHostUnregisterServiceExtension (mhClipboard);
1840
 
        }
1841
 
 
1842
 
        unlockConsoleVRDPServer ();
1843
 
    }
1844
 
}
1845
 
 
1846
 
/* That is called on INPUT thread of the VRDP server.
1847
 
 * The ConsoleVRDPServer keeps a list of created backend instances.
1848
 
 */
1849
 
void ConsoleVRDPServer::USBBackendCreate (uint32_t u32ClientId, void **ppvIntercept)
1850
 
{
1851
 
#ifdef VBOX_WITH_USB
1852
 
    LogFlow(("ConsoleVRDPServer::USBBackendCreate: u32ClientId = %d\n", u32ClientId));
1853
 
 
1854
 
    /* Create a new instance of the USB backend for the new client. */
1855
 
    RemoteUSBBackend *pRemoteUSBBackend = new RemoteUSBBackend (mConsole, this, u32ClientId);
1856
 
 
1857
 
    if (pRemoteUSBBackend)
1858
 
    {
1859
 
        pRemoteUSBBackend->AddRef (); /* 'Release' called in USBBackendDelete. */
1860
 
 
1861
 
        /* Append the new instance in the list. */
1862
 
        int rc = lockConsoleVRDPServer ();
1863
 
 
1864
 
        if (RT_SUCCESS(rc))
1865
 
        {
1866
 
            pRemoteUSBBackend->pNext = mUSBBackends.pHead;
1867
 
            if (mUSBBackends.pHead)
1868
 
            {
1869
 
                mUSBBackends.pHead->pPrev = pRemoteUSBBackend;
1870
 
            }
1871
 
            else
1872
 
            {
1873
 
                mUSBBackends.pTail = pRemoteUSBBackend;
1874
 
            }
1875
 
 
1876
 
            mUSBBackends.pHead = pRemoteUSBBackend;
1877
 
 
1878
 
            unlockConsoleVRDPServer ();
1879
 
 
1880
 
            if (ppvIntercept)
1881
 
            {
1882
 
                *ppvIntercept = pRemoteUSBBackend;
1883
 
            }
1884
 
        }
1885
 
 
1886
 
        if (RT_FAILURE(rc))
1887
 
        {
1888
 
            pRemoteUSBBackend->Release ();
1889
 
        }
1890
 
    }
1891
 
#endif /* VBOX_WITH_USB */
1892
 
}
1893
 
 
1894
 
void ConsoleVRDPServer::USBBackendDelete (uint32_t u32ClientId)
1895
 
{
1896
 
#ifdef VBOX_WITH_USB
1897
 
    LogFlow(("ConsoleVRDPServer::USBBackendDelete: u32ClientId = %d\n", u32ClientId));
1898
 
 
1899
 
    RemoteUSBBackend *pRemoteUSBBackend = NULL;
1900
 
 
1901
 
    /* Find the instance. */
1902
 
    int rc = lockConsoleVRDPServer ();
1903
 
 
1904
 
    if (RT_SUCCESS(rc))
1905
 
    {
1906
 
        pRemoteUSBBackend = usbBackendFind (u32ClientId);
1907
 
 
1908
 
        if (pRemoteUSBBackend)
1909
 
        {
1910
 
            /* Notify that it will be deleted. */
1911
 
            pRemoteUSBBackend->NotifyDelete ();
1912
 
        }
1913
 
 
1914
 
        unlockConsoleVRDPServer ();
1915
 
    }
1916
 
 
1917
 
    if (pRemoteUSBBackend)
1918
 
    {
1919
 
        /* Here the instance has been excluded from the list and can be dereferenced. */
1920
 
        pRemoteUSBBackend->Release ();
1921
 
    }
1922
 
#endif
1923
 
}
1924
 
 
1925
 
void *ConsoleVRDPServer::USBBackendRequestPointer (uint32_t u32ClientId, const Guid *pGuid)
1926
 
{
1927
 
#ifdef VBOX_WITH_USB
1928
 
    RemoteUSBBackend *pRemoteUSBBackend = NULL;
1929
 
 
1930
 
    /* Find the instance. */
1931
 
    int rc = lockConsoleVRDPServer ();
1932
 
 
1933
 
    if (RT_SUCCESS(rc))
1934
 
    {
1935
 
        pRemoteUSBBackend = usbBackendFind (u32ClientId);
1936
 
 
1937
 
        if (pRemoteUSBBackend)
1938
 
        {
1939
 
            /* Inform the backend instance that it is referenced by the Guid. */
1940
 
            bool fAdded = pRemoteUSBBackend->addUUID (pGuid);
1941
 
 
1942
 
            if (fAdded)
1943
 
            {
1944
 
                /* Reference the instance because its pointer is being taken. */
1945
 
                pRemoteUSBBackend->AddRef (); /* 'Release' is called in USBBackendReleasePointer. */
1946
 
            }
1947
 
            else
1948
 
            {
1949
 
                pRemoteUSBBackend = NULL;
1950
 
            }
1951
 
        }
1952
 
 
1953
 
        unlockConsoleVRDPServer ();
1954
 
    }
1955
 
 
1956
 
    if (pRemoteUSBBackend)
1957
 
    {
1958
 
        return pRemoteUSBBackend->GetBackendCallbackPointer ();
1959
 
    }
1960
 
 
1961
 
#endif
1962
 
    return NULL;
1963
 
}
1964
 
 
1965
 
void ConsoleVRDPServer::USBBackendReleasePointer (const Guid *pGuid)
1966
 
{
1967
 
#ifdef VBOX_WITH_USB
1968
 
    RemoteUSBBackend *pRemoteUSBBackend = NULL;
1969
 
 
1970
 
    /* Find the instance. */
1971
 
    int rc = lockConsoleVRDPServer ();
1972
 
 
1973
 
    if (RT_SUCCESS(rc))
1974
 
    {
1975
 
        pRemoteUSBBackend = usbBackendFindByUUID (pGuid);
1976
 
 
1977
 
        if (pRemoteUSBBackend)
1978
 
        {
1979
 
            pRemoteUSBBackend->removeUUID (pGuid);
1980
 
        }
1981
 
 
1982
 
        unlockConsoleVRDPServer ();
1983
 
 
1984
 
        if (pRemoteUSBBackend)
1985
 
        {
1986
 
            pRemoteUSBBackend->Release ();
1987
 
        }
1988
 
    }
1989
 
#endif
1990
 
}
1991
 
 
1992
 
RemoteUSBBackend *ConsoleVRDPServer::usbBackendGetNext (RemoteUSBBackend *pRemoteUSBBackend)
1993
 
{
1994
 
    LogFlow(("ConsoleVRDPServer::usbBackendGetNext: pBackend = %p\n", pRemoteUSBBackend));
1995
 
 
1996
 
    RemoteUSBBackend *pNextRemoteUSBBackend = NULL;
1997
 
#ifdef VBOX_WITH_USB
1998
 
 
1999
 
    int rc = lockConsoleVRDPServer ();
2000
 
 
2001
 
    if (RT_SUCCESS(rc))
2002
 
    {
2003
 
        if (pRemoteUSBBackend == NULL)
2004
 
        {
2005
 
            /* The first backend in the list is requested. */
2006
 
            pNextRemoteUSBBackend = mUSBBackends.pHead;
2007
 
        }
2008
 
        else
2009
 
        {
2010
 
            /* Get pointer to the next backend. */
2011
 
            pNextRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
2012
 
        }
2013
 
 
2014
 
        if (pNextRemoteUSBBackend)
2015
 
        {
2016
 
            pNextRemoteUSBBackend->AddRef ();
2017
 
        }
2018
 
 
2019
 
        unlockConsoleVRDPServer ();
2020
 
 
2021
 
        if (pRemoteUSBBackend)
2022
 
        {
2023
 
            pRemoteUSBBackend->Release ();
2024
 
        }
2025
 
    }
2026
 
#endif
2027
 
 
2028
 
    return pNextRemoteUSBBackend;
2029
 
}
2030
 
 
2031
 
#ifdef VBOX_WITH_USB
2032
 
/* Internal method. Called under the ConsoleVRDPServerLock. */
2033
 
RemoteUSBBackend *ConsoleVRDPServer::usbBackendFind (uint32_t u32ClientId)
2034
 
{
2035
 
    RemoteUSBBackend *pRemoteUSBBackend = mUSBBackends.pHead;
2036
 
 
2037
 
    while (pRemoteUSBBackend)
2038
 
    {
2039
 
        if (pRemoteUSBBackend->ClientId () == u32ClientId)
2040
 
        {
2041
 
            break;
2042
 
        }
2043
 
 
2044
 
        pRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
2045
 
    }
2046
 
 
2047
 
    return pRemoteUSBBackend;
2048
 
}
2049
 
 
2050
 
/* Internal method. Called under the ConsoleVRDPServerLock. */
2051
 
RemoteUSBBackend *ConsoleVRDPServer::usbBackendFindByUUID (const Guid *pGuid)
2052
 
{
2053
 
    RemoteUSBBackend *pRemoteUSBBackend = mUSBBackends.pHead;
2054
 
 
2055
 
    while (pRemoteUSBBackend)
2056
 
    {
2057
 
        if (pRemoteUSBBackend->findUUID (pGuid))
2058
 
        {
2059
 
            break;
2060
 
        }
2061
 
 
2062
 
        pRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
2063
 
    }
2064
 
 
2065
 
    return pRemoteUSBBackend;
2066
 
}
2067
 
#endif
2068
 
 
2069
 
/* Internal method. Called by the backend destructor. */
2070
 
void ConsoleVRDPServer::usbBackendRemoveFromList (RemoteUSBBackend *pRemoteUSBBackend)
2071
 
{
2072
 
#ifdef VBOX_WITH_USB
2073
 
    int rc = lockConsoleVRDPServer ();
2074
 
    AssertRC(rc);
2075
 
 
2076
 
    /* Exclude the found instance from the list. */
2077
 
    if (pRemoteUSBBackend->pNext)
2078
 
    {
2079
 
        pRemoteUSBBackend->pNext->pPrev = pRemoteUSBBackend->pPrev;
2080
 
    }
2081
 
    else
2082
 
    {
2083
 
        mUSBBackends.pTail = (RemoteUSBBackend *)pRemoteUSBBackend->pPrev;
2084
 
    }
2085
 
 
2086
 
    if (pRemoteUSBBackend->pPrev)
2087
 
    {
2088
 
        pRemoteUSBBackend->pPrev->pNext = pRemoteUSBBackend->pNext;
2089
 
    }
2090
 
    else
2091
 
    {
2092
 
        mUSBBackends.pHead = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
2093
 
    }
2094
 
 
2095
 
    pRemoteUSBBackend->pNext = pRemoteUSBBackend->pPrev = NULL;
2096
 
 
2097
 
    unlockConsoleVRDPServer ();
2098
 
#endif
2099
 
}
2100
 
 
2101
 
 
2102
 
void ConsoleVRDPServer::SendUpdate (unsigned uScreenId, void *pvUpdate, uint32_t cbUpdate) const
2103
 
{
2104
 
#ifdef VBOX_WITH_VRDP
2105
 
    if (mpEntryPoints && mhServer)
2106
 
    {
2107
 
        mpEntryPoints->VRDPUpdate (mhServer, uScreenId, pvUpdate, cbUpdate);
2108
 
    }
2109
 
#endif
2110
 
}
2111
 
 
2112
 
void ConsoleVRDPServer::SendResize (void) const
2113
 
{
2114
 
#ifdef VBOX_WITH_VRDP
2115
 
    if (mpEntryPoints && mhServer)
2116
 
    {
2117
 
        mpEntryPoints->VRDPResize (mhServer);
2118
 
    }
2119
 
#endif
2120
 
}
2121
 
 
2122
 
void ConsoleVRDPServer::SendUpdateBitmap (unsigned uScreenId, uint32_t x, uint32_t y, uint32_t w, uint32_t h) const
2123
 
{
2124
 
#ifdef VBOX_WITH_VRDP
2125
 
    VRDPORDERHDR update;
2126
 
    update.x = x;
2127
 
    update.y = y;
2128
 
    update.w = w;
2129
 
    update.h = h;
2130
 
    if (mpEntryPoints && mhServer)
2131
 
    {
2132
 
        mpEntryPoints->VRDPUpdate (mhServer, uScreenId, &update, sizeof (update));
2133
 
    }
2134
 
#endif
2135
 
}
2136
 
 
2137
 
void ConsoleVRDPServer::SendAudioSamples (void *pvSamples, uint32_t cSamples, VRDPAUDIOFORMAT format) const
2138
 
{
2139
 
#ifdef VBOX_WITH_VRDP
2140
 
    if (mpEntryPoints && mhServer)
2141
 
    {
2142
 
        mpEntryPoints->VRDPAudioSamples (mhServer, pvSamples, cSamples, format);
2143
 
    }
2144
 
#endif
2145
 
}
2146
 
 
2147
 
void ConsoleVRDPServer::SendAudioVolume (uint16_t left, uint16_t right) const
2148
 
{
2149
 
#ifdef VBOX_WITH_VRDP
2150
 
    if (mpEntryPoints && mhServer)
2151
 
    {
2152
 
        mpEntryPoints->VRDPAudioVolume (mhServer, left, right);
2153
 
    }
2154
 
#endif
2155
 
}
2156
 
 
2157
 
void ConsoleVRDPServer::SendUSBRequest (uint32_t u32ClientId, void *pvParms, uint32_t cbParms) const
2158
 
{
2159
 
#ifdef VBOX_WITH_VRDP
2160
 
    if (mpEntryPoints && mhServer)
2161
 
    {
2162
 
        mpEntryPoints->VRDPUSBRequest (mhServer, u32ClientId, pvParms, cbParms);
2163
 
    }
2164
 
#endif
2165
 
}
2166
 
 
2167
 
void ConsoleVRDPServer::QueryInfo (uint32_t index, void *pvBuffer, uint32_t cbBuffer, uint32_t *pcbOut) const
2168
 
{
2169
 
#ifdef VBOX_WITH_VRDP
2170
 
    if (index == VRDP_QI_PORT)
2171
 
    {
2172
 
        uint32_t cbOut = sizeof (int32_t);
2173
 
 
2174
 
        if (cbBuffer >= cbOut)
2175
 
        {
2176
 
            *pcbOut = cbOut;
2177
 
            *(int32_t *)pvBuffer = (int32_t)mVRDPBindPort;
2178
 
        }
2179
 
    }
2180
 
    else if (mpEntryPoints && mhServer)
2181
 
    {
2182
 
        mpEntryPoints->VRDPQueryInfo (mhServer, index, pvBuffer, cbBuffer, pcbOut);
2183
 
    }
2184
 
#endif
2185
 
}
2186
 
 
2187
 
#ifdef VBOX_WITH_VRDP
2188
 
/* note: static function now! */
2189
 
bool ConsoleVRDPServer::loadVRDPLibrary (void)
2190
 
{
2191
 
    int rc = VINF_SUCCESS;
2192
 
 
2193
 
    if (!mVRDPLibrary)
2194
 
    {
2195
 
        rc = SUPR3HardenedLdrLoadAppPriv ("VBoxVRDP", &mVRDPLibrary);
2196
 
 
2197
 
        if (RT_SUCCESS(rc))
2198
 
        {
2199
 
            LogFlow(("VRDPServer::loadLibrary(): successfully loaded VRDP library.\n"));
2200
 
 
2201
 
            struct SymbolEntry
2202
 
            {
2203
 
                const char *name;
2204
 
                void **ppfn;
2205
 
            };
2206
 
 
2207
 
            #define DEFSYMENTRY(a) { #a, (void**)&mpfn##a }
2208
 
 
2209
 
            static const struct SymbolEntry symbols[] =
2210
 
            {
2211
 
                DEFSYMENTRY(VRDPCreateServer)
2212
 
            };
2213
 
 
2214
 
            #undef DEFSYMENTRY
2215
 
 
2216
 
            for (unsigned i = 0; i < RT_ELEMENTS(symbols); i++)
2217
 
            {
2218
 
                rc = RTLdrGetSymbol(mVRDPLibrary, symbols[i].name, symbols[i].ppfn);
2219
 
 
2220
 
                AssertMsgRC(rc, ("Error resolving VRDP symbol %s\n", symbols[i].name));
2221
 
 
2222
 
                if (RT_FAILURE(rc))
2223
 
                {
2224
 
                    break;
2225
 
                }
2226
 
            }
2227
 
        }
2228
 
        else
2229
 
        {
2230
 
            LogRel(("VRDPServer::loadLibrary(): failed to load VRDP library! VRDP not available: rc = %Rrc\n", rc));
2231
 
            mVRDPLibrary = NULL;
2232
 
        }
2233
 
    }
2234
 
 
2235
 
    // just to be safe
2236
 
    if (RT_FAILURE(rc))
2237
 
    {
2238
 
        if (mVRDPLibrary)
2239
 
        {
2240
 
            RTLdrClose (mVRDPLibrary);
2241
 
            mVRDPLibrary = NULL;
2242
 
        }
2243
 
    }
2244
 
 
2245
 
    return (mVRDPLibrary != NULL);
2246
 
}
2247
 
#endif /* VBOX_WITH_VRDP */
2248
 
 
2249
 
/*
2250
 
 * IRemoteDisplayInfo implementation.
2251
 
 */
2252
 
// constructor / destructor
2253
 
/////////////////////////////////////////////////////////////////////////////
2254
 
 
2255
 
RemoteDisplayInfo::RemoteDisplayInfo()
2256
 
    : mParent(NULL)
2257
 
{
2258
 
}
2259
 
 
2260
 
RemoteDisplayInfo::~RemoteDisplayInfo()
2261
 
{
2262
 
}
2263
 
 
2264
 
 
2265
 
HRESULT RemoteDisplayInfo::FinalConstruct()
2266
 
{
2267
 
    return S_OK;
2268
 
}
2269
 
 
2270
 
void RemoteDisplayInfo::FinalRelease()
2271
 
{
2272
 
    uninit ();
2273
 
}
2274
 
 
2275
 
// public methods only for internal purposes
2276
 
/////////////////////////////////////////////////////////////////////////////
2277
 
 
2278
 
/**
2279
 
 * Initializes the guest object.
2280
 
 */
2281
 
HRESULT RemoteDisplayInfo::init (Console *aParent)
2282
 
{
2283
 
    LogFlowThisFunc(("aParent=%p\n", aParent));
2284
 
 
2285
 
    ComAssertRet(aParent, E_INVALIDARG);
2286
 
 
2287
 
    /* Enclose the state transition NotReady->InInit->Ready */
2288
 
    AutoInitSpan autoInitSpan(this);
2289
 
    AssertReturn(autoInitSpan.isOk(), E_FAIL);
2290
 
 
2291
 
    unconst(mParent) = aParent;
2292
 
 
2293
 
    /* Confirm a successful initialization */
2294
 
    autoInitSpan.setSucceeded();
2295
 
 
2296
 
    return S_OK;
2297
 
}
2298
 
 
2299
 
/**
2300
 
 *  Uninitializes the instance and sets the ready flag to FALSE.
2301
 
 *  Called either from FinalRelease() or by the parent when it gets destroyed.
2302
 
 */
2303
 
void RemoteDisplayInfo::uninit()
2304
 
{
2305
 
    LogFlowThisFunc(("\n"));
2306
 
 
2307
 
    /* Enclose the state transition Ready->InUninit->NotReady */
2308
 
    AutoUninitSpan autoUninitSpan(this);
2309
 
    if (autoUninitSpan.uninitDone())
2310
 
        return;
2311
 
 
2312
 
    unconst(mParent) = NULL;
2313
 
}
2314
 
 
2315
 
// IRemoteDisplayInfo properties
2316
 
/////////////////////////////////////////////////////////////////////////////
2317
 
 
2318
 
#define IMPL_GETTER_BOOL(_aType, _aName, _aIndex)                         \
2319
 
    STDMETHODIMP RemoteDisplayInfo::COMGETTER(_aName) (_aType *a##_aName) \
2320
 
    {                                                                     \
2321
 
        if (!a##_aName)                                                   \
2322
 
            return E_POINTER;                                             \
2323
 
                                                                          \
2324
 
        AutoCaller autoCaller(this);                                      \
2325
 
        if (FAILED(autoCaller.rc())) return autoCaller.rc();              \
2326
 
                                                                          \
2327
 
        /* todo: Not sure if a AutoReadLock would be sufficient. */       \
2328
 
        AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);                  \
2329
 
                                                                          \
2330
 
        uint32_t value;                                                   \
2331
 
        uint32_t cbOut = 0;                                               \
2332
 
                                                                          \
2333
 
        mParent->consoleVRDPServer ()->QueryInfo                          \
2334
 
            (_aIndex, &value, sizeof (value), &cbOut);                    \
2335
 
                                                                          \
2336
 
        *a##_aName = cbOut? !!value: FALSE;                               \
2337
 
                                                                          \
2338
 
        return S_OK;                                                      \
2339
 
    }                                                                     \
2340
 
    extern void IMPL_GETTER_BOOL_DUMMY(void)
2341
 
 
2342
 
#define IMPL_GETTER_SCALAR(_aType, _aName, _aIndex)                       \
2343
 
    STDMETHODIMP RemoteDisplayInfo::COMGETTER(_aName) (_aType *a##_aName) \
2344
 
    {                                                                     \
2345
 
        if (!a##_aName)                                                   \
2346
 
            return E_POINTER;                                             \
2347
 
                                                                          \
2348
 
        AutoCaller autoCaller(this);                                      \
2349
 
        if (FAILED(autoCaller.rc())) return autoCaller.rc();              \
2350
 
                                                                          \
2351
 
        /* todo: Not sure if a AutoReadLock would be sufficient. */       \
2352
 
        AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);                  \
2353
 
                                                                          \
2354
 
        _aType value;                                                     \
2355
 
        uint32_t cbOut = 0;                                               \
2356
 
                                                                          \
2357
 
        mParent->consoleVRDPServer ()->QueryInfo                          \
2358
 
            (_aIndex, &value, sizeof (value), &cbOut);                    \
2359
 
                                                                          \
2360
 
        *a##_aName = cbOut? value: 0;                                     \
2361
 
                                                                          \
2362
 
        return S_OK;                                                      \
2363
 
    }                                                                     \
2364
 
    extern void IMPL_GETTER_SCALAR_DUMMY(void)
2365
 
 
2366
 
#define IMPL_GETTER_BSTR(_aType, _aName, _aIndex)                         \
2367
 
    STDMETHODIMP RemoteDisplayInfo::COMGETTER(_aName) (_aType *a##_aName) \
2368
 
    {                                                                     \
2369
 
        if (!a##_aName)                                                   \
2370
 
            return E_POINTER;                                             \
2371
 
                                                                          \
2372
 
        AutoCaller autoCaller(this);                                      \
2373
 
        if (FAILED(autoCaller.rc())) return autoCaller.rc();              \
2374
 
                                                                          \
2375
 
        /* todo: Not sure if a AutoReadLock would be sufficient. */       \
2376
 
        AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);                  \
2377
 
                                                                          \
2378
 
        uint32_t cbOut = 0;                                               \
2379
 
                                                                          \
2380
 
        mParent->consoleVRDPServer ()->QueryInfo                          \
2381
 
            (_aIndex, NULL, 0, &cbOut);                                   \
2382
 
                                                                          \
2383
 
        if (cbOut == 0)                                                   \
2384
 
        {                                                                 \
2385
 
            Bstr str("");                                                 \
2386
 
            str.cloneTo(a##_aName);                                       \
2387
 
            return S_OK;                                                  \
2388
 
        }                                                                 \
2389
 
                                                                          \
2390
 
        char *pchBuffer = (char *)RTMemTmpAlloc (cbOut);                  \
2391
 
                                                                          \
2392
 
        if (!pchBuffer)                                                   \
2393
 
        {                                                                 \
2394
 
            Log(("RemoteDisplayInfo::"                                    \
2395
 
                 #_aName                                                  \
2396
 
                 ": Failed to allocate memory %d bytes\n", cbOut));       \
2397
 
            return E_OUTOFMEMORY;                                         \
2398
 
        }                                                                 \
2399
 
                                                                          \
2400
 
        mParent->consoleVRDPServer ()->QueryInfo                          \
2401
 
            (_aIndex, pchBuffer, cbOut, &cbOut);                          \
2402
 
                                                                          \
2403
 
        Bstr str(pchBuffer);                                              \
2404
 
                                                                          \
2405
 
        str.cloneTo(a##_aName);                                           \
2406
 
                                                                          \
2407
 
        RTMemTmpFree (pchBuffer);                                         \
2408
 
                                                                          \
2409
 
        return S_OK;                                                      \
2410
 
    }                                                                     \
2411
 
    extern void IMPL_GETTER_BSTR_DUMMY(void)
2412
 
 
2413
 
IMPL_GETTER_BOOL   (BOOL,    Active,             VRDP_QI_ACTIVE);
2414
 
IMPL_GETTER_SCALAR (LONG,    Port,               VRDP_QI_PORT);
2415
 
IMPL_GETTER_SCALAR (ULONG,   NumberOfClients,    VRDP_QI_NUMBER_OF_CLIENTS);
2416
 
IMPL_GETTER_SCALAR (LONG64,  BeginTime,          VRDP_QI_BEGIN_TIME);
2417
 
IMPL_GETTER_SCALAR (LONG64,  EndTime,            VRDP_QI_END_TIME);
2418
 
IMPL_GETTER_SCALAR (ULONG64, BytesSent,          VRDP_QI_BYTES_SENT);
2419
 
IMPL_GETTER_SCALAR (ULONG64, BytesSentTotal,     VRDP_QI_BYTES_SENT_TOTAL);
2420
 
IMPL_GETTER_SCALAR (ULONG64, BytesReceived,      VRDP_QI_BYTES_RECEIVED);
2421
 
IMPL_GETTER_SCALAR (ULONG64, BytesReceivedTotal, VRDP_QI_BYTES_RECEIVED_TOTAL);
2422
 
IMPL_GETTER_BSTR   (BSTR,    User,               VRDP_QI_USER);
2423
 
IMPL_GETTER_BSTR   (BSTR,    Domain,             VRDP_QI_DOMAIN);
2424
 
IMPL_GETTER_BSTR   (BSTR,    ClientName,         VRDP_QI_CLIENT_NAME);
2425
 
IMPL_GETTER_BSTR   (BSTR,    ClientIP,           VRDP_QI_CLIENT_IP);
2426
 
IMPL_GETTER_SCALAR (ULONG,   ClientVersion,      VRDP_QI_CLIENT_VERSION);
2427
 
IMPL_GETTER_SCALAR (ULONG,   EncryptionStyle,    VRDP_QI_ENCRYPTION_STYLE);
2428
 
 
2429
 
#undef IMPL_GETTER_BSTR
2430
 
#undef IMPL_GETTER_SCALAR
2431
 
#undef IMPL_GETTER_BOOL
2432
 
/* vi: set tabstop=4 shiftwidth=4 expandtab: */