summaryrefslogtreecommitdiff
path: root/src/VBox/Frontends/VirtualBox/src/runtime/UIMultiScreenLayout.cpp
blob: bebd2ebd0d9df80dc2348adda497106b6d440b80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/* $Id$ */
/** @file
 * VBox Qt GUI - UIMultiScreenLayout class implementation.
 */

/*
 * Copyright (C) 2010-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

/* GUI includes: */
#include "UIActionPoolRuntime.h"
#include "UICommon.h"
#include "UIDesktopWidgetWatchdog.h"
#include "UIExtraDataManager.h"
#include "UIMachine.h"
#include "UIMachineLogic.h"
#include "UIMessageCenter.h"
#include "UIMultiScreenLayout.h"

/* COM includes: */
#include "COMEnums.h"
#include "CMachine.h"
#include "CGraphicsAdapter.h"


UIMultiScreenLayout::UIMultiScreenLayout(UIMachineLogic *pMachineLogic)
    : m_pMachineLogic(pMachineLogic)
    , m_cGuestScreens(m_pMachineLogic->machine().GetGraphicsAdapter().GetMonitorCount())
    , m_cHostMonitors(0)
{
    prepare();
}

bool UIMultiScreenLayout::hasHostScreenForGuestScreen(int iScreenId) const
{
    return m_screenMap.contains(iScreenId);
}

int UIMultiScreenLayout::hostScreenForGuestScreen(int iScreenId) const
{
    return m_screenMap.value(iScreenId, 0);
}

quint64 UIMultiScreenLayout::memoryRequirements() const
{
    return memoryRequirements(m_screenMap);
}

void UIMultiScreenLayout::update()
{
    LogRelFlow(("UIMultiScreenLayout::update: Started...\n"));

    /* Clear screen-map initially: */
    m_screenMap.clear();

    /* Make a pool of available host screens: */
    QList<int> availableScreens;
    for (int i = 0; i < m_cHostMonitors; ++i)
        availableScreens << i;

    /* Load all combinations stored in the settings file.
     * We have to make sure they are valid, which means there have to be unique combinations
     * and all guests screens need there own host-monitor. */
    const bool fShouldWeAutoMountGuestScreens = gEDataManager->autoMountGuestScreensEnabled(uiCommon().managedVMUuid());
    LogRel(("GUI: UIMultiScreenLayout::update: GUI/AutomountGuestScreens is %s\n", fShouldWeAutoMountGuestScreens ? "enabled" : "disabled"));
    foreach (int iGuestScreen, m_guestScreens)
    {
        /* Initialize variables: */
        bool fValid = false;
        int iHostScreen = -1;

        if (!fValid)
        {
            /* If the user ever selected a combination in the view menu, we have the following entry: */
            iHostScreen = gEDataManager->hostScreenForPassedGuestScreen(iGuestScreen, uiCommon().managedVMUuid());
            /* Revalidate: */
            fValid =    iHostScreen >= 0 && iHostScreen < m_cHostMonitors /* In the host-monitor bounds? */
                     && m_screenMap.key(iHostScreen, -1) == -1; /* Not taken already? */
        }

        if (!fValid)
        {
            /* Check the position of the guest window in normal mode.
             * This makes sure that on first use fullscreen/seamless window opens on the same host-screen as the normal window was before.
             * This even works with multi-screen. The user just have to move all the normal windows to the target host-screens
             * and they will magically open there in fullscreen/seamless also. */
            QRect geo = gEDataManager->machineWindowGeometry(UIVisualStateType_Normal, iGuestScreen, uiCommon().managedVMUuid());
            /* If geometry is valid: */
            if (!geo.isNull())
            {
                /* Get top-left corner position: */
                QPoint topLeftPosition(geo.topLeft());
                /* Check which host-screen the position belongs to: */
                iHostScreen = UIDesktopWidgetWatchdog::screenNumber(topLeftPosition);
                /* Revalidate: */
                fValid =    iHostScreen >= 0 && iHostScreen < m_cHostMonitors /* In the host-monitor bounds? */
                         && m_screenMap.key(iHostScreen, -1) == -1; /* Not taken already? */
            }
        }

        if (!fValid)
        {
            /* If still not valid, pick the next one
             * if there is still available host-monitor: */
            if (!availableScreens.isEmpty())
            {
                iHostScreen = availableScreens.first();
                fValid = true;
            }
        }

        if (fValid)
        {
            /* Register host-monitor for the guest-screen: */
            m_screenMap.insert(iGuestScreen, iHostScreen);
            /* Remove it from the list of available host screens: */
            availableScreens.removeOne(iHostScreen);
        }
        /* Do we have opinion about what to do with excessive guest-screen? */
        else if (fShouldWeAutoMountGuestScreens)
        {
            /* Then we have to disable excessive guest-screen: */
            LogRel(("GUI: UIMultiScreenLayout::update: Disabling excessive guest-screen %d\n", iGuestScreen));
            uimachine()->setScreenVisibleHostDesires(iGuestScreen, false);
            uimachine()->setVideoModeHint(iGuestScreen,
                                          false /* enabled? */,
                                          false /* change origin? */,
                                          0 /* origin x */, 0 /* origin y */,
                                          0 /* width */, 0 /* height*/,
                                          0 /* bits per pixel */,
                                          true /* notify? */);
        }
    }

    /* Are we still have available host-screens
     * and have opinion about what to do with disabled guest-screens? */
    if (!availableScreens.isEmpty() && fShouldWeAutoMountGuestScreens)
    {
        /* How many excessive host-screens do we have? */
        int cExcessiveHostScreens = availableScreens.size();
        /* How many disabled guest-screens do we have? */
        int cDisabledGuestScreens = m_disabledGuestScreens.size();
        /* We have to try to enable disabled guest-screens if any: */
        int cGuestScreensToEnable = qMin(cExcessiveHostScreens, cDisabledGuestScreens);
        for (int iGuestScreenIndex = 0; iGuestScreenIndex < cGuestScreensToEnable; ++iGuestScreenIndex)
        {
            /* Defaults: */
            ULONG uWidth = 800;
            ULONG uHeight = 600;
            /* Try to get previous guest-screen arguments: */
            const int iGuestScreen = m_disabledGuestScreens.at(iGuestScreenIndex);
            const QSize guestScreenSize = uimachine()->guestScreenSize(iGuestScreen);
            {
                if (guestScreenSize.width() > 0)
                    uWidth = guestScreenSize.width();
                if (guestScreenSize.height() > 0)
                    uHeight = guestScreenSize.height();
            }
            /* Re-enable guest-screen with proper resolution: */
            LogRel(("GUI: UIMultiScreenLayout::update: Enabling guest-screen %d with following resolution: %dx%d\n",
                    iGuestScreen, uWidth, uHeight));
            uimachine()->setScreenVisibleHostDesires(iGuestScreen, true);
            uimachine()->setVideoModeHint(iGuestScreen,
                                          true/* enabled? */,
                                          false /* change origin? */,
                                          0 /* origin x */, 0 /* origin y */,
                                          uWidth, uHeight,
                                          32/* bits per pixel */,
                                          true /* notify? */);
        }
    }

    /* Make sure action-pool knows whether multi-screen layout has host-screen for guest-screen: */
    actionPool()->toRuntime()->setHostScreenForGuestScreenMap(m_screenMap);

    LogRelFlow(("UIMultiScreenLayout::update: Finished!\n"));
}

void UIMultiScreenLayout::rebuild()
{
    LogRelFlow(("UIMultiScreenLayout::rebuild: Started...\n"));

    /* Recalculate host/guest screen count: */
    calculateHostMonitorCount();
    calculateGuestScreenCount();

    /* Update layout: */
    update();

    LogRelFlow(("UIMultiScreenLayout::rebuild: Finished!\n"));
}

void UIMultiScreenLayout::sltHandleScreenLayoutChange(int iRequestedGuestScreen, int iRequestedHostMonitor)
{
    /* Search for the virtual screen which is currently displayed on the
     * requested host-monitor. When there is one found, we swap both. */
    QMap<int,int> tmpMap(m_screenMap);
    const int iCurrentGuestScreen = tmpMap.key(iRequestedHostMonitor, -1);
    if (iCurrentGuestScreen != -1 && tmpMap.contains(iRequestedGuestScreen))
        tmpMap.insert(iCurrentGuestScreen, tmpMap.value(iRequestedGuestScreen));
    else
        tmpMap.remove(iCurrentGuestScreen);
    tmpMap.insert(iRequestedGuestScreen, iRequestedHostMonitor);

    /* Check the memory requirements first: */
    bool fSuccess = true;
    if (uimachine()->isGuestSupportsGraphics())
    {
        quint64 uAvailBits = machineLogic()->machine().GetGraphicsAdapter().GetVRAMSize() * _1M * 8;
        quint64 uUsedBits = memoryRequirements(tmpMap);
        fSuccess = uAvailBits >= uUsedBits;
        if (!fSuccess)
        {
            /* We have too little video memory for the new layout, so say it to the user and revert all the changes: */
            if (machineLogic()->visualStateType() == UIVisualStateType_Seamless)
                msgCenter().cannotSwitchScreenInSeamless((((uUsedBits + 7) / 8 + _1M - 1) / _1M) * _1M);
            else
                fSuccess = msgCenter().cannotSwitchScreenInFullscreen((((uUsedBits + 7) / 8 + _1M - 1) / _1M) * _1M);
        }
    }
    /* Make sure memory requirements matched: */
    if (!fSuccess)
        return;

    /* Swap the maps: */
    m_screenMap = tmpMap;

    /* Make sure action-pool knows whether multi-screen layout has host-screen for guest-screen: */
    actionPool()->toRuntime()->setHostScreenForGuestScreenMap(m_screenMap);

    /* Save guest-to-host mapping: */
    foreach (const int &iGuestScreen, m_guestScreens)
    {
        const int iHostScreen = m_screenMap.value(iGuestScreen, -1);
        gEDataManager->setHostScreenForPassedGuestScreen(iGuestScreen, iHostScreen, uiCommon().managedVMUuid());
    }

    /* Notifies about layout change: */
    emit sigScreenLayoutChange();
}

void UIMultiScreenLayout::prepare()
{
    /* Make sure logic is always set: */
    AssertPtrReturnVoid(machineLogic());

    /* Recalculate host/guest screen count: */
    calculateHostMonitorCount();
    calculateGuestScreenCount();

    /* Prepare connections: */
    prepareConnections();
}

void UIMultiScreenLayout::prepareConnections()
{
    /* Connect action-pool: */
    connect(actionPool()->toRuntime(), &UIActionPoolRuntime::sigNotifyAboutTriggeringViewScreenRemap,
            this, &UIMultiScreenLayout::sltHandleScreenLayoutChange);
}

UIMachine *UIMultiScreenLayout::uimachine() const
{
    return machineLogic() ? machineLogic()->uimachine() : 0;
}

UIActionPool *UIMultiScreenLayout::actionPool() const
{
    return machineLogic() ? machineLogic()->actionPool() : 0;
}

void UIMultiScreenLayout::calculateHostMonitorCount()
{
    m_cHostMonitors = UIDesktopWidgetWatchdog::screenCount();
}

void UIMultiScreenLayout::calculateGuestScreenCount()
{
    m_guestScreens.clear();
    m_disabledGuestScreens.clear();
    for (uint iGuestScreen = 0; iGuestScreen < m_cGuestScreens; ++iGuestScreen)
        if (uimachine()->isScreenVisible(iGuestScreen))
            m_guestScreens << iGuestScreen;
        else
            m_disabledGuestScreens << iGuestScreen;
}

quint64 UIMultiScreenLayout::memoryRequirements(const QMap<int, int> &screenLayout) const
{
    quint64 uUsedBits = 0;
    const UIVisualStateType enmVisualStateType = machineLogic()->visualStateType();
    foreach (int iGuestScreen, m_guestScreens)
    {
        /* Make sure corresponding screen is valid: */
        const QRect screen = enmVisualStateType == UIVisualStateType_Fullscreen
                           ? gpDesktop->screenGeometry(screenLayout.value(iGuestScreen, 0))
                           : enmVisualStateType == UIVisualStateType_Seamless
                           ? gpDesktop->availableGeometry(screenLayout.value(iGuestScreen, 0))
                           : QRect();
        AssertReturn(screen.isValid(), 0);

        /* Get some useful screen info: */
        ulong uDummy = 0, uGuestBpp = 0;
        long iDummy = 0;
        KGuestMonitorStatus enmDummy = KGuestMonitorStatus_Disabled;
        uimachine()->acquireGuestScreenParameters(iGuestScreen, uDummy, uDummy, uGuestBpp,
                                                  iDummy, iDummy, enmDummy);
        uUsedBits += screen.width() * /* display width */
                     screen.height() * /* display height */
                     uGuestBpp + /* guest bits per pixel */
                     _1M * 8; /* current cache per screen - may be changed in future */
    }
    uUsedBits += 4096 * 8; /* adapter info */
    return uUsedBits;
}