VirtualBox

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

Last change on this file since 50914 was 50914, checked in by vboxsync, 11 years ago

6813 src-all/ExtPackManagerImpl.cpp

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 138.7 KB
Line 
1/* $Id: ConsoleVRDPServer.cpp 50914 2014-03-27 19:07:53Z 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.215389.xyz. 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, uint32_t height, uint32_t *pxSkip, uint32_t *pySkip)
200{
201 /*
202 * Find the top border of the AND mask. First assign to special value.
203 */
204 uint32_t ySkipAnd = ~0;
205
206 const uint8_t *pu8And = pu8AndMask;
207 const uint32_t cbAndRow = (width + 7) / 8;
208 const uint8_t maskLastByte = (uint8_t)( 0xFF << (cbAndRow * 8 - width) );
209
210 Assert(cbAndRow > 0);
211
212 unsigned y;
213 unsigned x;
214
215 for (y = 0; y < height && ySkipAnd == ~(uint32_t)0; y++, pu8And += cbAndRow)
216 {
217 /* For each complete byte in the row. */
218 for (x = 0; x < cbAndRow - 1; x++)
219 {
220 if (pu8And[x] != 0xFF)
221 {
222 ySkipAnd = y;
223 break;
224 }
225 }
226
227 if (ySkipAnd == ~(uint32_t)0)
228 {
229 /* Last byte. */
230 if ((pu8And[cbAndRow - 1] & maskLastByte) != maskLastByte)
231 {
232 ySkipAnd = y;
233 }
234 }
235 }
236
237 if (ySkipAnd == ~(uint32_t)0)
238 {
239 ySkipAnd = 0;
240 }
241
242 /*
243 * Find the left border of the AND mask.
244 */
245 uint32_t xSkipAnd = ~0;
246
247 /* For all bit columns. */
248 for (x = 0; x < width && xSkipAnd == ~(uint32_t)0; x++)
249 {
250 pu8And = pu8AndMask + x/8; /* Currently checking byte. */
251 uint8_t mask = 1 << (7 - x%8); /* Currently checking bit in the byte. */
252
253 for (y = ySkipAnd; y < height; y++, pu8And += cbAndRow)
254 {
255 if ((*pu8And & mask) == 0)
256 {
257 xSkipAnd = x;
258 break;
259 }
260 }
261 }
262
263 if (xSkipAnd == ~(uint32_t)0)
264 {
265 xSkipAnd = 0;
266 }
267
268 /*
269 * Find the XOR mask top border.
270 */
271 uint32_t ySkipXor = ~0;
272
273 uint32_t *pu32XorStart = (uint32_t *)pu8XorMask;
274
275 uint32_t *pu32Xor = pu32XorStart;
276
277 for (y = 0; y < height && ySkipXor == ~(uint32_t)0; y++, pu32Xor += width)
278 {
279 for (x = 0; x < width; x++)
280 {
281 if (pu32Xor[x] != 0)
282 {
283 ySkipXor = y;
284 break;
285 }
286 }
287 }
288
289 if (ySkipXor == ~(uint32_t)0)
290 {
291 ySkipXor = 0;
292 }
293
294 /*
295 * Find the left border of the XOR mask.
296 */
297 uint32_t xSkipXor = ~(uint32_t)0;
298
299 /* For all columns. */
300 for (x = 0; x < width && xSkipXor == ~(uint32_t)0; x++)
301 {
302 pu32Xor = pu32XorStart + x; /* Currently checking dword. */
303
304 for (y = ySkipXor; y < height; y++, pu32Xor += width)
305 {
306 if (*pu32Xor != 0)
307 {
308 xSkipXor = x;
309 break;
310 }
311 }
312 }
313
314 if (xSkipXor == ~(uint32_t)0)
315 {
316 xSkipXor = 0;
317 }
318
319 *pxSkip = RT_MIN(xSkipAnd, xSkipXor);
320 *pySkip = RT_MIN(ySkipAnd, ySkipXor);
321}
322
323/* Generate an AND mask for alpha pointers here, because
324 * guest driver does not do that correctly for Vista pointers.
325 * Similar fix, changing the alpha threshold, could be applied
326 * for the guest driver, but then additions reinstall would be
327 * necessary, which we try to avoid.
328 */
329static void mousePointerGenerateANDMask(uint8_t *pu8DstAndMask, int cbDstAndMask, const uint8_t *pu8SrcAlpha, int w, int h)
330{
331 memset(pu8DstAndMask, 0xFF, cbDstAndMask);
332
333 int y;
334 for (y = 0; y < h; y++)
335 {
336 uint8_t bitmask = 0x80;
337
338 int x;
339 for (x = 0; x < w; x++, bitmask >>= 1)
340 {
341 if (bitmask == 0)
342 {
343 bitmask = 0x80;
344 }
345
346 /* Whether alpha channel value is not transparent enough for the pixel to be seen. */
347 if (pu8SrcAlpha[x * 4 + 3] > 0x7f)
348 {
349 pu8DstAndMask[x / 8] &= ~bitmask;
350 }
351 }
352
353 /* Point to next source and dest scans. */
354 pu8SrcAlpha += w * 4;
355 pu8DstAndMask += (w + 7) / 8;
356 }
357}
358
359STDMETHODIMP VRDPConsoleListener::OnMousePointerShapeChange(BOOL visible,
360 BOOL alpha,
361 ULONG xHot,
362 ULONG yHot,
363 ULONG width,
364 ULONG height,
365 ComSafeArrayIn(BYTE,inShape))
366{
367 LogSunlover(("VRDPConsoleListener::OnMousePointerShapeChange: %d, %d, %lux%lu, @%lu,%lu\n", visible, alpha, width, height, xHot, yHot));
368
369 if (m_server)
370 {
371 com::SafeArray <BYTE> aShape(ComSafeArrayInArg(inShape));
372 if (aShape.size() == 0)
373 {
374 if (!visible)
375 {
376 m_server->MousePointerHide();
377 }
378 }
379 else if (width != 0 && height != 0)
380 {
381 uint8_t* shape = aShape.raw();
382
383 dumpPointer(shape, width, height, true);
384
385 if (m_server->MousePointer(alpha, xHot, yHot, width, height, shape) == VINF_SUCCESS)
386 {
387 return S_OK;
388 }
389
390 /* Pointer consists of 1 bpp AND and 24 BPP XOR masks.
391 * 'shape' AND mask followed by XOR mask.
392 * XOR mask contains 32 bit (lsb)BGR0(msb) values.
393 *
394 * We convert this to RDP color format which consist of
395 * one bpp AND mask and 24 BPP (BGR) color XOR image.
396 *
397 * RDP clients expect 8 aligned width and height of
398 * pointer (preferably 32x32).
399 *
400 * They even contain bugs which do not appear for
401 * 32x32 pointers but would appear for a 41x32 one.
402 *
403 * So set pointer size to 32x32. This can be done safely
404 * because most pointers are 32x32.
405 */
406
407 int cbDstAndMask = (((width + 7) / 8) * height + 3) & ~3;
408
409 uint8_t *pu8AndMask = shape;
410 uint8_t *pu8XorMask = shape + cbDstAndMask;
411
412 if (alpha)
413 {
414 pu8AndMask = (uint8_t*)alloca(cbDstAndMask);
415
416 mousePointerGenerateANDMask(pu8AndMask, cbDstAndMask, pu8XorMask, width, height);
417 }
418
419 /* Windows guest alpha pointers are wider than 32 pixels.
420 * Try to find out the top-left border of the pointer and
421 * then copy only meaningful bits. All complete top rows
422 * and all complete left columns where (AND == 1 && XOR == 0)
423 * are skipped. Hot spot is adjusted.
424 */
425 uint32_t ySkip = 0; /* How many rows to skip at the top. */
426 uint32_t xSkip = 0; /* How many columns to skip at the left. */
427
428 findTopLeftBorder(pu8AndMask, pu8XorMask, width, height, &xSkip, &ySkip);
429
430 /* Must not skip the hot spot. */
431 xSkip = RT_MIN(xSkip, xHot);
432 ySkip = RT_MIN(ySkip, yHot);
433
434 /*
435 * Compute size and allocate memory for the pointer.
436 */
437 const uint32_t dstwidth = 32;
438 const uint32_t dstheight = 32;
439
440 VRDECOLORPOINTER *pointer = NULL;
441
442 uint32_t dstmaskwidth = (dstwidth + 7) / 8;
443
444 uint32_t rdpmaskwidth = dstmaskwidth;
445 uint32_t rdpmasklen = dstheight * rdpmaskwidth;
446
447 uint32_t rdpdatawidth = dstwidth * 3;
448 uint32_t rdpdatalen = dstheight * rdpdatawidth;
449
450 pointer = (VRDECOLORPOINTER *)RTMemTmpAlloc(sizeof(VRDECOLORPOINTER) + rdpmasklen + rdpdatalen);
451
452 if (pointer)
453 {
454 uint8_t *maskarray = (uint8_t*)pointer + sizeof(VRDECOLORPOINTER);
455 uint8_t *dataarray = maskarray + rdpmasklen;
456
457 memset(maskarray, 0xFF, rdpmasklen);
458 memset(dataarray, 0x00, rdpdatalen);
459
460 uint32_t srcmaskwidth = (width + 7) / 8;
461 uint32_t srcdatawidth = width * 4;
462
463 /* Copy AND mask. */
464 uint8_t *src = pu8AndMask + ySkip * srcmaskwidth;
465 uint8_t *dst = maskarray + (dstheight - 1) * rdpmaskwidth;
466
467 uint32_t minheight = RT_MIN(height - ySkip, dstheight);
468 uint32_t minwidth = RT_MIN(width - xSkip, dstwidth);
469
470 unsigned x, y;
471
472 for (y = 0; y < minheight; y++)
473 {
474 for (x = 0; x < minwidth; x++)
475 {
476 uint32_t byteIndex = (x + xSkip) / 8;
477 uint32_t bitIndex = (x + xSkip) % 8;
478
479 bool bit = (src[byteIndex] & (1 << (7 - bitIndex))) != 0;
480
481 if (!bit)
482 {
483 byteIndex = x / 8;
484 bitIndex = x % 8;
485
486 dst[byteIndex] &= ~(1 << (7 - bitIndex));
487 }
488 }
489
490 src += srcmaskwidth;
491 dst -= rdpmaskwidth;
492 }
493
494 /* Point src to XOR mask */
495 src = pu8XorMask + ySkip * srcdatawidth;
496 dst = dataarray + (dstheight - 1) * rdpdatawidth;
497
498 for (y = 0; y < minheight ; y++)
499 {
500 for (x = 0; x < minwidth; x++)
501 {
502 memcpy(dst + x * 3, &src[4 * (x + xSkip)], 3);
503 }
504
505 src += srcdatawidth;
506 dst -= rdpdatawidth;
507 }
508
509 pointer->u16HotX = (uint16_t)(xHot - xSkip);
510 pointer->u16HotY = (uint16_t)(yHot - ySkip);
511
512 pointer->u16Width = (uint16_t)dstwidth;
513 pointer->u16Height = (uint16_t)dstheight;
514
515 pointer->u16MaskLen = (uint16_t)rdpmasklen;
516 pointer->u16DataLen = (uint16_t)rdpdatalen;
517
518 dumpPointer((uint8_t*)pointer + sizeof(*pointer), dstwidth, dstheight, false);
519
520 m_server->MousePointerUpdate(pointer);
521
522 RTMemTmpFree(pointer);
523 }
524 }
525 }
526
527 return S_OK;
528}
529
530
531// ConsoleVRDPServer
532////////////////////////////////////////////////////////////////////////////////
533
534RTLDRMOD ConsoleVRDPServer::mVRDPLibrary = NIL_RTLDRMOD;
535
536PFNVRDECREATESERVER ConsoleVRDPServer::mpfnVRDECreateServer = NULL;
537
538VRDEENTRYPOINTS_4 ConsoleVRDPServer::mEntryPoints; /* A copy of the server entry points. */
539VRDEENTRYPOINTS_4 *ConsoleVRDPServer::mpEntryPoints = NULL;
540
541VRDECALLBACKS_4 ConsoleVRDPServer::mCallbacks =
542{
543 { VRDE_INTERFACE_VERSION_4, sizeof(VRDECALLBACKS_4) },
544 ConsoleVRDPServer::VRDPCallbackQueryProperty,
545 ConsoleVRDPServer::VRDPCallbackClientLogon,
546 ConsoleVRDPServer::VRDPCallbackClientConnect,
547 ConsoleVRDPServer::VRDPCallbackClientDisconnect,
548 ConsoleVRDPServer::VRDPCallbackIntercept,
549 ConsoleVRDPServer::VRDPCallbackUSB,
550 ConsoleVRDPServer::VRDPCallbackClipboard,
551 ConsoleVRDPServer::VRDPCallbackFramebufferQuery,
552 ConsoleVRDPServer::VRDPCallbackFramebufferLock,
553 ConsoleVRDPServer::VRDPCallbackFramebufferUnlock,
554 ConsoleVRDPServer::VRDPCallbackInput,
555 ConsoleVRDPServer::VRDPCallbackVideoModeHint,
556 ConsoleVRDPServer::VRDECallbackAudioIn
557};
558
559DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackQueryProperty(void *pvCallback, uint32_t index, void *pvBuffer, uint32_t cbBuffer, uint32_t *pcbOut)
560{
561 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
562
563 int rc = VERR_NOT_SUPPORTED;
564
565 switch (index)
566 {
567 case VRDE_QP_NETWORK_PORT:
568 {
569 /* This is obsolete, the VRDE server uses VRDE_QP_NETWORK_PORT_RANGE instead. */
570 ULONG port = 0;
571
572 if (cbBuffer >= sizeof(uint32_t))
573 {
574 *(uint32_t *)pvBuffer = (uint32_t)port;
575 rc = VINF_SUCCESS;
576 }
577 else
578 {
579 rc = VINF_BUFFER_OVERFLOW;
580 }
581
582 *pcbOut = sizeof(uint32_t);
583 } break;
584
585 case VRDE_QP_NETWORK_ADDRESS:
586 {
587 com::Bstr bstr;
588 server->mConsole->getVRDEServer()->GetVRDEProperty(Bstr("TCP/Address").raw(), bstr.asOutParam());
589
590 /* The server expects UTF8. */
591 com::Utf8Str address = bstr;
592
593 size_t cbAddress = address.length() + 1;
594
595 if (cbAddress >= 0x10000)
596 {
597 /* More than 64K seems to be an invalid address. */
598 rc = VERR_TOO_MUCH_DATA;
599 break;
600 }
601
602 if ((size_t)cbBuffer >= cbAddress)
603 {
604 memcpy(pvBuffer, address.c_str(), cbAddress);
605 rc = VINF_SUCCESS;
606 }
607 else
608 {
609 rc = VINF_BUFFER_OVERFLOW;
610 }
611
612 *pcbOut = (uint32_t)cbAddress;
613 } break;
614
615 case VRDE_QP_NUMBER_MONITORS:
616 {
617 ULONG cMonitors = 1;
618
619 server->mConsole->machine()->COMGETTER(MonitorCount)(&cMonitors);
620
621 if (cbBuffer >= sizeof(uint32_t))
622 {
623 *(uint32_t *)pvBuffer = (uint32_t)cMonitors;
624 rc = VINF_SUCCESS;
625 }
626 else
627 {
628 rc = VINF_BUFFER_OVERFLOW;
629 }
630
631 *pcbOut = sizeof(uint32_t);
632 } break;
633
634 case VRDE_QP_NETWORK_PORT_RANGE:
635 {
636 com::Bstr bstr;
637 HRESULT hrc = server->mConsole->getVRDEServer()->GetVRDEProperty(Bstr("TCP/Ports").raw(), bstr.asOutParam());
638
639 if (hrc != S_OK)
640 {
641 bstr = "";
642 }
643
644 if (bstr == "0")
645 {
646 bstr = "3389";
647 }
648
649 /* The server expects UTF8. */
650 com::Utf8Str portRange = bstr;
651
652 size_t cbPortRange = portRange.length() + 1;
653
654 if (cbPortRange >= 0x10000)
655 {
656 /* More than 64K seems to be an invalid port range string. */
657 rc = VERR_TOO_MUCH_DATA;
658 break;
659 }
660
661 if ((size_t)cbBuffer >= cbPortRange)
662 {
663 memcpy(pvBuffer, portRange.c_str(), cbPortRange);
664 rc = VINF_SUCCESS;
665 }
666 else
667 {
668 rc = VINF_BUFFER_OVERFLOW;
669 }
670
671 *pcbOut = (uint32_t)cbPortRange;
672 } break;
673
674 case VRDE_QP_VIDEO_CHANNEL:
675 {
676 com::Bstr bstr;
677 HRESULT hrc = server->mConsole->getVRDEServer()->GetVRDEProperty(Bstr("VideoChannel/Enabled").raw(), bstr.asOutParam());
678
679 if (hrc != S_OK)
680 {
681 bstr = "";
682 }
683
684 com::Utf8Str value = bstr;
685
686 BOOL fVideoEnabled = RTStrICmp(value.c_str(), "true") == 0
687 || RTStrICmp(value.c_str(), "1") == 0;
688
689 if (cbBuffer >= sizeof(uint32_t))
690 {
691 *(uint32_t *)pvBuffer = (uint32_t)fVideoEnabled;
692 rc = VINF_SUCCESS;
693 }
694 else
695 {
696 rc = VINF_BUFFER_OVERFLOW;
697 }
698
699 *pcbOut = sizeof(uint32_t);
700 } break;
701
702 case VRDE_QP_VIDEO_CHANNEL_QUALITY:
703 {
704 com::Bstr bstr;
705 HRESULT hrc = server->mConsole->getVRDEServer()->GetVRDEProperty(Bstr("VideoChannel/Quality").raw(), bstr.asOutParam());
706
707 if (hrc != S_OK)
708 {
709 bstr = "";
710 }
711
712 com::Utf8Str value = bstr;
713
714 ULONG ulQuality = RTStrToUInt32(value.c_str()); /* This returns 0 on invalid string which is ok. */
715
716 if (cbBuffer >= sizeof(uint32_t))
717 {
718 *(uint32_t *)pvBuffer = (uint32_t)ulQuality;
719 rc = VINF_SUCCESS;
720 }
721 else
722 {
723 rc = VINF_BUFFER_OVERFLOW;
724 }
725
726 *pcbOut = sizeof(uint32_t);
727 } break;
728
729 case VRDE_QP_VIDEO_CHANNEL_SUNFLSH:
730 {
731 ULONG ulSunFlsh = 1;
732
733 com::Bstr bstr;
734 HRESULT hrc = server->mConsole->machine()->GetExtraData(Bstr("VRDP/SunFlsh").raw(),
735 bstr.asOutParam());
736 if (hrc == S_OK && !bstr.isEmpty())
737 {
738 com::Utf8Str sunFlsh = bstr;
739 if (!sunFlsh.isEmpty())
740 {
741 ulSunFlsh = sunFlsh.toUInt32();
742 }
743 }
744
745 if (cbBuffer >= sizeof(uint32_t))
746 {
747 *(uint32_t *)pvBuffer = (uint32_t)ulSunFlsh;
748 rc = VINF_SUCCESS;
749 }
750 else
751 {
752 rc = VINF_BUFFER_OVERFLOW;
753 }
754
755 *pcbOut = sizeof(uint32_t);
756 } break;
757
758 case VRDE_QP_FEATURE:
759 {
760 if (cbBuffer < sizeof(VRDEFEATURE))
761 {
762 rc = VERR_INVALID_PARAMETER;
763 break;
764 }
765
766 size_t cbInfo = cbBuffer - RT_OFFSETOF(VRDEFEATURE, achInfo);
767
768 VRDEFEATURE *pFeature = (VRDEFEATURE *)pvBuffer;
769
770 size_t cchInfo = 0;
771 rc = RTStrNLenEx(pFeature->achInfo, cbInfo, &cchInfo);
772
773 if (RT_FAILURE(rc))
774 {
775 rc = VERR_INVALID_PARAMETER;
776 break;
777 }
778
779 Log(("VRDE_QP_FEATURE [%s]\n", pFeature->achInfo));
780
781 com::Bstr bstrValue;
782
783 if ( RTStrICmp(pFeature->achInfo, "Client/DisableDisplay") == 0
784 || RTStrICmp(pFeature->achInfo, "Client/DisableInput") == 0
785 || RTStrICmp(pFeature->achInfo, "Client/DisableAudio") == 0
786 || RTStrICmp(pFeature->achInfo, "Client/DisableUSB") == 0
787 || RTStrICmp(pFeature->achInfo, "Client/DisableClipboard") == 0
788 )
789 {
790 /* @todo these features should be per client. */
791 NOREF(pFeature->u32ClientId);
792
793 /* These features are mapped to "VRDE/Feature/NAME" extra data. */
794 com::Utf8Str extraData("VRDE/Feature/");
795 extraData += pFeature->achInfo;
796
797 HRESULT hrc = server->mConsole->machine()->GetExtraData(com::Bstr(extraData).raw(),
798 bstrValue.asOutParam());
799 if (FAILED(hrc) || bstrValue.isEmpty())
800 {
801 /* Also try the old "VRDP/Feature/NAME" */
802 extraData = "VRDP/Feature/";
803 extraData += pFeature->achInfo;
804
805 hrc = server->mConsole->machine()->GetExtraData(com::Bstr(extraData).raw(),
806 bstrValue.asOutParam());
807 if (FAILED(hrc))
808 {
809 rc = VERR_NOT_SUPPORTED;
810 }
811 }
812 }
813 else if (RTStrNCmp(pFeature->achInfo, "Property/", 9) == 0)
814 {
815 /* Generic properties. */
816 const char *pszPropertyName = &pFeature->achInfo[9];
817 HRESULT hrc = server->mConsole->getVRDEServer()->GetVRDEProperty(Bstr(pszPropertyName).raw(), bstrValue.asOutParam());
818 if (FAILED(hrc))
819 {
820 rc = VERR_NOT_SUPPORTED;
821 }
822 }
823 else
824 {
825 rc = VERR_NOT_SUPPORTED;
826 }
827
828 /* Copy the value string to the callers buffer. */
829 if (rc == VINF_SUCCESS)
830 {
831 com::Utf8Str value = bstrValue;
832
833 size_t cb = value.length() + 1;
834
835 if ((size_t)cbInfo >= cb)
836 {
837 memcpy(pFeature->achInfo, value.c_str(), cb);
838 }
839 else
840 {
841 rc = VINF_BUFFER_OVERFLOW;
842 }
843
844 *pcbOut = (uint32_t)cb;
845 }
846 } break;
847
848 case VRDE_SP_NETWORK_BIND_PORT:
849 {
850 if (cbBuffer != sizeof(uint32_t))
851 {
852 rc = VERR_INVALID_PARAMETER;
853 break;
854 }
855
856 ULONG port = *(uint32_t *)pvBuffer;
857
858 server->mVRDPBindPort = port;
859
860 rc = VINF_SUCCESS;
861
862 if (pcbOut)
863 {
864 *pcbOut = sizeof(uint32_t);
865 }
866
867 server->mConsole->onVRDEServerInfoChange();
868 } break;
869
870 case VRDE_SP_CLIENT_STATUS:
871 {
872 if (cbBuffer < sizeof(VRDECLIENTSTATUS))
873 {
874 rc = VERR_INVALID_PARAMETER;
875 break;
876 }
877
878 size_t cbStatus = cbBuffer - RT_UOFFSETOF(VRDECLIENTSTATUS, achStatus);
879
880 VRDECLIENTSTATUS *pStatus = (VRDECLIENTSTATUS *)pvBuffer;
881
882 if (cbBuffer < RT_UOFFSETOF(VRDECLIENTSTATUS, achStatus) + pStatus->cbStatus)
883 {
884 rc = VERR_INVALID_PARAMETER;
885 break;
886 }
887
888 size_t cchStatus = 0;
889 rc = RTStrNLenEx(pStatus->achStatus, cbStatus, &cchStatus);
890
891 if (RT_FAILURE(rc))
892 {
893 rc = VERR_INVALID_PARAMETER;
894 break;
895 }
896
897 Log(("VRDE_SP_CLIENT_STATUS [%s]\n", pStatus->achStatus));
898
899 server->mConsole->VRDPClientStatusChange(pStatus->u32ClientId, pStatus->achStatus);
900
901 rc = VINF_SUCCESS;
902
903 if (pcbOut)
904 {
905 *pcbOut = cbBuffer;
906 }
907
908 server->mConsole->onVRDEServerInfoChange();
909 } break;
910
911 default:
912 break;
913 }
914
915 return rc;
916}
917
918DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackClientLogon(void *pvCallback, uint32_t u32ClientId, const char *pszUser, const char *pszPassword, const char *pszDomain)
919{
920 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
921
922 return server->mConsole->VRDPClientLogon(u32ClientId, pszUser, pszPassword, pszDomain);
923}
924
925DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackClientConnect(void *pvCallback, uint32_t u32ClientId)
926{
927 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
928
929 server->mConsole->VRDPClientConnect(u32ClientId);
930
931 /* Should the server report usage of an interface for each client?
932 * Similar to Intercept.
933 */
934 int c = ASMAtomicIncS32(&server->mcClients);
935 if (c == 1)
936 {
937 /* Features which should be enabled only if there is a client. */
938 server->remote3DRedirect(true);
939 }
940}
941
942DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackClientDisconnect(void *pvCallback, uint32_t u32ClientId, uint32_t fu32Intercepted)
943{
944 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
945
946 server->mConsole->VRDPClientDisconnect(u32ClientId, fu32Intercepted);
947
948 if (ASMAtomicReadU32(&server->mu32AudioInputClientId) == u32ClientId)
949 {
950 Log(("AUDIOIN: disconnected client %u\n", u32ClientId));
951 ASMAtomicWriteU32(&server->mu32AudioInputClientId, 0);
952
953#ifdef VBOX_WITH_PDM_AUDIO_DRIVER
954 server->mConsole->getAudioVRDE()->handleVRDESvrCmdAudioInputIntercept(false);
955#else
956 PPDMIAUDIOSNIFFERPORT pPort = server->mConsole->getAudioSniffer()->getAudioSnifferPort();
957 if (pPort)
958 {
959 pPort->pfnAudioInputIntercept(pPort, false);
960 }
961 else
962 {
963 AssertFailed();
964 }
965#endif
966 }
967
968 int c = ASMAtomicDecS32(&server->mcClients);
969 if (c == 0)
970 {
971 /* Features which should be enabled only if there is a client. */
972 server->remote3DRedirect(false);
973 }
974}
975
976DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackIntercept(void *pvCallback, uint32_t u32ClientId, uint32_t fu32Intercept, void **ppvIntercept)
977{
978 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
979
980 LogFlowFunc(("%x\n", fu32Intercept));
981
982 int rc = VERR_NOT_SUPPORTED;
983
984 switch (fu32Intercept)
985 {
986 case VRDE_CLIENT_INTERCEPT_AUDIO:
987 {
988 server->mConsole->VRDPInterceptAudio(u32ClientId);
989 if (ppvIntercept)
990 {
991 *ppvIntercept = server;
992 }
993 rc = VINF_SUCCESS;
994 } break;
995
996 case VRDE_CLIENT_INTERCEPT_USB:
997 {
998 server->mConsole->VRDPInterceptUSB(u32ClientId, ppvIntercept);
999 rc = VINF_SUCCESS;
1000 } break;
1001
1002 case VRDE_CLIENT_INTERCEPT_CLIPBOARD:
1003 {
1004 server->mConsole->VRDPInterceptClipboard(u32ClientId);
1005 if (ppvIntercept)
1006 {
1007 *ppvIntercept = server;
1008 }
1009 rc = VINF_SUCCESS;
1010 } break;
1011
1012 case VRDE_CLIENT_INTERCEPT_AUDIO_INPUT:
1013 {
1014 /* This request is processed internally by the ConsoleVRDPServer.
1015 * Only one client is allowed to intercept audio input.
1016 */
1017 if (ASMAtomicCmpXchgU32(&server->mu32AudioInputClientId, u32ClientId, 0) == true)
1018 {
1019 Log(("AUDIOIN: connected client %u\n", u32ClientId));
1020#ifdef VBOX_WITH_PDM_AUDIO_DRIVER
1021 server->mConsole->getAudioVRDE()->handleVRDESvrCmdAudioInputIntercept(true);
1022#else
1023 PPDMIAUDIOSNIFFERPORT pPort = server->mConsole->getAudioSniffer()->getAudioSnifferPort();
1024 if (pPort)
1025 {
1026 pPort->pfnAudioInputIntercept(pPort, true);
1027 if (ppvIntercept)
1028 {
1029 *ppvIntercept = server;
1030 }
1031 }
1032 else
1033 {
1034 AssertFailed();
1035 ASMAtomicWriteU32(&server->mu32AudioInputClientId, 0);
1036 rc = VERR_NOT_SUPPORTED;
1037 }
1038#endif
1039 }
1040 else
1041 {
1042 Log(("AUDIOIN: ignored client %u, active client %u\n", u32ClientId, server->mu32AudioInputClientId));
1043 rc = VERR_NOT_SUPPORTED;
1044 }
1045 } break;
1046
1047 default:
1048 break;
1049 }
1050
1051 return rc;
1052}
1053
1054DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackUSB(void *pvCallback, void *pvIntercept, uint32_t u32ClientId, uint8_t u8Code, const void *pvRet, uint32_t cbRet)
1055{
1056#ifdef VBOX_WITH_USB
1057 return USBClientResponseCallback(pvIntercept, u32ClientId, u8Code, pvRet, cbRet);
1058#else
1059 return VERR_NOT_SUPPORTED;
1060#endif
1061}
1062
1063DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackClipboard(void *pvCallback, void *pvIntercept, uint32_t u32ClientId, uint32_t u32Function, uint32_t u32Format, const void *pvData, uint32_t cbData)
1064{
1065 return ClipboardCallback(pvIntercept, u32ClientId, u32Function, u32Format, pvData, cbData);
1066}
1067
1068DECLCALLBACK(bool) ConsoleVRDPServer::VRDPCallbackFramebufferQuery(void *pvCallback, unsigned uScreenId, VRDEFRAMEBUFFERINFO *pInfo)
1069{
1070 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1071
1072 bool fAvailable = false;
1073
1074 IFramebuffer *pfb = NULL;
1075 LONG xOrigin = 0;
1076 LONG yOrigin = 0;
1077
1078 server->mConsole->getDisplay()->GetFramebuffer(uScreenId, &pfb, &xOrigin, &yOrigin);
1079
1080 if (pfb)
1081 {
1082 pfb->Lock ();
1083
1084 /* Query framebuffer parameters. */
1085 ULONG lineSize = 0;
1086 pfb->COMGETTER(BytesPerLine)(&lineSize);
1087
1088 ULONG bitsPerPixel = 0;
1089 pfb->COMGETTER(BitsPerPixel)(&bitsPerPixel);
1090
1091 BYTE *address = NULL;
1092 pfb->COMGETTER(Address)(&address);
1093
1094 ULONG height = 0;
1095 pfb->COMGETTER(Height)(&height);
1096
1097 ULONG width = 0;
1098 pfb->COMGETTER(Width)(&width);
1099
1100 /* Now fill the information as requested by the caller. */
1101 pInfo->pu8Bits = address;
1102 pInfo->xOrigin = xOrigin;
1103 pInfo->yOrigin = yOrigin;
1104 pInfo->cWidth = width;
1105 pInfo->cHeight = height;
1106 pInfo->cBitsPerPixel = bitsPerPixel;
1107 pInfo->cbLine = lineSize;
1108
1109 pfb->Unlock();
1110
1111 fAvailable = true;
1112 }
1113
1114 if (server->maFramebuffers[uScreenId])
1115 {
1116 server->maFramebuffers[uScreenId]->Release();
1117 }
1118 server->maFramebuffers[uScreenId] = pfb;
1119
1120 return fAvailable;
1121}
1122
1123DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackFramebufferLock(void *pvCallback, unsigned uScreenId)
1124{
1125 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1126
1127 if (server->maFramebuffers[uScreenId])
1128 {
1129 server->maFramebuffers[uScreenId]->Lock();
1130 }
1131}
1132
1133DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackFramebufferUnlock(void *pvCallback, unsigned uScreenId)
1134{
1135 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1136
1137 if (server->maFramebuffers[uScreenId])
1138 {
1139 server->maFramebuffers[uScreenId]->Unlock();
1140 }
1141}
1142
1143static void fixKbdLockStatus(VRDPInputSynch *pInputSynch, IKeyboard *pKeyboard)
1144{
1145 if ( pInputSynch->cGuestNumLockAdaptions
1146 && (pInputSynch->fGuestNumLock != pInputSynch->fClientNumLock))
1147 {
1148 pInputSynch->cGuestNumLockAdaptions--;
1149 pKeyboard->PutScancode(0x45);
1150 pKeyboard->PutScancode(0x45 | 0x80);
1151 }
1152 if ( pInputSynch->cGuestCapsLockAdaptions
1153 && (pInputSynch->fGuestCapsLock != pInputSynch->fClientCapsLock))
1154 {
1155 pInputSynch->cGuestCapsLockAdaptions--;
1156 pKeyboard->PutScancode(0x3a);
1157 pKeyboard->PutScancode(0x3a | 0x80);
1158 }
1159}
1160
1161DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackInput(void *pvCallback, int type, const void *pvInput, unsigned cbInput)
1162{
1163 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1164 Console *pConsole = server->mConsole;
1165
1166 switch (type)
1167 {
1168 case VRDE_INPUT_SCANCODE:
1169 {
1170 if (cbInput == sizeof(VRDEINPUTSCANCODE))
1171 {
1172 IKeyboard *pKeyboard = pConsole->getKeyboard();
1173
1174 const VRDEINPUTSCANCODE *pInputScancode = (VRDEINPUTSCANCODE *)pvInput;
1175
1176 /* Track lock keys. */
1177 if (pInputScancode->uScancode == 0x45)
1178 {
1179 server->m_InputSynch.fClientNumLock = !server->m_InputSynch.fClientNumLock;
1180 }
1181 else if (pInputScancode->uScancode == 0x3a)
1182 {
1183 server->m_InputSynch.fClientCapsLock = !server->m_InputSynch.fClientCapsLock;
1184 }
1185 else if (pInputScancode->uScancode == 0x46)
1186 {
1187 server->m_InputSynch.fClientScrollLock = !server->m_InputSynch.fClientScrollLock;
1188 }
1189 else if ((pInputScancode->uScancode & 0x80) == 0)
1190 {
1191 /* Key pressed. */
1192 fixKbdLockStatus(&server->m_InputSynch, pKeyboard);
1193 }
1194
1195 pKeyboard->PutScancode((LONG)pInputScancode->uScancode);
1196 }
1197 } break;
1198
1199 case VRDE_INPUT_POINT:
1200 {
1201 if (cbInput == sizeof(VRDEINPUTPOINT))
1202 {
1203 const VRDEINPUTPOINT *pInputPoint = (VRDEINPUTPOINT *)pvInput;
1204
1205 int mouseButtons = 0;
1206 int iWheel = 0;
1207
1208 if (pInputPoint->uButtons & VRDE_INPUT_POINT_BUTTON1)
1209 {
1210 mouseButtons |= MouseButtonState_LeftButton;
1211 }
1212 if (pInputPoint->uButtons & VRDE_INPUT_POINT_BUTTON2)
1213 {
1214 mouseButtons |= MouseButtonState_RightButton;
1215 }
1216 if (pInputPoint->uButtons & VRDE_INPUT_POINT_BUTTON3)
1217 {
1218 mouseButtons |= MouseButtonState_MiddleButton;
1219 }
1220 if (pInputPoint->uButtons & VRDE_INPUT_POINT_WHEEL_UP)
1221 {
1222 mouseButtons |= MouseButtonState_WheelUp;
1223 iWheel = -1;
1224 }
1225 if (pInputPoint->uButtons & VRDE_INPUT_POINT_WHEEL_DOWN)
1226 {
1227 mouseButtons |= MouseButtonState_WheelDown;
1228 iWheel = 1;
1229 }
1230
1231 if (server->m_fGuestWantsAbsolute)
1232 {
1233 pConsole->getMouse()->PutMouseEventAbsolute(pInputPoint->x + 1, pInputPoint->y + 1, iWheel, 0 /* Horizontal wheel */, mouseButtons);
1234 } else
1235 {
1236 pConsole->getMouse()->PutMouseEvent(pInputPoint->x - server->m_mousex,
1237 pInputPoint->y - server->m_mousey,
1238 iWheel, 0 /* Horizontal wheel */, mouseButtons);
1239 server->m_mousex = pInputPoint->x;
1240 server->m_mousey = pInputPoint->y;
1241 }
1242 }
1243 } break;
1244
1245 case VRDE_INPUT_CAD:
1246 {
1247 pConsole->getKeyboard()->PutCAD();
1248 } break;
1249
1250 case VRDE_INPUT_RESET:
1251 {
1252 pConsole->Reset();
1253 } break;
1254
1255 case VRDE_INPUT_SYNCH:
1256 {
1257 if (cbInput == sizeof(VRDEINPUTSYNCH))
1258 {
1259 IKeyboard *pKeyboard = pConsole->getKeyboard();
1260
1261 const VRDEINPUTSYNCH *pInputSynch = (VRDEINPUTSYNCH *)pvInput;
1262
1263 server->m_InputSynch.fClientNumLock = (pInputSynch->uLockStatus & VRDE_INPUT_SYNCH_NUMLOCK) != 0;
1264 server->m_InputSynch.fClientCapsLock = (pInputSynch->uLockStatus & VRDE_INPUT_SYNCH_CAPITAL) != 0;
1265 server->m_InputSynch.fClientScrollLock = (pInputSynch->uLockStatus & VRDE_INPUT_SYNCH_SCROLL) != 0;
1266
1267 /* The client initiated synchronization. Always make the guest to reflect the client state.
1268 * Than means, when the guest changes the state itself, it is forced to return to the client
1269 * state.
1270 */
1271 if (server->m_InputSynch.fClientNumLock != server->m_InputSynch.fGuestNumLock)
1272 {
1273 server->m_InputSynch.cGuestNumLockAdaptions = 2;
1274 }
1275
1276 if (server->m_InputSynch.fClientCapsLock != server->m_InputSynch.fGuestCapsLock)
1277 {
1278 server->m_InputSynch.cGuestCapsLockAdaptions = 2;
1279 }
1280
1281 fixKbdLockStatus(&server->m_InputSynch, pKeyboard);
1282 }
1283 } break;
1284
1285 default:
1286 break;
1287 }
1288}
1289
1290DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackVideoModeHint(void *pvCallback, unsigned cWidth, unsigned cHeight, unsigned cBitsPerPixel, unsigned uScreenId)
1291{
1292 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1293
1294 server->mConsole->getDisplay()->SetVideoModeHint(uScreenId, TRUE /*=enabled*/,
1295 FALSE /*=changeOrigin*/, 0/*=OriginX*/, 0/*=OriginY*/,
1296 cWidth, cHeight, cBitsPerPixel);
1297}
1298
1299DECLCALLBACK(void) ConsoleVRDPServer::VRDECallbackAudioIn(void *pvCallback,
1300 void *pvCtx,
1301 uint32_t u32ClientId,
1302 uint32_t u32Event,
1303 const void *pvData,
1304 uint32_t cbData)
1305{
1306 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1307#ifndef VBOX_WITH_PDM_AUDIO_DRIVER
1308 PPDMIAUDIOSNIFFERPORT pPort = server->mConsole->getAudioSniffer()->getAudioSnifferPort();
1309#endif
1310
1311 switch (u32Event)
1312 {
1313 case VRDE_AUDIOIN_BEGIN:
1314 {
1315 const VRDEAUDIOINBEGIN *pParms = (const VRDEAUDIOINBEGIN *)pvData;
1316#ifdef VBOX_WITH_PDM_AUDIO_DRIVER
1317 server->mConsole->getAudioVRDE()->handleVRDESvrCmdAudioInputEventBegin(pvCtx,
1318 VRDE_AUDIO_FMT_SAMPLE_FREQ(pParms->fmt),
1319 VRDE_AUDIO_FMT_CHANNELS(pParms->fmt),
1320 VRDE_AUDIO_FMT_BITS_PER_SAMPLE(pParms->fmt),
1321 VRDE_AUDIO_FMT_SIGNED(pParms->fmt)
1322 );
1323#else
1324 pPort->pfnAudioInputEventBegin (pPort, pvCtx,
1325 VRDE_AUDIO_FMT_SAMPLE_FREQ(pParms->fmt),
1326 VRDE_AUDIO_FMT_CHANNELS(pParms->fmt),
1327 VRDE_AUDIO_FMT_BITS_PER_SAMPLE(pParms->fmt),
1328 VRDE_AUDIO_FMT_SIGNED(pParms->fmt)
1329 );
1330#endif
1331 } break;
1332
1333 case VRDE_AUDIOIN_DATA:
1334 {
1335#ifdef VBOX_WITH_PDM_AUDIO_DRIVER
1336 server->mConsole->getAudioVRDE()->handleVRDESvrCmdAudioInputEventData(pvCtx, pvData, cbData);
1337#else
1338 pPort->pfnAudioInputEventData (pPort, pvCtx, pvData, cbData);
1339#endif
1340 } break;
1341
1342 case VRDE_AUDIOIN_END:
1343 {
1344#ifdef VBOX_WITH_PDM_AUDIO_DRIVER
1345 server->mConsole->getAudioVRDE()->handleVRDESvrCmdAudioInputEventEnd(pvCtx);
1346#else
1347 pPort->pfnAudioInputEventEnd (pPort, pvCtx);
1348#endif
1349 } break;
1350
1351 default:
1352 return;
1353 }
1354}
1355
1356
1357ConsoleVRDPServer::ConsoleVRDPServer(Console *console)
1358{
1359 mConsole = console;
1360
1361 int rc = RTCritSectInit(&mCritSect);
1362 AssertRC(rc);
1363
1364 mcClipboardRefs = 0;
1365 mpfnClipboardCallback = NULL;
1366#ifdef VBOX_WITH_USB
1367 mUSBBackends.pHead = NULL;
1368 mUSBBackends.pTail = NULL;
1369
1370 mUSBBackends.thread = NIL_RTTHREAD;
1371 mUSBBackends.fThreadRunning = false;
1372 mUSBBackends.event = 0;
1373#endif
1374
1375 mhServer = 0;
1376 mServerInterfaceVersion = 0;
1377
1378 mcInResize = 0;
1379
1380 m_fGuestWantsAbsolute = false;
1381 m_mousex = 0;
1382 m_mousey = 0;
1383
1384 m_InputSynch.cGuestNumLockAdaptions = 2;
1385 m_InputSynch.cGuestCapsLockAdaptions = 2;
1386
1387 m_InputSynch.fGuestNumLock = false;
1388 m_InputSynch.fGuestCapsLock = false;
1389 m_InputSynch.fGuestScrollLock = false;
1390
1391 m_InputSynch.fClientNumLock = false;
1392 m_InputSynch.fClientCapsLock = false;
1393 m_InputSynch.fClientScrollLock = false;
1394
1395 RT_ZERO(maFramebuffers);
1396
1397 {
1398 ComPtr<IEventSource> es;
1399 console->COMGETTER(EventSource)(es.asOutParam());
1400 ComObjPtr<VRDPConsoleListenerImpl> aConsoleListener;
1401 aConsoleListener.createObject();
1402 aConsoleListener->init(new VRDPConsoleListener(), this);
1403 mConsoleListener = aConsoleListener;
1404 com::SafeArray <VBoxEventType_T> eventTypes;
1405 eventTypes.push_back(VBoxEventType_OnMousePointerShapeChanged);
1406 eventTypes.push_back(VBoxEventType_OnMouseCapabilityChanged);
1407 eventTypes.push_back(VBoxEventType_OnKeyboardLedsChanged);
1408 es->RegisterListener(mConsoleListener, ComSafeArrayAsInParam(eventTypes), true);
1409 }
1410
1411 mVRDPBindPort = -1;
1412
1413 mAuthLibrary = 0;
1414
1415 mu32AudioInputClientId = 0;
1416 mcClients = 0;
1417
1418 /*
1419 * Optional interfaces.
1420 */
1421 m_fInterfaceImage = false;
1422 RT_ZERO(m_interfaceImage);
1423 RT_ZERO(m_interfaceCallbacksImage);
1424 RT_ZERO(m_interfaceMousePtr);
1425 RT_ZERO(m_interfaceSCard);
1426 RT_ZERO(m_interfaceCallbacksSCard);
1427 RT_ZERO(m_interfaceTSMF);
1428 RT_ZERO(m_interfaceCallbacksTSMF);
1429 RT_ZERO(m_interfaceVideoIn);
1430 RT_ZERO(m_interfaceCallbacksVideoIn);
1431 RT_ZERO(m_interfaceInput);
1432 RT_ZERO(m_interfaceCallbacksInput);
1433
1434 rc = RTCritSectInit(&mTSMFLock);
1435 AssertRC(rc);
1436
1437 mEmWebcam = new EmWebcam(this);
1438 AssertPtr(mEmWebcam);
1439}
1440
1441ConsoleVRDPServer::~ConsoleVRDPServer()
1442{
1443 Stop();
1444
1445 if (mConsoleListener)
1446 {
1447 ComPtr<IEventSource> es;
1448 mConsole->COMGETTER(EventSource)(es.asOutParam());
1449 es->UnregisterListener(mConsoleListener);
1450 mConsoleListener.setNull();
1451 }
1452
1453 unsigned i;
1454 for (i = 0; i < RT_ELEMENTS(maFramebuffers); i++)
1455 {
1456 if (maFramebuffers[i])
1457 {
1458 maFramebuffers[i]->Release();
1459 maFramebuffers[i] = NULL;
1460 }
1461 }
1462
1463 if (mEmWebcam)
1464 {
1465 delete mEmWebcam;
1466 mEmWebcam = NULL;
1467 }
1468
1469 if (RTCritSectIsInitialized(&mCritSect))
1470 {
1471 RTCritSectDelete(&mCritSect);
1472 RT_ZERO(mCritSect);
1473 }
1474
1475 if (RTCritSectIsInitialized(&mTSMFLock))
1476 {
1477 RTCritSectDelete(&mTSMFLock);
1478 RT_ZERO(mTSMFLock);
1479 }
1480}
1481
1482int ConsoleVRDPServer::Launch(void)
1483{
1484 LogFlowThisFunc(("\n"));
1485
1486 IVRDEServer *server = mConsole->getVRDEServer();
1487 AssertReturn(server, VERR_INTERNAL_ERROR_2);
1488
1489 /*
1490 * Check if VRDE is enabled.
1491 */
1492 BOOL fEnabled;
1493 HRESULT hrc = server->COMGETTER(Enabled)(&fEnabled);
1494 AssertComRCReturn(hrc, Global::vboxStatusCodeFromCOM(hrc));
1495 if (!fEnabled)
1496 return VINF_SUCCESS;
1497
1498 /*
1499 * Check that a VRDE extension pack name is set and resolve it into a
1500 * library path.
1501 */
1502 Bstr bstrExtPack;
1503 hrc = server->COMGETTER(VRDEExtPack)(bstrExtPack.asOutParam());
1504 if (FAILED(hrc))
1505 return Global::vboxStatusCodeFromCOM(hrc);
1506 if (bstrExtPack.isEmpty())
1507 return VINF_NOT_SUPPORTED;
1508
1509 Utf8Str strExtPack(bstrExtPack);
1510 Utf8Str strVrdeLibrary;
1511 int vrc = VINF_SUCCESS;
1512 if (strExtPack.equals(VBOXVRDP_KLUDGE_EXTPACK_NAME))
1513 strVrdeLibrary = "VBoxVRDP";
1514 else
1515 {
1516#ifdef VBOX_WITH_EXTPACK
1517 ExtPackManager *pExtPackMgr = mConsole->getExtPackManager();
1518 vrc = pExtPackMgr->i_getVrdeLibraryPathForExtPack(&strExtPack, &strVrdeLibrary);
1519#else
1520 vrc = VERR_FILE_NOT_FOUND;
1521#endif
1522 }
1523 if (RT_SUCCESS(vrc))
1524 {
1525 /*
1526 * Load the VRDE library and start the server, if it is enabled.
1527 */
1528 vrc = loadVRDPLibrary(strVrdeLibrary.c_str());
1529 if (RT_SUCCESS(vrc))
1530 {
1531 VRDEENTRYPOINTS_4 *pEntryPoints4;
1532 vrc = mpfnVRDECreateServer(&mCallbacks.header, this, (VRDEINTERFACEHDR **)&pEntryPoints4, &mhServer);
1533
1534 if (RT_SUCCESS(vrc))
1535 {
1536 mServerInterfaceVersion = 4;
1537 mEntryPoints = *pEntryPoints4;
1538 mpEntryPoints = &mEntryPoints;
1539 }
1540 else if (vrc == VERR_VERSION_MISMATCH)
1541 {
1542 /* An older version of VRDE is installed, try version 3. */
1543 VRDEENTRYPOINTS_3 *pEntryPoints3;
1544
1545 static VRDECALLBACKS_3 sCallbacks3 =
1546 {
1547 { VRDE_INTERFACE_VERSION_3, sizeof(VRDECALLBACKS_3) },
1548 ConsoleVRDPServer::VRDPCallbackQueryProperty,
1549 ConsoleVRDPServer::VRDPCallbackClientLogon,
1550 ConsoleVRDPServer::VRDPCallbackClientConnect,
1551 ConsoleVRDPServer::VRDPCallbackClientDisconnect,
1552 ConsoleVRDPServer::VRDPCallbackIntercept,
1553 ConsoleVRDPServer::VRDPCallbackUSB,
1554 ConsoleVRDPServer::VRDPCallbackClipboard,
1555 ConsoleVRDPServer::VRDPCallbackFramebufferQuery,
1556 ConsoleVRDPServer::VRDPCallbackFramebufferLock,
1557 ConsoleVRDPServer::VRDPCallbackFramebufferUnlock,
1558 ConsoleVRDPServer::VRDPCallbackInput,
1559 ConsoleVRDPServer::VRDPCallbackVideoModeHint,
1560 ConsoleVRDPServer::VRDECallbackAudioIn
1561 };
1562
1563 vrc = mpfnVRDECreateServer(&sCallbacks3.header, this, (VRDEINTERFACEHDR **)&pEntryPoints3, &mhServer);
1564 if (RT_SUCCESS(vrc))
1565 {
1566 mServerInterfaceVersion = 3;
1567 mEntryPoints.header = pEntryPoints3->header;
1568 mEntryPoints.VRDEDestroy = pEntryPoints3->VRDEDestroy;
1569 mEntryPoints.VRDEEnableConnections = pEntryPoints3->VRDEEnableConnections;
1570 mEntryPoints.VRDEDisconnect = pEntryPoints3->VRDEDisconnect;
1571 mEntryPoints.VRDEResize = pEntryPoints3->VRDEResize;
1572 mEntryPoints.VRDEUpdate = pEntryPoints3->VRDEUpdate;
1573 mEntryPoints.VRDEColorPointer = pEntryPoints3->VRDEColorPointer;
1574 mEntryPoints.VRDEHidePointer = pEntryPoints3->VRDEHidePointer;
1575 mEntryPoints.VRDEAudioSamples = pEntryPoints3->VRDEAudioSamples;
1576 mEntryPoints.VRDEAudioVolume = pEntryPoints3->VRDEAudioVolume;
1577 mEntryPoints.VRDEUSBRequest = pEntryPoints3->VRDEUSBRequest;
1578 mEntryPoints.VRDEClipboard = pEntryPoints3->VRDEClipboard;
1579 mEntryPoints.VRDEQueryInfo = pEntryPoints3->VRDEQueryInfo;
1580 mEntryPoints.VRDERedirect = pEntryPoints3->VRDERedirect;
1581 mEntryPoints.VRDEAudioInOpen = pEntryPoints3->VRDEAudioInOpen;
1582 mEntryPoints.VRDEAudioInClose = pEntryPoints3->VRDEAudioInClose;
1583 mEntryPoints.VRDEGetInterface = NULL;
1584 mpEntryPoints = &mEntryPoints;
1585 }
1586 else if (vrc == VERR_VERSION_MISMATCH)
1587 {
1588 /* An older version of VRDE is installed, try version 1. */
1589 VRDEENTRYPOINTS_1 *pEntryPoints1;
1590
1591 static VRDECALLBACKS_1 sCallbacks1 =
1592 {
1593 { VRDE_INTERFACE_VERSION_1, sizeof(VRDECALLBACKS_1) },
1594 ConsoleVRDPServer::VRDPCallbackQueryProperty,
1595 ConsoleVRDPServer::VRDPCallbackClientLogon,
1596 ConsoleVRDPServer::VRDPCallbackClientConnect,
1597 ConsoleVRDPServer::VRDPCallbackClientDisconnect,
1598 ConsoleVRDPServer::VRDPCallbackIntercept,
1599 ConsoleVRDPServer::VRDPCallbackUSB,
1600 ConsoleVRDPServer::VRDPCallbackClipboard,
1601 ConsoleVRDPServer::VRDPCallbackFramebufferQuery,
1602 ConsoleVRDPServer::VRDPCallbackFramebufferLock,
1603 ConsoleVRDPServer::VRDPCallbackFramebufferUnlock,
1604 ConsoleVRDPServer::VRDPCallbackInput,
1605 ConsoleVRDPServer::VRDPCallbackVideoModeHint
1606 };
1607
1608 vrc = mpfnVRDECreateServer(&sCallbacks1.header, this, (VRDEINTERFACEHDR **)&pEntryPoints1, &mhServer);
1609 if (RT_SUCCESS(vrc))
1610 {
1611 mServerInterfaceVersion = 1;
1612 mEntryPoints.header = pEntryPoints1->header;
1613 mEntryPoints.VRDEDestroy = pEntryPoints1->VRDEDestroy;
1614 mEntryPoints.VRDEEnableConnections = pEntryPoints1->VRDEEnableConnections;
1615 mEntryPoints.VRDEDisconnect = pEntryPoints1->VRDEDisconnect;
1616 mEntryPoints.VRDEResize = pEntryPoints1->VRDEResize;
1617 mEntryPoints.VRDEUpdate = pEntryPoints1->VRDEUpdate;
1618 mEntryPoints.VRDEColorPointer = pEntryPoints1->VRDEColorPointer;
1619 mEntryPoints.VRDEHidePointer = pEntryPoints1->VRDEHidePointer;
1620 mEntryPoints.VRDEAudioSamples = pEntryPoints1->VRDEAudioSamples;
1621 mEntryPoints.VRDEAudioVolume = pEntryPoints1->VRDEAudioVolume;
1622 mEntryPoints.VRDEUSBRequest = pEntryPoints1->VRDEUSBRequest;
1623 mEntryPoints.VRDEClipboard = pEntryPoints1->VRDEClipboard;
1624 mEntryPoints.VRDEQueryInfo = pEntryPoints1->VRDEQueryInfo;
1625 mEntryPoints.VRDERedirect = NULL;
1626 mEntryPoints.VRDEAudioInOpen = NULL;
1627 mEntryPoints.VRDEAudioInClose = NULL;
1628 mEntryPoints.VRDEGetInterface = NULL;
1629 mpEntryPoints = &mEntryPoints;
1630 }
1631 }
1632 }
1633
1634 if (RT_SUCCESS(vrc))
1635 {
1636 LogRel(("VRDE: loaded version %d of the server.\n", mServerInterfaceVersion));
1637
1638 if (mServerInterfaceVersion >= 4)
1639 {
1640 /* The server supports optional interfaces. */
1641 Assert(mpEntryPoints->VRDEGetInterface != NULL);
1642
1643 /* Image interface. */
1644 m_interfaceImage.header.u64Version = 1;
1645 m_interfaceImage.header.u64Size = sizeof(m_interfaceImage);
1646
1647 m_interfaceCallbacksImage.header.u64Version = 1;
1648 m_interfaceCallbacksImage.header.u64Size = sizeof(m_interfaceCallbacksImage);
1649 m_interfaceCallbacksImage.VRDEImageCbNotify = VRDEImageCbNotify;
1650
1651 vrc = mpEntryPoints->VRDEGetInterface(mhServer,
1652 VRDE_IMAGE_INTERFACE_NAME,
1653 &m_interfaceImage.header,
1654 &m_interfaceCallbacksImage.header,
1655 this);
1656 if (RT_SUCCESS(vrc))
1657 {
1658 LogRel(("VRDE: [%s]\n", VRDE_IMAGE_INTERFACE_NAME));
1659 m_fInterfaceImage = true;
1660 }
1661
1662 /* Mouse pointer interface. */
1663 m_interfaceMousePtr.header.u64Version = 1;
1664 m_interfaceMousePtr.header.u64Size = sizeof(m_interfaceMousePtr);
1665
1666 vrc = mpEntryPoints->VRDEGetInterface(mhServer,
1667 VRDE_MOUSEPTR_INTERFACE_NAME,
1668 &m_interfaceMousePtr.header,
1669 NULL,
1670 this);
1671 if (RT_SUCCESS(vrc))
1672 {
1673 LogRel(("VRDE: [%s]\n", VRDE_MOUSEPTR_INTERFACE_NAME));
1674 }
1675 else
1676 {
1677 RT_ZERO(m_interfaceMousePtr);
1678 }
1679
1680 /* Smartcard interface. */
1681 m_interfaceSCard.header.u64Version = 1;
1682 m_interfaceSCard.header.u64Size = sizeof(m_interfaceSCard);
1683
1684 m_interfaceCallbacksSCard.header.u64Version = 1;
1685 m_interfaceCallbacksSCard.header.u64Size = sizeof(m_interfaceCallbacksSCard);
1686 m_interfaceCallbacksSCard.VRDESCardCbNotify = VRDESCardCbNotify;
1687 m_interfaceCallbacksSCard.VRDESCardCbResponse = VRDESCardCbResponse;
1688
1689 vrc = mpEntryPoints->VRDEGetInterface(mhServer,
1690 VRDE_SCARD_INTERFACE_NAME,
1691 &m_interfaceSCard.header,
1692 &m_interfaceCallbacksSCard.header,
1693 this);
1694 if (RT_SUCCESS(vrc))
1695 {
1696 LogRel(("VRDE: [%s]\n", VRDE_SCARD_INTERFACE_NAME));
1697 }
1698 else
1699 {
1700 RT_ZERO(m_interfaceSCard);
1701 }
1702
1703 /* Raw TSMF interface. */
1704 m_interfaceTSMF.header.u64Version = 1;
1705 m_interfaceTSMF.header.u64Size = sizeof(m_interfaceTSMF);
1706
1707 m_interfaceCallbacksTSMF.header.u64Version = 1;
1708 m_interfaceCallbacksTSMF.header.u64Size = sizeof(m_interfaceCallbacksTSMF);
1709 m_interfaceCallbacksTSMF.VRDETSMFCbNotify = VRDETSMFCbNotify;
1710
1711 vrc = mpEntryPoints->VRDEGetInterface(mhServer,
1712 VRDE_TSMF_INTERFACE_NAME,
1713 &m_interfaceTSMF.header,
1714 &m_interfaceCallbacksTSMF.header,
1715 this);
1716 if (RT_SUCCESS(vrc))
1717 {
1718 LogRel(("VRDE: [%s]\n", VRDE_TSMF_INTERFACE_NAME));
1719 }
1720 else
1721 {
1722 RT_ZERO(m_interfaceTSMF);
1723 }
1724
1725 /* VideoIn interface. */
1726 m_interfaceVideoIn.header.u64Version = 1;
1727 m_interfaceVideoIn.header.u64Size = sizeof(m_interfaceVideoIn);
1728
1729 m_interfaceCallbacksVideoIn.header.u64Version = 1;
1730 m_interfaceCallbacksVideoIn.header.u64Size = sizeof(m_interfaceCallbacksVideoIn);
1731 m_interfaceCallbacksVideoIn.VRDECallbackVideoInNotify = VRDECallbackVideoInNotify;
1732 m_interfaceCallbacksVideoIn.VRDECallbackVideoInDeviceDesc = VRDECallbackVideoInDeviceDesc;
1733 m_interfaceCallbacksVideoIn.VRDECallbackVideoInControl = VRDECallbackVideoInControl;
1734 m_interfaceCallbacksVideoIn.VRDECallbackVideoInFrame = VRDECallbackVideoInFrame;
1735
1736 vrc = mpEntryPoints->VRDEGetInterface(mhServer,
1737 VRDE_VIDEOIN_INTERFACE_NAME,
1738 &m_interfaceVideoIn.header,
1739 &m_interfaceCallbacksVideoIn.header,
1740 this);
1741 if (RT_SUCCESS(vrc))
1742 {
1743 LogRel(("VRDE: [%s]\n", VRDE_VIDEOIN_INTERFACE_NAME));
1744 }
1745 else
1746 {
1747 RT_ZERO(m_interfaceVideoIn);
1748 }
1749
1750 /* Input interface. */
1751 m_interfaceInput.header.u64Version = 1;
1752 m_interfaceInput.header.u64Size = sizeof(m_interfaceInput);
1753
1754 m_interfaceCallbacksInput.header.u64Version = 1;
1755 m_interfaceCallbacksInput.header.u64Size = sizeof(m_interfaceCallbacksInput);
1756 m_interfaceCallbacksInput.VRDECallbackInputSetup = VRDECallbackInputSetup;
1757 m_interfaceCallbacksInput.VRDECallbackInputEvent = VRDECallbackInputEvent;
1758
1759 vrc = mpEntryPoints->VRDEGetInterface(mhServer,
1760 VRDE_INPUT_INTERFACE_NAME,
1761 &m_interfaceInput.header,
1762 &m_interfaceCallbacksInput.header,
1763 this);
1764 if (RT_SUCCESS(vrc))
1765 {
1766 LogRel(("VRDE: [%s]\n", VRDE_INPUT_INTERFACE_NAME));
1767 }
1768 else
1769 {
1770 RT_ZERO(m_interfaceInput);
1771 }
1772
1773 /* Since these interfaces are optional, it is always a success here. */
1774 vrc = VINF_SUCCESS;
1775 }
1776#ifdef VBOX_WITH_USB
1777 remoteUSBThreadStart();
1778#endif
1779 }
1780 else
1781 {
1782 if (vrc != VERR_NET_ADDRESS_IN_USE)
1783 LogRel(("VRDE: Could not start the server rc = %Rrc\n", vrc));
1784 /* Don't unload the lib, because it prevents us trying again or
1785 because there may be other users? */
1786 }
1787 }
1788 }
1789
1790 return vrc;
1791}
1792
1793typedef struct H3DORInstance
1794{
1795 ConsoleVRDPServer *pThis;
1796 HVRDEIMAGE hImageBitmap;
1797 int32_t x;
1798 int32_t y;
1799 uint32_t w;
1800 uint32_t h;
1801 bool fCreated;
1802 bool fFallback;
1803 bool fTopDown;
1804} H3DORInstance;
1805
1806#define H3DORLOG Log
1807
1808/* static */ DECLCALLBACK(void) ConsoleVRDPServer::H3DORBegin(const void *pvContext, void **ppvInstance,
1809 const char *pszFormat)
1810{
1811 H3DORLOG(("H3DORBegin: ctx %p [%s]\n", pvContext, pszFormat));
1812
1813 H3DORInstance *p = (H3DORInstance *)RTMemAlloc(sizeof(H3DORInstance));
1814
1815 if (p)
1816 {
1817 p->pThis = (ConsoleVRDPServer *)pvContext;
1818 p->hImageBitmap = NULL;
1819 p->x = 0;
1820 p->y = 0;
1821 p->w = 0;
1822 p->h = 0;
1823 p->fCreated = false;
1824 p->fFallback = false;
1825
1826 /* Host 3D service passes the actual format of data in this redirect instance.
1827 * That is what will be in the H3DORFrame's parameters pvData and cbData.
1828 */
1829 if (RTStrICmp(pszFormat, H3DOR_FMT_RGBA_TOPDOWN) == 0)
1830 {
1831 /* Accept it. */
1832 p->fTopDown = true;
1833 }
1834 else if (RTStrICmp(pszFormat, H3DOR_FMT_RGBA) == 0)
1835 {
1836 /* Accept it. */
1837 p->fTopDown = false;
1838 }
1839 else
1840 {
1841 RTMemFree(p);
1842 p = NULL;
1843 }
1844 }
1845
1846 H3DORLOG(("H3DORBegin: ins %p\n", p));
1847
1848 /* Caller checks this for NULL. */
1849 *ppvInstance = p;
1850}
1851
1852/* static */ DECLCALLBACK(void) ConsoleVRDPServer::H3DORGeometry(void *pvInstance,
1853 int32_t x, int32_t y, uint32_t w, uint32_t h)
1854{
1855 H3DORLOG(("H3DORGeometry: ins %p %d,%d %dx%d\n", pvInstance, x, y, w, h));
1856
1857 H3DORInstance *p = (H3DORInstance *)pvInstance;
1858 Assert(p);
1859 Assert(p->pThis);
1860
1861 /* @todo find out what to do if size changes to 0x0 from non zero */
1862 if (w == 0 || h == 0)
1863 {
1864 /* Do nothing. */
1865 return;
1866 }
1867
1868 RTRECT rect;
1869 rect.xLeft = x;
1870 rect.yTop = y;
1871 rect.xRight = x + w;
1872 rect.yBottom = y + h;
1873
1874 if (p->hImageBitmap)
1875 {
1876 /* An image handle has been already created,
1877 * check if it has the same size as the reported geometry.
1878 */
1879 if ( p->x == x
1880 && p->y == y
1881 && p->w == w
1882 && p->h == h)
1883 {
1884 H3DORLOG(("H3DORGeometry: geometry not changed\n"));
1885 /* Do nothing. Continue using the existing handle. */
1886 }
1887 else
1888 {
1889 int rc = p->fFallback?
1890 VERR_NOT_SUPPORTED: /* Try to go out of fallback mode. */
1891 p->pThis->m_interfaceImage.VRDEImageGeometrySet(p->hImageBitmap, &rect);
1892 if (RT_SUCCESS(rc))
1893 {
1894 p->x = x;
1895 p->y = y;
1896 p->w = w;
1897 p->h = h;
1898 }
1899 else
1900 {
1901 /* The handle must be recreated. Delete existing handle here. */
1902 p->pThis->m_interfaceImage.VRDEImageHandleClose(p->hImageBitmap);
1903 p->hImageBitmap = NULL;
1904 }
1905 }
1906 }
1907
1908 if (!p->hImageBitmap)
1909 {
1910 /* Create a new bitmap handle. */
1911 uint32_t u32ScreenId = 0; /* @todo clip to corresponding screens.
1912 * Clipping can be done here or in VRDP server.
1913 * If VRDP does clipping, then uScreenId parameter
1914 * is not necessary and coords must be global.
1915 * (have to check which coords are used in opengl service).
1916 * Since all VRDE API uses a ScreenId,
1917 * the clipping must be done here in ConsoleVRDPServer
1918 */
1919 uint32_t fu32CompletionFlags = 0;
1920 p->fFallback = false;
1921 int rc = p->pThis->m_interfaceImage.VRDEImageHandleCreate(p->pThis->mhServer,
1922 &p->hImageBitmap,
1923 p,
1924 u32ScreenId,
1925 VRDE_IMAGE_F_CREATE_CONTENT_3D
1926 | VRDE_IMAGE_F_CREATE_WINDOW,
1927 &rect,
1928 VRDE_IMAGE_FMT_ID_BITMAP_BGRA8,
1929 NULL,
1930 0,
1931 &fu32CompletionFlags);
1932 if (RT_FAILURE(rc))
1933 {
1934 /* No support for a 3D + WINDOW. Try bitmap updates. */
1935 H3DORLOG(("H3DORGeometry: Fallback to bitmaps\n"));
1936 fu32CompletionFlags = 0;
1937 p->fFallback = true;
1938 rc = p->pThis->m_interfaceImage.VRDEImageHandleCreate(p->pThis->mhServer,
1939 &p->hImageBitmap,
1940 p,
1941 u32ScreenId,
1942 0,
1943 &rect,
1944 VRDE_IMAGE_FMT_ID_BITMAP_BGRA8,
1945 NULL,
1946 0,
1947 &fu32CompletionFlags);
1948 }
1949
1950 H3DORLOG(("H3DORGeometry: Image handle create %Rrc, flags 0x%RX32\n", rc, fu32CompletionFlags));
1951
1952 if (RT_SUCCESS(rc))
1953 {
1954 p->x = x;
1955 p->y = y;
1956 p->w = w;
1957 p->h = h;
1958
1959 if ((fu32CompletionFlags & VRDE_IMAGE_F_COMPLETE_ASYNC) == 0)
1960 {
1961 p->fCreated = true;
1962 }
1963 }
1964 else
1965 {
1966 p->hImageBitmap = NULL;
1967 p->w = 0;
1968 p->h = 0;
1969 }
1970 }
1971
1972 H3DORLOG(("H3DORGeometry: ins %p completed\n", pvInstance));
1973}
1974
1975/* static */ DECLCALLBACK(void) ConsoleVRDPServer::H3DORVisibleRegion(void *pvInstance,
1976 uint32_t cRects, const RTRECT *paRects)
1977{
1978 H3DORLOG(("H3DORVisibleRegion: ins %p %d\n", pvInstance, cRects));
1979
1980 H3DORInstance *p = (H3DORInstance *)pvInstance;
1981 Assert(p);
1982 Assert(p->pThis);
1983
1984 if (cRects == 0)
1985 {
1986 /* Complete image is visible. */
1987 RTRECT rect;
1988 rect.xLeft = p->x;
1989 rect.yTop = p->y;
1990 rect.xRight = p->x + p->w;
1991 rect.yBottom = p->y + p->h;
1992 p->pThis->m_interfaceImage.VRDEImageRegionSet (p->hImageBitmap,
1993 1,
1994 &rect);
1995 }
1996 else
1997 {
1998 p->pThis->m_interfaceImage.VRDEImageRegionSet (p->hImageBitmap,
1999 cRects,
2000 paRects);
2001 }
2002
2003 H3DORLOG(("H3DORVisibleRegion: ins %p completed\n", pvInstance));
2004}
2005
2006/* static */ DECLCALLBACK(void) ConsoleVRDPServer::H3DORFrame(void *pvInstance,
2007 void *pvData, uint32_t cbData)
2008{
2009 H3DORLOG(("H3DORFrame: ins %p %p %d\n", pvInstance, pvData, cbData));
2010
2011 H3DORInstance *p = (H3DORInstance *)pvInstance;
2012 Assert(p);
2013 Assert(p->pThis);
2014
2015 /* Currently only a topdown BGR0 bitmap format is supported. */
2016 VRDEIMAGEBITMAP image;
2017
2018 image.cWidth = p->w;
2019 image.cHeight = p->h;
2020 image.pvData = pvData;
2021 image.cbData = cbData;
2022 image.pvScanLine0 = (uint8_t *)pvData + (p->h - 1) * p->w * 4;
2023 image.iScanDelta = 4 * p->w;
2024 if (p->fTopDown)
2025 {
2026 image.iScanDelta = -image.iScanDelta;
2027 }
2028
2029 p->pThis->m_interfaceImage.VRDEImageUpdate (p->hImageBitmap,
2030 p->x,
2031 p->y,
2032 p->w,
2033 p->h,
2034 &image,
2035 sizeof(VRDEIMAGEBITMAP));
2036
2037 H3DORLOG(("H3DORFrame: ins %p completed\n", pvInstance));
2038}
2039
2040/* static */ DECLCALLBACK(void) ConsoleVRDPServer::H3DOREnd(void *pvInstance)
2041{
2042 H3DORLOG(("H3DOREnd: ins %p\n", pvInstance));
2043
2044 H3DORInstance *p = (H3DORInstance *)pvInstance;
2045 Assert(p);
2046 Assert(p->pThis);
2047
2048 p->pThis->m_interfaceImage.VRDEImageHandleClose(p->hImageBitmap);
2049
2050 RTMemFree(p);
2051
2052 H3DORLOG(("H3DOREnd: ins %p completed\n", pvInstance));
2053}
2054
2055/* static */ DECLCALLBACK(int) ConsoleVRDPServer::H3DORContextProperty(const void *pvContext, uint32_t index,
2056 void *pvBuffer, uint32_t cbBuffer, uint32_t *pcbOut)
2057{
2058 int rc = VINF_SUCCESS;
2059
2060 H3DORLOG(("H3DORContextProperty: index %d\n", index));
2061
2062 if (index == H3DOR_PROP_FORMATS)
2063 {
2064 /* Return a comma separated list of supported formats. */
2065 uint32_t cbOut = (uint32_t)strlen(H3DOR_FMT_RGBA_TOPDOWN) + 1
2066 + (uint32_t)strlen(H3DOR_FMT_RGBA) + 1;
2067 if (cbOut <= cbBuffer)
2068 {
2069 char *pch = (char *)pvBuffer;
2070 memcpy(pch, H3DOR_FMT_RGBA_TOPDOWN, strlen(H3DOR_FMT_RGBA_TOPDOWN));
2071 pch += strlen(H3DOR_FMT_RGBA_TOPDOWN);
2072 *pch++ = ',';
2073 memcpy(pch, H3DOR_FMT_RGBA, strlen(H3DOR_FMT_RGBA));
2074 pch += strlen(H3DOR_FMT_RGBA);
2075 *pch++ = '\0';
2076 }
2077 else
2078 {
2079 rc = VERR_BUFFER_OVERFLOW;
2080 }
2081 *pcbOut = cbOut;
2082 }
2083 else
2084 {
2085 rc = VERR_NOT_SUPPORTED;
2086 }
2087
2088 H3DORLOG(("H3DORContextProperty: %Rrc\n", rc));
2089 return rc;
2090}
2091
2092void ConsoleVRDPServer::remote3DRedirect(bool fEnable)
2093{
2094 if (!m_fInterfaceImage)
2095 {
2096 /* No redirect without corresponding interface. */
2097 return;
2098 }
2099
2100 /* Check if 3D redirection has been enabled. It is enabled by default. */
2101 com::Bstr bstr;
2102 HRESULT hrc = mConsole->getVRDEServer()->GetVRDEProperty(Bstr("H3DRedirect/Enabled").raw(), bstr.asOutParam());
2103
2104 com::Utf8Str value = hrc == S_OK? bstr: "";
2105
2106 bool fAllowed = RTStrICmp(value.c_str(), "true") == 0
2107 || RTStrICmp(value.c_str(), "1") == 0
2108 || value.c_str()[0] == 0;
2109
2110 if (!fAllowed && fEnable)
2111 {
2112 return;
2113 }
2114
2115 /* Tell the host 3D service to redirect output using the ConsoleVRDPServer callbacks. */
2116 H3DOUTPUTREDIRECT outputRedirect =
2117 {
2118 this,
2119 H3DORBegin,
2120 H3DORGeometry,
2121 H3DORVisibleRegion,
2122 H3DORFrame,
2123 H3DOREnd,
2124 H3DORContextProperty
2125 };
2126
2127 if (!fEnable)
2128 {
2129 /* This will tell the service to disable rediection. */
2130 RT_ZERO(outputRedirect);
2131 }
2132
2133 VBOXCRCMDCTL_HGCM data;
2134 data.Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
2135 data.Hdr.u32Function = SHCRGL_HOST_FN_SET_OUTPUT_REDIRECT;
2136
2137 data.aParms[0].type = VBOX_HGCM_SVC_PARM_PTR;
2138 data.aParms[0].u.pointer.addr = &outputRedirect;
2139 data.aParms[0].u.pointer.size = sizeof(outputRedirect);
2140
2141 int rc = mConsole->getDisplay()->crCtlSubmitSync(&data.Hdr, sizeof (data));
2142 if (!RT_SUCCESS(rc))
2143 {
2144 Log(("SHCRGL_HOST_FN_SET_CONSOLE failed with %Rrc\n", rc));
2145 return;
2146 }
2147
2148 LogRel(("VRDE: %s 3D redirect.\n", fEnable? "Enabled": "Disabled"));
2149#ifdef DEBUG_misha
2150 AssertFailed();
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->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->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->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->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->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->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->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->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->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.

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