VirtualBox

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

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

compilable w/o CROGL

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 138.8 KB
Line 
1/* $Id: ConsoleVRDPServer.cpp 50940 2014-04-01 11:22:34Z 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#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
2134 VBOXCRCMDCTL_HGCM data;
2135 data.Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
2136 data.Hdr.u32Function = SHCRGL_HOST_FN_SET_OUTPUT_REDIRECT;
2137
2138 data.aParms[0].type = VBOX_HGCM_SVC_PARM_PTR;
2139 data.aParms[0].u.pointer.addr = &outputRedirect;
2140 data.aParms[0].u.pointer.size = sizeof(outputRedirect);
2141
2142 int rc = mConsole->getDisplay()->crCtlSubmitSync(&data.Hdr, sizeof (data));
2143 if (!RT_SUCCESS(rc))
2144 {
2145 Log(("SHCRGL_HOST_FN_SET_CONSOLE failed with %Rrc\n", rc));
2146 return;
2147 }
2148
2149 LogRel(("VRDE: %s 3D redirect.\n", fEnable? "Enabled": "Disabled"));
2150# ifdef DEBUG_misha
2151 AssertFailed();
2152# endif
2153#endif
2154
2155 return;
2156}
2157
2158/* static */ DECLCALLBACK(int) ConsoleVRDPServer::VRDEImageCbNotify (void *pvContext,
2159 void *pvUser,
2160 HVRDEIMAGE hVideo,
2161 uint32_t u32Id,
2162 void *pvData,
2163 uint32_t cbData)
2164{
2165 H3DORLOG(("H3DOR: VRDEImageCbNotify: pvContext %p, pvUser %p, hVideo %p, u32Id %u, pvData %p, cbData %d\n",
2166 pvContext, pvUser, hVideo, u32Id, pvData, cbData));
2167
2168 ConsoleVRDPServer *pServer = static_cast<ConsoleVRDPServer*>(pvContext);
2169 H3DORInstance *p = (H3DORInstance *)pvUser;
2170 Assert(p);
2171 Assert(p->pThis);
2172 Assert(p->pThis == pServer);
2173
2174 if (u32Id == VRDE_IMAGE_NOTIFY_HANDLE_CREATE)
2175 {
2176 if (cbData != sizeof(uint32_t))
2177 {
2178 AssertFailed();
2179 return VERR_INVALID_PARAMETER;
2180 }
2181
2182 uint32_t u32StreamId = *(uint32_t *)pvData;
2183 H3DORLOG(("H3DOR: VRDE_IMAGE_NOTIFY_HANDLE_CREATE u32StreamId %d\n",
2184 u32StreamId));
2185
2186 if (u32StreamId != 0)
2187 {
2188 p->fCreated = true; // @todo not needed?
2189 }
2190 else
2191 {
2192 /* The stream has not been created. */
2193 }
2194 }
2195
2196 return VINF_SUCCESS;
2197}
2198
2199#undef H3DORLOG
2200
2201/* static */ DECLCALLBACK(int) ConsoleVRDPServer::VRDESCardCbNotify(void *pvContext,
2202 uint32_t u32Id,
2203 void *pvData,
2204 uint32_t cbData)
2205{
2206#ifdef VBOX_WITH_USB_CARDREADER
2207 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvContext);
2208 UsbCardReader *pReader = pThis->mConsole->getUsbCardReader();
2209 return pReader->VRDENotify(u32Id, pvData, cbData);
2210#else
2211 NOREF(pvContext);
2212 NOREF(u32Id);
2213 NOREF(pvData);
2214 NOREF(cbData);
2215 return VERR_NOT_SUPPORTED;
2216#endif
2217}
2218
2219/* static */ DECLCALLBACK(int) ConsoleVRDPServer::VRDESCardCbResponse(void *pvContext,
2220 int rcRequest,
2221 void *pvUser,
2222 uint32_t u32Function,
2223 void *pvData,
2224 uint32_t cbData)
2225{
2226#ifdef VBOX_WITH_USB_CARDREADER
2227 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvContext);
2228 UsbCardReader *pReader = pThis->mConsole->getUsbCardReader();
2229 return pReader->VRDEResponse(rcRequest, pvUser, u32Function, pvData, cbData);
2230#else
2231 NOREF(pvContext);
2232 NOREF(rcRequest);
2233 NOREF(pvUser);
2234 NOREF(u32Function);
2235 NOREF(pvData);
2236 NOREF(cbData);
2237 return VERR_NOT_SUPPORTED;
2238#endif
2239}
2240
2241int ConsoleVRDPServer::SCardRequest(void *pvUser, uint32_t u32Function, const void *pvData, uint32_t cbData)
2242{
2243 int rc = VINF_SUCCESS;
2244
2245 if (mhServer && mpEntryPoints && m_interfaceSCard.VRDESCardRequest)
2246 {
2247 rc = m_interfaceSCard.VRDESCardRequest(mhServer, pvUser, u32Function, pvData, cbData);
2248 }
2249 else
2250 {
2251 rc = VERR_NOT_SUPPORTED;
2252 }
2253
2254 return rc;
2255}
2256
2257
2258struct TSMFHOSTCHCTX;
2259struct TSMFVRDPCTX;
2260
2261typedef struct TSMFHOSTCHCTX
2262{
2263 ConsoleVRDPServer *pThis;
2264
2265 struct TSMFVRDPCTX *pVRDPCtx; /* NULL if no corresponding host channel context. */
2266
2267 void *pvDataReceived;
2268 uint32_t cbDataReceived;
2269 uint32_t cbDataAllocated;
2270} TSMFHOSTCHCTX;
2271
2272typedef struct TSMFVRDPCTX
2273{
2274 ConsoleVRDPServer *pThis;
2275
2276 VBOXHOSTCHANNELCALLBACKS *pCallbacks;
2277 void *pvCallbacks;
2278
2279 TSMFHOSTCHCTX *pHostChCtx; /* NULL if no corresponding host channel context. */
2280
2281 uint32_t u32ChannelHandle;
2282} TSMFVRDPCTX;
2283
2284static int tsmfContextsAlloc(TSMFHOSTCHCTX **ppHostChCtx, TSMFVRDPCTX **ppVRDPCtx)
2285{
2286 TSMFHOSTCHCTX *pHostChCtx = (TSMFHOSTCHCTX *)RTMemAllocZ(sizeof(TSMFHOSTCHCTX));
2287 if (!pHostChCtx)
2288 {
2289 return VERR_NO_MEMORY;
2290 }
2291
2292 TSMFVRDPCTX *pVRDPCtx = (TSMFVRDPCTX *)RTMemAllocZ(sizeof(TSMFVRDPCTX));
2293 if (!pVRDPCtx)
2294 {
2295 RTMemFree(pHostChCtx);
2296 return VERR_NO_MEMORY;
2297 }
2298
2299 *ppHostChCtx = pHostChCtx;
2300 *ppVRDPCtx = pVRDPCtx;
2301 return VINF_SUCCESS;
2302}
2303
2304int ConsoleVRDPServer::tsmfLock(void)
2305{
2306 int rc = RTCritSectEnter(&mTSMFLock);
2307 AssertRC(rc);
2308 return rc;
2309}
2310
2311void ConsoleVRDPServer::tsmfUnlock(void)
2312{
2313 RTCritSectLeave(&mTSMFLock);
2314}
2315
2316/* static */ DECLCALLBACK(int) ConsoleVRDPServer::tsmfHostChannelAttach(void *pvProvider,
2317 void **ppvChannel,
2318 uint32_t u32Flags,
2319 VBOXHOSTCHANNELCALLBACKS *pCallbacks,
2320 void *pvCallbacks)
2321{
2322 LogFlowFunc(("\n"));
2323
2324 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvProvider);
2325
2326 /* Create 2 context structures: for the VRDP server and for the host service. */
2327 TSMFHOSTCHCTX *pHostChCtx = NULL;
2328 TSMFVRDPCTX *pVRDPCtx = NULL;
2329
2330 int rc = tsmfContextsAlloc(&pHostChCtx, &pVRDPCtx);
2331 if (RT_FAILURE(rc))
2332 {
2333 return rc;
2334 }
2335
2336 pHostChCtx->pThis = pThis;
2337 pHostChCtx->pVRDPCtx = pVRDPCtx;
2338
2339 pVRDPCtx->pThis = pThis;
2340 pVRDPCtx->pCallbacks = pCallbacks;
2341 pVRDPCtx->pvCallbacks = pvCallbacks;
2342 pVRDPCtx->pHostChCtx = pHostChCtx;
2343
2344 rc = pThis->m_interfaceTSMF.VRDETSMFChannelCreate(pThis->mhServer, pVRDPCtx, u32Flags);
2345
2346 if (RT_SUCCESS(rc))
2347 {
2348 /* @todo contexts should be in a list for accounting. */
2349 *ppvChannel = pHostChCtx;
2350 }
2351 else
2352 {
2353 RTMemFree(pHostChCtx);
2354 RTMemFree(pVRDPCtx);
2355 }
2356
2357 return rc;
2358}
2359
2360/* static */ DECLCALLBACK(void) ConsoleVRDPServer::tsmfHostChannelDetach(void *pvChannel)
2361{
2362 LogFlowFunc(("\n"));
2363
2364 TSMFHOSTCHCTX *pHostChCtx = (TSMFHOSTCHCTX *)pvChannel;
2365 ConsoleVRDPServer *pThis = pHostChCtx->pThis;
2366
2367 int rc = pThis->tsmfLock();
2368 if (RT_SUCCESS(rc))
2369 {
2370 bool fClose = false;
2371 uint32_t u32ChannelHandle = 0;
2372
2373 if (pHostChCtx->pVRDPCtx)
2374 {
2375 /* There is still a VRDP context for this channel. */
2376 pHostChCtx->pVRDPCtx->pHostChCtx = NULL;
2377 u32ChannelHandle = pHostChCtx->pVRDPCtx->u32ChannelHandle;
2378 fClose = true;
2379 }
2380
2381 pThis->tsmfUnlock();
2382
2383 RTMemFree(pHostChCtx);
2384
2385 if (fClose)
2386 {
2387 LogFlowFunc(("Closing VRDE channel %d.\n", u32ChannelHandle));
2388 pThis->m_interfaceTSMF.VRDETSMFChannelClose(pThis->mhServer, u32ChannelHandle);
2389 }
2390 else
2391 {
2392 LogFlowFunc(("No VRDE channel.\n"));
2393 }
2394 }
2395}
2396
2397/* static */ DECLCALLBACK(int) ConsoleVRDPServer::tsmfHostChannelSend(void *pvChannel,
2398 const void *pvData,
2399 uint32_t cbData)
2400{
2401 LogFlowFunc(("cbData %d\n", cbData));
2402
2403 TSMFHOSTCHCTX *pHostChCtx = (TSMFHOSTCHCTX *)pvChannel;
2404 ConsoleVRDPServer *pThis = pHostChCtx->pThis;
2405
2406 int rc = pThis->tsmfLock();
2407 if (RT_SUCCESS(rc))
2408 {
2409 bool fSend = false;
2410 uint32_t u32ChannelHandle = 0;
2411
2412 if (pHostChCtx->pVRDPCtx)
2413 {
2414 u32ChannelHandle = pHostChCtx->pVRDPCtx->u32ChannelHandle;
2415 fSend = true;
2416 }
2417
2418 pThis->tsmfUnlock();
2419
2420 if (fSend)
2421 {
2422 LogFlowFunc(("Send to VRDE channel %d.\n", u32ChannelHandle));
2423 rc = pThis->m_interfaceTSMF.VRDETSMFChannelSend(pThis->mhServer, u32ChannelHandle,
2424 pvData, cbData);
2425 }
2426 }
2427
2428 return rc;
2429}
2430
2431/* static */ DECLCALLBACK(int) ConsoleVRDPServer::tsmfHostChannelRecv(void *pvChannel,
2432 void *pvData,
2433 uint32_t cbData,
2434 uint32_t *pcbReceived,
2435 uint32_t *pcbRemaining)
2436{
2437 LogFlowFunc(("cbData %d\n", cbData));
2438
2439 TSMFHOSTCHCTX *pHostChCtx = (TSMFHOSTCHCTX *)pvChannel;
2440 ConsoleVRDPServer *pThis = pHostChCtx->pThis;
2441
2442 int rc = pThis->tsmfLock();
2443 if (RT_SUCCESS(rc))
2444 {
2445 uint32_t cbToCopy = RT_MIN(cbData, pHostChCtx->cbDataReceived);
2446 uint32_t cbRemaining = pHostChCtx->cbDataReceived - cbToCopy;
2447
2448 LogFlowFunc(("cbToCopy %d, cbRemaining %d\n", cbToCopy, cbRemaining));
2449
2450 if (cbToCopy != 0)
2451 {
2452 memcpy(pvData, pHostChCtx->pvDataReceived, cbToCopy);
2453
2454 if (cbRemaining != 0)
2455 {
2456 memmove(pHostChCtx->pvDataReceived, (uint8_t *)pHostChCtx->pvDataReceived + cbToCopy, cbRemaining);
2457 }
2458
2459 pHostChCtx->cbDataReceived = cbRemaining;
2460 }
2461
2462 pThis->tsmfUnlock();
2463
2464 *pcbRemaining = cbRemaining;
2465 *pcbReceived = cbToCopy;
2466 }
2467
2468 return rc;
2469}
2470
2471/* static */ DECLCALLBACK(int) ConsoleVRDPServer::tsmfHostChannelControl(void *pvChannel,
2472 uint32_t u32Code,
2473 const void *pvParm,
2474 uint32_t cbParm,
2475 const void *pvData,
2476 uint32_t cbData,
2477 uint32_t *pcbDataReturned)
2478{
2479 LogFlowFunc(("u32Code %u\n", u32Code));
2480
2481 if (!pvChannel)
2482 {
2483 /* Special case, the provider must answer rather than a channel instance. */
2484 if (u32Code == VBOX_HOST_CHANNEL_CTRL_EXISTS)
2485 {
2486 *pcbDataReturned = 0;
2487 return VINF_SUCCESS;
2488 }
2489
2490 return VERR_NOT_IMPLEMENTED;
2491 }
2492
2493 /* Channels do not support this. */
2494 return VERR_NOT_IMPLEMENTED;
2495}
2496
2497
2498void ConsoleVRDPServer::setupTSMF(void)
2499{
2500 if (m_interfaceTSMF.header.u64Size == 0)
2501 {
2502 return;
2503 }
2504
2505 /* Register with the host channel service. */
2506 VBOXHOSTCHANNELINTERFACE hostChannelInterface =
2507 {
2508 this,
2509 tsmfHostChannelAttach,
2510 tsmfHostChannelDetach,
2511 tsmfHostChannelSend,
2512 tsmfHostChannelRecv,
2513 tsmfHostChannelControl
2514 };
2515
2516 VBoxHostChannelHostRegister parms;
2517
2518 static char szProviderName[] = "/vrde/tsmf";
2519
2520 parms.name.type = VBOX_HGCM_SVC_PARM_PTR;
2521 parms.name.u.pointer.addr = &szProviderName[0];
2522 parms.name.u.pointer.size = sizeof(szProviderName);
2523
2524 parms.iface.type = VBOX_HGCM_SVC_PARM_PTR;
2525 parms.iface.u.pointer.addr = &hostChannelInterface;
2526 parms.iface.u.pointer.size = sizeof(hostChannelInterface);
2527
2528 VMMDev *pVMMDev = mConsole->getVMMDev();
2529
2530 if (!pVMMDev)
2531 {
2532 AssertMsgFailed(("setupTSMF no vmmdev\n"));
2533 return;
2534 }
2535
2536 int rc = pVMMDev->hgcmHostCall("VBoxHostChannel",
2537 VBOX_HOST_CHANNEL_HOST_FN_REGISTER,
2538 2,
2539 &parms.name);
2540
2541 if (!RT_SUCCESS(rc))
2542 {
2543 Log(("VBOX_HOST_CHANNEL_HOST_FN_REGISTER failed with %Rrc\n", rc));
2544 return;
2545 }
2546
2547 LogRel(("VRDE: Enabled TSMF channel.\n"));
2548
2549 return;
2550}
2551
2552/* @todo these defines must be in a header, which is used by guest component as well. */
2553#define VBOX_TSMF_HCH_CREATE_ACCEPTED (VBOX_HOST_CHANNEL_EVENT_USER + 0)
2554#define VBOX_TSMF_HCH_CREATE_DECLINED (VBOX_HOST_CHANNEL_EVENT_USER + 1)
2555#define VBOX_TSMF_HCH_DISCONNECTED (VBOX_HOST_CHANNEL_EVENT_USER + 2)
2556
2557/* static */ DECLCALLBACK(void) ConsoleVRDPServer::VRDETSMFCbNotify(void *pvContext,
2558 uint32_t u32Notification,
2559 void *pvChannel,
2560 const void *pvParm,
2561 uint32_t cbParm)
2562{
2563 int rc = VINF_SUCCESS;
2564
2565 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvContext);
2566
2567 TSMFVRDPCTX *pVRDPCtx = (TSMFVRDPCTX *)pvChannel;
2568
2569 Assert(pVRDPCtx->pThis == pThis);
2570
2571 if (pVRDPCtx->pCallbacks == NULL)
2572 {
2573 LogFlowFunc(("tsmfHostChannel: Channel disconnected. Skipping.\n"));
2574 return;
2575 }
2576
2577 switch (u32Notification)
2578 {
2579 case VRDE_TSMF_N_CREATE_ACCEPTED:
2580 {
2581 VRDETSMFNOTIFYCREATEACCEPTED *p = (VRDETSMFNOTIFYCREATEACCEPTED *)pvParm;
2582 Assert(cbParm == sizeof(VRDETSMFNOTIFYCREATEACCEPTED));
2583
2584 LogFlowFunc(("tsmfHostChannel: VRDE_TSMF_N_CREATE_ACCEPTED(%p): p->u32ChannelHandle %d\n",
2585 pVRDPCtx, p->u32ChannelHandle));
2586
2587 pVRDPCtx->u32ChannelHandle = p->u32ChannelHandle;
2588
2589 pVRDPCtx->pCallbacks->HostChannelCallbackEvent(pVRDPCtx->pvCallbacks, pVRDPCtx->pHostChCtx,
2590 VBOX_TSMF_HCH_CREATE_ACCEPTED,
2591 NULL, 0);
2592 } break;
2593
2594 case VRDE_TSMF_N_CREATE_DECLINED:
2595 {
2596 LogFlowFunc(("tsmfHostChannel: VRDE_TSMF_N_CREATE_DECLINED(%p)\n", pVRDPCtx));
2597
2598 pVRDPCtx->pCallbacks->HostChannelCallbackEvent(pVRDPCtx->pvCallbacks, pVRDPCtx->pHostChCtx,
2599 VBOX_TSMF_HCH_CREATE_DECLINED,
2600 NULL, 0);
2601 } break;
2602
2603 case VRDE_TSMF_N_DATA:
2604 {
2605 /* Save the data in the intermediate buffer and send the event. */
2606 VRDETSMFNOTIFYDATA *p = (VRDETSMFNOTIFYDATA *)pvParm;
2607 Assert(cbParm == sizeof(VRDETSMFNOTIFYDATA));
2608
2609 LogFlowFunc(("tsmfHostChannel: VRDE_TSMF_N_DATA(%p): p->cbData %d\n", pVRDPCtx, p->cbData));
2610
2611 VBOXHOSTCHANNELEVENTRECV ev;
2612 ev.u32SizeAvailable = 0;
2613
2614 rc = pThis->tsmfLock();
2615
2616 if (RT_SUCCESS(rc))
2617 {
2618 TSMFHOSTCHCTX *pHostChCtx = pVRDPCtx->pHostChCtx;
2619
2620 if (pHostChCtx)
2621 {
2622 if (pHostChCtx->pvDataReceived)
2623 {
2624 uint32_t cbAlloc = p->cbData + pHostChCtx->cbDataReceived;
2625 pHostChCtx->pvDataReceived = RTMemRealloc(pHostChCtx->pvDataReceived, cbAlloc);
2626 memcpy((uint8_t *)pHostChCtx->pvDataReceived + pHostChCtx->cbDataReceived, p->pvData, p->cbData);
2627
2628 pHostChCtx->cbDataReceived += p->cbData;
2629 pHostChCtx->cbDataAllocated = cbAlloc;
2630 }
2631 else
2632 {
2633 pHostChCtx->pvDataReceived = RTMemAlloc(p->cbData);
2634 memcpy(pHostChCtx->pvDataReceived, p->pvData, p->cbData);
2635
2636 pHostChCtx->cbDataReceived = p->cbData;
2637 pHostChCtx->cbDataAllocated = p->cbData;
2638 }
2639
2640 ev.u32SizeAvailable = p->cbData;
2641 }
2642 else
2643 {
2644 LogFlowFunc(("tsmfHostChannel: VRDE_TSMF_N_DATA: no host channel. Skipping\n"));
2645 }
2646
2647 pThis->tsmfUnlock();
2648 }
2649
2650 pVRDPCtx->pCallbacks->HostChannelCallbackEvent(pVRDPCtx->pvCallbacks, pVRDPCtx->pHostChCtx,
2651 VBOX_HOST_CHANNEL_EVENT_RECV,
2652 &ev, sizeof(ev));
2653 } break;
2654
2655 case VRDE_TSMF_N_DISCONNECTED:
2656 {
2657 LogFlowFunc(("tsmfHostChannel: VRDE_TSMF_N_DISCONNECTED(%p)\n", pVRDPCtx));
2658
2659 pVRDPCtx->pCallbacks->HostChannelCallbackEvent(pVRDPCtx->pvCallbacks, pVRDPCtx->pHostChCtx,
2660 VBOX_TSMF_HCH_DISCONNECTED,
2661 NULL, 0);
2662
2663 /* The callback context will not be used anymore. */
2664 pVRDPCtx->pCallbacks->HostChannelCallbackDeleted(pVRDPCtx->pvCallbacks, pVRDPCtx->pHostChCtx);
2665 pVRDPCtx->pCallbacks = NULL;
2666 pVRDPCtx->pvCallbacks = NULL;
2667
2668 rc = pThis->tsmfLock();
2669 if (RT_SUCCESS(rc))
2670 {
2671 if (pVRDPCtx->pHostChCtx)
2672 {
2673 /* There is still a host channel context for this channel. */
2674 pVRDPCtx->pHostChCtx->pVRDPCtx = NULL;
2675 }
2676
2677 pThis->tsmfUnlock();
2678
2679 RT_ZERO(*pVRDPCtx);
2680 RTMemFree(pVRDPCtx);
2681 }
2682 } break;
2683
2684 default:
2685 {
2686 AssertFailed();
2687 } break;
2688 }
2689}
2690
2691/* static */ DECLCALLBACK(void) ConsoleVRDPServer::VRDECallbackVideoInNotify(void *pvCallback,
2692 uint32_t u32Id,
2693 const void *pvData,
2694 uint32_t cbData)
2695{
2696 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvCallback);
2697 if (pThis->mEmWebcam)
2698 {
2699 pThis->mEmWebcam->EmWebcamCbNotify(u32Id, pvData, cbData);
2700 }
2701}
2702
2703/* static */ DECLCALLBACK(void) ConsoleVRDPServer::VRDECallbackVideoInDeviceDesc(void *pvCallback,
2704 int rcRequest,
2705 void *pDeviceCtx,
2706 void *pvUser,
2707 const VRDEVIDEOINDEVICEDESC *pDeviceDesc,
2708 uint32_t cbDevice)
2709{
2710 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvCallback);
2711 if (pThis->mEmWebcam)
2712 {
2713 pThis->mEmWebcam->EmWebcamCbDeviceDesc(rcRequest, pDeviceCtx, pvUser, pDeviceDesc, cbDevice);
2714 }
2715}
2716
2717/* static */ DECLCALLBACK(void) ConsoleVRDPServer::VRDECallbackVideoInControl(void *pvCallback,
2718 int rcRequest,
2719 void *pDeviceCtx,
2720 void *pvUser,
2721 const VRDEVIDEOINCTRLHDR *pControl,
2722 uint32_t cbControl)
2723{
2724 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvCallback);
2725 if (pThis->mEmWebcam)
2726 {
2727 pThis->mEmWebcam->EmWebcamCbControl(rcRequest, pDeviceCtx, pvUser, pControl, cbControl);
2728 }
2729}
2730
2731/* static */ DECLCALLBACK(void) ConsoleVRDPServer::VRDECallbackVideoInFrame(void *pvCallback,
2732 int rcRequest,
2733 void *pDeviceCtx,
2734 const VRDEVIDEOINPAYLOADHDR *pFrame,
2735 uint32_t cbFrame)
2736{
2737 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvCallback);
2738 if (pThis->mEmWebcam)
2739 {
2740 pThis->mEmWebcam->EmWebcamCbFrame(rcRequest, pDeviceCtx, pFrame, cbFrame);
2741 }
2742}
2743
2744int ConsoleVRDPServer::VideoInDeviceAttach(const VRDEVIDEOINDEVICEHANDLE *pDeviceHandle, void *pvDeviceCtx)
2745{
2746 int rc;
2747
2748 if (mhServer && mpEntryPoints && m_interfaceVideoIn.VRDEVideoInDeviceAttach)
2749 {
2750 rc = m_interfaceVideoIn.VRDEVideoInDeviceAttach(mhServer, pDeviceHandle, pvDeviceCtx);
2751 }
2752 else
2753 {
2754 rc = VERR_NOT_SUPPORTED;
2755 }
2756
2757 return rc;
2758}
2759
2760int ConsoleVRDPServer::VideoInDeviceDetach(const VRDEVIDEOINDEVICEHANDLE *pDeviceHandle)
2761{
2762 int rc;
2763
2764 if (mhServer && mpEntryPoints && m_interfaceVideoIn.VRDEVideoInDeviceDetach)
2765 {
2766 rc = m_interfaceVideoIn.VRDEVideoInDeviceDetach(mhServer, pDeviceHandle);
2767 }
2768 else
2769 {
2770 rc = VERR_NOT_SUPPORTED;
2771 }
2772
2773 return rc;
2774}
2775
2776int ConsoleVRDPServer::VideoInGetDeviceDesc(void *pvUser, const VRDEVIDEOINDEVICEHANDLE *pDeviceHandle)
2777{
2778 int rc;
2779
2780 if (mhServer && mpEntryPoints && m_interfaceVideoIn.VRDEVideoInGetDeviceDesc)
2781 {
2782 rc = m_interfaceVideoIn.VRDEVideoInGetDeviceDesc(mhServer, pvUser, pDeviceHandle);
2783 }
2784 else
2785 {
2786 rc = VERR_NOT_SUPPORTED;
2787 }
2788
2789 return rc;
2790}
2791
2792int ConsoleVRDPServer::VideoInControl(void *pvUser, const VRDEVIDEOINDEVICEHANDLE *pDeviceHandle,
2793 const VRDEVIDEOINCTRLHDR *pReq, uint32_t cbReq)
2794{
2795 int rc;
2796
2797 if (mhServer && mpEntryPoints && m_interfaceVideoIn.VRDEVideoInControl)
2798 {
2799 rc = m_interfaceVideoIn.VRDEVideoInControl(mhServer, pvUser, pDeviceHandle, pReq, cbReq);
2800 }
2801 else
2802 {
2803 rc = VERR_NOT_SUPPORTED;
2804 }
2805
2806 return rc;
2807}
2808
2809
2810/* static */ DECLCALLBACK(void) ConsoleVRDPServer::VRDECallbackInputSetup(void *pvCallback,
2811 int rcRequest,
2812 uint32_t u32Method,
2813 const void *pvResult,
2814 uint32_t cbResult)
2815{
2816 NOREF(pvCallback);
2817 NOREF(rcRequest);
2818 NOREF(u32Method);
2819 NOREF(pvResult);
2820 NOREF(cbResult);
2821}
2822
2823/* static */ DECLCALLBACK(void) ConsoleVRDPServer::VRDECallbackInputEvent(void *pvCallback,
2824 uint32_t u32Method,
2825 const void *pvEvent,
2826 uint32_t cbEvent)
2827{
2828 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvCallback);
2829
2830 if (u32Method == VRDE_INPUT_METHOD_TOUCH)
2831 {
2832 if (cbEvent >= sizeof(VRDEINPUTHEADER))
2833 {
2834 VRDEINPUTHEADER *pHeader = (VRDEINPUTHEADER *)pvEvent;
2835
2836 if (pHeader->u16EventId == VRDEINPUT_EVENTID_TOUCH)
2837 {
2838 IMouse *pMouse = pThis->mConsole->getMouse();
2839
2840 VRDEINPUT_TOUCH_EVENT_PDU *p = (VRDEINPUT_TOUCH_EVENT_PDU *)pHeader;
2841
2842 uint16_t iFrame;
2843 for (iFrame = 0; iFrame < p->u16FrameCount; iFrame++)
2844 {
2845 VRDEINPUT_TOUCH_FRAME *pFrame = &p->aFrames[iFrame];
2846
2847 com::SafeArray<LONG64> aContacts(pFrame->u16ContactCount);
2848
2849 uint16_t iContact;
2850 for (iContact = 0; iContact < pFrame->u16ContactCount; iContact++)
2851 {
2852 VRDEINPUT_CONTACT_DATA *pContact = &pFrame->aContacts[iContact];
2853
2854 int16_t x = (int16_t)(pContact->i32X + 1);
2855 int16_t y = (int16_t)(pContact->i32Y + 1);
2856 uint8_t contactId = pContact->u8ContactId;
2857 uint8_t contactState = TouchContactState_None;
2858
2859 if (pContact->u32ContactFlags & VRDEINPUT_CONTACT_FLAG_INRANGE)
2860 {
2861 contactState |= TouchContactState_InRange;
2862 }
2863 if (pContact->u32ContactFlags & VRDEINPUT_CONTACT_FLAG_INCONTACT)
2864 {
2865 contactState |= TouchContactState_InContact;
2866 }
2867
2868 aContacts[iContact] = RT_MAKE_U64_FROM_U16((uint16_t)x,
2869 (uint16_t)y,
2870 RT_MAKE_U16(contactId, contactState),
2871 0);
2872 }
2873
2874 if (pFrame->u64FrameOffset == 0)
2875 {
2876 pThis->mu64TouchInputTimestampMCS = 0;
2877 }
2878 else
2879 {
2880 pThis->mu64TouchInputTimestampMCS += pFrame->u64FrameOffset;
2881 }
2882
2883 pMouse->PutEventMultiTouch(pFrame->u16ContactCount,
2884 ComSafeArrayAsInParam(aContacts),
2885 (ULONG)(pThis->mu64TouchInputTimestampMCS / 1000)); /* Micro->milliseconds. */
2886 }
2887 }
2888 else if (pHeader->u16EventId == VRDEINPUT_EVENTID_DISMISS_HOVERING_CONTACT)
2889 {
2890 /* @todo */
2891 }
2892 else
2893 {
2894 AssertMsgFailed(("EventId %d\n", pHeader->u16EventId));
2895 }
2896 }
2897 }
2898}
2899
2900
2901void ConsoleVRDPServer::EnableConnections(void)
2902{
2903 if (mpEntryPoints && mhServer)
2904 {
2905 mpEntryPoints->VRDEEnableConnections(mhServer, true);
2906
2907 /* Setup the generic TSMF channel. */
2908 setupTSMF();
2909 }
2910}
2911
2912void ConsoleVRDPServer::DisconnectClient(uint32_t u32ClientId, bool fReconnect)
2913{
2914 if (mpEntryPoints && mhServer)
2915 {
2916 mpEntryPoints->VRDEDisconnect(mhServer, u32ClientId, fReconnect);
2917 }
2918}
2919
2920int ConsoleVRDPServer::MousePointer(BOOL alpha,
2921 ULONG xHot,
2922 ULONG yHot,
2923 ULONG width,
2924 ULONG height,
2925 const uint8_t *pu8Shape)
2926{
2927 int rc = VINF_SUCCESS;
2928
2929 if (mhServer && mpEntryPoints && m_interfaceMousePtr.VRDEMousePtr)
2930 {
2931 size_t cbMask = (((width + 7) / 8) * height + 3) & ~3;
2932 size_t cbData = width * height * 4;
2933
2934 size_t cbDstMask = alpha? 0: cbMask;
2935
2936 size_t cbPointer = sizeof(VRDEMOUSEPTRDATA) + cbDstMask + cbData;
2937 uint8_t *pu8Pointer = (uint8_t *)RTMemAlloc(cbPointer);
2938 if (pu8Pointer != NULL)
2939 {
2940 VRDEMOUSEPTRDATA *pPointer = (VRDEMOUSEPTRDATA *)pu8Pointer;
2941
2942 pPointer->u16HotX = (uint16_t)xHot;
2943 pPointer->u16HotY = (uint16_t)yHot;
2944 pPointer->u16Width = (uint16_t)width;
2945 pPointer->u16Height = (uint16_t)height;
2946 pPointer->u16MaskLen = (uint16_t)cbDstMask;
2947 pPointer->u32DataLen = (uint32_t)cbData;
2948
2949 /* AND mask. */
2950 uint8_t *pu8Mask = pu8Pointer + sizeof(VRDEMOUSEPTRDATA);
2951 if (cbDstMask)
2952 {
2953 memcpy(pu8Mask, pu8Shape, cbDstMask);
2954 }
2955
2956 /* XOR mask */
2957 uint8_t *pu8Data = pu8Mask + pPointer->u16MaskLen;
2958 memcpy(pu8Data, pu8Shape + cbMask, cbData);
2959
2960 m_interfaceMousePtr.VRDEMousePtr(mhServer, pPointer);
2961
2962 RTMemFree(pu8Pointer);
2963 }
2964 else
2965 {
2966 rc = VERR_NO_MEMORY;
2967 }
2968 }
2969 else
2970 {
2971 rc = VERR_NOT_SUPPORTED;
2972 }
2973
2974 return rc;
2975}
2976
2977void ConsoleVRDPServer::MousePointerUpdate(const VRDECOLORPOINTER *pPointer)
2978{
2979 if (mpEntryPoints && mhServer)
2980 {
2981 mpEntryPoints->VRDEColorPointer(mhServer, pPointer);
2982 }
2983}
2984
2985void ConsoleVRDPServer::MousePointerHide(void)
2986{
2987 if (mpEntryPoints && mhServer)
2988 {
2989 mpEntryPoints->VRDEHidePointer(mhServer);
2990 }
2991}
2992
2993void ConsoleVRDPServer::Stop(void)
2994{
2995 Assert(VALID_PTR(this)); /** @todo r=bird: there are(/was) some odd cases where this buster was invalid on
2996 * linux. Just remove this when it's 100% sure that problem has been fixed. */
2997
2998#ifdef VBOX_WITH_USB
2999 remoteUSBThreadStop();
3000#endif /* VBOX_WITH_USB */
3001
3002 if (mhServer)
3003 {
3004 HVRDESERVER hServer = mhServer;
3005
3006 /* Reset the handle to avoid further calls to the server. */
3007 mhServer = 0;
3008
3009 /* Workaround for VM process hangs on termination.
3010 *
3011 * Make sure that the server is not currently processing a resize.
3012 * mhServer 0 will not allow to enter the server again.
3013 * Wait until any current resize returns from the server.
3014 */
3015 if (mcInResize)
3016 {
3017 LogRel(("VRDP: waiting for resize %d\n", mcInResize));
3018
3019 int i = 0;
3020 while (mcInResize && ++i < 100)
3021 {
3022 RTThreadSleep(10);
3023 }
3024 }
3025
3026 if (mpEntryPoints && hServer)
3027 {
3028 mpEntryPoints->VRDEDestroy(hServer);
3029 }
3030 }
3031
3032 mpfnAuthEntry = NULL;
3033 mpfnAuthEntry2 = NULL;
3034 mpfnAuthEntry3 = NULL;
3035
3036 if (mAuthLibrary)
3037 {
3038 RTLdrClose(mAuthLibrary);
3039 mAuthLibrary = 0;
3040 }
3041}
3042
3043/* Worker thread for Remote USB. The thread polls the clients for
3044 * the list of attached USB devices.
3045 * The thread is also responsible for attaching/detaching devices
3046 * to/from the VM.
3047 *
3048 * It is expected that attaching/detaching is not a frequent operation.
3049 *
3050 * The thread is always running when the VRDP server is active.
3051 *
3052 * The thread scans backends and requests the device list every 2 seconds.
3053 *
3054 * When device list is available, the thread calls the Console to process it.
3055 *
3056 */
3057#define VRDP_DEVICE_LIST_PERIOD_MS (2000)
3058
3059#ifdef VBOX_WITH_USB
3060static DECLCALLBACK(int) threadRemoteUSB(RTTHREAD self, void *pvUser)
3061{
3062 ConsoleVRDPServer *pOwner = (ConsoleVRDPServer *)pvUser;
3063
3064 LogFlow(("Console::threadRemoteUSB: start. owner = %p.\n", pOwner));
3065
3066 pOwner->notifyRemoteUSBThreadRunning(self);
3067
3068 while (pOwner->isRemoteUSBThreadRunning())
3069 {
3070 RemoteUSBBackend *pRemoteUSBBackend = NULL;
3071
3072 while ((pRemoteUSBBackend = pOwner->usbBackendGetNext(pRemoteUSBBackend)) != NULL)
3073 {
3074 pRemoteUSBBackend->PollRemoteDevices();
3075 }
3076
3077 pOwner->waitRemoteUSBThreadEvent(VRDP_DEVICE_LIST_PERIOD_MS);
3078
3079 LogFlow(("Console::threadRemoteUSB: iteration. owner = %p.\n", pOwner));
3080 }
3081
3082 return VINF_SUCCESS;
3083}
3084
3085void ConsoleVRDPServer::notifyRemoteUSBThreadRunning(RTTHREAD thread)
3086{
3087 mUSBBackends.thread = thread;
3088 mUSBBackends.fThreadRunning = true;
3089 int rc = RTThreadUserSignal(thread);
3090 AssertRC(rc);
3091}
3092
3093bool ConsoleVRDPServer::isRemoteUSBThreadRunning(void)
3094{
3095 return mUSBBackends.fThreadRunning;
3096}
3097
3098void ConsoleVRDPServer::waitRemoteUSBThreadEvent(RTMSINTERVAL cMillies)
3099{
3100 int rc = RTSemEventWait(mUSBBackends.event, cMillies);
3101 Assert(RT_SUCCESS(rc) || rc == VERR_TIMEOUT);
3102 NOREF(rc);
3103}
3104
3105void ConsoleVRDPServer::remoteUSBThreadStart(void)
3106{
3107 int rc = RTSemEventCreate(&mUSBBackends.event);
3108
3109 if (RT_FAILURE(rc))
3110 {
3111 AssertFailed();
3112 mUSBBackends.event = 0;
3113 }
3114
3115 if (RT_SUCCESS(rc))
3116 {
3117 rc = RTThreadCreate(&mUSBBackends.thread, threadRemoteUSB, this, 65536,
3118 RTTHREADTYPE_VRDP_IO, RTTHREADFLAGS_WAITABLE, "remote usb");
3119 }
3120
3121 if (RT_FAILURE(rc))
3122 {
3123 LogRel(("Warning: could not start the remote USB thread, rc = %Rrc!!!\n", rc));
3124 mUSBBackends.thread = NIL_RTTHREAD;
3125 }
3126 else
3127 {
3128 /* Wait until the thread is ready. */
3129 rc = RTThreadUserWait(mUSBBackends.thread, 60000);
3130 AssertRC(rc);
3131 Assert (mUSBBackends.fThreadRunning || RT_FAILURE(rc));
3132 }
3133}
3134
3135void ConsoleVRDPServer::remoteUSBThreadStop(void)
3136{
3137 mUSBBackends.fThreadRunning = false;
3138
3139 if (mUSBBackends.thread != NIL_RTTHREAD)
3140 {
3141 Assert (mUSBBackends.event != 0);
3142
3143 RTSemEventSignal(mUSBBackends.event);
3144
3145 int rc = RTThreadWait(mUSBBackends.thread, 60000, NULL);
3146 AssertRC(rc);
3147
3148 mUSBBackends.thread = NIL_RTTHREAD;
3149 }
3150
3151 if (mUSBBackends.event)
3152 {
3153 RTSemEventDestroy(mUSBBackends.event);
3154 mUSBBackends.event = 0;
3155 }
3156}
3157#endif /* VBOX_WITH_USB */
3158
3159typedef struct AuthCtx
3160{
3161 AuthResult result;
3162
3163 PAUTHENTRY3 pfnAuthEntry3;
3164 PAUTHENTRY2 pfnAuthEntry2;
3165 PAUTHENTRY pfnAuthEntry;
3166
3167 const char *pszCaller;
3168 PAUTHUUID pUuid;
3169 AuthGuestJudgement guestJudgement;
3170 const char *pszUser;
3171 const char *pszPassword;
3172 const char *pszDomain;
3173 int fLogon;
3174 unsigned clientId;
3175} AuthCtx;
3176
3177static DECLCALLBACK(int) authThread(RTTHREAD self, void *pvUser)
3178{
3179 AuthCtx *pCtx = (AuthCtx *)pvUser;
3180
3181 if (pCtx->pfnAuthEntry3)
3182 {
3183 pCtx->result = pCtx->pfnAuthEntry3(pCtx->pszCaller, pCtx->pUuid, pCtx->guestJudgement,
3184 pCtx->pszUser, pCtx->pszPassword, pCtx->pszDomain,
3185 pCtx->fLogon, pCtx->clientId);
3186 }
3187 else if (pCtx->pfnAuthEntry2)
3188 {
3189 pCtx->result = pCtx->pfnAuthEntry2(pCtx->pUuid, pCtx->guestJudgement,
3190 pCtx->pszUser, pCtx->pszPassword, pCtx->pszDomain,
3191 pCtx->fLogon, pCtx->clientId);
3192 }
3193 else if (pCtx->pfnAuthEntry)
3194 {
3195 pCtx->result = pCtx->pfnAuthEntry(pCtx->pUuid, pCtx->guestJudgement,
3196 pCtx->pszUser, pCtx->pszPassword, pCtx->pszDomain);
3197 }
3198 return VINF_SUCCESS;
3199}
3200
3201static AuthResult authCall(AuthCtx *pCtx)
3202{
3203 AuthResult result = AuthResultAccessDenied;
3204
3205 /* Use a separate thread because external modules might need a lot of stack space. */
3206 RTTHREAD thread = NIL_RTTHREAD;
3207 int rc = RTThreadCreate(&thread, authThread, pCtx, 512*_1K,
3208 RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "VRDEAuth");
3209 LogFlow(("authCall: RTThreadCreate %Rrc\n", rc));
3210
3211 if (RT_SUCCESS(rc))
3212 {
3213 rc = RTThreadWait(thread, RT_INDEFINITE_WAIT, NULL);
3214 LogFlow(("authCall: RTThreadWait %Rrc\n", rc));
3215 }
3216
3217 if (RT_SUCCESS(rc))
3218 {
3219 /* Only update the result if the thread finished without errors. */
3220 result = pCtx->result;
3221 }
3222 else
3223 {
3224 LogRel(("AUTH: unable to execute the auth thread %Rrc\n", rc));
3225 }
3226
3227 return result;
3228}
3229
3230AuthResult ConsoleVRDPServer::Authenticate(const Guid &uuid, AuthGuestJudgement guestJudgement,
3231 const char *pszUser, const char *pszPassword, const char *pszDomain,
3232 uint32_t u32ClientId)
3233{
3234 AUTHUUID rawuuid;
3235
3236 memcpy(rawuuid, uuid.raw(), sizeof(rawuuid));
3237
3238 LogFlow(("ConsoleVRDPServer::Authenticate: uuid = %RTuuid, guestJudgement = %d, pszUser = %s, pszPassword = %s, pszDomain = %s, u32ClientId = %d\n",
3239 rawuuid, guestJudgement, pszUser, pszPassword, pszDomain, u32ClientId));
3240
3241 /*
3242 * Called only from VRDP input thread. So thread safety is not required.
3243 */
3244
3245 if (!mAuthLibrary)
3246 {
3247 /* Load the external authentication library. */
3248 Bstr authLibrary;
3249 mConsole->getVRDEServer()->COMGETTER(AuthLibrary)(authLibrary.asOutParam());
3250
3251 Utf8Str filename = authLibrary;
3252
3253 LogRel(("AUTH: loading external authentication library '%ls'\n", authLibrary.raw()));
3254
3255 int rc;
3256 if (RTPathHavePath(filename.c_str()))
3257 rc = RTLdrLoad(filename.c_str(), &mAuthLibrary);
3258 else
3259 {
3260 rc = RTLdrLoadAppPriv(filename.c_str(), &mAuthLibrary);
3261 if (RT_FAILURE(rc))
3262 {
3263 /* Backward compatibility with old default 'VRDPAuth' name.
3264 * Try to load new default 'VBoxAuth' instead.
3265 */
3266 if (filename == "VRDPAuth")
3267 {
3268 LogRel(("AUTH: ConsoleVRDPServer::Authenticate: loading external authentication library VBoxAuth\n"));
3269 rc = RTLdrLoadAppPriv("VBoxAuth", &mAuthLibrary);
3270 }
3271 }
3272 }
3273
3274 if (RT_FAILURE(rc))
3275 LogRel(("AUTH: Failed to load external authentication library. Error code: %Rrc\n", rc));
3276
3277 if (RT_SUCCESS(rc))
3278 {
3279 typedef struct AuthEntryInfoStruct
3280 {
3281 const char *pszName;
3282 void **ppvAddress;
3283
3284 } AuthEntryInfo;
3285 AuthEntryInfo entries[] =
3286 {
3287 { AUTHENTRY3_NAME, (void **)&mpfnAuthEntry3 },
3288 { AUTHENTRY2_NAME, (void **)&mpfnAuthEntry2 },
3289 { AUTHENTRY_NAME, (void **)&mpfnAuthEntry },
3290 { NULL, NULL }
3291 };
3292
3293 /* Get the entry point. */
3294 AuthEntryInfo *pEntryInfo = &entries[0];
3295 while (pEntryInfo->pszName)
3296 {
3297 *pEntryInfo->ppvAddress = NULL;
3298
3299 int rc2 = RTLdrGetSymbol(mAuthLibrary, pEntryInfo->pszName, pEntryInfo->ppvAddress);
3300 if (RT_SUCCESS(rc2))
3301 {
3302 /* Found an entry point. */
3303 LogRel(("AUTH: Using entry point '%s'.\n", pEntryInfo->pszName));
3304 rc = VINF_SUCCESS;
3305 break;
3306 }
3307
3308 if (rc2 != VERR_SYMBOL_NOT_FOUND)
3309 {
3310 LogRel(("AUTH: Could not resolve import '%s'. Error code: %Rrc\n", pEntryInfo->pszName, rc2));
3311 }
3312 rc = rc2;
3313
3314 pEntryInfo++;
3315 }
3316 }
3317
3318 if (RT_FAILURE(rc))
3319 {
3320 mConsole->setError(E_FAIL,
3321 mConsole->tr("Could not load the external authentication library '%s' (%Rrc)"),
3322 filename.c_str(),
3323 rc);
3324
3325 mpfnAuthEntry = NULL;
3326 mpfnAuthEntry2 = NULL;
3327 mpfnAuthEntry3 = NULL;
3328
3329 if (mAuthLibrary)
3330 {
3331 RTLdrClose(mAuthLibrary);
3332 mAuthLibrary = 0;
3333 }
3334
3335 return AuthResultAccessDenied;
3336 }
3337 }
3338
3339 Assert(mAuthLibrary && (mpfnAuthEntry || mpfnAuthEntry2 || mpfnAuthEntry3));
3340
3341 AuthCtx ctx;
3342 ctx.result = AuthResultAccessDenied; /* Denied by default. */
3343 ctx.pfnAuthEntry3 = mpfnAuthEntry3;
3344 ctx.pfnAuthEntry2 = mpfnAuthEntry2;
3345 ctx.pfnAuthEntry = mpfnAuthEntry;
3346 ctx.pszCaller = "vrde";
3347 ctx.pUuid = &rawuuid;
3348 ctx.guestJudgement = guestJudgement;
3349 ctx.pszUser = pszUser;
3350 ctx.pszPassword = pszPassword;
3351 ctx.pszDomain = pszDomain;
3352 ctx.fLogon = true;
3353 ctx.clientId = u32ClientId;
3354
3355 AuthResult result = authCall(&ctx);
3356
3357 switch (result)
3358 {
3359 case AuthResultAccessDenied:
3360 LogRel(("AUTH: external authentication module returned 'access denied'\n"));
3361 break;
3362 case AuthResultAccessGranted:
3363 LogRel(("AUTH: external authentication module returned 'access granted'\n"));
3364 break;
3365 case AuthResultDelegateToGuest:
3366 LogRel(("AUTH: external authentication module returned 'delegate request to guest'\n"));
3367 break;
3368 default:
3369 LogRel(("AUTH: external authentication module returned incorrect return code %d\n", result));
3370 result = AuthResultAccessDenied;
3371 }
3372
3373 LogFlow(("ConsoleVRDPServer::Authenticate: result = %d\n", result));
3374
3375 return result;
3376}
3377
3378void ConsoleVRDPServer::AuthDisconnect(const Guid &uuid, uint32_t u32ClientId)
3379{
3380 AUTHUUID rawuuid;
3381
3382 memcpy(rawuuid, uuid.raw(), sizeof(rawuuid));
3383
3384 LogFlow(("ConsoleVRDPServer::AuthDisconnect: uuid = %RTuuid, u32ClientId = %d\n",
3385 rawuuid, u32ClientId));
3386
3387 Assert(mAuthLibrary && (mpfnAuthEntry || mpfnAuthEntry2 || mpfnAuthEntry3));
3388
3389 AuthCtx ctx;
3390 ctx.result = AuthResultAccessDenied; /* Not used. */
3391 ctx.pfnAuthEntry3 = mpfnAuthEntry3;
3392 ctx.pfnAuthEntry2 = mpfnAuthEntry2;
3393 ctx.pfnAuthEntry = NULL; /* Does not use disconnect notification. */
3394 ctx.pszCaller = "vrde";
3395 ctx.pUuid = &rawuuid;
3396 ctx.guestJudgement = AuthGuestNotAsked;
3397 ctx.pszUser = NULL;
3398 ctx.pszPassword = NULL;
3399 ctx.pszDomain = NULL;
3400 ctx.fLogon = false;
3401 ctx.clientId = u32ClientId;
3402
3403 authCall(&ctx);
3404}
3405
3406int ConsoleVRDPServer::lockConsoleVRDPServer(void)
3407{
3408 int rc = RTCritSectEnter(&mCritSect);
3409 AssertRC(rc);
3410 return rc;
3411}
3412
3413void ConsoleVRDPServer::unlockConsoleVRDPServer(void)
3414{
3415 RTCritSectLeave(&mCritSect);
3416}
3417
3418DECLCALLBACK(int) ConsoleVRDPServer::ClipboardCallback(void *pvCallback,
3419 uint32_t u32ClientId,
3420 uint32_t u32Function,
3421 uint32_t u32Format,
3422 const void *pvData,
3423 uint32_t cbData)
3424{
3425 LogFlowFunc(("pvCallback = %p, u32ClientId = %d, u32Function = %d, u32Format = 0x%08X, pvData = %p, cbData = %d\n",
3426 pvCallback, u32ClientId, u32Function, u32Format, pvData, cbData));
3427
3428 int rc = VINF_SUCCESS;
3429
3430 ConsoleVRDPServer *pServer = static_cast <ConsoleVRDPServer *>(pvCallback);
3431
3432 NOREF(u32ClientId);
3433
3434 switch (u32Function)
3435 {
3436 case VRDE_CLIPBOARD_FUNCTION_FORMAT_ANNOUNCE:
3437 {
3438 if (pServer->mpfnClipboardCallback)
3439 {
3440 pServer->mpfnClipboardCallback(VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE,
3441 u32Format,
3442 (void *)pvData,
3443 cbData);
3444 }
3445 } break;
3446
3447 case VRDE_CLIPBOARD_FUNCTION_DATA_READ:
3448 {
3449 if (pServer->mpfnClipboardCallback)
3450 {
3451 pServer->mpfnClipboardCallback(VBOX_CLIPBOARD_EXT_FN_DATA_READ,
3452 u32Format,
3453 (void *)pvData,
3454 cbData);
3455 }
3456 } break;
3457
3458 default:
3459 rc = VERR_NOT_SUPPORTED;
3460 }
3461
3462 return rc;
3463}
3464
3465DECLCALLBACK(int) ConsoleVRDPServer::ClipboardServiceExtension(void *pvExtension,
3466 uint32_t u32Function,
3467 void *pvParms,
3468 uint32_t cbParms)
3469{
3470 LogFlowFunc(("pvExtension = %p, u32Function = %d, pvParms = %p, cbParms = %d\n",
3471 pvExtension, u32Function, pvParms, cbParms));
3472
3473 int rc = VINF_SUCCESS;
3474
3475 ConsoleVRDPServer *pServer = static_cast <ConsoleVRDPServer *>(pvExtension);
3476
3477 VBOXCLIPBOARDEXTPARMS *pParms = (VBOXCLIPBOARDEXTPARMS *)pvParms;
3478
3479 switch (u32Function)
3480 {
3481 case VBOX_CLIPBOARD_EXT_FN_SET_CALLBACK:
3482 {
3483 pServer->mpfnClipboardCallback = pParms->u.pfnCallback;
3484 } break;
3485
3486 case VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE:
3487 {
3488 /* The guest announces clipboard formats. This must be delivered to all clients. */
3489 if (mpEntryPoints && pServer->mhServer)
3490 {
3491 mpEntryPoints->VRDEClipboard(pServer->mhServer,
3492 VRDE_CLIPBOARD_FUNCTION_FORMAT_ANNOUNCE,
3493 pParms->u32Format,
3494 NULL,
3495 0,
3496 NULL);
3497 }
3498 } break;
3499
3500 case VBOX_CLIPBOARD_EXT_FN_DATA_READ:
3501 {
3502 /* The clipboard service expects that the pvData buffer will be filled
3503 * with clipboard data. The server returns the data from the client that
3504 * announced the requested format most recently.
3505 */
3506 if (mpEntryPoints && pServer->mhServer)
3507 {
3508 mpEntryPoints->VRDEClipboard(pServer->mhServer,
3509 VRDE_CLIPBOARD_FUNCTION_DATA_READ,
3510 pParms->u32Format,
3511 pParms->u.pvData,
3512 pParms->cbData,
3513 &pParms->cbData);
3514 }
3515 } break;
3516
3517 case VBOX_CLIPBOARD_EXT_FN_DATA_WRITE:
3518 {
3519 if (mpEntryPoints && pServer->mhServer)
3520 {
3521 mpEntryPoints->VRDEClipboard(pServer->mhServer,
3522 VRDE_CLIPBOARD_FUNCTION_DATA_WRITE,
3523 pParms->u32Format,
3524 pParms->u.pvData,
3525 pParms->cbData,
3526 NULL);
3527 }
3528 } break;
3529
3530 default:
3531 rc = VERR_NOT_SUPPORTED;
3532 }
3533
3534 return rc;
3535}
3536
3537void ConsoleVRDPServer::ClipboardCreate(uint32_t u32ClientId)
3538{
3539 int rc = lockConsoleVRDPServer();
3540
3541 if (RT_SUCCESS(rc))
3542 {
3543 if (mcClipboardRefs == 0)
3544 {
3545 rc = HGCMHostRegisterServiceExtension(&mhClipboard, "VBoxSharedClipboard", ClipboardServiceExtension, this);
3546
3547 if (RT_SUCCESS(rc))
3548 {
3549 mcClipboardRefs++;
3550 }
3551 }
3552
3553 unlockConsoleVRDPServer();
3554 }
3555}
3556
3557void ConsoleVRDPServer::ClipboardDelete(uint32_t u32ClientId)
3558{
3559 int rc = lockConsoleVRDPServer();
3560
3561 if (RT_SUCCESS(rc))
3562 {
3563 mcClipboardRefs--;
3564
3565 if (mcClipboardRefs == 0)
3566 {
3567 HGCMHostUnregisterServiceExtension(mhClipboard);
3568 }
3569
3570 unlockConsoleVRDPServer();
3571 }
3572}
3573
3574/* That is called on INPUT thread of the VRDP server.
3575 * The ConsoleVRDPServer keeps a list of created backend instances.
3576 */
3577void ConsoleVRDPServer::USBBackendCreate(uint32_t u32ClientId, void **ppvIntercept)
3578{
3579#ifdef VBOX_WITH_USB
3580 LogFlow(("ConsoleVRDPServer::USBBackendCreate: u32ClientId = %d\n", u32ClientId));
3581
3582 /* Create a new instance of the USB backend for the new client. */
3583 RemoteUSBBackend *pRemoteUSBBackend = new RemoteUSBBackend(mConsole, this, u32ClientId);
3584
3585 if (pRemoteUSBBackend)
3586 {
3587 pRemoteUSBBackend->AddRef(); /* 'Release' called in USBBackendDelete. */
3588
3589 /* Append the new instance in the list. */
3590 int rc = lockConsoleVRDPServer();
3591
3592 if (RT_SUCCESS(rc))
3593 {
3594 pRemoteUSBBackend->pNext = mUSBBackends.pHead;
3595 if (mUSBBackends.pHead)
3596 {
3597 mUSBBackends.pHead->pPrev = pRemoteUSBBackend;
3598 }
3599 else
3600 {
3601 mUSBBackends.pTail = pRemoteUSBBackend;
3602 }
3603
3604 mUSBBackends.pHead = pRemoteUSBBackend;
3605
3606 unlockConsoleVRDPServer();
3607
3608 if (ppvIntercept)
3609 {
3610 *ppvIntercept = pRemoteUSBBackend;
3611 }
3612 }
3613
3614 if (RT_FAILURE(rc))
3615 {
3616 pRemoteUSBBackend->Release();
3617 }
3618 }
3619#endif /* VBOX_WITH_USB */
3620}
3621
3622void ConsoleVRDPServer::USBBackendDelete(uint32_t u32ClientId)
3623{
3624#ifdef VBOX_WITH_USB
3625 LogFlow(("ConsoleVRDPServer::USBBackendDelete: u32ClientId = %d\n", u32ClientId));
3626
3627 RemoteUSBBackend *pRemoteUSBBackend = NULL;
3628
3629 /* Find the instance. */
3630 int rc = lockConsoleVRDPServer();
3631
3632 if (RT_SUCCESS(rc))
3633 {
3634 pRemoteUSBBackend = usbBackendFind(u32ClientId);
3635
3636 if (pRemoteUSBBackend)
3637 {
3638 /* Notify that it will be deleted. */
3639 pRemoteUSBBackend->NotifyDelete();
3640 }
3641
3642 unlockConsoleVRDPServer();
3643 }
3644
3645 if (pRemoteUSBBackend)
3646 {
3647 /* Here the instance has been excluded from the list and can be dereferenced. */
3648 pRemoteUSBBackend->Release();
3649 }
3650#endif
3651}
3652
3653void *ConsoleVRDPServer::USBBackendRequestPointer(uint32_t u32ClientId, const Guid *pGuid)
3654{
3655#ifdef VBOX_WITH_USB
3656 RemoteUSBBackend *pRemoteUSBBackend = NULL;
3657
3658 /* Find the instance. */
3659 int rc = lockConsoleVRDPServer();
3660
3661 if (RT_SUCCESS(rc))
3662 {
3663 pRemoteUSBBackend = usbBackendFind(u32ClientId);
3664
3665 if (pRemoteUSBBackend)
3666 {
3667 /* Inform the backend instance that it is referenced by the Guid. */
3668 bool fAdded = pRemoteUSBBackend->addUUID(pGuid);
3669
3670 if (fAdded)
3671 {
3672 /* Reference the instance because its pointer is being taken. */
3673 pRemoteUSBBackend->AddRef(); /* 'Release' is called in USBBackendReleasePointer. */
3674 }
3675 else
3676 {
3677 pRemoteUSBBackend = NULL;
3678 }
3679 }
3680
3681 unlockConsoleVRDPServer();
3682 }
3683
3684 if (pRemoteUSBBackend)
3685 {
3686 return pRemoteUSBBackend->GetBackendCallbackPointer();
3687 }
3688
3689#endif
3690 return NULL;
3691}
3692
3693void ConsoleVRDPServer::USBBackendReleasePointer(const Guid *pGuid)
3694{
3695#ifdef VBOX_WITH_USB
3696 RemoteUSBBackend *pRemoteUSBBackend = NULL;
3697
3698 /* Find the instance. */
3699 int rc = lockConsoleVRDPServer();
3700
3701 if (RT_SUCCESS(rc))
3702 {
3703 pRemoteUSBBackend = usbBackendFindByUUID(pGuid);
3704
3705 if (pRemoteUSBBackend)
3706 {
3707 pRemoteUSBBackend->removeUUID(pGuid);
3708 }
3709
3710 unlockConsoleVRDPServer();
3711
3712 if (pRemoteUSBBackend)
3713 {
3714 pRemoteUSBBackend->Release();
3715 }
3716 }
3717#endif
3718}
3719
3720RemoteUSBBackend *ConsoleVRDPServer::usbBackendGetNext(RemoteUSBBackend *pRemoteUSBBackend)
3721{
3722 LogFlow(("ConsoleVRDPServer::usbBackendGetNext: pBackend = %p\n", pRemoteUSBBackend));
3723
3724 RemoteUSBBackend *pNextRemoteUSBBackend = NULL;
3725#ifdef VBOX_WITH_USB
3726
3727 int rc = lockConsoleVRDPServer();
3728
3729 if (RT_SUCCESS(rc))
3730 {
3731 if (pRemoteUSBBackend == NULL)
3732 {
3733 /* The first backend in the list is requested. */
3734 pNextRemoteUSBBackend = mUSBBackends.pHead;
3735 }
3736 else
3737 {
3738 /* Get pointer to the next backend. */
3739 pNextRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
3740 }
3741
3742 if (pNextRemoteUSBBackend)
3743 {
3744 pNextRemoteUSBBackend->AddRef();
3745 }
3746
3747 unlockConsoleVRDPServer();
3748
3749 if (pRemoteUSBBackend)
3750 {
3751 pRemoteUSBBackend->Release();
3752 }
3753 }
3754#endif
3755
3756 return pNextRemoteUSBBackend;
3757}
3758
3759#ifdef VBOX_WITH_USB
3760/* Internal method. Called under the ConsoleVRDPServerLock. */
3761RemoteUSBBackend *ConsoleVRDPServer::usbBackendFind(uint32_t u32ClientId)
3762{
3763 RemoteUSBBackend *pRemoteUSBBackend = mUSBBackends.pHead;
3764
3765 while (pRemoteUSBBackend)
3766 {
3767 if (pRemoteUSBBackend->ClientId() == u32ClientId)
3768 {
3769 break;
3770 }
3771
3772 pRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
3773 }
3774
3775 return pRemoteUSBBackend;
3776}
3777
3778/* Internal method. Called under the ConsoleVRDPServerLock. */
3779RemoteUSBBackend *ConsoleVRDPServer::usbBackendFindByUUID(const Guid *pGuid)
3780{
3781 RemoteUSBBackend *pRemoteUSBBackend = mUSBBackends.pHead;
3782
3783 while (pRemoteUSBBackend)
3784 {
3785 if (pRemoteUSBBackend->findUUID(pGuid))
3786 {
3787 break;
3788 }
3789
3790 pRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
3791 }
3792
3793 return pRemoteUSBBackend;
3794}
3795#endif
3796
3797/* Internal method. Called by the backend destructor. */
3798void ConsoleVRDPServer::usbBackendRemoveFromList(RemoteUSBBackend *pRemoteUSBBackend)
3799{
3800#ifdef VBOX_WITH_USB
3801 int rc = lockConsoleVRDPServer();
3802 AssertRC(rc);
3803
3804 /* Exclude the found instance from the list. */
3805 if (pRemoteUSBBackend->pNext)
3806 {
3807 pRemoteUSBBackend->pNext->pPrev = pRemoteUSBBackend->pPrev;
3808 }
3809 else
3810 {
3811 mUSBBackends.pTail = (RemoteUSBBackend *)pRemoteUSBBackend->pPrev;
3812 }
3813
3814 if (pRemoteUSBBackend->pPrev)
3815 {
3816 pRemoteUSBBackend->pPrev->pNext = pRemoteUSBBackend->pNext;
3817 }
3818 else
3819 {
3820 mUSBBackends.pHead = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
3821 }
3822
3823 pRemoteUSBBackend->pNext = pRemoteUSBBackend->pPrev = NULL;
3824
3825 unlockConsoleVRDPServer();
3826#endif
3827}
3828
3829
3830void ConsoleVRDPServer::SendUpdate(unsigned uScreenId, void *pvUpdate, uint32_t cbUpdate) const
3831{
3832 if (mpEntryPoints && mhServer)
3833 {
3834 mpEntryPoints->VRDEUpdate(mhServer, uScreenId, pvUpdate, cbUpdate);
3835 }
3836}
3837
3838void ConsoleVRDPServer::SendResize(void)
3839{
3840 if (mpEntryPoints && mhServer)
3841 {
3842 ++mcInResize;
3843 mpEntryPoints->VRDEResize(mhServer);
3844 --mcInResize;
3845 }
3846}
3847
3848void ConsoleVRDPServer::SendUpdateBitmap(unsigned uScreenId, uint32_t x, uint32_t y, uint32_t w, uint32_t h) const
3849{
3850 VRDEORDERHDR update;
3851 update.x = x;
3852 update.y = y;
3853 update.w = w;
3854 update.h = h;
3855 if (mpEntryPoints && mhServer)
3856 {
3857 mpEntryPoints->VRDEUpdate(mhServer, uScreenId, &update, sizeof(update));
3858 }
3859}
3860
3861void ConsoleVRDPServer::SendAudioSamples(void *pvSamples, uint32_t cSamples, VRDEAUDIOFORMAT format) const
3862{
3863 if (mpEntryPoints && mhServer)
3864 {
3865 mpEntryPoints->VRDEAudioSamples(mhServer, pvSamples, cSamples, format);
3866 }
3867}
3868
3869void ConsoleVRDPServer::SendAudioVolume(uint16_t left, uint16_t right) const
3870{
3871 if (mpEntryPoints && mhServer)
3872 {
3873 mpEntryPoints->VRDEAudioVolume(mhServer, left, right);
3874 }
3875}
3876
3877void ConsoleVRDPServer::SendUSBRequest(uint32_t u32ClientId, void *pvParms, uint32_t cbParms) const
3878{
3879 if (mpEntryPoints && mhServer)
3880 {
3881 mpEntryPoints->VRDEUSBRequest(mhServer, u32ClientId, pvParms, cbParms);
3882 }
3883}
3884
3885/* @todo rc not needed? */
3886int ConsoleVRDPServer::SendAudioInputBegin(void **ppvUserCtx,
3887 void *pvContext,
3888 uint32_t cSamples,
3889 uint32_t iSampleHz,
3890 uint32_t cChannels,
3891 uint32_t cBits)
3892{
3893 if (mpEntryPoints && mhServer && mpEntryPoints->VRDEAudioInOpen)
3894 {
3895 uint32_t u32ClientId = ASMAtomicReadU32(&mu32AudioInputClientId);
3896 if (u32ClientId != 0) /* 0 would mean broadcast to all clients. */
3897 {
3898 VRDEAUDIOFORMAT audioFormat = VRDE_AUDIO_FMT_MAKE(iSampleHz, cChannels, cBits, 0);
3899 mpEntryPoints->VRDEAudioInOpen (mhServer,
3900 pvContext,
3901 u32ClientId,
3902 audioFormat,
3903 cSamples);
3904#ifdef VBOX_WITH_PDM_AUDIO_DRIVER
3905 //*ppvUserCtx = NULL;
3906#else
3907 *ppvUserCtx = NULL; /* This is the ConsoleVRDPServer context.
3908- * Currently not used because only one client is allowed to
3909- * do audio input and the client id is saved by the ConsoleVRDPServer.
3910- */
3911#endif
3912
3913 return VINF_SUCCESS;
3914 }
3915 }
3916 return VERR_NOT_SUPPORTED;
3917}
3918
3919void ConsoleVRDPServer::SendAudioInputEnd(void *pvUserCtx)
3920{
3921 if (mpEntryPoints && mhServer && mpEntryPoints->VRDEAudioInClose)
3922 {
3923 uint32_t u32ClientId = ASMAtomicReadU32(&mu32AudioInputClientId);
3924 if (u32ClientId != 0) /* 0 would mean broadcast to all clients. */
3925 {
3926 mpEntryPoints->VRDEAudioInClose(mhServer, u32ClientId);
3927 }
3928 }
3929}
3930
3931
3932void ConsoleVRDPServer::QueryInfo(uint32_t index, void *pvBuffer, uint32_t cbBuffer, uint32_t *pcbOut) const
3933{
3934 if (index == VRDE_QI_PORT)
3935 {
3936 uint32_t cbOut = sizeof(int32_t);
3937
3938 if (cbBuffer >= cbOut)
3939 {
3940 *pcbOut = cbOut;
3941 *(int32_t *)pvBuffer = (int32_t)mVRDPBindPort;
3942 }
3943 }
3944 else if (mpEntryPoints && mhServer)
3945 {
3946 mpEntryPoints->VRDEQueryInfo(mhServer, index, pvBuffer, cbBuffer, pcbOut);
3947 }
3948}
3949
3950/* static */ int ConsoleVRDPServer::loadVRDPLibrary(const char *pszLibraryName)
3951{
3952 int rc = VINF_SUCCESS;
3953
3954 if (mVRDPLibrary == NIL_RTLDRMOD)
3955 {
3956 RTERRINFOSTATIC ErrInfo;
3957 RTErrInfoInitStatic(&ErrInfo);
3958
3959 if (RTPathHavePath(pszLibraryName))
3960 rc = SUPR3HardenedLdrLoadPlugIn(pszLibraryName, &mVRDPLibrary, &ErrInfo.Core);
3961 else
3962 rc = SUPR3HardenedLdrLoadAppPriv(pszLibraryName, &mVRDPLibrary, RTLDRLOAD_FLAGS_LOCAL, &ErrInfo.Core);
3963 if (RT_SUCCESS(rc))
3964 {
3965 struct SymbolEntry
3966 {
3967 const char *name;
3968 void **ppfn;
3969 };
3970
3971 #define DEFSYMENTRY(a) { #a, (void**)&mpfn##a }
3972
3973 static const struct SymbolEntry s_aSymbols[] =
3974 {
3975 DEFSYMENTRY(VRDECreateServer)
3976 };
3977
3978 #undef DEFSYMENTRY
3979
3980 for (unsigned i = 0; i < RT_ELEMENTS(s_aSymbols); i++)
3981 {
3982 rc = RTLdrGetSymbol(mVRDPLibrary, s_aSymbols[i].name, s_aSymbols[i].ppfn);
3983
3984 if (RT_FAILURE(rc))
3985 {
3986 LogRel(("VRDE: Error resolving symbol '%s', rc %Rrc.\n", s_aSymbols[i].name, rc));
3987 break;
3988 }
3989 }
3990 }
3991 else
3992 {
3993 if (RTErrInfoIsSet(&ErrInfo.Core))
3994 LogRel(("VRDE: Error loading the library '%s': %s (%Rrc)\n", pszLibraryName, ErrInfo.Core.pszMsg, rc));
3995 else
3996 LogRel(("VRDE: Error loading the library '%s' rc = %Rrc.\n", pszLibraryName, rc));
3997
3998 mVRDPLibrary = NIL_RTLDRMOD;
3999 }
4000 }
4001
4002 if (RT_FAILURE(rc))
4003 {
4004 if (mVRDPLibrary != NIL_RTLDRMOD)
4005 {
4006 RTLdrClose(mVRDPLibrary);
4007 mVRDPLibrary = NIL_RTLDRMOD;
4008 }
4009 }
4010
4011 return rc;
4012}
4013
4014/*
4015 * IVRDEServerInfo implementation.
4016 */
4017// constructor / destructor
4018/////////////////////////////////////////////////////////////////////////////
4019
4020VRDEServerInfo::VRDEServerInfo()
4021 : mParent(NULL)
4022{
4023}
4024
4025VRDEServerInfo::~VRDEServerInfo()
4026{
4027}
4028
4029
4030HRESULT VRDEServerInfo::FinalConstruct()
4031{
4032 return BaseFinalConstruct();
4033}
4034
4035void VRDEServerInfo::FinalRelease()
4036{
4037 uninit();
4038 BaseFinalRelease();
4039}
4040
4041// public methods only for internal purposes
4042/////////////////////////////////////////////////////////////////////////////
4043
4044/**
4045 * Initializes the guest object.
4046 */
4047HRESULT VRDEServerInfo::init(Console *aParent)
4048{
4049 LogFlowThisFunc(("aParent=%p\n", aParent));
4050
4051 ComAssertRet(aParent, E_INVALIDARG);
4052
4053 /* Enclose the state transition NotReady->InInit->Ready */
4054 AutoInitSpan autoInitSpan(this);
4055 AssertReturn(autoInitSpan.isOk(), E_FAIL);
4056
4057 unconst(mParent) = aParent;
4058
4059 /* Confirm a successful initialization */
4060 autoInitSpan.setSucceeded();
4061
4062 return S_OK;
4063}
4064
4065/**
4066 * Uninitializes the instance and sets the ready flag to FALSE.
4067 * Called either from FinalRelease() or by the parent when it gets destroyed.
4068 */
4069void VRDEServerInfo::uninit()
4070{
4071 LogFlowThisFunc(("\n"));
4072
4073 /* Enclose the state transition Ready->InUninit->NotReady */
4074 AutoUninitSpan autoUninitSpan(this);
4075 if (autoUninitSpan.uninitDone())
4076 return;
4077
4078 unconst(mParent) = NULL;
4079}
4080
4081// IVRDEServerInfo properties
4082/////////////////////////////////////////////////////////////////////////////
4083
4084#define IMPL_GETTER_BOOL(_aType, _aName, _aIndex) \
4085 STDMETHODIMP VRDEServerInfo::COMGETTER(_aName)(_aType *a##_aName) \
4086 { \
4087 if (!a##_aName) \
4088 return E_POINTER; \
4089 \
4090 AutoCaller autoCaller(this); \
4091 if (FAILED(autoCaller.rc())) return autoCaller.rc(); \
4092 \
4093 /* todo: Not sure if a AutoReadLock would be sufficient. */ \
4094 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); \
4095 \
4096 uint32_t value; \
4097 uint32_t cbOut = 0; \
4098 \
4099 mParent->consoleVRDPServer()->QueryInfo \
4100 (_aIndex, &value, sizeof(value), &cbOut); \
4101 \
4102 *a##_aName = cbOut? !!value: FALSE; \
4103 \
4104 return S_OK; \
4105 } \
4106 extern void IMPL_GETTER_BOOL_DUMMY(void)
4107
4108#define IMPL_GETTER_SCALAR(_aType, _aName, _aIndex, _aValueMask) \
4109 STDMETHODIMP VRDEServerInfo::COMGETTER(_aName)(_aType *a##_aName) \
4110 { \
4111 if (!a##_aName) \
4112 return E_POINTER; \
4113 \
4114 AutoCaller autoCaller(this); \
4115 if (FAILED(autoCaller.rc())) return autoCaller.rc(); \
4116 \
4117 /* todo: Not sure if a AutoReadLock would be sufficient. */ \
4118 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); \
4119 \
4120 _aType value; \
4121 uint32_t cbOut = 0; \
4122 \
4123 mParent->consoleVRDPServer()->QueryInfo \
4124 (_aIndex, &value, sizeof(value), &cbOut); \
4125 \
4126 if (_aValueMask) value &= (_aValueMask); \
4127 *a##_aName = cbOut? value: 0; \
4128 \
4129 return S_OK; \
4130 } \
4131 extern void IMPL_GETTER_SCALAR_DUMMY(void)
4132
4133#define IMPL_GETTER_BSTR(_aType, _aName, _aIndex) \
4134 STDMETHODIMP VRDEServerInfo::COMGETTER(_aName)(_aType *a##_aName) \
4135 { \
4136 if (!a##_aName) \
4137 return E_POINTER; \
4138 \
4139 AutoCaller autoCaller(this); \
4140 if (FAILED(autoCaller.rc())) return autoCaller.rc(); \
4141 \
4142 /* todo: Not sure if a AutoReadLock would be sufficient. */ \
4143 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); \
4144 \
4145 uint32_t cbOut = 0; \
4146 \
4147 mParent->consoleVRDPServer()->QueryInfo \
4148 (_aIndex, NULL, 0, &cbOut); \
4149 \
4150 if (cbOut == 0) \
4151 { \
4152 Bstr str(""); \
4153 str.cloneTo(a##_aName); \
4154 return S_OK; \
4155 } \
4156 \
4157 char *pchBuffer = (char *)RTMemTmpAlloc(cbOut); \
4158 \
4159 if (!pchBuffer) \
4160 { \
4161 Log(("VRDEServerInfo::" \
4162 #_aName \
4163 ": Failed to allocate memory %d bytes\n", cbOut)); \
4164 return E_OUTOFMEMORY; \
4165 } \
4166 \
4167 mParent->consoleVRDPServer()->QueryInfo \
4168 (_aIndex, pchBuffer, cbOut, &cbOut); \
4169 \
4170 Bstr str(pchBuffer); \
4171 \
4172 str.cloneTo(a##_aName); \
4173 \
4174 RTMemTmpFree(pchBuffer); \
4175 \
4176 return S_OK; \
4177 } \
4178 extern void IMPL_GETTER_BSTR_DUMMY(void)
4179
4180IMPL_GETTER_BOOL (BOOL, Active, VRDE_QI_ACTIVE);
4181IMPL_GETTER_SCALAR (LONG, Port, VRDE_QI_PORT, 0);
4182IMPL_GETTER_SCALAR (ULONG, NumberOfClients, VRDE_QI_NUMBER_OF_CLIENTS, 0);
4183IMPL_GETTER_SCALAR (LONG64, BeginTime, VRDE_QI_BEGIN_TIME, 0);
4184IMPL_GETTER_SCALAR (LONG64, EndTime, VRDE_QI_END_TIME, 0);
4185IMPL_GETTER_SCALAR (LONG64, BytesSent, VRDE_QI_BYTES_SENT, INT64_MAX);
4186IMPL_GETTER_SCALAR (LONG64, BytesSentTotal, VRDE_QI_BYTES_SENT_TOTAL, INT64_MAX);
4187IMPL_GETTER_SCALAR (LONG64, BytesReceived, VRDE_QI_BYTES_RECEIVED, INT64_MAX);
4188IMPL_GETTER_SCALAR (LONG64, BytesReceivedTotal, VRDE_QI_BYTES_RECEIVED_TOTAL, INT64_MAX);
4189IMPL_GETTER_BSTR (BSTR, User, VRDE_QI_USER);
4190IMPL_GETTER_BSTR (BSTR, Domain, VRDE_QI_DOMAIN);
4191IMPL_GETTER_BSTR (BSTR, ClientName, VRDE_QI_CLIENT_NAME);
4192IMPL_GETTER_BSTR (BSTR, ClientIP, VRDE_QI_CLIENT_IP);
4193IMPL_GETTER_SCALAR (ULONG, ClientVersion, VRDE_QI_CLIENT_VERSION, 0);
4194IMPL_GETTER_SCALAR (ULONG, EncryptionStyle, VRDE_QI_ENCRYPTION_STYLE, 0);
4195
4196#undef IMPL_GETTER_BSTR
4197#undef IMPL_GETTER_SCALAR
4198#undef IMPL_GETTER_BOOL
4199/* 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