VirtualBox

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

Last change on this file since 3719 was 3652, checked in by vboxsync, 18 years ago

Parallel port support. Contributed by: Alexander Eichner

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.8 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 onSerialPortChange(ISerialPort *serialPort);
175 HRESULT onParallelPortChange(IParallelPort *parallelPort);
176 HRESULT onVRDPServerChange();
177 HRESULT onUSBControllerChange();
178 HRESULT onUSBDeviceAttach (IUSBDevice *aDevice, IVirtualBoxErrorInfo *aError);
179 HRESULT onUSBDeviceDetach (INPTR GUIDPARAM aId, IVirtualBoxErrorInfo *aError);
180
181 VMMDev *getVMMDev() { return mVMMDev; }
182 AudioSniffer *getAudioSniffer () { return mAudioSniffer; }
183
184 static VRDPSERVERCALLBACK *getVrdpServerCallback () { return &sVrdpServerCallback; };
185
186 void processRemoteUSBDevices (uint32_t u32ClientId, VRDPUSBDEVICEDESC *pDevList, uint32_t cbDevList);
187
188 // callback callers
189 void onMousePointerShapeChange(bool fVisible, bool fAlpha,
190 uint32_t xHot, uint32_t yHot,
191 uint32_t width, uint32_t height,
192 void *pShape);
193 void onMouseCapabilityChange (BOOL supportsAbsolute, BOOL needsHostCursor);
194 void onStateChange (MachineState_T aMachineState);
195 void onAdditionsStateChange();
196 void onAdditionsOutdated();
197 void onKeyboardLedsChange (bool fNumLock, bool fCapsLock, bool fScrollLock);
198 void onUSBDeviceStateChange (IUSBDevice *aDevice, bool aAttached,
199 IVirtualBoxErrorInfo *aError);
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 * Same as AutoVMCaller but doesn't set extended error info on failure.
277 */
278 typedef AutoVMCallerBase <true, false> AutoVMCallerQuiet;
279
280 /**
281 * Base template for SaveVMPtr and SaveVMPtrQuiet.
282 */
283 template <bool taQuiet = false>
284 class SafeVMPtrBase : public AutoVMCallerBase <taQuiet, true>
285 {
286 typedef AutoVMCallerBase <taQuiet, true> Base;
287 public:
288 SafeVMPtrBase (Console *aThat) : Base (aThat), mpVM (NULL)
289 {
290 if (SUCCEEDED (Base::mRC))
291 mpVM = aThat->mpVM;
292 }
293 /** Smart SaveVMPtr to PVM cast operator */
294 operator PVM() const { return mpVM; }
295 /** Direct PVM access for printf()-like functions */
296 PVM raw() const { return mpVM; }
297 private:
298 PVM mpVM;
299 DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP (SafeVMPtrBase)
300 DECLARE_CLS_NEW_DELETE_NOOP (SafeVMPtrBase)
301 };
302
303public:
304
305 /**
306 * Helper class that safely manages the Console::mpVM pointer
307 * by calling addVMCaller() on construction and releaseVMCaller() on
308 * destruction. Intended for Console children. The usage pattern is:
309 * <code>
310 * Console::SaveVMPtr pVM (mParent);
311 * CheckComRCReturnRC (pVM.rc());
312 * ...
313 * VMR3ReqCall (pVM, ...
314 * ...
315 * printf ("%p\n", pVM.raw());
316 * </code>
317 *
318 * @sa SafeVMPtrQuiet, AutoVMCaller
319 */
320 typedef SafeVMPtrBase <false> SafeVMPtr;
321
322 /**
323 * A deviation of SaveVMPtr that doesn't set the error info on failure.
324 * Intenede for pieces of code that don't need to return the VM access
325 * failure to the caller. The usage pattern is:
326 * <code>
327 * Console::SaveVMPtrQuiet pVM (mParent);
328 * if (pVM.rc())
329 * VMR3ReqCall (pVM, ...
330 * return S_OK;
331 * </code>
332 *
333 * @sa SafeVMPtr, AutoVMCaller
334 */
335 typedef SafeVMPtrBase <true> SafeVMPtrQuiet;
336
337private:
338
339 typedef std::list <ComObjPtr <OUSBDevice> > USBDeviceList;
340 typedef std::list <ComObjPtr <RemoteUSBDevice> > RemoteUSBDeviceList;
341 typedef std::list <ComObjPtr <SharedFolder> > SharedFolderList;
342
343 HRESULT addVMCaller (bool aQuiet = false, bool aAllowNullVM = false);
344 void releaseVMCaller();
345
346 HRESULT powerDown();
347
348 HRESULT callTapSetupApplication(bool isStatic, RTFILE tapFD, Bstr &tapDevice,
349 Bstr &tapSetupApplication);
350 HRESULT attachToHostInterface(INetworkAdapter *networkAdapter);
351 HRESULT detachFromHostInterface(INetworkAdapter *networkAdapter);
352 HRESULT powerDownHostInterfaces();
353
354 HRESULT setMachineState (MachineState_T aMachineState, bool aUpdateServer = true);
355 HRESULT setMachineStateLocally (MachineState_T aMachineState)
356 {
357 return setMachineState (aMachineState, false /* aUpdateServer */);
358 }
359
360 HRESULT findSharedFolder (const BSTR aName,
361 ComObjPtr <SharedFolder> &aSharedFolder,
362 bool aSetError = false);
363
364 static DECLCALLBACK(int) configConstructor(PVM pVM, void *pvConsole);
365 static DECLCALLBACK(void) vmstateChangeCallback(PVM aVM, VMSTATE aState,
366 VMSTATE aOldState, void *aUser);
367 HRESULT doDriveChange (const char *pszDevice, unsigned uInstance,
368 unsigned uLun, DriveState_T eState,
369 DriveState_T *peState, const char *pszPath,
370 bool fPassthrough);
371 static DECLCALLBACK(int) changeDrive (Console *pThis, const char *pszDevice,
372 unsigned uInstance, unsigned uLun,
373 DriveState_T eState, DriveState_T *peState,
374 const char *pszPath, bool fPassthrough);
375
376 HRESULT attachUSBDevice (IUSBDevice *aHostDevice, PVUSBIRHCONFIG aConfig);
377 HRESULT detachUSBDevice (USBDeviceList::iterator &aIt);
378
379 static DECLCALLBACK(int)
380 usbAttachCallback (Console *that, IUSBDevice *aHostDevice,
381 PVUSBIRHCONFIG aConfig, PCRTUUID aUuid, bool aRemote,
382 const char *aAddress, void *aRemoteBackend);
383 static DECLCALLBACK(int)
384 usbDetachCallback (Console *that, USBDeviceList::iterator *aIt,
385 PVUSBIRHCONFIG aConfig, PCRTUUID aUuid);
386
387 static DECLCALLBACK (int)
388 stateProgressCallback (PVM pVM, unsigned uPercent, void *pvUser);
389
390 static DECLCALLBACK(void)
391 setVMErrorCallback (PVM pVM, void *pvUser, int rc, RT_SRC_POS_DECL,
392 const char *pszFormat, va_list args);
393
394 static DECLCALLBACK(void)
395 setVMRuntimeErrorCallback (PVM pVM, void *pvUser, bool fFatal,
396 const char *pszErrorID,
397 const char *pszFormat, va_list args);
398
399 HRESULT captureUSBDevices (PVM pVM);
400 void detachAllUSBDevices (bool aDone);
401
402 static DECLCALLBACK (int) powerUpThread (RTTHREAD Thread, void *pvUser);
403 static DECLCALLBACK (int) saveStateThread (RTTHREAD Thread, void *pvUser);
404 static DECLCALLBACK (int) powerDownThread (RTTHREAD Thread, void *pvUser);
405
406 static DECLCALLBACK(void *) drvStatus_QueryInterface(PPDMIBASE pInterface, PDMINTERFACE enmInterface);
407 static DECLCALLBACK(void) drvStatus_UnitChanged(PPDMILEDCONNECTORS pInterface, unsigned iLUN);
408 static DECLCALLBACK(void) drvStatus_Destruct(PPDMDRVINS pDrvIns);
409 static DECLCALLBACK(int) drvStatus_Construct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle);
410
411 int mcAudioRefs;
412 volatile uint32_t mcVRDPClients;
413
414 static DECLCALLBACK(int) vrdp_ClientLogon (void *pvUser, uint32_t u32ClientId, const char *pszUser, const char *pszPassword, const char *pszDomain);
415 static DECLCALLBACK(void) vrdp_ClientConnect (void *pvUser, uint32_t u32ClientId);
416 static DECLCALLBACK(void) vrdp_ClientDisconnect (void *pvUser, uint32_t u32ClientId, uint32_t fu32Intercepted);
417 static DECLCALLBACK(void) vrdp_InterceptAudio (void *pvUser, uint32_t u32ClientId);
418 static DECLCALLBACK(void) vrdp_InterceptUSB (void *pvUser, uint32_t u32ClientId, PFNVRDPUSBCALLBACK *ppfn, void **ppv);
419 static DECLCALLBACK(void) vrdp_InterceptClipboard (void *pvUser, uint32_t u32ClientId, PFNVRDPCLIPBOARDCALLBACK *ppfn, void **ppv);
420
421 static VRDPSERVERCALLBACK sVrdpServerCallback;
422
423 static const char *sSSMConsoleUnit;
424 static uint32_t sSSMConsoleVer;
425
426 HRESULT loadDataFromSavedState();
427
428 static DECLCALLBACK(void) saveStateFileExec (PSSMHANDLE pSSM, void *pvUser);
429 static DECLCALLBACK(int) loadStateFileExec (PSSMHANDLE pSSM, void *pvUser, uint32_t u32Version);
430
431 bool mSavedStateDataLoaded : 1;
432
433 const ComPtr <IMachine> mMachine;
434 const ComPtr <IInternalMachineControl> mControl;
435
436 const ComPtr <IVRDPServer> mVRDPServer;
437 const ComPtr <IDVDDrive> mDVDDrive;
438 const ComPtr <IFloppyDrive> mFloppyDrive;
439
440 ConsoleVRDPServer * const mConsoleVRDPServer;
441
442 const ComObjPtr <Guest> mGuest;
443 const ComObjPtr <Keyboard> mKeyboard;
444 const ComObjPtr <Mouse> mMouse;
445 const ComObjPtr <Display> mDisplay;
446 const ComObjPtr <MachineDebugger> mDebugger;
447 const ComObjPtr <RemoteDisplayInfo> mRemoteDisplayInfo;
448
449 USBDeviceList mUSBDevices;
450 RemoteUSBDeviceList mRemoteUSBDevices;
451 SharedFolderList mSharedFolders;
452
453 /** The VM instance handle. */
454 PVM mpVM;
455 /** Holds the number of "readonly" mpVM callers (users) */
456 uint32_t mVMCallers;
457 /** Semaphore posted when the number of mpVM callers drops to zero */
458 RTSEMEVENT mVMZeroCallersSem;
459 /** true when Console has entered the mpVM destruction phase */
460 bool mVMDestroying : 1;
461
462 /** The current DVD drive state in the VM.
463 * This does not have to match the state maintained in the DVD. */
464 DriveState_T meDVDState;
465 /** The current Floppy drive state in the VM.
466 * This does not have to match the state maintained in the Floppy. */
467 DriveState_T meFloppyState;
468
469 VMMDev * const mVMMDev;
470 AudioSniffer * const mAudioSniffer;
471
472 PPDMLED mapFDLeds[2];
473 PPDMLED mapIDELeds[4];
474 PPDMLED mapNetworkLeds[8];
475#ifdef VBOX_WITH_UNIXY_TAP_NETWORKING
476 Utf8Str maTAPDeviceName[8];
477 RTFILE maTapFD[8];
478#endif
479
480 bool mVMStateChangeCallbackDisabled;
481
482 /* Local machine state value */
483 MachineState_T mMachineState;
484
485 typedef std::list <ComPtr <IConsoleCallback> > CallbackList;
486 CallbackList mCallbacks;
487
488 struct
489 {
490 /** OnMousePointerShapeChange() cache */
491 struct
492 {
493 bool valid;
494 bool visible;
495 bool alpha;
496 uint32_t xHot;
497 uint32_t yHot;
498 uint32_t width;
499 uint32_t height;
500 BYTE *shape;
501 size_t shapeSize;
502 }
503 mpsc;
504
505 /** OnMouseCapabilityChange() cache */
506 struct
507 {
508 bool valid;
509 BOOL supportsAbsolute;
510 BOOL needsHostCursor;
511 }
512 mcc;
513
514 /** OnKeyboardLedsChange() cache */
515 struct
516 {
517 bool valid;
518 bool numLock;
519 bool capsLock;
520 bool scrollLock;
521 }
522 klc;
523 }
524 mCallbackData;
525
526 friend struct VMTask;
527};
528
529#endif // ____H_CONSOLEIMPL
Note: See TracBrowser for help on using the repository browser.

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