VirtualBox

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

Last change on this file since 44152 was 43929, checked in by vboxsync, 12 years ago

ConsoleVRDPServer: VideoIn updates

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

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