VirtualBox

source: vbox/trunk/src/VBox/Main/ConsoleVRDPServer.cpp@ 30627

Last change on this file since 30627 was 30627, checked in by vboxsync, 15 years ago

Main, some frontends: removing callbacks

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette