VirtualBox

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

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

PDM Audio: Branch -> trunk.

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