VirtualBox

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

Last change on this file since 73370 was 73370, checked in by vboxsync, 6 years ago

Audio: Implemented backend-independent (pre-)buffering support. Work in progress.

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