VirtualBox

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

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

The Giant CDDL Dual-License Header Change.

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