on 6efc4c4 help sign in
300 lines 8.6 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);
155QString ScreenInterface::outputTypeName() const
157 switch (m_wrapped->outputType()) {
158 case qtmir::Unknown:
159 return tr("Unknown");
160 case qtmir::VGA:
161 return tr("VGA");
162 case qtmir::DVII:
163 case qtmir::DVID:
164 case qtmir::DVIA:
165 return tr("DVI");
166 case qtmir::Composite:
167 return tr("Composite");
168 case qtmir::SVideo:
169 return tr("S-Video");
170 case qtmir::LVDS:
171 case qtmir::NinePinDIN:
172 case qtmir::EDP:
173 return tr("Internal");
174 case qtmir::Component:
175 return tr("Component");
176 case qtmir::DisplayPort:
177 return tr("DisplayPort");
178 case qtmir::HDMIA:
179 case qtmir::HDMIB:
180 return tr("HDMI");
181 case qtmir::TV:
182 return tr("TV");
183 }
184 return QString();
187void ScreenInterface::sync(ScreenInterface *proxy)
189 if (!proxy) return;
190 workspaces()->sync(proxy->workspaces());
193Screen::Screen(qtmir::Screen* wrapped)
194 : m_workspaces(new WorkspaceModel)
196 connectToScreen(wrapped);
198 // Connect the active workspace to activate the screen.
199 connect(m_workspaces.data(), &WorkspaceModel::workspaceInserted, this, [this](int, Workspace* workspace) {
200 connect(workspace, &Workspace::activeChanged, this, [this, workspace](bool active) {
201 if (active) {
202 setCurrentWorkspace(workspace);
203 activate();
204 }
205 });
206 if (workspace->isActive()) {
207 activate();
208 setCurrentWorkspace(workspace);
209 }
210 if (!m_currentWorspace) {
211 setCurrentWorkspace(workspace);
212 }
213 });
214 connect(m_workspaces.data(), &WorkspaceModel::workspaceRemoved, this, [this](Workspace* workspace) {
215 disconnect(workspace, &Workspace::activeChanged, this, 0);
216 if (workspace == m_currentWorspace) {
217 resetCurrentWorkspace();
218 }
219 });
220 connect(this, &Screen::activeChanged, this, [this](bool active) {
221 if (active && m_currentWorspace) {
222 m_currentWorspace->activate();
223 }
224 });
226 WorkspaceManager::instance()->createWorkspace()->assign(m_workspaces.data());
227 WorkspaceManager::instance()->createWorkspace()->assign(m_workspaces.data());
230void Screen::resetCurrentWorkspace()
232 auto newCurrent = m_workspaces->rowCount() > 0 ? m_workspaces->get(0) : nullptr;
233 if (m_currentWorspace != newCurrent) {
234 m_currentWorspace = newCurrent;
235 Q_EMIT currentWorkspaceChanged(newCurrent);
236 }
240WorkspaceModel *Screen::workspaces() const
242 return m_workspaces.data();
245Workspace *Screen::currentWorkspace() const
247 return m_currentWorspace.data();
250void Screen::setCurrentWorkspace(Workspace *workspace)
252 if (m_currentWorspace != workspace) {
253 m_currentWorspace = workspace;
254 Q_EMIT currentWorkspaceChanged(workspace);
255 }
258ScreenProxy::ScreenProxy(ScreenInterface *const screen)
259 : m_workspaces(new WorkspaceModelProxy(screen->workspaces()))
260 , m_original(screen)
262 connectToScreen(screen);
264 auto updateCurrentWorkspaceFn = [this](Workspace* realWorkspace) {
265 Q_FOREACH(Workspace* workspace, m_workspaces->list()) {
266 auto p = qobject_cast<WorkspaceProxy*>(workspace);
267 if (p && p->proxyObject() == realWorkspace) {
268 if (m_currentWorspace != p) {
269 m_currentWorspace = p;
270 Q_EMIT currentWorkspaceChanged(p);
271 }
272 }
273 }
274 };
275 connect(screen, &ScreenInterface::currentWorkspaceChanged, this, updateCurrentWorkspaceFn);
276 updateCurrentWorkspaceFn(screen->currentWorkspace());
279WorkspaceModel *ScreenProxy::workspaces() const
281 return m_workspaces.data();
284Workspace *ScreenProxy::currentWorkspace() const
286 return m_currentWorspace.data();
289void ScreenProxy::setCurrentWorkspace(Workspace *workspace)
291 auto p = qobject_cast<WorkspaceProxy*>(workspace);
292 if (p) {
293 m_original->setCurrentWorkspace(p->proxyObject());
294 }
297void ScreenProxy::addWorkspace()
299 (new WorkspaceProxy(WorkspaceManager::instance()->createWorkspace()))->assign(workspaces());