on 0bcac19 help sign in
294 lines 8.4 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);
42qtmir::OutputId ScreenInterface::outputId() const
44 if (!m_wrapped) return qtmir::OutputId(-1);
45 return m_wrapped->outputId();
48bool ScreenInterface::used() const
50 if (!m_wrapped) return false;
51 return m_wrapped->used();
54QString ScreenInterface::name() const
56 if (!m_wrapped) return QString();
57 return m_wrapped->name();
60float ScreenInterface::scale() const
62 if (!m_wrapped) return 1.0;
63 return m_wrapped->scale();
66QSizeF ScreenInterface::physicalSize() const
68 if (!m_wrapped) return QSizeF();
69 return m_wrapped->physicalSize();
72qtmir::FormFactor ScreenInterface::formFactor() const
74 if (!m_wrapped) return qtmir::FormFactorUnknown;
75 return m_wrapped->formFactor();
78qtmir::OutputTypes ScreenInterface::outputType() const
80 if (!m_wrapped) return qtmir::Unknown;
81 return m_wrapped->outputType();
84MirPowerMode ScreenInterface::powerMode() const
86 if (!m_wrapped) return mir_power_mode_on;
87 return m_wrapped->powerMode();
90Qt::ScreenOrientation ScreenInterface::orientation() const
92 if (!m_wrapped) return Qt::PrimaryOrientation;
93 return m_wrapped->orientation();
96QPoint ScreenInterface::position() const
98 if (!m_wrapped) return QPoint();
99 return m_wrapped->position();
102QQmlListProperty<qtmir::ScreenMode> ScreenInterface::availableModes()
104 if (!m_wrapped) return QQmlListProperty<qtmir::ScreenMode>();
105 return m_wrapped->availableModes();
108uint ScreenInterface::currentModeIndex() const
110 if (!m_wrapped) return -1;
111 return m_wrapped->currentModeIndex();
114bool ScreenInterface::isActive() const
116 if (!m_wrapped) return false;
117 return m_wrapped->isActive();
120void ScreenInterface::activate()
122 setActive(true);
125void ScreenInterface::setActive(bool active)
127 if (!m_wrapped) return;
128 m_wrapped->setActive(active);
131QScreen *ScreenInterface::qscreen() const
133 if (!m_wrapped) return nullptr;
134 return m_wrapped->qscreen();
137qtmir::ScreenConfiguration *ScreenInterface::beginConfiguration() const
139 if (!m_wrapped) return nullptr;
140 return m_wrapped->beginConfiguration();
143bool ScreenInterface::applyConfiguration(qtmir::ScreenConfiguration *configuration)
145 if (!m_wrapped) return false;
146 return m_wrapped->applyConfiguration(configuration);
149QString ScreenInterface::outputTypeName() const
151 switch (m_wrapped->outputType()) {
152 case qtmir::Unknown:
153 return tr("Unknown");
154 case qtmir::VGA:
155 return tr("VGA");
156 case qtmir::DVII:
157 case qtmir::DVID:
158 case qtmir::DVIA:
159 return tr("DVI");
160 case qtmir::Composite:
161 return tr("Composite");
162 case qtmir::SVideo:
163 return tr("S-Video");
164 case qtmir::LVDS:
165 case qtmir::NinePinDIN:
166 case qtmir::EDP:
167 return tr("Internal");
168 case qtmir::Component:
169 return tr("Component");
170 case qtmir::DisplayPort:
171 return tr("DisplayPort");
172 case qtmir::HDMIA:
173 case qtmir::HDMIB:
174 return tr("HDMI");
175 case qtmir::TV:
176 return tr("TV");
177 }
178 return QString();
181void ScreenInterface::sync(ScreenInterface *proxy)
183 if (!proxy) return;
184 workspaces()->sync(proxy->workspaces());
187Screen::Screen(qtmir::Screen* wrapped)
188 : m_workspaces(new WorkspaceModel)
190 connectToScreen(wrapped);
192 // Connect the active workspace to activate the screen.
193 connect(m_workspaces.data(), &WorkspaceModel::workspaceInserted, this, [this](int, Workspace* workspace) {
194 connect(workspace, &Workspace::activeChanged, this, [this, workspace](bool active) {
195 if (active) {
196 setCurrentWorkspace(workspace);
197 activate();
198 }
199 });
200 if (workspace->isActive()) {
201 activate();
202 setCurrentWorkspace(workspace);
203 }
204 if (!m_currentWorspace) {
205 setCurrentWorkspace(workspace);
206 }
207 });
208 connect(m_workspaces.data(), &WorkspaceModel::workspaceRemoved, this, [this](Workspace* workspace) {
209 disconnect(workspace, &Workspace::activeChanged, this, 0);
210 if (workspace == m_currentWorspace) {
211 resetCurrentWorkspace();
212 }
213 });
214 connect(this, &Screen::activeChanged, this, [this](bool active) {
215 if (active && m_currentWorspace) {
216 m_currentWorspace->activate();
217 }
218 });
220 WorkspaceManager::instance()->createWorkspace()->assign(m_workspaces.data());
221 WorkspaceManager::instance()->createWorkspace()->assign(m_workspaces.data());
224void Screen::resetCurrentWorkspace()
226 auto newCurrent = m_workspaces->rowCount() > 0 ? m_workspaces->get(0) : nullptr;
227 if (m_currentWorspace != newCurrent) {
228 m_currentWorspace = newCurrent;
229 Q_EMIT currentWorkspaceChanged(newCurrent);
230 }
234WorkspaceModel *Screen::workspaces() const
236 return m_workspaces.data();
239Workspace *Screen::currentWorkspace() const
241 return m_currentWorspace.data();
244void Screen::setCurrentWorkspace(Workspace *workspace)
246 if (m_currentWorspace != workspace) {
247 m_currentWorspace = workspace;
248 Q_EMIT currentWorkspaceChanged(workspace);
249 }
252ScreenProxy::ScreenProxy(ScreenInterface *const screen)
253 : m_workspaces(new WorkspaceModelProxy(screen->workspaces()))
254 , m_original(screen)
256 connectToScreen(screen->wrapped());
258 auto updateCurrentWorkspaceFn = [this](Workspace* realWorkspace) {
259 Q_FOREACH(Workspace* workspace, m_workspaces->list()) {
260 auto p = qobject_cast<WorkspaceProxy*>(workspace);
261 if (p && p->proxyObject() == realWorkspace) {
262 if (m_currentWorspace != p) {
263 m_currentWorspace = p;
264 Q_EMIT currentWorkspaceChanged(p);
265 }
266 }
267 }
268 };
269 connect(screen, &ScreenInterface::currentWorkspaceChanged, this, updateCurrentWorkspaceFn);
270 updateCurrentWorkspaceFn(screen->currentWorkspace());
273WorkspaceModel *ScreenProxy::workspaces() const
275 return m_workspaces.data();
278Workspace *ScreenProxy::currentWorkspace() const
280 return m_currentWorspace.data();
283void ScreenProxy::setCurrentWorkspace(Workspace *workspace)
285 auto p = qobject_cast<WorkspaceProxy*>(workspace);
286 if (p) {
287 m_original->setCurrentWorkspace(p->proxyObject());
288 }
291void ScreenProxy::addWorkspace()
293 (new WorkspaceProxy(WorkspaceManager::instance()->createWorkspace()))->assign(workspaces());