VirtualBox

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

Last change on this file was 106061, checked in by vboxsync, 8 weeks ago

Copyright year updates by scm.

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