VirtualBox

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

Last change on this file since 98273 was 98103, checked in by vboxsync, 23 months ago

Copyright year updates by scm.

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