VirtualBox

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

Last change on this file since 54828 was 54828, checked in by vboxsync, 10 years ago

Main,Frontends: use BitmapFormat in API.

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