VirtualBox

source: vbox/trunk/src/VBox/Main/include/ConsoleImpl.h@ 2988

Last change on this file since 2988 was 2981, checked in by vboxsync, 17 years ago

InnoTek -> innotek: all the headers and comments.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 19.1 KB
Line 
1/** @file
2 *
3 * VBox Console COM Class definition
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
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 as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * If you received this file as part of a commercial VirtualBox
18 * distribution, then only the terms of your commercial VirtualBox
19 * license agreement apply instead of the previous paragraph.
20 */
21
22#ifndef ____H_CONSOLEIMPL
23#define ____H_CONSOLEIMPL
24
25#include "VirtualBoxBase.h"
26#include "ProgressImpl.h"
27
28class Guest;
29class Keyboard;
30class Mouse;
31class Display;
32class MachineDebugger;
33class OUSBDevice;
34class RemoteUSBDevice;
35class SharedFolder;
36class RemoteDisplayInfo;
37class AudioSniffer;
38class ConsoleVRDPServer;
39class VMMDev;
40
41#include <VBox/vrdpapi.h>
42#include <VBox/pdm.h>
43
44struct VUSBIRHCONFIG;
45typedef struct VUSBIRHCONFIG *PVUSBIRHCONFIG;
46
47#include <list>
48
49// defines
50///////////////////////////////////////////////////////////////////////////////
51
52/**
53 * Checks the availability of the underlying VM device driver corresponding
54 * to the COM interface (IKeyboard, IMouse, IDisplay, etc.). When the driver is
55 * not available (NULL), sets error info and returns returns E_ACCESSDENIED.
56 * The translatable error message is defined in null context.
57 *
58 * Intended to used only within Console children (i,e. Keyboard, Mouse,
59 * Display, etc.).
60 *
61 * @param drv driver pointer to check (compare it with NULL)
62 */
63#define CHECK_CONSOLE_DRV(drv) \
64 do { \
65 if (!(drv)) \
66 return setError (E_ACCESSDENIED, tr ("The console is not powered up")); \
67 } while (0)
68
69/** @def VBOX_WITH_UNIXY_TAP_NETWORKING
70 * Unixy style TAP networking. This is defined in the Makefile since it's also
71 * used by NetworkAdapterImpl.h/cpp.
72 */
73#ifdef __DOXYGEN__
74# define VBOX_WITH_UNIXY_TAP_NETWORKING
75#endif
76
77// Console
78///////////////////////////////////////////////////////////////////////////////
79
80/** IConsole implementation class */
81class ATL_NO_VTABLE Console :
82 public VirtualBoxBaseWithChildrenNEXT,
83 public VirtualBoxSupportErrorInfoImpl <Console, IConsole>,
84 public VirtualBoxSupportTranslation <Console>,
85 public IConsole
86{
87 Q_OBJECT
88
89public:
90
91 DECLARE_NOT_AGGREGATABLE(Console)
92
93 DECLARE_PROTECT_FINAL_CONSTRUCT()
94
95 BEGIN_COM_MAP(Console)
96 COM_INTERFACE_ENTRY(ISupportErrorInfo)
97 COM_INTERFACE_ENTRY(IConsole)
98 END_COM_MAP()
99
100 NS_DECL_ISUPPORTS
101
102 Console();
103 ~Console();
104
105 HRESULT FinalConstruct();
106 void FinalRelease();
107
108 // public initializers/uninitializers for internal purposes only
109 HRESULT init (IMachine *aMachine, IInternalMachineControl *aControl);
110 void uninit();
111
112 // IConsole properties
113 STDMETHOD(COMGETTER(Machine)) (IMachine **aMachine);
114 STDMETHOD(COMGETTER(State)) (MachineState_T *aMachineState);
115 STDMETHOD(COMGETTER(Guest)) (IGuest **aGuest);
116 STDMETHOD(COMGETTER(Keyboard)) (IKeyboard **aKeyboard);
117 STDMETHOD(COMGETTER(Mouse)) (IMouse **aMouse);
118 STDMETHOD(COMGETTER(Display)) (IDisplay **aDisplay);
119 STDMETHOD(COMGETTER(Debugger)) (IMachineDebugger **aDebugger);
120 STDMETHOD(COMGETTER(USBDevices)) (IUSBDeviceCollection **aUSBDevices);
121 STDMETHOD(COMGETTER(RemoteUSBDevices)) (IHostUSBDeviceCollection **aRemoteUSBDevices);
122 STDMETHOD(COMGETTER(RemoteDisplayInfo)) (IRemoteDisplayInfo **aRemoteDisplayInfo);
123 STDMETHOD(COMGETTER(SharedFolders)) (ISharedFolderCollection **aSharedFolders);
124
125 // IConsole methods
126 STDMETHOD(PowerUp) (IProgress **aProgress);
127 STDMETHOD(PowerDown)();
128 STDMETHOD(Reset)();
129 STDMETHOD(Pause)();
130 STDMETHOD(Resume)();
131 STDMETHOD(PowerButton)();
132 STDMETHOD(SaveState) (IProgress **aProgress);
133 STDMETHOD(DiscardSavedState)();
134 STDMETHOD(GetDeviceActivity) (DeviceType_T aDeviceType,
135 DeviceActivity_T *aDeviceActivity);
136 STDMETHOD(AttachUSBDevice) (INPTR GUIDPARAM aId);
137 STDMETHOD(DetachUSBDevice) (INPTR GUIDPARAM aId, IUSBDevice **aDevice);
138 STDMETHOD(CreateSharedFolder) (INPTR BSTR aName, INPTR BSTR aHostPath);
139 STDMETHOD(RemoveSharedFolder) (INPTR BSTR aName);
140 STDMETHOD(TakeSnapshot) (INPTR BSTR aName, INPTR BSTR aDescription,
141 IProgress **aProgress);
142 STDMETHOD(DiscardSnapshot) (INPTR GUIDPARAM aId, IProgress **aProgress);
143 STDMETHOD(DiscardCurrentState) (IProgress **aProgress);
144 STDMETHOD(DiscardCurrentSnapshotAndState) (IProgress **aProgress);
145 STDMETHOD(RegisterCallback) (IConsoleCallback *aCallback);
146 STDMETHOD(UnregisterCallback)(IConsoleCallback *aCallback);
147
148 // public methods for internal purposes only
149
150 /*
151 * Note: the following methods do not increase refcount. intended to be
152 * called only by the VM execution thread.
153 */
154
155 Guest *getGuest() { return mGuest; }
156 Keyboard *getKeyboard() { return mKeyboard; }
157 Mouse *getMouse() { return mMouse; }
158 Display *getDisplay() { return mDisplay; }
159 MachineDebugger *getMachineDebugger() { return mDebugger; }
160
161 const ComPtr <IMachine> &machine() { return mMachine; }
162
163 /** Method is called only from ConsoleVRDPServer */
164 IVRDPServer *getVRDPServer() { return mVRDPServer; }
165
166 ConsoleVRDPServer *consoleVRDPServer() { return mConsoleVRDPServer; }
167
168 HRESULT updateMachineState (MachineState_T aMachineState);
169
170 // events from IInternalSessionControl
171 HRESULT onDVDDriveChange();
172 HRESULT onFloppyDriveChange();
173 HRESULT onNetworkAdapterChange(INetworkAdapter *networkAdapter);
174 HRESULT onVRDPServerChange();
175 HRESULT onUSBControllerChange();
176 HRESULT onUSBDeviceAttach(IUSBDevice *aDevice);
177 HRESULT onUSBDeviceDetach(INPTR GUIDPARAM aId);
178
179 VMMDev *getVMMDev() { return mVMMDev; }
180 AudioSniffer *getAudioSniffer () { return mAudioSniffer; }
181
182 static VRDPSERVERCALLBACK *getVrdpServerCallback () { return &sVrdpServerCallback; };
183
184#ifdef VRDP_MC
185 void processRemoteUSBDevices (uint32_t u32ClientId, VRDPUSBDEVICEDESC *pDevList, uint32_t cbDevList);
186#else
187 void processRemoteUSBDevices (VRDPUSBDEVICEDESC *pDevList, uint32_t cbDevList);
188#endif /* VRDP_MC */
189
190 // callback callers
191 void onMousePointerShapeChange(bool fVisible, bool fAlpha,
192 uint32_t xHot, uint32_t yHot,
193 uint32_t width, uint32_t height,
194 void *pShape);
195 void onMouseCapabilityChange (BOOL supportsAbsolute, BOOL needsHostCursor);
196 void onStateChange (MachineState_T aMachineState);
197 void onAdditionsStateChange();
198 void onAdditionsOutdated();
199 void onKeyboardLedsChange (bool fNumLock, bool fCapsLock, bool fScrollLock);
200 void onRuntimeError (BOOL aFatal, INPTR BSTR aErrorID, INPTR BSTR aMessage);
201 HRESULT onShowWindow (BOOL aCheck, BOOL *aCanShow, ULONG64 *aWinId);
202
203 static const PDMDRVREG DrvStatusReg;
204
205 void reportAuthLibraryError (const char *filename, int rc)
206 {
207 setError (E_FAIL, tr("Could not load the external authentication library '%s' (%Vrc)"), filename, rc);
208 }
209
210 // for VirtualBoxSupportErrorInfoImpl
211 static const wchar_t *getComponentName() { return L"Console"; }
212
213private:
214
215 /**
216 * Base template for AutoVMCaller and SaveVMPtr. Template arguments
217 * have the same meaning as arguments of Console::addVMCaller().
218 */
219 template <bool taQuiet = false, bool taAllowNullVM = false>
220 class AutoVMCallerBase
221 {
222 public:
223 AutoVMCallerBase (Console *aThat) : mThat (aThat), mRC (S_OK)
224 {
225 Assert (aThat);
226 mRC = aThat->addVMCaller (taQuiet, taAllowNullVM);
227 }
228 ~AutoVMCallerBase()
229 {
230 if (SUCCEEDED (mRC))
231 mThat->releaseVMCaller();
232 }
233 /** Decreases the number of callers before the instance is destroyed. */
234 void release()
235 {
236 AssertReturnVoid (SUCCEEDED (mRC));
237 mThat->releaseVMCaller();
238 mRC = E_FAIL;
239 }
240 /** Restores the number of callers after by #release(). #rc() must be
241 * rechecked to ensure the operation succeeded. */
242 void add()
243 {
244 AssertReturnVoid (!SUCCEEDED (mRC));
245 mRC = mThat->addVMCaller (taQuiet, taAllowNullVM);
246 }
247 /** Returns the result of Console::addVMCaller() */
248 HRESULT rc() const { return mRC; }
249 /** Shortcut to SUCCEEDED (rc()) */
250 bool isOk() const { return SUCCEEDED (mRC); }
251 protected:
252 Console *mThat;
253 HRESULT mRC;
254 private:
255 DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP (AutoVMCallerBase)
256 DECLARE_CLS_NEW_DELETE_NOOP (AutoVMCallerBase)
257 };
258
259 /**
260 * Helper class that protects sections of code using the mpVM pointer by
261 * automatically calling addVMCaller() on construction and
262 * releaseVMCaller() on destruction. Intended for Console methods dealing
263 * with mpVM. The usage pattern is:
264 * <code>
265 * AutoVMCaller autoVMCaller (this);
266 * CheckComRCReturnRC (autoVMCaller.rc());
267 * ...
268 * VMR3ReqCall (mpVM, ...
269 * </code>
270 *
271 * @sa SafeVMPtr, SafeVMPtrQuiet
272 */
273 typedef AutoVMCallerBase <false, false> AutoVMCaller;
274
275 /**
276 * Base template for SaveVMPtr and SaveVMPtrQuiet.
277 */
278 template <bool taQuiet = false>
279 class SafeVMPtrBase : public AutoVMCallerBase <taQuiet, true>
280 {
281 typedef AutoVMCallerBase <taQuiet, true> Base;
282 public:
283 SafeVMPtrBase (Console *aThat) : Base (aThat), mpVM (NULL)
284 {
285 if (SUCCEEDED (Base::mRC))
286 mpVM = aThat->mpVM;
287 }
288 /** Smart SaveVMPtr to PVM cast operator */
289 operator PVM() const { return mpVM; }
290 /** Direct PVM access for printf()-like functions */
291 PVM raw() const { return mpVM; }
292 private:
293 PVM mpVM;
294 DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP (SafeVMPtrBase)
295 DECLARE_CLS_NEW_DELETE_NOOP (SafeVMPtrBase)
296 };
297
298public:
299
300 /**
301 * Helper class that safely manages the Console::mpVM pointer
302 * by calling addVMCaller() on construction and releaseVMCaller() on
303 * destruction. Intended for Console children. The usage pattern is:
304 * <code>
305 * Console::SaveVMPtr pVM (mParent);
306 * CheckComRCReturnRC (pVM.rc());
307 * ...
308 * VMR3ReqCall (pVM, ...
309 * ...
310 * printf ("%p\n", pVM.raw());
311 * </code>
312 *
313 * @sa SafeVMPtrQuiet, AutoVMCaller
314 */
315 typedef SafeVMPtrBase <false> SafeVMPtr;
316
317 /**
318 * A deviation of SaveVMPtr that doesn't set the error info on failure.
319 * Intenede for pieces of code that don't need to return the VM access
320 * failure to the caller. The usage pattern is:
321 * <code>
322 * Console::SaveVMPtrQuiet pVM (mParent);
323 * if (pVM.rc())
324 * VMR3ReqCall (pVM, ...
325 * return S_OK;
326 * </code>
327 *
328 * @sa SafeVMPtr, AutoVMCaller
329 */
330 typedef SafeVMPtrBase <true> SafeVMPtrQuiet;
331
332private:
333
334 typedef std::list <ComObjPtr <OUSBDevice> > USBDeviceList;
335 typedef std::list <ComObjPtr <RemoteUSBDevice> > RemoteUSBDeviceList;
336 typedef std::list <ComObjPtr <SharedFolder> > SharedFolderList;
337
338 HRESULT addVMCaller (bool aQuiet = false, bool aAllowNullVM = false);
339 void releaseVMCaller();
340
341 HRESULT powerDown();
342
343 HRESULT callTapSetupApplication(bool isStatic, RTFILE tapFD, Bstr &tapDevice,
344 Bstr &tapSetupApplication);
345 HRESULT attachToHostInterface(INetworkAdapter *networkAdapter);
346 HRESULT detachFromHostInterface(INetworkAdapter *networkAdapter);
347 HRESULT powerDownHostInterfaces();
348
349 HRESULT setMachineState (MachineState_T aMachineState, bool aUpdateServer = true);
350 HRESULT setMachineStateLocally (MachineState_T aMachineState)
351 {
352 return setMachineState (aMachineState, false /* aUpdateServer */);
353 }
354
355 HRESULT findSharedFolder (const BSTR aName,
356 ComObjPtr <SharedFolder> &aSharedFolder,
357 bool aSetError = false);
358
359 static DECLCALLBACK(int) configConstructor(PVM pVM, void *pvConsole);
360 static DECLCALLBACK(void) vmstateChangeCallback(PVM aVM, VMSTATE aState,
361 VMSTATE aOldState, void *aUser);
362 HRESULT doDriveChange (const char *pszDevice, unsigned uInstance,
363 unsigned uLun, DriveState_T eState,
364 DriveState_T *peState, const char *pszPath,
365 bool fPassthrough);
366 static DECLCALLBACK(int) changeDrive (Console *pThis, const char *pszDevice,
367 unsigned uInstance, unsigned uLun,
368 DriveState_T eState, DriveState_T *peState,
369 const char *pszPath, bool fPassthrough);
370
371 HRESULT attachUSBDevice (IUSBDevice *aHostDevice, bool aManual,
372 PVUSBIRHCONFIG aConfig);
373
374 static DECLCALLBACK(int)
375 usbAttachCallback (Console *that, IUSBDevice *aHostDevice,
376 PVUSBIRHCONFIG aConfig, PCRTUUID aUuid, bool aRemote,
377 const char *aAddress, void *aRemoteBackend);
378 static DECLCALLBACK(int)
379 usbDetachCallback (Console *that, USBDeviceList::iterator *aIt,
380 bool aManual, PVUSBIRHCONFIG aConfig, PCRTUUID aUuid);
381
382 static DECLCALLBACK (int)
383 stateProgressCallback (PVM pVM, unsigned uPercent, void *pvUser);
384
385 static DECLCALLBACK(void)
386 setVMErrorCallback (PVM pVM, void *pvUser, int rc, RT_SRC_POS_DECL,
387 const char *pszFormat, va_list args);
388
389 static DECLCALLBACK(void)
390 setVMRuntimeErrorCallback (PVM pVM, void *pvUser, bool fFatal,
391 const char *pszErrorID,
392 const char *pszFormat, va_list args);
393
394 HRESULT captureUSBDevices (PVM pVM);
395 void releaseAllUSBDevices (void);
396
397 static DECLCALLBACK (int) powerUpThread (RTTHREAD Thread, void *pvUser);
398 static DECLCALLBACK (int) saveStateThread (RTTHREAD Thread, void *pvUser);
399 static DECLCALLBACK (int) powerDownThread (RTTHREAD Thread, void *pvUser);
400
401 static DECLCALLBACK(void *) drvStatus_QueryInterface(PPDMIBASE pInterface, PDMINTERFACE enmInterface);
402 static DECLCALLBACK(void) drvStatus_UnitChanged(PPDMILEDCONNECTORS pInterface, unsigned iLUN);
403 static DECLCALLBACK(void) drvStatus_Destruct(PPDMDRVINS pDrvIns);
404 static DECLCALLBACK(int) drvStatus_Construct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle);
405
406#ifdef VRDP_MC
407 int mcAudioRefs;
408 volatile uint32_t mcVRDPClients;
409
410 static DECLCALLBACK(int) vrdp_ClientLogon (void *pvUser, uint32_t u32ClientId, const char *pszUser, const char *pszPassword, const char *pszDomain);
411 static DECLCALLBACK(void) vrdp_ClientConnect (void *pvUser, uint32_t u32ClientId);
412 static DECLCALLBACK(void) vrdp_ClientDisconnect (void *pvUser, uint32_t u32ClientId, uint32_t fu32Intercepted);
413 static DECLCALLBACK(void) vrdp_InterceptAudio (void *pvUser, uint32_t u32ClientId);
414 static DECLCALLBACK(void) vrdp_InterceptUSB (void *pvUser, uint32_t u32ClientId, PFNVRDPUSBCALLBACK *ppfn, void **ppv);
415 static DECLCALLBACK(void) vrdp_InterceptClipboard (void *pvUser, uint32_t u32ClientId, PFNVRDPCLIPBOARDCALLBACK *ppfn, void **ppv);
416#else
417 static DECLCALLBACK(int) vrdp_ClientLogon (void *pvUser, const char *pszUser, const char *pszPassword, const char *pszDomain);
418 static DECLCALLBACK(void) vrdp_ClientConnect (void *pvUser, uint32_t fu32SupportedOrders);
419 static DECLCALLBACK(void) vrdp_ClientDisconnect (void *pvUser);
420 static DECLCALLBACK(void) vrdp_InterceptAudio (void *pvUser, bool keepHostAudio);
421 static DECLCALLBACK(void) vrdp_InterceptUSB (void *pvUser, PFNVRDPUSBCALLBACK *ppfn, void **ppv);
422 static DECLCALLBACK(void) vrdp_InterceptClipboard (void *pvUser, PFNVRDPCLIPBOARDCALLBACK *ppfn, void **ppv);
423#endif /* VRDP_MC */
424
425 static VRDPSERVERCALLBACK sVrdpServerCallback;
426
427 static const char *sSSMConsoleUnit;
428 static uint32_t sSSMConsoleVer;
429
430 HRESULT loadDataFromSavedState();
431
432 static DECLCALLBACK(void) saveStateFileExec (PSSMHANDLE pSSM, void *pvUser);
433 static DECLCALLBACK(int) loadStateFileExec (PSSMHANDLE pSSM, void *pvUser, uint32_t u32Version);
434
435 bool mSavedStateDataLoaded : 1;
436
437 const ComPtr <IMachine> mMachine;
438 const ComPtr <IInternalMachineControl> mControl;
439
440 const ComPtr <IVRDPServer> mVRDPServer;
441 const ComPtr <IDVDDrive> mDVDDrive;
442 const ComPtr <IFloppyDrive> mFloppyDrive;
443
444 ConsoleVRDPServer * const mConsoleVRDPServer;
445
446 const ComObjPtr <Guest> mGuest;
447 const ComObjPtr <Keyboard> mKeyboard;
448 const ComObjPtr <Mouse> mMouse;
449 const ComObjPtr <Display> mDisplay;
450 const ComObjPtr <MachineDebugger> mDebugger;
451 const ComObjPtr <RemoteDisplayInfo> mRemoteDisplayInfo;
452
453 USBDeviceList mUSBDevices;
454 RemoteUSBDeviceList mRemoteUSBDevices;
455 SharedFolderList mSharedFolders;
456
457 /** The VM instance handle. */
458 PVM mpVM;
459 /** Holds the number of "readonly" mpVM callers (users) */
460 uint32_t mVMCallers;
461 /** Semaphore posted when the number of mpVM callers drops to zero */
462 RTSEMEVENT mVMZeroCallersSem;
463 /** true when Console has entered the mpVM destruction phase */
464 bool mVMDestroying : 1;
465
466 /** The current DVD drive state in the VM.
467 * This does not have to match the state maintained in the DVD. */
468 DriveState_T meDVDState;
469 /** The current Floppy drive state in the VM.
470 * This does not have to match the state maintained in the Floppy. */
471 DriveState_T meFloppyState;
472
473 VMMDev * const mVMMDev;
474 AudioSniffer * const mAudioSniffer;
475
476 PPDMLED mapFDLeds[2];
477 PPDMLED mapIDELeds[4];
478 PPDMLED mapNetworkLeds[8];
479#ifdef VBOX_WITH_UNIXY_TAP_NETWORKING
480 Utf8Str maTAPDeviceName[8];
481 RTFILE maTapFD[8];
482#endif
483
484 bool mVMStateChangeCallbackDisabled;
485
486 /* Local machine state value */
487 MachineState_T mMachineState;
488
489 typedef std::list <ComPtr <IConsoleCallback> > CallbackList;
490 CallbackList mCallbacks;
491
492 struct
493 {
494 /** OnMousePointerShapeChange() cache */
495 struct
496 {
497 bool valid;
498 bool visible;
499 bool alpha;
500 uint32_t xHot;
501 uint32_t yHot;
502 uint32_t width;
503 uint32_t height;
504 BYTE *shape;
505 size_t shapeSize;
506 }
507 mpsc;
508
509 /** OnMouseCapabilityChange() cache */
510 struct
511 {
512 bool valid;
513 BOOL supportsAbsolute;
514 BOOL needsHostCursor;
515 }
516 mcc;
517
518 /** OnKeyboardLedsChange() cache */
519 struct
520 {
521 bool valid;
522 bool numLock;
523 bool capsLock;
524 bool scrollLock;
525 }
526 klc;
527 }
528 mCallbackData;
529
530 friend struct VMTask;
531};
532
533#endif // ____H_CONSOLEIMPL
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