on bionic help sign in
unity8/tests/mocks/Unity/Application/ApplicationInfo.cpp
417 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 m_surfaceList->get(0)->requestState(Mir::FullscreenState);
272 }
275bool ApplicationInfo::fullscreen() const
277 if (m_surfaceList->rowCount() > 0) {
278 return m_surfaceList->get(0)->state() == Mir::FullscreenState;
279 } else {
280 return m_fullscreen;
281 }
284void ApplicationInfo::setManualSurfaceCreation(bool value)
286 if (value != m_manualSurfaceCreation) {
287 m_manualSurfaceCreation = value;
288 Q_EMIT manualSurfaceCreationChanged(value);
290 if (m_manualSurfaceCreation && m_surfaceCreationTimer.isActive()) {
291 m_surfaceCreationTimer.stop();
292 }
293 }
296Qt::ScreenOrientations ApplicationInfo::supportedOrientations() const
298 return m_supportedOrientations;
301void ApplicationInfo::setSupportedOrientations(Qt::ScreenOrientations orientations)
303 m_supportedOrientations = orientations;
306bool ApplicationInfo::rotatesWindowContents() const
308 return m_rotatesWindowContents;
311void ApplicationInfo::setRotatesWindowContents(bool value)
313 m_rotatesWindowContents = value;
316ApplicationInfo::RequestedState ApplicationInfo::requestedState() const
318 return m_requestedState;
321void ApplicationInfo::setRequestedState(RequestedState value)
323 if (m_requestedState == value) {
324 return;
325 }
326 DEBUG_MSG((value == RequestedRunning ? "RequestedRunning" : "RequestedSuspended") );
328 m_requestedState = value;
329 Q_EMIT requestedStateChanged(m_requestedState);
331 if (m_requestedState == RequestedRunning) {
333 if (m_state == Suspended) {
334 Q_ASSERT(m_liveSurfaceCount > 0);
335 setState(Running);
336 } else if (m_state == Stopped) {
337 Q_ASSERT(m_liveSurfaceCount == 0);
338 // it's restarting
339 setState(Starting);
340 }
342 } else if (m_requestedState == RequestedSuspended && m_state == Running
343 && m_closingSurfaces.isEmpty()) {
344 setState(Suspended);
345 }
348bool ApplicationInfo::isTouchApp() const
350 return m_isTouchApp;
353void ApplicationInfo::setIsTouchApp(bool isTouchApp)
355 m_isTouchApp = isTouchApp;
358bool ApplicationInfo::exemptFromLifecycle() const
360 return m_exemptFromLifecycle;
363void ApplicationInfo::setExemptFromLifecycle(bool exemptFromLifecycle)
365 if (m_exemptFromLifecycle != exemptFromLifecycle)
366 {
367 m_exemptFromLifecycle = exemptFromLifecycle;
368 Q_EMIT exemptFromLifecycleChanged(m_exemptFromLifecycle);
369 }
372QSize ApplicationInfo::initialSurfaceSize() const
374 return m_initialSurfaceSize;
377void ApplicationInfo::setInitialSurfaceSize(const QSize &size)
379 if (size != m_initialSurfaceSize) {
380 m_initialSurfaceSize = size;
381 Q_EMIT initialSurfaceSizeChanged(m_initialSurfaceSize);
382 }
385void ApplicationInfo::setShellChrome(Mir::ShellChrome shellChrome)
387 m_shellChrome = shellChrome;
388 if (m_surfaceList->rowCount() > 0) {
389 static_cast<MirSurface*>(m_surfaceList->get(0))->setShellChrome(shellChrome);
390 }
393bool ApplicationInfo::focused() const
395 bool someSurfaceHasFocus = false; // to be proven wrong
396 for (int i = 0; i < m_surfaceList->count() && !someSurfaceHasFocus; ++i) {
397 someSurfaceHasFocus = m_surfaceList->get(i)->focused();
398 }
399 return someSurfaceHasFocus;
402void ApplicationInfo::onSurfaceCountChanged()
404 if (m_surfaceList->count() == 0 && m_state == Running) {
405 setState(Stopped);
406 }
409void ApplicationInfo::requestFocus()
411 if (m_surfaceList->count() == 0) {
412 Q_EMIT focusRequested();
413 } else {
414 auto surface = static_cast<MirSurface*>(m_surfaceList->get(0));
415 surface->requestFocus();
416 }