VirtualBox

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

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

Main,SharedOpenGL: VRDP cleanup. bugref:9225

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

© 2023 Oracle
ContactPrivacy policyTerms of Use