on e174ec3 help sign in
268 lines 7.8 kB history raw
1/*
2 * Copyright (C) 2017 Canonical, Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify it under
5 * the terms of the GNU Lesser General Public License version 3, as published by
6 * the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
10 * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
17#include "Screen.h"
18#include "Screens.h"
19#include "WorkspaceManager.h"
20#include "Workspace.h"
22ScreenInterface::ScreenInterface(QObject *parent)
23 : QObject(parent)
27void ScreenInterface::connectToScreen(qtmir::Screen *screen)
29 m_wrapped = screen;
30 connect(screen, &qtmir::Screen::usedChanged, this, &ScreenInterface::usedChanged);
31 connect(screen, &qtmir::Screen::nameChanged, this, &ScreenInterface::nameChanged);
32 connect(screen, &qtmir::Screen::outputTypeChanged, this, &ScreenInterface::outputTypeChanged);
33 connect(screen, &qtmir::Screen::scaleChanged, this, &ScreenInterface::scaleChanged);
34 connect(screen, &qtmir::Screen::formFactorChanged, this, &ScreenInterface::formFactorChanged);
35 connect(screen, &qtmir::Screen::physicalSizeChanged, this, &ScreenInterface::physicalSizeChanged);
36 connect(screen, &qtmir::Screen::positionChanged, this, &ScreenInterface::positionChanged);
37 connect(screen, &qtmir::Screen::activeChanged, this, &ScreenInterface::activeChanged);
38 connect(screen, &qtmir::Screen::currentModeIndexChanged, this, &ScreenInterface::currentModeIndexChanged);
39 connect(screen, &qtmir::Screen::availableModesChanged, this, &ScreenInterface::availableModesChanged);
42void ScreenInterface::connectToScreen(ScreenInterface *screen)
44 connectToScreen(screen->wrapped());
45 connect(screen, &ScreenInterface::currentWorkspaceChanged, this, &ScreenInterface::currentWorkspaceChanged);
48qtmir::OutputId ScreenInterface::outputId() const
50 if (!m_wrapped) return qtmir::OutputId(-1);
51 return m_wrapped->outputId();
54bool ScreenInterface::used() const
56 if (!m_wrapped) return false;
57 return m_wrapped->used();
60QString ScreenInterface::name() const
62 if (!m_wrapped) return QString();
63 return m_wrapped->name();
66float ScreenInterface::scale() const
68 if (!m_wrapped) return 1.0;
69 return m_wrapped->scale();
72QSizeF ScreenInterface::physicalSize() const
74 if (!m_wrapped) return QSizeF();
75 return m_wrapped->physicalSize();
78qtmir::FormFactor ScreenInterface::formFactor() const
80 if (!m_wrapped) return qtmir::FormFactorUnknown;
81 return m_wrapped->formFactor();
84qtmir::OutputTypes ScreenInterface::outputType() const
86 if (!m_wrapped) return qtmir::Unknown;
87 return m_wrapped->outputType();
90MirPowerMode ScreenInterface::powerMode() const
92 if (!m_wrapped) return mir_power_mode_on;
93 return m_wrapped->powerMode();
96Qt::ScreenOrientation ScreenInterface::orientation() const
98 if (!m_wrapped) return Qt::PrimaryOrientation;
99 return m_wrapped->orientation();
102QPoint ScreenInterface::position() const
104 if (!m_wrapped) return QPoint();
105 return m_wrapped->position();
108QQmlListProperty<qtmir::ScreenMode> ScreenInterface::availableModes()
110 if (!m_wrapped) return QQmlListProperty<qtmir::ScreenMode>();
111 return m_wrapped->availableModes();
114uint ScreenInterface::currentModeIndex() const
116 if (!m_wrapped) return -1;
117 return m_wrapped->currentModeIndex();
120bool ScreenInterface::isActive() const
122 if (!m_wrapped) return false;
123 return m_wrapped->isActive();
126void ScreenInterface::activate()
128 setActive(true);
131void ScreenInterface::setActive(bool active)
133 if (!m_wrapped) return;
134 m_wrapped->setActive(active);
137QScreen *ScreenInterface::qscreen() const
139 if (!m_wrapped) return nullptr;
140 return m_wrapped->qscreen();
143qtmir::ScreenConfiguration *ScreenInterface::beginConfiguration() const
145 if (!m_wrapped) return nullptr;
146 return m_wrapped->beginConfiguration();
149bool ScreenInterface::applyConfiguration(qtmir::ScreenConfiguration *configuration)
151 if (!m_wrapped) return false;
152 return m_wrapped->applyConfiguration(configuration);
155void ScreenInterface::sync(ScreenInterface *proxy)
157 if (!proxy) return;
158 workspaces()->sync(proxy->workspaces());
161Screen::Screen(qtmir::Screen* wrapped)
162 : m_workspaces(new WorkspaceModel)
164 connectToScreen(wrapped);
166 // Connect the active workspace to activate the screen.
167 connect(m_workspaces.data(), &WorkspaceModel::workspaceInserted, this, [this](int, Workspace* workspace) {
168 connect(workspace, &Workspace::activeChanged, this, [this, workspace](bool active) {
169 if (active) {
170 setCurrentWorkspace(workspace);
171 activate();
172 }
173 });
174 if (workspace->isActive()) {
175 activate();
176 setCurrentWorkspace(workspace);
177 }
178 if (!m_currentWorspace) {
179 setCurrentWorkspace(workspace);
180 }
181 });
182 connect(m_workspaces.data(), &WorkspaceModel::workspaceRemoved, this, [this](Workspace* workspace) {
183 disconnect(workspace, &Workspace::activeChanged, this, 0);
184 if (workspace == m_currentWorspace) {
185 resetCurrentWorkspace();
186 }
187 });
188 connect(this, &Screen::activeChanged, this, [this](bool active) {
189 if (active && m_currentWorspace) {
190 m_currentWorspace->activate();
191 }
192 });
194 WorkspaceManager::instance()->createWorkspace()->assign(m_workspaces.data());
195 WorkspaceManager::instance()->createWorkspace()->assign(m_workspaces.data());
198void Screen::resetCurrentWorkspace()
200 auto newCurrent = m_workspaces->rowCount() > 0 ? m_workspaces->get(0) : nullptr;
201 if (m_currentWorspace != newCurrent) {
202 m_currentWorspace = newCurrent;
203 Q_EMIT currentWorkspaceChanged(newCurrent);
204 }
208WorkspaceModel *Screen::workspaces() const
210 return m_workspaces.data();
213Workspace *Screen::currentWorkspace() const
215 return m_currentWorspace.data();
218void Screen::setCurrentWorkspace(Workspace *workspace)
220 if (m_currentWorspace != workspace) {
221 m_currentWorspace = workspace;
222 Q_EMIT currentWorkspaceChanged(workspace);
223 }
226ScreenProxy::ScreenProxy(ScreenInterface *const screen)
227 : m_workspaces(new WorkspaceModelProxy(screen->workspaces()))
228 , m_original(screen)
230 connectToScreen(screen);
232 auto updateCurrentWorkspaceFn = [this](Workspace* realWorkspace) {
233 Q_FOREACH(Workspace* workspace, m_workspaces->list()) {
234 auto p = qobject_cast<WorkspaceProxy*>(workspace);
235 if (p && p->proxyObject() == realWorkspace) {
236 if (m_currentWorspace != p) {
237 m_currentWorspace = p;
238 Q_EMIT currentWorkspaceChanged(p);
239 }
240 }
241 }
242 };
243 connect(screen, &ScreenInterface::currentWorkspaceChanged, this, updateCurrentWorkspaceFn);
244 updateCurrentWorkspaceFn(screen->currentWorkspace());
247WorkspaceModel *ScreenProxy::workspaces() const
249 return m_workspaces.data();
252Workspace *ScreenProxy::currentWorkspace() const
254 return m_currentWorspace.data();
257void ScreenProxy::setCurrentWorkspace(Workspace *workspace)
259 auto p = qobject_cast<WorkspaceProxy*>(workspace);
260 if (p) {
261 m_original->setCurrentWorkspace(p->proxyObject());
262 }
265void ScreenProxy::addWorkspace()
267 (new WorkspaceProxy(WorkspaceManager::instance()->createWorkspace()))->assign(workspaces());