VirtualBox

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

Last change on this file since 255 was 235, checked in by vboxsync, 18 years ago

Main: Implemented runtime error notifications in IConsole (VMSetRuntimeError and friends).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.6 KB
Line 
1/** @file
2 *
3 * VBox Console COM Class definition
4 */
5
6/*
7 * Copyright (C) 2006 InnoTek Systemberatung 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 USBDevice;
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// Console
70///////////////////////////////////////////////////////////////////////////////
71
72/** IConsole implementation class */
73class ATL_NO_VTABLE Console :
74 public VirtualBoxBaseWithChildrenNEXT,
75 public VirtualBoxSupportErrorInfoImpl <Console, IConsole>,
76 public VirtualBoxSupportTranslation <Console>,
77 public IConsole
78{
79 Q_OBJECT
80
81public:
82
83 DECLARE_NOT_AGGREGATABLE(Console)
84
85 DECLARE_PROTECT_FINAL_CONSTRUCT()
86
87 BEGIN_COM_MAP(Console)
88 COM_INTERFACE_ENTRY(ISupportErrorInfo)
89 COM_INTERFACE_ENTRY(IConsole)
90 END_COM_MAP()
91
92 NS_DECL_ISUPPORTS
93
94 Console();
95 ~Console();
96
97 HRESULT FinalConstruct();
98 void FinalRelease();
99
100 // public initializers/uninitializers for internal purposes only
101 HRESULT init (IMachine *aMachine, IInternalMachineControl *aControl);
102 void uninit();
103
104 // IConsole properties
105 STDMETHOD(COMGETTER(Machine)) (IMachine **aMachine);
106 STDMETHOD(COMGETTER(State)) (MachineState_T *aMachineState);
107 STDMETHOD(COMGETTER(Guest)) (IGuest **aGuest);
108 STDMETHOD(COMGETTER(Keyboard)) (IKeyboard **aKeyboard);
109 STDMETHOD(COMGETTER(Mouse)) (IMouse **aMouse);
110 STDMETHOD(COMGETTER(Display)) (IDisplay **aDisplay);
111 STDMETHOD(COMGETTER(Debugger)) (IMachineDebugger **aDebugger);
112 STDMETHOD(COMGETTER(USBDevices)) (IUSBDeviceCollection **aUSBDevices);
113 STDMETHOD(COMGETTER(RemoteUSBDevices)) (IHostUSBDeviceCollection **aRemoteUSBDevices);
114 STDMETHOD(COMGETTER(RemoteDisplayInfo)) (IRemoteDisplayInfo **aRemoteDisplayInfo);
115 STDMETHOD(COMGETTER(SharedFolders)) (ISharedFolderCollection **aSharedFolders);
116
117 // IConsole methods
118 STDMETHOD(PowerUp) (IProgress **aProgress);
119 STDMETHOD(PowerDown)();
120 STDMETHOD(Reset)();
121 STDMETHOD(Pause)();
122 STDMETHOD(Resume)();
123 STDMETHOD(PowerButton)();
124 STDMETHOD(SaveState) (IProgress **aProgress);
125 STDMETHOD(DiscardSavedState)();
126 STDMETHOD(GetDeviceActivity) (DeviceType_T aDeviceType,
127 DeviceActivity_T *aDeviceActivity);
128 STDMETHOD(AttachUSBDevice) (INPTR GUIDPARAM aId);
129 STDMETHOD(DetachUSBDevice) (INPTR GUIDPARAM aId, IUSBDevice **aDevice);
130 STDMETHOD(CreateSharedFolder) (INPTR BSTR aName, INPTR BSTR aHostPath);
131 STDMETHOD(RemoveSharedFolder) (INPTR BSTR aName);
132 STDMETHOD(TakeSnapshot) (INPTR BSTR aName, INPTR BSTR aDescription,
133 IProgress **aProgress);
134 STDMETHOD(DiscardSnapshot) (INPTR GUIDPARAM aId, IProgress **aProgress);
135 STDMETHOD(DiscardCurrentState) (IProgress **aProgress);
136 STDMETHOD(DiscardCurrentSnapshotAndState) (IProgress **aProgress);
137 STDMETHOD(RegisterCallback) (IConsoleCallback *aCallback);
138 STDMETHOD(UnregisterCallback)(IConsoleCallback *aCallback);
139
140 // public methods for internal purposes only
141
142 /*
143 * Note: the following methods do not increase refcount. intended to be
144 * called only by the VM execution thread.
145 */
146
147 Guest *getGuest() { return mGuest; }
148 Keyboard *getKeyboard() { return mKeyboard; }
149 Mouse *getMouse() { return mMouse; }
150 Display *getDisplay() { return mDisplay; }
151 MachineDebugger *getMachineDebugger() { return mDebugger; }
152
153 const ComPtr <IMachine> &machine() { return mMachine; }
154
155 /** Method is called only from ConsoleVRDPServer */
156 IVRDPServer *getVRDPServer() { return mVRDPServer; }
157
158 ConsoleVRDPServer *consoleVRDPServer() { return mConsoleVRDPServer; }
159
160 HRESULT updateMachineState (MachineState_T aMachineState);
161
162 // events from IInternalSessionControl
163 HRESULT onDVDDriveChange();
164 HRESULT onFloppyDriveChange();
165 HRESULT onNetworkAdapterChange(INetworkAdapter *networkAdapter);
166 HRESULT onVRDPServerChange();
167 HRESULT onUSBControllerChange();
168 HRESULT onUSBDeviceAttach(IUSBDevice *aDevice);
169 HRESULT onUSBDeviceDetach(INPTR GUIDPARAM aId);
170
171 VMMDev *getVMMDev() { return mVMMDev; }
172 AudioSniffer *getAudioSniffer () { return mAudioSniffer; }
173
174 static VRDPSERVERCALLBACK *getVrdpServerCallback () { return &sVrdpServerCallback; };
175
176#ifdef VRDP_MC
177 void processRemoteUSBDevices (uint32_t u32ClientId, VRDPUSBDEVICEDESC *pDevList, uint32_t cbDevList);
178#else
179 void processRemoteUSBDevices (VRDPUSBDEVICEDESC *pDevList, uint32_t cbDevList);
180#endif /* VRDP_MC */
181
182 // callback callers
183 void onMousePointerShapeChange(bool fVisible, bool fAlpha,
184 uint32_t xHot, uint32_t yHot,
185 uint32_t width, uint32_t height,
186 void *pShape);
187 void onMouseCapabilityChange (BOOL supportsAbsolute, BOOL needsHostCursor);
188 void onStateChange (MachineState_T aMachineState);
189 void onAdditionsStateChange();
190 void onAdditionsOutdated();
191 void onKeyboardLedsChange (bool fNumLock, bool fCapsLock, bool fScrollLock);
192 void onRuntimeError (BOOL aFatal, INPTR BSTR aErrorID, INPTR BSTR aMessage);
193
194 static const PDMDRVREG DrvStatusReg;
195
196 void reportAuthLibraryError (const char *filename, int rc)
197 {
198 setError (E_FAIL, tr("Could not load the external authentication library '%s' (%Vrc)"), filename, rc);
199 }
200
201 // for VirtualBoxSupportErrorInfoImpl
202 static const wchar_t *getComponentName() { return L"Console"; }
203
204private:
205
206 /**
207 * Base template for AutoVMCaller and SaveVMPtr. Template arguments
208 * have the same meaning as arguments of Console::addVMCaller().
209 */
210 template <bool taQuiet = false, bool taAllowNullVM = false>
211 class AutoVMCallerBase
212 {
213 public:
214 AutoVMCallerBase (Console *aThat) : mThat (aThat), mRC (S_OK)
215 {
216 Assert (aThat);
217 mRC = aThat->addVMCaller (taQuiet, taAllowNullVM);
218 }
219 ~AutoVMCallerBase()
220 {
221 if (SUCCEEDED (mRC))
222 mThat->releaseVMCaller();
223 }
224 /** Decreases the number of callers before the instance is destroyed. */
225 void release()
226 {
227 AssertReturnVoid (SUCCEEDED (mRC));
228 mThat->releaseVMCaller();
229 mRC = E_FAIL;
230 }
231 /** Restores the number of callers after by #release(). #rc() must be
232 * rechecked to ensure the operation succeeded. */
233 void add()
234 {
235 AssertReturnVoid (!SUCCEEDED (mRC));
236 mRC = mThat->addVMCaller (taQuiet, taAllowNullVM);
237 }
238 /** Returns the result of Console::addVMCaller() */
239 HRESULT rc() const { return mRC; }
240 /** Shortcut to SUCCEEDED (rc()) */
241 bool isOk() const { return SUCCEEDED (mRC); }
242 protected:
243 Console *mThat;
244 HRESULT mRC;
245 private:
246 DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP (AutoVMCallerBase)
247 DECLARE_CLS_NEW_DELETE_NOOP (AutoVMCallerBase)
248 };
249
250 /**
251 * Helper class that protects sections of code using the mpVM pointer by
252 * automatically calling addVMCaller() on construction and
253 * releaseVMCaller() on destruction. Intended for Console methods dealing
254 * with mpVM. The usage pattern is:
255 * <code>
256 * AutoVMCaller autoVMCaller (this);
257 * CheckComRCReturnRC (autoVMCaller.rc());
258 * ...
259 * VMR3ReqCall (mpVM, ...
260 * </code>
261 *
262 * @sa SafeVMPtr, SafeVMPtrQuiet
263 */
264 typedef AutoVMCallerBase <false, false> AutoVMCaller;
265
266 /**
267 * Base template for SaveVMPtr and SaveVMPtrQuiet.
268 */
269 template <bool taQuiet = false>
270 class SafeVMPtrBase : public AutoVMCallerBase <taQuiet, true>
271 {
272 typedef AutoVMCallerBase <taQuiet, true> Base;
273 public:
274 SafeVMPtrBase (Console *aThat) : Base (aThat), mpVM (NULL)
275 {
276 if (SUCCEEDED (Base::mRC))
277 mpVM = aThat->mpVM;
278 }
279 /** Smart SaveVMPtr to PVM cast operator */
280 operator PVM() const { return mpVM; }
281 /** Direct PVM access for printf()-like functions */
282 PVM raw() const { return mpVM; }
283 private:
284 PVM mpVM;
285 DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP (SafeVMPtrBase)
286 DECLARE_CLS_NEW_DELETE_NOOP (SafeVMPtrBase)
287 };
288
289public:
290
291 /**
292 * Helper class that safely manages the Console::mpVM pointer
293 * by calling addVMCaller() on construction and releaseVMCaller() on
294 * destruction. Intended for Console children. The usage pattern is:
295 * <code>
296 * Console::SaveVMPtr pVM (mParent);
297 * CheckComRCReturnRC (pVM.rc());
298 * ...
299 * VMR3ReqCall (pVM, ...
300 * ...
301 * printf ("%p\n", pVM.raw());
302 * </code>
303 *
304 * @sa SafeVMPtrQuiet, AutoVMCaller
305 */
306 typedef SafeVMPtrBase <false> SafeVMPtr;
307
308 /**
309 * A deviation of SaveVMPtr that doesn't set the error info on failure.
310 * Intenede for pieces of code that don't need to return the VM access
311 * failure to the caller. The usage pattern is:
312 * <code>
313 * Console::SaveVMPtrQuiet pVM (mParent);
314 * if (pVM.rc())
315 * VMR3ReqCall (pVM, ...
316 * return S_OK;
317 * </code>
318 *
319 * @sa SafeVMPtr, AutoVMCaller
320 */
321 typedef SafeVMPtrBase <true> SafeVMPtrQuiet;
322
323private:
324
325 typedef std::list <ComObjPtr <USBDevice> > USBDeviceList;
326 typedef std::list <ComObjPtr <RemoteUSBDevice> > RemoteUSBDeviceList;
327 typedef std::list <ComObjPtr <SharedFolder> > SharedFolderList;
328
329 HRESULT addVMCaller (bool aQuiet = false, bool aAllowNullVM = false);
330 void releaseVMCaller();
331
332 HRESULT powerDown();
333
334 HRESULT attachToHostInterface(INetworkAdapter *networkAdapter);
335 HRESULT detachFromHostInterface(INetworkAdapter *networkAdapter);
336 HRESULT powerDownHostInterfaces();
337
338 HRESULT setMachineState (MachineState_T aMachineState, bool aUpdateServer = true);
339 HRESULT setMachineStateLocally (MachineState_T aMachineState)
340 {
341 return setMachineState (aMachineState, false /* aUpdateServer */);
342 }
343
344 HRESULT findSharedFolder (const BSTR aName,
345 ComObjPtr <SharedFolder> &aSharedFolder,
346 bool aSetError = false);
347
348 static DECLCALLBACK(int) configConstructor(PVM pVM, void *pvConsole);
349 static DECLCALLBACK(void) vmstateChangeCallback(PVM aVM, VMSTATE aState,
350 VMSTATE aOldState, void *aUser);
351 HRESULT doDriveChange (const char *pszDevice, unsigned uInstance,
352 unsigned uLun, DriveState_T eState,
353 DriveState_T *peState, const char *pszPath,
354 bool fPassthrough);
355 static DECLCALLBACK(int) changeDrive (Console *pThis, const char *pszDevice,
356 unsigned uInstance, unsigned uLun,
357 DriveState_T eState, DriveState_T *peState,
358 const char *pszPath, bool fPassthrough);
359
360 HRESULT attachUSBDevice (IUSBDevice *aHostDevice, bool aManual,
361 PVUSBIRHCONFIG aConfig);
362
363 static DECLCALLBACK(int)
364 usbAttachCallback (Console *that, IUSBDevice *aHostDevice,
365 PVUSBIRHCONFIG aConfig, PCRTUUID aUuid, bool aRemote,
366 const char *aAddress, void *aRemoteBackend);
367 static DECLCALLBACK(int)
368 usbDetachCallback (Console *that, USBDeviceList::iterator *aIt,
369 bool aManual, PVUSBIRHCONFIG aConfig, PCRTUUID aUuid);
370
371 static DECLCALLBACK (int)
372 stateProgressCallback (PVM pVM, unsigned uPercent, void *pvUser);
373
374 static DECLCALLBACK(void)
375 setVMErrorCallback (PVM pVM, void *pvUser, int rc, RT_SRC_POS_DECL,
376 const char *pszFormat, va_list args);
377
378 static DECLCALLBACK(void)
379 setVMRuntimeErrorCallback (PVM pVM, void *pvUser, bool fFatal,
380 const char *pszErrorID,
381 const char *pszFormat, va_list args);
382
383 HRESULT captureUSBDevices (PVM pVM);
384 void releaseAllUSBDevices (void);
385
386 static DECLCALLBACK (int) powerUpThread (RTTHREAD Thread, void *pvUser);
387 static DECLCALLBACK (int) saveStateThread (RTTHREAD Thread, void *pvUser);
388 static DECLCALLBACK (int) powerDownThread (RTTHREAD Thread, void *pvUser);
389
390 static DECLCALLBACK(void *) drvStatus_QueryInterface(PPDMIBASE pInterface, PDMINTERFACE enmInterface);
391 static DECLCALLBACK(void) drvStatus_UnitChanged(PPDMILEDCONNECTORS pInterface, unsigned iLUN);
392 static DECLCALLBACK(void) drvStatus_Destruct(PPDMDRVINS pDrvIns);
393 static DECLCALLBACK(int) drvStatus_Construct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle);
394
395#ifdef VRDP_MC
396 int m_cAudioRefs;
397
398 static DECLCALLBACK(int) vrdp_ClientLogon (void *pvUser, uint32_t u32ClientId, const char *pszUser, const char *pszPassword, const char *pszDomain);
399 static DECLCALLBACK(void) vrdp_ClientConnect (void *pvUser, uint32_t u32ClientId);
400 static DECLCALLBACK(void) vrdp_ClientDisconnect (void *pvUser, uint32_t u32ClientId, uint32_t fu32Intercepted);
401 static DECLCALLBACK(void) vrdp_InterceptAudio (void *pvUser, uint32_t u32ClientId);
402 static DECLCALLBACK(void) vrdp_InterceptUSB (void *pvUser, uint32_t u32ClientId, PFNVRDPUSBCALLBACK *ppfn, void **ppv);
403#else
404 static DECLCALLBACK(int) vrdp_ClientLogon (void *pvUser, const char *pszUser, const char *pszPassword, const char *pszDomain);
405 static DECLCALLBACK(void) vrdp_ClientConnect (void *pvUser, uint32_t fu32SupportedOrders);
406 static DECLCALLBACK(void) vrdp_ClientDisconnect (void *pvUser);
407 static DECLCALLBACK(void) vrdp_InterceptAudio (void *pvUser, bool keepHostAudio);
408 static DECLCALLBACK(void) vrdp_InterceptUSB (void *pvUser, PFNVRDPUSBCALLBACK *ppfn, void **ppv);
409#endif /* VRDP_MC */
410
411 static VRDPSERVERCALLBACK sVrdpServerCallback;
412
413 static char *sSSMConsoleUnit;
414 static uint32_t sSSMConsoleVer;
415
416 HRESULT loadDataFromSavedState();
417
418 static DECLCALLBACK(void) saveStateFileExec (PSSMHANDLE pSSM, void *pvUser);
419 static DECLCALLBACK(int) loadStateFileExec (PSSMHANDLE pSSM, void *pvUser, uint32_t u32Version);
420
421 bool mSavedStateDataLoaded : 1;
422
423 const ComPtr <IMachine> mMachine;
424 const ComPtr <IInternalMachineControl> mControl;
425
426 const ComPtr <IVRDPServer> mVRDPServer;
427 const ComPtr <IDVDDrive> mDVDDrive;
428 const ComPtr <IFloppyDrive> mFloppyDrive;
429
430 ConsoleVRDPServer * const mConsoleVRDPServer;
431
432 const ComObjPtr <Guest> mGuest;
433 const ComObjPtr <Keyboard> mKeyboard;
434 const ComObjPtr <Mouse> mMouse;
435 const ComObjPtr <Display> mDisplay;
436 const ComObjPtr <MachineDebugger> mDebugger;
437 const ComObjPtr <RemoteDisplayInfo> mRemoteDisplayInfo;
438
439 USBDeviceList mUSBDevices;
440 RemoteUSBDeviceList mRemoteUSBDevices;
441 SharedFolderList mSharedFolders;
442
443 /** The VM instance handle. */
444 PVM mpVM;
445 /** Holds the number of "readonly" mpVM callers (users) */
446 uint32_t mVMCallers;
447 /** Semaphore posted when the number of mpVM callers drops to zero */
448 RTSEMEVENT mVMZeroCallersSem;
449 /** true when Console has entered the mpVM destruction phase */
450 bool mVMDestroying : 1;
451
452 /** The current DVD drive state in the VM.
453 * This does not have to match the state maintained in the DVD. */
454 DriveState_T meDVDState;
455 /** The current Floppy drive state in the VM.
456 * This does not have to match the state maintained in the Floppy. */
457 DriveState_T meFloppyState;
458
459 VMMDev * const mVMMDev;
460 AudioSniffer * const mAudioSniffer;
461
462 PPDMLED mapFDLeds[2];
463 PPDMLED mapIDELeds[4];
464 PPDMLED mapNetworkLeds[8];
465#ifdef __LINUX__
466 Utf8Str maTAPDeviceName[8];
467 RTFILE maTapFD[8];
468#endif
469
470 bool mVMStateChangeCallbackDisabled;
471
472 // local state value
473 MachineState_T mMachineState;
474
475 typedef std::list <ComPtr <IConsoleCallback> > CallbackList;
476 CallbackList mCallbacks;
477
478 friend struct VMTask;
479};
480
481#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