VirtualBox

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

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

6813 Use of server side API wrapper code - ConsoleImpl.cpp

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette