on b0fa722 help sign in
unity8/tests/mocks/Unity/Application/ApplicationInfo.cpp
420 lines 12 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::createPromptSurface()
80 if (state() == ApplicationInfo::Stopped) { return; }
82 auto surfaceManager = SurfaceManager::instance();
83 if (!surfaceManager) {
84 WARNING_MSG("No SurfaceManager");
85 return;
86 }
88 QStringList screenshotIds = {"gallery", "map", "facebook", "camera", "browser", "music", "twitter"};
89 int i = rand() % screenshotIds.count();
91 QUrl screenshotUrl = QString("qrc:///Unity/Application/screenshots/%1@12.png")
92 .arg(screenshotIds[i]);
94 auto surface = surfaceManager->createSurface(QString("prompt foo"),
95 Mir::NormalType,
96 Mir::RestoredState,
97 nullptr, /* parentSurface */
98 screenshotUrl);
99 surfaceManager->notifySurfaceCreated(surface);
101 m_promptSurfaceList->addSurface(surface);
104void ApplicationInfo::createSurface()
106 if (state() == ApplicationInfo::Stopped) { return; }
108 QString surfaceName = name() + "s window title";
109 if (m_surfaceList->count() > 0) {
110 surfaceName.append(QString(" %1").arg(m_surfaceList->count()+1));
111 }
113 auto surfaceManager = SurfaceManager::instance();
114 if (!surfaceManager) {
115 WARNING_MSG("No SurfaceManager");
116 return;
117 }
119 bool wasFocused = focused();
121 auto surface = surfaceManager->createSurface(surfaceName,
122 Mir::NormalType,
123 fullscreen() ? Mir::FullscreenState : Mir::RestoredState,
124 nullptr, /* parentSurface */
125 m_screenshotFileName,
126 m_qmlFilePath);
128 surface->setShellChrome(m_shellChrome);
130 m_surfaceList->addSurface(surface);
132 ++m_liveSurfaceCount;
133 connect(surface, &MirSurface::liveChanged, this, [this, surface](){
134 if (!surface->live()) {
135 --m_liveSurfaceCount;
136 if (m_liveSurfaceCount == 0) {
137 if (m_closingSurfaces.contains(surface)
138 || (m_state == Running && m_requestedState == RequestedRunning)) {
139 Q_EMIT closed();
140 }
141 setState(Stopped);
142 } else {
143 if (m_closingSurfaces.contains(surface) && m_requestedState == RequestedSuspended
144 && m_closingSurfaces.count() == 1) {
145 setState(Suspended);
146 }
147 }
148 m_closingSurfaces.removeAll(surface);
149 }
150 });
151 connect(surface, &MirSurface::closeRequested, this, [this, surface](){
152 m_closingSurfaces.append(surface);
153 if (m_state == Suspended) {
154 // resume to allow application to close its surface
155 setState(Running);
156 }
157 });
158 connect(surface, &MirSurfaceInterface::focusedChanged, this, [&](bool /*value*/) {
159 #if APPLICATION_DEBUG
160 qDebug().nospace() << "Application[" << appId() << "].focusedChanged(" << focused() << ")";
161 #endif
162 Q_EMIT focusedChanged(focused());
163 });
165 connect(surface, &MirSurface::focusRequested, this, &ApplicationInfo::focusRequested);
167 if (wasFocused != focused()) {
168 Q_EMIT focusedChanged(focused());
169 }
171 if (m_state == Starting) {
172 if (m_requestedState == RequestedRunning) {
173 setState(Running);
174 } else {
175 setState(Suspended);
176 }
177 }
179 surfaceManager->notifySurfaceCreated(surface);
182void ApplicationInfo::setIconId(const QString &iconId)
184 setIcon(QUrl(testDataDir() + "/graphics/applicationIcons/" + iconId + "@18.png"));
187void ApplicationInfo::setScreenshotId(const QString &screenshotId)
189 QString screenshotFileName;
191 if (screenshotId.endsWith(".svg")) {
192 screenshotFileName = QString("qrc:///Unity/Application/screenshots/%2")
193 .arg(screenshotId);
194 } else {
195 screenshotFileName = QString("qrc:///Unity/Application/screenshots/%2@12.png")
196 .arg(screenshotId);
197 }
199 if (screenshotFileName != m_screenshotFileName) {
200 m_screenshotFileName = screenshotFileName;
201 }
204void ApplicationInfo::setQmlFilename(const QString &qmlFilename)
206 m_qmlFilePath = QString("qrc:///Unity/Application/%1").arg(qmlFilename);
209void ApplicationInfo::setName(const QString &value)
211 if (value != m_name) {
212 m_name = value;
213 Q_EMIT nameChanged(value);
214 }
217void ApplicationInfo::setIcon(const QUrl &value)
219 if (value != m_icon) {
220 m_icon = value;
221 Q_EMIT iconChanged(value);
222 }
225void ApplicationInfo::setState(State value)
227 if (value != m_state) {
228 DEBUG_MSG(qPrintable(stateToStr(value)));
229 if (!m_manualSurfaceCreation && value == ApplicationInfo::Starting) {
230 Q_ASSERT(m_surfaceList->count() == 0);
231 m_surfaceCreationTimer.start();
232 } else if (value == ApplicationInfo::Stopped) {
233 m_surfaceCreationTimer.stop();
234 for (int i = 0; i < m_surfaceList->count(); ++i) {
235 MirSurface *surface = static_cast<MirSurface*>(m_surfaceList->get(i));
236 surface->setLive(false);
237 }
238 for (int i = 0; i < m_promptSurfaceList->count(); ++i) {
239 auto surface = static_cast<MirSurface*>(m_promptSurfaceList->get(i));
240 surface->setLive(false);
241 }
242 }
244 m_state = value;
245 Q_EMIT stateChanged(value);
246 }
249void ApplicationInfo::close()
251 DEBUG_MSG("");
253 if (m_surfaceList->count() > 0) {
254 for (int i = 0; i < m_surfaceList->count(); ++i) {
255 MirSurface *surface = static_cast<MirSurface*>(m_surfaceList->get(i));
256 surface->close();
257 }
258 } else {
259 setState(Stopped);
260 Q_EMIT closed();
261 }
264void ApplicationInfo::setFullscreen(bool value)
266 m_fullscreen = value;
267 if (m_surfaceList->rowCount() > 0) {
268 Mir::State newState;
269 if (value) {
270 newState = Mir::FullscreenState;
271 } else {
272 newState = Mir::RestoredState;
273 }
274 m_surfaceList->get(0)->requestState(newState);
275 }
278bool ApplicationInfo::fullscreen() const
280 if (m_surfaceList->rowCount() > 0) {
281 return m_surfaceList->get(0)->state() == Mir::FullscreenState;
282 } else {
283 return m_fullscreen;
284 }
287void ApplicationInfo::setManualSurfaceCreation(bool value)
289 if (value != m_manualSurfaceCreation) {
290 m_manualSurfaceCreation = value;
291 Q_EMIT manualSurfaceCreationChanged(value);
293 if (m_manualSurfaceCreation && m_surfaceCreationTimer.isActive()) {
294 m_surfaceCreationTimer.stop();
295 }
296 }
299Qt::ScreenOrientations ApplicationInfo::supportedOrientations() const
301 return m_supportedOrientations;
304void ApplicationInfo::setSupportedOrientations(Qt::ScreenOrientations orientations)
306 m_supportedOrientations = orientations;
309bool ApplicationInfo::rotatesWindowContents() const
311 return m_rotatesWindowContents;
314void ApplicationInfo::setRotatesWindowContents(bool value)
316 m_rotatesWindowContents = value;
319ApplicationInfo::RequestedState ApplicationInfo::requestedState() const
321 return m_requestedState;
324void ApplicationInfo::setRequestedState(RequestedState value)
326 if (m_requestedState == value) {
327 return;
328 }
329 DEBUG_MSG((value == RequestedRunning ? "RequestedRunning" : "RequestedSuspended") );
331 m_requestedState = value;
332 Q_EMIT requestedStateChanged(m_requestedState);
334 if (m_requestedState == RequestedRunning) {
336 if (m_state == Suspended) {
337 Q_ASSERT(m_liveSurfaceCount > 0);
338 setState(Running);
339 } else if (m_state == Stopped) {
340 Q_ASSERT(m_liveSurfaceCount == 0);
341 // it's restarting
342 setState(Starting);
343 }
345 } else if (m_requestedState == RequestedSuspended && m_state == Running
346 && m_closingSurfaces.isEmpty()) {
347 setState(Suspended);
348 }
351bool ApplicationInfo::isTouchApp() const
353 return m_isTouchApp;
356void ApplicationInfo::setIsTouchApp(bool isTouchApp)
358 m_isTouchApp = isTouchApp;
361bool ApplicationInfo::exemptFromLifecycle() const
363 return m_exemptFromLifecycle;
366void ApplicationInfo::setExemptFromLifecycle(bool exemptFromLifecycle)
368 if (m_exemptFromLifecycle != exemptFromLifecycle)
369 {
370 m_exemptFromLifecycle = exemptFromLifecycle;
371 Q_EMIT exemptFromLifecycleChanged(m_exemptFromLifecycle);
372 }
375QSize ApplicationInfo::initialSurfaceSize() const
377 return m_initialSurfaceSize;
380void ApplicationInfo::setInitialSurfaceSize(const QSize &size)
382 if (size != m_initialSurfaceSize) {
383 m_initialSurfaceSize = size;
384 Q_EMIT initialSurfaceSizeChanged(m_initialSurfaceSize);
385 }
388void ApplicationInfo::setShellChrome(Mir::ShellChrome shellChrome)
390 m_shellChrome = shellChrome;
391 if (m_surfaceList->rowCount() > 0) {
392 static_cast<MirSurface*>(m_surfaceList->get(0))->setShellChrome(shellChrome);
393 }
396bool ApplicationInfo::focused() const
398 bool someSurfaceHasFocus = false; // to be proven wrong
399 for (int i = 0; i < m_surfaceList->count() && !someSurfaceHasFocus; ++i) {
400 someSurfaceHasFocus = m_surfaceList->get(i)->focused();
401 }
402 return someSurfaceHasFocus;
405void ApplicationInfo::onSurfaceCountChanged()
407 if (m_surfaceList->count() == 0 && m_state == Running) {
408 setState(Stopped);
409 }
412void ApplicationInfo::requestFocus()
414 if (m_surfaceList->count() == 0) {
415 Q_EMIT focusRequested();
416 } else {
417 auto surface = static_cast<MirSurface*>(m_surfaceList->get(0));
418 surface->requestFocus();
419 }