[26709] | 1 | /* $Id: UIMachineViewNormal.cpp 103538 2024-02-22 17:06:26Z vboxsync $ */
|
---|
[382] | 2 | /** @file
|
---|
[52727] | 3 | * VBox Qt GUI - UIMachineViewNormal class implementation.
|
---|
[382] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2010-2023 Oracle and/or its affiliates.
|
---|
[382] | 8 | *
|
---|
[96407] | 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
|
---|
[382] | 26 | */
|
---|
| 27 |
|
---|
[53909] | 28 | /* Qt includes: */
|
---|
[76606] | 29 | #include <QApplication>
|
---|
| 30 | #include <QMainWindow>
|
---|
| 31 | #include <QMenuBar>
|
---|
| 32 | #include <QScrollBar>
|
---|
| 33 | #include <QTimer>
|
---|
[19817] | 34 |
|
---|
[53909] | 35 | /* GUI includes: */
|
---|
[76606] | 36 | #include "UIActionPoolRuntime.h"
|
---|
[98385] | 37 | #include "UIDesktopWidgetWatchdog.h"
|
---|
| 38 | #include "UIExtraDataManager.h"
|
---|
| 39 | #include "UIFrameBuffer.h"
|
---|
[103538] | 40 | #include "UILoggingDefs.h"
|
---|
[98385] | 41 | #include "UIMachine.h"
|
---|
[76606] | 42 | #include "UIMachineLogic.h"
|
---|
[98385] | 43 | #include "UIMachineViewNormal.h"
|
---|
[76606] | 44 | #include "UIMachineWindow.h"
|
---|
[382] | 45 |
|
---|
[63054] | 46 |
|
---|
[84790] | 47 | UIMachineViewNormal::UIMachineViewNormal(UIMachineWindow *pMachineWindow, ulong uScreenId)
|
---|
| 48 | : UIMachineView(pMachineWindow, uScreenId)
|
---|
[97975] | 49 | , m_fGuestAutoresizeEnabled(actionPool()->action(UIActionIndexRT_M_View_T_GuestAutoresize)->isChecked())
|
---|
[382] | 50 | {
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[27236] | 53 | void UIMachineViewNormal::sltAdditionsStateChanged()
|
---|
| 54 | {
|
---|
[52705] | 55 | adjustGuestScreenSize();
|
---|
[27236] | 56 | }
|
---|
| 57 |
|
---|
[26878] | 58 | bool UIMachineViewNormal::eventFilter(QObject *pWatched, QEvent *pEvent)
|
---|
| 59 | {
|
---|
[41107] | 60 | if (pWatched != 0 && pWatched == machineWindow())
|
---|
[26878] | 61 | {
|
---|
| 62 | switch (pEvent->type())
|
---|
| 63 | {
|
---|
| 64 | case QEvent::Resize:
|
---|
| 65 | {
|
---|
[97976] | 66 | /* Recalculate maximum guest size: */
|
---|
| 67 | setMaximumGuestSize();
|
---|
[48688] | 68 | /* And resize guest to current window size: */
|
---|
[98450] | 69 | if (m_fGuestAutoresizeEnabled && uimachine()->isGuestSupportsGraphics())
|
---|
[26878] | 70 | QTimer::singleShot(300, this, SLOT(sltPerformGuestResize()));
|
---|
| 71 | break;
|
---|
| 72 | }
|
---|
| 73 | default:
|
---|
| 74 | break;
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
[27235] | 77 |
|
---|
[60607] | 78 | /* For scroll-bars of the machine-view: */
|
---|
| 79 | if ( pWatched == verticalScrollBar()
|
---|
| 80 | || pWatched == horizontalScrollBar())
|
---|
| 81 | {
|
---|
| 82 | switch (pEvent->type())
|
---|
| 83 | {
|
---|
| 84 | /* On show/hide event: */
|
---|
| 85 | case QEvent::Show:
|
---|
| 86 | case QEvent::Hide:
|
---|
| 87 | {
|
---|
| 88 | /* Set maximum-size to size-hint: */
|
---|
| 89 | setMaximumSize(sizeHint());
|
---|
| 90 | break;
|
---|
| 91 | }
|
---|
| 92 | default:
|
---|
| 93 | break;
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 |
|
---|
[26878] | 97 | return UIMachineView::eventFilter(pWatched, pEvent);
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[30291] | 100 | void UIMachineViewNormal::prepareCommon()
|
---|
| 101 | {
|
---|
| 102 | /* Base class common settings: */
|
---|
| 103 | UIMachineView::prepareCommon();
|
---|
| 104 |
|
---|
| 105 | /* Setup size-policy: */
|
---|
| 106 | setSizePolicy(QSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum));
|
---|
[60607] | 107 | /* Set maximum-size to size-hint: */
|
---|
[30291] | 108 | setMaximumSize(sizeHint());
|
---|
| 109 | }
|
---|
| 110 |
|
---|
[27090] | 111 | void UIMachineViewNormal::prepareFilters()
|
---|
| 112 | {
|
---|
| 113 | /* Base class filters: */
|
---|
| 114 | UIMachineView::prepareFilters();
|
---|
| 115 |
|
---|
[60607] | 116 | /* Install scroll-bars event-filters: */
|
---|
| 117 | verticalScrollBar()->installEventFilter(this);
|
---|
| 118 | horizontalScrollBar()->installEventFilter(this);
|
---|
| 119 |
|
---|
[60362] | 120 | #ifdef VBOX_WS_WIN
|
---|
[44952] | 121 | /* Install menu-bar event-filter: */
|
---|
[41107] | 122 | machineWindow()->menuBar()->installEventFilter(this);
|
---|
[60362] | 123 | #endif /* VBOX_WS_WIN */
|
---|
[27090] | 124 | }
|
---|
| 125 |
|
---|
| 126 | void UIMachineViewNormal::prepareConsoleConnections()
|
---|
| 127 | {
|
---|
| 128 | /* Base class connections: */
|
---|
| 129 | UIMachineView::prepareConsoleConnections();
|
---|
| 130 |
|
---|
| 131 | /* Guest additions state-change updater: */
|
---|
[98385] | 132 | connect(uimachine(), &UIMachine::sigAdditionsStateActualChange, this, &UIMachineViewNormal::sltAdditionsStateChanged);
|
---|
[27090] | 133 | }
|
---|
| 134 |
|
---|
[49176] | 135 | void UIMachineViewNormal::setGuestAutoresizeEnabled(bool fEnabled)
|
---|
| 136 | {
|
---|
[97975] | 137 | if (m_fGuestAutoresizeEnabled != fEnabled)
|
---|
[49176] | 138 | {
|
---|
[97975] | 139 | m_fGuestAutoresizeEnabled = fEnabled;
|
---|
[49176] | 140 |
|
---|
[98450] | 141 | if (m_fGuestAutoresizeEnabled && uimachine()->isGuestSupportsGraphics())
|
---|
[49176] | 142 | sltPerformGuestResize();
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
| 145 |
|
---|
[55939] | 146 | void UIMachineViewNormal::resendSizeHint()
|
---|
[38978] | 147 | {
|
---|
[98547] | 148 | /* Skip if VM isn't running/paused yet: */
|
---|
| 149 | if ( !uimachine()->isRunning()
|
---|
| 150 | && !uimachine()->isPaused())
|
---|
| 151 | return;
|
---|
| 152 |
|
---|
[65450] | 153 | /* Skip if another visual representation mode requested: */
|
---|
[98419] | 154 | if (uimachine()->requestedVisualState() == UIVisualStateType_Seamless) // Seamless only for now.
|
---|
[65450] | 155 | return;
|
---|
| 156 |
|
---|
[56701] | 157 | /* Get the last guest-screen size-hint, taking the scale factor into account. */
|
---|
[98010] | 158 | const QSize storedSizeHint = storedGuestScreenSizeHint();
|
---|
| 159 | const QSize effectiveSizeHint = scaledBackward(storedSizeHint);
|
---|
[55939] | 160 | LogRel(("GUI: UIMachineViewNormal::resendSizeHint: Restoring guest size-hint for screen %d to %dx%d\n",
|
---|
[98010] | 161 | (int)screenId(), effectiveSizeHint.width(), effectiveSizeHint.height()));
|
---|
[56701] | 162 |
|
---|
| 163 | /* Expand current limitations: */
|
---|
[98010] | 164 | setMaximumGuestSize(effectiveSizeHint);
|
---|
[56701] | 165 |
|
---|
[57495] | 166 | /* Temporarily restrict the size to prevent a brief resize to the
|
---|
| 167 | * frame-buffer dimensions when we exit full-screen. This is only
|
---|
| 168 | * applied if the frame-buffer is at full-screen dimensions and
|
---|
| 169 | * until the first machine view resize. */
|
---|
[98010] | 170 | m_sizeHintOverride = scaledForward(QSize(640, 480)).expandedTo(storedSizeHint);
|
---|
[56701] | 171 |
|
---|
[78995] | 172 | /* Restore saved monitor information to the guest. The guest may not respond
|
---|
| 173 | * until a suitable driver or helper is enabled (or at all). We do not notify
|
---|
| 174 | * the guest (aNotify == false), because there is technically no change (same
|
---|
| 175 | * hardware as before shutdown), and notifying would interfere with the Windows
|
---|
| 176 | * guest driver which saves the video mode to the registry on shutdown. */
|
---|
[98386] | 177 | uimachine()->setScreenVisibleHostDesires(screenId(), guestScreenVisibilityStatus());
|
---|
[98547] | 178 | uimachine()->setVideoModeHint(screenId(),
|
---|
| 179 | guestScreenVisibilityStatus(),
|
---|
| 180 | false /* change origin? */,
|
---|
| 181 | 0 /* origin x */, 0 /* origin y */,
|
---|
| 182 | effectiveSizeHint.width(), effectiveSizeHint.height(),
|
---|
| 183 | 0 /* bits per pixel */,
|
---|
| 184 | false /* notify? */);
|
---|
[38978] | 185 | }
|
---|
| 186 |
|
---|
[52705] | 187 | void UIMachineViewNormal::adjustGuestScreenSize()
|
---|
[27090] | 188 | {
|
---|
[72458] | 189 | LogRel(("GUI: UIMachineViewNormal::adjustGuestScreenSize: Adjust guest-screen size if necessary\n"));
|
---|
[57285] | 190 |
|
---|
[97978] | 191 | /* Acquire requested guest-screen size-hint or at least actual frame-buffer size: */
|
---|
| 192 | QSize guestScreenSizeHint = requestedGuestScreenSizeHint();
|
---|
| 193 | /* Take the scale-factor(s) into account: */
|
---|
| 194 | guestScreenSizeHint = scaledForward(guestScreenSizeHint);
|
---|
[57285] | 195 |
|
---|
[97978] | 196 | /* Calculate maximum possible guest screen size: */
|
---|
| 197 | const QSize maximumGuestScreenSize = calculateMaxGuestSize();
|
---|
[57285] | 198 |
|
---|
[97978] | 199 | /* Adjust guest-screen size if the requested one is too big for the screen: */
|
---|
| 200 | if ( guestScreenSizeHint.width() > maximumGuestScreenSize.width()
|
---|
| 201 | || guestScreenSizeHint.height() > maximumGuestScreenSize.height())
|
---|
[57285] | 202 | sltPerformGuestResize(machineWindow()->centralWidget()->size());
|
---|
[27090] | 203 | }
|
---|
| 204 |
|
---|
[60607] | 205 | QSize UIMachineViewNormal::sizeHint() const
|
---|
| 206 | {
|
---|
| 207 | /* Call to base-class: */
|
---|
| 208 | QSize size = UIMachineView::sizeHint();
|
---|
| 209 |
|
---|
| 210 | /* If guest-screen auto-resize is not enabled
|
---|
| 211 | * or the guest-additions doesn't support graphics
|
---|
| 212 | * we should take scroll-bars size-hints into account: */
|
---|
[98450] | 213 | if (!m_fGuestAutoresizeEnabled || !uimachine()->isGuestSupportsGraphics())
|
---|
[60607] | 214 | {
|
---|
| 215 | if (verticalScrollBar()->isVisible())
|
---|
| 216 | size += QSize(verticalScrollBar()->sizeHint().width(), 0);
|
---|
| 217 | if (horizontalScrollBar()->isVisible())
|
---|
| 218 | size += QSize(0, horizontalScrollBar()->sizeHint().height());
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | /* Return resulting size-hint finally: */
|
---|
| 222 | return size;
|
---|
| 223 | }
|
---|
| 224 |
|
---|
[39021] | 225 | QRect UIMachineViewNormal::workingArea() const
|
---|
[27090] | 226 | {
|
---|
[97682] | 227 | return gpDesktop->availableGeometry(this);
|
---|
[27090] | 228 | }
|
---|
| 229 |
|
---|
[39023] | 230 | QSize UIMachineViewNormal::calculateMaxGuestSize() const
|
---|
[27237] | 231 | {
|
---|
[48644] | 232 | /* 1) The calculation below is not reliable on some (X11) platforms until we
|
---|
[48688] | 233 | * have been visible for a fraction of a second, so do the best we can
|
---|
[48644] | 234 | * otherwise.
|
---|
| 235 | * 2) We also get called early before "machineWindow" has been fully
|
---|
| 236 | * initialised, at which time we can't perform the calculation. */
|
---|
| 237 | if (!isVisible())
|
---|
| 238 | return workingArea().size() * 0.95;
|
---|
[43022] | 239 | /* The area taken up by the machine window on the desktop, including window
|
---|
| 240 | * frame, title, menu bar and status bar. */
|
---|
| 241 | QSize windowSize = machineWindow()->frameGeometry().size();
|
---|
| 242 | /* The window shouldn't be allowed to expand beyond the working area
|
---|
| 243 | * unless it already does. In that case the guest shouldn't expand it
|
---|
| 244 | * any further though. */
|
---|
| 245 | QSize maximumSize = workingArea().size().expandedTo(windowSize);
|
---|
| 246 | /* The current size of the machine display. */
|
---|
| 247 | QSize centralWidgetSize = machineWindow()->centralWidget()->size();
|
---|
| 248 | /* To work out how big the guest display can get without the window going
|
---|
| 249 | * over the maximum size we calculated above, we work out how much space
|
---|
| 250 | * the other parts of the window (frame, menu bar, status bar and so on)
|
---|
[48950] | 251 | * take up and subtract that space from the maximum window size. The
|
---|
[43022] | 252 | * central widget shouldn't be bigger than the window, but we bound it for
|
---|
| 253 | * sanity (or insanity) reasons. */
|
---|
| 254 | return maximumSize - (windowSize - centralWidgetSize.boundedTo(windowSize));
|
---|
[27237] | 255 | }
|
---|