on e4400a2 help sign in
unity8/tests/mocks/Unity/Application/ApplicationInfo.cpp
409 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 screenshotUrl);
98 surfaceManager->notifySurfaceCreated(surface);
100 m_promptSurfaceList->addSurface(surface);
102 surfaceManager->activate(surface);
105void ApplicationInfo::createSurface()
107 if (state() == ApplicationInfo::Stopped) { return; }
109 QString surfaceName = name() + "s window title";
110 if (m_surfaceList->count() > 0) {
111 surfaceName.append(QString(" %1").arg(m_surfaceList->count()+1));
112 }
114 auto surfaceManager = SurfaceManager::instance();
115 if (!surfaceManager) {
116 WARNING_MSG("No SurfaceManager");
117 return;
118 }
120 bool wasFocused = focused();
122 auto surface = surfaceManager->createSurface(surfaceName,
123 Mir::NormalType,
124 fullscreen() ? Mir::FullscreenState : Mir::RestoredState,
125 m_screenshotFileName);
127 surface->setShellChrome(m_shellChrome);
129 m_surfaceList->addSurface(surface);
131 ++m_liveSurfaceCount;
132 connect(surface, &MirSurface::liveChanged, this, [this, surface](){
133 if (!surface->live()) {
134 --m_liveSurfaceCount;
135 if (m_liveSurfaceCount == 0) {
136 if (m_closingSurfaces.contains(surface)
137 || (m_state == Running && m_requestedState == RequestedRunning)) {
138 Q_EMIT closed();
139 }
140 setState(Stopped);
141 } else {
142 if (m_closingSurfaces.contains(surface) && m_requestedState == RequestedSuspended
143 && m_closingSurfaces.count() == 1) {
144 setState(Suspended);
145 }
146 }
147 m_closingSurfaces.removeAll(surface);
148 }
149 });
150 connect(surface, &MirSurface::closeRequested, this, [this, surface](){
151 m_closingSurfaces.append(surface);
152 if (m_state == Suspended) {
153 // resume to allow application to close its surface
154 setState(Running);
155 }
156 });
157 connect(surface, &MirSurfaceInterface::focusedChanged, this, [&](bool /*value*/) {
158 #if APPLICATION_DEBUG
159 qDebug().nospace() << "Application[" << appId() << "].focusedChanged(" << focused() << ")";
160 #endif
161 Q_EMIT focusedChanged(focused());
162 });
164 connect(surface, &MirSurface::focusRequested, this, &ApplicationInfo::focusRequested);
166 if (wasFocused != focused()) {
167 Q_EMIT focusedChanged(focused());
168 }
170 if (m_state == Starting) {
171 if (m_requestedState == RequestedRunning) {
172 setState(Running);
173 } else {
174 setState(Suspended);
175 }
176 }
178 surfaceManager->notifySurfaceCreated(surface);
179 surfaceManager->activate(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::setName(const QString &value)
206 if (value != m_name) {
207 m_name = value;
208 Q_EMIT nameChanged(value);
209 }
212void ApplicationInfo::setIcon(const QUrl &value)
214 if (value != m_icon) {
215 m_icon = value;
216 Q_EMIT iconChanged(value);
217 }
220void ApplicationInfo::setState(State value)
222 if (value != m_state) {
223 DEBUG_MSG(qPrintable(stateToStr(value)));
224 if (!m_manualSurfaceCreation && value == ApplicationInfo::Starting) {
225 Q_ASSERT(m_surfaceList->count() == 0);
226 m_surfaceCreationTimer.start();
227 } else if (value == ApplicationInfo::Stopped) {
228 m_surfaceCreationTimer.stop();
229 for (int i = 0; i < m_surfaceList->count(); ++i) {
230 MirSurface *surface = static_cast<MirSurface*>(m_surfaceList->get(i));
231 surface->setLive(false);
232 }
233 for (int i = 0; i < m_promptSurfaceList->count(); ++i) {
234 auto surface = static_cast<MirSurface*>(m_promptSurfaceList->get(i));
235 surface->setLive(false);
236 }
237 }
239 m_state = value;
240 Q_EMIT stateChanged(value);
241 }
244void ApplicationInfo::close()
246 DEBUG_MSG("");
248 if (m_surfaceList->count() > 0) {
249 for (int i = 0; i < m_surfaceList->count(); ++i) {
250 MirSurface *surface = static_cast<MirSurface*>(m_surfaceList->get(i));
251 surface->close();
252 }
253 } else {
254 setState(Stopped);
255 Q_EMIT closed();
256 }
259void ApplicationInfo::setFullscreen(bool value)
261 m_fullscreen = value;
262 if (m_surfaceList->rowCount() > 0) {
263 m_surfaceList->get(0)->requestState(Mir::FullscreenState);
264 }
267bool ApplicationInfo::fullscreen() const
269 if (m_surfaceList->rowCount() > 0) {
270 return m_surfaceList->get(0)->state() == Mir::FullscreenState;
271 } else {
272 return m_fullscreen;
273 }
276void ApplicationInfo::setManualSurfaceCreation(bool value)
278 if (value != m_manualSurfaceCreation) {
279 m_manualSurfaceCreation = value;
280 Q_EMIT manualSurfaceCreationChanged(value);
282 if (m_manualSurfaceCreation && m_surfaceCreationTimer.isActive()) {
283 m_surfaceCreationTimer.stop();
284 }
285 }
288Qt::ScreenOrientations ApplicationInfo::supportedOrientations() const
290 return m_supportedOrientations;
293void ApplicationInfo::setSupportedOrientations(Qt::ScreenOrientations orientations)
295 m_supportedOrientations = orientations;
298bool ApplicationInfo::rotatesWindowContents() const
300 return m_rotatesWindowContents;
303void ApplicationInfo::setRotatesWindowContents(bool value)
305 m_rotatesWindowContents = value;
308ApplicationInfo::RequestedState ApplicationInfo::requestedState() const
310 return m_requestedState;
313void ApplicationInfo::setRequestedState(RequestedState value)
315 if (m_requestedState == value) {
316 return;
317 }
318 DEBUG_MSG((value == RequestedRunning ? "RequestedRunning" : "RequestedSuspended") );
320 m_requestedState = value;
321 Q_EMIT requestedStateChanged(m_requestedState);
323 if (m_requestedState == RequestedRunning) {
325 if (m_state == Suspended) {
326 Q_ASSERT(m_liveSurfaceCount > 0);
327 setState(Running);
328 } else if (m_state == Stopped) {
329 Q_ASSERT(m_liveSurfaceCount == 0);
330 // it's restarting
331 setState(Starting);
332 }
334 } else if (m_requestedState == RequestedSuspended && m_state == Running
335 && m_closingSurfaces.isEmpty()) {
336 setState(Suspended);
337 }
340bool ApplicationInfo::isTouchApp() const
342 return m_isTouchApp;
345void ApplicationInfo::setIsTouchApp(bool isTouchApp)
347 m_isTouchApp = isTouchApp;
350bool ApplicationInfo::exemptFromLifecycle() const
352 return m_exemptFromLifecycle;
355void ApplicationInfo::setExemptFromLifecycle(bool exemptFromLifecycle)
357 if (m_exemptFromLifecycle != exemptFromLifecycle)
358 {
359 m_exemptFromLifecycle = exemptFromLifecycle;
360 Q_EMIT exemptFromLifecycleChanged(m_exemptFromLifecycle);
361 }
364QSize ApplicationInfo::initialSurfaceSize() const
366 return m_initialSurfaceSize;
369void ApplicationInfo::setInitialSurfaceSize(const QSize &size)
371 if (size != m_initialSurfaceSize) {
372 m_initialSurfaceSize = size;
373 Q_EMIT initialSurfaceSizeChanged(m_initialSurfaceSize);
374 }
377void ApplicationInfo::setShellChrome(Mir::ShellChrome shellChrome)
379 m_shellChrome = shellChrome;
380 if (m_surfaceList->rowCount() > 0) {
381 static_cast<MirSurface*>(m_surfaceList->get(0))->setShellChrome(shellChrome);
382 }
385bool ApplicationInfo::focused() const
387 bool someSurfaceHasFocus = false; // to be proven wrong
388 for (int i = 0; i < m_surfaceList->count() && !someSurfaceHasFocus; ++i) {
389 someSurfaceHasFocus = m_surfaceList->get(i)->focused();
390 }
391 return someSurfaceHasFocus;
394void ApplicationInfo::onSurfaceCountChanged()
396 if (m_surfaceList->count() == 0 && m_state == Running) {
397 setState(Stopped);
398 }
401void ApplicationInfo::requestFocus()
403 if (m_surfaceList->count() == 0) {
404 Q_EMIT focusRequested();
405 } else {
406 auto surface = static_cast<MirSurface*>(m_surfaceList->get(0));
407 surface->requestFocus();
408 }