VirtualBox

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

Last change on this file since 67414 was 65164, checked in by vboxsync, 8 years ago

Main/VRDE: Got rid of Doppelmoppel i_getAudioVRDE() functions and use VBOX_WITH_VRDE_AUDIO where needed.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 133.4 KB
Line 
1/* $Id: ConsoleVRDPServer.cpp 65164 2017-01-05 17:45:12Z vboxsync $ */
2/** @file
3 * VBox Console VRDP helper class.
4 */
5
6/*
7 * Copyright (C) 2006-2017 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#ifdef VBOX_WITH_VRDE_AUDIO
24#include "DrvAudioVRDE.h"
25#endif
26#ifdef VBOX_WITH_EXTPACK
27# include "ExtPackManagerImpl.h"
28#endif
29#include "VMMDev.h"
30#ifdef VBOX_WITH_USB_CARDREADER
31# include "UsbCardReader.h"
32#endif
33#include "UsbWebcamInterface.h"
34
35#include "Global.h"
36#include "AutoCaller.h"
37#include "Logging.h"
38
39#include <iprt/asm.h>
40#include <iprt/alloca.h>
41#include <iprt/ldr.h>
42#include <iprt/param.h>
43#include <iprt/path.h>
44#include <iprt/cpp/utils.h>
45
46#include <VBox/err.h>
47#include <VBox/RemoteDesktop/VRDEOrders.h>
48#include <VBox/com/listeners.h>
49#include <VBox/HostServices/VBoxCrOpenGLSvc.h>
50
51class VRDPConsoleListener
52{
53public:
54 VRDPConsoleListener()
55 {
56 }
57
58 virtual ~VRDPConsoleListener()
59 {
60 }
61
62 HRESULT init(ConsoleVRDPServer *server)
63 {
64 m_server = server;
65 return S_OK;
66 }
67
68 void uninit()
69 {
70 }
71
72 STDMETHOD(HandleEvent)(VBoxEventType_T aType, IEvent * aEvent)
73 {
74 switch (aType)
75 {
76 case VBoxEventType_OnMousePointerShapeChanged:
77 {
78 ComPtr<IMousePointerShapeChangedEvent> mpscev = aEvent;
79 Assert(mpscev);
80 BOOL visible, alpha;
81 ULONG xHot, yHot, width, height;
82 com::SafeArray <BYTE> shape;
83
84 mpscev->COMGETTER(Visible)(&visible);
85 mpscev->COMGETTER(Alpha)(&alpha);
86 mpscev->COMGETTER(Xhot)(&xHot);
87 mpscev->COMGETTER(Yhot)(&yHot);
88 mpscev->COMGETTER(Width)(&width);
89 mpscev->COMGETTER(Height)(&height);
90 mpscev->COMGETTER(Shape)(ComSafeArrayAsOutParam(shape));
91
92 m_server->onMousePointerShapeChange(visible, alpha, xHot, yHot, width, height, ComSafeArrayAsInParam(shape));
93 break;
94 }
95 case VBoxEventType_OnMouseCapabilityChanged:
96 {
97 ComPtr<IMouseCapabilityChangedEvent> mccev = aEvent;
98 Assert(mccev);
99 if (m_server)
100 {
101 BOOL fAbsoluteMouse;
102 mccev->COMGETTER(SupportsAbsolute)(&fAbsoluteMouse);
103 m_server->NotifyAbsoluteMouse(!!fAbsoluteMouse);
104 }
105 break;
106 }
107 case VBoxEventType_OnKeyboardLedsChanged:
108 {
109 ComPtr<IKeyboardLedsChangedEvent> klcev = aEvent;
110 Assert(klcev);
111
112 if (m_server)
113 {
114 BOOL fNumLock, fCapsLock, fScrollLock;
115 klcev->COMGETTER(NumLock)(&fNumLock);
116 klcev->COMGETTER(CapsLock)(&fCapsLock);
117 klcev->COMGETTER(ScrollLock)(&fScrollLock);
118 m_server->NotifyKeyboardLedsChange(fNumLock, fCapsLock, fScrollLock);
119 }
120 break;
121 }
122
123 default:
124 AssertFailed();
125 }
126
127 return S_OK;
128 }
129
130private:
131 ConsoleVRDPServer *m_server;
132};
133
134typedef ListenerImpl<VRDPConsoleListener, ConsoleVRDPServer*> VRDPConsoleListenerImpl;
135
136VBOX_LISTENER_DECLARE(VRDPConsoleListenerImpl)
137
138#ifdef DEBUG_sunlover
139#define LOGDUMPPTR Log
140void dumpPointer(const uint8_t *pu8Shape, uint32_t width, uint32_t height, bool fXorMaskRGB32)
141{
142 unsigned i;
143
144 const uint8_t *pu8And = pu8Shape;
145
146 for (i = 0; i < height; i++)
147 {
148 unsigned j;
149 LOGDUMPPTR(("%p: ", pu8And));
150 for (j = 0; j < (width + 7) / 8; j++)
151 {
152 unsigned k;
153 for (k = 0; k < 8; k++)
154 {
155 LOGDUMPPTR(("%d", ((*pu8And) & (1 << (7 - k)))? 1: 0));
156 }
157
158 pu8And++;
159 }
160 LOGDUMPPTR(("\n"));
161 }
162
163 if (fXorMaskRGB32)
164 {
165 uint32_t *pu32Xor = (uint32_t*)(pu8Shape + ((((width + 7) / 8) * height + 3) & ~3));
166
167 for (i = 0; i < height; i++)
168 {
169 unsigned j;
170 LOGDUMPPTR(("%p: ", pu32Xor));
171 for (j = 0; j < width; j++)
172 {
173 LOGDUMPPTR(("%08X", *pu32Xor++));
174 }
175 LOGDUMPPTR(("\n"));
176 }
177 }
178 else
179 {
180 /* RDP 24 bit RGB mask. */
181 uint8_t *pu8Xor = (uint8_t*)(pu8Shape + ((((width + 7) / 8) * height + 3) & ~3));
182 for (i = 0; i < height; i++)
183 {
184 unsigned j;
185 LOGDUMPPTR(("%p: ", pu8Xor));
186 for (j = 0; j < width; j++)
187 {
188 LOGDUMPPTR(("%02X%02X%02X", pu8Xor[2], pu8Xor[1], pu8Xor[0]));
189 pu8Xor += 3;
190 }
191 LOGDUMPPTR(("\n"));
192 }
193 }
194}
195#else
196#define dumpPointer(a, b, c, d) do {} while (0)
197#endif /* DEBUG_sunlover */
198
199static void findTopLeftBorder(const uint8_t *pu8AndMask, const uint8_t *pu8XorMask, uint32_t width,
200 uint32_t height, uint32_t *pxSkip, uint32_t *pySkip)
201{
202 /*
203 * Find the top border of the AND mask. First assign to special value.
204 */
205 uint32_t ySkipAnd = UINT32_MAX;
206
207 const uint8_t *pu8And = pu8AndMask;
208 const uint32_t cbAndRow = (width + 7) / 8;
209 const uint8_t maskLastByte = (uint8_t)( 0xFF << (cbAndRow * 8 - width) );
210
211 Assert(cbAndRow > 0);
212
213 unsigned y;
214 unsigned x;
215
216 for (y = 0; y < height && ySkipAnd == ~(uint32_t)0; y++, pu8And += cbAndRow)
217 {
218 /* For each complete byte in the row. */
219 for (x = 0; x < cbAndRow - 1; x++)
220 {
221 if (pu8And[x] != 0xFF)
222 {
223 ySkipAnd = y;
224 break;
225 }
226 }
227
228 if (ySkipAnd == ~(uint32_t)0)
229 {
230 /* Last byte. */
231 if ((pu8And[cbAndRow - 1] & maskLastByte) != maskLastByte)
232 {
233 ySkipAnd = y;
234 }
235 }
236 }
237
238 if (ySkipAnd == ~(uint32_t)0)
239 {
240 ySkipAnd = 0;
241 }
242
243 /*
244 * Find the left border of the AND mask.
245 */
246 uint32_t xSkipAnd = UINT32_MAX;
247
248 /* For all bit columns. */
249 for (x = 0; x < width && xSkipAnd == ~(uint32_t)0; x++)
250 {
251 pu8And = pu8AndMask + x/8; /* Currently checking byte. */
252 uint8_t mask = 1 << (7 - x%8); /* Currently checking bit in the byte. */
253
254 for (y = ySkipAnd; y < height; y++, pu8And += cbAndRow)
255 {
256 if ((*pu8And & mask) == 0)
257 {
258 xSkipAnd = x;
259 break;
260 }
261 }
262 }
263
264 if (xSkipAnd == ~(uint32_t)0)
265 {
266 xSkipAnd = 0;
267 }
268
269 /*
270 * Find the XOR mask top border.
271 */
272 uint32_t ySkipXor = UINT32_MAX;
273
274 uint32_t *pu32XorStart = (uint32_t *)pu8XorMask;
275
276 uint32_t *pu32Xor = pu32XorStart;
277
278 for (y = 0; y < height && ySkipXor == ~(uint32_t)0; y++, pu32Xor += width)
279 {
280 for (x = 0; x < width; x++)
281 {
282 if (pu32Xor[x] != 0)
283 {
284 ySkipXor = y;
285 break;
286 }
287 }
288 }
289
290 if (ySkipXor == ~(uint32_t)0)
291 {
292 ySkipXor = 0;
293 }
294
295 /*
296 * Find the left border of the XOR mask.
297 */
298 uint32_t xSkipXor = ~(uint32_t)0;
299
300 /* For all columns. */
301 for (x = 0; x < width && xSkipXor == ~(uint32_t)0; x++)
302 {
303 pu32Xor = pu32XorStart + x; /* Currently checking dword. */
304
305 for (y = ySkipXor; y < height; y++, pu32Xor += width)
306 {
307 if (*pu32Xor != 0)
308 {
309 xSkipXor = x;
310 break;
311 }
312 }
313 }
314
315 if (xSkipXor == ~(uint32_t)0)
316 {
317 xSkipXor = 0;
318 }
319
320 *pxSkip = RT_MIN(xSkipAnd, xSkipXor);
321 *pySkip = RT_MIN(ySkipAnd, ySkipXor);
322}
323
324/* Generate an AND mask for alpha pointers here, because
325 * guest driver does not do that correctly for Vista pointers.
326 * Similar fix, changing the alpha threshold, could be applied
327 * for the guest driver, but then additions reinstall would be
328 * necessary, which we try to avoid.
329 */
330static void mousePointerGenerateANDMask(uint8_t *pu8DstAndMask, int cbDstAndMask, const uint8_t *pu8SrcAlpha, int w, int h)
331{
332 memset(pu8DstAndMask, 0xFF, cbDstAndMask);
333
334 int y;
335 for (y = 0; y < h; y++)
336 {
337 uint8_t bitmask = 0x80;
338
339 int x;
340 for (x = 0; x < w; x++, bitmask >>= 1)
341 {
342 if (bitmask == 0)
343 {
344 bitmask = 0x80;
345 }
346
347 /* Whether alpha channel value is not transparent enough for the pixel to be seen. */
348 if (pu8SrcAlpha[x * 4 + 3] > 0x7f)
349 {
350 pu8DstAndMask[x / 8] &= ~bitmask;
351 }
352 }
353
354 /* Point to next source and dest scans. */
355 pu8SrcAlpha += w * 4;
356 pu8DstAndMask += (w + 7) / 8;
357 }
358}
359
360void ConsoleVRDPServer::onMousePointerShapeChange(BOOL visible,
361 BOOL alpha,
362 ULONG xHot,
363 ULONG yHot,
364 ULONG width,
365 ULONG height,
366 ComSafeArrayIn(BYTE,inShape))
367{
368 Log9(("VRDPConsoleListener::OnMousePointerShapeChange: %d, %d, %lux%lu, @%lu,%lu\n",
369 visible, alpha, width, height, xHot, yHot));
370
371 com::SafeArray <BYTE> aShape(ComSafeArrayInArg(inShape));
372 if (aShape.size() == 0)
373 {
374 if (!visible)
375 {
376 MousePointerHide();
377 }
378 }
379 else if (width != 0 && height != 0)
380 {
381 uint8_t* shape = aShape.raw();
382
383 dumpPointer(shape, width, height, true);
384
385 /* Try the new interface. */
386 if (MousePointer(alpha, xHot, yHot, width, height, shape) == VINF_SUCCESS)
387 {
388 return;
389 }
390
391 /* Continue with the old interface. */
392
393 /* Pointer consists of 1 bpp AND and 24 BPP XOR masks.
394 * 'shape' AND mask followed by XOR mask.
395 * XOR mask contains 32 bit (lsb)BGR0(msb) values.
396 *
397 * We convert this to RDP color format which consist of
398 * one bpp AND mask and 24 BPP (BGR) color XOR image.
399 *
400 * RDP clients expect 8 aligned width and height of
401 * pointer (preferably 32x32).
402 *
403 * They even contain bugs which do not appear for
404 * 32x32 pointers but would appear for a 41x32 one.
405 *
406 * So set pointer size to 32x32. This can be done safely
407 * because most pointers are 32x32.
408 */
409
410 int cbDstAndMask = (((width + 7) / 8) * height + 3) & ~3;
411
412 uint8_t *pu8AndMask = shape;
413 uint8_t *pu8XorMask = shape + cbDstAndMask;
414
415 if (alpha)
416 {
417 pu8AndMask = (uint8_t*)alloca(cbDstAndMask);
418
419 mousePointerGenerateANDMask(pu8AndMask, cbDstAndMask, pu8XorMask, width, height);
420 }
421
422 /* Windows guest alpha pointers are wider than 32 pixels.
423 * Try to find out the top-left border of the pointer and
424 * then copy only meaningful bits. All complete top rows
425 * and all complete left columns where (AND == 1 && XOR == 0)
426 * are skipped. Hot spot is adjusted.
427 */
428 uint32_t ySkip = 0; /* How many rows to skip at the top. */
429 uint32_t xSkip = 0; /* How many columns to skip at the left. */
430
431 findTopLeftBorder(pu8AndMask, pu8XorMask, width, height, &xSkip, &ySkip);
432
433 /* Must not skip the hot spot. */
434 xSkip = RT_MIN(xSkip, xHot);
435 ySkip = RT_MIN(ySkip, yHot);
436
437 /*
438 * Compute size and allocate memory for the pointer.
439 */
440 const uint32_t dstwidth = 32;
441 const uint32_t dstheight = 32;
442
443 VRDECOLORPOINTER *pointer = NULL;
444
445 uint32_t dstmaskwidth = (dstwidth + 7) / 8;
446
447 uint32_t rdpmaskwidth = dstmaskwidth;
448 uint32_t rdpmasklen = dstheight * rdpmaskwidth;
449
450 uint32_t rdpdatawidth = dstwidth * 3;
451 uint32_t rdpdatalen = dstheight * rdpdatawidth;
452
453 pointer = (VRDECOLORPOINTER *)RTMemTmpAlloc(sizeof(VRDECOLORPOINTER) + rdpmasklen + rdpdatalen);
454
455 if (pointer)
456 {
457 uint8_t *maskarray = (uint8_t*)pointer + sizeof(VRDECOLORPOINTER);
458 uint8_t *dataarray = maskarray + rdpmasklen;
459
460 memset(maskarray, 0xFF, rdpmasklen);
461 memset(dataarray, 0x00, rdpdatalen);
462
463 uint32_t srcmaskwidth = (width + 7) / 8;
464 uint32_t srcdatawidth = width * 4;
465
466 /* Copy AND mask. */
467 uint8_t *src = pu8AndMask + ySkip * srcmaskwidth;
468 uint8_t *dst = maskarray + (dstheight - 1) * rdpmaskwidth;
469
470 uint32_t minheight = RT_MIN(height - ySkip, dstheight);
471 uint32_t minwidth = RT_MIN(width - xSkip, dstwidth);
472
473 unsigned x, y;
474
475 for (y = 0; y < minheight; y++)
476 {
477 for (x = 0; x < minwidth; x++)
478 {
479 uint32_t byteIndex = (x + xSkip) / 8;
480 uint32_t bitIndex = (x + xSkip) % 8;
481
482 bool bit = (src[byteIndex] & (1 << (7 - bitIndex))) != 0;
483
484 if (!bit)
485 {
486 byteIndex = x / 8;
487 bitIndex = x % 8;
488
489 dst[byteIndex] &= ~(1 << (7 - bitIndex));
490 }
491 }
492
493 src += srcmaskwidth;
494 dst -= rdpmaskwidth;
495 }
496
497 /* Point src to XOR mask */
498 src = pu8XorMask + ySkip * srcdatawidth;
499 dst = dataarray + (dstheight - 1) * rdpdatawidth;
500
501 for (y = 0; y < minheight ; y++)
502 {
503 for (x = 0; x < minwidth; x++)
504 {
505 memcpy(dst + x * 3, &src[4 * (x + xSkip)], 3);
506 }
507
508 src += srcdatawidth;
509 dst -= rdpdatawidth;
510 }
511
512 pointer->u16HotX = (uint16_t)(xHot - xSkip);
513 pointer->u16HotY = (uint16_t)(yHot - ySkip);
514
515 pointer->u16Width = (uint16_t)dstwidth;
516 pointer->u16Height = (uint16_t)dstheight;
517
518 pointer->u16MaskLen = (uint16_t)rdpmasklen;
519 pointer->u16DataLen = (uint16_t)rdpdatalen;
520
521 dumpPointer((uint8_t*)pointer + sizeof(*pointer), dstwidth, dstheight, false);
522
523 MousePointerUpdate(pointer);
524
525 RTMemTmpFree(pointer);
526 }
527 }
528}
529
530
531// ConsoleVRDPServer
532////////////////////////////////////////////////////////////////////////////////
533
534RTLDRMOD ConsoleVRDPServer::mVRDPLibrary = NIL_RTLDRMOD;
535
536PFNVRDECREATESERVER ConsoleVRDPServer::mpfnVRDECreateServer = NULL;
537
538VRDEENTRYPOINTS_4 ConsoleVRDPServer::mEntryPoints; /* A copy of the server entry points. */
539VRDEENTRYPOINTS_4 *ConsoleVRDPServer::mpEntryPoints = NULL;
540
541VRDECALLBACKS_4 ConsoleVRDPServer::mCallbacks =
542{
543 { VRDE_INTERFACE_VERSION_4, sizeof(VRDECALLBACKS_4) },
544 ConsoleVRDPServer::VRDPCallbackQueryProperty,
545 ConsoleVRDPServer::VRDPCallbackClientLogon,
546 ConsoleVRDPServer::VRDPCallbackClientConnect,
547 ConsoleVRDPServer::VRDPCallbackClientDisconnect,
548 ConsoleVRDPServer::VRDPCallbackIntercept,
549 ConsoleVRDPServer::VRDPCallbackUSB,
550 ConsoleVRDPServer::VRDPCallbackClipboard,
551 ConsoleVRDPServer::VRDPCallbackFramebufferQuery,
552 ConsoleVRDPServer::VRDPCallbackFramebufferLock,
553 ConsoleVRDPServer::VRDPCallbackFramebufferUnlock,
554 ConsoleVRDPServer::VRDPCallbackInput,
555 ConsoleVRDPServer::VRDPCallbackVideoModeHint,
556 ConsoleVRDPServer::VRDECallbackAudioIn
557};
558
559DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackQueryProperty(void *pvCallback, uint32_t index, void *pvBuffer,
560 uint32_t cbBuffer, uint32_t *pcbOut)
561{
562 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
563
564 int rc = VERR_NOT_SUPPORTED;
565
566 switch (index)
567 {
568 case VRDE_QP_NETWORK_PORT:
569 {
570 /* This is obsolete, the VRDE server uses VRDE_QP_NETWORK_PORT_RANGE instead. */
571 ULONG port = 0;
572
573 if (cbBuffer >= sizeof(uint32_t))
574 {
575 *(uint32_t *)pvBuffer = (uint32_t)port;
576 rc = VINF_SUCCESS;
577 }
578 else
579 {
580 rc = VINF_BUFFER_OVERFLOW;
581 }
582
583 *pcbOut = sizeof(uint32_t);
584 } break;
585
586 case VRDE_QP_NETWORK_ADDRESS:
587 {
588 com::Bstr bstr;
589 server->mConsole->i_getVRDEServer()->GetVRDEProperty(Bstr("TCP/Address").raw(), bstr.asOutParam());
590
591 /* The server expects UTF8. */
592 com::Utf8Str address = bstr;
593
594 size_t cbAddress = address.length() + 1;
595
596 if (cbAddress >= 0x10000)
597 {
598 /* More than 64K seems to be an invalid address. */
599 rc = VERR_TOO_MUCH_DATA;
600 break;
601 }
602
603 if ((size_t)cbBuffer >= cbAddress)
604 {
605 memcpy(pvBuffer, address.c_str(), cbAddress);
606 rc = VINF_SUCCESS;
607 }
608 else
609 {
610 rc = VINF_BUFFER_OVERFLOW;
611 }
612
613 *pcbOut = (uint32_t)cbAddress;
614 } break;
615
616 case VRDE_QP_NUMBER_MONITORS:
617 {
618 ULONG cMonitors = 1;
619
620 server->mConsole->i_machine()->COMGETTER(MonitorCount)(&cMonitors);
621
622 if (cbBuffer >= sizeof(uint32_t))
623 {
624 *(uint32_t *)pvBuffer = (uint32_t)cMonitors;
625 rc = VINF_SUCCESS;
626 }
627 else
628 {
629 rc = VINF_BUFFER_OVERFLOW;
630 }
631
632 *pcbOut = sizeof(uint32_t);
633 } break;
634
635 case VRDE_QP_NETWORK_PORT_RANGE:
636 {
637 com::Bstr bstr;
638 HRESULT hrc = server->mConsole->i_getVRDEServer()->GetVRDEProperty(Bstr("TCP/Ports").raw(), bstr.asOutParam());
639
640 if (hrc != S_OK)
641 {
642 bstr = "";
643 }
644
645 if (bstr == "0")
646 {
647 bstr = "3389";
648 }
649
650 /* The server expects UTF8. */
651 com::Utf8Str portRange = bstr;
652
653 size_t cbPortRange = portRange.length() + 1;
654
655 if (cbPortRange >= _64K)
656 {
657 /* More than 64K seems to be an invalid port range string. */
658 rc = VERR_TOO_MUCH_DATA;
659 break;
660 }
661
662 if ((size_t)cbBuffer >= cbPortRange)
663 {
664 memcpy(pvBuffer, portRange.c_str(), cbPortRange);
665 rc = VINF_SUCCESS;
666 }
667 else
668 {
669 rc = VINF_BUFFER_OVERFLOW;
670 }
671
672 *pcbOut = (uint32_t)cbPortRange;
673 } break;
674
675 case VRDE_QP_VIDEO_CHANNEL:
676 {
677 com::Bstr bstr;
678 HRESULT hrc = server->mConsole->i_getVRDEServer()->GetVRDEProperty(Bstr("VideoChannel/Enabled").raw(),
679 bstr.asOutParam());
680
681 if (hrc != S_OK)
682 {
683 bstr = "";
684 }
685
686 com::Utf8Str value = bstr;
687
688 BOOL fVideoEnabled = RTStrICmp(value.c_str(), "true") == 0
689 || RTStrICmp(value.c_str(), "1") == 0;
690
691 if (cbBuffer >= sizeof(uint32_t))
692 {
693 *(uint32_t *)pvBuffer = (uint32_t)fVideoEnabled;
694 rc = VINF_SUCCESS;
695 }
696 else
697 {
698 rc = VINF_BUFFER_OVERFLOW;
699 }
700
701 *pcbOut = sizeof(uint32_t);
702 } break;
703
704 case VRDE_QP_VIDEO_CHANNEL_QUALITY:
705 {
706 com::Bstr bstr;
707 HRESULT hrc = server->mConsole->i_getVRDEServer()->GetVRDEProperty(Bstr("VideoChannel/Quality").raw(),
708 bstr.asOutParam());
709
710 if (hrc != S_OK)
711 {
712 bstr = "";
713 }
714
715 com::Utf8Str value = bstr;
716
717 ULONG ulQuality = RTStrToUInt32(value.c_str()); /* This returns 0 on invalid string which is ok. */
718
719 if (cbBuffer >= sizeof(uint32_t))
720 {
721 *(uint32_t *)pvBuffer = (uint32_t)ulQuality;
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 VRDE_QP_VIDEO_CHANNEL_SUNFLSH:
733 {
734 ULONG ulSunFlsh = 1;
735
736 com::Bstr bstr;
737 HRESULT hrc = server->mConsole->i_machine()->GetExtraData(Bstr("VRDP/SunFlsh").raw(),
738 bstr.asOutParam());
739 if (hrc == S_OK && !bstr.isEmpty())
740 {
741 com::Utf8Str sunFlsh = bstr;
742 if (!sunFlsh.isEmpty())
743 {
744 ulSunFlsh = sunFlsh.toUInt32();
745 }
746 }
747
748 if (cbBuffer >= sizeof(uint32_t))
749 {
750 *(uint32_t *)pvBuffer = (uint32_t)ulSunFlsh;
751 rc = VINF_SUCCESS;
752 }
753 else
754 {
755 rc = VINF_BUFFER_OVERFLOW;
756 }
757
758 *pcbOut = sizeof(uint32_t);
759 } break;
760
761 case VRDE_QP_FEATURE:
762 {
763 if (cbBuffer < sizeof(VRDEFEATURE))
764 {
765 rc = VERR_INVALID_PARAMETER;
766 break;
767 }
768
769 size_t cbInfo = cbBuffer - RT_OFFSETOF(VRDEFEATURE, achInfo);
770
771 VRDEFEATURE *pFeature = (VRDEFEATURE *)pvBuffer;
772
773 size_t cchInfo = 0;
774 rc = RTStrNLenEx(pFeature->achInfo, cbInfo, &cchInfo);
775
776 if (RT_FAILURE(rc))
777 {
778 rc = VERR_INVALID_PARAMETER;
779 break;
780 }
781
782 Log(("VRDE_QP_FEATURE [%s]\n", pFeature->achInfo));
783
784 com::Bstr bstrValue;
785
786 if ( RTStrICmp(pFeature->achInfo, "Client/DisableDisplay") == 0
787 || RTStrICmp(pFeature->achInfo, "Client/DisableInput") == 0
788 || RTStrICmp(pFeature->achInfo, "Client/DisableAudio") == 0
789 || RTStrICmp(pFeature->achInfo, "Client/DisableUSB") == 0
790 || RTStrICmp(pFeature->achInfo, "Client/DisableClipboard") == 0
791 )
792 {
793 /** @todo these features should be per client. */
794 NOREF(pFeature->u32ClientId);
795
796 /* These features are mapped to "VRDE/Feature/NAME" extra data. */
797 com::Utf8Str extraData("VRDE/Feature/");
798 extraData += pFeature->achInfo;
799
800 HRESULT hrc = server->mConsole->i_machine()->GetExtraData(com::Bstr(extraData).raw(),
801 bstrValue.asOutParam());
802 if (FAILED(hrc) || bstrValue.isEmpty())
803 {
804 /* Also try the old "VRDP/Feature/NAME" */
805 extraData = "VRDP/Feature/";
806 extraData += pFeature->achInfo;
807
808 hrc = server->mConsole->i_machine()->GetExtraData(com::Bstr(extraData).raw(),
809 bstrValue.asOutParam());
810 if (FAILED(hrc))
811 {
812 rc = VERR_NOT_SUPPORTED;
813 }
814 }
815 }
816 else if (RTStrNCmp(pFeature->achInfo, "Property/", 9) == 0)
817 {
818 /* Generic properties. */
819 const char *pszPropertyName = &pFeature->achInfo[9];
820 HRESULT hrc = server->mConsole->i_getVRDEServer()->GetVRDEProperty(Bstr(pszPropertyName).raw(),
821 bstrValue.asOutParam());
822 if (FAILED(hrc))
823 {
824 rc = VERR_NOT_SUPPORTED;
825 }
826 }
827 else
828 {
829 rc = VERR_NOT_SUPPORTED;
830 }
831
832 /* Copy the value string to the callers buffer. */
833 if (rc == VINF_SUCCESS)
834 {
835 com::Utf8Str value = bstrValue;
836
837 size_t cb = value.length() + 1;
838
839 if ((size_t)cbInfo >= cb)
840 {
841 memcpy(pFeature->achInfo, value.c_str(), cb);
842 }
843 else
844 {
845 rc = VINF_BUFFER_OVERFLOW;
846 }
847
848 *pcbOut = (uint32_t)cb;
849 }
850 } break;
851
852 case VRDE_SP_NETWORK_BIND_PORT:
853 {
854 if (cbBuffer != sizeof(uint32_t))
855 {
856 rc = VERR_INVALID_PARAMETER;
857 break;
858 }
859
860 ULONG port = *(uint32_t *)pvBuffer;
861
862 server->mVRDPBindPort = port;
863
864 rc = VINF_SUCCESS;
865
866 if (pcbOut)
867 {
868 *pcbOut = sizeof(uint32_t);
869 }
870
871 server->mConsole->i_onVRDEServerInfoChange();
872 } break;
873
874 case VRDE_SP_CLIENT_STATUS:
875 {
876 if (cbBuffer < sizeof(VRDECLIENTSTATUS))
877 {
878 rc = VERR_INVALID_PARAMETER;
879 break;
880 }
881
882 size_t cbStatus = cbBuffer - RT_UOFFSETOF(VRDECLIENTSTATUS, achStatus);
883
884 VRDECLIENTSTATUS *pStatus = (VRDECLIENTSTATUS *)pvBuffer;
885
886 if (cbBuffer < RT_UOFFSETOF(VRDECLIENTSTATUS, achStatus) + pStatus->cbStatus)
887 {
888 rc = VERR_INVALID_PARAMETER;
889 break;
890 }
891
892 size_t cchStatus = 0;
893 rc = RTStrNLenEx(pStatus->achStatus, cbStatus, &cchStatus);
894
895 if (RT_FAILURE(rc))
896 {
897 rc = VERR_INVALID_PARAMETER;
898 break;
899 }
900
901 Log(("VRDE_SP_CLIENT_STATUS [%s]\n", pStatus->achStatus));
902
903 server->mConsole->i_VRDPClientStatusChange(pStatus->u32ClientId, pStatus->achStatus);
904
905 rc = VINF_SUCCESS;
906
907 if (pcbOut)
908 {
909 *pcbOut = cbBuffer;
910 }
911
912 server->mConsole->i_onVRDEServerInfoChange();
913 } break;
914
915 default:
916 break;
917 }
918
919 return rc;
920}
921
922DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackClientLogon(void *pvCallback, uint32_t u32ClientId, const char *pszUser,
923 const char *pszPassword, const char *pszDomain)
924{
925 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
926
927 return server->mConsole->i_VRDPClientLogon(u32ClientId, pszUser, pszPassword, pszDomain);
928}
929
930DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackClientConnect(void *pvCallback, uint32_t u32ClientId)
931{
932 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
933
934 server->mConsole->i_VRDPClientConnect(u32ClientId);
935
936 /* Should the server report usage of an interface for each client?
937 * Similar to Intercept.
938 */
939 int c = ASMAtomicIncS32(&server->mcClients);
940 if (c == 1)
941 {
942 /* Features which should be enabled only if there is a client. */
943 server->remote3DRedirect(true);
944 }
945}
946
947DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackClientDisconnect(void *pvCallback, uint32_t u32ClientId,
948 uint32_t fu32Intercepted)
949{
950 ConsoleVRDPServer *pServer = static_cast<ConsoleVRDPServer*>(pvCallback);
951 AssertPtrReturnVoid(pServer);
952
953 pServer->mConsole->i_VRDPClientDisconnect(u32ClientId, fu32Intercepted);
954
955 if (ASMAtomicReadU32(&pServer->mu32AudioInputClientId) == u32ClientId)
956 {
957 LogFunc(("Disconnected client %u\n", u32ClientId));
958 ASMAtomicWriteU32(&pServer->mu32AudioInputClientId, 0);
959
960#ifdef VBOX_WITH_VRDE_AUDIO
961 AudioVRDE *pVRDE = pServer->mConsole->i_getAudioVRDE();
962 if (pVRDE)
963 pVRDE->onVRDEInputIntercept(false /* fIntercept */);
964#endif
965 }
966
967 int32_t cClients = ASMAtomicDecS32(&pServer->mcClients);
968 if (cClients == 0)
969 {
970 /* Features which should be enabled only if there is a client. */
971 pServer->remote3DRedirect(false);
972 }
973}
974
975DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackIntercept(void *pvCallback, uint32_t u32ClientId, uint32_t fu32Intercept,
976 void **ppvIntercept)
977{
978 ConsoleVRDPServer *pServer = static_cast<ConsoleVRDPServer*>(pvCallback);
979 AssertPtrReturn(pServer, VERR_INVALID_POINTER);
980
981 LogFlowFunc(("%x\n", fu32Intercept));
982
983 int rc = VERR_NOT_SUPPORTED;
984
985 switch (fu32Intercept)
986 {
987 case VRDE_CLIENT_INTERCEPT_AUDIO:
988 {
989 pServer->mConsole->i_VRDPInterceptAudio(u32ClientId);
990 if (ppvIntercept)
991 {
992 *ppvIntercept = pServer;
993 }
994 rc = VINF_SUCCESS;
995 } break;
996
997 case VRDE_CLIENT_INTERCEPT_USB:
998 {
999 pServer->mConsole->i_VRDPInterceptUSB(u32ClientId, ppvIntercept);
1000 rc = VINF_SUCCESS;
1001 } break;
1002
1003 case VRDE_CLIENT_INTERCEPT_CLIPBOARD:
1004 {
1005 pServer->mConsole->i_VRDPInterceptClipboard(u32ClientId);
1006 if (ppvIntercept)
1007 {
1008 *ppvIntercept = pServer;
1009 }
1010 rc = VINF_SUCCESS;
1011 } break;
1012
1013 case VRDE_CLIENT_INTERCEPT_AUDIO_INPUT:
1014 {
1015 /*
1016 * This request is processed internally by the ConsoleVRDPServer.
1017 * Only one client is allowed to intercept audio input.
1018 */
1019 if (ASMAtomicCmpXchgU32(&pServer->mu32AudioInputClientId, u32ClientId, 0) == true)
1020 {
1021 LogFunc(("Intercepting audio input by client %RU32\n", u32ClientId));
1022
1023#ifdef VBOX_WITH_VRDE_AUDIO
1024 AudioVRDE *pVRDE = pServer->mConsole->i_getAudioVRDE();
1025 if (pVRDE)
1026 pVRDE->onVRDEInputIntercept(true /* fIntercept */);
1027#endif
1028 }
1029 else
1030 {
1031 Log(("AUDIOIN: ignored client %RU32, active client %RU32\n", u32ClientId, pServer->mu32AudioInputClientId));
1032 rc = VERR_NOT_SUPPORTED;
1033 }
1034 } break;
1035
1036 default:
1037 break;
1038 }
1039
1040 return rc;
1041}
1042
1043DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackUSB(void *pvCallback, void *pvIntercept, uint32_t u32ClientId,
1044 uint8_t u8Code, const void *pvRet, uint32_t cbRet)
1045{
1046 RT_NOREF(pvCallback);
1047#ifdef VBOX_WITH_USB
1048 return USBClientResponseCallback(pvIntercept, u32ClientId, u8Code, pvRet, cbRet);
1049#else
1050 return VERR_NOT_SUPPORTED;
1051#endif
1052}
1053
1054DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackClipboard(void *pvCallback, void *pvIntercept, uint32_t u32ClientId,
1055 uint32_t u32Function, uint32_t u32Format,
1056 const void *pvData, uint32_t cbData)
1057{
1058 RT_NOREF(pvCallback);
1059 return ClipboardCallback(pvIntercept, u32ClientId, u32Function, u32Format, pvData, cbData);
1060}
1061
1062DECLCALLBACK(bool) ConsoleVRDPServer::VRDPCallbackFramebufferQuery(void *pvCallback, unsigned uScreenId,
1063 VRDEFRAMEBUFFERINFO *pInfo)
1064{
1065 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1066
1067 bool fAvailable = false;
1068
1069 /* Obtain the new screen bitmap. */
1070 HRESULT hr = server->mConsole->i_getDisplay()->QuerySourceBitmap(uScreenId, server->maSourceBitmaps[uScreenId].asOutParam());
1071 if (SUCCEEDED(hr))
1072 {
1073 LONG xOrigin = 0;
1074 LONG yOrigin = 0;
1075 BYTE *pAddress = NULL;
1076 ULONG ulWidth = 0;
1077 ULONG ulHeight = 0;
1078 ULONG ulBitsPerPixel = 0;
1079 ULONG ulBytesPerLine = 0;
1080 BitmapFormat_T bitmapFormat = BitmapFormat_Opaque;
1081
1082 hr = server->maSourceBitmaps[uScreenId]->QueryBitmapInfo(&pAddress,
1083 &ulWidth,
1084 &ulHeight,
1085 &ulBitsPerPixel,
1086 &ulBytesPerLine,
1087 &bitmapFormat);
1088
1089 if (SUCCEEDED(hr))
1090 {
1091 ULONG dummy;
1092 GuestMonitorStatus_T monitorStatus;
1093 hr = server->mConsole->i_getDisplay()->GetScreenResolution(uScreenId, &dummy, &dummy, &dummy,
1094 &xOrigin, &yOrigin, &monitorStatus);
1095
1096 if (SUCCEEDED(hr))
1097 {
1098 /* Now fill the information as requested by the caller. */
1099 pInfo->pu8Bits = pAddress;
1100 pInfo->xOrigin = xOrigin;
1101 pInfo->yOrigin = yOrigin;
1102 pInfo->cWidth = ulWidth;
1103 pInfo->cHeight = ulHeight;
1104 pInfo->cBitsPerPixel = ulBitsPerPixel;
1105 pInfo->cbLine = ulBytesPerLine;
1106
1107 fAvailable = true;
1108 }
1109 }
1110 }
1111
1112 return fAvailable;
1113}
1114
1115DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackFramebufferLock(void *pvCallback, unsigned uScreenId)
1116{
1117 NOREF(pvCallback);
1118 NOREF(uScreenId);
1119 /* Do nothing */
1120}
1121
1122DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackFramebufferUnlock(void *pvCallback, unsigned uScreenId)
1123{
1124 NOREF(pvCallback);
1125 NOREF(uScreenId);
1126 /* Do nothing */
1127}
1128
1129static void fixKbdLockStatus(VRDPInputSynch *pInputSynch, IKeyboard *pKeyboard)
1130{
1131 if ( pInputSynch->cGuestNumLockAdaptions
1132 && (pInputSynch->fGuestNumLock != pInputSynch->fClientNumLock))
1133 {
1134 pInputSynch->cGuestNumLockAdaptions--;
1135 pKeyboard->PutScancode(0x45);
1136 pKeyboard->PutScancode(0x45 | 0x80);
1137 }
1138 if ( pInputSynch->cGuestCapsLockAdaptions
1139 && (pInputSynch->fGuestCapsLock != pInputSynch->fClientCapsLock))
1140 {
1141 pInputSynch->cGuestCapsLockAdaptions--;
1142 pKeyboard->PutScancode(0x3a);
1143 pKeyboard->PutScancode(0x3a | 0x80);
1144 }
1145}
1146
1147DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackInput(void *pvCallback, int type, const void *pvInput, unsigned cbInput)
1148{
1149 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1150 Console *pConsole = server->mConsole;
1151
1152 switch (type)
1153 {
1154 case VRDE_INPUT_SCANCODE:
1155 {
1156 if (cbInput == sizeof(VRDEINPUTSCANCODE))
1157 {
1158 IKeyboard *pKeyboard = pConsole->i_getKeyboard();
1159
1160 const VRDEINPUTSCANCODE *pInputScancode = (VRDEINPUTSCANCODE *)pvInput;
1161
1162 /* Track lock keys. */
1163 if (pInputScancode->uScancode == 0x45)
1164 {
1165 server->m_InputSynch.fClientNumLock = !server->m_InputSynch.fClientNumLock;
1166 }
1167 else if (pInputScancode->uScancode == 0x3a)
1168 {
1169 server->m_InputSynch.fClientCapsLock = !server->m_InputSynch.fClientCapsLock;
1170 }
1171 else if (pInputScancode->uScancode == 0x46)
1172 {
1173 server->m_InputSynch.fClientScrollLock = !server->m_InputSynch.fClientScrollLock;
1174 }
1175 else if ((pInputScancode->uScancode & 0x80) == 0)
1176 {
1177 /* Key pressed. */
1178 fixKbdLockStatus(&server->m_InputSynch, pKeyboard);
1179 }
1180
1181 pKeyboard->PutScancode((LONG)pInputScancode->uScancode);
1182 }
1183 } break;
1184
1185 case VRDE_INPUT_POINT:
1186 {
1187 if (cbInput == sizeof(VRDEINPUTPOINT))
1188 {
1189 const VRDEINPUTPOINT *pInputPoint = (VRDEINPUTPOINT *)pvInput;
1190
1191 int mouseButtons = 0;
1192 int iWheel = 0;
1193
1194 if (pInputPoint->uButtons & VRDE_INPUT_POINT_BUTTON1)
1195 {
1196 mouseButtons |= MouseButtonState_LeftButton;
1197 }
1198 if (pInputPoint->uButtons & VRDE_INPUT_POINT_BUTTON2)
1199 {
1200 mouseButtons |= MouseButtonState_RightButton;
1201 }
1202 if (pInputPoint->uButtons & VRDE_INPUT_POINT_BUTTON3)
1203 {
1204 mouseButtons |= MouseButtonState_MiddleButton;
1205 }
1206 if (pInputPoint->uButtons & VRDE_INPUT_POINT_WHEEL_UP)
1207 {
1208 mouseButtons |= MouseButtonState_WheelUp;
1209 iWheel = -1;
1210 }
1211 if (pInputPoint->uButtons & VRDE_INPUT_POINT_WHEEL_DOWN)
1212 {
1213 mouseButtons |= MouseButtonState_WheelDown;
1214 iWheel = 1;
1215 }
1216
1217 if (server->m_fGuestWantsAbsolute)
1218 {
1219 pConsole->i_getMouse()->PutMouseEventAbsolute(pInputPoint->x + 1, pInputPoint->y + 1, iWheel,
1220 0 /* Horizontal wheel */, mouseButtons);
1221 } else
1222 {
1223 pConsole->i_getMouse()->PutMouseEvent(pInputPoint->x - server->m_mousex,
1224 pInputPoint->y - server->m_mousey,
1225 iWheel, 0 /* Horizontal wheel */, mouseButtons);
1226 server->m_mousex = pInputPoint->x;
1227 server->m_mousey = pInputPoint->y;
1228 }
1229 }
1230 } break;
1231
1232 case VRDE_INPUT_CAD:
1233 {
1234 pConsole->i_getKeyboard()->PutCAD();
1235 } break;
1236
1237 case VRDE_INPUT_RESET:
1238 {
1239 pConsole->Reset();
1240 } break;
1241
1242 case VRDE_INPUT_SYNCH:
1243 {
1244 if (cbInput == sizeof(VRDEINPUTSYNCH))
1245 {
1246 IKeyboard *pKeyboard = pConsole->i_getKeyboard();
1247
1248 const VRDEINPUTSYNCH *pInputSynch = (VRDEINPUTSYNCH *)pvInput;
1249
1250 server->m_InputSynch.fClientNumLock = (pInputSynch->uLockStatus & VRDE_INPUT_SYNCH_NUMLOCK) != 0;
1251 server->m_InputSynch.fClientCapsLock = (pInputSynch->uLockStatus & VRDE_INPUT_SYNCH_CAPITAL) != 0;
1252 server->m_InputSynch.fClientScrollLock = (pInputSynch->uLockStatus & VRDE_INPUT_SYNCH_SCROLL) != 0;
1253
1254 /* The client initiated synchronization. Always make the guest to reflect the client state.
1255 * Than means, when the guest changes the state itself, it is forced to return to the client
1256 * state.
1257 */
1258 if (server->m_InputSynch.fClientNumLock != server->m_InputSynch.fGuestNumLock)
1259 {
1260 server->m_InputSynch.cGuestNumLockAdaptions = 2;
1261 }
1262
1263 if (server->m_InputSynch.fClientCapsLock != server->m_InputSynch.fGuestCapsLock)
1264 {
1265 server->m_InputSynch.cGuestCapsLockAdaptions = 2;
1266 }
1267
1268 fixKbdLockStatus(&server->m_InputSynch, pKeyboard);
1269 }
1270 } break;
1271
1272 default:
1273 break;
1274 }
1275}
1276
1277DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackVideoModeHint(void *pvCallback, unsigned cWidth, unsigned cHeight,
1278 unsigned cBitsPerPixel, unsigned uScreenId)
1279{
1280 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1281
1282 server->mConsole->i_getDisplay()->SetVideoModeHint(uScreenId, TRUE /*=enabled*/,
1283 FALSE /*=changeOrigin*/, 0/*=OriginX*/, 0/*=OriginY*/,
1284 cWidth, cHeight, cBitsPerPixel);
1285}
1286
1287DECLCALLBACK(void) ConsoleVRDPServer::VRDECallbackAudioIn(void *pvCallback,
1288 void *pvCtx,
1289 uint32_t u32ClientId,
1290 uint32_t u32Event,
1291 const void *pvData,
1292 uint32_t cbData)
1293{
1294 RT_NOREF(u32ClientId);
1295 ConsoleVRDPServer *pServer = static_cast<ConsoleVRDPServer*>(pvCallback);
1296 AssertPtrReturnVoid(pServer);
1297
1298#ifdef VBOX_WITH_VRDE_AUDIO
1299 AudioVRDE *pVRDE = pServer->mConsole->i_getAudioVRDE();
1300 if (!pVRDE) /* Nothing to do, bail out early. */
1301 return;
1302
1303 switch (u32Event)
1304 {
1305 case VRDE_AUDIOIN_BEGIN:
1306 {
1307 pVRDE->onVRDEInputBegin(pvCtx, (PVRDEAUDIOINBEGIN)pvData);
1308 break;
1309 }
1310
1311 case VRDE_AUDIOIN_DATA:
1312 pVRDE->onVRDEInputData(pvCtx, pvData, cbData);
1313 break;
1314
1315 case VRDE_AUDIOIN_END:
1316 pVRDE->onVRDEInputEnd(pvCtx);
1317 break;
1318
1319 default:
1320 break;
1321 }
1322#else
1323 RT_NOREF(pvCtx, u32Event, pvData, cbData);
1324#endif /* VBOX_WITH_VRDE_AUDIO */
1325}
1326
1327ConsoleVRDPServer::ConsoleVRDPServer(Console *console)
1328{
1329 mConsole = console;
1330
1331 int rc = RTCritSectInit(&mCritSect);
1332 AssertRC(rc);
1333
1334 mcClipboardRefs = 0;
1335 mpfnClipboardCallback = NULL;
1336#ifdef VBOX_WITH_USB
1337 mUSBBackends.pHead = NULL;
1338 mUSBBackends.pTail = NULL;
1339
1340 mUSBBackends.thread = NIL_RTTHREAD;
1341 mUSBBackends.fThreadRunning = false;
1342 mUSBBackends.event = 0;
1343#endif
1344
1345 mhServer = 0;
1346 mServerInterfaceVersion = 0;
1347
1348 mcInResize = 0;
1349
1350 m_fGuestWantsAbsolute = false;
1351 m_mousex = 0;
1352 m_mousey = 0;
1353
1354 m_InputSynch.cGuestNumLockAdaptions = 2;
1355 m_InputSynch.cGuestCapsLockAdaptions = 2;
1356
1357 m_InputSynch.fGuestNumLock = false;
1358 m_InputSynch.fGuestCapsLock = false;
1359 m_InputSynch.fGuestScrollLock = false;
1360
1361 m_InputSynch.fClientNumLock = false;
1362 m_InputSynch.fClientCapsLock = false;
1363 m_InputSynch.fClientScrollLock = false;
1364
1365 {
1366 ComPtr<IEventSource> es;
1367 console->COMGETTER(EventSource)(es.asOutParam());
1368 ComObjPtr<VRDPConsoleListenerImpl> aConsoleListener;
1369 aConsoleListener.createObject();
1370 aConsoleListener->init(new VRDPConsoleListener(), this);
1371 mConsoleListener = aConsoleListener;
1372 com::SafeArray <VBoxEventType_T> eventTypes;
1373 eventTypes.push_back(VBoxEventType_OnMousePointerShapeChanged);
1374 eventTypes.push_back(VBoxEventType_OnMouseCapabilityChanged);
1375 eventTypes.push_back(VBoxEventType_OnKeyboardLedsChanged);
1376 es->RegisterListener(mConsoleListener, ComSafeArrayAsInParam(eventTypes), true);
1377 }
1378
1379 mVRDPBindPort = -1;
1380
1381#ifndef VBOX_WITH_VRDEAUTH_IN_VBOXSVC
1382 RT_ZERO(mAuthLibCtx);
1383#endif
1384
1385 mu32AudioInputClientId = 0;
1386 mcClients = 0;
1387
1388 /*
1389 * Optional interfaces.
1390 */
1391 m_fInterfaceImage = false;
1392 RT_ZERO(m_interfaceImage);
1393 RT_ZERO(m_interfaceCallbacksImage);
1394 RT_ZERO(m_interfaceMousePtr);
1395 RT_ZERO(m_interfaceSCard);
1396 RT_ZERO(m_interfaceCallbacksSCard);
1397 RT_ZERO(m_interfaceTSMF);
1398 RT_ZERO(m_interfaceCallbacksTSMF);
1399 RT_ZERO(m_interfaceVideoIn);
1400 RT_ZERO(m_interfaceCallbacksVideoIn);
1401 RT_ZERO(m_interfaceInput);
1402 RT_ZERO(m_interfaceCallbacksInput);
1403
1404 rc = RTCritSectInit(&mTSMFLock);
1405 AssertRC(rc);
1406
1407 mEmWebcam = new EmWebcam(this);
1408 AssertPtr(mEmWebcam);
1409}
1410
1411ConsoleVRDPServer::~ConsoleVRDPServer()
1412{
1413 Stop();
1414
1415 if (mConsoleListener)
1416 {
1417 ComPtr<IEventSource> es;
1418 mConsole->COMGETTER(EventSource)(es.asOutParam());
1419 es->UnregisterListener(mConsoleListener);
1420 mConsoleListener.setNull();
1421 }
1422
1423 unsigned i;
1424 for (i = 0; i < RT_ELEMENTS(maSourceBitmaps); i++)
1425 {
1426 maSourceBitmaps[i].setNull();
1427 }
1428
1429 if (mEmWebcam)
1430 {
1431 delete mEmWebcam;
1432 mEmWebcam = NULL;
1433 }
1434
1435 if (RTCritSectIsInitialized(&mCritSect))
1436 {
1437 RTCritSectDelete(&mCritSect);
1438 RT_ZERO(mCritSect);
1439 }
1440
1441 if (RTCritSectIsInitialized(&mTSMFLock))
1442 {
1443 RTCritSectDelete(&mTSMFLock);
1444 RT_ZERO(mTSMFLock);
1445 }
1446}
1447
1448int ConsoleVRDPServer::Launch(void)
1449{
1450 LogFlowThisFunc(("\n"));
1451
1452 IVRDEServer *server = mConsole->i_getVRDEServer();
1453 AssertReturn(server, VERR_INTERNAL_ERROR_2);
1454
1455 /*
1456 * Check if VRDE is enabled.
1457 */
1458 BOOL fEnabled;
1459 HRESULT hrc = server->COMGETTER(Enabled)(&fEnabled);
1460 AssertComRCReturn(hrc, Global::vboxStatusCodeFromCOM(hrc));
1461 if (!fEnabled)
1462 return VINF_SUCCESS;
1463
1464 /*
1465 * Check that a VRDE extension pack name is set and resolve it into a
1466 * library path.
1467 */
1468 Bstr bstrExtPack;
1469 hrc = server->COMGETTER(VRDEExtPack)(bstrExtPack.asOutParam());
1470 if (FAILED(hrc))
1471 return Global::vboxStatusCodeFromCOM(hrc);
1472 if (bstrExtPack.isEmpty())
1473 return VINF_NOT_SUPPORTED;
1474
1475 Utf8Str strExtPack(bstrExtPack);
1476 Utf8Str strVrdeLibrary;
1477 int vrc = VINF_SUCCESS;
1478 if (strExtPack.equals(VBOXVRDP_KLUDGE_EXTPACK_NAME))
1479 strVrdeLibrary = "VBoxVRDP";
1480 else
1481 {
1482#ifdef VBOX_WITH_EXTPACK
1483 ExtPackManager *pExtPackMgr = mConsole->i_getExtPackManager();
1484 vrc = pExtPackMgr->i_getVrdeLibraryPathForExtPack(&strExtPack, &strVrdeLibrary);
1485#else
1486 vrc = VERR_FILE_NOT_FOUND;
1487#endif
1488 }
1489 if (RT_SUCCESS(vrc))
1490 {
1491 /*
1492 * Load the VRDE library and start the server, if it is enabled.
1493 */
1494 vrc = loadVRDPLibrary(strVrdeLibrary.c_str());
1495 if (RT_SUCCESS(vrc))
1496 {
1497 VRDEENTRYPOINTS_4 *pEntryPoints4;
1498 vrc = mpfnVRDECreateServer(&mCallbacks.header, this, (VRDEINTERFACEHDR **)&pEntryPoints4, &mhServer);
1499
1500 if (RT_SUCCESS(vrc))
1501 {
1502 mServerInterfaceVersion = 4;
1503 mEntryPoints = *pEntryPoints4;
1504 mpEntryPoints = &mEntryPoints;
1505 }
1506 else if (vrc == VERR_VERSION_MISMATCH)
1507 {
1508 /* An older version of VRDE is installed, try version 3. */
1509 VRDEENTRYPOINTS_3 *pEntryPoints3;
1510
1511 static VRDECALLBACKS_3 sCallbacks3 =
1512 {
1513 { VRDE_INTERFACE_VERSION_3, sizeof(VRDECALLBACKS_3) },
1514 ConsoleVRDPServer::VRDPCallbackQueryProperty,
1515 ConsoleVRDPServer::VRDPCallbackClientLogon,
1516 ConsoleVRDPServer::VRDPCallbackClientConnect,
1517 ConsoleVRDPServer::VRDPCallbackClientDisconnect,
1518 ConsoleVRDPServer::VRDPCallbackIntercept,
1519 ConsoleVRDPServer::VRDPCallbackUSB,
1520 ConsoleVRDPServer::VRDPCallbackClipboard,
1521 ConsoleVRDPServer::VRDPCallbackFramebufferQuery,
1522 ConsoleVRDPServer::VRDPCallbackFramebufferLock,
1523 ConsoleVRDPServer::VRDPCallbackFramebufferUnlock,
1524 ConsoleVRDPServer::VRDPCallbackInput,
1525 ConsoleVRDPServer::VRDPCallbackVideoModeHint,
1526 ConsoleVRDPServer::VRDECallbackAudioIn
1527 };
1528
1529 vrc = mpfnVRDECreateServer(&sCallbacks3.header, this, (VRDEINTERFACEHDR **)&pEntryPoints3, &mhServer);
1530 if (RT_SUCCESS(vrc))
1531 {
1532 mServerInterfaceVersion = 3;
1533 mEntryPoints.header = pEntryPoints3->header;
1534 mEntryPoints.VRDEDestroy = pEntryPoints3->VRDEDestroy;
1535 mEntryPoints.VRDEEnableConnections = pEntryPoints3->VRDEEnableConnections;
1536 mEntryPoints.VRDEDisconnect = pEntryPoints3->VRDEDisconnect;
1537 mEntryPoints.VRDEResize = pEntryPoints3->VRDEResize;
1538 mEntryPoints.VRDEUpdate = pEntryPoints3->VRDEUpdate;
1539 mEntryPoints.VRDEColorPointer = pEntryPoints3->VRDEColorPointer;
1540 mEntryPoints.VRDEHidePointer = pEntryPoints3->VRDEHidePointer;
1541 mEntryPoints.VRDEAudioSamples = pEntryPoints3->VRDEAudioSamples;
1542 mEntryPoints.VRDEAudioVolume = pEntryPoints3->VRDEAudioVolume;
1543 mEntryPoints.VRDEUSBRequest = pEntryPoints3->VRDEUSBRequest;
1544 mEntryPoints.VRDEClipboard = pEntryPoints3->VRDEClipboard;
1545 mEntryPoints.VRDEQueryInfo = pEntryPoints3->VRDEQueryInfo;
1546 mEntryPoints.VRDERedirect = pEntryPoints3->VRDERedirect;
1547 mEntryPoints.VRDEAudioInOpen = pEntryPoints3->VRDEAudioInOpen;
1548 mEntryPoints.VRDEAudioInClose = pEntryPoints3->VRDEAudioInClose;
1549 mEntryPoints.VRDEGetInterface = NULL;
1550 mpEntryPoints = &mEntryPoints;
1551 }
1552 else if (vrc == VERR_VERSION_MISMATCH)
1553 {
1554 /* An older version of VRDE is installed, try version 1. */
1555 VRDEENTRYPOINTS_1 *pEntryPoints1;
1556
1557 static VRDECALLBACKS_1 sCallbacks1 =
1558 {
1559 { VRDE_INTERFACE_VERSION_1, sizeof(VRDECALLBACKS_1) },
1560 ConsoleVRDPServer::VRDPCallbackQueryProperty,
1561 ConsoleVRDPServer::VRDPCallbackClientLogon,
1562 ConsoleVRDPServer::VRDPCallbackClientConnect,
1563 ConsoleVRDPServer::VRDPCallbackClientDisconnect,
1564 ConsoleVRDPServer::VRDPCallbackIntercept,
1565 ConsoleVRDPServer::VRDPCallbackUSB,
1566 ConsoleVRDPServer::VRDPCallbackClipboard,
1567 ConsoleVRDPServer::VRDPCallbackFramebufferQuery,
1568 ConsoleVRDPServer::VRDPCallbackFramebufferLock,
1569 ConsoleVRDPServer::VRDPCallbackFramebufferUnlock,
1570 ConsoleVRDPServer::VRDPCallbackInput,
1571 ConsoleVRDPServer::VRDPCallbackVideoModeHint
1572 };
1573
1574 vrc = mpfnVRDECreateServer(&sCallbacks1.header, this, (VRDEINTERFACEHDR **)&pEntryPoints1, &mhServer);
1575 if (RT_SUCCESS(vrc))
1576 {
1577 mServerInterfaceVersion = 1;
1578 mEntryPoints.header = pEntryPoints1->header;
1579 mEntryPoints.VRDEDestroy = pEntryPoints1->VRDEDestroy;
1580 mEntryPoints.VRDEEnableConnections = pEntryPoints1->VRDEEnableConnections;
1581 mEntryPoints.VRDEDisconnect = pEntryPoints1->VRDEDisconnect;
1582 mEntryPoints.VRDEResize = pEntryPoints1->VRDEResize;
1583 mEntryPoints.VRDEUpdate = pEntryPoints1->VRDEUpdate;
1584 mEntryPoints.VRDEColorPointer = pEntryPoints1->VRDEColorPointer;
1585 mEntryPoints.VRDEHidePointer = pEntryPoints1->VRDEHidePointer;
1586 mEntryPoints.VRDEAudioSamples = pEntryPoints1->VRDEAudioSamples;
1587 mEntryPoints.VRDEAudioVolume = pEntryPoints1->VRDEAudioVolume;
1588 mEntryPoints.VRDEUSBRequest = pEntryPoints1->VRDEUSBRequest;
1589 mEntryPoints.VRDEClipboard = pEntryPoints1->VRDEClipboard;
1590 mEntryPoints.VRDEQueryInfo = pEntryPoints1->VRDEQueryInfo;
1591 mEntryPoints.VRDERedirect = NULL;
1592 mEntryPoints.VRDEAudioInOpen = NULL;
1593 mEntryPoints.VRDEAudioInClose = NULL;
1594 mEntryPoints.VRDEGetInterface = NULL;
1595 mpEntryPoints = &mEntryPoints;
1596 }
1597 }
1598 }
1599
1600 if (RT_SUCCESS(vrc))
1601 {
1602 LogRel(("VRDE: loaded version %d of the server.\n", mServerInterfaceVersion));
1603
1604 if (mServerInterfaceVersion >= 4)
1605 {
1606 /* The server supports optional interfaces. */
1607 Assert(mpEntryPoints->VRDEGetInterface != NULL);
1608
1609 /* Image interface. */
1610 m_interfaceImage.header.u64Version = 1;
1611 m_interfaceImage.header.u64Size = sizeof(m_interfaceImage);
1612
1613 m_interfaceCallbacksImage.header.u64Version = 1;
1614 m_interfaceCallbacksImage.header.u64Size = sizeof(m_interfaceCallbacksImage);
1615 m_interfaceCallbacksImage.VRDEImageCbNotify = VRDEImageCbNotify;
1616
1617 vrc = mpEntryPoints->VRDEGetInterface(mhServer,
1618 VRDE_IMAGE_INTERFACE_NAME,
1619 &m_interfaceImage.header,
1620 &m_interfaceCallbacksImage.header,
1621 this);
1622 if (RT_SUCCESS(vrc))
1623 {
1624 LogRel(("VRDE: [%s]\n", VRDE_IMAGE_INTERFACE_NAME));
1625 m_fInterfaceImage = true;
1626 }
1627
1628 /* Mouse pointer interface. */
1629 m_interfaceMousePtr.header.u64Version = 1;
1630 m_interfaceMousePtr.header.u64Size = sizeof(m_interfaceMousePtr);
1631
1632 vrc = mpEntryPoints->VRDEGetInterface(mhServer,
1633 VRDE_MOUSEPTR_INTERFACE_NAME,
1634 &m_interfaceMousePtr.header,
1635 NULL,
1636 this);
1637 if (RT_SUCCESS(vrc))
1638 {
1639 LogRel(("VRDE: [%s]\n", VRDE_MOUSEPTR_INTERFACE_NAME));
1640 }
1641 else
1642 {
1643 RT_ZERO(m_interfaceMousePtr);
1644 }
1645
1646 /* Smartcard interface. */
1647 m_interfaceSCard.header.u64Version = 1;
1648 m_interfaceSCard.header.u64Size = sizeof(m_interfaceSCard);
1649
1650 m_interfaceCallbacksSCard.header.u64Version = 1;
1651 m_interfaceCallbacksSCard.header.u64Size = sizeof(m_interfaceCallbacksSCard);
1652 m_interfaceCallbacksSCard.VRDESCardCbNotify = VRDESCardCbNotify;
1653 m_interfaceCallbacksSCard.VRDESCardCbResponse = VRDESCardCbResponse;
1654
1655 vrc = mpEntryPoints->VRDEGetInterface(mhServer,
1656 VRDE_SCARD_INTERFACE_NAME,
1657 &m_interfaceSCard.header,
1658 &m_interfaceCallbacksSCard.header,
1659 this);
1660 if (RT_SUCCESS(vrc))
1661 {
1662 LogRel(("VRDE: [%s]\n", VRDE_SCARD_INTERFACE_NAME));
1663 }
1664 else
1665 {
1666 RT_ZERO(m_interfaceSCard);
1667 }
1668
1669 /* Raw TSMF interface. */
1670 m_interfaceTSMF.header.u64Version = 1;
1671 m_interfaceTSMF.header.u64Size = sizeof(m_interfaceTSMF);
1672
1673 m_interfaceCallbacksTSMF.header.u64Version = 1;
1674 m_interfaceCallbacksTSMF.header.u64Size = sizeof(m_interfaceCallbacksTSMF);
1675 m_interfaceCallbacksTSMF.VRDETSMFCbNotify = VRDETSMFCbNotify;
1676
1677 vrc = mpEntryPoints->VRDEGetInterface(mhServer,
1678 VRDE_TSMF_INTERFACE_NAME,
1679 &m_interfaceTSMF.header,
1680 &m_interfaceCallbacksTSMF.header,
1681 this);
1682 if (RT_SUCCESS(vrc))
1683 {
1684 LogRel(("VRDE: [%s]\n", VRDE_TSMF_INTERFACE_NAME));
1685 }
1686 else
1687 {
1688 RT_ZERO(m_interfaceTSMF);
1689 }
1690
1691 /* VideoIn interface. */
1692 m_interfaceVideoIn.header.u64Version = 1;
1693 m_interfaceVideoIn.header.u64Size = sizeof(m_interfaceVideoIn);
1694
1695 m_interfaceCallbacksVideoIn.header.u64Version = 1;
1696 m_interfaceCallbacksVideoIn.header.u64Size = sizeof(m_interfaceCallbacksVideoIn);
1697 m_interfaceCallbacksVideoIn.VRDECallbackVideoInNotify = VRDECallbackVideoInNotify;
1698 m_interfaceCallbacksVideoIn.VRDECallbackVideoInDeviceDesc = VRDECallbackVideoInDeviceDesc;
1699 m_interfaceCallbacksVideoIn.VRDECallbackVideoInControl = VRDECallbackVideoInControl;
1700 m_interfaceCallbacksVideoIn.VRDECallbackVideoInFrame = VRDECallbackVideoInFrame;
1701
1702 vrc = mpEntryPoints->VRDEGetInterface(mhServer,
1703 VRDE_VIDEOIN_INTERFACE_NAME,
1704 &m_interfaceVideoIn.header,
1705 &m_interfaceCallbacksVideoIn.header,
1706 this);
1707 if (RT_SUCCESS(vrc))
1708 {
1709 LogRel(("VRDE: [%s]\n", VRDE_VIDEOIN_INTERFACE_NAME));
1710 }
1711 else
1712 {
1713 RT_ZERO(m_interfaceVideoIn);
1714 }
1715
1716 /* Input interface. */
1717 m_interfaceInput.header.u64Version = 1;
1718 m_interfaceInput.header.u64Size = sizeof(m_interfaceInput);
1719
1720 m_interfaceCallbacksInput.header.u64Version = 1;
1721 m_interfaceCallbacksInput.header.u64Size = sizeof(m_interfaceCallbacksInput);
1722 m_interfaceCallbacksInput.VRDECallbackInputSetup = VRDECallbackInputSetup;
1723 m_interfaceCallbacksInput.VRDECallbackInputEvent = VRDECallbackInputEvent;
1724
1725 vrc = mpEntryPoints->VRDEGetInterface(mhServer,
1726 VRDE_INPUT_INTERFACE_NAME,
1727 &m_interfaceInput.header,
1728 &m_interfaceCallbacksInput.header,
1729 this);
1730 if (RT_SUCCESS(vrc))
1731 {
1732 LogRel(("VRDE: [%s]\n", VRDE_INPUT_INTERFACE_NAME));
1733 }
1734 else
1735 {
1736 RT_ZERO(m_interfaceInput);
1737 }
1738
1739 /* Since these interfaces are optional, it is always a success here. */
1740 vrc = VINF_SUCCESS;
1741 }
1742#ifdef VBOX_WITH_USB
1743 remoteUSBThreadStart();
1744#endif
1745
1746 /*
1747 * Re-init the server current state, which is usually obtained from events.
1748 */
1749 fetchCurrentState();
1750 }
1751 else
1752 {
1753 if (vrc != VERR_NET_ADDRESS_IN_USE)
1754 LogRel(("VRDE: Could not start the server rc = %Rrc\n", vrc));
1755 /* Don't unload the lib, because it prevents us trying again or
1756 because there may be other users? */
1757 }
1758 }
1759 }
1760
1761 return vrc;
1762}
1763
1764void ConsoleVRDPServer::fetchCurrentState(void)
1765{
1766 ComPtr<IMousePointerShape> mps;
1767 mConsole->i_getMouse()->COMGETTER(PointerShape)(mps.asOutParam());
1768 if (!mps.isNull())
1769 {
1770 BOOL visible, alpha;
1771 ULONG hotX, hotY, width, height;
1772 com::SafeArray <BYTE> shape;
1773
1774 mps->COMGETTER(Visible)(&visible);
1775 mps->COMGETTER(Alpha)(&alpha);
1776 mps->COMGETTER(HotX)(&hotX);
1777 mps->COMGETTER(HotY)(&hotY);
1778 mps->COMGETTER(Width)(&width);
1779 mps->COMGETTER(Height)(&height);
1780 mps->COMGETTER(Shape)(ComSafeArrayAsOutParam(shape));
1781
1782 onMousePointerShapeChange(visible, alpha, hotX, hotY, width, height, ComSafeArrayAsInParam(shape));
1783 }
1784}
1785
1786typedef struct H3DORInstance
1787{
1788 ConsoleVRDPServer *pThis;
1789 HVRDEIMAGE hImageBitmap;
1790 int32_t x;
1791 int32_t y;
1792 uint32_t w;
1793 uint32_t h;
1794 bool fCreated;
1795 bool fFallback;
1796 bool fTopDown;
1797} H3DORInstance;
1798
1799#define H3DORLOG Log
1800
1801/* static */ DECLCALLBACK(void) ConsoleVRDPServer::H3DORBegin(const void *pvContext, void **ppvInstance,
1802 const char *pszFormat)
1803{
1804 H3DORLOG(("H3DORBegin: ctx %p [%s]\n", pvContext, pszFormat));
1805
1806 H3DORInstance *p = (H3DORInstance *)RTMemAlloc(sizeof(H3DORInstance));
1807
1808 if (p)
1809 {
1810 p->pThis = (ConsoleVRDPServer *)pvContext;
1811 p->hImageBitmap = NULL;
1812 p->x = 0;
1813 p->y = 0;
1814 p->w = 0;
1815 p->h = 0;
1816 p->fCreated = false;
1817 p->fFallback = false;
1818
1819 /* Host 3D service passes the actual format of data in this redirect instance.
1820 * That is what will be in the H3DORFrame's parameters pvData and cbData.
1821 */
1822 if (RTStrICmp(pszFormat, H3DOR_FMT_RGBA_TOPDOWN) == 0)
1823 {
1824 /* Accept it. */
1825 p->fTopDown = true;
1826 }
1827 else if (RTStrICmp(pszFormat, H3DOR_FMT_RGBA) == 0)
1828 {
1829 /* Accept it. */
1830 p->fTopDown = false;
1831 }
1832 else
1833 {
1834 RTMemFree(p);
1835 p = NULL;
1836 }
1837 }
1838
1839 H3DORLOG(("H3DORBegin: ins %p\n", p));
1840
1841 /* Caller checks this for NULL. */
1842 *ppvInstance = p;
1843}
1844
1845/* static */ DECLCALLBACK(void) ConsoleVRDPServer::H3DORGeometry(void *pvInstance,
1846 int32_t x, int32_t y, uint32_t w, uint32_t h)
1847{
1848 H3DORLOG(("H3DORGeometry: ins %p %d,%d %dx%d\n", pvInstance, x, y, w, h));
1849
1850 H3DORInstance *p = (H3DORInstance *)pvInstance;
1851 Assert(p);
1852 Assert(p->pThis);
1853
1854 /** @todo find out what to do if size changes to 0x0 from non zero */
1855 if (w == 0 || h == 0)
1856 {
1857 /* Do nothing. */
1858 return;
1859 }
1860
1861 RTRECT rect;
1862 rect.xLeft = x;
1863 rect.yTop = y;
1864 rect.xRight = x + w;
1865 rect.yBottom = y + h;
1866
1867 if (p->hImageBitmap)
1868 {
1869 /* An image handle has been already created,
1870 * check if it has the same size as the reported geometry.
1871 */
1872 if ( p->x == x
1873 && p->y == y
1874 && p->w == w
1875 && p->h == h)
1876 {
1877 H3DORLOG(("H3DORGeometry: geometry not changed\n"));
1878 /* Do nothing. Continue using the existing handle. */
1879 }
1880 else
1881 {
1882 int rc = p->fFallback?
1883 VERR_NOT_SUPPORTED: /* Try to go out of fallback mode. */
1884 p->pThis->m_interfaceImage.VRDEImageGeometrySet(p->hImageBitmap, &rect);
1885 if (RT_SUCCESS(rc))
1886 {
1887 p->x = x;
1888 p->y = y;
1889 p->w = w;
1890 p->h = h;
1891 }
1892 else
1893 {
1894 /* The handle must be recreated. Delete existing handle here. */
1895 p->pThis->m_interfaceImage.VRDEImageHandleClose(p->hImageBitmap);
1896 p->hImageBitmap = NULL;
1897 }
1898 }
1899 }
1900
1901 if (!p->hImageBitmap)
1902 {
1903 /* Create a new bitmap handle. */
1904 uint32_t u32ScreenId = 0; /** @todo clip to corresponding screens.
1905 * Clipping can be done here or in VRDP server.
1906 * If VRDP does clipping, then uScreenId parameter
1907 * is not necessary and coords must be global.
1908 * (have to check which coords are used in opengl service).
1909 * Since all VRDE API uses a ScreenId,
1910 * the clipping must be done here in ConsoleVRDPServer
1911 */
1912 uint32_t fu32CompletionFlags = 0;
1913 p->fFallback = false;
1914 int rc = p->pThis->m_interfaceImage.VRDEImageHandleCreate(p->pThis->mhServer,
1915 &p->hImageBitmap,
1916 p,
1917 u32ScreenId,
1918 VRDE_IMAGE_F_CREATE_CONTENT_3D
1919 | VRDE_IMAGE_F_CREATE_WINDOW,
1920 &rect,
1921 VRDE_IMAGE_FMT_ID_BITMAP_BGRA8,
1922 NULL,
1923 0,
1924 &fu32CompletionFlags);
1925 if (RT_FAILURE(rc))
1926 {
1927 /* No support for a 3D + WINDOW. Try bitmap updates. */
1928 H3DORLOG(("H3DORGeometry: Fallback to bitmaps\n"));
1929 fu32CompletionFlags = 0;
1930 p->fFallback = true;
1931 rc = p->pThis->m_interfaceImage.VRDEImageHandleCreate(p->pThis->mhServer,
1932 &p->hImageBitmap,
1933 p,
1934 u32ScreenId,
1935 0,
1936 &rect,
1937 VRDE_IMAGE_FMT_ID_BITMAP_BGRA8,
1938 NULL,
1939 0,
1940 &fu32CompletionFlags);
1941 }
1942
1943 H3DORLOG(("H3DORGeometry: Image handle create %Rrc, flags 0x%RX32\n", rc, fu32CompletionFlags));
1944
1945 if (RT_SUCCESS(rc))
1946 {
1947 p->x = x;
1948 p->y = y;
1949 p->w = w;
1950 p->h = h;
1951
1952 if ((fu32CompletionFlags & VRDE_IMAGE_F_COMPLETE_ASYNC) == 0)
1953 {
1954 p->fCreated = true;
1955 }
1956 }
1957 else
1958 {
1959 p->hImageBitmap = NULL;
1960 p->w = 0;
1961 p->h = 0;
1962 }
1963 }
1964
1965 H3DORLOG(("H3DORGeometry: ins %p completed\n", pvInstance));
1966}
1967
1968/* static */ DECLCALLBACK(void) ConsoleVRDPServer::H3DORVisibleRegion(void *pvInstance,
1969 uint32_t cRects, const RTRECT *paRects)
1970{
1971 H3DORLOG(("H3DORVisibleRegion: ins %p %d\n", pvInstance, cRects));
1972
1973 H3DORInstance *p = (H3DORInstance *)pvInstance;
1974 Assert(p);
1975 Assert(p->pThis);
1976
1977 if (cRects == 0)
1978 {
1979 /* Complete image is visible. */
1980 RTRECT rect;
1981 rect.xLeft = p->x;
1982 rect.yTop = p->y;
1983 rect.xRight = p->x + p->w;
1984 rect.yBottom = p->y + p->h;
1985 p->pThis->m_interfaceImage.VRDEImageRegionSet (p->hImageBitmap,
1986 1,
1987 &rect);
1988 }
1989 else
1990 {
1991 p->pThis->m_interfaceImage.VRDEImageRegionSet (p->hImageBitmap,
1992 cRects,
1993 paRects);
1994 }
1995
1996 H3DORLOG(("H3DORVisibleRegion: ins %p completed\n", pvInstance));
1997}
1998
1999/* static */ DECLCALLBACK(void) ConsoleVRDPServer::H3DORFrame(void *pvInstance,
2000 void *pvData, uint32_t cbData)
2001{
2002 H3DORLOG(("H3DORFrame: ins %p %p %d\n", pvInstance, pvData, cbData));
2003
2004 H3DORInstance *p = (H3DORInstance *)pvInstance;
2005 Assert(p);
2006 Assert(p->pThis);
2007
2008 /* Currently only a topdown BGR0 bitmap format is supported. */
2009 VRDEIMAGEBITMAP image;
2010
2011 image.cWidth = p->w;
2012 image.cHeight = p->h;
2013 image.pvData = pvData;
2014 image.cbData = cbData;
2015 image.pvScanLine0 = (uint8_t *)pvData + (p->h - 1) * p->w * 4;
2016 image.iScanDelta = 4 * p->w;
2017 if (p->fTopDown)
2018 {
2019 image.iScanDelta = -image.iScanDelta;
2020 }
2021
2022 p->pThis->m_interfaceImage.VRDEImageUpdate (p->hImageBitmap,
2023 p->x,
2024 p->y,
2025 p->w,
2026 p->h,
2027 &image,
2028 sizeof(VRDEIMAGEBITMAP));
2029
2030 H3DORLOG(("H3DORFrame: ins %p completed\n", pvInstance));
2031}
2032
2033/* static */ DECLCALLBACK(void) ConsoleVRDPServer::H3DOREnd(void *pvInstance)
2034{
2035 H3DORLOG(("H3DOREnd: ins %p\n", pvInstance));
2036
2037 H3DORInstance *p = (H3DORInstance *)pvInstance;
2038 Assert(p);
2039 Assert(p->pThis);
2040
2041 p->pThis->m_interfaceImage.VRDEImageHandleClose(p->hImageBitmap);
2042
2043 RTMemFree(p);
2044
2045 H3DORLOG(("H3DOREnd: ins %p completed\n", pvInstance));
2046}
2047
2048/* static */ DECLCALLBACK(int) ConsoleVRDPServer::H3DORContextProperty(const void *pvContext, uint32_t index,
2049 void *pvBuffer, uint32_t cbBuffer, uint32_t *pcbOut)
2050{
2051 RT_NOREF(pvContext, pvBuffer);
2052 int rc = VINF_SUCCESS;
2053
2054 H3DORLOG(("H3DORContextProperty: index %d\n", index));
2055
2056 if (index == H3DOR_PROP_FORMATS)
2057 {
2058 /* Return a comma separated list of supported formats. */
2059 uint32_t cbOut = (uint32_t)strlen(H3DOR_FMT_RGBA_TOPDOWN) + 1
2060 + (uint32_t)strlen(H3DOR_FMT_RGBA) + 1;
2061 if (cbOut <= cbBuffer)
2062 {
2063 char *pch = (char *)pvBuffer;
2064 memcpy(pch, H3DOR_FMT_RGBA_TOPDOWN, strlen(H3DOR_FMT_RGBA_TOPDOWN));
2065 pch += strlen(H3DOR_FMT_RGBA_TOPDOWN);
2066 *pch++ = ',';
2067 memcpy(pch, H3DOR_FMT_RGBA, strlen(H3DOR_FMT_RGBA));
2068 pch += strlen(H3DOR_FMT_RGBA);
2069 *pch++ = '\0';
2070 }
2071 else
2072 {
2073 rc = VERR_BUFFER_OVERFLOW;
2074 }
2075 *pcbOut = cbOut;
2076 }
2077 else
2078 {
2079 rc = VERR_NOT_SUPPORTED;
2080 }
2081
2082 H3DORLOG(("H3DORContextProperty: %Rrc\n", rc));
2083 return rc;
2084}
2085
2086void ConsoleVRDPServer::remote3DRedirect(bool fEnable)
2087{
2088 if (!m_fInterfaceImage)
2089 {
2090 /* No redirect without corresponding interface. */
2091 return;
2092 }
2093
2094 /* Check if 3D redirection has been enabled. It is enabled by default. */
2095 com::Bstr bstr;
2096 HRESULT hrc = mConsole->i_getVRDEServer()->GetVRDEProperty(Bstr("H3DRedirect/Enabled").raw(), bstr.asOutParam());
2097
2098 com::Utf8Str value = hrc == S_OK? bstr: "";
2099
2100 bool fAllowed = RTStrICmp(value.c_str(), "true") == 0
2101 || RTStrICmp(value.c_str(), "1") == 0
2102 || value.c_str()[0] == 0;
2103
2104 if (!fAllowed && fEnable)
2105 {
2106 return;
2107 }
2108
2109 /* Tell the host 3D service to redirect output using the ConsoleVRDPServer callbacks. */
2110 H3DOUTPUTREDIRECT outputRedirect =
2111 {
2112 this,
2113 H3DORBegin,
2114 H3DORGeometry,
2115 H3DORVisibleRegion,
2116 H3DORFrame,
2117 H3DOREnd,
2118 H3DORContextProperty
2119 };
2120
2121 if (!fEnable)
2122 {
2123 /* This will tell the service to disable rediection. */
2124 RT_ZERO(outputRedirect);
2125 }
2126
2127#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
2128 VBOXCRCMDCTL_HGCM data;
2129 data.Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
2130 data.Hdr.u32Function = SHCRGL_HOST_FN_SET_OUTPUT_REDIRECT;
2131
2132 data.aParms[0].type = VBOX_HGCM_SVC_PARM_PTR;
2133 data.aParms[0].u.pointer.addr = &outputRedirect;
2134 data.aParms[0].u.pointer.size = sizeof(outputRedirect);
2135
2136 int rc = mConsole->i_getDisplay()->i_crCtlSubmitSync(&data.Hdr, sizeof (data));
2137 if (!RT_SUCCESS(rc))
2138 {
2139 Log(("SHCRGL_HOST_FN_SET_CONSOLE failed with %Rrc\n", rc));
2140 return;
2141 }
2142
2143 LogRel(("VRDE: %s 3D redirect.\n", fEnable? "Enabled": "Disabled"));
2144# ifdef DEBUG_misha
2145 AssertFailed();
2146# endif
2147#endif
2148
2149 return;
2150}
2151
2152/* static */ DECLCALLBACK(int) ConsoleVRDPServer::VRDEImageCbNotify (void *pvContext,
2153 void *pvUser,
2154 HVRDEIMAGE hVideo,
2155 uint32_t u32Id,
2156 void *pvData,
2157 uint32_t cbData)
2158{
2159 RT_NOREF(hVideo);
2160 H3DORLOG(("H3DOR: VRDEImageCbNotify: pvContext %p, pvUser %p, hVideo %p, u32Id %u, pvData %p, cbData %d\n",
2161 pvContext, pvUser, hVideo, u32Id, pvData, cbData));
2162
2163 ConsoleVRDPServer *pServer = static_cast<ConsoleVRDPServer*>(pvContext); NOREF(pServer);
2164 H3DORInstance *p = (H3DORInstance *)pvUser;
2165 Assert(p);
2166 Assert(p->pThis);
2167 Assert(p->pThis == pServer);
2168
2169 if (u32Id == VRDE_IMAGE_NOTIFY_HANDLE_CREATE)
2170 {
2171 if (cbData != sizeof(uint32_t))
2172 {
2173 AssertFailed();
2174 return VERR_INVALID_PARAMETER;
2175 }
2176
2177 uint32_t u32StreamId = *(uint32_t *)pvData;
2178 H3DORLOG(("H3DOR: VRDE_IMAGE_NOTIFY_HANDLE_CREATE u32StreamId %d\n",
2179 u32StreamId));
2180
2181 if (u32StreamId != 0)
2182 {
2183 p->fCreated = true; /// @todo not needed?
2184 }
2185 else
2186 {
2187 /* The stream has not been created. */
2188 }
2189 }
2190
2191 return VINF_SUCCESS;
2192}
2193
2194#undef H3DORLOG
2195
2196/* static */ DECLCALLBACK(int) ConsoleVRDPServer::VRDESCardCbNotify(void *pvContext,
2197 uint32_t u32Id,
2198 void *pvData,
2199 uint32_t cbData)
2200{
2201#ifdef VBOX_WITH_USB_CARDREADER
2202 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvContext);
2203 UsbCardReader *pReader = pThis->mConsole->i_getUsbCardReader();
2204 return pReader->VRDENotify(u32Id, pvData, cbData);
2205#else
2206 NOREF(pvContext);
2207 NOREF(u32Id);
2208 NOREF(pvData);
2209 NOREF(cbData);
2210 return VERR_NOT_SUPPORTED;
2211#endif
2212}
2213
2214/* static */ DECLCALLBACK(int) ConsoleVRDPServer::VRDESCardCbResponse(void *pvContext,
2215 int rcRequest,
2216 void *pvUser,
2217 uint32_t u32Function,
2218 void *pvData,
2219 uint32_t cbData)
2220{
2221#ifdef VBOX_WITH_USB_CARDREADER
2222 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvContext);
2223 UsbCardReader *pReader = pThis->mConsole->i_getUsbCardReader();
2224 return pReader->VRDEResponse(rcRequest, pvUser, u32Function, pvData, cbData);
2225#else
2226 NOREF(pvContext);
2227 NOREF(rcRequest);
2228 NOREF(pvUser);
2229 NOREF(u32Function);
2230 NOREF(pvData);
2231 NOREF(cbData);
2232 return VERR_NOT_SUPPORTED;
2233#endif
2234}
2235
2236int ConsoleVRDPServer::SCardRequest(void *pvUser, uint32_t u32Function, const void *pvData, uint32_t cbData)
2237{
2238 int rc = VINF_SUCCESS;
2239
2240 if (mhServer && mpEntryPoints && m_interfaceSCard.VRDESCardRequest)
2241 {
2242 rc = m_interfaceSCard.VRDESCardRequest(mhServer, pvUser, u32Function, pvData, cbData);
2243 }
2244 else
2245 {
2246 rc = VERR_NOT_SUPPORTED;
2247 }
2248
2249 return rc;
2250}
2251
2252
2253struct TSMFHOSTCHCTX;
2254struct TSMFVRDPCTX;
2255
2256typedef struct TSMFHOSTCHCTX
2257{
2258 ConsoleVRDPServer *pThis;
2259
2260 struct TSMFVRDPCTX *pVRDPCtx; /* NULL if no corresponding host channel context. */
2261
2262 void *pvDataReceived;
2263 uint32_t cbDataReceived;
2264 uint32_t cbDataAllocated;
2265} TSMFHOSTCHCTX;
2266
2267typedef struct TSMFVRDPCTX
2268{
2269 ConsoleVRDPServer *pThis;
2270
2271 VBOXHOSTCHANNELCALLBACKS *pCallbacks;
2272 void *pvCallbacks;
2273
2274 TSMFHOSTCHCTX *pHostChCtx; /* NULL if no corresponding host channel context. */
2275
2276 uint32_t u32ChannelHandle;
2277} TSMFVRDPCTX;
2278
2279static int tsmfContextsAlloc(TSMFHOSTCHCTX **ppHostChCtx, TSMFVRDPCTX **ppVRDPCtx)
2280{
2281 TSMFHOSTCHCTX *pHostChCtx = (TSMFHOSTCHCTX *)RTMemAllocZ(sizeof(TSMFHOSTCHCTX));
2282 if (!pHostChCtx)
2283 {
2284 return VERR_NO_MEMORY;
2285 }
2286
2287 TSMFVRDPCTX *pVRDPCtx = (TSMFVRDPCTX *)RTMemAllocZ(sizeof(TSMFVRDPCTX));
2288 if (!pVRDPCtx)
2289 {
2290 RTMemFree(pHostChCtx);
2291 return VERR_NO_MEMORY;
2292 }
2293
2294 *ppHostChCtx = pHostChCtx;
2295 *ppVRDPCtx = pVRDPCtx;
2296 return VINF_SUCCESS;
2297}
2298
2299int ConsoleVRDPServer::tsmfLock(void)
2300{
2301 int rc = RTCritSectEnter(&mTSMFLock);
2302 AssertRC(rc);
2303 return rc;
2304}
2305
2306void ConsoleVRDPServer::tsmfUnlock(void)
2307{
2308 RTCritSectLeave(&mTSMFLock);
2309}
2310
2311/* static */ DECLCALLBACK(int) ConsoleVRDPServer::tsmfHostChannelAttach(void *pvProvider,
2312 void **ppvChannel,
2313 uint32_t u32Flags,
2314 VBOXHOSTCHANNELCALLBACKS *pCallbacks,
2315 void *pvCallbacks)
2316{
2317 LogFlowFunc(("\n"));
2318
2319 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvProvider);
2320
2321 /* Create 2 context structures: for the VRDP server and for the host service. */
2322 TSMFHOSTCHCTX *pHostChCtx = NULL;
2323 TSMFVRDPCTX *pVRDPCtx = NULL;
2324
2325 int rc = tsmfContextsAlloc(&pHostChCtx, &pVRDPCtx);
2326 if (RT_FAILURE(rc))
2327 {
2328 return rc;
2329 }
2330
2331 pHostChCtx->pThis = pThis;
2332 pHostChCtx->pVRDPCtx = pVRDPCtx;
2333
2334 pVRDPCtx->pThis = pThis;
2335 pVRDPCtx->pCallbacks = pCallbacks;
2336 pVRDPCtx->pvCallbacks = pvCallbacks;
2337 pVRDPCtx->pHostChCtx = pHostChCtx;
2338
2339 rc = pThis->m_interfaceTSMF.VRDETSMFChannelCreate(pThis->mhServer, pVRDPCtx, u32Flags);
2340
2341 if (RT_SUCCESS(rc))
2342 {
2343 /** @todo contexts should be in a list for accounting. */
2344 *ppvChannel = pHostChCtx;
2345 }
2346 else
2347 {
2348 RTMemFree(pHostChCtx);
2349 RTMemFree(pVRDPCtx);
2350 }
2351
2352 return rc;
2353}
2354
2355/* static */ DECLCALLBACK(void) ConsoleVRDPServer::tsmfHostChannelDetach(void *pvChannel)
2356{
2357 LogFlowFunc(("\n"));
2358
2359 TSMFHOSTCHCTX *pHostChCtx = (TSMFHOSTCHCTX *)pvChannel;
2360 ConsoleVRDPServer *pThis = pHostChCtx->pThis;
2361
2362 int rc = pThis->tsmfLock();
2363 if (RT_SUCCESS(rc))
2364 {
2365 bool fClose = false;
2366 uint32_t u32ChannelHandle = 0;
2367
2368 if (pHostChCtx->pVRDPCtx)
2369 {
2370 /* There is still a VRDP context for this channel. */
2371 pHostChCtx->pVRDPCtx->pHostChCtx = NULL;
2372 u32ChannelHandle = pHostChCtx->pVRDPCtx->u32ChannelHandle;
2373 fClose = true;
2374 }
2375
2376 pThis->tsmfUnlock();
2377
2378 RTMemFree(pHostChCtx);
2379
2380 if (fClose)
2381 {
2382 LogFlowFunc(("Closing VRDE channel %d.\n", u32ChannelHandle));
2383 pThis->m_interfaceTSMF.VRDETSMFChannelClose(pThis->mhServer, u32ChannelHandle);
2384 }
2385 else
2386 {
2387 LogFlowFunc(("No VRDE channel.\n"));
2388 }
2389 }
2390}
2391
2392/* static */ DECLCALLBACK(int) ConsoleVRDPServer::tsmfHostChannelSend(void *pvChannel,
2393 const void *pvData,
2394 uint32_t cbData)
2395{
2396 LogFlowFunc(("cbData %d\n", cbData));
2397
2398 TSMFHOSTCHCTX *pHostChCtx = (TSMFHOSTCHCTX *)pvChannel;
2399 ConsoleVRDPServer *pThis = pHostChCtx->pThis;
2400
2401 int rc = pThis->tsmfLock();
2402 if (RT_SUCCESS(rc))
2403 {
2404 bool fSend = false;
2405 uint32_t u32ChannelHandle = 0;
2406
2407 if (pHostChCtx->pVRDPCtx)
2408 {
2409 u32ChannelHandle = pHostChCtx->pVRDPCtx->u32ChannelHandle;
2410 fSend = true;
2411 }
2412
2413 pThis->tsmfUnlock();
2414
2415 if (fSend)
2416 {
2417 LogFlowFunc(("Send to VRDE channel %d.\n", u32ChannelHandle));
2418 rc = pThis->m_interfaceTSMF.VRDETSMFChannelSend(pThis->mhServer, u32ChannelHandle,
2419 pvData, cbData);
2420 }
2421 }
2422
2423 return rc;
2424}
2425
2426/* static */ DECLCALLBACK(int) ConsoleVRDPServer::tsmfHostChannelRecv(void *pvChannel,
2427 void *pvData,
2428 uint32_t cbData,
2429 uint32_t *pcbReceived,
2430 uint32_t *pcbRemaining)
2431{
2432 LogFlowFunc(("cbData %d\n", cbData));
2433
2434 TSMFHOSTCHCTX *pHostChCtx = (TSMFHOSTCHCTX *)pvChannel;
2435 ConsoleVRDPServer *pThis = pHostChCtx->pThis;
2436
2437 int rc = pThis->tsmfLock();
2438 if (RT_SUCCESS(rc))
2439 {
2440 uint32_t cbToCopy = RT_MIN(cbData, pHostChCtx->cbDataReceived);
2441 uint32_t cbRemaining = pHostChCtx->cbDataReceived - cbToCopy;
2442
2443 LogFlowFunc(("cbToCopy %d, cbRemaining %d\n", cbToCopy, cbRemaining));
2444
2445 if (cbToCopy != 0)
2446 {
2447 memcpy(pvData, pHostChCtx->pvDataReceived, cbToCopy);
2448
2449 if (cbRemaining != 0)
2450 {
2451 memmove(pHostChCtx->pvDataReceived, (uint8_t *)pHostChCtx->pvDataReceived + cbToCopy, cbRemaining);
2452 }
2453
2454 pHostChCtx->cbDataReceived = cbRemaining;
2455 }
2456
2457 pThis->tsmfUnlock();
2458
2459 *pcbRemaining = cbRemaining;
2460 *pcbReceived = cbToCopy;
2461 }
2462
2463 return rc;
2464}
2465
2466/* static */ DECLCALLBACK(int) ConsoleVRDPServer::tsmfHostChannelControl(void *pvChannel,
2467 uint32_t u32Code,
2468 const void *pvParm,
2469 uint32_t cbParm,
2470 const void *pvData,
2471 uint32_t cbData,
2472 uint32_t *pcbDataReturned)
2473{
2474 RT_NOREF(pvParm, cbParm, pvData, cbData);
2475 LogFlowFunc(("u32Code %u\n", u32Code));
2476
2477 if (!pvChannel)
2478 {
2479 /* Special case, the provider must answer rather than a channel instance. */
2480 if (u32Code == VBOX_HOST_CHANNEL_CTRL_EXISTS)
2481 {
2482 *pcbDataReturned = 0;
2483 return VINF_SUCCESS;
2484 }
2485
2486 return VERR_NOT_IMPLEMENTED;
2487 }
2488
2489 /* Channels do not support this. */
2490 return VERR_NOT_IMPLEMENTED;
2491}
2492
2493
2494void ConsoleVRDPServer::setupTSMF(void)
2495{
2496 if (m_interfaceTSMF.header.u64Size == 0)
2497 {
2498 return;
2499 }
2500
2501 /* Register with the host channel service. */
2502 VBOXHOSTCHANNELINTERFACE hostChannelInterface =
2503 {
2504 this,
2505 tsmfHostChannelAttach,
2506 tsmfHostChannelDetach,
2507 tsmfHostChannelSend,
2508 tsmfHostChannelRecv,
2509 tsmfHostChannelControl
2510 };
2511
2512 VBoxHostChannelHostRegister parms;
2513
2514 static char szProviderName[] = "/vrde/tsmf";
2515
2516 parms.name.type = VBOX_HGCM_SVC_PARM_PTR;
2517 parms.name.u.pointer.addr = &szProviderName[0];
2518 parms.name.u.pointer.size = sizeof(szProviderName);
2519
2520 parms.iface.type = VBOX_HGCM_SVC_PARM_PTR;
2521 parms.iface.u.pointer.addr = &hostChannelInterface;
2522 parms.iface.u.pointer.size = sizeof(hostChannelInterface);
2523
2524 VMMDev *pVMMDev = mConsole->i_getVMMDev();
2525
2526 if (!pVMMDev)
2527 {
2528 AssertMsgFailed(("setupTSMF no vmmdev\n"));
2529 return;
2530 }
2531
2532 int rc = pVMMDev->hgcmHostCall("VBoxHostChannel",
2533 VBOX_HOST_CHANNEL_HOST_FN_REGISTER,
2534 2,
2535 &parms.name);
2536
2537 if (!RT_SUCCESS(rc))
2538 {
2539 Log(("VBOX_HOST_CHANNEL_HOST_FN_REGISTER failed with %Rrc\n", rc));
2540 return;
2541 }
2542
2543 LogRel(("VRDE: Enabled TSMF channel.\n"));
2544
2545 return;
2546}
2547
2548/** @todo these defines must be in a header, which is used by guest component as well. */
2549#define VBOX_TSMF_HCH_CREATE_ACCEPTED (VBOX_HOST_CHANNEL_EVENT_USER + 0)
2550#define VBOX_TSMF_HCH_CREATE_DECLINED (VBOX_HOST_CHANNEL_EVENT_USER + 1)
2551#define VBOX_TSMF_HCH_DISCONNECTED (VBOX_HOST_CHANNEL_EVENT_USER + 2)
2552
2553/* static */ DECLCALLBACK(void) ConsoleVRDPServer::VRDETSMFCbNotify(void *pvContext,
2554 uint32_t u32Notification,
2555 void *pvChannel,
2556 const void *pvParm,
2557 uint32_t cbParm)
2558{
2559 RT_NOREF(cbParm);
2560 int rc = VINF_SUCCESS;
2561
2562 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvContext);
2563
2564 TSMFVRDPCTX *pVRDPCtx = (TSMFVRDPCTX *)pvChannel;
2565
2566 Assert(pVRDPCtx->pThis == pThis);
2567
2568 if (pVRDPCtx->pCallbacks == NULL)
2569 {
2570 LogFlowFunc(("tsmfHostChannel: Channel disconnected. Skipping.\n"));
2571 return;
2572 }
2573
2574 switch (u32Notification)
2575 {
2576 case VRDE_TSMF_N_CREATE_ACCEPTED:
2577 {
2578 VRDETSMFNOTIFYCREATEACCEPTED *p = (VRDETSMFNOTIFYCREATEACCEPTED *)pvParm;
2579 Assert(cbParm == sizeof(VRDETSMFNOTIFYCREATEACCEPTED));
2580
2581 LogFlowFunc(("tsmfHostChannel: VRDE_TSMF_N_CREATE_ACCEPTED(%p): p->u32ChannelHandle %d\n",
2582 pVRDPCtx, p->u32ChannelHandle));
2583
2584 pVRDPCtx->u32ChannelHandle = p->u32ChannelHandle;
2585
2586 pVRDPCtx->pCallbacks->HostChannelCallbackEvent(pVRDPCtx->pvCallbacks, pVRDPCtx->pHostChCtx,
2587 VBOX_TSMF_HCH_CREATE_ACCEPTED,
2588 NULL, 0);
2589 } break;
2590
2591 case VRDE_TSMF_N_CREATE_DECLINED:
2592 {
2593 LogFlowFunc(("tsmfHostChannel: VRDE_TSMF_N_CREATE_DECLINED(%p)\n", pVRDPCtx));
2594
2595 pVRDPCtx->pCallbacks->HostChannelCallbackEvent(pVRDPCtx->pvCallbacks, pVRDPCtx->pHostChCtx,
2596 VBOX_TSMF_HCH_CREATE_DECLINED,
2597 NULL, 0);
2598 } break;
2599
2600 case VRDE_TSMF_N_DATA:
2601 {
2602 /* Save the data in the intermediate buffer and send the event. */
2603 VRDETSMFNOTIFYDATA *p = (VRDETSMFNOTIFYDATA *)pvParm;
2604 Assert(cbParm == sizeof(VRDETSMFNOTIFYDATA));
2605
2606 LogFlowFunc(("tsmfHostChannel: VRDE_TSMF_N_DATA(%p): p->cbData %d\n", pVRDPCtx, p->cbData));
2607
2608 VBOXHOSTCHANNELEVENTRECV ev;
2609 ev.u32SizeAvailable = 0;
2610
2611 rc = pThis->tsmfLock();
2612
2613 if (RT_SUCCESS(rc))
2614 {
2615 TSMFHOSTCHCTX *pHostChCtx = pVRDPCtx->pHostChCtx;
2616
2617 if (pHostChCtx)
2618 {
2619 if (pHostChCtx->pvDataReceived)
2620 {
2621 uint32_t cbAlloc = p->cbData + pHostChCtx->cbDataReceived;
2622 pHostChCtx->pvDataReceived = RTMemRealloc(pHostChCtx->pvDataReceived, cbAlloc);
2623 memcpy((uint8_t *)pHostChCtx->pvDataReceived + pHostChCtx->cbDataReceived, p->pvData, p->cbData);
2624
2625 pHostChCtx->cbDataReceived += p->cbData;
2626 pHostChCtx->cbDataAllocated = cbAlloc;
2627 }
2628 else
2629 {
2630 pHostChCtx->pvDataReceived = RTMemAlloc(p->cbData);
2631 memcpy(pHostChCtx->pvDataReceived, p->pvData, p->cbData);
2632
2633 pHostChCtx->cbDataReceived = p->cbData;
2634 pHostChCtx->cbDataAllocated = p->cbData;
2635 }
2636
2637 ev.u32SizeAvailable = p->cbData;
2638 }
2639 else
2640 {
2641 LogFlowFunc(("tsmfHostChannel: VRDE_TSMF_N_DATA: no host channel. Skipping\n"));
2642 }
2643
2644 pThis->tsmfUnlock();
2645 }
2646
2647 pVRDPCtx->pCallbacks->HostChannelCallbackEvent(pVRDPCtx->pvCallbacks, pVRDPCtx->pHostChCtx,
2648 VBOX_HOST_CHANNEL_EVENT_RECV,
2649 &ev, sizeof(ev));
2650 } break;
2651
2652 case VRDE_TSMF_N_DISCONNECTED:
2653 {
2654 LogFlowFunc(("tsmfHostChannel: VRDE_TSMF_N_DISCONNECTED(%p)\n", pVRDPCtx));
2655
2656 pVRDPCtx->pCallbacks->HostChannelCallbackEvent(pVRDPCtx->pvCallbacks, pVRDPCtx->pHostChCtx,
2657 VBOX_TSMF_HCH_DISCONNECTED,
2658 NULL, 0);
2659
2660 /* The callback context will not be used anymore. */
2661 pVRDPCtx->pCallbacks->HostChannelCallbackDeleted(pVRDPCtx->pvCallbacks, pVRDPCtx->pHostChCtx);
2662 pVRDPCtx->pCallbacks = NULL;
2663 pVRDPCtx->pvCallbacks = NULL;
2664
2665 rc = pThis->tsmfLock();
2666 if (RT_SUCCESS(rc))
2667 {
2668 if (pVRDPCtx->pHostChCtx)
2669 {
2670 /* There is still a host channel context for this channel. */
2671 pVRDPCtx->pHostChCtx->pVRDPCtx = NULL;
2672 }
2673
2674 pThis->tsmfUnlock();
2675
2676 RT_ZERO(*pVRDPCtx);
2677 RTMemFree(pVRDPCtx);
2678 }
2679 } break;
2680
2681 default:
2682 {
2683 AssertFailed();
2684 } break;
2685 }
2686}
2687
2688/* static */ DECLCALLBACK(void) ConsoleVRDPServer::VRDECallbackVideoInNotify(void *pvCallback,
2689 uint32_t u32Id,
2690 const void *pvData,
2691 uint32_t cbData)
2692{
2693 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvCallback);
2694 if (pThis->mEmWebcam)
2695 {
2696 pThis->mEmWebcam->EmWebcamCbNotify(u32Id, pvData, cbData);
2697 }
2698}
2699
2700/* static */ DECLCALLBACK(void) ConsoleVRDPServer::VRDECallbackVideoInDeviceDesc(void *pvCallback,
2701 int rcRequest,
2702 void *pDeviceCtx,
2703 void *pvUser,
2704 const VRDEVIDEOINDEVICEDESC *pDeviceDesc,
2705 uint32_t cbDevice)
2706{
2707 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvCallback);
2708 if (pThis->mEmWebcam)
2709 {
2710 pThis->mEmWebcam->EmWebcamCbDeviceDesc(rcRequest, pDeviceCtx, pvUser, pDeviceDesc, cbDevice);
2711 }
2712}
2713
2714/* static */ DECLCALLBACK(void) ConsoleVRDPServer::VRDECallbackVideoInControl(void *pvCallback,
2715 int rcRequest,
2716 void *pDeviceCtx,
2717 void *pvUser,
2718 const VRDEVIDEOINCTRLHDR *pControl,
2719 uint32_t cbControl)
2720{
2721 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvCallback);
2722 if (pThis->mEmWebcam)
2723 {
2724 pThis->mEmWebcam->EmWebcamCbControl(rcRequest, pDeviceCtx, pvUser, pControl, cbControl);
2725 }
2726}
2727
2728/* static */ DECLCALLBACK(void) ConsoleVRDPServer::VRDECallbackVideoInFrame(void *pvCallback,
2729 int rcRequest,
2730 void *pDeviceCtx,
2731 const VRDEVIDEOINPAYLOADHDR *pFrame,
2732 uint32_t cbFrame)
2733{
2734 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvCallback);
2735 if (pThis->mEmWebcam)
2736 {
2737 pThis->mEmWebcam->EmWebcamCbFrame(rcRequest, pDeviceCtx, pFrame, cbFrame);
2738 }
2739}
2740
2741int ConsoleVRDPServer::VideoInDeviceAttach(const VRDEVIDEOINDEVICEHANDLE *pDeviceHandle, void *pvDeviceCtx)
2742{
2743 int rc;
2744
2745 if (mhServer && mpEntryPoints && m_interfaceVideoIn.VRDEVideoInDeviceAttach)
2746 {
2747 rc = m_interfaceVideoIn.VRDEVideoInDeviceAttach(mhServer, pDeviceHandle, pvDeviceCtx);
2748 }
2749 else
2750 {
2751 rc = VERR_NOT_SUPPORTED;
2752 }
2753
2754 return rc;
2755}
2756
2757int ConsoleVRDPServer::VideoInDeviceDetach(const VRDEVIDEOINDEVICEHANDLE *pDeviceHandle)
2758{
2759 int rc;
2760
2761 if (mhServer && mpEntryPoints && m_interfaceVideoIn.VRDEVideoInDeviceDetach)
2762 {
2763 rc = m_interfaceVideoIn.VRDEVideoInDeviceDetach(mhServer, pDeviceHandle);
2764 }
2765 else
2766 {
2767 rc = VERR_NOT_SUPPORTED;
2768 }
2769
2770 return rc;
2771}
2772
2773int ConsoleVRDPServer::VideoInGetDeviceDesc(void *pvUser, const VRDEVIDEOINDEVICEHANDLE *pDeviceHandle)
2774{
2775 int rc;
2776
2777 if (mhServer && mpEntryPoints && m_interfaceVideoIn.VRDEVideoInGetDeviceDesc)
2778 {
2779 rc = m_interfaceVideoIn.VRDEVideoInGetDeviceDesc(mhServer, pvUser, pDeviceHandle);
2780 }
2781 else
2782 {
2783 rc = VERR_NOT_SUPPORTED;
2784 }
2785
2786 return rc;
2787}
2788
2789int ConsoleVRDPServer::VideoInControl(void *pvUser, const VRDEVIDEOINDEVICEHANDLE *pDeviceHandle,
2790 const VRDEVIDEOINCTRLHDR *pReq, uint32_t cbReq)
2791{
2792 int rc;
2793
2794 if (mhServer && mpEntryPoints && m_interfaceVideoIn.VRDEVideoInControl)
2795 {
2796 rc = m_interfaceVideoIn.VRDEVideoInControl(mhServer, pvUser, pDeviceHandle, pReq, cbReq);
2797 }
2798 else
2799 {
2800 rc = VERR_NOT_SUPPORTED;
2801 }
2802
2803 return rc;
2804}
2805
2806
2807/* static */ DECLCALLBACK(void) ConsoleVRDPServer::VRDECallbackInputSetup(void *pvCallback,
2808 int rcRequest,
2809 uint32_t u32Method,
2810 const void *pvResult,
2811 uint32_t cbResult)
2812{
2813 NOREF(pvCallback);
2814 NOREF(rcRequest);
2815 NOREF(u32Method);
2816 NOREF(pvResult);
2817 NOREF(cbResult);
2818}
2819
2820/* static */ DECLCALLBACK(void) ConsoleVRDPServer::VRDECallbackInputEvent(void *pvCallback,
2821 uint32_t u32Method,
2822 const void *pvEvent,
2823 uint32_t cbEvent)
2824{
2825 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvCallback);
2826
2827 if (u32Method == VRDE_INPUT_METHOD_TOUCH)
2828 {
2829 if (cbEvent >= sizeof(VRDEINPUTHEADER))
2830 {
2831 VRDEINPUTHEADER *pHeader = (VRDEINPUTHEADER *)pvEvent;
2832
2833 if (pHeader->u16EventId == VRDEINPUT_EVENTID_TOUCH)
2834 {
2835 IMouse *pMouse = pThis->mConsole->i_getMouse();
2836
2837 VRDEINPUT_TOUCH_EVENT_PDU *p = (VRDEINPUT_TOUCH_EVENT_PDU *)pHeader;
2838
2839 uint16_t iFrame;
2840 for (iFrame = 0; iFrame < p->u16FrameCount; iFrame++)
2841 {
2842 VRDEINPUT_TOUCH_FRAME *pFrame = &p->aFrames[iFrame];
2843
2844 com::SafeArray<LONG64> aContacts(pFrame->u16ContactCount);
2845
2846 uint16_t iContact;
2847 for (iContact = 0; iContact < pFrame->u16ContactCount; iContact++)
2848 {
2849 VRDEINPUT_CONTACT_DATA *pContact = &pFrame->aContacts[iContact];
2850
2851 int16_t x = (int16_t)(pContact->i32X + 1);
2852 int16_t y = (int16_t)(pContact->i32Y + 1);
2853 uint8_t contactId = pContact->u8ContactId;
2854 uint8_t contactState = TouchContactState_None;
2855
2856 if (pContact->u32ContactFlags & VRDEINPUT_CONTACT_FLAG_INRANGE)
2857 {
2858 contactState |= TouchContactState_InRange;
2859 }
2860 if (pContact->u32ContactFlags & VRDEINPUT_CONTACT_FLAG_INCONTACT)
2861 {
2862 contactState |= TouchContactState_InContact;
2863 }
2864
2865 aContacts[iContact] = RT_MAKE_U64_FROM_U16((uint16_t)x,
2866 (uint16_t)y,
2867 RT_MAKE_U16(contactId, contactState),
2868 0);
2869 }
2870
2871 if (pFrame->u64FrameOffset == 0)
2872 {
2873 pThis->mu64TouchInputTimestampMCS = 0;
2874 }
2875 else
2876 {
2877 pThis->mu64TouchInputTimestampMCS += pFrame->u64FrameOffset;
2878 }
2879
2880 pMouse->PutEventMultiTouch(pFrame->u16ContactCount,
2881 ComSafeArrayAsInParam(aContacts),
2882 (ULONG)(pThis->mu64TouchInputTimestampMCS / 1000)); /* Micro->milliseconds. */
2883 }
2884 }
2885 else if (pHeader->u16EventId == VRDEINPUT_EVENTID_DISMISS_HOVERING_CONTACT)
2886 {
2887 /** @todo */
2888 }
2889 else
2890 {
2891 AssertMsgFailed(("EventId %d\n", pHeader->u16EventId));
2892 }
2893 }
2894 }
2895}
2896
2897
2898void ConsoleVRDPServer::EnableConnections(void)
2899{
2900 if (mpEntryPoints && mhServer)
2901 {
2902 mpEntryPoints->VRDEEnableConnections(mhServer, true);
2903
2904 /* Setup the generic TSMF channel. */
2905 setupTSMF();
2906 }
2907}
2908
2909void ConsoleVRDPServer::DisconnectClient(uint32_t u32ClientId, bool fReconnect)
2910{
2911 if (mpEntryPoints && mhServer)
2912 {
2913 mpEntryPoints->VRDEDisconnect(mhServer, u32ClientId, fReconnect);
2914 }
2915}
2916
2917int ConsoleVRDPServer::MousePointer(BOOL alpha,
2918 ULONG xHot,
2919 ULONG yHot,
2920 ULONG width,
2921 ULONG height,
2922 const uint8_t *pu8Shape)
2923{
2924 int rc = VINF_SUCCESS;
2925
2926 if (mhServer && mpEntryPoints && m_interfaceMousePtr.VRDEMousePtr)
2927 {
2928 size_t cbMask = (((width + 7) / 8) * height + 3) & ~3;
2929 size_t cbData = width * height * 4;
2930
2931 size_t cbDstMask = alpha? 0: cbMask;
2932
2933 size_t cbPointer = sizeof(VRDEMOUSEPTRDATA) + cbDstMask + cbData;
2934 uint8_t *pu8Pointer = (uint8_t *)RTMemAlloc(cbPointer);
2935 if (pu8Pointer != NULL)
2936 {
2937 VRDEMOUSEPTRDATA *pPointer = (VRDEMOUSEPTRDATA *)pu8Pointer;
2938
2939 pPointer->u16HotX = (uint16_t)xHot;
2940 pPointer->u16HotY = (uint16_t)yHot;
2941 pPointer->u16Width = (uint16_t)width;
2942 pPointer->u16Height = (uint16_t)height;
2943 pPointer->u16MaskLen = (uint16_t)cbDstMask;
2944 pPointer->u32DataLen = (uint32_t)cbData;
2945
2946 /* AND mask. */
2947 uint8_t *pu8Mask = pu8Pointer + sizeof(VRDEMOUSEPTRDATA);
2948 if (cbDstMask)
2949 {
2950 memcpy(pu8Mask, pu8Shape, cbDstMask);
2951 }
2952
2953 /* XOR mask */
2954 uint8_t *pu8Data = pu8Mask + pPointer->u16MaskLen;
2955 memcpy(pu8Data, pu8Shape + cbMask, cbData);
2956
2957 m_interfaceMousePtr.VRDEMousePtr(mhServer, pPointer);
2958
2959 RTMemFree(pu8Pointer);
2960 }
2961 else
2962 {
2963 rc = VERR_NO_MEMORY;
2964 }
2965 }
2966 else
2967 {
2968 rc = VERR_NOT_SUPPORTED;
2969 }
2970
2971 return rc;
2972}
2973
2974void ConsoleVRDPServer::MousePointerUpdate(const VRDECOLORPOINTER *pPointer)
2975{
2976 if (mpEntryPoints && mhServer)
2977 {
2978 mpEntryPoints->VRDEColorPointer(mhServer, pPointer);
2979 }
2980}
2981
2982void ConsoleVRDPServer::MousePointerHide(void)
2983{
2984 if (mpEntryPoints && mhServer)
2985 {
2986 mpEntryPoints->VRDEHidePointer(mhServer);
2987 }
2988}
2989
2990void ConsoleVRDPServer::Stop(void)
2991{
2992 Assert(VALID_PTR(this)); /** @todo r=bird: there are(/was) some odd cases where this buster was invalid on
2993 * linux. Just remove this when it's 100% sure that problem has been fixed. */
2994
2995#ifdef VBOX_WITH_USB
2996 remoteUSBThreadStop();
2997#endif /* VBOX_WITH_USB */
2998
2999 if (mhServer)
3000 {
3001 HVRDESERVER hServer = mhServer;
3002
3003 /* Reset the handle to avoid further calls to the server. */
3004 mhServer = 0;
3005
3006 /* Workaround for VM process hangs on termination.
3007 *
3008 * Make sure that the server is not currently processing a resize.
3009 * mhServer 0 will not allow to enter the server again.
3010 * Wait until any current resize returns from the server.
3011 */
3012 if (mcInResize)
3013 {
3014 LogRel(("VRDP: waiting for resize %d\n", mcInResize));
3015
3016 int i = 0;
3017 while (mcInResize && ++i < 100)
3018 {
3019 RTThreadSleep(10);
3020 }
3021 }
3022
3023 if (mpEntryPoints && hServer)
3024 {
3025 mpEntryPoints->VRDEDestroy(hServer);
3026 }
3027 }
3028
3029#ifndef VBOX_WITH_VRDEAUTH_IN_VBOXSVC
3030 AuthLibUnload(&mAuthLibCtx);
3031#endif
3032}
3033
3034/* Worker thread for Remote USB. The thread polls the clients for
3035 * the list of attached USB devices.
3036 * The thread is also responsible for attaching/detaching devices
3037 * to/from the VM.
3038 *
3039 * It is expected that attaching/detaching is not a frequent operation.
3040 *
3041 * The thread is always running when the VRDP server is active.
3042 *
3043 * The thread scans backends and requests the device list every 2 seconds.
3044 *
3045 * When device list is available, the thread calls the Console to process it.
3046 *
3047 */
3048#define VRDP_DEVICE_LIST_PERIOD_MS (2000)
3049
3050#ifdef VBOX_WITH_USB
3051static DECLCALLBACK(int) threadRemoteUSB(RTTHREAD self, void *pvUser)
3052{
3053 ConsoleVRDPServer *pOwner = (ConsoleVRDPServer *)pvUser;
3054
3055 LogFlow(("Console::threadRemoteUSB: start. owner = %p.\n", pOwner));
3056
3057 pOwner->notifyRemoteUSBThreadRunning(self);
3058
3059 while (pOwner->isRemoteUSBThreadRunning())
3060 {
3061 RemoteUSBBackend *pRemoteUSBBackend = NULL;
3062
3063 while ((pRemoteUSBBackend = pOwner->usbBackendGetNext(pRemoteUSBBackend)) != NULL)
3064 {
3065 pRemoteUSBBackend->PollRemoteDevices();
3066 }
3067
3068 pOwner->waitRemoteUSBThreadEvent(VRDP_DEVICE_LIST_PERIOD_MS);
3069
3070 LogFlow(("Console::threadRemoteUSB: iteration. owner = %p.\n", pOwner));
3071 }
3072
3073 return VINF_SUCCESS;
3074}
3075
3076void ConsoleVRDPServer::notifyRemoteUSBThreadRunning(RTTHREAD thread)
3077{
3078 mUSBBackends.thread = thread;
3079 mUSBBackends.fThreadRunning = true;
3080 int rc = RTThreadUserSignal(thread);
3081 AssertRC(rc);
3082}
3083
3084bool ConsoleVRDPServer::isRemoteUSBThreadRunning(void)
3085{
3086 return mUSBBackends.fThreadRunning;
3087}
3088
3089void ConsoleVRDPServer::waitRemoteUSBThreadEvent(RTMSINTERVAL cMillies)
3090{
3091 int rc = RTSemEventWait(mUSBBackends.event, cMillies);
3092 Assert(RT_SUCCESS(rc) || rc == VERR_TIMEOUT);
3093 NOREF(rc);
3094}
3095
3096void ConsoleVRDPServer::remoteUSBThreadStart(void)
3097{
3098 int rc = RTSemEventCreate(&mUSBBackends.event);
3099
3100 if (RT_FAILURE(rc))
3101 {
3102 AssertFailed();
3103 mUSBBackends.event = 0;
3104 }
3105
3106 if (RT_SUCCESS(rc))
3107 {
3108 rc = RTThreadCreate(&mUSBBackends.thread, threadRemoteUSB, this, 65536,
3109 RTTHREADTYPE_VRDP_IO, RTTHREADFLAGS_WAITABLE, "remote usb");
3110 }
3111
3112 if (RT_FAILURE(rc))
3113 {
3114 LogRel(("Warning: could not start the remote USB thread, rc = %Rrc!!!\n", rc));
3115 mUSBBackends.thread = NIL_RTTHREAD;
3116 }
3117 else
3118 {
3119 /* Wait until the thread is ready. */
3120 rc = RTThreadUserWait(mUSBBackends.thread, 60000);
3121 AssertRC(rc);
3122 Assert (mUSBBackends.fThreadRunning || RT_FAILURE(rc));
3123 }
3124}
3125
3126void ConsoleVRDPServer::remoteUSBThreadStop(void)
3127{
3128 mUSBBackends.fThreadRunning = false;
3129
3130 if (mUSBBackends.thread != NIL_RTTHREAD)
3131 {
3132 Assert (mUSBBackends.event != 0);
3133
3134 RTSemEventSignal(mUSBBackends.event);
3135
3136 int rc = RTThreadWait(mUSBBackends.thread, 60000, NULL);
3137 AssertRC(rc);
3138
3139 mUSBBackends.thread = NIL_RTTHREAD;
3140 }
3141
3142 if (mUSBBackends.event)
3143 {
3144 RTSemEventDestroy(mUSBBackends.event);
3145 mUSBBackends.event = 0;
3146 }
3147}
3148#endif /* VBOX_WITH_USB */
3149
3150AuthResult ConsoleVRDPServer::Authenticate(const Guid &uuid, AuthGuestJudgement guestJudgement,
3151 const char *pszUser, const char *pszPassword, const char *pszDomain,
3152 uint32_t u32ClientId)
3153{
3154 LogFlowFunc(("uuid = %RTuuid, guestJudgement = %d, pszUser = %s, pszPassword = %s, pszDomain = %s, u32ClientId = %d\n",
3155 uuid.raw(), guestJudgement, pszUser, pszPassword, pszDomain, u32ClientId));
3156
3157 AuthResult result = AuthResultAccessDenied;
3158
3159#ifdef VBOX_WITH_VRDEAUTH_IN_VBOXSVC
3160 try
3161 {
3162 /* Init auth parameters. Order is important. */
3163 SafeArray<BSTR> authParams;
3164 Bstr("VRDEAUTH" ).detachTo(authParams.appendedRaw());
3165 Bstr(uuid.toUtf16() ).detachTo(authParams.appendedRaw());
3166 BstrFmt("%u", guestJudgement).detachTo(authParams.appendedRaw());
3167 Bstr(pszUser ).detachTo(authParams.appendedRaw());
3168 Bstr(pszPassword ).detachTo(authParams.appendedRaw());
3169 Bstr(pszDomain ).detachTo(authParams.appendedRaw());
3170 BstrFmt("%u", u32ClientId).detachTo(authParams.appendedRaw());
3171
3172 Bstr authResult;
3173 HRESULT hr = mConsole->mControl->AuthenticateExternal(ComSafeArrayAsInParam(authParams),
3174 authResult.asOutParam());
3175 LogFlowFunc(("%Rhrc [%ls]\n", hr, authResult.raw()));
3176
3177 size_t cbPassword = RTUtf16Len((PRTUTF16)authParams[4]) * sizeof(RTUTF16);
3178 if (cbPassword)
3179 RTMemWipeThoroughly(authParams[4], cbPassword, 10 /* cPasses */);
3180
3181 if (SUCCEEDED(hr) && authResult == "granted")
3182 result = AuthResultAccessGranted;
3183 }
3184 catch (std::bad_alloc)
3185 {
3186 }
3187#else
3188 /*
3189 * Called only from VRDP input thread. So thread safety is not required.
3190 */
3191
3192 if (!mAuthLibCtx.hAuthLibrary)
3193 {
3194 /* Load the external authentication library. */
3195 Bstr authLibrary;
3196 mConsole->i_getVRDEServer()->COMGETTER(AuthLibrary)(authLibrary.asOutParam());
3197
3198 Utf8Str filename = authLibrary;
3199
3200 int rc = AuthLibLoad(&mAuthLibCtx, filename.c_str());
3201
3202 if (RT_FAILURE(rc))
3203 {
3204 mConsole->setError(E_FAIL,
3205 mConsole->tr("Could not load the external authentication library '%s' (%Rrc)"),
3206 filename.c_str(),
3207 rc);
3208
3209 return AuthResultAccessDenied;
3210 }
3211 }
3212
3213 result = AuthLibAuthenticate(&mAuthLibCtx,
3214 uuid.raw(), guestJudgement,
3215 pszUser, pszPassword, pszDomain,
3216 u32ClientId);
3217#endif /* !VBOX_WITH_VRDEAUTH_IN_VBOXSVC */
3218
3219 switch (result)
3220 {
3221 case AuthResultAccessDenied:
3222 LogRel(("AUTH: external authentication module returned 'access denied'\n"));
3223 break;
3224 case AuthResultAccessGranted:
3225 LogRel(("AUTH: external authentication module returned 'access granted'\n"));
3226 break;
3227 case AuthResultDelegateToGuest:
3228 LogRel(("AUTH: external authentication module returned 'delegate request to guest'\n"));
3229 break;
3230 default:
3231 LogRel(("AUTH: external authentication module returned incorrect return code %d\n", result));
3232 result = AuthResultAccessDenied;
3233 }
3234
3235 LogFlowFunc(("result = %d\n", result));
3236
3237 return result;
3238}
3239
3240void ConsoleVRDPServer::AuthDisconnect(const Guid &uuid, uint32_t u32ClientId)
3241{
3242 LogFlow(("ConsoleVRDPServer::AuthDisconnect: uuid = %RTuuid, u32ClientId = %d\n",
3243 uuid.raw(), u32ClientId));
3244
3245#ifdef VBOX_WITH_VRDEAUTH_IN_VBOXSVC
3246 try
3247 {
3248 /* Init auth parameters. Order is important. */
3249 SafeArray<BSTR> authParams;
3250 Bstr("VRDEAUTHDISCONNECT").detachTo(authParams.appendedRaw());
3251 Bstr(uuid.toUtf16() ).detachTo(authParams.appendedRaw());
3252 BstrFmt("%u", u32ClientId).detachTo(authParams.appendedRaw());
3253
3254 Bstr authResult;
3255 HRESULT hr = mConsole->mControl->AuthenticateExternal(ComSafeArrayAsInParam(authParams),
3256 authResult.asOutParam());
3257 LogFlowFunc(("%Rhrc [%ls]\n", hr, authResult.raw())); NOREF(hr);
3258 }
3259 catch (std::bad_alloc)
3260 {
3261 }
3262#else
3263 AuthLibDisconnect(&mAuthLibCtx, uuid.raw(), u32ClientId);
3264#endif /* !VBOX_WITH_VRDEAUTH_IN_VBOXSVC */
3265}
3266
3267int ConsoleVRDPServer::lockConsoleVRDPServer(void)
3268{
3269 int rc = RTCritSectEnter(&mCritSect);
3270 AssertRC(rc);
3271 return rc;
3272}
3273
3274void ConsoleVRDPServer::unlockConsoleVRDPServer(void)
3275{
3276 RTCritSectLeave(&mCritSect);
3277}
3278
3279DECLCALLBACK(int) ConsoleVRDPServer::ClipboardCallback(void *pvCallback,
3280 uint32_t u32ClientId,
3281 uint32_t u32Function,
3282 uint32_t u32Format,
3283 const void *pvData,
3284 uint32_t cbData)
3285{
3286 LogFlowFunc(("pvCallback = %p, u32ClientId = %d, u32Function = %d, u32Format = 0x%08X, pvData = %p, cbData = %d\n",
3287 pvCallback, u32ClientId, u32Function, u32Format, pvData, cbData));
3288
3289 int rc = VINF_SUCCESS;
3290
3291 ConsoleVRDPServer *pServer = static_cast <ConsoleVRDPServer *>(pvCallback);
3292
3293 NOREF(u32ClientId);
3294
3295 switch (u32Function)
3296 {
3297 case VRDE_CLIPBOARD_FUNCTION_FORMAT_ANNOUNCE:
3298 {
3299 if (pServer->mpfnClipboardCallback)
3300 {
3301 pServer->mpfnClipboardCallback(VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE,
3302 u32Format,
3303 (void *)pvData,
3304 cbData);
3305 }
3306 } break;
3307
3308 case VRDE_CLIPBOARD_FUNCTION_DATA_READ:
3309 {
3310 if (pServer->mpfnClipboardCallback)
3311 {
3312 pServer->mpfnClipboardCallback(VBOX_CLIPBOARD_EXT_FN_DATA_READ,
3313 u32Format,
3314 (void *)pvData,
3315 cbData);
3316 }
3317 } break;
3318
3319 default:
3320 rc = VERR_NOT_SUPPORTED;
3321 }
3322
3323 return rc;
3324}
3325
3326DECLCALLBACK(int) ConsoleVRDPServer::ClipboardServiceExtension(void *pvExtension,
3327 uint32_t u32Function,
3328 void *pvParms,
3329 uint32_t cbParms)
3330{
3331 RT_NOREF(cbParms);
3332 LogFlowFunc(("pvExtension = %p, u32Function = %d, pvParms = %p, cbParms = %d\n",
3333 pvExtension, u32Function, pvParms, cbParms));
3334
3335 int rc = VINF_SUCCESS;
3336
3337 ConsoleVRDPServer *pServer = static_cast <ConsoleVRDPServer *>(pvExtension);
3338
3339 VBOXCLIPBOARDEXTPARMS *pParms = (VBOXCLIPBOARDEXTPARMS *)pvParms;
3340
3341 switch (u32Function)
3342 {
3343 case VBOX_CLIPBOARD_EXT_FN_SET_CALLBACK:
3344 {
3345 pServer->mpfnClipboardCallback = pParms->u.pfnCallback;
3346 } break;
3347
3348 case VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE:
3349 {
3350 /* The guest announces clipboard formats. This must be delivered to all clients. */
3351 if (mpEntryPoints && pServer->mhServer)
3352 {
3353 mpEntryPoints->VRDEClipboard(pServer->mhServer,
3354 VRDE_CLIPBOARD_FUNCTION_FORMAT_ANNOUNCE,
3355 pParms->u32Format,
3356 NULL,
3357 0,
3358 NULL);
3359 }
3360 } break;
3361
3362 case VBOX_CLIPBOARD_EXT_FN_DATA_READ:
3363 {
3364 /* The clipboard service expects that the pvData buffer will be filled
3365 * with clipboard data. The server returns the data from the client that
3366 * announced the requested format most recently.
3367 */
3368 if (mpEntryPoints && pServer->mhServer)
3369 {
3370 mpEntryPoints->VRDEClipboard(pServer->mhServer,
3371 VRDE_CLIPBOARD_FUNCTION_DATA_READ,
3372 pParms->u32Format,
3373 pParms->u.pvData,
3374 pParms->cbData,
3375 &pParms->cbData);
3376 }
3377 } break;
3378
3379 case VBOX_CLIPBOARD_EXT_FN_DATA_WRITE:
3380 {
3381 if (mpEntryPoints && pServer->mhServer)
3382 {
3383 mpEntryPoints->VRDEClipboard(pServer->mhServer,
3384 VRDE_CLIPBOARD_FUNCTION_DATA_WRITE,
3385 pParms->u32Format,
3386 pParms->u.pvData,
3387 pParms->cbData,
3388 NULL);
3389 }
3390 } break;
3391
3392 default:
3393 rc = VERR_NOT_SUPPORTED;
3394 }
3395
3396 return rc;
3397}
3398
3399void ConsoleVRDPServer::ClipboardCreate(uint32_t u32ClientId)
3400{
3401 RT_NOREF(u32ClientId);
3402 int rc = lockConsoleVRDPServer();
3403
3404 if (RT_SUCCESS(rc))
3405 {
3406 if (mcClipboardRefs == 0)
3407 {
3408 rc = HGCMHostRegisterServiceExtension(&mhClipboard, "VBoxSharedClipboard", ClipboardServiceExtension, this);
3409
3410 if (RT_SUCCESS(rc))
3411 {
3412 mcClipboardRefs++;
3413 }
3414 }
3415
3416 unlockConsoleVRDPServer();
3417 }
3418}
3419
3420void ConsoleVRDPServer::ClipboardDelete(uint32_t u32ClientId)
3421{
3422 RT_NOREF(u32ClientId);
3423 int rc = lockConsoleVRDPServer();
3424
3425 if (RT_SUCCESS(rc))
3426 {
3427 mcClipboardRefs--;
3428
3429 if (mcClipboardRefs == 0)
3430 {
3431 HGCMHostUnregisterServiceExtension(mhClipboard);
3432 }
3433
3434 unlockConsoleVRDPServer();
3435 }
3436}
3437
3438/* That is called on INPUT thread of the VRDP server.
3439 * The ConsoleVRDPServer keeps a list of created backend instances.
3440 */
3441void ConsoleVRDPServer::USBBackendCreate(uint32_t u32ClientId, void **ppvIntercept)
3442{
3443#ifdef VBOX_WITH_USB
3444 LogFlow(("ConsoleVRDPServer::USBBackendCreate: u32ClientId = %d\n", u32ClientId));
3445
3446 /* Create a new instance of the USB backend for the new client. */
3447 RemoteUSBBackend *pRemoteUSBBackend = new RemoteUSBBackend(mConsole, this, u32ClientId);
3448
3449 if (pRemoteUSBBackend)
3450 {
3451 pRemoteUSBBackend->AddRef(); /* 'Release' called in USBBackendDelete. */
3452
3453 /* Append the new instance in the list. */
3454 int rc = lockConsoleVRDPServer();
3455
3456 if (RT_SUCCESS(rc))
3457 {
3458 pRemoteUSBBackend->pNext = mUSBBackends.pHead;
3459 if (mUSBBackends.pHead)
3460 {
3461 mUSBBackends.pHead->pPrev = pRemoteUSBBackend;
3462 }
3463 else
3464 {
3465 mUSBBackends.pTail = pRemoteUSBBackend;
3466 }
3467
3468 mUSBBackends.pHead = pRemoteUSBBackend;
3469
3470 unlockConsoleVRDPServer();
3471
3472 if (ppvIntercept)
3473 {
3474 *ppvIntercept = pRemoteUSBBackend;
3475 }
3476 }
3477
3478 if (RT_FAILURE(rc))
3479 {
3480 pRemoteUSBBackend->Release();
3481 }
3482 }
3483#endif /* VBOX_WITH_USB */
3484}
3485
3486void ConsoleVRDPServer::USBBackendDelete(uint32_t u32ClientId)
3487{
3488#ifdef VBOX_WITH_USB
3489 LogFlow(("ConsoleVRDPServer::USBBackendDelete: u32ClientId = %d\n", u32ClientId));
3490
3491 RemoteUSBBackend *pRemoteUSBBackend = NULL;
3492
3493 /* Find the instance. */
3494 int rc = lockConsoleVRDPServer();
3495
3496 if (RT_SUCCESS(rc))
3497 {
3498 pRemoteUSBBackend = usbBackendFind(u32ClientId);
3499
3500 if (pRemoteUSBBackend)
3501 {
3502 /* Notify that it will be deleted. */
3503 pRemoteUSBBackend->NotifyDelete();
3504 }
3505
3506 unlockConsoleVRDPServer();
3507 }
3508
3509 if (pRemoteUSBBackend)
3510 {
3511 /* Here the instance has been excluded from the list and can be dereferenced. */
3512 pRemoteUSBBackend->Release();
3513 }
3514#endif
3515}
3516
3517void *ConsoleVRDPServer::USBBackendRequestPointer(uint32_t u32ClientId, const Guid *pGuid)
3518{
3519#ifdef VBOX_WITH_USB
3520 RemoteUSBBackend *pRemoteUSBBackend = NULL;
3521
3522 /* Find the instance. */
3523 int rc = lockConsoleVRDPServer();
3524
3525 if (RT_SUCCESS(rc))
3526 {
3527 pRemoteUSBBackend = usbBackendFind(u32ClientId);
3528
3529 if (pRemoteUSBBackend)
3530 {
3531 /* Inform the backend instance that it is referenced by the Guid. */
3532 bool fAdded = pRemoteUSBBackend->addUUID(pGuid);
3533
3534 if (fAdded)
3535 {
3536 /* Reference the instance because its pointer is being taken. */
3537 pRemoteUSBBackend->AddRef(); /* 'Release' is called in USBBackendReleasePointer. */
3538 }
3539 else
3540 {
3541 pRemoteUSBBackend = NULL;
3542 }
3543 }
3544
3545 unlockConsoleVRDPServer();
3546 }
3547
3548 if (pRemoteUSBBackend)
3549 {
3550 return pRemoteUSBBackend->GetBackendCallbackPointer();
3551 }
3552
3553#endif
3554 return NULL;
3555}
3556
3557void ConsoleVRDPServer::USBBackendReleasePointer(const Guid *pGuid)
3558{
3559#ifdef VBOX_WITH_USB
3560 RemoteUSBBackend *pRemoteUSBBackend = NULL;
3561
3562 /* Find the instance. */
3563 int rc = lockConsoleVRDPServer();
3564
3565 if (RT_SUCCESS(rc))
3566 {
3567 pRemoteUSBBackend = usbBackendFindByUUID(pGuid);
3568
3569 if (pRemoteUSBBackend)
3570 {
3571 pRemoteUSBBackend->removeUUID(pGuid);
3572 }
3573
3574 unlockConsoleVRDPServer();
3575
3576 if (pRemoteUSBBackend)
3577 {
3578 pRemoteUSBBackend->Release();
3579 }
3580 }
3581#endif
3582}
3583
3584RemoteUSBBackend *ConsoleVRDPServer::usbBackendGetNext(RemoteUSBBackend *pRemoteUSBBackend)
3585{
3586 LogFlow(("ConsoleVRDPServer::usbBackendGetNext: pBackend = %p\n", pRemoteUSBBackend));
3587
3588 RemoteUSBBackend *pNextRemoteUSBBackend = NULL;
3589#ifdef VBOX_WITH_USB
3590
3591 int rc = lockConsoleVRDPServer();
3592
3593 if (RT_SUCCESS(rc))
3594 {
3595 if (pRemoteUSBBackend == NULL)
3596 {
3597 /* The first backend in the list is requested. */
3598 pNextRemoteUSBBackend = mUSBBackends.pHead;
3599 }
3600 else
3601 {
3602 /* Get pointer to the next backend. */
3603 pNextRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
3604 }
3605
3606 if (pNextRemoteUSBBackend)
3607 {
3608 pNextRemoteUSBBackend->AddRef();
3609 }
3610
3611 unlockConsoleVRDPServer();
3612
3613 if (pRemoteUSBBackend)
3614 {
3615 pRemoteUSBBackend->Release();
3616 }
3617 }
3618#endif
3619
3620 return pNextRemoteUSBBackend;
3621}
3622
3623#ifdef VBOX_WITH_USB
3624/* Internal method. Called under the ConsoleVRDPServerLock. */
3625RemoteUSBBackend *ConsoleVRDPServer::usbBackendFind(uint32_t u32ClientId)
3626{
3627 RemoteUSBBackend *pRemoteUSBBackend = mUSBBackends.pHead;
3628
3629 while (pRemoteUSBBackend)
3630 {
3631 if (pRemoteUSBBackend->ClientId() == u32ClientId)
3632 {
3633 break;
3634 }
3635
3636 pRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
3637 }
3638
3639 return pRemoteUSBBackend;
3640}
3641
3642/* Internal method. Called under the ConsoleVRDPServerLock. */
3643RemoteUSBBackend *ConsoleVRDPServer::usbBackendFindByUUID(const Guid *pGuid)
3644{
3645 RemoteUSBBackend *pRemoteUSBBackend = mUSBBackends.pHead;
3646
3647 while (pRemoteUSBBackend)
3648 {
3649 if (pRemoteUSBBackend->findUUID(pGuid))
3650 {
3651 break;
3652 }
3653
3654 pRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
3655 }
3656
3657 return pRemoteUSBBackend;
3658}
3659#endif
3660
3661/* Internal method. Called by the backend destructor. */
3662void ConsoleVRDPServer::usbBackendRemoveFromList(RemoteUSBBackend *pRemoteUSBBackend)
3663{
3664#ifdef VBOX_WITH_USB
3665 int rc = lockConsoleVRDPServer();
3666 AssertRC(rc);
3667
3668 /* Exclude the found instance from the list. */
3669 if (pRemoteUSBBackend->pNext)
3670 {
3671 pRemoteUSBBackend->pNext->pPrev = pRemoteUSBBackend->pPrev;
3672 }
3673 else
3674 {
3675 mUSBBackends.pTail = (RemoteUSBBackend *)pRemoteUSBBackend->pPrev;
3676 }
3677
3678 if (pRemoteUSBBackend->pPrev)
3679 {
3680 pRemoteUSBBackend->pPrev->pNext = pRemoteUSBBackend->pNext;
3681 }
3682 else
3683 {
3684 mUSBBackends.pHead = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
3685 }
3686
3687 pRemoteUSBBackend->pNext = pRemoteUSBBackend->pPrev = NULL;
3688
3689 unlockConsoleVRDPServer();
3690#endif
3691}
3692
3693
3694void ConsoleVRDPServer::SendUpdate(unsigned uScreenId, void *pvUpdate, uint32_t cbUpdate) const
3695{
3696 if (mpEntryPoints && mhServer)
3697 {
3698 mpEntryPoints->VRDEUpdate(mhServer, uScreenId, pvUpdate, cbUpdate);
3699 }
3700}
3701
3702void ConsoleVRDPServer::SendResize(void)
3703{
3704 if (mpEntryPoints && mhServer)
3705 {
3706 ++mcInResize;
3707 mpEntryPoints->VRDEResize(mhServer);
3708 --mcInResize;
3709 }
3710}
3711
3712void ConsoleVRDPServer::SendUpdateBitmap(unsigned uScreenId, uint32_t x, uint32_t y, uint32_t w, uint32_t h) const
3713{
3714 VRDEORDERHDR update;
3715 update.x = (uint16_t)x;
3716 update.y = (uint16_t)y;
3717 update.w = (uint16_t)w;
3718 update.h = (uint16_t)h;
3719 if (mpEntryPoints && mhServer)
3720 {
3721 mpEntryPoints->VRDEUpdate(mhServer, uScreenId, &update, sizeof(update));
3722 }
3723}
3724
3725void ConsoleVRDPServer::SendAudioSamples(void *pvSamples, uint32_t cSamples, VRDEAUDIOFORMAT format) const
3726{
3727 if (mpEntryPoints && mhServer)
3728 {
3729 mpEntryPoints->VRDEAudioSamples(mhServer, pvSamples, cSamples, format);
3730 }
3731}
3732
3733void ConsoleVRDPServer::SendAudioVolume(uint16_t left, uint16_t right) const
3734{
3735 if (mpEntryPoints && mhServer)
3736 {
3737 mpEntryPoints->VRDEAudioVolume(mhServer, left, right);
3738 }
3739}
3740
3741void ConsoleVRDPServer::SendUSBRequest(uint32_t u32ClientId, void *pvParms, uint32_t cbParms) const
3742{
3743 if (mpEntryPoints && mhServer)
3744 {
3745 mpEntryPoints->VRDEUSBRequest(mhServer, u32ClientId, pvParms, cbParms);
3746 }
3747}
3748
3749int ConsoleVRDPServer::SendAudioInputBegin(void **ppvUserCtx,
3750 void *pvContext,
3751 uint32_t cSamples,
3752 uint32_t iSampleHz,
3753 uint32_t cChannels,
3754 uint32_t cBits)
3755{
3756 if ( mhServer
3757 && mpEntryPoints && mpEntryPoints->VRDEAudioInOpen)
3758 {
3759 uint32_t u32ClientId = ASMAtomicReadU32(&mu32AudioInputClientId);
3760 if (u32ClientId != 0) /* 0 would mean broadcast to all clients. */
3761 {
3762 VRDEAUDIOFORMAT audioFormat = VRDE_AUDIO_FMT_MAKE(iSampleHz, cChannels, cBits, 0);
3763 mpEntryPoints->VRDEAudioInOpen(mhServer,
3764 pvContext,
3765 u32ClientId,
3766 audioFormat,
3767 cSamples);
3768 if (ppvUserCtx)
3769 *ppvUserCtx = NULL; /* This is the ConsoleVRDPServer context.
3770 * Currently not used because only one client is allowed to
3771 * do audio input and the client ID is saved by the ConsoleVRDPServer.
3772 */
3773 return VINF_SUCCESS;
3774 }
3775 }
3776
3777 /*
3778 * Not supported or no client connected.
3779 */
3780 return VERR_NOT_SUPPORTED;
3781}
3782
3783void ConsoleVRDPServer::SendAudioInputEnd(void *pvUserCtx)
3784{
3785 RT_NOREF(pvUserCtx);
3786 if (mpEntryPoints && mhServer && mpEntryPoints->VRDEAudioInClose)
3787 {
3788 uint32_t u32ClientId = ASMAtomicReadU32(&mu32AudioInputClientId);
3789 if (u32ClientId != 0) /* 0 would mean broadcast to all clients. */
3790 {
3791 mpEntryPoints->VRDEAudioInClose(mhServer, u32ClientId);
3792 }
3793 }
3794}
3795
3796void ConsoleVRDPServer::QueryInfo(uint32_t index, void *pvBuffer, uint32_t cbBuffer, uint32_t *pcbOut) const
3797{
3798 if (index == VRDE_QI_PORT)
3799 {
3800 uint32_t cbOut = sizeof(int32_t);
3801
3802 if (cbBuffer >= cbOut)
3803 {
3804 *pcbOut = cbOut;
3805 *(int32_t *)pvBuffer = (int32_t)mVRDPBindPort;
3806 }
3807 }
3808 else if (mpEntryPoints && mhServer)
3809 {
3810 mpEntryPoints->VRDEQueryInfo(mhServer, index, pvBuffer, cbBuffer, pcbOut);
3811 }
3812}
3813
3814/* static */ int ConsoleVRDPServer::loadVRDPLibrary(const char *pszLibraryName)
3815{
3816 int rc = VINF_SUCCESS;
3817
3818 if (mVRDPLibrary == NIL_RTLDRMOD)
3819 {
3820 RTERRINFOSTATIC ErrInfo;
3821 RTErrInfoInitStatic(&ErrInfo);
3822
3823 if (RTPathHavePath(pszLibraryName))
3824 rc = SUPR3HardenedLdrLoadPlugIn(pszLibraryName, &mVRDPLibrary, &ErrInfo.Core);
3825 else
3826 rc = SUPR3HardenedLdrLoadAppPriv(pszLibraryName, &mVRDPLibrary, RTLDRLOAD_FLAGS_LOCAL, &ErrInfo.Core);
3827 if (RT_SUCCESS(rc))
3828 {
3829 struct SymbolEntry
3830 {
3831 const char *name;
3832 void **ppfn;
3833 };
3834
3835 #define DEFSYMENTRY(a) { #a, (void**)&mpfn##a }
3836
3837 static const struct SymbolEntry s_aSymbols[] =
3838 {
3839 DEFSYMENTRY(VRDECreateServer)
3840 };
3841
3842 #undef DEFSYMENTRY
3843
3844 for (unsigned i = 0; i < RT_ELEMENTS(s_aSymbols); i++)
3845 {
3846 rc = RTLdrGetSymbol(mVRDPLibrary, s_aSymbols[i].name, s_aSymbols[i].ppfn);
3847
3848 if (RT_FAILURE(rc))
3849 {
3850 LogRel(("VRDE: Error resolving symbol '%s', rc %Rrc.\n", s_aSymbols[i].name, rc));
3851 break;
3852 }
3853 }
3854 }
3855 else
3856 {
3857 if (RTErrInfoIsSet(&ErrInfo.Core))
3858 LogRel(("VRDE: Error loading the library '%s': %s (%Rrc)\n", pszLibraryName, ErrInfo.Core.pszMsg, rc));
3859 else
3860 LogRel(("VRDE: Error loading the library '%s' rc = %Rrc.\n", pszLibraryName, rc));
3861
3862 mVRDPLibrary = NIL_RTLDRMOD;
3863 }
3864 }
3865
3866 if (RT_FAILURE(rc))
3867 {
3868 if (mVRDPLibrary != NIL_RTLDRMOD)
3869 {
3870 RTLdrClose(mVRDPLibrary);
3871 mVRDPLibrary = NIL_RTLDRMOD;
3872 }
3873 }
3874
3875 return rc;
3876}
3877
3878/*
3879 * IVRDEServerInfo implementation.
3880 */
3881// constructor / destructor
3882/////////////////////////////////////////////////////////////////////////////
3883
3884VRDEServerInfo::VRDEServerInfo()
3885 : mParent(NULL)
3886{
3887}
3888
3889VRDEServerInfo::~VRDEServerInfo()
3890{
3891}
3892
3893
3894HRESULT VRDEServerInfo::FinalConstruct()
3895{
3896 return BaseFinalConstruct();
3897}
3898
3899void VRDEServerInfo::FinalRelease()
3900{
3901 uninit();
3902 BaseFinalRelease();
3903}
3904
3905// public methods only for internal purposes
3906/////////////////////////////////////////////////////////////////////////////
3907
3908/**
3909 * Initializes the guest object.
3910 */
3911HRESULT VRDEServerInfo::init(Console *aParent)
3912{
3913 LogFlowThisFunc(("aParent=%p\n", aParent));
3914
3915 ComAssertRet(aParent, E_INVALIDARG);
3916
3917 /* Enclose the state transition NotReady->InInit->Ready */
3918 AutoInitSpan autoInitSpan(this);
3919 AssertReturn(autoInitSpan.isOk(), E_FAIL);
3920
3921 unconst(mParent) = aParent;
3922
3923 /* Confirm a successful initialization */
3924 autoInitSpan.setSucceeded();
3925
3926 return S_OK;
3927}
3928
3929/**
3930 * Uninitializes the instance and sets the ready flag to FALSE.
3931 * Called either from FinalRelease() or by the parent when it gets destroyed.
3932 */
3933void VRDEServerInfo::uninit()
3934{
3935 LogFlowThisFunc(("\n"));
3936
3937 /* Enclose the state transition Ready->InUninit->NotReady */
3938 AutoUninitSpan autoUninitSpan(this);
3939 if (autoUninitSpan.uninitDone())
3940 return;
3941
3942 unconst(mParent) = NULL;
3943}
3944
3945// IVRDEServerInfo properties
3946/////////////////////////////////////////////////////////////////////////////
3947
3948#define IMPL_GETTER_BOOL(_aType, _aName, _aIndex) \
3949 HRESULT VRDEServerInfo::get##_aName(_aType *a##_aName) \
3950 { \
3951 /** @todo Not sure if a AutoReadLock would be sufficient. */ \
3952 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); \
3953 \
3954 uint32_t value; \
3955 uint32_t cbOut = 0; \
3956 \
3957 mParent->i_consoleVRDPServer()->QueryInfo \
3958 (_aIndex, &value, sizeof(value), &cbOut); \
3959 \
3960 *a##_aName = cbOut? !!value: FALSE; \
3961 \
3962 return S_OK; \
3963 } \
3964 extern void IMPL_GETTER_BOOL_DUMMY(void)
3965
3966#define IMPL_GETTER_SCALAR(_aType, _aName, _aIndex, _aValueMask) \
3967 HRESULT VRDEServerInfo::get##_aName(_aType *a##_aName) \
3968 { \
3969 /** @todo Not sure if a AutoReadLock would be sufficient. */ \
3970 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); \
3971 \
3972 _aType value; \
3973 uint32_t cbOut = 0; \
3974 \
3975 mParent->i_consoleVRDPServer()->QueryInfo \
3976 (_aIndex, &value, sizeof(value), &cbOut); \
3977 \
3978 if (_aValueMask) value &= (_aValueMask); \
3979 *a##_aName = cbOut? value: 0; \
3980 \
3981 return S_OK; \
3982 } \
3983 extern void IMPL_GETTER_SCALAR_DUMMY(void)
3984
3985#define IMPL_GETTER_UTF8STR(_aType, _aName, _aIndex) \
3986 HRESULT VRDEServerInfo::get##_aName(_aType &a##_aName) \
3987 { \
3988 /** @todo Not sure if a AutoReadLock would be sufficient. */ \
3989 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); \
3990 \
3991 uint32_t cbOut = 0; \
3992 \
3993 mParent->i_consoleVRDPServer()->QueryInfo \
3994 (_aIndex, NULL, 0, &cbOut); \
3995 \
3996 if (cbOut == 0) \
3997 { \
3998 a##_aName = Utf8Str::Empty; \
3999 return S_OK; \
4000 } \
4001 \
4002 char *pchBuffer = (char *)RTMemTmpAlloc(cbOut); \
4003 \
4004 if (!pchBuffer) \
4005 { \
4006 Log(("VRDEServerInfo::" \
4007 #_aName \
4008 ": Failed to allocate memory %d bytes\n", cbOut)); \
4009 return E_OUTOFMEMORY; \
4010 } \
4011 \
4012 mParent->i_consoleVRDPServer()->QueryInfo \
4013 (_aIndex, pchBuffer, cbOut, &cbOut); \
4014 \
4015 a##_aName = pchBuffer; \
4016 \
4017 RTMemTmpFree(pchBuffer); \
4018 \
4019 return S_OK; \
4020 } \
4021 extern void IMPL_GETTER_BSTR_DUMMY(void)
4022
4023IMPL_GETTER_BOOL (BOOL, Active, VRDE_QI_ACTIVE);
4024IMPL_GETTER_SCALAR (LONG, Port, VRDE_QI_PORT, 0);
4025IMPL_GETTER_SCALAR (ULONG, NumberOfClients, VRDE_QI_NUMBER_OF_CLIENTS, 0);
4026IMPL_GETTER_SCALAR (LONG64, BeginTime, VRDE_QI_BEGIN_TIME, 0);
4027IMPL_GETTER_SCALAR (LONG64, EndTime, VRDE_QI_END_TIME, 0);
4028IMPL_GETTER_SCALAR (LONG64, BytesSent, VRDE_QI_BYTES_SENT, INT64_MAX);
4029IMPL_GETTER_SCALAR (LONG64, BytesSentTotal, VRDE_QI_BYTES_SENT_TOTAL, INT64_MAX);
4030IMPL_GETTER_SCALAR (LONG64, BytesReceived, VRDE_QI_BYTES_RECEIVED, INT64_MAX);
4031IMPL_GETTER_SCALAR (LONG64, BytesReceivedTotal, VRDE_QI_BYTES_RECEIVED_TOTAL, INT64_MAX);
4032IMPL_GETTER_UTF8STR(Utf8Str, User, VRDE_QI_USER);
4033IMPL_GETTER_UTF8STR(Utf8Str, Domain, VRDE_QI_DOMAIN);
4034IMPL_GETTER_UTF8STR(Utf8Str, ClientName, VRDE_QI_CLIENT_NAME);
4035IMPL_GETTER_UTF8STR(Utf8Str, ClientIP, VRDE_QI_CLIENT_IP);
4036IMPL_GETTER_SCALAR (ULONG, ClientVersion, VRDE_QI_CLIENT_VERSION, 0);
4037IMPL_GETTER_SCALAR (ULONG, EncryptionStyle, VRDE_QI_ENCRYPTION_STYLE, 0);
4038
4039#undef IMPL_GETTER_UTF8STR
4040#undef IMPL_GETTER_SCALAR
4041#undef IMPL_GETTER_BOOL
4042/* 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