VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/runtime/UISession.cpp@ 98486

Last change on this file since 98486 was 98486, checked in by vboxsync, 2 years ago

FE/Qt: bugref:10322: Runtime UI: Reworking UIIndicatorsPool audio/video indicators to move COM related logic to UISession.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 30.5 KB
Line 
1/* $Id: UISession.cpp 98486 2023-02-07 11:02:29Z vboxsync $ */
2/** @file
3 * VBox Qt GUI - UISession class implementation.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28/* Qt includes: */
29#include <QApplication>
30#include <QWidget>
31#ifdef VBOX_WS_WIN
32# include <iprt/win/windows.h> /* Workaround for compile errors if included directly by QtWin. */
33# include <QtWin>
34#endif
35
36/* GUI includes: */
37#include "UIActionPoolRuntime.h"
38#include "UICommon.h"
39#include "UIConsoleEventHandler.h"
40#include "UIDetailsGenerator.h"
41#include "UIExtraDataManager.h"
42#include "UIFrameBuffer.h"
43#include "UIMachine.h"
44#include "UIMachineLogic.h"
45#include "UIMachineView.h"
46#include "UIMachineWindow.h"
47#include "UIMedium.h"
48#include "UIMessageCenter.h"
49#include "UIMousePointerShapeData.h"
50#include "UINotificationCenter.h"
51#include "UISession.h"
52#include "UISettingsDialogSpecific.h"
53#ifdef VBOX_GUI_WITH_KEYS_RESET_HANDLER
54# include "UIKeyboardHandler.h"
55# include <signal.h>
56#endif
57
58/* COM includes: */
59#include "CGraphicsAdapter.h"
60#include "CHostNetworkInterface.h"
61#include "CHostUSBDevice.h"
62#include "CMedium.h"
63#include "CMediumAttachment.h"
64#include "CSnapshot.h"
65#include "CStorageController.h"
66#include "CSystemProperties.h"
67#include "CUSBController.h"
68#include "CUSBDeviceFilter.h"
69#include "CUSBDeviceFilters.h"
70#ifdef VBOX_WITH_NETFLT
71# include "CNetworkAdapter.h"
72#endif
73
74/* External includes: */
75#ifdef VBOX_WS_X11
76# include <X11/Xlib.h>
77# include <X11/Xutil.h>
78#endif
79
80
81#ifdef VBOX_GUI_WITH_KEYS_RESET_HANDLER
82static void signalHandlerSIGUSR1(int sig, siginfo_t *, void *);
83#endif
84
85/* static */
86bool UISession::create(UISession *&pSession, UIMachine *pMachine)
87{
88 /* Make sure NULL pointer passed: */
89 AssertReturn(!pSession, false);
90
91 /* Create session UI: */
92 pSession = new UISession(pMachine);
93 AssertPtrReturn(pSession, false);
94
95 /* Make sure it's prepared: */
96 if (!pSession->prepare())
97 {
98 /* Destroy session UI otherwise: */
99 destroy(pSession);
100 /* False in that case: */
101 return false;
102 }
103
104 /* True by default: */
105 return true;
106}
107
108/* static */
109void UISession::destroy(UISession *&pSession)
110{
111 /* Make sure valid pointer passed: */
112 AssertPtrReturnVoid(pSession);
113
114 /* Delete session: */
115 delete pSession;
116 pSession = 0;
117}
118
119bool UISession::initialize()
120{
121 /* Preprocess initialization: */
122 if (!preprocessInitialization())
123 return false;
124
125 /* Notify user about mouse&keyboard auto-capturing: */
126 if (gEDataManager->autoCaptureEnabled())
127 UINotificationMessage::remindAboutAutoCapture();
128
129 m_enmMachineState = machine().GetState();
130
131 /* Apply debug settings from the command line. */
132 if (!debugger().isNull() && debugger().isOk())
133 {
134 if (uiCommon().areWeToExecuteAllInIem())
135 debugger().SetExecuteAllInIEM(true);
136 if (!uiCommon().isDefaultWarpPct())
137 debugger().SetVirtualTimeRate(uiCommon().getWarpPct());
138 }
139
140 /* Apply ad-hoc reconfigurations from the command line: */
141 if (uiCommon().hasFloppyImageToMount())
142 mountAdHocImage(KDeviceType_Floppy, UIMediumDeviceType_Floppy, uiCommon().getFloppyImage().toString());
143 if (uiCommon().hasDvdImageToMount())
144 mountAdHocImage(KDeviceType_DVD, UIMediumDeviceType_DVD, uiCommon().getDvdImage().toString());
145
146 /* Power UP if this is NOT separate process: */
147 if (!uiCommon().isSeparateProcess())
148 if (!powerUp())
149 return false;
150
151 /* Make sure all the pending Console events converted to signals
152 * during the powerUp() progress above reached their destinations.
153 * That is necessary to make sure all the pending machine state change events processed.
154 * We can't just use the machine state directly acquired from IMachine because there
155 * will be few places which are using stale machine state, not just this one. */
156 QApplication::sendPostedEvents(0, QEvent::MetaCall);
157
158 /* Check if we missed a really quick termination after successful startup: */
159 if (isTurnedOff())
160 {
161 LogRel(("GUI: Aborting startup due to invalid machine state detected: %d\n", machineState()));
162 return false;
163 }
164
165 /* Fetch corresponding states: */
166 if (uiCommon().isSeparateProcess())
167 {
168 sltAdditionsChange();
169 }
170 machineLogic()->initializePostPowerUp();
171
172#ifdef VBOX_GUI_WITH_PIDFILE
173 uiCommon().createPidfile();
174#endif /* VBOX_GUI_WITH_PIDFILE */
175
176 /* True by default: */
177 return true;
178}
179
180bool UISession::powerUp()
181{
182 /* Power UP machine: */
183 CProgress comProgress = uiCommon().shouldStartPaused() ? console().PowerUpPaused() : console().PowerUp();
184
185 /* Check for immediate failure: */
186 if (!console().isOk() || comProgress.isNull())
187 {
188 if (uiCommon().showStartVMErrors())
189 msgCenter().cannotStartMachine(console(), machineName());
190 LogRel(("GUI: Aborting startup due to power up issue detected...\n"));
191 return false;
192 }
193
194 /* Some logging right after we powered up: */
195 LogRel(("GUI: Qt version: %s\n", UICommon::qtRTVersionString().toUtf8().constData()));
196#ifdef VBOX_WS_X11
197 LogRel(("GUI: X11 Window Manager code: %d\n", (int)uiCommon().typeOfWindowManager()));
198#endif
199#if defined(VBOX_WS_MAC) || defined(VBOX_WS_WIN)
200 LogRel(("GUI: HID LEDs sync is %s\n", uimachine()->isHidLedsSyncEnabled() ? "enabled" : "disabled"));
201#else
202 LogRel(("GUI: HID LEDs sync is not supported on this platform\n"));
203#endif
204
205 /* Enable 'manual-override',
206 * preventing automatic Runtime UI closing
207 * and visual representation mode changes: */
208 uimachine()->setManualOverrideMode(true);
209
210 /* Show "Starting/Restoring" progress dialog: */
211 if (isSaved())
212 {
213 msgCenter().showModalProgressDialog(comProgress, machineName(), ":/progress_state_restore_90px.png", 0, 0);
214 /* After restoring from 'saved' state, machine-window(s) geometry should be adjusted: */
215 machineLogic()->adjustMachineWindowsGeometry();
216 }
217 else
218 {
219#ifdef VBOX_IS_QT6_OR_LATER /** @todo why is this any problem on qt6? */
220 msgCenter().showModalProgressDialog(comProgress, machineName(), ":/progress_start_90px.png", 0, 0);
221#else
222 msgCenter().showModalProgressDialog(comProgress, machineName(), ":/progress_start_90px.png");
223#endif
224 /* After VM start, machine-window(s) size-hint(s) should be sent: */
225 machineLogic()->sendMachineWindowsSizeHints();
226 }
227
228 /* Check for progress failure: */
229 if (!comProgress.isOk() || comProgress.GetResultCode() != 0)
230 {
231 if (uiCommon().showStartVMErrors())
232 msgCenter().cannotStartMachine(comProgress, machineName());
233 LogRel(("GUI: Aborting startup due to power up progress issue detected...\n"));
234 return false;
235 }
236
237 /* Disable 'manual-override' finally: */
238 uimachine()->setManualOverrideMode(false);
239
240 /* True by default: */
241 return true;
242}
243
244WId UISession::mainMachineWindowId() const
245{
246 return mainMachineWindow() ? mainMachineWindow()->winId() : 0;
247}
248
249bool UISession::setPause(bool fPause)
250{
251 if (fPause)
252 console().Pause();
253 else
254 console().Resume();
255
256 const bool fOk = console().isOk();
257 if (!fOk)
258 {
259 if (fPause)
260 UINotificationMessage::cannotPauseMachine(console());
261 else
262 UINotificationMessage::cannotResumeMachine(console());
263 }
264
265 return fOk;
266}
267
268bool UISession::guestAdditionsUpgradable()
269{
270 if (!machine().isOk())
271 return false;
272
273 /* Auto GA update is currently for Windows and Linux guests only */
274 const CGuestOSType osType = uiCommon().vmGuestOSType(machine().GetOSTypeId());
275 if (!osType.isOk())
276 return false;
277
278 const QString strGuestFamily = osType.GetFamilyId();
279 bool fIsWindowOrLinux = strGuestFamily.contains("windows", Qt::CaseInsensitive) || strGuestFamily.contains("linux", Qt::CaseInsensitive);
280
281 if (!fIsWindowOrLinux)
282 return false;
283
284 /* Also check whether we have something to update automatically: */
285 if (m_ulGuestAdditionsRunLevel < (ULONG)KAdditionsRunLevelType_Userland)
286 return false;
287
288 return true;
289}
290
291UIFrameBuffer *UISession::frameBuffer(ulong uScreenId) const
292{
293 Assert(uScreenId < (ulong)m_frameBufferVector.size());
294 return m_frameBufferVector.value((int)uScreenId, 0);
295}
296
297void UISession::setFrameBuffer(ulong uScreenId, UIFrameBuffer *pFrameBuffer)
298{
299 Assert(uScreenId < (ulong)m_frameBufferVector.size());
300 if (uScreenId < (ulong)m_frameBufferVector.size())
301 m_frameBufferVector[(int)uScreenId] = pFrameBuffer;
302}
303
304QSize UISession::frameBufferSize(ulong uScreenId) const
305{
306 UIFrameBuffer *pFramebuffer = frameBuffer(uScreenId);
307 return pFramebuffer ? QSize(pFramebuffer->width(), pFramebuffer->height()) : QSize();
308}
309
310void UISession::acquireDeviceActivity(const QVector<KDeviceType> &deviceTypes, QVector<KDeviceActivity> &states)
311{
312 CConsole comConsole = console();
313 states = comConsole.GetDeviceActivity(deviceTypes);
314 if (!comConsole.isOk())
315 UINotificationMessage::cannotAcquireConsoleParameter(comConsole);
316}
317
318void UISession::acquireHardDiskStatusInfo(QString &strInfo, bool &fAttachmentsPresent)
319{
320 CMachine comMachine = machine();
321 UIDetailsGenerator::acquireHardDiskStatusInfo(comMachine, strInfo, fAttachmentsPresent);
322}
323
324void UISession::acquireOpticalDiskStatusInfo(QString &strInfo, bool &fAttachmentsPresent, bool &fAttachmentsMounted)
325{
326 CMachine comMachine = machine();
327 UIDetailsGenerator::acquireOpticalDiskStatusInfo(comMachine, strInfo, fAttachmentsPresent, fAttachmentsMounted);
328}
329
330void UISession::acquireFloppyDiskStatusInfo(QString &strInfo, bool &fAttachmentsPresent, bool &fAttachmentsMounted)
331{
332 CMachine comMachine = machine();
333 UIDetailsGenerator::acquireFloppyDiskStatusInfo(comMachine, strInfo, fAttachmentsPresent, fAttachmentsMounted);
334}
335
336void UISession::acquireAudioStatusInfo(QString &strInfo, bool &fAudioEnabled, bool &fEnabledOutput, bool &fEnabledInput)
337{
338 CMachine comMachine = machine();
339 UIDetailsGenerator::acquireAudioStatusInfo(comMachine, strInfo, fAudioEnabled, fEnabledOutput, fEnabledInput);
340}
341
342void UISession::acquireDisplayStatusInfo(QString &strInfo, bool &fAcceleration3D)
343{
344 CMachine comMachine = machine();
345 UIDetailsGenerator::acquireDisplayStatusInfo(comMachine, strInfo, fAcceleration3D);
346}
347
348bool UISession::prepareToBeSaved()
349{
350 return isPaused()
351 || (isRunning() && pause());
352}
353
354bool UISession::prepareToBeShutdowned()
355{
356 const bool fValidMode = console().GetGuestEnteredACPIMode();
357 if (!fValidMode)
358 UINotificationMessage::cannotSendACPIToMachine();
359 return fValidMode;
360}
361
362void UISession::sltInstallGuestAdditionsFrom(const QString &strSource)
363{
364 if (!guestAdditionsUpgradable())
365 return sltMountDVDAdHoc(strSource);
366
367 /* Update guest additions automatically: */
368 UINotificationProgressGuestAdditionsInstall *pNotification =
369 new UINotificationProgressGuestAdditionsInstall(guest(), strSource);
370 connect(pNotification, &UINotificationProgressGuestAdditionsInstall::sigGuestAdditionsInstallationFailed,
371 this, &UISession::sltMountDVDAdHoc);
372 gpNotificationCenter->append(pNotification);
373}
374
375void UISession::sltMountDVDAdHoc(const QString &strSource)
376{
377 mountAdHocImage(KDeviceType_DVD, UIMediumDeviceType_DVD, strSource);
378}
379
380void UISession::sltDetachCOM()
381{
382 /* Cleanup everything COM related: */
383 cleanupFramebuffers();
384 cleanupConsoleEventHandlers();
385 cleanupNotificationCenter();
386 cleanupSession();
387}
388
389void UISession::sltStateChange(KMachineState enmState)
390{
391 /* Check if something had changed: */
392 if (m_enmMachineState != enmState)
393 {
394 /* Store new data: */
395 m_enmMachineStatePrevious = m_enmMachineState;
396 m_enmMachineState = enmState;
397
398 /* Notify listeners about machine state changed: */
399 emit sigMachineStateChange();
400 }
401}
402
403void UISession::sltAdditionsChange()
404{
405 /* Acquire actual states: */
406 const ULONG ulGuestAdditionsRunLevel = guest().GetAdditionsRunLevel();
407 LONG64 lLastUpdatedIgnored;
408 const bool fIsGuestSupportsGraphics = guest().GetFacilityStatus(KAdditionsFacilityType_Graphics, lLastUpdatedIgnored)
409 == KAdditionsFacilityStatus_Active;
410 const bool fIsGuestSupportsSeamless = guest().GetFacilityStatus(KAdditionsFacilityType_Seamless, lLastUpdatedIgnored)
411 == KAdditionsFacilityStatus_Active;
412
413 /* Check if something had changed: */
414 if ( m_ulGuestAdditionsRunLevel != ulGuestAdditionsRunLevel
415 || m_fIsGuestSupportsGraphics != fIsGuestSupportsGraphics
416 || m_fIsGuestSupportsSeamless != fIsGuestSupportsSeamless)
417 {
418 /* Store new data: */
419 m_ulGuestAdditionsRunLevel = ulGuestAdditionsRunLevel;
420 m_fIsGuestSupportsGraphics = fIsGuestSupportsGraphics;
421 m_fIsGuestSupportsSeamless = fIsGuestSupportsSeamless;
422
423 /* Notify listeners about GA state really changed: */
424 LogRel(("GUI: UISession::sltAdditionsChange: GA state really changed, notifying listeners.\n"));
425 emit sigAdditionsStateActualChange();
426 }
427 else
428 LogRel(("GUI: UISession::sltAdditionsChange: GA state doesn't really changed, still notifying listeners.\n"));
429
430 /* Notify listeners about GA state change event came: */
431 emit sigAdditionsStateChange();
432}
433
434UISession::UISession(UIMachine *pMachine)
435 : QObject(pMachine)
436 /* Base variables: */
437 , m_pMachine(pMachine)
438 , m_pConsoleEventhandler(0)
439 /* Common variables: */
440 , m_enmMachineStatePrevious(KMachineState_Null)
441 , m_enmMachineState(KMachineState_Null)
442 /* Guest additions flags: */
443 , m_ulGuestAdditionsRunLevel(0)
444 , m_fIsGuestSupportsGraphics(false)
445 , m_fIsGuestSupportsSeamless(false)
446{
447}
448
449UISession::~UISession()
450{
451}
452
453bool UISession::prepare()
454{
455 /* Prepare COM stuff: */
456 if (!prepareSession())
457 return false;
458
459 /* Cache media early if requested: */
460 if (uiCommon().agressiveCaching())
461 recacheMachineMedia();
462
463 /* Prepare GUI stuff: */
464 prepareNotificationCenter();
465 prepareConsoleEventHandlers();
466 prepareFramebuffers();
467 prepareConnections();
468 prepareSignalHandling();
469
470 /* True by default: */
471 return true;
472}
473
474bool UISession::prepareSession()
475{
476 /* Open session: */
477 m_comSession = uiCommon().openSession(uiCommon().managedVMUuid(),
478 uiCommon().isSeparateProcess()
479 ? KLockType_Shared
480 : KLockType_VM);
481 if (m_comSession.isNull())
482 return false;
483
484 /* Get machine: */
485 m_comMachine = m_comSession.GetMachine();
486 if (m_comMachine.isNull())
487 return false;
488
489 /* Get console: */
490 m_comConsole = m_comSession.GetConsole();
491 if (m_comConsole.isNull())
492 return false;
493
494 /* Get display: */
495 m_comDisplay = m_comConsole.GetDisplay();
496 if (m_comDisplay.isNull())
497 return false;
498
499 /* Get guest: */
500 m_comGuest = m_comConsole.GetGuest();
501 if (m_comGuest.isNull())
502 return false;
503
504 /* Get mouse: */
505 m_comMouse = m_comConsole.GetMouse();
506 if (m_comMouse.isNull())
507 return false;
508
509 /* Get keyboard: */
510 m_comKeyboard = m_comConsole.GetKeyboard();
511 if (m_comKeyboard.isNull())
512 return false;
513
514 /* Get debugger: */
515 m_comDebugger = m_comConsole.GetDebugger();
516 if (m_comDebugger.isNull())
517 return false;
518
519 /* Update machine-name: */
520 m_strMachineName = machine().GetName();
521
522 /* Update machine-state: */
523 m_enmMachineState = machine().GetState();
524
525 /* True by default: */
526 return true;
527}
528
529void UISession::prepareNotificationCenter()
530{
531 UINotificationCenter::create();
532}
533
534void UISession::prepareConsoleEventHandlers()
535{
536 /* Create console event-handler: */
537 m_pConsoleEventhandler = new UIConsoleEventHandler(this);
538
539 /* Console event connections: */
540 connect(m_pConsoleEventhandler, &UIConsoleEventHandler::sigAdditionsChange,
541 this, &UISession::sltAdditionsChange);
542 connect(m_pConsoleEventhandler, &UIConsoleEventHandler::sigAudioAdapterChange,
543 this, &UISession::sigAudioAdapterChange);
544 connect(m_pConsoleEventhandler, &UIConsoleEventHandler::sigClipboardModeChange,
545 this, &UISession::sigClipboardModeChange);
546 connect(m_pConsoleEventhandler, &UIConsoleEventHandler::sigCPUExecutionCapChange,
547 this, &UISession::sigCPUExecutionCapChange);
548 connect(m_pConsoleEventhandler, &UIConsoleEventHandler::sigDnDModeChange,
549 this, &UISession::sigDnDModeChange);
550 connect(m_pConsoleEventhandler, &UIConsoleEventHandler::sigGuestMonitorChange,
551 this, &UISession::sigGuestMonitorChange);
552 connect(m_pConsoleEventhandler, &UIConsoleEventHandler::sigMediumChange,
553 this, &UISession::sigMediumChange);
554 connect(m_pConsoleEventhandler, &UIConsoleEventHandler::sigNetworkAdapterChange,
555 this, &UISession::sigNetworkAdapterChange);
556 connect(m_pConsoleEventhandler, &UIConsoleEventHandler::sigRecordingChange,
557 this, &UISession::sigRecordingChange);
558 connect(m_pConsoleEventhandler, &UIConsoleEventHandler::sigSharedFolderChange,
559 this, &UISession::sigSharedFolderChange);
560 connect(m_pConsoleEventhandler, &UIConsoleEventHandler::sigStateChange,
561 this, &UISession::sltStateChange);
562 connect(m_pConsoleEventhandler, &UIConsoleEventHandler::sigStorageDeviceChange,
563 this, &UISession::sigStorageDeviceChange);
564 connect(m_pConsoleEventhandler, &UIConsoleEventHandler::sigUSBControllerChange,
565 this, &UISession::sigUSBControllerChange);
566 connect(m_pConsoleEventhandler, &UIConsoleEventHandler::sigUSBDeviceStateChange,
567 this, &UISession::sigUSBDeviceStateChange);
568 connect(m_pConsoleEventhandler, &UIConsoleEventHandler::sigVRDEChange,
569 this, &UISession::sigVRDEChange);
570 connect(m_pConsoleEventhandler, &UIConsoleEventHandler::sigRuntimeError,
571 this, &UISession::sigRuntimeError);
572
573#ifdef VBOX_WS_MAC
574 connect(m_pConsoleEventhandler, &UIConsoleEventHandler::sigShowWindow,
575 this, &UISession::sigShowWindows, Qt::QueuedConnection);
576#endif
577
578 /* Console keyboard connections: */
579 connect(m_pConsoleEventhandler, &UIConsoleEventHandler::sigKeyboardLedsChange,
580 this, &UISession::sigKeyboardLedsChange);
581
582 /* Console mouse connections: */
583 connect(m_pConsoleEventhandler, &UIConsoleEventHandler::sigMousePointerShapeChange,
584 this, &UISession::sigMousePointerShapeChange);
585 connect(m_pConsoleEventhandler, &UIConsoleEventHandler::sigMouseCapabilityChange,
586 this, &UISession::sigMouseCapabilityChange);
587 connect(m_pConsoleEventhandler, &UIConsoleEventHandler::sigCursorPositionChange,
588 this, &UISession::sigCursorPositionChange);
589}
590
591void UISession::prepareFramebuffers()
592{
593 /* Each framebuffer will be really prepared on first UIMachineView creation: */
594 m_frameBufferVector.resize(machine().GetGraphicsAdapter().GetMonitorCount());
595}
596
597void UISession::prepareConnections()
598{
599 /* UICommon connections: */
600 connect(&uiCommon(), &UICommon::sigAskToDetachCOM, this, &UISession::sltDetachCOM);
601}
602
603void UISession::prepareSignalHandling()
604{
605#ifdef VBOX_GUI_WITH_KEYS_RESET_HANDLER
606 struct sigaction sa;
607 sa.sa_sigaction = &signalHandlerSIGUSR1;
608 sigemptyset(&sa.sa_mask);
609 sa.sa_flags = SA_RESTART | SA_SIGINFO;
610 sigaction(SIGUSR1, &sa, NULL);
611#endif /* VBOX_GUI_WITH_KEYS_RESET_HANDLER */
612}
613
614void UISession::cleanupFramebuffers()
615{
616 /* Cleanup framebuffers finally: */
617 for (int i = m_frameBufferVector.size() - 1; i >= 0; --i)
618 {
619 UIFrameBuffer *pFrameBuffer = m_frameBufferVector[i];
620 if (pFrameBuffer)
621 {
622 /* Mark framebuffer as unused: */
623 pFrameBuffer->setMarkAsUnused(true);
624 /* Detach framebuffer from Display: */
625 pFrameBuffer->detach();
626 /* Delete framebuffer reference: */
627 delete pFrameBuffer;
628 }
629 }
630 m_frameBufferVector.clear();
631}
632
633void UISession::cleanupConsoleEventHandlers()
634{
635 /* Destroy console event-handler: */
636 delete m_pConsoleEventhandler;
637 m_pConsoleEventhandler = 0;
638}
639
640void UISession::cleanupNotificationCenter()
641{
642 UINotificationCenter::destroy();
643}
644
645void UISession::cleanupSession()
646{
647 /* Detach debugger: */
648 if (!m_comDebugger.isNull())
649 m_comDebugger.detach();
650
651 /* Detach keyboard: */
652 if (!m_comKeyboard.isNull())
653 m_comKeyboard.detach();
654
655 /* Detach mouse: */
656 if (!m_comMouse.isNull())
657 m_comMouse.detach();
658
659 /* Detach guest: */
660 if (!m_comGuest.isNull())
661 m_comGuest.detach();
662
663 /* Detach display: */
664 if (!m_comDisplay.isNull())
665 m_comDisplay.detach();
666
667 /* Detach console: */
668 if (!m_comConsole.isNull())
669 m_comConsole.detach();
670
671 /* Detach machine: */
672 if (!m_comMachine.isNull())
673 m_comMachine.detach();
674
675 /* Close session: */
676 if (!m_comSession.isNull() && uiCommon().isVBoxSVCAvailable())
677 {
678 m_comSession.UnlockMachine();
679 m_comSession.detach();
680 }
681}
682
683UIMachineLogic *UISession::machineLogic() const
684{
685 return uimachine() ? uimachine()->machineLogic() : 0;
686}
687
688UIMachineWindow *UISession::activeMachineWindow() const
689{
690 return machineLogic() ? machineLogic()->activeMachineWindow() : 0;
691}
692
693QWidget *UISession::mainMachineWindow() const
694{
695 return machineLogic() ? machineLogic()->mainMachineWindow() : 0;
696}
697
698bool UISession::preprocessInitialization()
699{
700#ifdef VBOX_WITH_NETFLT
701 /* Skip network interface name checks if VM in saved state: */
702 if (!isSaved())
703 {
704 /* Make sure all the attached and enabled network
705 * adapters are present on the host. This check makes sense
706 * in two cases only - when attachement type is Bridged Network
707 * or Host-only Interface. NOTE: Only currently enabled
708 * attachement type is checked (incorrect parameters check for
709 * currently disabled attachement types is skipped). */
710 QStringList failedInterfaceNames;
711 QStringList availableInterfaceNames;
712
713 /* Create host network interface names list: */
714 foreach (const CHostNetworkInterface &comNetIface, uiCommon().host().GetNetworkInterfaces())
715 {
716 availableInterfaceNames << comNetIface.GetName();
717 availableInterfaceNames << comNetIface.GetShortName();
718 }
719
720 /* Enumerate all the virtual network adapters: */
721 const ulong cCount = uiCommon().virtualBox().GetSystemProperties().GetMaxNetworkAdapters(machine().GetChipsetType());
722 for (ulong uAdapterIndex = 0; uAdapterIndex < cCount; ++uAdapterIndex)
723 {
724 CNetworkAdapter comNetworkAdapter = machine().GetNetworkAdapter(uAdapterIndex);
725 if (comNetworkAdapter.GetEnabled())
726 {
727 /* Get physical network interface name for
728 * currently enabled network attachement type: */
729 QString strInterfaceName;
730 switch (comNetworkAdapter.GetAttachmentType())
731 {
732 case KNetworkAttachmentType_Bridged:
733 strInterfaceName = comNetworkAdapter.GetBridgedInterface();
734 break;
735#ifndef VBOX_WITH_VMNET
736 case KNetworkAttachmentType_HostOnly:
737 strInterfaceName = comNetworkAdapter.GetHostOnlyInterface();
738 break;
739#endif /* !VBOX_WITH_VMNET */
740 default:
741 break;
742 }
743
744 if ( !strInterfaceName.isEmpty()
745 && !availableInterfaceNames.contains(strInterfaceName))
746 {
747 LogRel(("GUI: Invalid network interface found: %s\n", strInterfaceName.toUtf8().constData()));
748 failedInterfaceNames << QString("%1 (adapter %2)").arg(strInterfaceName).arg(uAdapterIndex + 1);
749 }
750 }
751 }
752
753 /* Check if non-existent interfaces found: */
754 if (!failedInterfaceNames.isEmpty())
755 {
756 if (msgCenter().warnAboutNetworkInterfaceNotFound(machineName(), failedInterfaceNames.join(", ")))
757 machineLogic()->openNetworkSettingsDialog();
758 else
759 {
760 LogRel(("GUI: Aborting startup due to preprocess initialization issue detected...\n"));
761 return false;
762 }
763 }
764 }
765#endif /* VBOX_WITH_NETFLT */
766
767 /* Check for USB enumeration warning. Don't return false even if we have a warning: */
768 CHost comHost = uiCommon().host();
769 if (comHost.GetUSBDevices().isEmpty() && comHost.isWarning())
770 {
771 /* Do not bitch if USB disabled: */
772 if (!machine().GetUSBControllers().isEmpty())
773 {
774 /* Do not bitch if there are no filters (check if enabled too?): */
775 if (!machine().GetUSBDeviceFilters().GetDeviceFilters().isEmpty())
776 UINotificationMessage::cannotEnumerateHostUSBDevices(comHost);
777 }
778 }
779
780 /* True by default: */
781 return true;
782}
783
784bool UISession::mountAdHocImage(KDeviceType enmDeviceType, UIMediumDeviceType enmMediumType, const QString &strMediumName)
785{
786 /* Get VBox: */
787 CVirtualBox comVBox = uiCommon().virtualBox();
788
789 /* Prepare medium to mount: */
790 UIMedium guiMedium;
791
792 /* The 'none' medium name means ejecting what ever is in the drive,
793 * in that case => leave the guiMedium variable null. */
794 if (strMediumName != "none")
795 {
796 /* Open the medium: */
797 const CMedium comMedium = comVBox.OpenMedium(strMediumName, enmDeviceType, KAccessMode_ReadWrite, false /* fForceNewUuid */);
798 if (!comVBox.isOk() || comMedium.isNull())
799 {
800 UINotificationMessage::cannotOpenMedium(comVBox, strMediumName);
801 return false;
802 }
803
804 /* Make sure medium ID is valid: */
805 const QUuid uMediumId = comMedium.GetId();
806 AssertReturn(!uMediumId.isNull(), false);
807
808 /* Try to find UIMedium among cached: */
809 guiMedium = uiCommon().medium(uMediumId);
810 if (guiMedium.isNull())
811 {
812 /* Cache new one if necessary: */
813 guiMedium = UIMedium(comMedium, enmMediumType, KMediumState_Created);
814 uiCommon().createMedium(guiMedium);
815 }
816 }
817
818 /* Search for a suitable storage slots: */
819 QList<ExactStorageSlot> aFreeStorageSlots;
820 QList<ExactStorageSlot> aBusyStorageSlots;
821 foreach (const CStorageController &comController, machine().GetStorageControllers())
822 {
823 foreach (const CMediumAttachment &comAttachment, machine().GetMediumAttachmentsOfController(comController.GetName()))
824 {
825 /* Look for an optical devices only: */
826 if (comAttachment.GetType() == enmDeviceType)
827 {
828 /* Append storage slot to corresponding list: */
829 if (comAttachment.GetMedium().isNull())
830 aFreeStorageSlots << ExactStorageSlot(comController.GetName(), comController.GetBus(),
831 comAttachment.GetPort(), comAttachment.GetDevice());
832 else
833 aBusyStorageSlots << ExactStorageSlot(comController.GetName(), comController.GetBus(),
834 comAttachment.GetPort(), comAttachment.GetDevice());
835 }
836 }
837 }
838
839 /* Make sure at least one storage slot found: */
840 QList<ExactStorageSlot> sStorageSlots = aFreeStorageSlots + aBusyStorageSlots;
841 if (sStorageSlots.isEmpty())
842 {
843 UINotificationMessage::cannotMountImage(machineName(), strMediumName);
844 return false;
845 }
846
847 /* Try to mount medium into first available storage slot: */
848 while (!sStorageSlots.isEmpty())
849 {
850 const ExactStorageSlot storageSlot = sStorageSlots.takeFirst();
851 machine().MountMedium(storageSlot.controller, storageSlot.port, storageSlot.device, guiMedium.medium(), false /* force */);
852 if (machine().isOk())
853 break;
854 }
855
856 /* Show error message if necessary: */
857 if (!machine().isOk())
858 {
859 msgCenter().cannotRemountMedium(machine(), guiMedium, true /* mount? */, false /* retry? */, activeMachineWindow());
860 return false;
861 }
862
863 /* Save machine settings: */
864 machine().SaveSettings();
865
866 /* Show error message if necessary: */
867 if (!machine().isOk())
868 {
869 UINotificationMessage::cannotSaveMachineSettings(machine());
870 return false;
871 }
872
873 /* True by default: */
874 return true;
875}
876
877void UISession::recacheMachineMedia()
878{
879 /* Compose a list of machine media: */
880 CMediumVector comMedia;
881
882 /* Enumerate all the controllers: */
883 foreach (const CStorageController &comController, machine().GetStorageControllers())
884 {
885 /* Enumerate all the attachments: */
886 foreach (const CMediumAttachment &comAttachment, machine().GetMediumAttachmentsOfController(comController.GetName()))
887 {
888 /* Skip unrelated device types: */
889 const KDeviceType enmDeviceType = comAttachment.GetType();
890 if ( enmDeviceType != KDeviceType_HardDisk
891 && enmDeviceType != KDeviceType_Floppy
892 && enmDeviceType != KDeviceType_DVD)
893 continue;
894 if ( comAttachment.GetIsEjected()
895 || comAttachment.GetMedium().isNull())
896 continue;
897 comMedia.append(comAttachment.GetMedium());
898 }
899 }
900
901 /* Start media enumeration: */
902 uiCommon().enumerateMedia(comMedia);
903}
904
905#ifdef VBOX_GUI_WITH_KEYS_RESET_HANDLER
906/**
907 * Custom signal handler. When switching VTs, we might not get release events
908 * for Ctrl-Alt and in case a savestate is performed on the new VT, the VM will
909 * be saved with modifier keys stuck. This is annoying enough for introducing
910 * this hack.
911 */
912/* static */
913static void signalHandlerSIGUSR1(int sig, siginfo_t * /* pInfo */, void * /*pSecret */)
914{
915 /* Only SIGUSR1 is interesting: */
916 if (sig == SIGUSR1)
917 if (gpMachine)
918 gpMachine->machineLogic()->keyboardHandler()->releaseAllPressedKeys();
919}
920#endif /* VBOX_GUI_WITH_KEYS_RESET_HANDLER */
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