VirtualBox

source: vbox/trunk/src/VBox/Main/src-client/ConsoleVRDPServer.cpp@ 38869

Last change on this file since 38869 was 37809, checked in by vboxsync, 13 years ago

gcc-4.6 warnings (shadowed declaration)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 98.3 KB
Line 
1/* $Id: ConsoleVRDPServer.cpp 37809 2011-07-07 08:15:02Z 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#include "AudioSnifferInterface.h"
24#ifdef VBOX_WITH_EXTPACK
25# include "ExtPackManagerImpl.h"
26#endif
27#include "VMMDev.h"
28
29#include "Global.h"
30#include "AutoCaller.h"
31#include "Logging.h"
32
33#include <iprt/asm.h>
34#include <iprt/alloca.h>
35#include <iprt/ldr.h>
36#include <iprt/param.h>
37#include <iprt/path.h>
38#include <iprt/cpp/utils.h>
39
40#include <VBox/err.h>
41#include <VBox/RemoteDesktop/VRDEOrders.h>
42#include <VBox/com/listeners.h>
43#include <VBox/HostServices/VBoxCrOpenGLSvc.h>
44
45class VRDPConsoleListener
46{
47public:
48 VRDPConsoleListener()
49 {
50 }
51
52 HRESULT init(ConsoleVRDPServer *server)
53 {
54 m_server = server;
55 return S_OK;
56 }
57
58 void uninit()
59 {
60 }
61
62 STDMETHOD(HandleEvent)(VBoxEventType_T aType, IEvent * aEvent)
63 {
64 switch (aType)
65 {
66 case VBoxEventType_OnMousePointerShapeChanged:
67 {
68 ComPtr<IMousePointerShapeChangedEvent> mpscev = aEvent;
69 Assert(mpscev);
70 BOOL visible, alpha;
71 ULONG xHot, yHot, width, height;
72 com::SafeArray <BYTE> shape;
73
74 mpscev->COMGETTER(Visible)(&visible);
75 mpscev->COMGETTER(Alpha)(&alpha);
76 mpscev->COMGETTER(Xhot)(&xHot);
77 mpscev->COMGETTER(Yhot)(&yHot);
78 mpscev->COMGETTER(Width)(&width);
79 mpscev->COMGETTER(Height)(&height);
80 mpscev->COMGETTER(Shape)(ComSafeArrayAsOutParam(shape));
81
82 OnMousePointerShapeChange(visible, alpha, xHot, yHot, width, height, ComSafeArrayAsInParam(shape));
83 break;
84 }
85 case VBoxEventType_OnMouseCapabilityChanged:
86 {
87 ComPtr<IMouseCapabilityChangedEvent> mccev = aEvent;
88 Assert(mccev);
89 if (m_server)
90 {
91 BOOL fAbsoluteMouse;
92 mccev->COMGETTER(SupportsAbsolute)(&fAbsoluteMouse);
93 m_server->NotifyAbsoluteMouse(!!fAbsoluteMouse);
94 }
95 break;
96 }
97 case VBoxEventType_OnKeyboardLedsChanged:
98 {
99 ComPtr<IKeyboardLedsChangedEvent> klcev = aEvent;
100 Assert(klcev);
101
102 if (m_server)
103 {
104 BOOL fNumLock, fCapsLock, fScrollLock;
105 klcev->COMGETTER(NumLock)(&fNumLock);
106 klcev->COMGETTER(CapsLock)(&fCapsLock);
107 klcev->COMGETTER(ScrollLock)(&fScrollLock);
108 m_server->NotifyKeyboardLedsChange(fNumLock, fCapsLock, fScrollLock);
109 }
110 break;
111 }
112
113 default:
114 AssertFailed();
115 }
116
117 return S_OK;
118 }
119
120private:
121 STDMETHOD(OnMousePointerShapeChange)(BOOL visible, BOOL alpha, ULONG xHot, ULONG yHot,
122 ULONG width, ULONG height, ComSafeArrayIn(BYTE,shape));
123 ConsoleVRDPServer *m_server;
124};
125
126typedef ListenerImpl<VRDPConsoleListener, ConsoleVRDPServer*> VRDPConsoleListenerImpl;
127
128VBOX_LISTENER_DECLARE(VRDPConsoleListenerImpl)
129
130#ifdef DEBUG_sunlover
131#define LOGDUMPPTR Log
132void dumpPointer(const uint8_t *pu8Shape, uint32_t width, uint32_t height, bool fXorMaskRGB32)
133{
134 unsigned i;
135
136 const uint8_t *pu8And = pu8Shape;
137
138 for (i = 0; i < height; i++)
139 {
140 unsigned j;
141 LOGDUMPPTR(("%p: ", pu8And));
142 for (j = 0; j < (width + 7) / 8; j++)
143 {
144 unsigned k;
145 for (k = 0; k < 8; k++)
146 {
147 LOGDUMPPTR(("%d", ((*pu8And) & (1 << (7 - k)))? 1: 0));
148 }
149
150 pu8And++;
151 }
152 LOGDUMPPTR(("\n"));
153 }
154
155 if (fXorMaskRGB32)
156 {
157 uint32_t *pu32Xor = (uint32_t*)(pu8Shape + ((((width + 7) / 8) * height + 3) & ~3));
158
159 for (i = 0; i < height; i++)
160 {
161 unsigned j;
162 LOGDUMPPTR(("%p: ", pu32Xor));
163 for (j = 0; j < width; j++)
164 {
165 LOGDUMPPTR(("%08X", *pu32Xor++));
166 }
167 LOGDUMPPTR(("\n"));
168 }
169 }
170 else
171 {
172 /* RDP 24 bit RGB mask. */
173 uint8_t *pu8Xor = (uint8_t*)(pu8Shape + ((((width + 7) / 8) * height + 3) & ~3));
174 for (i = 0; i < height; i++)
175 {
176 unsigned j;
177 LOGDUMPPTR(("%p: ", pu8Xor));
178 for (j = 0; j < width; j++)
179 {
180 LOGDUMPPTR(("%02X%02X%02X", pu8Xor[2], pu8Xor[1], pu8Xor[0]));
181 pu8Xor += 3;
182 }
183 LOGDUMPPTR(("\n"));
184 }
185 }
186}
187#else
188#define dumpPointer(a, b, c, d) do {} while (0)
189#endif /* DEBUG_sunlover */
190
191static void findTopLeftBorder(const uint8_t *pu8AndMask, const uint8_t *pu8XorMask, uint32_t width, uint32_t height, uint32_t *pxSkip, uint32_t *pySkip)
192{
193 /*
194 * Find the top border of the AND mask. First assign to special value.
195 */
196 uint32_t ySkipAnd = ~0;
197
198 const uint8_t *pu8And = pu8AndMask;
199 const uint32_t cbAndRow = (width + 7) / 8;
200 const uint8_t maskLastByte = (uint8_t)( 0xFF << (cbAndRow * 8 - width) );
201
202 Assert(cbAndRow > 0);
203
204 unsigned y;
205 unsigned x;
206
207 for (y = 0; y < height && ySkipAnd == ~(uint32_t)0; y++, pu8And += cbAndRow)
208 {
209 /* For each complete byte in the row. */
210 for (x = 0; x < cbAndRow - 1; x++)
211 {
212 if (pu8And[x] != 0xFF)
213 {
214 ySkipAnd = y;
215 break;
216 }
217 }
218
219 if (ySkipAnd == ~(uint32_t)0)
220 {
221 /* Last byte. */
222 if ((pu8And[cbAndRow - 1] & maskLastByte) != maskLastByte)
223 {
224 ySkipAnd = y;
225 }
226 }
227 }
228
229 if (ySkipAnd == ~(uint32_t)0)
230 {
231 ySkipAnd = 0;
232 }
233
234 /*
235 * Find the left border of the AND mask.
236 */
237 uint32_t xSkipAnd = ~0;
238
239 /* For all bit columns. */
240 for (x = 0; x < width && xSkipAnd == ~(uint32_t)0; x++)
241 {
242 pu8And = pu8AndMask + x/8; /* Currently checking byte. */
243 uint8_t mask = 1 << (7 - x%8); /* Currently checking bit in the byte. */
244
245 for (y = ySkipAnd; y < height; y++, pu8And += cbAndRow)
246 {
247 if ((*pu8And & mask) == 0)
248 {
249 xSkipAnd = x;
250 break;
251 }
252 }
253 }
254
255 if (xSkipAnd == ~(uint32_t)0)
256 {
257 xSkipAnd = 0;
258 }
259
260 /*
261 * Find the XOR mask top border.
262 */
263 uint32_t ySkipXor = ~0;
264
265 uint32_t *pu32XorStart = (uint32_t *)pu8XorMask;
266
267 uint32_t *pu32Xor = pu32XorStart;
268
269 for (y = 0; y < height && ySkipXor == ~(uint32_t)0; y++, pu32Xor += width)
270 {
271 for (x = 0; x < width; x++)
272 {
273 if (pu32Xor[x] != 0)
274 {
275 ySkipXor = y;
276 break;
277 }
278 }
279 }
280
281 if (ySkipXor == ~(uint32_t)0)
282 {
283 ySkipXor = 0;
284 }
285
286 /*
287 * Find the left border of the XOR mask.
288 */
289 uint32_t xSkipXor = ~(uint32_t)0;
290
291 /* For all columns. */
292 for (x = 0; x < width && xSkipXor == ~(uint32_t)0; x++)
293 {
294 pu32Xor = pu32XorStart + x; /* Currently checking dword. */
295
296 for (y = ySkipXor; y < height; y++, pu32Xor += width)
297 {
298 if (*pu32Xor != 0)
299 {
300 xSkipXor = x;
301 break;
302 }
303 }
304 }
305
306 if (xSkipXor == ~(uint32_t)0)
307 {
308 xSkipXor = 0;
309 }
310
311 *pxSkip = RT_MIN(xSkipAnd, xSkipXor);
312 *pySkip = RT_MIN(ySkipAnd, ySkipXor);
313}
314
315/* Generate an AND mask for alpha pointers here, because
316 * guest driver does not do that correctly for Vista pointers.
317 * Similar fix, changing the alpha threshold, could be applied
318 * for the guest driver, but then additions reinstall would be
319 * necessary, which we try to avoid.
320 */
321static void mousePointerGenerateANDMask(uint8_t *pu8DstAndMask, int cbDstAndMask, const uint8_t *pu8SrcAlpha, int w, int h)
322{
323 memset(pu8DstAndMask, 0xFF, cbDstAndMask);
324
325 int y;
326 for (y = 0; y < h; y++)
327 {
328 uint8_t bitmask = 0x80;
329
330 int x;
331 for (x = 0; x < w; x++, bitmask >>= 1)
332 {
333 if (bitmask == 0)
334 {
335 bitmask = 0x80;
336 }
337
338 /* Whether alpha channel value is not transparent enough for the pixel to be seen. */
339 if (pu8SrcAlpha[x * 4 + 3] > 0x7f)
340 {
341 pu8DstAndMask[x / 8] &= ~bitmask;
342 }
343 }
344
345 /* Point to next source and dest scans. */
346 pu8SrcAlpha += w * 4;
347 pu8DstAndMask += (w + 7) / 8;
348 }
349}
350
351STDMETHODIMP VRDPConsoleListener::OnMousePointerShapeChange(BOOL visible,
352 BOOL alpha,
353 ULONG xHot,
354 ULONG yHot,
355 ULONG width,
356 ULONG height,
357 ComSafeArrayIn(BYTE,inShape))
358{
359 LogSunlover(("VRDPConsoleListener::OnMousePointerShapeChange: %d, %d, %lux%lu, @%lu,%lu\n", visible, alpha, width, height, xHot, yHot));
360
361 if (m_server)
362 {
363 com::SafeArray <BYTE> aShape(ComSafeArrayInArg(inShape));
364 if (aShape.size() == 0)
365 {
366 if (!visible)
367 {
368 m_server->MousePointerHide();
369 }
370 }
371 else if (width != 0 && height != 0)
372 {
373 /* Pointer consists of 1 bpp AND and 24 BPP XOR masks.
374 * 'shape' AND mask followed by XOR mask.
375 * XOR mask contains 32 bit (lsb)BGR0(msb) values.
376 *
377 * We convert this to RDP color format which consist of
378 * one bpp AND mask and 24 BPP (BGR) color XOR image.
379 *
380 * RDP clients expect 8 aligned width and height of
381 * pointer (preferably 32x32).
382 *
383 * They even contain bugs which do not appear for
384 * 32x32 pointers but would appear for a 41x32 one.
385 *
386 * So set pointer size to 32x32. This can be done safely
387 * because most pointers are 32x32.
388 */
389 uint8_t* shape = aShape.raw();
390
391 dumpPointer(shape, width, height, true);
392
393 int cbDstAndMask = (((width + 7) / 8) * height + 3) & ~3;
394
395 uint8_t *pu8AndMask = shape;
396 uint8_t *pu8XorMask = shape + cbDstAndMask;
397
398 if (alpha)
399 {
400 pu8AndMask = (uint8_t*)alloca(cbDstAndMask);
401
402 mousePointerGenerateANDMask(pu8AndMask, cbDstAndMask, pu8XorMask, width, height);
403 }
404
405 /* Windows guest alpha pointers are wider than 32 pixels.
406 * Try to find out the top-left border of the pointer and
407 * then copy only meaningful bits. All complete top rows
408 * and all complete left columns where (AND == 1 && XOR == 0)
409 * are skipped. Hot spot is adjusted.
410 */
411 uint32_t ySkip = 0; /* How many rows to skip at the top. */
412 uint32_t xSkip = 0; /* How many columns to skip at the left. */
413
414 findTopLeftBorder(pu8AndMask, pu8XorMask, width, height, &xSkip, &ySkip);
415
416 /* Must not skip the hot spot. */
417 xSkip = RT_MIN(xSkip, xHot);
418 ySkip = RT_MIN(ySkip, yHot);
419
420 /*
421 * Compute size and allocate memory for the pointer.
422 */
423 const uint32_t dstwidth = 32;
424 const uint32_t dstheight = 32;
425
426 VRDECOLORPOINTER *pointer = NULL;
427
428 uint32_t dstmaskwidth = (dstwidth + 7) / 8;
429
430 uint32_t rdpmaskwidth = dstmaskwidth;
431 uint32_t rdpmasklen = dstheight * rdpmaskwidth;
432
433 uint32_t rdpdatawidth = dstwidth * 3;
434 uint32_t rdpdatalen = dstheight * rdpdatawidth;
435
436 pointer = (VRDECOLORPOINTER *)RTMemTmpAlloc(sizeof(VRDECOLORPOINTER) + rdpmasklen + rdpdatalen);
437
438 if (pointer)
439 {
440 uint8_t *maskarray = (uint8_t*)pointer + sizeof(VRDECOLORPOINTER);
441 uint8_t *dataarray = maskarray + rdpmasklen;
442
443 memset(maskarray, 0xFF, rdpmasklen);
444 memset(dataarray, 0x00, rdpdatalen);
445
446 uint32_t srcmaskwidth = (width + 7) / 8;
447 uint32_t srcdatawidth = width * 4;
448
449 /* Copy AND mask. */
450 uint8_t *src = pu8AndMask + ySkip * srcmaskwidth;
451 uint8_t *dst = maskarray + (dstheight - 1) * rdpmaskwidth;
452
453 uint32_t minheight = RT_MIN(height - ySkip, dstheight);
454 uint32_t minwidth = RT_MIN(width - xSkip, dstwidth);
455
456 unsigned x, y;
457
458 for (y = 0; y < minheight; y++)
459 {
460 for (x = 0; x < minwidth; x++)
461 {
462 uint32_t byteIndex = (x + xSkip) / 8;
463 uint32_t bitIndex = (x + xSkip) % 8;
464
465 bool bit = (src[byteIndex] & (1 << (7 - bitIndex))) != 0;
466
467 if (!bit)
468 {
469 byteIndex = x / 8;
470 bitIndex = x % 8;
471
472 dst[byteIndex] &= ~(1 << (7 - bitIndex));
473 }
474 }
475
476 src += srcmaskwidth;
477 dst -= rdpmaskwidth;
478 }
479
480 /* Point src to XOR mask */
481 src = pu8XorMask + ySkip * srcdatawidth;
482 dst = dataarray + (dstheight - 1) * rdpdatawidth;
483
484 for (y = 0; y < minheight ; y++)
485 {
486 for (x = 0; x < minwidth; x++)
487 {
488 memcpy(dst + x * 3, &src[4 * (x + xSkip)], 3);
489 }
490
491 src += srcdatawidth;
492 dst -= rdpdatawidth;
493 }
494
495 pointer->u16HotX = (uint16_t)(xHot - xSkip);
496 pointer->u16HotY = (uint16_t)(yHot - ySkip);
497
498 pointer->u16Width = (uint16_t)dstwidth;
499 pointer->u16Height = (uint16_t)dstheight;
500
501 pointer->u16MaskLen = (uint16_t)rdpmasklen;
502 pointer->u16DataLen = (uint16_t)rdpdatalen;
503
504 dumpPointer((uint8_t*)pointer + sizeof(*pointer), dstwidth, dstheight, false);
505
506 m_server->MousePointerUpdate(pointer);
507
508 RTMemTmpFree(pointer);
509 }
510 }
511 }
512
513 return S_OK;
514}
515
516
517// ConsoleVRDPServer
518////////////////////////////////////////////////////////////////////////////////
519
520RTLDRMOD ConsoleVRDPServer::mVRDPLibrary = NIL_RTLDRMOD;
521
522PFNVRDECREATESERVER ConsoleVRDPServer::mpfnVRDECreateServer = NULL;
523
524VRDEENTRYPOINTS_4 ConsoleVRDPServer::mEntryPoints; /* A copy of the server entry points. */
525VRDEENTRYPOINTS_4 *ConsoleVRDPServer::mpEntryPoints = NULL;
526
527VRDECALLBACKS_4 ConsoleVRDPServer::mCallbacks =
528{
529 { VRDE_INTERFACE_VERSION_4, sizeof(VRDECALLBACKS_4) },
530 ConsoleVRDPServer::VRDPCallbackQueryProperty,
531 ConsoleVRDPServer::VRDPCallbackClientLogon,
532 ConsoleVRDPServer::VRDPCallbackClientConnect,
533 ConsoleVRDPServer::VRDPCallbackClientDisconnect,
534 ConsoleVRDPServer::VRDPCallbackIntercept,
535 ConsoleVRDPServer::VRDPCallbackUSB,
536 ConsoleVRDPServer::VRDPCallbackClipboard,
537 ConsoleVRDPServer::VRDPCallbackFramebufferQuery,
538 ConsoleVRDPServer::VRDPCallbackFramebufferLock,
539 ConsoleVRDPServer::VRDPCallbackFramebufferUnlock,
540 ConsoleVRDPServer::VRDPCallbackInput,
541 ConsoleVRDPServer::VRDPCallbackVideoModeHint,
542 ConsoleVRDPServer::VRDECallbackAudioIn
543};
544
545DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackQueryProperty(void *pvCallback, uint32_t index, void *pvBuffer, uint32_t cbBuffer, uint32_t *pcbOut)
546{
547 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
548
549 int rc = VERR_NOT_SUPPORTED;
550
551 switch (index)
552 {
553 case VRDE_QP_NETWORK_PORT:
554 {
555 /* This is obsolete, the VRDE server uses VRDE_QP_NETWORK_PORT_RANGE instead. */
556 ULONG port = 0;
557
558 if (cbBuffer >= sizeof(uint32_t))
559 {
560 *(uint32_t *)pvBuffer = (uint32_t)port;
561 rc = VINF_SUCCESS;
562 }
563 else
564 {
565 rc = VINF_BUFFER_OVERFLOW;
566 }
567
568 *pcbOut = sizeof(uint32_t);
569 } break;
570
571 case VRDE_QP_NETWORK_ADDRESS:
572 {
573 com::Bstr bstr;
574 server->mConsole->getVRDEServer()->GetVRDEProperty(Bstr("TCP/Address").raw(), bstr.asOutParam());
575
576 /* The server expects UTF8. */
577 com::Utf8Str address = bstr;
578
579 size_t cbAddress = address.length() + 1;
580
581 if (cbAddress >= 0x10000)
582 {
583 /* More than 64K seems to be an invalid address. */
584 rc = VERR_TOO_MUCH_DATA;
585 break;
586 }
587
588 if ((size_t)cbBuffer >= cbAddress)
589 {
590 memcpy(pvBuffer, address.c_str(), cbAddress);
591 rc = VINF_SUCCESS;
592 }
593 else
594 {
595 rc = VINF_BUFFER_OVERFLOW;
596 }
597
598 *pcbOut = (uint32_t)cbAddress;
599 } break;
600
601 case VRDE_QP_NUMBER_MONITORS:
602 {
603 ULONG cMonitors = 1;
604
605 server->mConsole->machine()->COMGETTER(MonitorCount)(&cMonitors);
606
607 if (cbBuffer >= sizeof(uint32_t))
608 {
609 *(uint32_t *)pvBuffer = (uint32_t)cMonitors;
610 rc = VINF_SUCCESS;
611 }
612 else
613 {
614 rc = VINF_BUFFER_OVERFLOW;
615 }
616
617 *pcbOut = sizeof(uint32_t);
618 } break;
619
620 case VRDE_QP_NETWORK_PORT_RANGE:
621 {
622 com::Bstr bstr;
623 HRESULT hrc = server->mConsole->getVRDEServer()->GetVRDEProperty(Bstr("TCP/Ports").raw(), bstr.asOutParam());
624
625 if (hrc != S_OK)
626 {
627 bstr = "";
628 }
629
630 if (bstr == "0")
631 {
632 bstr = "3389";
633 }
634
635 /* The server expects UTF8. */
636 com::Utf8Str portRange = bstr;
637
638 size_t cbPortRange = portRange.length() + 1;
639
640 if (cbPortRange >= 0x10000)
641 {
642 /* More than 64K seems to be an invalid port range string. */
643 rc = VERR_TOO_MUCH_DATA;
644 break;
645 }
646
647 if ((size_t)cbBuffer >= cbPortRange)
648 {
649 memcpy(pvBuffer, portRange.c_str(), cbPortRange);
650 rc = VINF_SUCCESS;
651 }
652 else
653 {
654 rc = VINF_BUFFER_OVERFLOW;
655 }
656
657 *pcbOut = (uint32_t)cbPortRange;
658 } break;
659
660#ifdef VBOX_WITH_VRDP_VIDEO_CHANNEL
661 case VRDE_QP_VIDEO_CHANNEL:
662 {
663 com::Bstr bstr;
664 HRESULT hrc = server->mConsole->getVRDEServer()->GetVRDEProperty(Bstr("VideoChannel/Enabled").raw(), bstr.asOutParam());
665
666 if (hrc != S_OK)
667 {
668 bstr = "";
669 }
670
671 com::Utf8Str value = bstr;
672
673 BOOL fVideoEnabled = RTStrICmp(value.c_str(), "true") == 0
674 || RTStrICmp(value.c_str(), "1") == 0;
675
676 if (cbBuffer >= sizeof(uint32_t))
677 {
678 *(uint32_t *)pvBuffer = (uint32_t)fVideoEnabled;
679 rc = VINF_SUCCESS;
680 }
681 else
682 {
683 rc = VINF_BUFFER_OVERFLOW;
684 }
685
686 *pcbOut = sizeof(uint32_t);
687 } break;
688
689 case VRDE_QP_VIDEO_CHANNEL_QUALITY:
690 {
691 com::Bstr bstr;
692 HRESULT hrc = server->mConsole->getVRDEServer()->GetVRDEProperty(Bstr("VideoChannel/Quality").raw(), bstr.asOutParam());
693
694 if (hrc != S_OK)
695 {
696 bstr = "";
697 }
698
699 com::Utf8Str value = bstr;
700
701 ULONG ulQuality = RTStrToUInt32(value.c_str()); /* This returns 0 on invalid string which is ok. */
702
703 if (cbBuffer >= sizeof(uint32_t))
704 {
705 *(uint32_t *)pvBuffer = (uint32_t)ulQuality;
706 rc = VINF_SUCCESS;
707 }
708 else
709 {
710 rc = VINF_BUFFER_OVERFLOW;
711 }
712
713 *pcbOut = sizeof(uint32_t);
714 } break;
715
716 case VRDE_QP_VIDEO_CHANNEL_SUNFLSH:
717 {
718 ULONG ulSunFlsh = 1;
719
720 com::Bstr bstr;
721 HRESULT hrc = server->mConsole->machine()->GetExtraData(Bstr("VRDP/SunFlsh").raw(),
722 bstr.asOutParam());
723 if (hrc == S_OK && !bstr.isEmpty())
724 {
725 com::Utf8Str sunFlsh = bstr;
726 if (!sunFlsh.isEmpty())
727 {
728 ulSunFlsh = sunFlsh.toUInt32();
729 }
730 }
731
732 if (cbBuffer >= sizeof(uint32_t))
733 {
734 *(uint32_t *)pvBuffer = (uint32_t)ulSunFlsh;
735 rc = VINF_SUCCESS;
736 }
737 else
738 {
739 rc = VINF_BUFFER_OVERFLOW;
740 }
741
742 *pcbOut = sizeof(uint32_t);
743 } break;
744#endif /* VBOX_WITH_VRDP_VIDEO_CHANNEL */
745
746 case VRDE_QP_FEATURE:
747 {
748 if (cbBuffer < sizeof(VRDEFEATURE))
749 {
750 rc = VERR_INVALID_PARAMETER;
751 break;
752 }
753
754 size_t cbInfo = cbBuffer - RT_OFFSETOF(VRDEFEATURE, achInfo);
755
756 VRDEFEATURE *pFeature = (VRDEFEATURE *)pvBuffer;
757
758 size_t cchInfo = 0;
759 rc = RTStrNLenEx(pFeature->achInfo, cbInfo, &cchInfo);
760
761 if (RT_FAILURE(rc))
762 {
763 rc = VERR_INVALID_PARAMETER;
764 break;
765 }
766
767 Log(("VRDE_QP_FEATURE [%s]\n", pFeature->achInfo));
768
769 com::Bstr bstrValue;
770
771 if ( RTStrICmp(pFeature->achInfo, "Client/DisableDisplay") == 0
772 || RTStrICmp(pFeature->achInfo, "Client/DisableInput") == 0
773 || RTStrICmp(pFeature->achInfo, "Client/DisableAudio") == 0
774 || RTStrICmp(pFeature->achInfo, "Client/DisableUSB") == 0
775 || RTStrICmp(pFeature->achInfo, "Client/DisableClipboard") == 0
776 )
777 {
778 /* @todo these features should be per client. */
779 NOREF(pFeature->u32ClientId);
780
781 /* These features are mapped to "VRDE/Feature/NAME" extra data. */
782 com::Utf8Str extraData("VRDE/Feature/");
783 extraData += pFeature->achInfo;
784
785 HRESULT hrc = server->mConsole->machine()->GetExtraData(com::Bstr(extraData).raw(),
786 bstrValue.asOutParam());
787 if (FAILED(hrc) || bstrValue.isEmpty())
788 {
789 /* Also try the old "VRDP/Feature/NAME" */
790 extraData = "VRDP/Feature/";
791 extraData += pFeature->achInfo;
792
793 hrc = server->mConsole->machine()->GetExtraData(com::Bstr(extraData).raw(),
794 bstrValue.asOutParam());
795 if (FAILED(hrc))
796 {
797 rc = VERR_NOT_SUPPORTED;
798 }
799 }
800 }
801 else if (RTStrNCmp(pFeature->achInfo, "Property/", 9) == 0)
802 {
803 /* Generic properties. */
804 const char *pszPropertyName = &pFeature->achInfo[9];
805 HRESULT hrc = server->mConsole->getVRDEServer()->GetVRDEProperty(Bstr(pszPropertyName).raw(), bstrValue.asOutParam());
806 if (FAILED(hrc))
807 {
808 rc = VERR_NOT_SUPPORTED;
809 }
810 }
811 else
812 {
813 rc = VERR_NOT_SUPPORTED;
814 }
815
816 /* Copy the value string to the callers buffer. */
817 if (rc == VINF_SUCCESS)
818 {
819 com::Utf8Str value = bstrValue;
820
821 size_t cb = value.length() + 1;
822
823 if ((size_t)cbInfo >= cb)
824 {
825 memcpy(pFeature->achInfo, value.c_str(), cb);
826 }
827 else
828 {
829 rc = VINF_BUFFER_OVERFLOW;
830 }
831
832 *pcbOut = (uint32_t)cb;
833 }
834 } break;
835
836 case VRDE_SP_NETWORK_BIND_PORT:
837 {
838 if (cbBuffer != sizeof(uint32_t))
839 {
840 rc = VERR_INVALID_PARAMETER;
841 break;
842 }
843
844 ULONG port = *(uint32_t *)pvBuffer;
845
846 server->mVRDPBindPort = port;
847
848 rc = VINF_SUCCESS;
849
850 if (pcbOut)
851 {
852 *pcbOut = sizeof(uint32_t);
853 }
854
855 server->mConsole->onVRDEServerInfoChange();
856 } break;
857
858 default:
859 break;
860 }
861
862 return rc;
863}
864
865DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackClientLogon(void *pvCallback, uint32_t u32ClientId, const char *pszUser, const char *pszPassword, const char *pszDomain)
866{
867 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
868
869 return server->mConsole->VRDPClientLogon(u32ClientId, pszUser, pszPassword, pszDomain);
870}
871
872DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackClientConnect(void *pvCallback, uint32_t u32ClientId)
873{
874 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
875
876 server->mConsole->VRDPClientConnect(u32ClientId);
877}
878
879DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackClientDisconnect(void *pvCallback, uint32_t u32ClientId, uint32_t fu32Intercepted)
880{
881 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
882
883 server->mConsole->VRDPClientDisconnect(u32ClientId, fu32Intercepted);
884
885 if (ASMAtomicReadU32(&server->mu32AudioInputClientId) == u32ClientId)
886 {
887 Log(("AUDIOIN: disconnected client %u\n", u32ClientId));
888 ASMAtomicWriteU32(&server->mu32AudioInputClientId, 0);
889
890 PPDMIAUDIOSNIFFERPORT pPort = server->mConsole->getAudioSniffer()->getAudioSnifferPort();
891 if (pPort)
892 {
893 pPort->pfnAudioInputIntercept(pPort, false);
894 }
895 else
896 {
897 AssertFailed();
898 }
899 }
900}
901
902DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackIntercept(void *pvCallback, uint32_t u32ClientId, uint32_t fu32Intercept, void **ppvIntercept)
903{
904 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
905
906 LogFlowFunc(("%x\n", fu32Intercept));
907
908 int rc = VERR_NOT_SUPPORTED;
909
910 switch (fu32Intercept)
911 {
912 case VRDE_CLIENT_INTERCEPT_AUDIO:
913 {
914 server->mConsole->VRDPInterceptAudio(u32ClientId);
915 if (ppvIntercept)
916 {
917 *ppvIntercept = server;
918 }
919 rc = VINF_SUCCESS;
920 } break;
921
922 case VRDE_CLIENT_INTERCEPT_USB:
923 {
924 server->mConsole->VRDPInterceptUSB(u32ClientId, ppvIntercept);
925 rc = VINF_SUCCESS;
926 } break;
927
928 case VRDE_CLIENT_INTERCEPT_CLIPBOARD:
929 {
930 server->mConsole->VRDPInterceptClipboard(u32ClientId);
931 if (ppvIntercept)
932 {
933 *ppvIntercept = server;
934 }
935 rc = VINF_SUCCESS;
936 } break;
937
938 case VRDE_CLIENT_INTERCEPT_AUDIO_INPUT:
939 {
940 /* This request is processed internally by the ConsoleVRDPServer.
941 * Only one client is allowed to intercept audio input.
942 */
943 if (ASMAtomicCmpXchgU32(&server->mu32AudioInputClientId, u32ClientId, 0) == true)
944 {
945 Log(("AUDIOIN: connected client %u\n", u32ClientId));
946
947 PPDMIAUDIOSNIFFERPORT pPort = server->mConsole->getAudioSniffer()->getAudioSnifferPort();
948 if (pPort)
949 {
950 pPort->pfnAudioInputIntercept(pPort, true);
951 if (ppvIntercept)
952 {
953 *ppvIntercept = server;
954 }
955 }
956 else
957 {
958 AssertFailed();
959 ASMAtomicWriteU32(&server->mu32AudioInputClientId, 0);
960 rc = VERR_NOT_SUPPORTED;
961 }
962 }
963 else
964 {
965 Log(("AUDIOIN: ignored client %u, active client %u\n", u32ClientId, server->mu32AudioInputClientId));
966 rc = VERR_NOT_SUPPORTED;
967 }
968 } break;
969
970 default:
971 break;
972 }
973
974 return rc;
975}
976
977DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackUSB(void *pvCallback, void *pvIntercept, uint32_t u32ClientId, uint8_t u8Code, const void *pvRet, uint32_t cbRet)
978{
979#ifdef VBOX_WITH_USB
980 return USBClientResponseCallback(pvIntercept, u32ClientId, u8Code, pvRet, cbRet);
981#else
982 return VERR_NOT_SUPPORTED;
983#endif
984}
985
986DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackClipboard(void *pvCallback, void *pvIntercept, uint32_t u32ClientId, uint32_t u32Function, uint32_t u32Format, const void *pvData, uint32_t cbData)
987{
988 return ClipboardCallback(pvIntercept, u32ClientId, u32Function, u32Format, pvData, cbData);
989}
990
991DECLCALLBACK(bool) ConsoleVRDPServer::VRDPCallbackFramebufferQuery(void *pvCallback, unsigned uScreenId, VRDEFRAMEBUFFERINFO *pInfo)
992{
993 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
994
995 bool fAvailable = false;
996
997 IFramebuffer *pfb = NULL;
998 LONG xOrigin = 0;
999 LONG yOrigin = 0;
1000
1001 server->mConsole->getDisplay()->GetFramebuffer(uScreenId, &pfb, &xOrigin, &yOrigin);
1002
1003 if (pfb)
1004 {
1005 pfb->Lock ();
1006
1007 /* Query framebuffer parameters. */
1008 ULONG lineSize = 0;
1009 pfb->COMGETTER(BytesPerLine)(&lineSize);
1010
1011 ULONG bitsPerPixel = 0;
1012 pfb->COMGETTER(BitsPerPixel)(&bitsPerPixel);
1013
1014 BYTE *address = NULL;
1015 pfb->COMGETTER(Address)(&address);
1016
1017 ULONG height = 0;
1018 pfb->COMGETTER(Height)(&height);
1019
1020 ULONG width = 0;
1021 pfb->COMGETTER(Width)(&width);
1022
1023 /* Now fill the information as requested by the caller. */
1024 pInfo->pu8Bits = address;
1025 pInfo->xOrigin = xOrigin;
1026 pInfo->yOrigin = yOrigin;
1027 pInfo->cWidth = width;
1028 pInfo->cHeight = height;
1029 pInfo->cBitsPerPixel = bitsPerPixel;
1030 pInfo->cbLine = lineSize;
1031
1032 pfb->Unlock();
1033
1034 fAvailable = true;
1035 }
1036
1037 if (server->maFramebuffers[uScreenId])
1038 {
1039 server->maFramebuffers[uScreenId]->Release();
1040 }
1041 server->maFramebuffers[uScreenId] = pfb;
1042
1043 return fAvailable;
1044}
1045
1046DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackFramebufferLock(void *pvCallback, unsigned uScreenId)
1047{
1048 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1049
1050 if (server->maFramebuffers[uScreenId])
1051 {
1052 server->maFramebuffers[uScreenId]->Lock();
1053 }
1054}
1055
1056DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackFramebufferUnlock(void *pvCallback, unsigned uScreenId)
1057{
1058 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1059
1060 if (server->maFramebuffers[uScreenId])
1061 {
1062 server->maFramebuffers[uScreenId]->Unlock();
1063 }
1064}
1065
1066static void fixKbdLockStatus(VRDPInputSynch *pInputSynch, IKeyboard *pKeyboard)
1067{
1068 if ( pInputSynch->cGuestNumLockAdaptions
1069 && (pInputSynch->fGuestNumLock != pInputSynch->fClientNumLock))
1070 {
1071 pInputSynch->cGuestNumLockAdaptions--;
1072 pKeyboard->PutScancode(0x45);
1073 pKeyboard->PutScancode(0x45 | 0x80);
1074 }
1075 if ( pInputSynch->cGuestCapsLockAdaptions
1076 && (pInputSynch->fGuestCapsLock != pInputSynch->fClientCapsLock))
1077 {
1078 pInputSynch->cGuestCapsLockAdaptions--;
1079 pKeyboard->PutScancode(0x3a);
1080 pKeyboard->PutScancode(0x3a | 0x80);
1081 }
1082}
1083
1084DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackInput(void *pvCallback, int type, const void *pvInput, unsigned cbInput)
1085{
1086 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1087 Console *pConsole = server->mConsole;
1088
1089 switch (type)
1090 {
1091 case VRDE_INPUT_SCANCODE:
1092 {
1093 if (cbInput == sizeof(VRDEINPUTSCANCODE))
1094 {
1095 IKeyboard *pKeyboard = pConsole->getKeyboard();
1096
1097 const VRDEINPUTSCANCODE *pInputScancode = (VRDEINPUTSCANCODE *)pvInput;
1098
1099 /* Track lock keys. */
1100 if (pInputScancode->uScancode == 0x45)
1101 {
1102 server->m_InputSynch.fClientNumLock = !server->m_InputSynch.fClientNumLock;
1103 }
1104 else if (pInputScancode->uScancode == 0x3a)
1105 {
1106 server->m_InputSynch.fClientCapsLock = !server->m_InputSynch.fClientCapsLock;
1107 }
1108 else if (pInputScancode->uScancode == 0x46)
1109 {
1110 server->m_InputSynch.fClientScrollLock = !server->m_InputSynch.fClientScrollLock;
1111 }
1112 else if ((pInputScancode->uScancode & 0x80) == 0)
1113 {
1114 /* Key pressed. */
1115 fixKbdLockStatus(&server->m_InputSynch, pKeyboard);
1116 }
1117
1118 pKeyboard->PutScancode((LONG)pInputScancode->uScancode);
1119 }
1120 } break;
1121
1122 case VRDE_INPUT_POINT:
1123 {
1124 if (cbInput == sizeof(VRDEINPUTPOINT))
1125 {
1126 const VRDEINPUTPOINT *pInputPoint = (VRDEINPUTPOINT *)pvInput;
1127
1128 int mouseButtons = 0;
1129 int iWheel = 0;
1130
1131 if (pInputPoint->uButtons & VRDE_INPUT_POINT_BUTTON1)
1132 {
1133 mouseButtons |= MouseButtonState_LeftButton;
1134 }
1135 if (pInputPoint->uButtons & VRDE_INPUT_POINT_BUTTON2)
1136 {
1137 mouseButtons |= MouseButtonState_RightButton;
1138 }
1139 if (pInputPoint->uButtons & VRDE_INPUT_POINT_BUTTON3)
1140 {
1141 mouseButtons |= MouseButtonState_MiddleButton;
1142 }
1143 if (pInputPoint->uButtons & VRDE_INPUT_POINT_WHEEL_UP)
1144 {
1145 mouseButtons |= MouseButtonState_WheelUp;
1146 iWheel = -1;
1147 }
1148 if (pInputPoint->uButtons & VRDE_INPUT_POINT_WHEEL_DOWN)
1149 {
1150 mouseButtons |= MouseButtonState_WheelDown;
1151 iWheel = 1;
1152 }
1153
1154 if (server->m_fGuestWantsAbsolute)
1155 {
1156 pConsole->getMouse()->PutMouseEventAbsolute(pInputPoint->x + 1, pInputPoint->y + 1, iWheel, 0 /* Horizontal wheel */, mouseButtons);
1157 } else
1158 {
1159 pConsole->getMouse()->PutMouseEvent(pInputPoint->x - server->m_mousex,
1160 pInputPoint->y - server->m_mousey,
1161 iWheel, 0 /* Horizontal wheel */, mouseButtons);
1162 server->m_mousex = pInputPoint->x;
1163 server->m_mousey = pInputPoint->y;
1164 }
1165 }
1166 } break;
1167
1168 case VRDE_INPUT_CAD:
1169 {
1170 pConsole->getKeyboard()->PutCAD();
1171 } break;
1172
1173 case VRDE_INPUT_RESET:
1174 {
1175 pConsole->Reset();
1176 } break;
1177
1178 case VRDE_INPUT_SYNCH:
1179 {
1180 if (cbInput == sizeof(VRDEINPUTSYNCH))
1181 {
1182 IKeyboard *pKeyboard = pConsole->getKeyboard();
1183
1184 const VRDEINPUTSYNCH *pInputSynch = (VRDEINPUTSYNCH *)pvInput;
1185
1186 server->m_InputSynch.fClientNumLock = (pInputSynch->uLockStatus & VRDE_INPUT_SYNCH_NUMLOCK) != 0;
1187 server->m_InputSynch.fClientCapsLock = (pInputSynch->uLockStatus & VRDE_INPUT_SYNCH_CAPITAL) != 0;
1188 server->m_InputSynch.fClientScrollLock = (pInputSynch->uLockStatus & VRDE_INPUT_SYNCH_SCROLL) != 0;
1189
1190 /* The client initiated synchronization. Always make the guest to reflect the client state.
1191 * Than means, when the guest changes the state itself, it is forced to return to the client
1192 * state.
1193 */
1194 if (server->m_InputSynch.fClientNumLock != server->m_InputSynch.fGuestNumLock)
1195 {
1196 server->m_InputSynch.cGuestNumLockAdaptions = 2;
1197 }
1198
1199 if (server->m_InputSynch.fClientCapsLock != server->m_InputSynch.fGuestCapsLock)
1200 {
1201 server->m_InputSynch.cGuestCapsLockAdaptions = 2;
1202 }
1203
1204 fixKbdLockStatus(&server->m_InputSynch, pKeyboard);
1205 }
1206 } break;
1207
1208 default:
1209 break;
1210 }
1211}
1212
1213DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackVideoModeHint(void *pvCallback, unsigned cWidth, unsigned cHeight, unsigned cBitsPerPixel, unsigned uScreenId)
1214{
1215 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1216
1217 server->mConsole->getDisplay()->SetVideoModeHint(cWidth, cHeight, cBitsPerPixel, uScreenId);
1218}
1219
1220DECLCALLBACK(void) ConsoleVRDPServer::VRDECallbackAudioIn(void *pvCallback,
1221 void *pvCtx,
1222 uint32_t u32ClientId,
1223 uint32_t u32Event,
1224 const void *pvData,
1225 uint32_t cbData)
1226{
1227 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1228
1229 PPDMIAUDIOSNIFFERPORT pPort = server->mConsole->getAudioSniffer()->getAudioSnifferPort();
1230
1231 switch (u32Event)
1232 {
1233 case VRDE_AUDIOIN_BEGIN:
1234 {
1235 const VRDEAUDIOINBEGIN *pParms = (const VRDEAUDIOINBEGIN *)pvData;
1236
1237 pPort->pfnAudioInputEventBegin (pPort, pvCtx,
1238 VRDE_AUDIO_FMT_SAMPLE_FREQ(pParms->fmt),
1239 VRDE_AUDIO_FMT_CHANNELS(pParms->fmt),
1240 VRDE_AUDIO_FMT_BITS_PER_SAMPLE(pParms->fmt),
1241 VRDE_AUDIO_FMT_SIGNED(pParms->fmt)
1242 );
1243 } break;
1244
1245 case VRDE_AUDIOIN_DATA:
1246 {
1247 pPort->pfnAudioInputEventData (pPort, pvCtx, pvData, cbData);
1248 } break;
1249
1250 case VRDE_AUDIOIN_END:
1251 {
1252 pPort->pfnAudioInputEventEnd (pPort, pvCtx);
1253 } break;
1254
1255 default:
1256 return;
1257 }
1258}
1259
1260
1261ConsoleVRDPServer::ConsoleVRDPServer(Console *console)
1262{
1263 mConsole = console;
1264
1265 int rc = RTCritSectInit(&mCritSect);
1266 AssertRC(rc);
1267
1268 mcClipboardRefs = 0;
1269 mpfnClipboardCallback = NULL;
1270
1271#ifdef VBOX_WITH_USB
1272 mUSBBackends.pHead = NULL;
1273 mUSBBackends.pTail = NULL;
1274
1275 mUSBBackends.thread = NIL_RTTHREAD;
1276 mUSBBackends.fThreadRunning = false;
1277 mUSBBackends.event = 0;
1278#endif
1279
1280 mhServer = 0;
1281 mServerInterfaceVersion = 0;
1282
1283 m_fGuestWantsAbsolute = false;
1284 m_mousex = 0;
1285 m_mousey = 0;
1286
1287 m_InputSynch.cGuestNumLockAdaptions = 2;
1288 m_InputSynch.cGuestCapsLockAdaptions = 2;
1289
1290 m_InputSynch.fGuestNumLock = false;
1291 m_InputSynch.fGuestCapsLock = false;
1292 m_InputSynch.fGuestScrollLock = false;
1293
1294 m_InputSynch.fClientNumLock = false;
1295 m_InputSynch.fClientCapsLock = false;
1296 m_InputSynch.fClientScrollLock = false;
1297
1298 memset(maFramebuffers, 0, sizeof(maFramebuffers));
1299
1300 {
1301 ComPtr<IEventSource> es;
1302 console->COMGETTER(EventSource)(es.asOutParam());
1303 ComObjPtr<VRDPConsoleListenerImpl> aConsoleListener;
1304 aConsoleListener.createObject();
1305 aConsoleListener->init(new VRDPConsoleListener(), this);
1306 mConsoleListener = aConsoleListener;
1307 com::SafeArray <VBoxEventType_T> eventTypes;
1308 eventTypes.push_back(VBoxEventType_OnMousePointerShapeChanged);
1309 eventTypes.push_back(VBoxEventType_OnMouseCapabilityChanged);
1310 eventTypes.push_back(VBoxEventType_OnKeyboardLedsChanged);
1311 es->RegisterListener(mConsoleListener, ComSafeArrayAsInParam(eventTypes), true);
1312 }
1313
1314 mVRDPBindPort = -1;
1315
1316 mAuthLibrary = 0;
1317
1318 mu32AudioInputClientId = 0;
1319
1320 /*
1321 * Optional interfaces.
1322 */
1323 m_fInterfaceImage = false;
1324 memset(&m_interfaceImage, 0, sizeof (m_interfaceImage));
1325 memset(&m_interfaceCallbacksImage, 0, sizeof (m_interfaceCallbacksImage));
1326}
1327
1328ConsoleVRDPServer::~ConsoleVRDPServer()
1329{
1330 Stop();
1331
1332 if (mConsoleListener)
1333 {
1334 ComPtr<IEventSource> es;
1335 mConsole->COMGETTER(EventSource)(es.asOutParam());
1336 es->UnregisterListener(mConsoleListener);
1337 mConsoleListener.setNull();
1338 }
1339
1340 unsigned i;
1341 for (i = 0; i < RT_ELEMENTS(maFramebuffers); i++)
1342 {
1343 if (maFramebuffers[i])
1344 {
1345 maFramebuffers[i]->Release();
1346 maFramebuffers[i] = NULL;
1347 }
1348 }
1349
1350 if (RTCritSectIsInitialized(&mCritSect))
1351 {
1352 RTCritSectDelete(&mCritSect);
1353 memset(&mCritSect, 0, sizeof(mCritSect));
1354 }
1355}
1356
1357int ConsoleVRDPServer::Launch(void)
1358{
1359 LogFlowThisFunc(("\n"));
1360
1361 IVRDEServer *server = mConsole->getVRDEServer();
1362 AssertReturn(server, VERR_INTERNAL_ERROR_2);
1363
1364 /*
1365 * Check if VRDE is enabled.
1366 */
1367 BOOL fEnabled;
1368 HRESULT hrc = server->COMGETTER(Enabled)(&fEnabled);
1369 AssertComRCReturn(hrc, Global::vboxStatusCodeFromCOM(hrc));
1370 if (!fEnabled)
1371 return VINF_SUCCESS;
1372
1373 /*
1374 * Check that a VRDE extension pack name is set and resolve it into a
1375 * library path.
1376 */
1377 Bstr bstrExtPack;
1378 hrc = server->COMGETTER(VRDEExtPack)(bstrExtPack.asOutParam());
1379 if (FAILED(hrc))
1380 return Global::vboxStatusCodeFromCOM(hrc);
1381 if (bstrExtPack.isEmpty())
1382 return VINF_NOT_SUPPORTED;
1383
1384 Utf8Str strExtPack(bstrExtPack);
1385 Utf8Str strVrdeLibrary;
1386 int vrc = VINF_SUCCESS;
1387 if (strExtPack.equals(VBOXVRDP_KLUDGE_EXTPACK_NAME))
1388 strVrdeLibrary = "VBoxVRDP";
1389 else
1390 {
1391#ifdef VBOX_WITH_EXTPACK
1392 ExtPackManager *pExtPackMgr = mConsole->getExtPackManager();
1393 vrc = pExtPackMgr->getVrdeLibraryPathForExtPack(&strExtPack, &strVrdeLibrary);
1394#else
1395 vrc = VERR_FILE_NOT_FOUND;
1396#endif
1397 }
1398 if (RT_SUCCESS(vrc))
1399 {
1400 /*
1401 * Load the VRDE library and start the server, if it is enabled.
1402 */
1403 vrc = loadVRDPLibrary(strVrdeLibrary.c_str());
1404 if (RT_SUCCESS(vrc))
1405 {
1406 VRDEENTRYPOINTS_4 *pEntryPoints4;
1407 vrc = mpfnVRDECreateServer(&mCallbacks.header, this, (VRDEINTERFACEHDR **)&pEntryPoints4, &mhServer);
1408
1409 if (RT_SUCCESS(vrc))
1410 {
1411 mServerInterfaceVersion = 4;
1412 mEntryPoints = *pEntryPoints4;
1413 mpEntryPoints = &mEntryPoints;
1414 }
1415 else if (vrc == VERR_VERSION_MISMATCH)
1416 {
1417 /* An older version of VRDE is installed, try version 3. */
1418 VRDEENTRYPOINTS_3 *pEntryPoints3;
1419
1420 static VRDECALLBACKS_3 sCallbacks3 =
1421 {
1422 { VRDE_INTERFACE_VERSION_3, sizeof(VRDECALLBACKS_3) },
1423 ConsoleVRDPServer::VRDPCallbackQueryProperty,
1424 ConsoleVRDPServer::VRDPCallbackClientLogon,
1425 ConsoleVRDPServer::VRDPCallbackClientConnect,
1426 ConsoleVRDPServer::VRDPCallbackClientDisconnect,
1427 ConsoleVRDPServer::VRDPCallbackIntercept,
1428 ConsoleVRDPServer::VRDPCallbackUSB,
1429 ConsoleVRDPServer::VRDPCallbackClipboard,
1430 ConsoleVRDPServer::VRDPCallbackFramebufferQuery,
1431 ConsoleVRDPServer::VRDPCallbackFramebufferLock,
1432 ConsoleVRDPServer::VRDPCallbackFramebufferUnlock,
1433 ConsoleVRDPServer::VRDPCallbackInput,
1434 ConsoleVRDPServer::VRDPCallbackVideoModeHint,
1435 ConsoleVRDPServer::VRDECallbackAudioIn
1436 };
1437
1438 vrc = mpfnVRDECreateServer(&sCallbacks3.header, this, (VRDEINTERFACEHDR **)&pEntryPoints3, &mhServer);
1439 if (RT_SUCCESS(vrc))
1440 {
1441 mServerInterfaceVersion = 3;
1442 mEntryPoints.header = pEntryPoints3->header;
1443 mEntryPoints.VRDEDestroy = pEntryPoints3->VRDEDestroy;
1444 mEntryPoints.VRDEEnableConnections = pEntryPoints3->VRDEEnableConnections;
1445 mEntryPoints.VRDEDisconnect = pEntryPoints3->VRDEDisconnect;
1446 mEntryPoints.VRDEResize = pEntryPoints3->VRDEResize;
1447 mEntryPoints.VRDEUpdate = pEntryPoints3->VRDEUpdate;
1448 mEntryPoints.VRDEColorPointer = pEntryPoints3->VRDEColorPointer;
1449 mEntryPoints.VRDEHidePointer = pEntryPoints3->VRDEHidePointer;
1450 mEntryPoints.VRDEAudioSamples = pEntryPoints3->VRDEAudioSamples;
1451 mEntryPoints.VRDEAudioVolume = pEntryPoints3->VRDEAudioVolume;
1452 mEntryPoints.VRDEUSBRequest = pEntryPoints3->VRDEUSBRequest;
1453 mEntryPoints.VRDEClipboard = pEntryPoints3->VRDEClipboard;
1454 mEntryPoints.VRDEQueryInfo = pEntryPoints3->VRDEQueryInfo;
1455 mEntryPoints.VRDERedirect = pEntryPoints3->VRDERedirect;
1456 mEntryPoints.VRDEAudioInOpen = pEntryPoints3->VRDEAudioInOpen;
1457 mEntryPoints.VRDEAudioInClose = pEntryPoints3->VRDEAudioInClose;
1458 mEntryPoints.VRDEGetInterface = NULL;
1459 mpEntryPoints = &mEntryPoints;
1460 }
1461 else if (vrc == VERR_VERSION_MISMATCH)
1462 {
1463 /* An older version of VRDE is installed, try version 1. */
1464 VRDEENTRYPOINTS_1 *pEntryPoints1;
1465
1466 static VRDECALLBACKS_1 sCallbacks1 =
1467 {
1468 { VRDE_INTERFACE_VERSION_1, sizeof(VRDECALLBACKS_1) },
1469 ConsoleVRDPServer::VRDPCallbackQueryProperty,
1470 ConsoleVRDPServer::VRDPCallbackClientLogon,
1471 ConsoleVRDPServer::VRDPCallbackClientConnect,
1472 ConsoleVRDPServer::VRDPCallbackClientDisconnect,
1473 ConsoleVRDPServer::VRDPCallbackIntercept,
1474 ConsoleVRDPServer::VRDPCallbackUSB,
1475 ConsoleVRDPServer::VRDPCallbackClipboard,
1476 ConsoleVRDPServer::VRDPCallbackFramebufferQuery,
1477 ConsoleVRDPServer::VRDPCallbackFramebufferLock,
1478 ConsoleVRDPServer::VRDPCallbackFramebufferUnlock,
1479 ConsoleVRDPServer::VRDPCallbackInput,
1480 ConsoleVRDPServer::VRDPCallbackVideoModeHint
1481 };
1482
1483 vrc = mpfnVRDECreateServer(&sCallbacks1.header, this, (VRDEINTERFACEHDR **)&pEntryPoints1, &mhServer);
1484 if (RT_SUCCESS(vrc))
1485 {
1486 mServerInterfaceVersion = 1;
1487 mEntryPoints.header = pEntryPoints1->header;
1488 mEntryPoints.VRDEDestroy = pEntryPoints1->VRDEDestroy;
1489 mEntryPoints.VRDEEnableConnections = pEntryPoints1->VRDEEnableConnections;
1490 mEntryPoints.VRDEDisconnect = pEntryPoints1->VRDEDisconnect;
1491 mEntryPoints.VRDEResize = pEntryPoints1->VRDEResize;
1492 mEntryPoints.VRDEUpdate = pEntryPoints1->VRDEUpdate;
1493 mEntryPoints.VRDEColorPointer = pEntryPoints1->VRDEColorPointer;
1494 mEntryPoints.VRDEHidePointer = pEntryPoints1->VRDEHidePointer;
1495 mEntryPoints.VRDEAudioSamples = pEntryPoints1->VRDEAudioSamples;
1496 mEntryPoints.VRDEAudioVolume = pEntryPoints1->VRDEAudioVolume;
1497 mEntryPoints.VRDEUSBRequest = pEntryPoints1->VRDEUSBRequest;
1498 mEntryPoints.VRDEClipboard = pEntryPoints1->VRDEClipboard;
1499 mEntryPoints.VRDEQueryInfo = pEntryPoints1->VRDEQueryInfo;
1500 mEntryPoints.VRDERedirect = NULL;
1501 mEntryPoints.VRDEAudioInOpen = NULL;
1502 mEntryPoints.VRDEAudioInClose = NULL;
1503 mEntryPoints.VRDEGetInterface = NULL;
1504 mpEntryPoints = &mEntryPoints;
1505 }
1506 }
1507 }
1508
1509 if (RT_SUCCESS(vrc))
1510 {
1511 LogRel(("VRDE: loaded version %d of the server.\n", mServerInterfaceVersion));
1512
1513 if (mServerInterfaceVersion >= 4)
1514 {
1515 /* The server supports optional interfaces. */
1516 Assert(mpEntryPoints->VRDEGetInterface != NULL);
1517
1518 /* Image interface. */
1519 m_interfaceImage.header.u64Version = 1;
1520 m_interfaceImage.header.u64Size = sizeof(m_interfaceImage);
1521
1522 m_interfaceCallbacksImage.header.u64Version = 1;
1523 m_interfaceCallbacksImage.header.u64Size = sizeof(m_interfaceCallbacksImage);
1524 m_interfaceCallbacksImage.VRDEImageCbNotify = VRDEImageCbNotify;
1525
1526 vrc = mpEntryPoints->VRDEGetInterface(mhServer,
1527 VRDE_IMAGE_INTERFACE_NAME,
1528 &m_interfaceImage.header,
1529 &m_interfaceCallbacksImage.header,
1530 this);
1531 if (RT_SUCCESS(vrc))
1532 {
1533 m_fInterfaceImage = true;
1534 }
1535
1536 /* Since these interfaces are optional, it is always a success here. */
1537 vrc = VINF_SUCCESS;
1538 }
1539#ifdef VBOX_WITH_USB
1540 remoteUSBThreadStart();
1541#endif
1542 }
1543 else
1544 {
1545 if (vrc != VERR_NET_ADDRESS_IN_USE)
1546 LogRel(("VRDE: Could not start the server rc = %Rrc\n", vrc));
1547 /* Don't unload the lib, because it prevents us trying again or
1548 because there may be other users? */
1549 }
1550 }
1551 }
1552
1553 return vrc;
1554}
1555
1556typedef struct H3DORInstance
1557{
1558 ConsoleVRDPServer *pThis;
1559 HVRDEIMAGE hImageBitmap;
1560 int32_t x;
1561 int32_t y;
1562 uint32_t w;
1563 uint32_t h;
1564 bool fCreated;
1565} H3DORInstance;
1566
1567/* static */ DECLCALLBACK(void) ConsoleVRDPServer::H3DORBegin(const void *pvContext, void **ppvInstance,
1568 const char *pszFormat)
1569{
1570 LogFlowFunc(("ctx %p\n", pvContext));
1571
1572 H3DORInstance *p = (H3DORInstance *)RTMemAlloc(sizeof (H3DORInstance));
1573
1574 if (p)
1575 {
1576 p->pThis = (ConsoleVRDPServer *)pvContext;
1577 p->hImageBitmap = NULL;
1578 p->x = 0;
1579 p->y = 0;
1580 p->w = 0;
1581 p->h = 0;
1582 p->fCreated = false;
1583
1584 /* Host 3D service passes the actual format of data in this redirect instance.
1585 * That is what will be in the H3DORFrame's parameters pvData and cbData.
1586 */
1587 if (RTStrICmp(pszFormat, H3DOR_FMT_RGBA_TOPDOWN) == 0)
1588 {
1589 /* Accept it. */
1590 }
1591 else
1592 {
1593 RTMemFree(p);
1594 p = NULL;
1595 }
1596 }
1597
1598 /* Caller check this for NULL. */
1599 *ppvInstance = p;
1600}
1601
1602/* static */ DECLCALLBACK(void) ConsoleVRDPServer::H3DORGeometry(void *pvInstance,
1603 int32_t x, int32_t y, uint32_t w, uint32_t h)
1604{
1605 LogFlowFunc(("ins %p %d,%d %dx%d\n", pvInstance, x, y, w, h));
1606
1607 H3DORInstance *p = (H3DORInstance *)pvInstance;
1608 Assert(p);
1609 Assert(p->pThis);
1610
1611 /* @todo find out what to do if size changes to 0x0 from non zero */
1612 if (w == 0 || h == 0)
1613 {
1614 /* Do nothing. */
1615 return;
1616 }
1617
1618 RTRECT rect;
1619 rect.xLeft = x;
1620 rect.yTop = y;
1621 rect.xRight = x + w;
1622 rect.yBottom = y + h;
1623
1624 if (p->hImageBitmap)
1625 {
1626 /* An image handle has been already created,
1627 * check if it has the same size as the reported geometry.
1628 */
1629 if ( p->x == x
1630 && p->y == y
1631 && p->w == w
1632 && p->h == h)
1633 {
1634 LogFlowFunc(("geometry not changed\n"));
1635 /* Do nothing. Continue using the existing handle. */
1636 }
1637 else
1638 {
1639 int rc = p->pThis->m_interfaceImage.VRDEImageGeometrySet(p->hImageBitmap, &rect);
1640 if (RT_SUCCESS(rc))
1641 {
1642 p->x = x;
1643 p->y = y;
1644 p->w = w;
1645 p->h = h;
1646 }
1647 else
1648 {
1649 /* The handle must be recreated. Delete existing handle here. */
1650 p->pThis->m_interfaceImage.VRDEImageHandleClose(p->hImageBitmap);
1651 p->hImageBitmap = NULL;
1652 }
1653 }
1654 }
1655
1656 if (!p->hImageBitmap)
1657 {
1658 /* Create a new bitmap handle. */
1659 uint32_t u32ScreenId = 0; /* @todo clip to corresponding screens.
1660 * Clipping can be done here or in VRDP server.
1661 * If VRDP does clipping, then uScreenId parameter
1662 * is not necessary and coords must be global.
1663 * (have to check which coords are used in opengl service).
1664 * Since all VRDE API uses a ScreenId,
1665 * the clipping must be done here in ConsoleVRDPServer
1666 */
1667 uint32_t fu32CompletionFlags = 0;
1668 int rc = p->pThis->m_interfaceImage.VRDEImageHandleCreate(p->pThis->mhServer,
1669 &p->hImageBitmap,
1670 p,
1671 u32ScreenId,
1672 VRDE_IMAGE_F_CREATE_CONTENT_3D
1673 | VRDE_IMAGE_F_CREATE_WINDOW,
1674 &rect,
1675 VRDE_IMAGE_FMT_ID_BITMAP_BGRA8,
1676 NULL,
1677 0,
1678 &fu32CompletionFlags);
1679 if (RT_SUCCESS(rc))
1680 {
1681 p->x = x;
1682 p->y = y;
1683 p->w = w;
1684 p->h = h;
1685
1686 if ((fu32CompletionFlags & VRDE_IMAGE_F_COMPLETE_ASYNC) == 0)
1687 {
1688 p->fCreated = true;
1689 }
1690 }
1691 else
1692 {
1693 p->hImageBitmap = NULL;
1694 p->w = 0;
1695 p->h = 0;
1696 }
1697 }
1698}
1699
1700/* static */ DECLCALLBACK(void) ConsoleVRDPServer::H3DORVisibleRegion(void *pvInstance,
1701 uint32_t cRects, RTRECT *paRects)
1702{
1703 LogFlowFunc(("ins %p %d\n", pvInstance, cRects));
1704
1705 H3DORInstance *p = (H3DORInstance *)pvInstance;
1706 Assert(p);
1707 Assert(p->pThis);
1708
1709 if (cRects == 0)
1710 {
1711 /* Complete image is visible. */
1712 RTRECT rect;
1713 rect.xLeft = p->x;
1714 rect.yTop = p->y;
1715 rect.xRight = p->x + p->w;
1716 rect.yBottom = p->y + p->h;
1717 p->pThis->m_interfaceImage.VRDEImageRegionSet (p->hImageBitmap,
1718 1,
1719 &rect);
1720 }
1721 else
1722 {
1723 p->pThis->m_interfaceImage.VRDEImageRegionSet (p->hImageBitmap,
1724 cRects,
1725 paRects);
1726 }
1727}
1728
1729/* static */ DECLCALLBACK(void) ConsoleVRDPServer::H3DORFrame(void *pvInstance,
1730 void *pvData, uint32_t cbData)
1731{
1732 LogFlowFunc(("ins %p %p %d\n", pvInstance, pvData, cbData));
1733
1734 H3DORInstance *p = (H3DORInstance *)pvInstance;
1735 Assert(p);
1736 Assert(p->pThis);
1737
1738 /* Currently only a topdown BGR0 bitmap format is supported. */
1739 VRDEIMAGEBITMAP image;
1740
1741 image.cWidth = p->w;
1742 image.cHeight = p->h;
1743 image.pvData = pvData;
1744 image.cbData = cbData;
1745 image.pvScanLine0 = (uint8_t *)pvData + (p->h - 1) * p->w * 4;
1746 image.iScanDelta = -4 * p->w;
1747
1748 p->pThis->m_interfaceImage.VRDEImageUpdate (p->hImageBitmap,
1749 p->x,
1750 p->y,
1751 p->w,
1752 p->h,
1753 &image,
1754 sizeof(VRDEIMAGEBITMAP));
1755}
1756
1757/* static */ DECLCALLBACK(void) ConsoleVRDPServer::H3DOREnd(void *pvInstance)
1758{
1759 LogFlowFunc(("ins %p\n", pvInstance));
1760
1761 H3DORInstance *p = (H3DORInstance *)pvInstance;
1762 Assert(p);
1763 Assert(p->pThis);
1764
1765 p->pThis->m_interfaceImage.VRDEImageHandleClose(p->hImageBitmap);
1766
1767 RTMemFree(p);
1768}
1769
1770/* static */ DECLCALLBACK(int) ConsoleVRDPServer::H3DORContextProperty(const void *pvContext, uint32_t index,
1771 void *pvBuffer, uint32_t cbBuffer, uint32_t *pcbOut)
1772{
1773 int rc = VINF_SUCCESS;
1774
1775 if (index == H3DOR_PROP_FORMATS)
1776 {
1777 /* Return a comma separated list of supported formats. */
1778 static const char *pszSupportedFormats = H3DOR_FMT_RGBA_TOPDOWN;
1779 uint32_t cbOut = (uint32_t)strlen(pszSupportedFormats) + 1;
1780 if (cbOut <= cbBuffer)
1781 {
1782 memcpy(pvBuffer, pszSupportedFormats, cbOut);
1783 }
1784 else
1785 {
1786 rc = VERR_BUFFER_OVERFLOW;
1787 }
1788 *pcbOut = cbOut;
1789 }
1790 else
1791 {
1792 rc = VERR_NOT_SUPPORTED;
1793 }
1794
1795 return rc;
1796}
1797
1798void ConsoleVRDPServer::remote3DRedirect(void)
1799{
1800 if (!m_fInterfaceImage)
1801 {
1802 /* No redirect without corresponding interface. */
1803 return;
1804 }
1805
1806 /* Check if 3D redirection has been enabled. */
1807 com::Bstr bstr;
1808 HRESULT hrc = mConsole->getVRDEServer()->GetVRDEProperty(Bstr("H3DRedirect/Enabled").raw(), bstr.asOutParam());
1809
1810 if (hrc != S_OK)
1811 {
1812 bstr = "";
1813 }
1814
1815 com::Utf8Str value = bstr;
1816
1817 bool fEnabled = RTStrICmp(value.c_str(), "true") == 0
1818 || RTStrICmp(value.c_str(), "1") == 0;
1819
1820 if (!fEnabled)
1821 {
1822 return;
1823 }
1824
1825 /* Tell the host 3D service to redirect output using the ConsoleVRDPServer callbacks. */
1826 H3DOUTPUTREDIRECT outputRedirect =
1827 {
1828 this,
1829 H3DORBegin,
1830 H3DORGeometry,
1831 H3DORVisibleRegion,
1832 H3DORFrame,
1833 H3DOREnd,
1834 H3DORContextProperty
1835 };
1836
1837 VBOXHGCMSVCPARM parm;
1838
1839 parm.type = VBOX_HGCM_SVC_PARM_PTR;
1840 parm.u.pointer.addr = &outputRedirect;
1841 parm.u.pointer.size = sizeof(outputRedirect);
1842
1843 VMMDev *pVMMDev = mConsole->getVMMDev();
1844
1845 if (!pVMMDev)
1846 {
1847 AssertMsgFailed(("remote3DRedirect no vmmdev\n"));
1848 return;
1849 }
1850
1851 int rc = pVMMDev->hgcmHostCall("VBoxSharedCrOpenGL",
1852 SHCRGL_HOST_FN_SET_OUTPUT_REDIRECT,
1853 SHCRGL_CPARMS_SET_OUTPUT_REDIRECT,
1854 &parm);
1855
1856 if (!RT_SUCCESS(rc))
1857 {
1858 AssertMsgFailed(("SHCRGL_HOST_FN_SET_CONSOLE failed with %Rrc\n", rc));
1859 return;
1860 }
1861
1862 LogRel(("VRDE: Enabled 3D redirect.\n"));
1863
1864 return;
1865}
1866
1867/* static */ DECLCALLBACK(int) ConsoleVRDPServer::VRDEImageCbNotify (void *pvContext,
1868 void *pvUser,
1869 HVRDEIMAGE hVideo,
1870 uint32_t u32Id,
1871 void *pvData,
1872 uint32_t cbData)
1873{
1874 LogFlowFunc(("pvContext %p, pvUser %p, hVideo %p, u32Id %u, pvData %p, cbData %d\n",
1875 pvContext, pvUser, hVideo, u32Id, pvData, cbData));
1876
1877 ConsoleVRDPServer *pServer = static_cast<ConsoleVRDPServer*>(pvContext);
1878 H3DORInstance *p = (H3DORInstance *)pvUser;
1879 Assert(p);
1880 Assert(p->pThis);
1881 Assert(p->pThis == pServer);
1882
1883 if (u32Id == VRDE_IMAGE_NOTIFY_HANDLE_CREATE)
1884 {
1885 if (cbData != sizeof(uint32_t))
1886 {
1887 AssertFailed();
1888 return VERR_INVALID_PARAMETER;
1889 }
1890
1891 uint32_t u32StreamId = *(uint32_t *)pvData;
1892 LogFlowFunc(("VRDE_IMAGE_NOTIFY_HANDLE_CREATE u32StreamId %d\n",
1893 u32StreamId));
1894
1895 if (u32StreamId != 0)
1896 {
1897 p->fCreated = true; // @todo not needed?
1898 }
1899 else
1900 {
1901 /* The stream has not been created. */
1902 }
1903 }
1904
1905 return VINF_SUCCESS;
1906}
1907
1908void ConsoleVRDPServer::EnableConnections(void)
1909{
1910 if (mpEntryPoints && mhServer)
1911 {
1912 mpEntryPoints->VRDEEnableConnections(mhServer, true);
1913
1914 /* Redirect 3D output if it is enabled. */
1915 remote3DRedirect();
1916 }
1917}
1918
1919void ConsoleVRDPServer::DisconnectClient(uint32_t u32ClientId, bool fReconnect)
1920{
1921 if (mpEntryPoints && mhServer)
1922 {
1923 mpEntryPoints->VRDEDisconnect(mhServer, u32ClientId, fReconnect);
1924 }
1925}
1926
1927void ConsoleVRDPServer::MousePointerUpdate(const VRDECOLORPOINTER *pPointer)
1928{
1929 if (mpEntryPoints && mhServer)
1930 {
1931 mpEntryPoints->VRDEColorPointer(mhServer, pPointer);
1932 }
1933}
1934
1935void ConsoleVRDPServer::MousePointerHide(void)
1936{
1937 if (mpEntryPoints && mhServer)
1938 {
1939 mpEntryPoints->VRDEHidePointer(mhServer);
1940 }
1941}
1942
1943void ConsoleVRDPServer::Stop(void)
1944{
1945 Assert(VALID_PTR(this)); /** @todo r=bird: there are(/was) some odd cases where this buster was invalid on
1946 * linux. Just remove this when it's 100% sure that problem has been fixed. */
1947 if (mhServer)
1948 {
1949 HVRDESERVER hServer = mhServer;
1950
1951 /* Reset the handle to avoid further calls to the server. */
1952 mhServer = 0;
1953
1954 if (mpEntryPoints && hServer)
1955 {
1956 mpEntryPoints->VRDEDestroy(hServer);
1957 }
1958 }
1959
1960#ifdef VBOX_WITH_USB
1961 remoteUSBThreadStop();
1962#endif /* VBOX_WITH_USB */
1963
1964 mpfnAuthEntry = NULL;
1965 mpfnAuthEntry2 = NULL;
1966 mpfnAuthEntry3 = NULL;
1967
1968 if (mAuthLibrary)
1969 {
1970 RTLdrClose(mAuthLibrary);
1971 mAuthLibrary = 0;
1972 }
1973}
1974
1975/* Worker thread for Remote USB. The thread polls the clients for
1976 * the list of attached USB devices.
1977 * The thread is also responsible for attaching/detaching devices
1978 * to/from the VM.
1979 *
1980 * It is expected that attaching/detaching is not a frequent operation.
1981 *
1982 * The thread is always running when the VRDP server is active.
1983 *
1984 * The thread scans backends and requests the device list every 2 seconds.
1985 *
1986 * When device list is available, the thread calls the Console to process it.
1987 *
1988 */
1989#define VRDP_DEVICE_LIST_PERIOD_MS (2000)
1990
1991#ifdef VBOX_WITH_USB
1992static DECLCALLBACK(int) threadRemoteUSB(RTTHREAD self, void *pvUser)
1993{
1994 ConsoleVRDPServer *pOwner = (ConsoleVRDPServer *)pvUser;
1995
1996 LogFlow(("Console::threadRemoteUSB: start. owner = %p.\n", pOwner));
1997
1998 pOwner->notifyRemoteUSBThreadRunning(self);
1999
2000 while (pOwner->isRemoteUSBThreadRunning())
2001 {
2002 RemoteUSBBackend *pRemoteUSBBackend = NULL;
2003
2004 while ((pRemoteUSBBackend = pOwner->usbBackendGetNext(pRemoteUSBBackend)) != NULL)
2005 {
2006 pRemoteUSBBackend->PollRemoteDevices();
2007 }
2008
2009 pOwner->waitRemoteUSBThreadEvent(VRDP_DEVICE_LIST_PERIOD_MS);
2010
2011 LogFlow(("Console::threadRemoteUSB: iteration. owner = %p.\n", pOwner));
2012 }
2013
2014 return VINF_SUCCESS;
2015}
2016
2017void ConsoleVRDPServer::notifyRemoteUSBThreadRunning(RTTHREAD thread)
2018{
2019 mUSBBackends.thread = thread;
2020 mUSBBackends.fThreadRunning = true;
2021 int rc = RTThreadUserSignal(thread);
2022 AssertRC(rc);
2023}
2024
2025bool ConsoleVRDPServer::isRemoteUSBThreadRunning(void)
2026{
2027 return mUSBBackends.fThreadRunning;
2028}
2029
2030void ConsoleVRDPServer::waitRemoteUSBThreadEvent(RTMSINTERVAL cMillies)
2031{
2032 int rc = RTSemEventWait(mUSBBackends.event, cMillies);
2033 Assert(RT_SUCCESS(rc) || rc == VERR_TIMEOUT);
2034 NOREF(rc);
2035}
2036
2037void ConsoleVRDPServer::remoteUSBThreadStart(void)
2038{
2039 int rc = RTSemEventCreate(&mUSBBackends.event);
2040
2041 if (RT_FAILURE(rc))
2042 {
2043 AssertFailed();
2044 mUSBBackends.event = 0;
2045 }
2046
2047 if (RT_SUCCESS(rc))
2048 {
2049 rc = RTThreadCreate(&mUSBBackends.thread, threadRemoteUSB, this, 65536,
2050 RTTHREADTYPE_VRDP_IO, RTTHREADFLAGS_WAITABLE, "remote usb");
2051 }
2052
2053 if (RT_FAILURE(rc))
2054 {
2055 LogRel(("Warning: could not start the remote USB thread, rc = %Rrc!!!\n", rc));
2056 mUSBBackends.thread = NIL_RTTHREAD;
2057 }
2058 else
2059 {
2060 /* Wait until the thread is ready. */
2061 rc = RTThreadUserWait(mUSBBackends.thread, 60000);
2062 AssertRC(rc);
2063 Assert (mUSBBackends.fThreadRunning || RT_FAILURE(rc));
2064 }
2065}
2066
2067void ConsoleVRDPServer::remoteUSBThreadStop(void)
2068{
2069 mUSBBackends.fThreadRunning = false;
2070
2071 if (mUSBBackends.thread != NIL_RTTHREAD)
2072 {
2073 Assert (mUSBBackends.event != 0);
2074
2075 RTSemEventSignal(mUSBBackends.event);
2076
2077 int rc = RTThreadWait(mUSBBackends.thread, 60000, NULL);
2078 AssertRC(rc);
2079
2080 mUSBBackends.thread = NIL_RTTHREAD;
2081 }
2082
2083 if (mUSBBackends.event)
2084 {
2085 RTSemEventDestroy(mUSBBackends.event);
2086 mUSBBackends.event = 0;
2087 }
2088}
2089#endif /* VBOX_WITH_USB */
2090
2091AuthResult ConsoleVRDPServer::Authenticate(const Guid &uuid, AuthGuestJudgement guestJudgement,
2092 const char *pszUser, const char *pszPassword, const char *pszDomain,
2093 uint32_t u32ClientId)
2094{
2095 AUTHUUID rawuuid;
2096
2097 memcpy(rawuuid, uuid.raw(), sizeof(rawuuid));
2098
2099 LogFlow(("ConsoleVRDPServer::Authenticate: uuid = %RTuuid, guestJudgement = %d, pszUser = %s, pszPassword = %s, pszDomain = %s, u32ClientId = %d\n",
2100 rawuuid, guestJudgement, pszUser, pszPassword, pszDomain, u32ClientId));
2101
2102 /*
2103 * Called only from VRDP input thread. So thread safety is not required.
2104 */
2105
2106 if (!mAuthLibrary)
2107 {
2108 /* Load the external authentication library. */
2109 Bstr authLibrary;
2110 mConsole->getVRDEServer()->COMGETTER(AuthLibrary)(authLibrary.asOutParam());
2111
2112 Utf8Str filename = authLibrary;
2113
2114 LogRel(("AUTH: ConsoleVRDPServer::Authenticate: loading external authentication library '%ls'\n", authLibrary.raw()));
2115
2116 int rc;
2117 if (RTPathHavePath(filename.c_str()))
2118 rc = RTLdrLoad(filename.c_str(), &mAuthLibrary);
2119 else
2120 {
2121 rc = RTLdrLoadAppPriv(filename.c_str(), &mAuthLibrary);
2122 if (RT_FAILURE(rc))
2123 {
2124 /* Backward compatibility with old default 'VRDPAuth' name.
2125 * Try to load new default 'VBoxAuth' instead.
2126 */
2127 if (filename == "VRDPAuth")
2128 {
2129 LogRel(("AUTH: ConsoleVRDPServer::Authenticate: loading external authentication library VBoxAuth\n"));
2130 rc = RTLdrLoadAppPriv("VBoxAuth", &mAuthLibrary);
2131 }
2132 }
2133 }
2134
2135 if (RT_FAILURE(rc))
2136 LogRel(("AUTH: Failed to load external authentication library. Error code: %Rrc\n", rc));
2137
2138 if (RT_SUCCESS(rc))
2139 {
2140 typedef struct AuthEntryInfoStruct
2141 {
2142 const char *pszName;
2143 void **ppvAddress;
2144
2145 } AuthEntryInfo;
2146 AuthEntryInfo entries[] =
2147 {
2148 { AUTHENTRY3_NAME, (void **)&mpfnAuthEntry3 },
2149 { AUTHENTRY2_NAME, (void **)&mpfnAuthEntry2 },
2150 { AUTHENTRY_NAME, (void **)&mpfnAuthEntry },
2151 { NULL, NULL }
2152 };
2153
2154 /* Get the entry point. */
2155 AuthEntryInfo *pEntryInfo = &entries[0];
2156 while (pEntryInfo->pszName)
2157 {
2158 *pEntryInfo->ppvAddress = NULL;
2159
2160 int rc2 = RTLdrGetSymbol(mAuthLibrary, pEntryInfo->pszName, pEntryInfo->ppvAddress);
2161 if (RT_SUCCESS(rc2))
2162 {
2163 /* Found an entry point. */
2164 LogRel(("AUTH: Using entry point '%s'.\n", pEntryInfo->pszName));
2165 rc = VINF_SUCCESS;
2166 break;
2167 }
2168
2169 if (rc2 != VERR_SYMBOL_NOT_FOUND)
2170 {
2171 LogRel(("AUTH: Could not resolve import '%s'. Error code: %Rrc\n", pEntryInfo->pszName, rc2));
2172 }
2173 rc = rc2;
2174
2175 pEntryInfo++;
2176 }
2177 }
2178
2179 if (RT_FAILURE(rc))
2180 {
2181 mConsole->setError(E_FAIL,
2182 mConsole->tr("Could not load the external authentication library '%s' (%Rrc)"),
2183 filename.c_str(),
2184 rc);
2185
2186 mpfnAuthEntry = NULL;
2187 mpfnAuthEntry2 = NULL;
2188 mpfnAuthEntry3 = NULL;
2189
2190 if (mAuthLibrary)
2191 {
2192 RTLdrClose(mAuthLibrary);
2193 mAuthLibrary = 0;
2194 }
2195
2196 return AuthResultAccessDenied;
2197 }
2198 }
2199
2200 Assert(mAuthLibrary && (mpfnAuthEntry || mpfnAuthEntry2 || mpfnAuthEntry3));
2201
2202 AuthResult result = AuthResultAccessDenied;
2203 if (mpfnAuthEntry3)
2204 {
2205 result = mpfnAuthEntry3("vrde", &rawuuid, guestJudgement, pszUser, pszPassword, pszDomain, true, u32ClientId);
2206 }
2207 else if (mpfnAuthEntry2)
2208 {
2209 result = mpfnAuthEntry2(&rawuuid, guestJudgement, pszUser, pszPassword, pszDomain, true, u32ClientId);
2210 }
2211 else if (mpfnAuthEntry)
2212 {
2213 result = mpfnAuthEntry(&rawuuid, guestJudgement, pszUser, pszPassword, pszDomain);
2214 }
2215
2216 switch (result)
2217 {
2218 case AuthResultAccessDenied:
2219 LogRel(("AUTH: external authentication module returned 'access denied'\n"));
2220 break;
2221 case AuthResultAccessGranted:
2222 LogRel(("AUTH: external authentication module returned 'access granted'\n"));
2223 break;
2224 case AuthResultDelegateToGuest:
2225 LogRel(("AUTH: external authentication module returned 'delegate request to guest'\n"));
2226 break;
2227 default:
2228 LogRel(("AUTH: external authentication module returned incorrect return code %d\n", result));
2229 result = AuthResultAccessDenied;
2230 }
2231
2232 LogFlow(("ConsoleVRDPServer::Authenticate: result = %d\n", result));
2233
2234 return result;
2235}
2236
2237void ConsoleVRDPServer::AuthDisconnect(const Guid &uuid, uint32_t u32ClientId)
2238{
2239 AUTHUUID rawuuid;
2240
2241 memcpy(rawuuid, uuid.raw(), sizeof(rawuuid));
2242
2243 LogFlow(("ConsoleVRDPServer::AuthDisconnect: uuid = %RTuuid, u32ClientId = %d\n",
2244 rawuuid, u32ClientId));
2245
2246 Assert(mAuthLibrary && (mpfnAuthEntry || mpfnAuthEntry2 || mpfnAuthEntry3));
2247
2248 if (mpfnAuthEntry3)
2249 mpfnAuthEntry3("vrde", &rawuuid, AuthGuestNotAsked, NULL, NULL, NULL, false, u32ClientId);
2250 else if (mpfnAuthEntry2)
2251 mpfnAuthEntry2(&rawuuid, AuthGuestNotAsked, NULL, NULL, NULL, false, u32ClientId);
2252}
2253
2254int ConsoleVRDPServer::lockConsoleVRDPServer(void)
2255{
2256 int rc = RTCritSectEnter(&mCritSect);
2257 AssertRC(rc);
2258 return rc;
2259}
2260
2261void ConsoleVRDPServer::unlockConsoleVRDPServer(void)
2262{
2263 RTCritSectLeave(&mCritSect);
2264}
2265
2266DECLCALLBACK(int) ConsoleVRDPServer::ClipboardCallback(void *pvCallback,
2267 uint32_t u32ClientId,
2268 uint32_t u32Function,
2269 uint32_t u32Format,
2270 const void *pvData,
2271 uint32_t cbData)
2272{
2273 LogFlowFunc(("pvCallback = %p, u32ClientId = %d, u32Function = %d, u32Format = 0x%08X, pvData = %p, cbData = %d\n",
2274 pvCallback, u32ClientId, u32Function, u32Format, pvData, cbData));
2275
2276 int rc = VINF_SUCCESS;
2277
2278 ConsoleVRDPServer *pServer = static_cast <ConsoleVRDPServer *>(pvCallback);
2279
2280 NOREF(u32ClientId);
2281
2282 switch (u32Function)
2283 {
2284 case VRDE_CLIPBOARD_FUNCTION_FORMAT_ANNOUNCE:
2285 {
2286 if (pServer->mpfnClipboardCallback)
2287 {
2288 pServer->mpfnClipboardCallback(VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE,
2289 u32Format,
2290 (void *)pvData,
2291 cbData);
2292 }
2293 } break;
2294
2295 case VRDE_CLIPBOARD_FUNCTION_DATA_READ:
2296 {
2297 if (pServer->mpfnClipboardCallback)
2298 {
2299 pServer->mpfnClipboardCallback(VBOX_CLIPBOARD_EXT_FN_DATA_READ,
2300 u32Format,
2301 (void *)pvData,
2302 cbData);
2303 }
2304 } break;
2305
2306 default:
2307 rc = VERR_NOT_SUPPORTED;
2308 }
2309
2310 return rc;
2311}
2312
2313DECLCALLBACK(int) ConsoleVRDPServer::ClipboardServiceExtension(void *pvExtension,
2314 uint32_t u32Function,
2315 void *pvParms,
2316 uint32_t cbParms)
2317{
2318 LogFlowFunc(("pvExtension = %p, u32Function = %d, pvParms = %p, cbParms = %d\n",
2319 pvExtension, u32Function, pvParms, cbParms));
2320
2321 int rc = VINF_SUCCESS;
2322
2323 ConsoleVRDPServer *pServer = static_cast <ConsoleVRDPServer *>(pvExtension);
2324
2325 VBOXCLIPBOARDEXTPARMS *pParms = (VBOXCLIPBOARDEXTPARMS *)pvParms;
2326
2327 switch (u32Function)
2328 {
2329 case VBOX_CLIPBOARD_EXT_FN_SET_CALLBACK:
2330 {
2331 pServer->mpfnClipboardCallback = pParms->u.pfnCallback;
2332 } break;
2333
2334 case VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE:
2335 {
2336 /* The guest announces clipboard formats. This must be delivered to all clients. */
2337 if (mpEntryPoints && pServer->mhServer)
2338 {
2339 mpEntryPoints->VRDEClipboard(pServer->mhServer,
2340 VRDE_CLIPBOARD_FUNCTION_FORMAT_ANNOUNCE,
2341 pParms->u32Format,
2342 NULL,
2343 0,
2344 NULL);
2345 }
2346 } break;
2347
2348 case VBOX_CLIPBOARD_EXT_FN_DATA_READ:
2349 {
2350 /* The clipboard service expects that the pvData buffer will be filled
2351 * with clipboard data. The server returns the data from the client that
2352 * announced the requested format most recently.
2353 */
2354 if (mpEntryPoints && pServer->mhServer)
2355 {
2356 mpEntryPoints->VRDEClipboard(pServer->mhServer,
2357 VRDE_CLIPBOARD_FUNCTION_DATA_READ,
2358 pParms->u32Format,
2359 pParms->u.pvData,
2360 pParms->cbData,
2361 &pParms->cbData);
2362 }
2363 } break;
2364
2365 case VBOX_CLIPBOARD_EXT_FN_DATA_WRITE:
2366 {
2367 if (mpEntryPoints && pServer->mhServer)
2368 {
2369 mpEntryPoints->VRDEClipboard(pServer->mhServer,
2370 VRDE_CLIPBOARD_FUNCTION_DATA_WRITE,
2371 pParms->u32Format,
2372 pParms->u.pvData,
2373 pParms->cbData,
2374 NULL);
2375 }
2376 } break;
2377
2378 default:
2379 rc = VERR_NOT_SUPPORTED;
2380 }
2381
2382 return rc;
2383}
2384
2385void ConsoleVRDPServer::ClipboardCreate(uint32_t u32ClientId)
2386{
2387 int rc = lockConsoleVRDPServer();
2388
2389 if (RT_SUCCESS(rc))
2390 {
2391 if (mcClipboardRefs == 0)
2392 {
2393 rc = HGCMHostRegisterServiceExtension(&mhClipboard, "VBoxSharedClipboard", ClipboardServiceExtension, this);
2394
2395 if (RT_SUCCESS(rc))
2396 {
2397 mcClipboardRefs++;
2398 }
2399 }
2400
2401 unlockConsoleVRDPServer();
2402 }
2403}
2404
2405void ConsoleVRDPServer::ClipboardDelete(uint32_t u32ClientId)
2406{
2407 int rc = lockConsoleVRDPServer();
2408
2409 if (RT_SUCCESS(rc))
2410 {
2411 mcClipboardRefs--;
2412
2413 if (mcClipboardRefs == 0)
2414 {
2415 HGCMHostUnregisterServiceExtension(mhClipboard);
2416 }
2417
2418 unlockConsoleVRDPServer();
2419 }
2420}
2421
2422/* That is called on INPUT thread of the VRDP server.
2423 * The ConsoleVRDPServer keeps a list of created backend instances.
2424 */
2425void ConsoleVRDPServer::USBBackendCreate(uint32_t u32ClientId, void **ppvIntercept)
2426{
2427#ifdef VBOX_WITH_USB
2428 LogFlow(("ConsoleVRDPServer::USBBackendCreate: u32ClientId = %d\n", u32ClientId));
2429
2430 /* Create a new instance of the USB backend for the new client. */
2431 RemoteUSBBackend *pRemoteUSBBackend = new RemoteUSBBackend(mConsole, this, u32ClientId);
2432
2433 if (pRemoteUSBBackend)
2434 {
2435 pRemoteUSBBackend->AddRef(); /* 'Release' called in USBBackendDelete. */
2436
2437 /* Append the new instance in the list. */
2438 int rc = lockConsoleVRDPServer();
2439
2440 if (RT_SUCCESS(rc))
2441 {
2442 pRemoteUSBBackend->pNext = mUSBBackends.pHead;
2443 if (mUSBBackends.pHead)
2444 {
2445 mUSBBackends.pHead->pPrev = pRemoteUSBBackend;
2446 }
2447 else
2448 {
2449 mUSBBackends.pTail = pRemoteUSBBackend;
2450 }
2451
2452 mUSBBackends.pHead = pRemoteUSBBackend;
2453
2454 unlockConsoleVRDPServer();
2455
2456 if (ppvIntercept)
2457 {
2458 *ppvIntercept = pRemoteUSBBackend;
2459 }
2460 }
2461
2462 if (RT_FAILURE(rc))
2463 {
2464 pRemoteUSBBackend->Release();
2465 }
2466 }
2467#endif /* VBOX_WITH_USB */
2468}
2469
2470void ConsoleVRDPServer::USBBackendDelete(uint32_t u32ClientId)
2471{
2472#ifdef VBOX_WITH_USB
2473 LogFlow(("ConsoleVRDPServer::USBBackendDelete: u32ClientId = %d\n", u32ClientId));
2474
2475 RemoteUSBBackend *pRemoteUSBBackend = NULL;
2476
2477 /* Find the instance. */
2478 int rc = lockConsoleVRDPServer();
2479
2480 if (RT_SUCCESS(rc))
2481 {
2482 pRemoteUSBBackend = usbBackendFind(u32ClientId);
2483
2484 if (pRemoteUSBBackend)
2485 {
2486 /* Notify that it will be deleted. */
2487 pRemoteUSBBackend->NotifyDelete();
2488 }
2489
2490 unlockConsoleVRDPServer();
2491 }
2492
2493 if (pRemoteUSBBackend)
2494 {
2495 /* Here the instance has been excluded from the list and can be dereferenced. */
2496 pRemoteUSBBackend->Release();
2497 }
2498#endif
2499}
2500
2501void *ConsoleVRDPServer::USBBackendRequestPointer(uint32_t u32ClientId, const Guid *pGuid)
2502{
2503#ifdef VBOX_WITH_USB
2504 RemoteUSBBackend *pRemoteUSBBackend = NULL;
2505
2506 /* Find the instance. */
2507 int rc = lockConsoleVRDPServer();
2508
2509 if (RT_SUCCESS(rc))
2510 {
2511 pRemoteUSBBackend = usbBackendFind(u32ClientId);
2512
2513 if (pRemoteUSBBackend)
2514 {
2515 /* Inform the backend instance that it is referenced by the Guid. */
2516 bool fAdded = pRemoteUSBBackend->addUUID(pGuid);
2517
2518 if (fAdded)
2519 {
2520 /* Reference the instance because its pointer is being taken. */
2521 pRemoteUSBBackend->AddRef(); /* 'Release' is called in USBBackendReleasePointer. */
2522 }
2523 else
2524 {
2525 pRemoteUSBBackend = NULL;
2526 }
2527 }
2528
2529 unlockConsoleVRDPServer();
2530 }
2531
2532 if (pRemoteUSBBackend)
2533 {
2534 return pRemoteUSBBackend->GetBackendCallbackPointer();
2535 }
2536
2537#endif
2538 return NULL;
2539}
2540
2541void ConsoleVRDPServer::USBBackendReleasePointer(const Guid *pGuid)
2542{
2543#ifdef VBOX_WITH_USB
2544 RemoteUSBBackend *pRemoteUSBBackend = NULL;
2545
2546 /* Find the instance. */
2547 int rc = lockConsoleVRDPServer();
2548
2549 if (RT_SUCCESS(rc))
2550 {
2551 pRemoteUSBBackend = usbBackendFindByUUID(pGuid);
2552
2553 if (pRemoteUSBBackend)
2554 {
2555 pRemoteUSBBackend->removeUUID(pGuid);
2556 }
2557
2558 unlockConsoleVRDPServer();
2559
2560 if (pRemoteUSBBackend)
2561 {
2562 pRemoteUSBBackend->Release();
2563 }
2564 }
2565#endif
2566}
2567
2568RemoteUSBBackend *ConsoleVRDPServer::usbBackendGetNext(RemoteUSBBackend *pRemoteUSBBackend)
2569{
2570 LogFlow(("ConsoleVRDPServer::usbBackendGetNext: pBackend = %p\n", pRemoteUSBBackend));
2571
2572 RemoteUSBBackend *pNextRemoteUSBBackend = NULL;
2573#ifdef VBOX_WITH_USB
2574
2575 int rc = lockConsoleVRDPServer();
2576
2577 if (RT_SUCCESS(rc))
2578 {
2579 if (pRemoteUSBBackend == NULL)
2580 {
2581 /* The first backend in the list is requested. */
2582 pNextRemoteUSBBackend = mUSBBackends.pHead;
2583 }
2584 else
2585 {
2586 /* Get pointer to the next backend. */
2587 pNextRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
2588 }
2589
2590 if (pNextRemoteUSBBackend)
2591 {
2592 pNextRemoteUSBBackend->AddRef();
2593 }
2594
2595 unlockConsoleVRDPServer();
2596
2597 if (pRemoteUSBBackend)
2598 {
2599 pRemoteUSBBackend->Release();
2600 }
2601 }
2602#endif
2603
2604 return pNextRemoteUSBBackend;
2605}
2606
2607#ifdef VBOX_WITH_USB
2608/* Internal method. Called under the ConsoleVRDPServerLock. */
2609RemoteUSBBackend *ConsoleVRDPServer::usbBackendFind(uint32_t u32ClientId)
2610{
2611 RemoteUSBBackend *pRemoteUSBBackend = mUSBBackends.pHead;
2612
2613 while (pRemoteUSBBackend)
2614 {
2615 if (pRemoteUSBBackend->ClientId() == u32ClientId)
2616 {
2617 break;
2618 }
2619
2620 pRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
2621 }
2622
2623 return pRemoteUSBBackend;
2624}
2625
2626/* Internal method. Called under the ConsoleVRDPServerLock. */
2627RemoteUSBBackend *ConsoleVRDPServer::usbBackendFindByUUID(const Guid *pGuid)
2628{
2629 RemoteUSBBackend *pRemoteUSBBackend = mUSBBackends.pHead;
2630
2631 while (pRemoteUSBBackend)
2632 {
2633 if (pRemoteUSBBackend->findUUID(pGuid))
2634 {
2635 break;
2636 }
2637
2638 pRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
2639 }
2640
2641 return pRemoteUSBBackend;
2642}
2643#endif
2644
2645/* Internal method. Called by the backend destructor. */
2646void ConsoleVRDPServer::usbBackendRemoveFromList(RemoteUSBBackend *pRemoteUSBBackend)
2647{
2648#ifdef VBOX_WITH_USB
2649 int rc = lockConsoleVRDPServer();
2650 AssertRC(rc);
2651
2652 /* Exclude the found instance from the list. */
2653 if (pRemoteUSBBackend->pNext)
2654 {
2655 pRemoteUSBBackend->pNext->pPrev = pRemoteUSBBackend->pPrev;
2656 }
2657 else
2658 {
2659 mUSBBackends.pTail = (RemoteUSBBackend *)pRemoteUSBBackend->pPrev;
2660 }
2661
2662 if (pRemoteUSBBackend->pPrev)
2663 {
2664 pRemoteUSBBackend->pPrev->pNext = pRemoteUSBBackend->pNext;
2665 }
2666 else
2667 {
2668 mUSBBackends.pHead = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
2669 }
2670
2671 pRemoteUSBBackend->pNext = pRemoteUSBBackend->pPrev = NULL;
2672
2673 unlockConsoleVRDPServer();
2674#endif
2675}
2676
2677
2678void ConsoleVRDPServer::SendUpdate(unsigned uScreenId, void *pvUpdate, uint32_t cbUpdate) const
2679{
2680 if (mpEntryPoints && mhServer)
2681 {
2682 mpEntryPoints->VRDEUpdate(mhServer, uScreenId, pvUpdate, cbUpdate);
2683 }
2684}
2685
2686void ConsoleVRDPServer::SendResize(void) const
2687{
2688 if (mpEntryPoints && mhServer)
2689 {
2690 mpEntryPoints->VRDEResize(mhServer);
2691 }
2692}
2693
2694void ConsoleVRDPServer::SendUpdateBitmap(unsigned uScreenId, uint32_t x, uint32_t y, uint32_t w, uint32_t h) const
2695{
2696 VRDEORDERHDR update;
2697 update.x = x;
2698 update.y = y;
2699 update.w = w;
2700 update.h = h;
2701 if (mpEntryPoints && mhServer)
2702 {
2703 mpEntryPoints->VRDEUpdate(mhServer, uScreenId, &update, sizeof(update));
2704 }
2705}
2706
2707void ConsoleVRDPServer::SendAudioSamples(void *pvSamples, uint32_t cSamples, VRDEAUDIOFORMAT format) const
2708{
2709 if (mpEntryPoints && mhServer)
2710 {
2711 mpEntryPoints->VRDEAudioSamples(mhServer, pvSamples, cSamples, format);
2712 }
2713}
2714
2715void ConsoleVRDPServer::SendAudioVolume(uint16_t left, uint16_t right) const
2716{
2717 if (mpEntryPoints && mhServer)
2718 {
2719 mpEntryPoints->VRDEAudioVolume(mhServer, left, right);
2720 }
2721}
2722
2723void ConsoleVRDPServer::SendUSBRequest(uint32_t u32ClientId, void *pvParms, uint32_t cbParms) const
2724{
2725 if (mpEntryPoints && mhServer)
2726 {
2727 mpEntryPoints->VRDEUSBRequest(mhServer, u32ClientId, pvParms, cbParms);
2728 }
2729}
2730
2731/* @todo rc not needed? */
2732int ConsoleVRDPServer::SendAudioInputBegin(void **ppvUserCtx,
2733 void *pvContext,
2734 uint32_t cSamples,
2735 uint32_t iSampleHz,
2736 uint32_t cChannels,
2737 uint32_t cBits)
2738{
2739 if (mpEntryPoints && mhServer && mpEntryPoints->VRDEAudioInOpen)
2740 {
2741 uint32_t u32ClientId = ASMAtomicReadU32(&mu32AudioInputClientId);
2742 if (u32ClientId != 0) /* 0 would mean broadcast to all clients. */
2743 {
2744 VRDEAUDIOFORMAT audioFormat = VRDE_AUDIO_FMT_MAKE(iSampleHz, cChannels, cBits, 0);
2745 mpEntryPoints->VRDEAudioInOpen (mhServer,
2746 pvContext,
2747 u32ClientId,
2748 audioFormat,
2749 cSamples);
2750 *ppvUserCtx = NULL; /* This is the ConsoleVRDPServer context.
2751 * Currently not used because only one client is allowed to
2752 * do audio input and the client id is saved by the ConsoleVRDPServer.
2753 */
2754
2755 return VINF_SUCCESS;
2756 }
2757 }
2758 return VERR_NOT_SUPPORTED;
2759}
2760
2761void ConsoleVRDPServer::SendAudioInputEnd(void *pvUserCtx)
2762{
2763 if (mpEntryPoints && mhServer && mpEntryPoints->VRDEAudioInClose)
2764 {
2765 uint32_t u32ClientId = ASMAtomicReadU32(&mu32AudioInputClientId);
2766 if (u32ClientId != 0) /* 0 would mean broadcast to all clients. */
2767 {
2768 mpEntryPoints->VRDEAudioInClose(mhServer, u32ClientId);
2769 }
2770 }
2771}
2772
2773#ifdef VBOX_WITH_USB_VIDEO
2774int ConsoleVRDPServer::GetVideoFrameDimensions(uint16_t *pu16Heigh, uint16_t *pu16Width)
2775{
2776 *pu16Heigh = 640;
2777 *pu16Width = 480;
2778 return VINF_SUCCESS;
2779}
2780
2781int ConsoleVRDPServer::SendVideoSreamOn(bool fFetch)
2782{
2783 /* Here we inform server that guest is starting/stopping
2784 * the stream
2785 */
2786 return VINF_SUCCESS;
2787}
2788#endif
2789
2790
2791
2792void ConsoleVRDPServer::QueryInfo(uint32_t index, void *pvBuffer, uint32_t cbBuffer, uint32_t *pcbOut) const
2793{
2794 if (index == VRDE_QI_PORT)
2795 {
2796 uint32_t cbOut = sizeof(int32_t);
2797
2798 if (cbBuffer >= cbOut)
2799 {
2800 *pcbOut = cbOut;
2801 *(int32_t *)pvBuffer = (int32_t)mVRDPBindPort;
2802 }
2803 }
2804 else if (mpEntryPoints && mhServer)
2805 {
2806 mpEntryPoints->VRDEQueryInfo(mhServer, index, pvBuffer, cbBuffer, pcbOut);
2807 }
2808}
2809
2810/* static */ int ConsoleVRDPServer::loadVRDPLibrary(const char *pszLibraryName)
2811{
2812 int rc = VINF_SUCCESS;
2813
2814 if (mVRDPLibrary == NIL_RTLDRMOD)
2815 {
2816 RTERRINFOSTATIC ErrInfo;
2817 RTErrInfoInitStatic(&ErrInfo);
2818
2819 if (RTPathHavePath(pszLibraryName))
2820 rc = SUPR3HardenedLdrLoadPlugIn(pszLibraryName, &mVRDPLibrary, &ErrInfo.Core);
2821 else
2822 rc = SUPR3HardenedLdrLoadAppPriv(pszLibraryName, &mVRDPLibrary, RTLDRLOAD_FLAGS_LOCAL, &ErrInfo.Core);
2823 if (RT_SUCCESS(rc))
2824 {
2825 struct SymbolEntry
2826 {
2827 const char *name;
2828 void **ppfn;
2829 };
2830
2831 #define DEFSYMENTRY(a) { #a, (void**)&mpfn##a }
2832
2833 static const struct SymbolEntry s_aSymbols[] =
2834 {
2835 DEFSYMENTRY(VRDECreateServer)
2836 };
2837
2838 #undef DEFSYMENTRY
2839
2840 for (unsigned i = 0; i < RT_ELEMENTS(s_aSymbols); i++)
2841 {
2842 rc = RTLdrGetSymbol(mVRDPLibrary, s_aSymbols[i].name, s_aSymbols[i].ppfn);
2843
2844 if (RT_FAILURE(rc))
2845 {
2846 LogRel(("VRDE: Error resolving symbol '%s', rc %Rrc.\n", s_aSymbols[i].name, rc));
2847 break;
2848 }
2849 }
2850 }
2851 else
2852 {
2853 if (RTErrInfoIsSet(&ErrInfo.Core))
2854 LogRel(("VRDE: Error loading the library '%s': %s (%Rrc)\n", pszLibraryName, ErrInfo.Core.pszMsg, rc));
2855 else
2856 LogRel(("VRDE: Error loading the library '%s' rc = %Rrc.\n", pszLibraryName, rc));
2857
2858 mVRDPLibrary = NIL_RTLDRMOD;
2859 }
2860 }
2861
2862 if (RT_FAILURE(rc))
2863 {
2864 if (mVRDPLibrary != NIL_RTLDRMOD)
2865 {
2866 RTLdrClose(mVRDPLibrary);
2867 mVRDPLibrary = NIL_RTLDRMOD;
2868 }
2869 }
2870
2871 return rc;
2872}
2873
2874/*
2875 * IVRDEServerInfo implementation.
2876 */
2877// constructor / destructor
2878/////////////////////////////////////////////////////////////////////////////
2879
2880VRDEServerInfo::VRDEServerInfo()
2881 : mParent(NULL)
2882{
2883}
2884
2885VRDEServerInfo::~VRDEServerInfo()
2886{
2887}
2888
2889
2890HRESULT VRDEServerInfo::FinalConstruct()
2891{
2892 return BaseFinalConstruct();
2893}
2894
2895void VRDEServerInfo::FinalRelease()
2896{
2897 uninit();
2898 BaseFinalRelease();
2899}
2900
2901// public methods only for internal purposes
2902/////////////////////////////////////////////////////////////////////////////
2903
2904/**
2905 * Initializes the guest object.
2906 */
2907HRESULT VRDEServerInfo::init(Console *aParent)
2908{
2909 LogFlowThisFunc(("aParent=%p\n", aParent));
2910
2911 ComAssertRet(aParent, E_INVALIDARG);
2912
2913 /* Enclose the state transition NotReady->InInit->Ready */
2914 AutoInitSpan autoInitSpan(this);
2915 AssertReturn(autoInitSpan.isOk(), E_FAIL);
2916
2917 unconst(mParent) = aParent;
2918
2919 /* Confirm a successful initialization */
2920 autoInitSpan.setSucceeded();
2921
2922 return S_OK;
2923}
2924
2925/**
2926 * Uninitializes the instance and sets the ready flag to FALSE.
2927 * Called either from FinalRelease() or by the parent when it gets destroyed.
2928 */
2929void VRDEServerInfo::uninit()
2930{
2931 LogFlowThisFunc(("\n"));
2932
2933 /* Enclose the state transition Ready->InUninit->NotReady */
2934 AutoUninitSpan autoUninitSpan(this);
2935 if (autoUninitSpan.uninitDone())
2936 return;
2937
2938 unconst(mParent) = NULL;
2939}
2940
2941// IVRDEServerInfo properties
2942/////////////////////////////////////////////////////////////////////////////
2943
2944#define IMPL_GETTER_BOOL(_aType, _aName, _aIndex) \
2945 STDMETHODIMP VRDEServerInfo::COMGETTER(_aName)(_aType *a##_aName) \
2946 { \
2947 if (!a##_aName) \
2948 return E_POINTER; \
2949 \
2950 AutoCaller autoCaller(this); \
2951 if (FAILED(autoCaller.rc())) return autoCaller.rc(); \
2952 \
2953 /* todo: Not sure if a AutoReadLock would be sufficient. */ \
2954 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); \
2955 \
2956 uint32_t value; \
2957 uint32_t cbOut = 0; \
2958 \
2959 mParent->consoleVRDPServer()->QueryInfo \
2960 (_aIndex, &value, sizeof(value), &cbOut); \
2961 \
2962 *a##_aName = cbOut? !!value: FALSE; \
2963 \
2964 return S_OK; \
2965 } \
2966 extern void IMPL_GETTER_BOOL_DUMMY(void)
2967
2968#define IMPL_GETTER_SCALAR(_aType, _aName, _aIndex, _aValueMask) \
2969 STDMETHODIMP VRDEServerInfo::COMGETTER(_aName)(_aType *a##_aName) \
2970 { \
2971 if (!a##_aName) \
2972 return E_POINTER; \
2973 \
2974 AutoCaller autoCaller(this); \
2975 if (FAILED(autoCaller.rc())) return autoCaller.rc(); \
2976 \
2977 /* todo: Not sure if a AutoReadLock would be sufficient. */ \
2978 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); \
2979 \
2980 _aType value; \
2981 uint32_t cbOut = 0; \
2982 \
2983 mParent->consoleVRDPServer()->QueryInfo \
2984 (_aIndex, &value, sizeof(value), &cbOut); \
2985 \
2986 if (_aValueMask) value &= (_aValueMask); \
2987 *a##_aName = cbOut? value: 0; \
2988 \
2989 return S_OK; \
2990 } \
2991 extern void IMPL_GETTER_SCALAR_DUMMY(void)
2992
2993#define IMPL_GETTER_BSTR(_aType, _aName, _aIndex) \
2994 STDMETHODIMP VRDEServerInfo::COMGETTER(_aName)(_aType *a##_aName) \
2995 { \
2996 if (!a##_aName) \
2997 return E_POINTER; \
2998 \
2999 AutoCaller autoCaller(this); \
3000 if (FAILED(autoCaller.rc())) return autoCaller.rc(); \
3001 \
3002 /* todo: Not sure if a AutoReadLock would be sufficient. */ \
3003 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); \
3004 \
3005 uint32_t cbOut = 0; \
3006 \
3007 mParent->consoleVRDPServer()->QueryInfo \
3008 (_aIndex, NULL, 0, &cbOut); \
3009 \
3010 if (cbOut == 0) \
3011 { \
3012 Bstr str(""); \
3013 str.cloneTo(a##_aName); \
3014 return S_OK; \
3015 } \
3016 \
3017 char *pchBuffer = (char *)RTMemTmpAlloc(cbOut); \
3018 \
3019 if (!pchBuffer) \
3020 { \
3021 Log(("VRDEServerInfo::" \
3022 #_aName \
3023 ": Failed to allocate memory %d bytes\n", cbOut)); \
3024 return E_OUTOFMEMORY; \
3025 } \
3026 \
3027 mParent->consoleVRDPServer()->QueryInfo \
3028 (_aIndex, pchBuffer, cbOut, &cbOut); \
3029 \
3030 Bstr str(pchBuffer); \
3031 \
3032 str.cloneTo(a##_aName); \
3033 \
3034 RTMemTmpFree(pchBuffer); \
3035 \
3036 return S_OK; \
3037 } \
3038 extern void IMPL_GETTER_BSTR_DUMMY(void)
3039
3040IMPL_GETTER_BOOL (BOOL, Active, VRDE_QI_ACTIVE);
3041IMPL_GETTER_SCALAR (LONG, Port, VRDE_QI_PORT, 0);
3042IMPL_GETTER_SCALAR (ULONG, NumberOfClients, VRDE_QI_NUMBER_OF_CLIENTS, 0);
3043IMPL_GETTER_SCALAR (LONG64, BeginTime, VRDE_QI_BEGIN_TIME, 0);
3044IMPL_GETTER_SCALAR (LONG64, EndTime, VRDE_QI_END_TIME, 0);
3045IMPL_GETTER_SCALAR (LONG64, BytesSent, VRDE_QI_BYTES_SENT, INT64_MAX);
3046IMPL_GETTER_SCALAR (LONG64, BytesSentTotal, VRDE_QI_BYTES_SENT_TOTAL, INT64_MAX);
3047IMPL_GETTER_SCALAR (LONG64, BytesReceived, VRDE_QI_BYTES_RECEIVED, INT64_MAX);
3048IMPL_GETTER_SCALAR (LONG64, BytesReceivedTotal, VRDE_QI_BYTES_RECEIVED_TOTAL, INT64_MAX);
3049IMPL_GETTER_BSTR (BSTR, User, VRDE_QI_USER);
3050IMPL_GETTER_BSTR (BSTR, Domain, VRDE_QI_DOMAIN);
3051IMPL_GETTER_BSTR (BSTR, ClientName, VRDE_QI_CLIENT_NAME);
3052IMPL_GETTER_BSTR (BSTR, ClientIP, VRDE_QI_CLIENT_IP);
3053IMPL_GETTER_SCALAR (ULONG, ClientVersion, VRDE_QI_CLIENT_VERSION, 0);
3054IMPL_GETTER_SCALAR (ULONG, EncryptionStyle, VRDE_QI_ENCRYPTION_STYLE, 0);
3055
3056#undef IMPL_GETTER_BSTR
3057#undef IMPL_GETTER_SCALAR
3058#undef IMPL_GETTER_BOOL
3059/* 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