VirtualBox

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

Last change on this file since 80375 was 80375, checked in by vboxsync, 6 years ago

Main/ConsoleVRDPServer.cpp: Remove dependency on old chromium based 3D redirection, disabled generic code so it might get reused when this gets implemented for VMSVGA3D, bugref:9529

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

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