on ded5363 help sign in
unity8/tests/mocks/Unity/Application/ApplicationInfo.cpp
423 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);
103 surfaceManager->activate(surface);
106void ApplicationInfo::createSurface()
108 if (state() == ApplicationInfo::Stopped) { return; }
110 QString surfaceName = name() + "s window title";
111 if (m_surfaceList->count() > 0) {
112 surfaceName.append(QString(" %1").arg(m_surfaceList->count()+1));
113 }
115 auto surfaceManager = SurfaceManager::instance();
116 if (!surfaceManager) {
117 WARNING_MSG("No SurfaceManager");
118 return;
119 }
121 bool wasFocused = focused();
123 auto surface = surfaceManager->createSurface(surfaceName,
124 Mir::NormalType,
125 fullscreen() ? Mir::FullscreenState : Mir::RestoredState,
126 nullptr, /* parentSurface */
127 m_screenshotFileName,
128 m_qmlFilePath);
130 surface->setShellChrome(m_shellChrome);
132 m_surfaceList->addSurface(surface);
134 ++m_liveSurfaceCount;
135 connect(surface, &MirSurface::liveChanged, this, [this, surface](){
136 if (!surface->live()) {
137 --m_liveSurfaceCount;
138 if (m_liveSurfaceCount == 0) {
139 if (m_closingSurfaces.contains(surface)
140 || (m_state == Running && m_requestedState == RequestedRunning)) {
141 Q_EMIT closed();
142 }
143 setState(Stopped);
144 } else {
145 if (m_closingSurfaces.contains(surface) && m_requestedState == RequestedSuspended
146 && m_closingSurfaces.count() == 1) {
147 setState(Suspended);
148 }
149 }
150 m_closingSurfaces.removeAll(surface);
151 }
152 });
153 connect(surface, &MirSurface::closeRequested, this, [this, surface](){
154 m_closingSurfaces.append(surface);
155 if (m_state == Suspended) {
156 // resume to allow application to close its surface
157 setState(Running);
158 }
159 });
160 connect(surface, &MirSurfaceInterface::focusedChanged, this, [&](bool /*value*/) {
161 #if APPLICATION_DEBUG
162 qDebug().nospace() << "Application[" << appId() << "].focusedChanged(" << focused() << ")";
163 #endif
164 Q_EMIT focusedChanged(focused());
165 });
167 connect(surface, &MirSurface::focusRequested, this, &ApplicationInfo::focusRequested);
169 if (wasFocused != focused()) {
170 Q_EMIT focusedChanged(focused());
171 }
173 if (m_state == Starting) {
174 if (m_requestedState == RequestedRunning) {
175 setState(Running);
176 } else {
177 setState(Suspended);
178 }
179 }
181 surfaceManager->notifySurfaceCreated(surface);
182 surfaceManager->activate(surface);
185void ApplicationInfo::setIconId(const QString &iconId)
187 setIcon(QUrl(testDataDir() + "/graphics/applicationIcons/" + iconId + "@18.png"));
190void ApplicationInfo::setScreenshotId(const QString &screenshotId)
192 QString screenshotFileName;
194 if (screenshotId.endsWith(".svg")) {
195 screenshotFileName = QString("qrc:///Unity/Application/screenshots/%2")
196 .arg(screenshotId);
197 } else {
198 screenshotFileName = QString("qrc:///Unity/Application/screenshots/%2@12.png")
199 .arg(screenshotId);
200 }
202 if (screenshotFileName != m_screenshotFileName) {
203 m_screenshotFileName = screenshotFileName;
204 }
207void ApplicationInfo::setQmlFilename(const QString &qmlFilename)
209 m_qmlFilePath = QString("qrc:///Unity/Application/%1").arg(qmlFilename);
212void ApplicationInfo::setName(const QString &value)
214 if (value != m_name) {
215 m_name = value;
216 Q_EMIT nameChanged(value);
217 }
220void ApplicationInfo::setIcon(const QUrl &value)
222 if (value != m_icon) {
223 m_icon = value;
224 Q_EMIT iconChanged(value);
225 }
228void ApplicationInfo::setState(State value)
230 if (value != m_state) {
231 DEBUG_MSG(qPrintable(stateToStr(value)));
232 if (!m_manualSurfaceCreation && value == ApplicationInfo::Starting) {
233 Q_ASSERT(m_surfaceList->count() == 0);
234 m_surfaceCreationTimer.start();
235 } else if (value == ApplicationInfo::Stopped) {
236 m_surfaceCreationTimer.stop();
237 for (int i = 0; i < m_surfaceList->count(); ++i) {
238 MirSurface *surface = static_cast<MirSurface*>(m_surfaceList->get(i));
239 surface->setLive(false);
240 }
241 for (int i = 0; i < m_promptSurfaceList->count(); ++i) {
242 auto surface = static_cast<MirSurface*>(m_promptSurfaceList->get(i));
243 surface->setLive(false);
244 }
245 }
247 m_state = value;
248 Q_EMIT stateChanged(value);
249 }
252void ApplicationInfo::close()
254 DEBUG_MSG("");
256 if (m_surfaceList->count() > 0) {
257 for (int i = 0; i < m_surfaceList->count(); ++i) {
258 MirSurface *surface = static_cast<MirSurface*>(m_surfaceList->get(i));
259 surface->close();
260 }
261 } else {
262 setState(Stopped);
263 Q_EMIT closed();
264 }
267void ApplicationInfo::setFullscreen(bool value)
269 m_fullscreen = value;
270 if (m_surfaceList->rowCount() > 0) {
271 Mir::State newState;
272 if (value) {
273 newState = Mir::FullscreenState;
274 } else {
275 newState = Mir::RestoredState;
276 }
277 m_surfaceList->get(0)->requestState(newState);
278 }
281bool ApplicationInfo::fullscreen() const
283 if (m_surfaceList->rowCount() > 0) {
284 return m_surfaceList->get(0)->state() == Mir::FullscreenState;
285 } else {
286 return m_fullscreen;
287 }
290void ApplicationInfo::setManualSurfaceCreation(bool value)
292 if (value != m_manualSurfaceCreation) {
293 m_manualSurfaceCreation = value;
294 Q_EMIT manualSurfaceCreationChanged(value);
296 if (m_manualSurfaceCreation && m_surfaceCreationTimer.isActive()) {
297 m_surfaceCreationTimer.stop();
298 }
299 }
302Qt::ScreenOrientations ApplicationInfo::supportedOrientations() const
304 return m_supportedOrientations;
307void ApplicationInfo::setSupportedOrientations(Qt::ScreenOrientations orientations)
309 m_supportedOrientations = orientations;
312bool ApplicationInfo::rotatesWindowContents() const
314 return m_rotatesWindowContents;
317void ApplicationInfo::setRotatesWindowContents(bool value)
319 m_rotatesWindowContents = value;
322ApplicationInfo::RequestedState ApplicationInfo::requestedState() const
324 return m_requestedState;
327void ApplicationInfo::setRequestedState(RequestedState value)
329 if (m_requestedState == value) {
330 return;
331 }
332 DEBUG_MSG((value == RequestedRunning ? "RequestedRunning" : "RequestedSuspended") );
334 m_requestedState = value;
335 Q_EMIT requestedStateChanged(m_requestedState);
337 if (m_requestedState == RequestedRunning) {
339 if (m_state == Suspended) {
340 Q_ASSERT(m_liveSurfaceCount > 0);
341 setState(Running);
342 } else if (m_state == Stopped) {
343 Q_ASSERT(m_liveSurfaceCount == 0);
344 // it's restarting
345 setState(Starting);
346 }
348 } else if (m_requestedState == RequestedSuspended && m_state == Running
349 && m_closingSurfaces.isEmpty()) {
350 setState(Suspended);
351 }
354bool ApplicationInfo::isTouchApp() const
356 return m_isTouchApp;
359void ApplicationInfo::setIsTouchApp(bool isTouchApp)
361 m_isTouchApp = isTouchApp;
364bool ApplicationInfo::exemptFromLifecycle() const
366 return m_exemptFromLifecycle;
369void ApplicationInfo::setExemptFromLifecycle(bool exemptFromLifecycle)
371 if (m_exemptFromLifecycle != exemptFromLifecycle)
372 {
373 m_exemptFromLifecycle = exemptFromLifecycle;
374 Q_EMIT exemptFromLifecycleChanged(m_exemptFromLifecycle);
375 }
378QSize ApplicationInfo::initialSurfaceSize() const
380 return m_initialSurfaceSize;
383void ApplicationInfo::setInitialSurfaceSize(const QSize &size)
385 if (size != m_initialSurfaceSize) {
386 m_initialSurfaceSize = size;
387 Q_EMIT initialSurfaceSizeChanged(m_initialSurfaceSize);
388 }
391void ApplicationInfo::setShellChrome(Mir::ShellChrome shellChrome)
393 m_shellChrome = shellChrome;
394 if (m_surfaceList->rowCount() > 0) {
395 static_cast<MirSurface*>(m_surfaceList->get(0))->setShellChrome(shellChrome);
396 }
399bool ApplicationInfo::focused() const
401 bool someSurfaceHasFocus = false; // to be proven wrong
402 for (int i = 0; i < m_surfaceList->count() && !someSurfaceHasFocus; ++i) {
403 someSurfaceHasFocus = m_surfaceList->get(i)->focused();
404 }
405 return someSurfaceHasFocus;
408void ApplicationInfo::onSurfaceCountChanged()
410 if (m_surfaceList->count() == 0 && m_state == Running) {
411 setState(Stopped);
412 }
415void ApplicationInfo::requestFocus()
417 if (m_surfaceList->count() == 0) {
418 Q_EMIT focusRequested();
419 } else {
420 auto surface = static_cast<MirSurface*>(m_surfaceList->get(0));
421 surface->requestFocus();
422 }