on cfcde21 help sign in
unity8/tests/mocks/Unity/Application/ApplicationInfo.cpp
386 lines 11 kB history raw
1/*
2 * Copyright (C) 2013-2016 Canonical, Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
17#include "ApplicationInfo.h"
18#include "MirSurface.h"
19#include "SurfaceManager.h"
21#include <paths.h>
23#include <QGuiApplication>
24#include <QQuickItem>
25#include <QQuickView>
26#include <QQmlComponent>
28#define APPLICATION_DEBUG 0
30#if APPLICATION_DEBUG
31#define DEBUG_MSG(params) qDebug().nospace() << "Application["<<appId()<<"]::" << __func__ << " " << params
33QString stateToStr(ApplicationInfo::State state)
35 switch (state) {
36 case ApplicationInfo::Starting:
37 return "starting";
38 case ApplicationInfo::Running:
39 return "running";
40 case ApplicationInfo::Suspended:
41 return "suspended";
42 case ApplicationInfo::Stopped:
43 return "stopped";
44 default:
45 return "???";
46 };
49#else
50#define DEBUG_MSG(params) ((void)0)
51#endif
53#define WARNING_MSG(params) qWarning().nospace() << "Application["<<appId()<<"]::" << __func__ << " " << params
55ApplicationInfo::ApplicationInfo(const QString &appId, QObject *parent)
56 : ApplicationInfoInterface(appId, parent)
57 , m_appId(appId)
58 , m_surfaceList(new MirSurfaceListModel(this))
59 , m_promptSurfaceList(new MirSurfaceListModel(this))
61 connect(m_surfaceList, &MirSurfaceListModel::countChanged,
62 this, &ApplicationInfo::onSurfaceCountChanged, Qt::QueuedConnection);
64 m_surfaceCreationTimer.setSingleShot(true);
65 m_surfaceCreationTimer.setInterval(500);
66 connect(&m_surfaceCreationTimer, &QTimer::timeout, this, &ApplicationInfo::createSurface);
69ApplicationInfo::ApplicationInfo(QObject *parent)
70 : ApplicationInfo(QString(), parent)
74ApplicationInfo::~ApplicationInfo()
78void ApplicationInfo::createSurface()
80 if (state() == ApplicationInfo::Stopped) { return; }
82 QString surfaceName = name() + "s window title";
83 if (m_surfaceList->count() > 0) {
84 surfaceName.append(QString(" %1").arg(m_surfaceList->count()+1));
85 }
87 auto surfaceManager = SurfaceManager::instance();
88 if (!surfaceManager) {
89 WARNING_MSG("No SurfaceManager");
90 return;
91 }
93 auto surface = surfaceManager->createSurface(surfaceName,
94 Mir::NormalType,
95 fullscreen() ? Mir::FullscreenState : Mir::MaximizedState,
96 m_screenshotFileName);
98 surface->setShellChrome(m_shellChrome);
100 m_surfaceList->appendSurface(surface);
102 ++m_liveSurfaceCount;
103 connect(surface, &MirSurface::liveChanged, this, [this, surface](){
104 if (!surface->live()) {
105 --m_liveSurfaceCount;
106 if (m_liveSurfaceCount == 0) {
107 if (m_closingSurfaces.contains(surface)
108 || (m_state == Running && m_requestedState == RequestedRunning)) {
109 Q_EMIT closed();
110 }
111 setState(Stopped);
112 } else {
113 if (m_closingSurfaces.contains(surface) && m_requestedState == RequestedSuspended
114 && m_closingSurfaces.count() == 1) {
115 setState(Suspended);
116 }
117 }
118 m_closingSurfaces.removeAll(surface);
119 }
120 });
121 connect(surface, &MirSurface::closeRequested, this, [this, surface](){
122 m_closingSurfaces.append(surface);
123 if (m_state == Suspended) {
124 // resume to allow application to close its surface
125 setState(Running);
126 }
127 });
128 connect(surface, &MirSurface::focusRequested, this, &ApplicationInfo::focusRequested);
130 if (m_state == Starting) {
131 if (m_requestedState == RequestedRunning) {
132 setState(Running);
133 } else {
134 setState(Suspended);
135 }
136 }
139void ApplicationInfo::setIconId(const QString &iconId)
141 setIcon(QString(sourceDirectory() + "/tests/graphics/applicationIcons/%2@18.png")
142 .arg(iconId));
145void ApplicationInfo::setScreenshotId(const QString &screenshotId)
147 QString screenshotFileName;
149 if (screenshotId.endsWith(".svg")) {
150 screenshotFileName = QString("qrc:///Unity/Application/screenshots/%2")
151 .arg(screenshotId);
152 } else {
153 screenshotFileName = QString("qrc:///Unity/Application/screenshots/%2@12.png")
154 .arg(screenshotId);
155 }
157 if (screenshotFileName != m_screenshotFileName) {
158 m_screenshotFileName = screenshotFileName;
159 }
162void ApplicationInfo::setName(const QString &value)
164 if (value != m_name) {
165 m_name = value;
166 Q_EMIT nameChanged(value);
167 }
170void ApplicationInfo::setIcon(const QUrl &value)
172 if (value != m_icon) {
173 m_icon = value;
174 Q_EMIT iconChanged(value);
175 }
178void ApplicationInfo::setState(State value)
180 if (value != m_state) {
181 DEBUG_MSG(qPrintable(stateToStr(value)));
182 if (!m_manualSurfaceCreation && value == ApplicationInfo::Starting) {
183 Q_ASSERT(m_surfaceList->count() == 0);
184 m_surfaceCreationTimer.start();
185 } else if (value == ApplicationInfo::Stopped) {
186 m_surfaceCreationTimer.stop();
187 for (int i = 0; i < m_surfaceList->count(); ++i) {
188 MirSurface *surface = static_cast<MirSurface*>(m_surfaceList->get(i));
189 surface->setLive(false);
190 }
191 for (int i = 0; i < m_promptSurfaceList->count(); ++i) {
192 auto surface = static_cast<MirSurface*>(m_promptSurfaceList->get(i));
193 surface->setLive(false);
194 }
195 }
197 m_state = value;
198 Q_EMIT stateChanged(value);
199 }
202void ApplicationInfo::close()
204 DEBUG_MSG("");
206 if (m_surfaceList->count() > 0) {
207 for (int i = 0; i < m_surfaceList->count(); ++i) {
208 MirSurface *surface = static_cast<MirSurface*>(m_surfaceList->get(i));
209 surface->close();
210 }
211 } else {
212 setState(Stopped);
213 Q_EMIT closed();
214 }
217void ApplicationInfo::setFullscreen(bool value)
219 m_fullscreen = value;
220 if (m_surfaceList->rowCount() > 0) {
221 m_surfaceList->get(0)->setState(Mir::FullscreenState);
222 }
225bool ApplicationInfo::fullscreen() const
227 if (m_surfaceList->rowCount() > 0) {
228 return m_surfaceList->get(0)->state() == Mir::FullscreenState;
229 } else {
230 return m_fullscreen;
231 }
234void ApplicationInfo::setManualSurfaceCreation(bool value)
236 if (value != m_manualSurfaceCreation) {
237 m_manualSurfaceCreation = value;
238 Q_EMIT manualSurfaceCreationChanged(value);
240 if (m_manualSurfaceCreation && m_surfaceCreationTimer.isActive()) {
241 m_surfaceCreationTimer.stop();
242 }
243 }
246Qt::ScreenOrientations ApplicationInfo::supportedOrientations() const
248 return m_supportedOrientations;
251void ApplicationInfo::setSupportedOrientations(Qt::ScreenOrientations orientations)
253 m_supportedOrientations = orientations;
256bool ApplicationInfo::rotatesWindowContents() const
258 return m_rotatesWindowContents;
261void ApplicationInfo::setRotatesWindowContents(bool value)
263 m_rotatesWindowContents = value;
266ApplicationInfo::RequestedState ApplicationInfo::requestedState() const
268 return m_requestedState;
271void ApplicationInfo::setRequestedState(RequestedState value)
273 if (m_requestedState == value) {
274 return;
275 }
276 DEBUG_MSG((value == RequestedRunning ? "RequestedRunning" : "RequestedSuspended") );
278 m_requestedState = value;
279 Q_EMIT requestedStateChanged(m_requestedState);
281 if (m_requestedState == RequestedRunning) {
283 if (m_state == Suspended) {
284 Q_ASSERT(m_liveSurfaceCount > 0);
285 setState(Running);
286 } else if (m_state == Stopped) {
287 Q_ASSERT(m_liveSurfaceCount == 0);
288 // it's restarting
289 setState(Starting);
290 }
292 } else if (m_requestedState == RequestedSuspended && m_state == Running
293 && m_closingSurfaces.isEmpty()) {
294 setState(Suspended);
295 }
298bool ApplicationInfo::isTouchApp() const
300 return m_isTouchApp;
303void ApplicationInfo::setIsTouchApp(bool isTouchApp)
305 m_isTouchApp = isTouchApp;
308bool ApplicationInfo::exemptFromLifecycle() const
310 return m_exemptFromLifecycle;
313void ApplicationInfo::setExemptFromLifecycle(bool exemptFromLifecycle)
315 if (m_exemptFromLifecycle != exemptFromLifecycle)
316 {
317 m_exemptFromLifecycle = exemptFromLifecycle;
318 Q_EMIT exemptFromLifecycleChanged(m_exemptFromLifecycle);
319 }
322QSize ApplicationInfo::initialSurfaceSize() const
324 return m_initialSurfaceSize;
327void ApplicationInfo::setInitialSurfaceSize(const QSize &size)
329 if (size != m_initialSurfaceSize) {
330 m_initialSurfaceSize = size;
331 Q_EMIT initialSurfaceSizeChanged(m_initialSurfaceSize);
332 }
335void ApplicationInfo::setShellChrome(Mir::ShellChrome shellChrome)
337 m_shellChrome = shellChrome;
338 if (m_surfaceList->rowCount() > 0) {
339 static_cast<MirSurface*>(m_surfaceList->get(0))->setShellChrome(shellChrome);
340 }
343bool ApplicationInfo::focused() const
345 bool someSurfaceHasFocus = false; // to be proven wrong
346 for (int i = 0; i < m_surfaceList->count() && !someSurfaceHasFocus; ++i) {
347 someSurfaceHasFocus = m_surfaceList->get(i)->focused();
348 }
349 return someSurfaceHasFocus;
352void ApplicationInfo::setFocused(bool value)
354 if (focused() == value) {
355 return;
356 }
358 if (value) {
359 if (m_surfaceList->count() > 0) {
360 m_surfaceList->get(0)->requestFocus();
361 }
362 } else {
363 for (int i = 0; i < m_surfaceList->count(); ++i) {
364 MirSurface *surface = static_cast<MirSurface*>(m_surfaceList->get(i));
365 if (surface->focused()) {
366 surface->setFocused(false);
367 }
368 }
369 }
372void ApplicationInfo::onSurfaceCountChanged()
374 if (m_surfaceList->count() == 0 && m_state == Running) {
375 setState(Stopped);
376 }
379void ApplicationInfo::requestFocus()
381 if (m_surfaceList->count() == 0) {
382 Q_EMIT focusRequested();
383 } else {
384 m_surfaceList->get(0)->requestFocus();
385 }