VirtualBox

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

Last change on this file since 82319 was 81964, checked in by vboxsync, 5 years ago

Main/GraphicsAdapter: Split off a few attributes from Machine interface, which affects quite a few other interfaces.
Frontends/VirtualBox+VBoxManage+VBoxSDL+VBoxShell: Adapt accordingly.

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