VirtualBox

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

Last change on this file since 64368 was 63563, checked in by vboxsync, 8 years ago

scm: cleaning up todos

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 133.2 KB
Line 
1/* $Id: ConsoleVRDPServer.cpp 63563 2016-08-16 14:04:28Z vboxsync $ */
2/** @file
3 * VBox Console VRDP helper class.
4 */
5
6/*
7 * Copyright (C) 2006-2016 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#include "DrvAudioVRDE.h"
24#ifdef VBOX_WITH_EXTPACK
25# include "ExtPackManagerImpl.h"
26#endif
27#include "VMMDev.h"
28#ifdef VBOX_WITH_USB_CARDREADER
29# include "UsbCardReader.h"
30#endif
31#include "UsbWebcamInterface.h"
32
33#include "Global.h"
34#include "AutoCaller.h"
35#include "Logging.h"
36
37#include <iprt/asm.h>
38#include <iprt/alloca.h>
39#include <iprt/ldr.h>
40#include <iprt/param.h>
41#include <iprt/path.h>
42#include <iprt/cpp/utils.h>
43
44#include <VBox/err.h>
45#include <VBox/RemoteDesktop/VRDEOrders.h>
46#include <VBox/com/listeners.h>
47#include <VBox/HostServices/VBoxCrOpenGLSvc.h>
48
49class VRDPConsoleListener
50{
51public:
52 VRDPConsoleListener()
53 {
54 }
55
56 virtual ~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 = UINT32_MAX;
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 = UINT32_MAX;
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 = UINT32_MAX;
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 Log9(("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 AudioVRDE *pVRDE = pServer->mConsole->i_getAudioVRDE();
959 if (pVRDE)
960 pVRDE->onVRDEInputIntercept(false /* fIntercept */);
961 }
962
963 int32_t cClients = ASMAtomicDecS32(&pServer->mcClients);
964 if (cClients == 0)
965 {
966 /* Features which should be enabled only if there is a client. */
967 pServer->remote3DRedirect(false);
968 }
969}
970
971DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackIntercept(void *pvCallback, uint32_t u32ClientId, uint32_t fu32Intercept,
972 void **ppvIntercept)
973{
974 ConsoleVRDPServer *pServer = static_cast<ConsoleVRDPServer*>(pvCallback);
975 AssertPtrReturn(pServer, VERR_INVALID_POINTER);
976
977 LogFlowFunc(("%x\n", fu32Intercept));
978
979 int rc = VERR_NOT_SUPPORTED;
980
981 switch (fu32Intercept)
982 {
983 case VRDE_CLIENT_INTERCEPT_AUDIO:
984 {
985 pServer->mConsole->i_VRDPInterceptAudio(u32ClientId);
986 if (ppvIntercept)
987 {
988 *ppvIntercept = pServer;
989 }
990 rc = VINF_SUCCESS;
991 } break;
992
993 case VRDE_CLIENT_INTERCEPT_USB:
994 {
995 pServer->mConsole->i_VRDPInterceptUSB(u32ClientId, ppvIntercept);
996 rc = VINF_SUCCESS;
997 } break;
998
999 case VRDE_CLIENT_INTERCEPT_CLIPBOARD:
1000 {
1001 pServer->mConsole->i_VRDPInterceptClipboard(u32ClientId);
1002 if (ppvIntercept)
1003 {
1004 *ppvIntercept = pServer;
1005 }
1006 rc = VINF_SUCCESS;
1007 } break;
1008
1009 case VRDE_CLIENT_INTERCEPT_AUDIO_INPUT:
1010 {
1011 /*
1012 * This request is processed internally by the ConsoleVRDPServer.
1013 * Only one client is allowed to intercept audio input.
1014 */
1015 if (ASMAtomicCmpXchgU32(&pServer->mu32AudioInputClientId, u32ClientId, 0) == true)
1016 {
1017 LogFunc(("Intercepting audio input by client %RU32\n", u32ClientId));
1018
1019 AudioVRDE *pVRDE = pServer->mConsole->i_getAudioVRDE();
1020 if (pVRDE)
1021 pVRDE->onVRDEInputIntercept(true /* fIntercept */);
1022 }
1023 else
1024 {
1025 Log(("AUDIOIN: ignored client %RU32, active client %RU32\n", u32ClientId, pServer->mu32AudioInputClientId));
1026 rc = VERR_NOT_SUPPORTED;
1027 }
1028 } break;
1029
1030 default:
1031 break;
1032 }
1033
1034 return rc;
1035}
1036
1037DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackUSB(void *pvCallback, void *pvIntercept, uint32_t u32ClientId,
1038 uint8_t u8Code, const void *pvRet, uint32_t cbRet)
1039{
1040 RT_NOREF(pvCallback);
1041#ifdef VBOX_WITH_USB
1042 return USBClientResponseCallback(pvIntercept, u32ClientId, u8Code, pvRet, cbRet);
1043#else
1044 return VERR_NOT_SUPPORTED;
1045#endif
1046}
1047
1048DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackClipboard(void *pvCallback, void *pvIntercept, uint32_t u32ClientId,
1049 uint32_t u32Function, uint32_t u32Format,
1050 const void *pvData, uint32_t cbData)
1051{
1052 RT_NOREF(pvCallback);
1053 return ClipboardCallback(pvIntercept, u32ClientId, u32Function, u32Format, pvData, cbData);
1054}
1055
1056DECLCALLBACK(bool) ConsoleVRDPServer::VRDPCallbackFramebufferQuery(void *pvCallback, unsigned uScreenId,
1057 VRDEFRAMEBUFFERINFO *pInfo)
1058{
1059 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1060
1061 bool fAvailable = false;
1062
1063 /* Obtain the new screen bitmap. */
1064 HRESULT hr = server->mConsole->i_getDisplay()->QuerySourceBitmap(uScreenId, server->maSourceBitmaps[uScreenId].asOutParam());
1065 if (SUCCEEDED(hr))
1066 {
1067 LONG xOrigin = 0;
1068 LONG yOrigin = 0;
1069 BYTE *pAddress = NULL;
1070 ULONG ulWidth = 0;
1071 ULONG ulHeight = 0;
1072 ULONG ulBitsPerPixel = 0;
1073 ULONG ulBytesPerLine = 0;
1074 BitmapFormat_T bitmapFormat = BitmapFormat_Opaque;
1075
1076 hr = server->maSourceBitmaps[uScreenId]->QueryBitmapInfo(&pAddress,
1077 &ulWidth,
1078 &ulHeight,
1079 &ulBitsPerPixel,
1080 &ulBytesPerLine,
1081 &bitmapFormat);
1082
1083 if (SUCCEEDED(hr))
1084 {
1085 ULONG dummy;
1086 GuestMonitorStatus_T monitorStatus;
1087 hr = server->mConsole->i_getDisplay()->GetScreenResolution(uScreenId, &dummy, &dummy, &dummy,
1088 &xOrigin, &yOrigin, &monitorStatus);
1089
1090 if (SUCCEEDED(hr))
1091 {
1092 /* Now fill the information as requested by the caller. */
1093 pInfo->pu8Bits = pAddress;
1094 pInfo->xOrigin = xOrigin;
1095 pInfo->yOrigin = yOrigin;
1096 pInfo->cWidth = ulWidth;
1097 pInfo->cHeight = ulHeight;
1098 pInfo->cBitsPerPixel = ulBitsPerPixel;
1099 pInfo->cbLine = ulBytesPerLine;
1100
1101 fAvailable = true;
1102 }
1103 }
1104 }
1105
1106 return fAvailable;
1107}
1108
1109DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackFramebufferLock(void *pvCallback, unsigned uScreenId)
1110{
1111 NOREF(pvCallback);
1112 NOREF(uScreenId);
1113 /* Do nothing */
1114}
1115
1116DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackFramebufferUnlock(void *pvCallback, unsigned uScreenId)
1117{
1118 NOREF(pvCallback);
1119 NOREF(uScreenId);
1120 /* Do nothing */
1121}
1122
1123static void fixKbdLockStatus(VRDPInputSynch *pInputSynch, IKeyboard *pKeyboard)
1124{
1125 if ( pInputSynch->cGuestNumLockAdaptions
1126 && (pInputSynch->fGuestNumLock != pInputSynch->fClientNumLock))
1127 {
1128 pInputSynch->cGuestNumLockAdaptions--;
1129 pKeyboard->PutScancode(0x45);
1130 pKeyboard->PutScancode(0x45 | 0x80);
1131 }
1132 if ( pInputSynch->cGuestCapsLockAdaptions
1133 && (pInputSynch->fGuestCapsLock != pInputSynch->fClientCapsLock))
1134 {
1135 pInputSynch->cGuestCapsLockAdaptions--;
1136 pKeyboard->PutScancode(0x3a);
1137 pKeyboard->PutScancode(0x3a | 0x80);
1138 }
1139}
1140
1141DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackInput(void *pvCallback, int type, const void *pvInput, unsigned cbInput)
1142{
1143 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1144 Console *pConsole = server->mConsole;
1145
1146 switch (type)
1147 {
1148 case VRDE_INPUT_SCANCODE:
1149 {
1150 if (cbInput == sizeof(VRDEINPUTSCANCODE))
1151 {
1152 IKeyboard *pKeyboard = pConsole->i_getKeyboard();
1153
1154 const VRDEINPUTSCANCODE *pInputScancode = (VRDEINPUTSCANCODE *)pvInput;
1155
1156 /* Track lock keys. */
1157 if (pInputScancode->uScancode == 0x45)
1158 {
1159 server->m_InputSynch.fClientNumLock = !server->m_InputSynch.fClientNumLock;
1160 }
1161 else if (pInputScancode->uScancode == 0x3a)
1162 {
1163 server->m_InputSynch.fClientCapsLock = !server->m_InputSynch.fClientCapsLock;
1164 }
1165 else if (pInputScancode->uScancode == 0x46)
1166 {
1167 server->m_InputSynch.fClientScrollLock = !server->m_InputSynch.fClientScrollLock;
1168 }
1169 else if ((pInputScancode->uScancode & 0x80) == 0)
1170 {
1171 /* Key pressed. */
1172 fixKbdLockStatus(&server->m_InputSynch, pKeyboard);
1173 }
1174
1175 pKeyboard->PutScancode((LONG)pInputScancode->uScancode);
1176 }
1177 } break;
1178
1179 case VRDE_INPUT_POINT:
1180 {
1181 if (cbInput == sizeof(VRDEINPUTPOINT))
1182 {
1183 const VRDEINPUTPOINT *pInputPoint = (VRDEINPUTPOINT *)pvInput;
1184
1185 int mouseButtons = 0;
1186 int iWheel = 0;
1187
1188 if (pInputPoint->uButtons & VRDE_INPUT_POINT_BUTTON1)
1189 {
1190 mouseButtons |= MouseButtonState_LeftButton;
1191 }
1192 if (pInputPoint->uButtons & VRDE_INPUT_POINT_BUTTON2)
1193 {
1194 mouseButtons |= MouseButtonState_RightButton;
1195 }
1196 if (pInputPoint->uButtons & VRDE_INPUT_POINT_BUTTON3)
1197 {
1198 mouseButtons |= MouseButtonState_MiddleButton;
1199 }
1200 if (pInputPoint->uButtons & VRDE_INPUT_POINT_WHEEL_UP)
1201 {
1202 mouseButtons |= MouseButtonState_WheelUp;
1203 iWheel = -1;
1204 }
1205 if (pInputPoint->uButtons & VRDE_INPUT_POINT_WHEEL_DOWN)
1206 {
1207 mouseButtons |= MouseButtonState_WheelDown;
1208 iWheel = 1;
1209 }
1210
1211 if (server->m_fGuestWantsAbsolute)
1212 {
1213 pConsole->i_getMouse()->PutMouseEventAbsolute(pInputPoint->x + 1, pInputPoint->y + 1, iWheel,
1214 0 /* Horizontal wheel */, mouseButtons);
1215 } else
1216 {
1217 pConsole->i_getMouse()->PutMouseEvent(pInputPoint->x - server->m_mousex,
1218 pInputPoint->y - server->m_mousey,
1219 iWheel, 0 /* Horizontal wheel */, mouseButtons);
1220 server->m_mousex = pInputPoint->x;
1221 server->m_mousey = pInputPoint->y;
1222 }
1223 }
1224 } break;
1225
1226 case VRDE_INPUT_CAD:
1227 {
1228 pConsole->i_getKeyboard()->PutCAD();
1229 } break;
1230
1231 case VRDE_INPUT_RESET:
1232 {
1233 pConsole->Reset();
1234 } break;
1235
1236 case VRDE_INPUT_SYNCH:
1237 {
1238 if (cbInput == sizeof(VRDEINPUTSYNCH))
1239 {
1240 IKeyboard *pKeyboard = pConsole->i_getKeyboard();
1241
1242 const VRDEINPUTSYNCH *pInputSynch = (VRDEINPUTSYNCH *)pvInput;
1243
1244 server->m_InputSynch.fClientNumLock = (pInputSynch->uLockStatus & VRDE_INPUT_SYNCH_NUMLOCK) != 0;
1245 server->m_InputSynch.fClientCapsLock = (pInputSynch->uLockStatus & VRDE_INPUT_SYNCH_CAPITAL) != 0;
1246 server->m_InputSynch.fClientScrollLock = (pInputSynch->uLockStatus & VRDE_INPUT_SYNCH_SCROLL) != 0;
1247
1248 /* The client initiated synchronization. Always make the guest to reflect the client state.
1249 * Than means, when the guest changes the state itself, it is forced to return to the client
1250 * state.
1251 */
1252 if (server->m_InputSynch.fClientNumLock != server->m_InputSynch.fGuestNumLock)
1253 {
1254 server->m_InputSynch.cGuestNumLockAdaptions = 2;
1255 }
1256
1257 if (server->m_InputSynch.fClientCapsLock != server->m_InputSynch.fGuestCapsLock)
1258 {
1259 server->m_InputSynch.cGuestCapsLockAdaptions = 2;
1260 }
1261
1262 fixKbdLockStatus(&server->m_InputSynch, pKeyboard);
1263 }
1264 } break;
1265
1266 default:
1267 break;
1268 }
1269}
1270
1271DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackVideoModeHint(void *pvCallback, unsigned cWidth, unsigned cHeight,
1272 unsigned cBitsPerPixel, unsigned uScreenId)
1273{
1274 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1275
1276 server->mConsole->i_getDisplay()->SetVideoModeHint(uScreenId, TRUE /*=enabled*/,
1277 FALSE /*=changeOrigin*/, 0/*=OriginX*/, 0/*=OriginY*/,
1278 cWidth, cHeight, cBitsPerPixel);
1279}
1280
1281DECLCALLBACK(void) ConsoleVRDPServer::VRDECallbackAudioIn(void *pvCallback,
1282 void *pvCtx,
1283 uint32_t u32ClientId,
1284 uint32_t u32Event,
1285 const void *pvData,
1286 uint32_t cbData)
1287{
1288 RT_NOREF(u32ClientId);
1289 ConsoleVRDPServer *pServer = static_cast<ConsoleVRDPServer*>(pvCallback);
1290 AssertPtrReturnVoid(pServer);
1291
1292 AudioVRDE *pVRDE = pServer->mConsole->i_getAudioVRDE();
1293 if (!pVRDE) /* Nothing to do, bail out early. */
1294 return;
1295
1296 switch (u32Event)
1297 {
1298 case VRDE_AUDIOIN_BEGIN:
1299 {
1300 pVRDE->onVRDEInputBegin(pvCtx, (PVRDEAUDIOINBEGIN)pvData);
1301 break;
1302 }
1303
1304 case VRDE_AUDIOIN_DATA:
1305 pVRDE->onVRDEInputData(pvCtx, pvData, cbData);
1306 break;
1307
1308 case VRDE_AUDIOIN_END:
1309 pVRDE->onVRDEInputEnd(pvCtx);
1310 break;
1311
1312 default:
1313 break;
1314 }
1315}
1316
1317ConsoleVRDPServer::ConsoleVRDPServer(Console *console)
1318{
1319 mConsole = console;
1320
1321 int rc = RTCritSectInit(&mCritSect);
1322 AssertRC(rc);
1323
1324 mcClipboardRefs = 0;
1325 mpfnClipboardCallback = NULL;
1326#ifdef VBOX_WITH_USB
1327 mUSBBackends.pHead = NULL;
1328 mUSBBackends.pTail = NULL;
1329
1330 mUSBBackends.thread = NIL_RTTHREAD;
1331 mUSBBackends.fThreadRunning = false;
1332 mUSBBackends.event = 0;
1333#endif
1334
1335 mhServer = 0;
1336 mServerInterfaceVersion = 0;
1337
1338 mcInResize = 0;
1339
1340 m_fGuestWantsAbsolute = false;
1341 m_mousex = 0;
1342 m_mousey = 0;
1343
1344 m_InputSynch.cGuestNumLockAdaptions = 2;
1345 m_InputSynch.cGuestCapsLockAdaptions = 2;
1346
1347 m_InputSynch.fGuestNumLock = false;
1348 m_InputSynch.fGuestCapsLock = false;
1349 m_InputSynch.fGuestScrollLock = false;
1350
1351 m_InputSynch.fClientNumLock = false;
1352 m_InputSynch.fClientCapsLock = false;
1353 m_InputSynch.fClientScrollLock = false;
1354
1355 {
1356 ComPtr<IEventSource> es;
1357 console->COMGETTER(EventSource)(es.asOutParam());
1358 ComObjPtr<VRDPConsoleListenerImpl> aConsoleListener;
1359 aConsoleListener.createObject();
1360 aConsoleListener->init(new VRDPConsoleListener(), this);
1361 mConsoleListener = aConsoleListener;
1362 com::SafeArray <VBoxEventType_T> eventTypes;
1363 eventTypes.push_back(VBoxEventType_OnMousePointerShapeChanged);
1364 eventTypes.push_back(VBoxEventType_OnMouseCapabilityChanged);
1365 eventTypes.push_back(VBoxEventType_OnKeyboardLedsChanged);
1366 es->RegisterListener(mConsoleListener, ComSafeArrayAsInParam(eventTypes), true);
1367 }
1368
1369 mVRDPBindPort = -1;
1370
1371#ifndef VBOX_WITH_VRDEAUTH_IN_VBOXSVC
1372 RT_ZERO(mAuthLibCtx);
1373#endif
1374
1375 mu32AudioInputClientId = 0;
1376 mcClients = 0;
1377
1378 /*
1379 * Optional interfaces.
1380 */
1381 m_fInterfaceImage = false;
1382 RT_ZERO(m_interfaceImage);
1383 RT_ZERO(m_interfaceCallbacksImage);
1384 RT_ZERO(m_interfaceMousePtr);
1385 RT_ZERO(m_interfaceSCard);
1386 RT_ZERO(m_interfaceCallbacksSCard);
1387 RT_ZERO(m_interfaceTSMF);
1388 RT_ZERO(m_interfaceCallbacksTSMF);
1389 RT_ZERO(m_interfaceVideoIn);
1390 RT_ZERO(m_interfaceCallbacksVideoIn);
1391 RT_ZERO(m_interfaceInput);
1392 RT_ZERO(m_interfaceCallbacksInput);
1393
1394 rc = RTCritSectInit(&mTSMFLock);
1395 AssertRC(rc);
1396
1397 mEmWebcam = new EmWebcam(this);
1398 AssertPtr(mEmWebcam);
1399}
1400
1401ConsoleVRDPServer::~ConsoleVRDPServer()
1402{
1403 Stop();
1404
1405 if (mConsoleListener)
1406 {
1407 ComPtr<IEventSource> es;
1408 mConsole->COMGETTER(EventSource)(es.asOutParam());
1409 es->UnregisterListener(mConsoleListener);
1410 mConsoleListener.setNull();
1411 }
1412
1413 unsigned i;
1414 for (i = 0; i < RT_ELEMENTS(maSourceBitmaps); i++)
1415 {
1416 maSourceBitmaps[i].setNull();
1417 }
1418
1419 if (mEmWebcam)
1420 {
1421 delete mEmWebcam;
1422 mEmWebcam = NULL;
1423 }
1424
1425 if (RTCritSectIsInitialized(&mCritSect))
1426 {
1427 RTCritSectDelete(&mCritSect);
1428 RT_ZERO(mCritSect);
1429 }
1430
1431 if (RTCritSectIsInitialized(&mTSMFLock))
1432 {
1433 RTCritSectDelete(&mTSMFLock);
1434 RT_ZERO(mTSMFLock);
1435 }
1436}
1437
1438int ConsoleVRDPServer::Launch(void)
1439{
1440 LogFlowThisFunc(("\n"));
1441
1442 IVRDEServer *server = mConsole->i_getVRDEServer();
1443 AssertReturn(server, VERR_INTERNAL_ERROR_2);
1444
1445 /*
1446 * Check if VRDE is enabled.
1447 */
1448 BOOL fEnabled;
1449 HRESULT hrc = server->COMGETTER(Enabled)(&fEnabled);
1450 AssertComRCReturn(hrc, Global::vboxStatusCodeFromCOM(hrc));
1451 if (!fEnabled)
1452 return VINF_SUCCESS;
1453
1454 /*
1455 * Check that a VRDE extension pack name is set and resolve it into a
1456 * library path.
1457 */
1458 Bstr bstrExtPack;
1459 hrc = server->COMGETTER(VRDEExtPack)(bstrExtPack.asOutParam());
1460 if (FAILED(hrc))
1461 return Global::vboxStatusCodeFromCOM(hrc);
1462 if (bstrExtPack.isEmpty())
1463 return VINF_NOT_SUPPORTED;
1464
1465 Utf8Str strExtPack(bstrExtPack);
1466 Utf8Str strVrdeLibrary;
1467 int vrc = VINF_SUCCESS;
1468 if (strExtPack.equals(VBOXVRDP_KLUDGE_EXTPACK_NAME))
1469 strVrdeLibrary = "VBoxVRDP";
1470 else
1471 {
1472#ifdef VBOX_WITH_EXTPACK
1473 ExtPackManager *pExtPackMgr = mConsole->i_getExtPackManager();
1474 vrc = pExtPackMgr->i_getVrdeLibraryPathForExtPack(&strExtPack, &strVrdeLibrary);
1475#else
1476 vrc = VERR_FILE_NOT_FOUND;
1477#endif
1478 }
1479 if (RT_SUCCESS(vrc))
1480 {
1481 /*
1482 * Load the VRDE library and start the server, if it is enabled.
1483 */
1484 vrc = loadVRDPLibrary(strVrdeLibrary.c_str());
1485 if (RT_SUCCESS(vrc))
1486 {
1487 VRDEENTRYPOINTS_4 *pEntryPoints4;
1488 vrc = mpfnVRDECreateServer(&mCallbacks.header, this, (VRDEINTERFACEHDR **)&pEntryPoints4, &mhServer);
1489
1490 if (RT_SUCCESS(vrc))
1491 {
1492 mServerInterfaceVersion = 4;
1493 mEntryPoints = *pEntryPoints4;
1494 mpEntryPoints = &mEntryPoints;
1495 }
1496 else if (vrc == VERR_VERSION_MISMATCH)
1497 {
1498 /* An older version of VRDE is installed, try version 3. */
1499 VRDEENTRYPOINTS_3 *pEntryPoints3;
1500
1501 static VRDECALLBACKS_3 sCallbacks3 =
1502 {
1503 { VRDE_INTERFACE_VERSION_3, sizeof(VRDECALLBACKS_3) },
1504 ConsoleVRDPServer::VRDPCallbackQueryProperty,
1505 ConsoleVRDPServer::VRDPCallbackClientLogon,
1506 ConsoleVRDPServer::VRDPCallbackClientConnect,
1507 ConsoleVRDPServer::VRDPCallbackClientDisconnect,
1508 ConsoleVRDPServer::VRDPCallbackIntercept,
1509 ConsoleVRDPServer::VRDPCallbackUSB,
1510 ConsoleVRDPServer::VRDPCallbackClipboard,
1511 ConsoleVRDPServer::VRDPCallbackFramebufferQuery,
1512 ConsoleVRDPServer::VRDPCallbackFramebufferLock,
1513 ConsoleVRDPServer::VRDPCallbackFramebufferUnlock,
1514 ConsoleVRDPServer::VRDPCallbackInput,
1515 ConsoleVRDPServer::VRDPCallbackVideoModeHint,
1516 ConsoleVRDPServer::VRDECallbackAudioIn
1517 };
1518
1519 vrc = mpfnVRDECreateServer(&sCallbacks3.header, this, (VRDEINTERFACEHDR **)&pEntryPoints3, &mhServer);
1520 if (RT_SUCCESS(vrc))
1521 {
1522 mServerInterfaceVersion = 3;
1523 mEntryPoints.header = pEntryPoints3->header;
1524 mEntryPoints.VRDEDestroy = pEntryPoints3->VRDEDestroy;
1525 mEntryPoints.VRDEEnableConnections = pEntryPoints3->VRDEEnableConnections;
1526 mEntryPoints.VRDEDisconnect = pEntryPoints3->VRDEDisconnect;
1527 mEntryPoints.VRDEResize = pEntryPoints3->VRDEResize;
1528 mEntryPoints.VRDEUpdate = pEntryPoints3->VRDEUpdate;
1529 mEntryPoints.VRDEColorPointer = pEntryPoints3->VRDEColorPointer;
1530 mEntryPoints.VRDEHidePointer = pEntryPoints3->VRDEHidePointer;
1531 mEntryPoints.VRDEAudioSamples = pEntryPoints3->VRDEAudioSamples;
1532 mEntryPoints.VRDEAudioVolume = pEntryPoints3->VRDEAudioVolume;
1533 mEntryPoints.VRDEUSBRequest = pEntryPoints3->VRDEUSBRequest;
1534 mEntryPoints.VRDEClipboard = pEntryPoints3->VRDEClipboard;
1535 mEntryPoints.VRDEQueryInfo = pEntryPoints3->VRDEQueryInfo;
1536 mEntryPoints.VRDERedirect = pEntryPoints3->VRDERedirect;
1537 mEntryPoints.VRDEAudioInOpen = pEntryPoints3->VRDEAudioInOpen;
1538 mEntryPoints.VRDEAudioInClose = pEntryPoints3->VRDEAudioInClose;
1539 mEntryPoints.VRDEGetInterface = NULL;
1540 mpEntryPoints = &mEntryPoints;
1541 }
1542 else if (vrc == VERR_VERSION_MISMATCH)
1543 {
1544 /* An older version of VRDE is installed, try version 1. */
1545 VRDEENTRYPOINTS_1 *pEntryPoints1;
1546
1547 static VRDECALLBACKS_1 sCallbacks1 =
1548 {
1549 { VRDE_INTERFACE_VERSION_1, sizeof(VRDECALLBACKS_1) },
1550 ConsoleVRDPServer::VRDPCallbackQueryProperty,
1551 ConsoleVRDPServer::VRDPCallbackClientLogon,
1552 ConsoleVRDPServer::VRDPCallbackClientConnect,
1553 ConsoleVRDPServer::VRDPCallbackClientDisconnect,
1554 ConsoleVRDPServer::VRDPCallbackIntercept,
1555 ConsoleVRDPServer::VRDPCallbackUSB,
1556 ConsoleVRDPServer::VRDPCallbackClipboard,
1557 ConsoleVRDPServer::VRDPCallbackFramebufferQuery,
1558 ConsoleVRDPServer::VRDPCallbackFramebufferLock,
1559 ConsoleVRDPServer::VRDPCallbackFramebufferUnlock,
1560 ConsoleVRDPServer::VRDPCallbackInput,
1561 ConsoleVRDPServer::VRDPCallbackVideoModeHint
1562 };
1563
1564 vrc = mpfnVRDECreateServer(&sCallbacks1.header, this, (VRDEINTERFACEHDR **)&pEntryPoints1, &mhServer);
1565 if (RT_SUCCESS(vrc))
1566 {
1567 mServerInterfaceVersion = 1;
1568 mEntryPoints.header = pEntryPoints1->header;
1569 mEntryPoints.VRDEDestroy = pEntryPoints1->VRDEDestroy;
1570 mEntryPoints.VRDEEnableConnections = pEntryPoints1->VRDEEnableConnections;
1571 mEntryPoints.VRDEDisconnect = pEntryPoints1->VRDEDisconnect;
1572 mEntryPoints.VRDEResize = pEntryPoints1->VRDEResize;
1573 mEntryPoints.VRDEUpdate = pEntryPoints1->VRDEUpdate;
1574 mEntryPoints.VRDEColorPointer = pEntryPoints1->VRDEColorPointer;
1575 mEntryPoints.VRDEHidePointer = pEntryPoints1->VRDEHidePointer;
1576 mEntryPoints.VRDEAudioSamples = pEntryPoints1->VRDEAudioSamples;
1577 mEntryPoints.VRDEAudioVolume = pEntryPoints1->VRDEAudioVolume;
1578 mEntryPoints.VRDEUSBRequest = pEntryPoints1->VRDEUSBRequest;
1579 mEntryPoints.VRDEClipboard = pEntryPoints1->VRDEClipboard;
1580 mEntryPoints.VRDEQueryInfo = pEntryPoints1->VRDEQueryInfo;
1581 mEntryPoints.VRDERedirect = NULL;
1582 mEntryPoints.VRDEAudioInOpen = NULL;
1583 mEntryPoints.VRDEAudioInClose = NULL;
1584 mEntryPoints.VRDEGetInterface = NULL;
1585 mpEntryPoints = &mEntryPoints;
1586 }
1587 }
1588 }
1589
1590 if (RT_SUCCESS(vrc))
1591 {
1592 LogRel(("VRDE: loaded version %d of the server.\n", mServerInterfaceVersion));
1593
1594 if (mServerInterfaceVersion >= 4)
1595 {
1596 /* The server supports optional interfaces. */
1597 Assert(mpEntryPoints->VRDEGetInterface != NULL);
1598
1599 /* Image interface. */
1600 m_interfaceImage.header.u64Version = 1;
1601 m_interfaceImage.header.u64Size = sizeof(m_interfaceImage);
1602
1603 m_interfaceCallbacksImage.header.u64Version = 1;
1604 m_interfaceCallbacksImage.header.u64Size = sizeof(m_interfaceCallbacksImage);
1605 m_interfaceCallbacksImage.VRDEImageCbNotify = VRDEImageCbNotify;
1606
1607 vrc = mpEntryPoints->VRDEGetInterface(mhServer,
1608 VRDE_IMAGE_INTERFACE_NAME,
1609 &m_interfaceImage.header,
1610 &m_interfaceCallbacksImage.header,
1611 this);
1612 if (RT_SUCCESS(vrc))
1613 {
1614 LogRel(("VRDE: [%s]\n", VRDE_IMAGE_INTERFACE_NAME));
1615 m_fInterfaceImage = true;
1616 }
1617
1618 /* Mouse pointer interface. */
1619 m_interfaceMousePtr.header.u64Version = 1;
1620 m_interfaceMousePtr.header.u64Size = sizeof(m_interfaceMousePtr);
1621
1622 vrc = mpEntryPoints->VRDEGetInterface(mhServer,
1623 VRDE_MOUSEPTR_INTERFACE_NAME,
1624 &m_interfaceMousePtr.header,
1625 NULL,
1626 this);
1627 if (RT_SUCCESS(vrc))
1628 {
1629 LogRel(("VRDE: [%s]\n", VRDE_MOUSEPTR_INTERFACE_NAME));
1630 }
1631 else
1632 {
1633 RT_ZERO(m_interfaceMousePtr);
1634 }
1635
1636 /* Smartcard interface. */
1637 m_interfaceSCard.header.u64Version = 1;
1638 m_interfaceSCard.header.u64Size = sizeof(m_interfaceSCard);
1639
1640 m_interfaceCallbacksSCard.header.u64Version = 1;
1641 m_interfaceCallbacksSCard.header.u64Size = sizeof(m_interfaceCallbacksSCard);
1642 m_interfaceCallbacksSCard.VRDESCardCbNotify = VRDESCardCbNotify;
1643 m_interfaceCallbacksSCard.VRDESCardCbResponse = VRDESCardCbResponse;
1644
1645 vrc = mpEntryPoints->VRDEGetInterface(mhServer,
1646 VRDE_SCARD_INTERFACE_NAME,
1647 &m_interfaceSCard.header,
1648 &m_interfaceCallbacksSCard.header,
1649 this);
1650 if (RT_SUCCESS(vrc))
1651 {
1652 LogRel(("VRDE: [%s]\n", VRDE_SCARD_INTERFACE_NAME));
1653 }
1654 else
1655 {
1656 RT_ZERO(m_interfaceSCard);
1657 }
1658
1659 /* Raw TSMF interface. */
1660 m_interfaceTSMF.header.u64Version = 1;
1661 m_interfaceTSMF.header.u64Size = sizeof(m_interfaceTSMF);
1662
1663 m_interfaceCallbacksTSMF.header.u64Version = 1;
1664 m_interfaceCallbacksTSMF.header.u64Size = sizeof(m_interfaceCallbacksTSMF);
1665 m_interfaceCallbacksTSMF.VRDETSMFCbNotify = VRDETSMFCbNotify;
1666
1667 vrc = mpEntryPoints->VRDEGetInterface(mhServer,
1668 VRDE_TSMF_INTERFACE_NAME,
1669 &m_interfaceTSMF.header,
1670 &m_interfaceCallbacksTSMF.header,
1671 this);
1672 if (RT_SUCCESS(vrc))
1673 {
1674 LogRel(("VRDE: [%s]\n", VRDE_TSMF_INTERFACE_NAME));
1675 }
1676 else
1677 {
1678 RT_ZERO(m_interfaceTSMF);
1679 }
1680
1681 /* VideoIn interface. */
1682 m_interfaceVideoIn.header.u64Version = 1;
1683 m_interfaceVideoIn.header.u64Size = sizeof(m_interfaceVideoIn);
1684
1685 m_interfaceCallbacksVideoIn.header.u64Version = 1;
1686 m_interfaceCallbacksVideoIn.header.u64Size = sizeof(m_interfaceCallbacksVideoIn);
1687 m_interfaceCallbacksVideoIn.VRDECallbackVideoInNotify = VRDECallbackVideoInNotify;
1688 m_interfaceCallbacksVideoIn.VRDECallbackVideoInDeviceDesc = VRDECallbackVideoInDeviceDesc;
1689 m_interfaceCallbacksVideoIn.VRDECallbackVideoInControl = VRDECallbackVideoInControl;
1690 m_interfaceCallbacksVideoIn.VRDECallbackVideoInFrame = VRDECallbackVideoInFrame;
1691
1692 vrc = mpEntryPoints->VRDEGetInterface(mhServer,
1693 VRDE_VIDEOIN_INTERFACE_NAME,
1694 &m_interfaceVideoIn.header,
1695 &m_interfaceCallbacksVideoIn.header,
1696 this);
1697 if (RT_SUCCESS(vrc))
1698 {
1699 LogRel(("VRDE: [%s]\n", VRDE_VIDEOIN_INTERFACE_NAME));
1700 }
1701 else
1702 {
1703 RT_ZERO(m_interfaceVideoIn);
1704 }
1705
1706 /* Input interface. */
1707 m_interfaceInput.header.u64Version = 1;
1708 m_interfaceInput.header.u64Size = sizeof(m_interfaceInput);
1709
1710 m_interfaceCallbacksInput.header.u64Version = 1;
1711 m_interfaceCallbacksInput.header.u64Size = sizeof(m_interfaceCallbacksInput);
1712 m_interfaceCallbacksInput.VRDECallbackInputSetup = VRDECallbackInputSetup;
1713 m_interfaceCallbacksInput.VRDECallbackInputEvent = VRDECallbackInputEvent;
1714
1715 vrc = mpEntryPoints->VRDEGetInterface(mhServer,
1716 VRDE_INPUT_INTERFACE_NAME,
1717 &m_interfaceInput.header,
1718 &m_interfaceCallbacksInput.header,
1719 this);
1720 if (RT_SUCCESS(vrc))
1721 {
1722 LogRel(("VRDE: [%s]\n", VRDE_INPUT_INTERFACE_NAME));
1723 }
1724 else
1725 {
1726 RT_ZERO(m_interfaceInput);
1727 }
1728
1729 /* Since these interfaces are optional, it is always a success here. */
1730 vrc = VINF_SUCCESS;
1731 }
1732#ifdef VBOX_WITH_USB
1733 remoteUSBThreadStart();
1734#endif
1735
1736 /*
1737 * Re-init the server current state, which is usually obtained from events.
1738 */
1739 fetchCurrentState();
1740 }
1741 else
1742 {
1743 if (vrc != VERR_NET_ADDRESS_IN_USE)
1744 LogRel(("VRDE: Could not start the server rc = %Rrc\n", vrc));
1745 /* Don't unload the lib, because it prevents us trying again or
1746 because there may be other users? */
1747 }
1748 }
1749 }
1750
1751 return vrc;
1752}
1753
1754void ConsoleVRDPServer::fetchCurrentState(void)
1755{
1756 ComPtr<IMousePointerShape> mps;
1757 mConsole->i_getMouse()->COMGETTER(PointerShape)(mps.asOutParam());
1758 if (!mps.isNull())
1759 {
1760 BOOL visible, alpha;
1761 ULONG hotX, hotY, width, height;
1762 com::SafeArray <BYTE> shape;
1763
1764 mps->COMGETTER(Visible)(&visible);
1765 mps->COMGETTER(Alpha)(&alpha);
1766 mps->COMGETTER(HotX)(&hotX);
1767 mps->COMGETTER(HotY)(&hotY);
1768 mps->COMGETTER(Width)(&width);
1769 mps->COMGETTER(Height)(&height);
1770 mps->COMGETTER(Shape)(ComSafeArrayAsOutParam(shape));
1771
1772 onMousePointerShapeChange(visible, alpha, hotX, hotY, width, height, ComSafeArrayAsInParam(shape));
1773 }
1774}
1775
1776typedef struct H3DORInstance
1777{
1778 ConsoleVRDPServer *pThis;
1779 HVRDEIMAGE hImageBitmap;
1780 int32_t x;
1781 int32_t y;
1782 uint32_t w;
1783 uint32_t h;
1784 bool fCreated;
1785 bool fFallback;
1786 bool fTopDown;
1787} H3DORInstance;
1788
1789#define H3DORLOG Log
1790
1791/* static */ DECLCALLBACK(void) ConsoleVRDPServer::H3DORBegin(const void *pvContext, void **ppvInstance,
1792 const char *pszFormat)
1793{
1794 H3DORLOG(("H3DORBegin: ctx %p [%s]\n", pvContext, pszFormat));
1795
1796 H3DORInstance *p = (H3DORInstance *)RTMemAlloc(sizeof(H3DORInstance));
1797
1798 if (p)
1799 {
1800 p->pThis = (ConsoleVRDPServer *)pvContext;
1801 p->hImageBitmap = NULL;
1802 p->x = 0;
1803 p->y = 0;
1804 p->w = 0;
1805 p->h = 0;
1806 p->fCreated = false;
1807 p->fFallback = false;
1808
1809 /* Host 3D service passes the actual format of data in this redirect instance.
1810 * That is what will be in the H3DORFrame's parameters pvData and cbData.
1811 */
1812 if (RTStrICmp(pszFormat, H3DOR_FMT_RGBA_TOPDOWN) == 0)
1813 {
1814 /* Accept it. */
1815 p->fTopDown = true;
1816 }
1817 else if (RTStrICmp(pszFormat, H3DOR_FMT_RGBA) == 0)
1818 {
1819 /* Accept it. */
1820 p->fTopDown = false;
1821 }
1822 else
1823 {
1824 RTMemFree(p);
1825 p = NULL;
1826 }
1827 }
1828
1829 H3DORLOG(("H3DORBegin: ins %p\n", p));
1830
1831 /* Caller checks this for NULL. */
1832 *ppvInstance = p;
1833}
1834
1835/* static */ DECLCALLBACK(void) ConsoleVRDPServer::H3DORGeometry(void *pvInstance,
1836 int32_t x, int32_t y, uint32_t w, uint32_t h)
1837{
1838 H3DORLOG(("H3DORGeometry: ins %p %d,%d %dx%d\n", pvInstance, x, y, w, h));
1839
1840 H3DORInstance *p = (H3DORInstance *)pvInstance;
1841 Assert(p);
1842 Assert(p->pThis);
1843
1844 /** @todo find out what to do if size changes to 0x0 from non zero */
1845 if (w == 0 || h == 0)
1846 {
1847 /* Do nothing. */
1848 return;
1849 }
1850
1851 RTRECT rect;
1852 rect.xLeft = x;
1853 rect.yTop = y;
1854 rect.xRight = x + w;
1855 rect.yBottom = y + h;
1856
1857 if (p->hImageBitmap)
1858 {
1859 /* An image handle has been already created,
1860 * check if it has the same size as the reported geometry.
1861 */
1862 if ( p->x == x
1863 && p->y == y
1864 && p->w == w
1865 && p->h == h)
1866 {
1867 H3DORLOG(("H3DORGeometry: geometry not changed\n"));
1868 /* Do nothing. Continue using the existing handle. */
1869 }
1870 else
1871 {
1872 int rc = p->fFallback?
1873 VERR_NOT_SUPPORTED: /* Try to go out of fallback mode. */
1874 p->pThis->m_interfaceImage.VRDEImageGeometrySet(p->hImageBitmap, &rect);
1875 if (RT_SUCCESS(rc))
1876 {
1877 p->x = x;
1878 p->y = y;
1879 p->w = w;
1880 p->h = h;
1881 }
1882 else
1883 {
1884 /* The handle must be recreated. Delete existing handle here. */
1885 p->pThis->m_interfaceImage.VRDEImageHandleClose(p->hImageBitmap);
1886 p->hImageBitmap = NULL;
1887 }
1888 }
1889 }
1890
1891 if (!p->hImageBitmap)
1892 {
1893 /* Create a new bitmap handle. */
1894 uint32_t u32ScreenId = 0; /** @todo clip to corresponding screens.
1895 * Clipping can be done here or in VRDP server.
1896 * If VRDP does clipping, then uScreenId parameter
1897 * is not necessary and coords must be global.
1898 * (have to check which coords are used in opengl service).
1899 * Since all VRDE API uses a ScreenId,
1900 * the clipping must be done here in ConsoleVRDPServer
1901 */
1902 uint32_t fu32CompletionFlags = 0;
1903 p->fFallback = false;
1904 int rc = p->pThis->m_interfaceImage.VRDEImageHandleCreate(p->pThis->mhServer,
1905 &p->hImageBitmap,
1906 p,
1907 u32ScreenId,
1908 VRDE_IMAGE_F_CREATE_CONTENT_3D
1909 | VRDE_IMAGE_F_CREATE_WINDOW,
1910 &rect,
1911 VRDE_IMAGE_FMT_ID_BITMAP_BGRA8,
1912 NULL,
1913 0,
1914 &fu32CompletionFlags);
1915 if (RT_FAILURE(rc))
1916 {
1917 /* No support for a 3D + WINDOW. Try bitmap updates. */
1918 H3DORLOG(("H3DORGeometry: Fallback to bitmaps\n"));
1919 fu32CompletionFlags = 0;
1920 p->fFallback = true;
1921 rc = p->pThis->m_interfaceImage.VRDEImageHandleCreate(p->pThis->mhServer,
1922 &p->hImageBitmap,
1923 p,
1924 u32ScreenId,
1925 0,
1926 &rect,
1927 VRDE_IMAGE_FMT_ID_BITMAP_BGRA8,
1928 NULL,
1929 0,
1930 &fu32CompletionFlags);
1931 }
1932
1933 H3DORLOG(("H3DORGeometry: Image handle create %Rrc, flags 0x%RX32\n", rc, fu32CompletionFlags));
1934
1935 if (RT_SUCCESS(rc))
1936 {
1937 p->x = x;
1938 p->y = y;
1939 p->w = w;
1940 p->h = h;
1941
1942 if ((fu32CompletionFlags & VRDE_IMAGE_F_COMPLETE_ASYNC) == 0)
1943 {
1944 p->fCreated = true;
1945 }
1946 }
1947 else
1948 {
1949 p->hImageBitmap = NULL;
1950 p->w = 0;
1951 p->h = 0;
1952 }
1953 }
1954
1955 H3DORLOG(("H3DORGeometry: ins %p completed\n", pvInstance));
1956}
1957
1958/* static */ DECLCALLBACK(void) ConsoleVRDPServer::H3DORVisibleRegion(void *pvInstance,
1959 uint32_t cRects, const RTRECT *paRects)
1960{
1961 H3DORLOG(("H3DORVisibleRegion: ins %p %d\n", pvInstance, cRects));
1962
1963 H3DORInstance *p = (H3DORInstance *)pvInstance;
1964 Assert(p);
1965 Assert(p->pThis);
1966
1967 if (cRects == 0)
1968 {
1969 /* Complete image is visible. */
1970 RTRECT rect;
1971 rect.xLeft = p->x;
1972 rect.yTop = p->y;
1973 rect.xRight = p->x + p->w;
1974 rect.yBottom = p->y + p->h;
1975 p->pThis->m_interfaceImage.VRDEImageRegionSet (p->hImageBitmap,
1976 1,
1977 &rect);
1978 }
1979 else
1980 {
1981 p->pThis->m_interfaceImage.VRDEImageRegionSet (p->hImageBitmap,
1982 cRects,
1983 paRects);
1984 }
1985
1986 H3DORLOG(("H3DORVisibleRegion: ins %p completed\n", pvInstance));
1987}
1988
1989/* static */ DECLCALLBACK(void) ConsoleVRDPServer::H3DORFrame(void *pvInstance,
1990 void *pvData, uint32_t cbData)
1991{
1992 H3DORLOG(("H3DORFrame: ins %p %p %d\n", pvInstance, pvData, cbData));
1993
1994 H3DORInstance *p = (H3DORInstance *)pvInstance;
1995 Assert(p);
1996 Assert(p->pThis);
1997
1998 /* Currently only a topdown BGR0 bitmap format is supported. */
1999 VRDEIMAGEBITMAP image;
2000
2001 image.cWidth = p->w;
2002 image.cHeight = p->h;
2003 image.pvData = pvData;
2004 image.cbData = cbData;
2005 image.pvScanLine0 = (uint8_t *)pvData + (p->h - 1) * p->w * 4;
2006 image.iScanDelta = 4 * p->w;
2007 if (p->fTopDown)
2008 {
2009 image.iScanDelta = -image.iScanDelta;
2010 }
2011
2012 p->pThis->m_interfaceImage.VRDEImageUpdate (p->hImageBitmap,
2013 p->x,
2014 p->y,
2015 p->w,
2016 p->h,
2017 &image,
2018 sizeof(VRDEIMAGEBITMAP));
2019
2020 H3DORLOG(("H3DORFrame: ins %p completed\n", pvInstance));
2021}
2022
2023/* static */ DECLCALLBACK(void) ConsoleVRDPServer::H3DOREnd(void *pvInstance)
2024{
2025 H3DORLOG(("H3DOREnd: ins %p\n", pvInstance));
2026
2027 H3DORInstance *p = (H3DORInstance *)pvInstance;
2028 Assert(p);
2029 Assert(p->pThis);
2030
2031 p->pThis->m_interfaceImage.VRDEImageHandleClose(p->hImageBitmap);
2032
2033 RTMemFree(p);
2034
2035 H3DORLOG(("H3DOREnd: ins %p completed\n", pvInstance));
2036}
2037
2038/* static */ DECLCALLBACK(int) ConsoleVRDPServer::H3DORContextProperty(const void *pvContext, uint32_t index,
2039 void *pvBuffer, uint32_t cbBuffer, uint32_t *pcbOut)
2040{
2041 RT_NOREF(pvContext, pvBuffer);
2042 int rc = VINF_SUCCESS;
2043
2044 H3DORLOG(("H3DORContextProperty: index %d\n", index));
2045
2046 if (index == H3DOR_PROP_FORMATS)
2047 {
2048 /* Return a comma separated list of supported formats. */
2049 uint32_t cbOut = (uint32_t)strlen(H3DOR_FMT_RGBA_TOPDOWN) + 1
2050 + (uint32_t)strlen(H3DOR_FMT_RGBA) + 1;
2051 if (cbOut <= cbBuffer)
2052 {
2053 char *pch = (char *)pvBuffer;
2054 memcpy(pch, H3DOR_FMT_RGBA_TOPDOWN, strlen(H3DOR_FMT_RGBA_TOPDOWN));
2055 pch += strlen(H3DOR_FMT_RGBA_TOPDOWN);
2056 *pch++ = ',';
2057 memcpy(pch, H3DOR_FMT_RGBA, strlen(H3DOR_FMT_RGBA));
2058 pch += strlen(H3DOR_FMT_RGBA);
2059 *pch++ = '\0';
2060 }
2061 else
2062 {
2063 rc = VERR_BUFFER_OVERFLOW;
2064 }
2065 *pcbOut = cbOut;
2066 }
2067 else
2068 {
2069 rc = VERR_NOT_SUPPORTED;
2070 }
2071
2072 H3DORLOG(("H3DORContextProperty: %Rrc\n", rc));
2073 return rc;
2074}
2075
2076void ConsoleVRDPServer::remote3DRedirect(bool fEnable)
2077{
2078 if (!m_fInterfaceImage)
2079 {
2080 /* No redirect without corresponding interface. */
2081 return;
2082 }
2083
2084 /* Check if 3D redirection has been enabled. It is enabled by default. */
2085 com::Bstr bstr;
2086 HRESULT hrc = mConsole->i_getVRDEServer()->GetVRDEProperty(Bstr("H3DRedirect/Enabled").raw(), bstr.asOutParam());
2087
2088 com::Utf8Str value = hrc == S_OK? bstr: "";
2089
2090 bool fAllowed = RTStrICmp(value.c_str(), "true") == 0
2091 || RTStrICmp(value.c_str(), "1") == 0
2092 || value.c_str()[0] == 0;
2093
2094 if (!fAllowed && fEnable)
2095 {
2096 return;
2097 }
2098
2099 /* Tell the host 3D service to redirect output using the ConsoleVRDPServer callbacks. */
2100 H3DOUTPUTREDIRECT outputRedirect =
2101 {
2102 this,
2103 H3DORBegin,
2104 H3DORGeometry,
2105 H3DORVisibleRegion,
2106 H3DORFrame,
2107 H3DOREnd,
2108 H3DORContextProperty
2109 };
2110
2111 if (!fEnable)
2112 {
2113 /* This will tell the service to disable rediection. */
2114 RT_ZERO(outputRedirect);
2115 }
2116
2117#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
2118 VBOXCRCMDCTL_HGCM data;
2119 data.Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
2120 data.Hdr.u32Function = SHCRGL_HOST_FN_SET_OUTPUT_REDIRECT;
2121
2122 data.aParms[0].type = VBOX_HGCM_SVC_PARM_PTR;
2123 data.aParms[0].u.pointer.addr = &outputRedirect;
2124 data.aParms[0].u.pointer.size = sizeof(outputRedirect);
2125
2126 int rc = mConsole->i_getDisplay()->i_crCtlSubmitSync(&data.Hdr, sizeof (data));
2127 if (!RT_SUCCESS(rc))
2128 {
2129 Log(("SHCRGL_HOST_FN_SET_CONSOLE failed with %Rrc\n", rc));
2130 return;
2131 }
2132
2133 LogRel(("VRDE: %s 3D redirect.\n", fEnable? "Enabled": "Disabled"));
2134# ifdef DEBUG_misha
2135 AssertFailed();
2136# endif
2137#endif
2138
2139 return;
2140}
2141
2142/* static */ DECLCALLBACK(int) ConsoleVRDPServer::VRDEImageCbNotify (void *pvContext,
2143 void *pvUser,
2144 HVRDEIMAGE hVideo,
2145 uint32_t u32Id,
2146 void *pvData,
2147 uint32_t cbData)
2148{
2149 RT_NOREF(hVideo);
2150 H3DORLOG(("H3DOR: VRDEImageCbNotify: pvContext %p, pvUser %p, hVideo %p, u32Id %u, pvData %p, cbData %d\n",
2151 pvContext, pvUser, hVideo, u32Id, pvData, cbData));
2152
2153 ConsoleVRDPServer *pServer = static_cast<ConsoleVRDPServer*>(pvContext); NOREF(pServer);
2154 H3DORInstance *p = (H3DORInstance *)pvUser;
2155 Assert(p);
2156 Assert(p->pThis);
2157 Assert(p->pThis == pServer);
2158
2159 if (u32Id == VRDE_IMAGE_NOTIFY_HANDLE_CREATE)
2160 {
2161 if (cbData != sizeof(uint32_t))
2162 {
2163 AssertFailed();
2164 return VERR_INVALID_PARAMETER;
2165 }
2166
2167 uint32_t u32StreamId = *(uint32_t *)pvData;
2168 H3DORLOG(("H3DOR: VRDE_IMAGE_NOTIFY_HANDLE_CREATE u32StreamId %d\n",
2169 u32StreamId));
2170
2171 if (u32StreamId != 0)
2172 {
2173 p->fCreated = true; /// @todo not needed?
2174 }
2175 else
2176 {
2177 /* The stream has not been created. */
2178 }
2179 }
2180
2181 return VINF_SUCCESS;
2182}
2183
2184#undef H3DORLOG
2185
2186/* static */ DECLCALLBACK(int) ConsoleVRDPServer::VRDESCardCbNotify(void *pvContext,
2187 uint32_t u32Id,
2188 void *pvData,
2189 uint32_t cbData)
2190{
2191#ifdef VBOX_WITH_USB_CARDREADER
2192 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvContext);
2193 UsbCardReader *pReader = pThis->mConsole->i_getUsbCardReader();
2194 return pReader->VRDENotify(u32Id, pvData, cbData);
2195#else
2196 NOREF(pvContext);
2197 NOREF(u32Id);
2198 NOREF(pvData);
2199 NOREF(cbData);
2200 return VERR_NOT_SUPPORTED;
2201#endif
2202}
2203
2204/* static */ DECLCALLBACK(int) ConsoleVRDPServer::VRDESCardCbResponse(void *pvContext,
2205 int rcRequest,
2206 void *pvUser,
2207 uint32_t u32Function,
2208 void *pvData,
2209 uint32_t cbData)
2210{
2211#ifdef VBOX_WITH_USB_CARDREADER
2212 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvContext);
2213 UsbCardReader *pReader = pThis->mConsole->i_getUsbCardReader();
2214 return pReader->VRDEResponse(rcRequest, pvUser, u32Function, pvData, cbData);
2215#else
2216 NOREF(pvContext);
2217 NOREF(rcRequest);
2218 NOREF(pvUser);
2219 NOREF(u32Function);
2220 NOREF(pvData);
2221 NOREF(cbData);
2222 return VERR_NOT_SUPPORTED;
2223#endif
2224}
2225
2226int ConsoleVRDPServer::SCardRequest(void *pvUser, uint32_t u32Function, const void *pvData, uint32_t cbData)
2227{
2228 int rc = VINF_SUCCESS;
2229
2230 if (mhServer && mpEntryPoints && m_interfaceSCard.VRDESCardRequest)
2231 {
2232 rc = m_interfaceSCard.VRDESCardRequest(mhServer, pvUser, u32Function, pvData, cbData);
2233 }
2234 else
2235 {
2236 rc = VERR_NOT_SUPPORTED;
2237 }
2238
2239 return rc;
2240}
2241
2242
2243struct TSMFHOSTCHCTX;
2244struct TSMFVRDPCTX;
2245
2246typedef struct TSMFHOSTCHCTX
2247{
2248 ConsoleVRDPServer *pThis;
2249
2250 struct TSMFVRDPCTX *pVRDPCtx; /* NULL if no corresponding host channel context. */
2251
2252 void *pvDataReceived;
2253 uint32_t cbDataReceived;
2254 uint32_t cbDataAllocated;
2255} TSMFHOSTCHCTX;
2256
2257typedef struct TSMFVRDPCTX
2258{
2259 ConsoleVRDPServer *pThis;
2260
2261 VBOXHOSTCHANNELCALLBACKS *pCallbacks;
2262 void *pvCallbacks;
2263
2264 TSMFHOSTCHCTX *pHostChCtx; /* NULL if no corresponding host channel context. */
2265
2266 uint32_t u32ChannelHandle;
2267} TSMFVRDPCTX;
2268
2269static int tsmfContextsAlloc(TSMFHOSTCHCTX **ppHostChCtx, TSMFVRDPCTX **ppVRDPCtx)
2270{
2271 TSMFHOSTCHCTX *pHostChCtx = (TSMFHOSTCHCTX *)RTMemAllocZ(sizeof(TSMFHOSTCHCTX));
2272 if (!pHostChCtx)
2273 {
2274 return VERR_NO_MEMORY;
2275 }
2276
2277 TSMFVRDPCTX *pVRDPCtx = (TSMFVRDPCTX *)RTMemAllocZ(sizeof(TSMFVRDPCTX));
2278 if (!pVRDPCtx)
2279 {
2280 RTMemFree(pHostChCtx);
2281 return VERR_NO_MEMORY;
2282 }
2283
2284 *ppHostChCtx = pHostChCtx;
2285 *ppVRDPCtx = pVRDPCtx;
2286 return VINF_SUCCESS;
2287}
2288
2289int ConsoleVRDPServer::tsmfLock(void)
2290{
2291 int rc = RTCritSectEnter(&mTSMFLock);
2292 AssertRC(rc);
2293 return rc;
2294}
2295
2296void ConsoleVRDPServer::tsmfUnlock(void)
2297{
2298 RTCritSectLeave(&mTSMFLock);
2299}
2300
2301/* static */ DECLCALLBACK(int) ConsoleVRDPServer::tsmfHostChannelAttach(void *pvProvider,
2302 void **ppvChannel,
2303 uint32_t u32Flags,
2304 VBOXHOSTCHANNELCALLBACKS *pCallbacks,
2305 void *pvCallbacks)
2306{
2307 LogFlowFunc(("\n"));
2308
2309 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvProvider);
2310
2311 /* Create 2 context structures: for the VRDP server and for the host service. */
2312 TSMFHOSTCHCTX *pHostChCtx = NULL;
2313 TSMFVRDPCTX *pVRDPCtx = NULL;
2314
2315 int rc = tsmfContextsAlloc(&pHostChCtx, &pVRDPCtx);
2316 if (RT_FAILURE(rc))
2317 {
2318 return rc;
2319 }
2320
2321 pHostChCtx->pThis = pThis;
2322 pHostChCtx->pVRDPCtx = pVRDPCtx;
2323
2324 pVRDPCtx->pThis = pThis;
2325 pVRDPCtx->pCallbacks = pCallbacks;
2326 pVRDPCtx->pvCallbacks = pvCallbacks;
2327 pVRDPCtx->pHostChCtx = pHostChCtx;
2328
2329 rc = pThis->m_interfaceTSMF.VRDETSMFChannelCreate(pThis->mhServer, pVRDPCtx, u32Flags);
2330
2331 if (RT_SUCCESS(rc))
2332 {
2333 /** @todo contexts should be in a list for accounting. */
2334 *ppvChannel = pHostChCtx;
2335 }
2336 else
2337 {
2338 RTMemFree(pHostChCtx);
2339 RTMemFree(pVRDPCtx);
2340 }
2341
2342 return rc;
2343}
2344
2345/* static */ DECLCALLBACK(void) ConsoleVRDPServer::tsmfHostChannelDetach(void *pvChannel)
2346{
2347 LogFlowFunc(("\n"));
2348
2349 TSMFHOSTCHCTX *pHostChCtx = (TSMFHOSTCHCTX *)pvChannel;
2350 ConsoleVRDPServer *pThis = pHostChCtx->pThis;
2351
2352 int rc = pThis->tsmfLock();
2353 if (RT_SUCCESS(rc))
2354 {
2355 bool fClose = false;
2356 uint32_t u32ChannelHandle = 0;
2357
2358 if (pHostChCtx->pVRDPCtx)
2359 {
2360 /* There is still a VRDP context for this channel. */
2361 pHostChCtx->pVRDPCtx->pHostChCtx = NULL;
2362 u32ChannelHandle = pHostChCtx->pVRDPCtx->u32ChannelHandle;
2363 fClose = true;
2364 }
2365
2366 pThis->tsmfUnlock();
2367
2368 RTMemFree(pHostChCtx);
2369
2370 if (fClose)
2371 {
2372 LogFlowFunc(("Closing VRDE channel %d.\n", u32ChannelHandle));
2373 pThis->m_interfaceTSMF.VRDETSMFChannelClose(pThis->mhServer, u32ChannelHandle);
2374 }
2375 else
2376 {
2377 LogFlowFunc(("No VRDE channel.\n"));
2378 }
2379 }
2380}
2381
2382/* static */ DECLCALLBACK(int) ConsoleVRDPServer::tsmfHostChannelSend(void *pvChannel,
2383 const void *pvData,
2384 uint32_t cbData)
2385{
2386 LogFlowFunc(("cbData %d\n", cbData));
2387
2388 TSMFHOSTCHCTX *pHostChCtx = (TSMFHOSTCHCTX *)pvChannel;
2389 ConsoleVRDPServer *pThis = pHostChCtx->pThis;
2390
2391 int rc = pThis->tsmfLock();
2392 if (RT_SUCCESS(rc))
2393 {
2394 bool fSend = false;
2395 uint32_t u32ChannelHandle = 0;
2396
2397 if (pHostChCtx->pVRDPCtx)
2398 {
2399 u32ChannelHandle = pHostChCtx->pVRDPCtx->u32ChannelHandle;
2400 fSend = true;
2401 }
2402
2403 pThis->tsmfUnlock();
2404
2405 if (fSend)
2406 {
2407 LogFlowFunc(("Send to VRDE channel %d.\n", u32ChannelHandle));
2408 rc = pThis->m_interfaceTSMF.VRDETSMFChannelSend(pThis->mhServer, u32ChannelHandle,
2409 pvData, cbData);
2410 }
2411 }
2412
2413 return rc;
2414}
2415
2416/* static */ DECLCALLBACK(int) ConsoleVRDPServer::tsmfHostChannelRecv(void *pvChannel,
2417 void *pvData,
2418 uint32_t cbData,
2419 uint32_t *pcbReceived,
2420 uint32_t *pcbRemaining)
2421{
2422 LogFlowFunc(("cbData %d\n", cbData));
2423
2424 TSMFHOSTCHCTX *pHostChCtx = (TSMFHOSTCHCTX *)pvChannel;
2425 ConsoleVRDPServer *pThis = pHostChCtx->pThis;
2426
2427 int rc = pThis->tsmfLock();
2428 if (RT_SUCCESS(rc))
2429 {
2430 uint32_t cbToCopy = RT_MIN(cbData, pHostChCtx->cbDataReceived);
2431 uint32_t cbRemaining = pHostChCtx->cbDataReceived - cbToCopy;
2432
2433 LogFlowFunc(("cbToCopy %d, cbRemaining %d\n", cbToCopy, cbRemaining));
2434
2435 if (cbToCopy != 0)
2436 {
2437 memcpy(pvData, pHostChCtx->pvDataReceived, cbToCopy);
2438
2439 if (cbRemaining != 0)
2440 {
2441 memmove(pHostChCtx->pvDataReceived, (uint8_t *)pHostChCtx->pvDataReceived + cbToCopy, cbRemaining);
2442 }
2443
2444 pHostChCtx->cbDataReceived = cbRemaining;
2445 }
2446
2447 pThis->tsmfUnlock();
2448
2449 *pcbRemaining = cbRemaining;
2450 *pcbReceived = cbToCopy;
2451 }
2452
2453 return rc;
2454}
2455
2456/* static */ DECLCALLBACK(int) ConsoleVRDPServer::tsmfHostChannelControl(void *pvChannel,
2457 uint32_t u32Code,
2458 const void *pvParm,
2459 uint32_t cbParm,
2460 const void *pvData,
2461 uint32_t cbData,
2462 uint32_t *pcbDataReturned)
2463{
2464 RT_NOREF(pvParm, cbParm, pvData, cbData);
2465 LogFlowFunc(("u32Code %u\n", u32Code));
2466
2467 if (!pvChannel)
2468 {
2469 /* Special case, the provider must answer rather than a channel instance. */
2470 if (u32Code == VBOX_HOST_CHANNEL_CTRL_EXISTS)
2471 {
2472 *pcbDataReturned = 0;
2473 return VINF_SUCCESS;
2474 }
2475
2476 return VERR_NOT_IMPLEMENTED;
2477 }
2478
2479 /* Channels do not support this. */
2480 return VERR_NOT_IMPLEMENTED;
2481}
2482
2483
2484void ConsoleVRDPServer::setupTSMF(void)
2485{
2486 if (m_interfaceTSMF.header.u64Size == 0)
2487 {
2488 return;
2489 }
2490
2491 /* Register with the host channel service. */
2492 VBOXHOSTCHANNELINTERFACE hostChannelInterface =
2493 {
2494 this,
2495 tsmfHostChannelAttach,
2496 tsmfHostChannelDetach,
2497 tsmfHostChannelSend,
2498 tsmfHostChannelRecv,
2499 tsmfHostChannelControl
2500 };
2501
2502 VBoxHostChannelHostRegister parms;
2503
2504 static char szProviderName[] = "/vrde/tsmf";
2505
2506 parms.name.type = VBOX_HGCM_SVC_PARM_PTR;
2507 parms.name.u.pointer.addr = &szProviderName[0];
2508 parms.name.u.pointer.size = sizeof(szProviderName);
2509
2510 parms.iface.type = VBOX_HGCM_SVC_PARM_PTR;
2511 parms.iface.u.pointer.addr = &hostChannelInterface;
2512 parms.iface.u.pointer.size = sizeof(hostChannelInterface);
2513
2514 VMMDev *pVMMDev = mConsole->i_getVMMDev();
2515
2516 if (!pVMMDev)
2517 {
2518 AssertMsgFailed(("setupTSMF no vmmdev\n"));
2519 return;
2520 }
2521
2522 int rc = pVMMDev->hgcmHostCall("VBoxHostChannel",
2523 VBOX_HOST_CHANNEL_HOST_FN_REGISTER,
2524 2,
2525 &parms.name);
2526
2527 if (!RT_SUCCESS(rc))
2528 {
2529 Log(("VBOX_HOST_CHANNEL_HOST_FN_REGISTER failed with %Rrc\n", rc));
2530 return;
2531 }
2532
2533 LogRel(("VRDE: Enabled TSMF channel.\n"));
2534
2535 return;
2536}
2537
2538/** @todo these defines must be in a header, which is used by guest component as well. */
2539#define VBOX_TSMF_HCH_CREATE_ACCEPTED (VBOX_HOST_CHANNEL_EVENT_USER + 0)
2540#define VBOX_TSMF_HCH_CREATE_DECLINED (VBOX_HOST_CHANNEL_EVENT_USER + 1)
2541#define VBOX_TSMF_HCH_DISCONNECTED (VBOX_HOST_CHANNEL_EVENT_USER + 2)
2542
2543/* static */ DECLCALLBACK(void) ConsoleVRDPServer::VRDETSMFCbNotify(void *pvContext,
2544 uint32_t u32Notification,
2545 void *pvChannel,
2546 const void *pvParm,
2547 uint32_t cbParm)
2548{
2549 RT_NOREF(cbParm);
2550 int rc = VINF_SUCCESS;
2551
2552 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvContext);
2553
2554 TSMFVRDPCTX *pVRDPCtx = (TSMFVRDPCTX *)pvChannel;
2555
2556 Assert(pVRDPCtx->pThis == pThis);
2557
2558 if (pVRDPCtx->pCallbacks == NULL)
2559 {
2560 LogFlowFunc(("tsmfHostChannel: Channel disconnected. Skipping.\n"));
2561 return;
2562 }
2563
2564 switch (u32Notification)
2565 {
2566 case VRDE_TSMF_N_CREATE_ACCEPTED:
2567 {
2568 VRDETSMFNOTIFYCREATEACCEPTED *p = (VRDETSMFNOTIFYCREATEACCEPTED *)pvParm;
2569 Assert(cbParm == sizeof(VRDETSMFNOTIFYCREATEACCEPTED));
2570
2571 LogFlowFunc(("tsmfHostChannel: VRDE_TSMF_N_CREATE_ACCEPTED(%p): p->u32ChannelHandle %d\n",
2572 pVRDPCtx, p->u32ChannelHandle));
2573
2574 pVRDPCtx->u32ChannelHandle = p->u32ChannelHandle;
2575
2576 pVRDPCtx->pCallbacks->HostChannelCallbackEvent(pVRDPCtx->pvCallbacks, pVRDPCtx->pHostChCtx,
2577 VBOX_TSMF_HCH_CREATE_ACCEPTED,
2578 NULL, 0);
2579 } break;
2580
2581 case VRDE_TSMF_N_CREATE_DECLINED:
2582 {
2583 LogFlowFunc(("tsmfHostChannel: VRDE_TSMF_N_CREATE_DECLINED(%p)\n", pVRDPCtx));
2584
2585 pVRDPCtx->pCallbacks->HostChannelCallbackEvent(pVRDPCtx->pvCallbacks, pVRDPCtx->pHostChCtx,
2586 VBOX_TSMF_HCH_CREATE_DECLINED,
2587 NULL, 0);
2588 } break;
2589
2590 case VRDE_TSMF_N_DATA:
2591 {
2592 /* Save the data in the intermediate buffer and send the event. */
2593 VRDETSMFNOTIFYDATA *p = (VRDETSMFNOTIFYDATA *)pvParm;
2594 Assert(cbParm == sizeof(VRDETSMFNOTIFYDATA));
2595
2596 LogFlowFunc(("tsmfHostChannel: VRDE_TSMF_N_DATA(%p): p->cbData %d\n", pVRDPCtx, p->cbData));
2597
2598 VBOXHOSTCHANNELEVENTRECV ev;
2599 ev.u32SizeAvailable = 0;
2600
2601 rc = pThis->tsmfLock();
2602
2603 if (RT_SUCCESS(rc))
2604 {
2605 TSMFHOSTCHCTX *pHostChCtx = pVRDPCtx->pHostChCtx;
2606
2607 if (pHostChCtx)
2608 {
2609 if (pHostChCtx->pvDataReceived)
2610 {
2611 uint32_t cbAlloc = p->cbData + pHostChCtx->cbDataReceived;
2612 pHostChCtx->pvDataReceived = RTMemRealloc(pHostChCtx->pvDataReceived, cbAlloc);
2613 memcpy((uint8_t *)pHostChCtx->pvDataReceived + pHostChCtx->cbDataReceived, p->pvData, p->cbData);
2614
2615 pHostChCtx->cbDataReceived += p->cbData;
2616 pHostChCtx->cbDataAllocated = cbAlloc;
2617 }
2618 else
2619 {
2620 pHostChCtx->pvDataReceived = RTMemAlloc(p->cbData);
2621 memcpy(pHostChCtx->pvDataReceived, p->pvData, p->cbData);
2622
2623 pHostChCtx->cbDataReceived = p->cbData;
2624 pHostChCtx->cbDataAllocated = p->cbData;
2625 }
2626
2627 ev.u32SizeAvailable = p->cbData;
2628 }
2629 else
2630 {
2631 LogFlowFunc(("tsmfHostChannel: VRDE_TSMF_N_DATA: no host channel. Skipping\n"));
2632 }
2633
2634 pThis->tsmfUnlock();
2635 }
2636
2637 pVRDPCtx->pCallbacks->HostChannelCallbackEvent(pVRDPCtx->pvCallbacks, pVRDPCtx->pHostChCtx,
2638 VBOX_HOST_CHANNEL_EVENT_RECV,
2639 &ev, sizeof(ev));
2640 } break;
2641
2642 case VRDE_TSMF_N_DISCONNECTED:
2643 {
2644 LogFlowFunc(("tsmfHostChannel: VRDE_TSMF_N_DISCONNECTED(%p)\n", pVRDPCtx));
2645
2646 pVRDPCtx->pCallbacks->HostChannelCallbackEvent(pVRDPCtx->pvCallbacks, pVRDPCtx->pHostChCtx,
2647 VBOX_TSMF_HCH_DISCONNECTED,
2648 NULL, 0);
2649
2650 /* The callback context will not be used anymore. */
2651 pVRDPCtx->pCallbacks->HostChannelCallbackDeleted(pVRDPCtx->pvCallbacks, pVRDPCtx->pHostChCtx);
2652 pVRDPCtx->pCallbacks = NULL;
2653 pVRDPCtx->pvCallbacks = NULL;
2654
2655 rc = pThis->tsmfLock();
2656 if (RT_SUCCESS(rc))
2657 {
2658 if (pVRDPCtx->pHostChCtx)
2659 {
2660 /* There is still a host channel context for this channel. */
2661 pVRDPCtx->pHostChCtx->pVRDPCtx = NULL;
2662 }
2663
2664 pThis->tsmfUnlock();
2665
2666 RT_ZERO(*pVRDPCtx);
2667 RTMemFree(pVRDPCtx);
2668 }
2669 } break;
2670
2671 default:
2672 {
2673 AssertFailed();
2674 } break;
2675 }
2676}
2677
2678/* static */ DECLCALLBACK(void) ConsoleVRDPServer::VRDECallbackVideoInNotify(void *pvCallback,
2679 uint32_t u32Id,
2680 const void *pvData,
2681 uint32_t cbData)
2682{
2683 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvCallback);
2684 if (pThis->mEmWebcam)
2685 {
2686 pThis->mEmWebcam->EmWebcamCbNotify(u32Id, pvData, cbData);
2687 }
2688}
2689
2690/* static */ DECLCALLBACK(void) ConsoleVRDPServer::VRDECallbackVideoInDeviceDesc(void *pvCallback,
2691 int rcRequest,
2692 void *pDeviceCtx,
2693 void *pvUser,
2694 const VRDEVIDEOINDEVICEDESC *pDeviceDesc,
2695 uint32_t cbDevice)
2696{
2697 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvCallback);
2698 if (pThis->mEmWebcam)
2699 {
2700 pThis->mEmWebcam->EmWebcamCbDeviceDesc(rcRequest, pDeviceCtx, pvUser, pDeviceDesc, cbDevice);
2701 }
2702}
2703
2704/* static */ DECLCALLBACK(void) ConsoleVRDPServer::VRDECallbackVideoInControl(void *pvCallback,
2705 int rcRequest,
2706 void *pDeviceCtx,
2707 void *pvUser,
2708 const VRDEVIDEOINCTRLHDR *pControl,
2709 uint32_t cbControl)
2710{
2711 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvCallback);
2712 if (pThis->mEmWebcam)
2713 {
2714 pThis->mEmWebcam->EmWebcamCbControl(rcRequest, pDeviceCtx, pvUser, pControl, cbControl);
2715 }
2716}
2717
2718/* static */ DECLCALLBACK(void) ConsoleVRDPServer::VRDECallbackVideoInFrame(void *pvCallback,
2719 int rcRequest,
2720 void *pDeviceCtx,
2721 const VRDEVIDEOINPAYLOADHDR *pFrame,
2722 uint32_t cbFrame)
2723{
2724 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvCallback);
2725 if (pThis->mEmWebcam)
2726 {
2727 pThis->mEmWebcam->EmWebcamCbFrame(rcRequest, pDeviceCtx, pFrame, cbFrame);
2728 }
2729}
2730
2731int ConsoleVRDPServer::VideoInDeviceAttach(const VRDEVIDEOINDEVICEHANDLE *pDeviceHandle, void *pvDeviceCtx)
2732{
2733 int rc;
2734
2735 if (mhServer && mpEntryPoints && m_interfaceVideoIn.VRDEVideoInDeviceAttach)
2736 {
2737 rc = m_interfaceVideoIn.VRDEVideoInDeviceAttach(mhServer, pDeviceHandle, pvDeviceCtx);
2738 }
2739 else
2740 {
2741 rc = VERR_NOT_SUPPORTED;
2742 }
2743
2744 return rc;
2745}
2746
2747int ConsoleVRDPServer::VideoInDeviceDetach(const VRDEVIDEOINDEVICEHANDLE *pDeviceHandle)
2748{
2749 int rc;
2750
2751 if (mhServer && mpEntryPoints && m_interfaceVideoIn.VRDEVideoInDeviceDetach)
2752 {
2753 rc = m_interfaceVideoIn.VRDEVideoInDeviceDetach(mhServer, pDeviceHandle);
2754 }
2755 else
2756 {
2757 rc = VERR_NOT_SUPPORTED;
2758 }
2759
2760 return rc;
2761}
2762
2763int ConsoleVRDPServer::VideoInGetDeviceDesc(void *pvUser, const VRDEVIDEOINDEVICEHANDLE *pDeviceHandle)
2764{
2765 int rc;
2766
2767 if (mhServer && mpEntryPoints && m_interfaceVideoIn.VRDEVideoInGetDeviceDesc)
2768 {
2769 rc = m_interfaceVideoIn.VRDEVideoInGetDeviceDesc(mhServer, pvUser, pDeviceHandle);
2770 }
2771 else
2772 {
2773 rc = VERR_NOT_SUPPORTED;
2774 }
2775
2776 return rc;
2777}
2778
2779int ConsoleVRDPServer::VideoInControl(void *pvUser, const VRDEVIDEOINDEVICEHANDLE *pDeviceHandle,
2780 const VRDEVIDEOINCTRLHDR *pReq, uint32_t cbReq)
2781{
2782 int rc;
2783
2784 if (mhServer && mpEntryPoints && m_interfaceVideoIn.VRDEVideoInControl)
2785 {
2786 rc = m_interfaceVideoIn.VRDEVideoInControl(mhServer, pvUser, pDeviceHandle, pReq, cbReq);
2787 }
2788 else
2789 {
2790 rc = VERR_NOT_SUPPORTED;
2791 }
2792
2793 return rc;
2794}
2795
2796
2797/* static */ DECLCALLBACK(void) ConsoleVRDPServer::VRDECallbackInputSetup(void *pvCallback,
2798 int rcRequest,
2799 uint32_t u32Method,
2800 const void *pvResult,
2801 uint32_t cbResult)
2802{
2803 NOREF(pvCallback);
2804 NOREF(rcRequest);
2805 NOREF(u32Method);
2806 NOREF(pvResult);
2807 NOREF(cbResult);
2808}
2809
2810/* static */ DECLCALLBACK(void) ConsoleVRDPServer::VRDECallbackInputEvent(void *pvCallback,
2811 uint32_t u32Method,
2812 const void *pvEvent,
2813 uint32_t cbEvent)
2814{
2815 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvCallback);
2816
2817 if (u32Method == VRDE_INPUT_METHOD_TOUCH)
2818 {
2819 if (cbEvent >= sizeof(VRDEINPUTHEADER))
2820 {
2821 VRDEINPUTHEADER *pHeader = (VRDEINPUTHEADER *)pvEvent;
2822
2823 if (pHeader->u16EventId == VRDEINPUT_EVENTID_TOUCH)
2824 {
2825 IMouse *pMouse = pThis->mConsole->i_getMouse();
2826
2827 VRDEINPUT_TOUCH_EVENT_PDU *p = (VRDEINPUT_TOUCH_EVENT_PDU *)pHeader;
2828
2829 uint16_t iFrame;
2830 for (iFrame = 0; iFrame < p->u16FrameCount; iFrame++)
2831 {
2832 VRDEINPUT_TOUCH_FRAME *pFrame = &p->aFrames[iFrame];
2833
2834 com::SafeArray<LONG64> aContacts(pFrame->u16ContactCount);
2835
2836 uint16_t iContact;
2837 for (iContact = 0; iContact < pFrame->u16ContactCount; iContact++)
2838 {
2839 VRDEINPUT_CONTACT_DATA *pContact = &pFrame->aContacts[iContact];
2840
2841 int16_t x = (int16_t)(pContact->i32X + 1);
2842 int16_t y = (int16_t)(pContact->i32Y + 1);
2843 uint8_t contactId = pContact->u8ContactId;
2844 uint8_t contactState = TouchContactState_None;
2845
2846 if (pContact->u32ContactFlags & VRDEINPUT_CONTACT_FLAG_INRANGE)
2847 {
2848 contactState |= TouchContactState_InRange;
2849 }
2850 if (pContact->u32ContactFlags & VRDEINPUT_CONTACT_FLAG_INCONTACT)
2851 {
2852 contactState |= TouchContactState_InContact;
2853 }
2854
2855 aContacts[iContact] = RT_MAKE_U64_FROM_U16((uint16_t)x,
2856 (uint16_t)y,
2857 RT_MAKE_U16(contactId, contactState),
2858 0);
2859 }
2860
2861 if (pFrame->u64FrameOffset == 0)
2862 {
2863 pThis->mu64TouchInputTimestampMCS = 0;
2864 }
2865 else
2866 {
2867 pThis->mu64TouchInputTimestampMCS += pFrame->u64FrameOffset;
2868 }
2869
2870 pMouse->PutEventMultiTouch(pFrame->u16ContactCount,
2871 ComSafeArrayAsInParam(aContacts),
2872 (ULONG)(pThis->mu64TouchInputTimestampMCS / 1000)); /* Micro->milliseconds. */
2873 }
2874 }
2875 else if (pHeader->u16EventId == VRDEINPUT_EVENTID_DISMISS_HOVERING_CONTACT)
2876 {
2877 /** @todo */
2878 }
2879 else
2880 {
2881 AssertMsgFailed(("EventId %d\n", pHeader->u16EventId));
2882 }
2883 }
2884 }
2885}
2886
2887
2888void ConsoleVRDPServer::EnableConnections(void)
2889{
2890 if (mpEntryPoints && mhServer)
2891 {
2892 mpEntryPoints->VRDEEnableConnections(mhServer, true);
2893
2894 /* Setup the generic TSMF channel. */
2895 setupTSMF();
2896 }
2897}
2898
2899void ConsoleVRDPServer::DisconnectClient(uint32_t u32ClientId, bool fReconnect)
2900{
2901 if (mpEntryPoints && mhServer)
2902 {
2903 mpEntryPoints->VRDEDisconnect(mhServer, u32ClientId, fReconnect);
2904 }
2905}
2906
2907int ConsoleVRDPServer::MousePointer(BOOL alpha,
2908 ULONG xHot,
2909 ULONG yHot,
2910 ULONG width,
2911 ULONG height,
2912 const uint8_t *pu8Shape)
2913{
2914 int rc = VINF_SUCCESS;
2915
2916 if (mhServer && mpEntryPoints && m_interfaceMousePtr.VRDEMousePtr)
2917 {
2918 size_t cbMask = (((width + 7) / 8) * height + 3) & ~3;
2919 size_t cbData = width * height * 4;
2920
2921 size_t cbDstMask = alpha? 0: cbMask;
2922
2923 size_t cbPointer = sizeof(VRDEMOUSEPTRDATA) + cbDstMask + cbData;
2924 uint8_t *pu8Pointer = (uint8_t *)RTMemAlloc(cbPointer);
2925 if (pu8Pointer != NULL)
2926 {
2927 VRDEMOUSEPTRDATA *pPointer = (VRDEMOUSEPTRDATA *)pu8Pointer;
2928
2929 pPointer->u16HotX = (uint16_t)xHot;
2930 pPointer->u16HotY = (uint16_t)yHot;
2931 pPointer->u16Width = (uint16_t)width;
2932 pPointer->u16Height = (uint16_t)height;
2933 pPointer->u16MaskLen = (uint16_t)cbDstMask;
2934 pPointer->u32DataLen = (uint32_t)cbData;
2935
2936 /* AND mask. */
2937 uint8_t *pu8Mask = pu8Pointer + sizeof(VRDEMOUSEPTRDATA);
2938 if (cbDstMask)
2939 {
2940 memcpy(pu8Mask, pu8Shape, cbDstMask);
2941 }
2942
2943 /* XOR mask */
2944 uint8_t *pu8Data = pu8Mask + pPointer->u16MaskLen;
2945 memcpy(pu8Data, pu8Shape + cbMask, cbData);
2946
2947 m_interfaceMousePtr.VRDEMousePtr(mhServer, pPointer);
2948
2949 RTMemFree(pu8Pointer);
2950 }
2951 else
2952 {
2953 rc = VERR_NO_MEMORY;
2954 }
2955 }
2956 else
2957 {
2958 rc = VERR_NOT_SUPPORTED;
2959 }
2960
2961 return rc;
2962}
2963
2964void ConsoleVRDPServer::MousePointerUpdate(const VRDECOLORPOINTER *pPointer)
2965{
2966 if (mpEntryPoints && mhServer)
2967 {
2968 mpEntryPoints->VRDEColorPointer(mhServer, pPointer);
2969 }
2970}
2971
2972void ConsoleVRDPServer::MousePointerHide(void)
2973{
2974 if (mpEntryPoints && mhServer)
2975 {
2976 mpEntryPoints->VRDEHidePointer(mhServer);
2977 }
2978}
2979
2980void ConsoleVRDPServer::Stop(void)
2981{
2982 Assert(VALID_PTR(this)); /** @todo r=bird: there are(/was) some odd cases where this buster was invalid on
2983 * linux. Just remove this when it's 100% sure that problem has been fixed. */
2984
2985#ifdef VBOX_WITH_USB
2986 remoteUSBThreadStop();
2987#endif /* VBOX_WITH_USB */
2988
2989 if (mhServer)
2990 {
2991 HVRDESERVER hServer = mhServer;
2992
2993 /* Reset the handle to avoid further calls to the server. */
2994 mhServer = 0;
2995
2996 /* Workaround for VM process hangs on termination.
2997 *
2998 * Make sure that the server is not currently processing a resize.
2999 * mhServer 0 will not allow to enter the server again.
3000 * Wait until any current resize returns from the server.
3001 */
3002 if (mcInResize)
3003 {
3004 LogRel(("VRDP: waiting for resize %d\n", mcInResize));
3005
3006 int i = 0;
3007 while (mcInResize && ++i < 100)
3008 {
3009 RTThreadSleep(10);
3010 }
3011 }
3012
3013 if (mpEntryPoints && hServer)
3014 {
3015 mpEntryPoints->VRDEDestroy(hServer);
3016 }
3017 }
3018
3019#ifndef VBOX_WITH_VRDEAUTH_IN_VBOXSVC
3020 AuthLibUnload(&mAuthLibCtx);
3021#endif
3022}
3023
3024/* Worker thread for Remote USB. The thread polls the clients for
3025 * the list of attached USB devices.
3026 * The thread is also responsible for attaching/detaching devices
3027 * to/from the VM.
3028 *
3029 * It is expected that attaching/detaching is not a frequent operation.
3030 *
3031 * The thread is always running when the VRDP server is active.
3032 *
3033 * The thread scans backends and requests the device list every 2 seconds.
3034 *
3035 * When device list is available, the thread calls the Console to process it.
3036 *
3037 */
3038#define VRDP_DEVICE_LIST_PERIOD_MS (2000)
3039
3040#ifdef VBOX_WITH_USB
3041static DECLCALLBACK(int) threadRemoteUSB(RTTHREAD self, void *pvUser)
3042{
3043 ConsoleVRDPServer *pOwner = (ConsoleVRDPServer *)pvUser;
3044
3045 LogFlow(("Console::threadRemoteUSB: start. owner = %p.\n", pOwner));
3046
3047 pOwner->notifyRemoteUSBThreadRunning(self);
3048
3049 while (pOwner->isRemoteUSBThreadRunning())
3050 {
3051 RemoteUSBBackend *pRemoteUSBBackend = NULL;
3052
3053 while ((pRemoteUSBBackend = pOwner->usbBackendGetNext(pRemoteUSBBackend)) != NULL)
3054 {
3055 pRemoteUSBBackend->PollRemoteDevices();
3056 }
3057
3058 pOwner->waitRemoteUSBThreadEvent(VRDP_DEVICE_LIST_PERIOD_MS);
3059
3060 LogFlow(("Console::threadRemoteUSB: iteration. owner = %p.\n", pOwner));
3061 }
3062
3063 return VINF_SUCCESS;
3064}
3065
3066void ConsoleVRDPServer::notifyRemoteUSBThreadRunning(RTTHREAD thread)
3067{
3068 mUSBBackends.thread = thread;
3069 mUSBBackends.fThreadRunning = true;
3070 int rc = RTThreadUserSignal(thread);
3071 AssertRC(rc);
3072}
3073
3074bool ConsoleVRDPServer::isRemoteUSBThreadRunning(void)
3075{
3076 return mUSBBackends.fThreadRunning;
3077}
3078
3079void ConsoleVRDPServer::waitRemoteUSBThreadEvent(RTMSINTERVAL cMillies)
3080{
3081 int rc = RTSemEventWait(mUSBBackends.event, cMillies);
3082 Assert(RT_SUCCESS(rc) || rc == VERR_TIMEOUT);
3083 NOREF(rc);
3084}
3085
3086void ConsoleVRDPServer::remoteUSBThreadStart(void)
3087{
3088 int rc = RTSemEventCreate(&mUSBBackends.event);
3089
3090 if (RT_FAILURE(rc))
3091 {
3092 AssertFailed();
3093 mUSBBackends.event = 0;
3094 }
3095
3096 if (RT_SUCCESS(rc))
3097 {
3098 rc = RTThreadCreate(&mUSBBackends.thread, threadRemoteUSB, this, 65536,
3099 RTTHREADTYPE_VRDP_IO, RTTHREADFLAGS_WAITABLE, "remote usb");
3100 }
3101
3102 if (RT_FAILURE(rc))
3103 {
3104 LogRel(("Warning: could not start the remote USB thread, rc = %Rrc!!!\n", rc));
3105 mUSBBackends.thread = NIL_RTTHREAD;
3106 }
3107 else
3108 {
3109 /* Wait until the thread is ready. */
3110 rc = RTThreadUserWait(mUSBBackends.thread, 60000);
3111 AssertRC(rc);
3112 Assert (mUSBBackends.fThreadRunning || RT_FAILURE(rc));
3113 }
3114}
3115
3116void ConsoleVRDPServer::remoteUSBThreadStop(void)
3117{
3118 mUSBBackends.fThreadRunning = false;
3119
3120 if (mUSBBackends.thread != NIL_RTTHREAD)
3121 {
3122 Assert (mUSBBackends.event != 0);
3123
3124 RTSemEventSignal(mUSBBackends.event);
3125
3126 int rc = RTThreadWait(mUSBBackends.thread, 60000, NULL);
3127 AssertRC(rc);
3128
3129 mUSBBackends.thread = NIL_RTTHREAD;
3130 }
3131
3132 if (mUSBBackends.event)
3133 {
3134 RTSemEventDestroy(mUSBBackends.event);
3135 mUSBBackends.event = 0;
3136 }
3137}
3138#endif /* VBOX_WITH_USB */
3139
3140AuthResult ConsoleVRDPServer::Authenticate(const Guid &uuid, AuthGuestJudgement guestJudgement,
3141 const char *pszUser, const char *pszPassword, const char *pszDomain,
3142 uint32_t u32ClientId)
3143{
3144 LogFlowFunc(("uuid = %RTuuid, guestJudgement = %d, pszUser = %s, pszPassword = %s, pszDomain = %s, u32ClientId = %d\n",
3145 uuid.raw(), guestJudgement, pszUser, pszPassword, pszDomain, u32ClientId));
3146
3147 AuthResult result = AuthResultAccessDenied;
3148
3149#ifdef VBOX_WITH_VRDEAUTH_IN_VBOXSVC
3150 try
3151 {
3152 /* Init auth parameters. Order is important. */
3153 SafeArray<BSTR> authParams;
3154 Bstr("VRDEAUTH" ).detachTo(authParams.appendedRaw());
3155 Bstr(uuid.toUtf16() ).detachTo(authParams.appendedRaw());
3156 BstrFmt("%u", guestJudgement).detachTo(authParams.appendedRaw());
3157 Bstr(pszUser ).detachTo(authParams.appendedRaw());
3158 Bstr(pszPassword ).detachTo(authParams.appendedRaw());
3159 Bstr(pszDomain ).detachTo(authParams.appendedRaw());
3160 BstrFmt("%u", u32ClientId).detachTo(authParams.appendedRaw());
3161
3162 Bstr authResult;
3163 HRESULT hr = mConsole->mControl->AuthenticateExternal(ComSafeArrayAsInParam(authParams),
3164 authResult.asOutParam());
3165 LogFlowFunc(("%Rhrc [%ls]\n", hr, authResult.raw()));
3166
3167 size_t cbPassword = RTUtf16Len((PRTUTF16)authParams[4]) * sizeof(RTUTF16);
3168 if (cbPassword)
3169 RTMemWipeThoroughly(authParams[4], cbPassword, 10 /* cPasses */);
3170
3171 if (SUCCEEDED(hr) && authResult == "granted")
3172 result = AuthResultAccessGranted;
3173 }
3174 catch (std::bad_alloc)
3175 {
3176 }
3177#else
3178 /*
3179 * Called only from VRDP input thread. So thread safety is not required.
3180 */
3181
3182 if (!mAuthLibCtx.hAuthLibrary)
3183 {
3184 /* Load the external authentication library. */
3185 Bstr authLibrary;
3186 mConsole->i_getVRDEServer()->COMGETTER(AuthLibrary)(authLibrary.asOutParam());
3187
3188 Utf8Str filename = authLibrary;
3189
3190 int rc = AuthLibLoad(&mAuthLibCtx, filename.c_str());
3191
3192 if (RT_FAILURE(rc))
3193 {
3194 mConsole->setError(E_FAIL,
3195 mConsole->tr("Could not load the external authentication library '%s' (%Rrc)"),
3196 filename.c_str(),
3197 rc);
3198
3199 return AuthResultAccessDenied;
3200 }
3201 }
3202
3203 result = AuthLibAuthenticate(&mAuthLibCtx,
3204 uuid.raw(), guestJudgement,
3205 pszUser, pszPassword, pszDomain,
3206 u32ClientId);
3207#endif /* !VBOX_WITH_VRDEAUTH_IN_VBOXSVC */
3208
3209 switch (result)
3210 {
3211 case AuthResultAccessDenied:
3212 LogRel(("AUTH: external authentication module returned 'access denied'\n"));
3213 break;
3214 case AuthResultAccessGranted:
3215 LogRel(("AUTH: external authentication module returned 'access granted'\n"));
3216 break;
3217 case AuthResultDelegateToGuest:
3218 LogRel(("AUTH: external authentication module returned 'delegate request to guest'\n"));
3219 break;
3220 default:
3221 LogRel(("AUTH: external authentication module returned incorrect return code %d\n", result));
3222 result = AuthResultAccessDenied;
3223 }
3224
3225 LogFlowFunc(("result = %d\n", result));
3226
3227 return result;
3228}
3229
3230void ConsoleVRDPServer::AuthDisconnect(const Guid &uuid, uint32_t u32ClientId)
3231{
3232 LogFlow(("ConsoleVRDPServer::AuthDisconnect: uuid = %RTuuid, u32ClientId = %d\n",
3233 uuid.raw(), u32ClientId));
3234
3235#ifdef VBOX_WITH_VRDEAUTH_IN_VBOXSVC
3236 try
3237 {
3238 /* Init auth parameters. Order is important. */
3239 SafeArray<BSTR> authParams;
3240 Bstr("VRDEAUTHDISCONNECT").detachTo(authParams.appendedRaw());
3241 Bstr(uuid.toUtf16() ).detachTo(authParams.appendedRaw());
3242 BstrFmt("%u", u32ClientId).detachTo(authParams.appendedRaw());
3243
3244 Bstr authResult;
3245 HRESULT hr = mConsole->mControl->AuthenticateExternal(ComSafeArrayAsInParam(authParams),
3246 authResult.asOutParam());
3247 LogFlowFunc(("%Rhrc [%ls]\n", hr, authResult.raw())); NOREF(hr);
3248 }
3249 catch (std::bad_alloc)
3250 {
3251 }
3252#else
3253 AuthLibDisconnect(&mAuthLibCtx, uuid.raw(), u32ClientId);
3254#endif /* !VBOX_WITH_VRDEAUTH_IN_VBOXSVC */
3255}
3256
3257int ConsoleVRDPServer::lockConsoleVRDPServer(void)
3258{
3259 int rc = RTCritSectEnter(&mCritSect);
3260 AssertRC(rc);
3261 return rc;
3262}
3263
3264void ConsoleVRDPServer::unlockConsoleVRDPServer(void)
3265{
3266 RTCritSectLeave(&mCritSect);
3267}
3268
3269DECLCALLBACK(int) ConsoleVRDPServer::ClipboardCallback(void *pvCallback,
3270 uint32_t u32ClientId,
3271 uint32_t u32Function,
3272 uint32_t u32Format,
3273 const void *pvData,
3274 uint32_t cbData)
3275{
3276 LogFlowFunc(("pvCallback = %p, u32ClientId = %d, u32Function = %d, u32Format = 0x%08X, pvData = %p, cbData = %d\n",
3277 pvCallback, u32ClientId, u32Function, u32Format, pvData, cbData));
3278
3279 int rc = VINF_SUCCESS;
3280
3281 ConsoleVRDPServer *pServer = static_cast <ConsoleVRDPServer *>(pvCallback);
3282
3283 NOREF(u32ClientId);
3284
3285 switch (u32Function)
3286 {
3287 case VRDE_CLIPBOARD_FUNCTION_FORMAT_ANNOUNCE:
3288 {
3289 if (pServer->mpfnClipboardCallback)
3290 {
3291 pServer->mpfnClipboardCallback(VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE,
3292 u32Format,
3293 (void *)pvData,
3294 cbData);
3295 }
3296 } break;
3297
3298 case VRDE_CLIPBOARD_FUNCTION_DATA_READ:
3299 {
3300 if (pServer->mpfnClipboardCallback)
3301 {
3302 pServer->mpfnClipboardCallback(VBOX_CLIPBOARD_EXT_FN_DATA_READ,
3303 u32Format,
3304 (void *)pvData,
3305 cbData);
3306 }
3307 } break;
3308
3309 default:
3310 rc = VERR_NOT_SUPPORTED;
3311 }
3312
3313 return rc;
3314}
3315
3316DECLCALLBACK(int) ConsoleVRDPServer::ClipboardServiceExtension(void *pvExtension,
3317 uint32_t u32Function,
3318 void *pvParms,
3319 uint32_t cbParms)
3320{
3321 RT_NOREF(cbParms);
3322 LogFlowFunc(("pvExtension = %p, u32Function = %d, pvParms = %p, cbParms = %d\n",
3323 pvExtension, u32Function, pvParms, cbParms));
3324
3325 int rc = VINF_SUCCESS;
3326
3327 ConsoleVRDPServer *pServer = static_cast <ConsoleVRDPServer *>(pvExtension);
3328
3329 VBOXCLIPBOARDEXTPARMS *pParms = (VBOXCLIPBOARDEXTPARMS *)pvParms;
3330
3331 switch (u32Function)
3332 {
3333 case VBOX_CLIPBOARD_EXT_FN_SET_CALLBACK:
3334 {
3335 pServer->mpfnClipboardCallback = pParms->u.pfnCallback;
3336 } break;
3337
3338 case VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE:
3339 {
3340 /* The guest announces clipboard formats. This must be delivered to all clients. */
3341 if (mpEntryPoints && pServer->mhServer)
3342 {
3343 mpEntryPoints->VRDEClipboard(pServer->mhServer,
3344 VRDE_CLIPBOARD_FUNCTION_FORMAT_ANNOUNCE,
3345 pParms->u32Format,
3346 NULL,
3347 0,
3348 NULL);
3349 }
3350 } break;
3351
3352 case VBOX_CLIPBOARD_EXT_FN_DATA_READ:
3353 {
3354 /* The clipboard service expects that the pvData buffer will be filled
3355 * with clipboard data. The server returns the data from the client that
3356 * announced the requested format most recently.
3357 */
3358 if (mpEntryPoints && pServer->mhServer)
3359 {
3360 mpEntryPoints->VRDEClipboard(pServer->mhServer,
3361 VRDE_CLIPBOARD_FUNCTION_DATA_READ,
3362 pParms->u32Format,
3363 pParms->u.pvData,
3364 pParms->cbData,
3365 &pParms->cbData);
3366 }
3367 } break;
3368
3369 case VBOX_CLIPBOARD_EXT_FN_DATA_WRITE:
3370 {
3371 if (mpEntryPoints && pServer->mhServer)
3372 {
3373 mpEntryPoints->VRDEClipboard(pServer->mhServer,
3374 VRDE_CLIPBOARD_FUNCTION_DATA_WRITE,
3375 pParms->u32Format,
3376 pParms->u.pvData,
3377 pParms->cbData,
3378 NULL);
3379 }
3380 } break;
3381
3382 default:
3383 rc = VERR_NOT_SUPPORTED;
3384 }
3385
3386 return rc;
3387}
3388
3389void ConsoleVRDPServer::ClipboardCreate(uint32_t u32ClientId)
3390{
3391 RT_NOREF(u32ClientId);
3392 int rc = lockConsoleVRDPServer();
3393
3394 if (RT_SUCCESS(rc))
3395 {
3396 if (mcClipboardRefs == 0)
3397 {
3398 rc = HGCMHostRegisterServiceExtension(&mhClipboard, "VBoxSharedClipboard", ClipboardServiceExtension, this);
3399
3400 if (RT_SUCCESS(rc))
3401 {
3402 mcClipboardRefs++;
3403 }
3404 }
3405
3406 unlockConsoleVRDPServer();
3407 }
3408}
3409
3410void ConsoleVRDPServer::ClipboardDelete(uint32_t u32ClientId)
3411{
3412 RT_NOREF(u32ClientId);
3413 int rc = lockConsoleVRDPServer();
3414
3415 if (RT_SUCCESS(rc))
3416 {
3417 mcClipboardRefs--;
3418
3419 if (mcClipboardRefs == 0)
3420 {
3421 HGCMHostUnregisterServiceExtension(mhClipboard);
3422 }
3423
3424 unlockConsoleVRDPServer();
3425 }
3426}
3427
3428/* That is called on INPUT thread of the VRDP server.
3429 * The ConsoleVRDPServer keeps a list of created backend instances.
3430 */
3431void ConsoleVRDPServer::USBBackendCreate(uint32_t u32ClientId, void **ppvIntercept)
3432{
3433#ifdef VBOX_WITH_USB
3434 LogFlow(("ConsoleVRDPServer::USBBackendCreate: u32ClientId = %d\n", u32ClientId));
3435
3436 /* Create a new instance of the USB backend for the new client. */
3437 RemoteUSBBackend *pRemoteUSBBackend = new RemoteUSBBackend(mConsole, this, u32ClientId);
3438
3439 if (pRemoteUSBBackend)
3440 {
3441 pRemoteUSBBackend->AddRef(); /* 'Release' called in USBBackendDelete. */
3442
3443 /* Append the new instance in the list. */
3444 int rc = lockConsoleVRDPServer();
3445
3446 if (RT_SUCCESS(rc))
3447 {
3448 pRemoteUSBBackend->pNext = mUSBBackends.pHead;
3449 if (mUSBBackends.pHead)
3450 {
3451 mUSBBackends.pHead->pPrev = pRemoteUSBBackend;
3452 }
3453 else
3454 {
3455 mUSBBackends.pTail = pRemoteUSBBackend;
3456 }
3457
3458 mUSBBackends.pHead = pRemoteUSBBackend;
3459
3460 unlockConsoleVRDPServer();
3461
3462 if (ppvIntercept)
3463 {
3464 *ppvIntercept = pRemoteUSBBackend;
3465 }
3466 }
3467
3468 if (RT_FAILURE(rc))
3469 {
3470 pRemoteUSBBackend->Release();
3471 }
3472 }
3473#endif /* VBOX_WITH_USB */
3474}
3475
3476void ConsoleVRDPServer::USBBackendDelete(uint32_t u32ClientId)
3477{
3478#ifdef VBOX_WITH_USB
3479 LogFlow(("ConsoleVRDPServer::USBBackendDelete: u32ClientId = %d\n", u32ClientId));
3480
3481 RemoteUSBBackend *pRemoteUSBBackend = NULL;
3482
3483 /* Find the instance. */
3484 int rc = lockConsoleVRDPServer();
3485
3486 if (RT_SUCCESS(rc))
3487 {
3488 pRemoteUSBBackend = usbBackendFind(u32ClientId);
3489
3490 if (pRemoteUSBBackend)
3491 {
3492 /* Notify that it will be deleted. */
3493 pRemoteUSBBackend->NotifyDelete();
3494 }
3495
3496 unlockConsoleVRDPServer();
3497 }
3498
3499 if (pRemoteUSBBackend)
3500 {
3501 /* Here the instance has been excluded from the list and can be dereferenced. */
3502 pRemoteUSBBackend->Release();
3503 }
3504#endif
3505}
3506
3507void *ConsoleVRDPServer::USBBackendRequestPointer(uint32_t u32ClientId, const Guid *pGuid)
3508{
3509#ifdef VBOX_WITH_USB
3510 RemoteUSBBackend *pRemoteUSBBackend = NULL;
3511
3512 /* Find the instance. */
3513 int rc = lockConsoleVRDPServer();
3514
3515 if (RT_SUCCESS(rc))
3516 {
3517 pRemoteUSBBackend = usbBackendFind(u32ClientId);
3518
3519 if (pRemoteUSBBackend)
3520 {
3521 /* Inform the backend instance that it is referenced by the Guid. */
3522 bool fAdded = pRemoteUSBBackend->addUUID(pGuid);
3523
3524 if (fAdded)
3525 {
3526 /* Reference the instance because its pointer is being taken. */
3527 pRemoteUSBBackend->AddRef(); /* 'Release' is called in USBBackendReleasePointer. */
3528 }
3529 else
3530 {
3531 pRemoteUSBBackend = NULL;
3532 }
3533 }
3534
3535 unlockConsoleVRDPServer();
3536 }
3537
3538 if (pRemoteUSBBackend)
3539 {
3540 return pRemoteUSBBackend->GetBackendCallbackPointer();
3541 }
3542
3543#endif
3544 return NULL;
3545}
3546
3547void ConsoleVRDPServer::USBBackendReleasePointer(const Guid *pGuid)
3548{
3549#ifdef VBOX_WITH_USB
3550 RemoteUSBBackend *pRemoteUSBBackend = NULL;
3551
3552 /* Find the instance. */
3553 int rc = lockConsoleVRDPServer();
3554
3555 if (RT_SUCCESS(rc))
3556 {
3557 pRemoteUSBBackend = usbBackendFindByUUID(pGuid);
3558
3559 if (pRemoteUSBBackend)
3560 {
3561 pRemoteUSBBackend->removeUUID(pGuid);
3562 }
3563
3564 unlockConsoleVRDPServer();
3565
3566 if (pRemoteUSBBackend)
3567 {
3568 pRemoteUSBBackend->Release();
3569 }
3570 }
3571#endif
3572}
3573
3574RemoteUSBBackend *ConsoleVRDPServer::usbBackendGetNext(RemoteUSBBackend *pRemoteUSBBackend)
3575{
3576 LogFlow(("ConsoleVRDPServer::usbBackendGetNext: pBackend = %p\n", pRemoteUSBBackend));
3577
3578 RemoteUSBBackend *pNextRemoteUSBBackend = NULL;
3579#ifdef VBOX_WITH_USB
3580
3581 int rc = lockConsoleVRDPServer();
3582
3583 if (RT_SUCCESS(rc))
3584 {
3585 if (pRemoteUSBBackend == NULL)
3586 {
3587 /* The first backend in the list is requested. */
3588 pNextRemoteUSBBackend = mUSBBackends.pHead;
3589 }
3590 else
3591 {
3592 /* Get pointer to the next backend. */
3593 pNextRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
3594 }
3595
3596 if (pNextRemoteUSBBackend)
3597 {
3598 pNextRemoteUSBBackend->AddRef();
3599 }
3600
3601 unlockConsoleVRDPServer();
3602
3603 if (pRemoteUSBBackend)
3604 {
3605 pRemoteUSBBackend->Release();
3606 }
3607 }
3608#endif
3609
3610 return pNextRemoteUSBBackend;
3611}
3612
3613#ifdef VBOX_WITH_USB
3614/* Internal method. Called under the ConsoleVRDPServerLock. */
3615RemoteUSBBackend *ConsoleVRDPServer::usbBackendFind(uint32_t u32ClientId)
3616{
3617 RemoteUSBBackend *pRemoteUSBBackend = mUSBBackends.pHead;
3618
3619 while (pRemoteUSBBackend)
3620 {
3621 if (pRemoteUSBBackend->ClientId() == u32ClientId)
3622 {
3623 break;
3624 }
3625
3626 pRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
3627 }
3628
3629 return pRemoteUSBBackend;
3630}
3631
3632/* Internal method. Called under the ConsoleVRDPServerLock. */
3633RemoteUSBBackend *ConsoleVRDPServer::usbBackendFindByUUID(const Guid *pGuid)
3634{
3635 RemoteUSBBackend *pRemoteUSBBackend = mUSBBackends.pHead;
3636
3637 while (pRemoteUSBBackend)
3638 {
3639 if (pRemoteUSBBackend->findUUID(pGuid))
3640 {
3641 break;
3642 }
3643
3644 pRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
3645 }
3646
3647 return pRemoteUSBBackend;
3648}
3649#endif
3650
3651/* Internal method. Called by the backend destructor. */
3652void ConsoleVRDPServer::usbBackendRemoveFromList(RemoteUSBBackend *pRemoteUSBBackend)
3653{
3654#ifdef VBOX_WITH_USB
3655 int rc = lockConsoleVRDPServer();
3656 AssertRC(rc);
3657
3658 /* Exclude the found instance from the list. */
3659 if (pRemoteUSBBackend->pNext)
3660 {
3661 pRemoteUSBBackend->pNext->pPrev = pRemoteUSBBackend->pPrev;
3662 }
3663 else
3664 {
3665 mUSBBackends.pTail = (RemoteUSBBackend *)pRemoteUSBBackend->pPrev;
3666 }
3667
3668 if (pRemoteUSBBackend->pPrev)
3669 {
3670 pRemoteUSBBackend->pPrev->pNext = pRemoteUSBBackend->pNext;
3671 }
3672 else
3673 {
3674 mUSBBackends.pHead = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
3675 }
3676
3677 pRemoteUSBBackend->pNext = pRemoteUSBBackend->pPrev = NULL;
3678
3679 unlockConsoleVRDPServer();
3680#endif
3681}
3682
3683
3684void ConsoleVRDPServer::SendUpdate(unsigned uScreenId, void *pvUpdate, uint32_t cbUpdate) const
3685{
3686 if (mpEntryPoints && mhServer)
3687 {
3688 mpEntryPoints->VRDEUpdate(mhServer, uScreenId, pvUpdate, cbUpdate);
3689 }
3690}
3691
3692void ConsoleVRDPServer::SendResize(void)
3693{
3694 if (mpEntryPoints && mhServer)
3695 {
3696 ++mcInResize;
3697 mpEntryPoints->VRDEResize(mhServer);
3698 --mcInResize;
3699 }
3700}
3701
3702void ConsoleVRDPServer::SendUpdateBitmap(unsigned uScreenId, uint32_t x, uint32_t y, uint32_t w, uint32_t h) const
3703{
3704 VRDEORDERHDR update;
3705 update.x = (uint16_t)x;
3706 update.y = (uint16_t)y;
3707 update.w = (uint16_t)w;
3708 update.h = (uint16_t)h;
3709 if (mpEntryPoints && mhServer)
3710 {
3711 mpEntryPoints->VRDEUpdate(mhServer, uScreenId, &update, sizeof(update));
3712 }
3713}
3714
3715void ConsoleVRDPServer::SendAudioSamples(void *pvSamples, uint32_t cSamples, VRDEAUDIOFORMAT format) const
3716{
3717 if (mpEntryPoints && mhServer)
3718 {
3719 mpEntryPoints->VRDEAudioSamples(mhServer, pvSamples, cSamples, format);
3720 }
3721}
3722
3723void ConsoleVRDPServer::SendAudioVolume(uint16_t left, uint16_t right) const
3724{
3725 if (mpEntryPoints && mhServer)
3726 {
3727 mpEntryPoints->VRDEAudioVolume(mhServer, left, right);
3728 }
3729}
3730
3731void ConsoleVRDPServer::SendUSBRequest(uint32_t u32ClientId, void *pvParms, uint32_t cbParms) const
3732{
3733 if (mpEntryPoints && mhServer)
3734 {
3735 mpEntryPoints->VRDEUSBRequest(mhServer, u32ClientId, pvParms, cbParms);
3736 }
3737}
3738
3739int ConsoleVRDPServer::SendAudioInputBegin(void **ppvUserCtx,
3740 void *pvContext,
3741 uint32_t cSamples,
3742 uint32_t iSampleHz,
3743 uint32_t cChannels,
3744 uint32_t cBits)
3745{
3746 if ( mhServer
3747 && mpEntryPoints && mpEntryPoints->VRDEAudioInOpen)
3748 {
3749 uint32_t u32ClientId = ASMAtomicReadU32(&mu32AudioInputClientId);
3750 if (u32ClientId != 0) /* 0 would mean broadcast to all clients. */
3751 {
3752 VRDEAUDIOFORMAT audioFormat = VRDE_AUDIO_FMT_MAKE(iSampleHz, cChannels, cBits, 0);
3753 mpEntryPoints->VRDEAudioInOpen(mhServer,
3754 pvContext,
3755 u32ClientId,
3756 audioFormat,
3757 cSamples);
3758 if (ppvUserCtx)
3759 *ppvUserCtx = NULL; /* This is the ConsoleVRDPServer context.
3760 * Currently not used because only one client is allowed to
3761 * do audio input and the client ID is saved by the ConsoleVRDPServer.
3762 */
3763 return VINF_SUCCESS;
3764 }
3765 }
3766
3767 /*
3768 * Not supported or no client connected.
3769 */
3770 return VERR_NOT_SUPPORTED;
3771}
3772
3773void ConsoleVRDPServer::SendAudioInputEnd(void *pvUserCtx)
3774{
3775 RT_NOREF(pvUserCtx);
3776 if (mpEntryPoints && mhServer && mpEntryPoints->VRDEAudioInClose)
3777 {
3778 uint32_t u32ClientId = ASMAtomicReadU32(&mu32AudioInputClientId);
3779 if (u32ClientId != 0) /* 0 would mean broadcast to all clients. */
3780 {
3781 mpEntryPoints->VRDEAudioInClose(mhServer, u32ClientId);
3782 }
3783 }
3784}
3785
3786void ConsoleVRDPServer::QueryInfo(uint32_t index, void *pvBuffer, uint32_t cbBuffer, uint32_t *pcbOut) const
3787{
3788 if (index == VRDE_QI_PORT)
3789 {
3790 uint32_t cbOut = sizeof(int32_t);
3791
3792 if (cbBuffer >= cbOut)
3793 {
3794 *pcbOut = cbOut;
3795 *(int32_t *)pvBuffer = (int32_t)mVRDPBindPort;
3796 }
3797 }
3798 else if (mpEntryPoints && mhServer)
3799 {
3800 mpEntryPoints->VRDEQueryInfo(mhServer, index, pvBuffer, cbBuffer, pcbOut);
3801 }
3802}
3803
3804/* static */ int ConsoleVRDPServer::loadVRDPLibrary(const char *pszLibraryName)
3805{
3806 int rc = VINF_SUCCESS;
3807
3808 if (mVRDPLibrary == NIL_RTLDRMOD)
3809 {
3810 RTERRINFOSTATIC ErrInfo;
3811 RTErrInfoInitStatic(&ErrInfo);
3812
3813 if (RTPathHavePath(pszLibraryName))
3814 rc = SUPR3HardenedLdrLoadPlugIn(pszLibraryName, &mVRDPLibrary, &ErrInfo.Core);
3815 else
3816 rc = SUPR3HardenedLdrLoadAppPriv(pszLibraryName, &mVRDPLibrary, RTLDRLOAD_FLAGS_LOCAL, &ErrInfo.Core);
3817 if (RT_SUCCESS(rc))
3818 {
3819 struct SymbolEntry
3820 {
3821 const char *name;
3822 void **ppfn;
3823 };
3824
3825 #define DEFSYMENTRY(a) { #a, (void**)&mpfn##a }
3826
3827 static const struct SymbolEntry s_aSymbols[] =
3828 {
3829 DEFSYMENTRY(VRDECreateServer)
3830 };
3831
3832 #undef DEFSYMENTRY
3833
3834 for (unsigned i = 0; i < RT_ELEMENTS(s_aSymbols); i++)
3835 {
3836 rc = RTLdrGetSymbol(mVRDPLibrary, s_aSymbols[i].name, s_aSymbols[i].ppfn);
3837
3838 if (RT_FAILURE(rc))
3839 {
3840 LogRel(("VRDE: Error resolving symbol '%s', rc %Rrc.\n", s_aSymbols[i].name, rc));
3841 break;
3842 }
3843 }
3844 }
3845 else
3846 {
3847 if (RTErrInfoIsSet(&ErrInfo.Core))
3848 LogRel(("VRDE: Error loading the library '%s': %s (%Rrc)\n", pszLibraryName, ErrInfo.Core.pszMsg, rc));
3849 else
3850 LogRel(("VRDE: Error loading the library '%s' rc = %Rrc.\n", pszLibraryName, rc));
3851
3852 mVRDPLibrary = NIL_RTLDRMOD;
3853 }
3854 }
3855
3856 if (RT_FAILURE(rc))
3857 {
3858 if (mVRDPLibrary != NIL_RTLDRMOD)
3859 {
3860 RTLdrClose(mVRDPLibrary);
3861 mVRDPLibrary = NIL_RTLDRMOD;
3862 }
3863 }
3864
3865 return rc;
3866}
3867
3868/*
3869 * IVRDEServerInfo implementation.
3870 */
3871// constructor / destructor
3872/////////////////////////////////////////////////////////////////////////////
3873
3874VRDEServerInfo::VRDEServerInfo()
3875 : mParent(NULL)
3876{
3877}
3878
3879VRDEServerInfo::~VRDEServerInfo()
3880{
3881}
3882
3883
3884HRESULT VRDEServerInfo::FinalConstruct()
3885{
3886 return BaseFinalConstruct();
3887}
3888
3889void VRDEServerInfo::FinalRelease()
3890{
3891 uninit();
3892 BaseFinalRelease();
3893}
3894
3895// public methods only for internal purposes
3896/////////////////////////////////////////////////////////////////////////////
3897
3898/**
3899 * Initializes the guest object.
3900 */
3901HRESULT VRDEServerInfo::init(Console *aParent)
3902{
3903 LogFlowThisFunc(("aParent=%p\n", aParent));
3904
3905 ComAssertRet(aParent, E_INVALIDARG);
3906
3907 /* Enclose the state transition NotReady->InInit->Ready */
3908 AutoInitSpan autoInitSpan(this);
3909 AssertReturn(autoInitSpan.isOk(), E_FAIL);
3910
3911 unconst(mParent) = aParent;
3912
3913 /* Confirm a successful initialization */
3914 autoInitSpan.setSucceeded();
3915
3916 return S_OK;
3917}
3918
3919/**
3920 * Uninitializes the instance and sets the ready flag to FALSE.
3921 * Called either from FinalRelease() or by the parent when it gets destroyed.
3922 */
3923void VRDEServerInfo::uninit()
3924{
3925 LogFlowThisFunc(("\n"));
3926
3927 /* Enclose the state transition Ready->InUninit->NotReady */
3928 AutoUninitSpan autoUninitSpan(this);
3929 if (autoUninitSpan.uninitDone())
3930 return;
3931
3932 unconst(mParent) = NULL;
3933}
3934
3935// IVRDEServerInfo properties
3936/////////////////////////////////////////////////////////////////////////////
3937
3938#define IMPL_GETTER_BOOL(_aType, _aName, _aIndex) \
3939 HRESULT VRDEServerInfo::get##_aName(_aType *a##_aName) \
3940 { \
3941 /** @todo Not sure if a AutoReadLock would be sufficient. */ \
3942 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); \
3943 \
3944 uint32_t value; \
3945 uint32_t cbOut = 0; \
3946 \
3947 mParent->i_consoleVRDPServer()->QueryInfo \
3948 (_aIndex, &value, sizeof(value), &cbOut); \
3949 \
3950 *a##_aName = cbOut? !!value: FALSE; \
3951 \
3952 return S_OK; \
3953 } \
3954 extern void IMPL_GETTER_BOOL_DUMMY(void)
3955
3956#define IMPL_GETTER_SCALAR(_aType, _aName, _aIndex, _aValueMask) \
3957 HRESULT VRDEServerInfo::get##_aName(_aType *a##_aName) \
3958 { \
3959 /** @todo Not sure if a AutoReadLock would be sufficient. */ \
3960 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); \
3961 \
3962 _aType value; \
3963 uint32_t cbOut = 0; \
3964 \
3965 mParent->i_consoleVRDPServer()->QueryInfo \
3966 (_aIndex, &value, sizeof(value), &cbOut); \
3967 \
3968 if (_aValueMask) value &= (_aValueMask); \
3969 *a##_aName = cbOut? value: 0; \
3970 \
3971 return S_OK; \
3972 } \
3973 extern void IMPL_GETTER_SCALAR_DUMMY(void)
3974
3975#define IMPL_GETTER_UTF8STR(_aType, _aName, _aIndex) \
3976 HRESULT VRDEServerInfo::get##_aName(_aType &a##_aName) \
3977 { \
3978 /** @todo Not sure if a AutoReadLock would be sufficient. */ \
3979 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); \
3980 \
3981 uint32_t cbOut = 0; \
3982 \
3983 mParent->i_consoleVRDPServer()->QueryInfo \
3984 (_aIndex, NULL, 0, &cbOut); \
3985 \
3986 if (cbOut == 0) \
3987 { \
3988 a##_aName = Utf8Str::Empty; \
3989 return S_OK; \
3990 } \
3991 \
3992 char *pchBuffer = (char *)RTMemTmpAlloc(cbOut); \
3993 \
3994 if (!pchBuffer) \
3995 { \
3996 Log(("VRDEServerInfo::" \
3997 #_aName \
3998 ": Failed to allocate memory %d bytes\n", cbOut)); \
3999 return E_OUTOFMEMORY; \
4000 } \
4001 \
4002 mParent->i_consoleVRDPServer()->QueryInfo \
4003 (_aIndex, pchBuffer, cbOut, &cbOut); \
4004 \
4005 a##_aName = pchBuffer; \
4006 \
4007 RTMemTmpFree(pchBuffer); \
4008 \
4009 return S_OK; \
4010 } \
4011 extern void IMPL_GETTER_BSTR_DUMMY(void)
4012
4013IMPL_GETTER_BOOL (BOOL, Active, VRDE_QI_ACTIVE);
4014IMPL_GETTER_SCALAR (LONG, Port, VRDE_QI_PORT, 0);
4015IMPL_GETTER_SCALAR (ULONG, NumberOfClients, VRDE_QI_NUMBER_OF_CLIENTS, 0);
4016IMPL_GETTER_SCALAR (LONG64, BeginTime, VRDE_QI_BEGIN_TIME, 0);
4017IMPL_GETTER_SCALAR (LONG64, EndTime, VRDE_QI_END_TIME, 0);
4018IMPL_GETTER_SCALAR (LONG64, BytesSent, VRDE_QI_BYTES_SENT, INT64_MAX);
4019IMPL_GETTER_SCALAR (LONG64, BytesSentTotal, VRDE_QI_BYTES_SENT_TOTAL, INT64_MAX);
4020IMPL_GETTER_SCALAR (LONG64, BytesReceived, VRDE_QI_BYTES_RECEIVED, INT64_MAX);
4021IMPL_GETTER_SCALAR (LONG64, BytesReceivedTotal, VRDE_QI_BYTES_RECEIVED_TOTAL, INT64_MAX);
4022IMPL_GETTER_UTF8STR(Utf8Str, User, VRDE_QI_USER);
4023IMPL_GETTER_UTF8STR(Utf8Str, Domain, VRDE_QI_DOMAIN);
4024IMPL_GETTER_UTF8STR(Utf8Str, ClientName, VRDE_QI_CLIENT_NAME);
4025IMPL_GETTER_UTF8STR(Utf8Str, ClientIP, VRDE_QI_CLIENT_IP);
4026IMPL_GETTER_SCALAR (ULONG, ClientVersion, VRDE_QI_CLIENT_VERSION, 0);
4027IMPL_GETTER_SCALAR (ULONG, EncryptionStyle, VRDE_QI_ENCRYPTION_STYLE, 0);
4028
4029#undef IMPL_GETTER_UTF8STR
4030#undef IMPL_GETTER_SCALAR
4031#undef IMPL_GETTER_BOOL
4032/* 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