VirtualBox

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

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

Main: IDisplay converted to use API wrappers.

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

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