on c250d36 help sign in
unity8/src/ShellApplication.cpp
213 lines 7.8 kB history raw
1/*
2 * Copyright (C) 2015 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 "ShellApplication.h"
19// Qt
20#include <QLibrary>
21#include <QProcess>
22#include <QScreen>
24#include <QGSettings>
26#include <libintl.h>
28// libandroid-properties
29#include <hybris/properties/properties.h>
31// local
32#include <paths.h>
33#include "CachingNetworkManagerFactory.h"
34#include "UnityCommandLineParser.h"
35#include "DebuggingController.h"
37ShellApplication::ShellApplication(int & argc, char ** argv, bool isMirServer)
38 : QGuiApplication(argc, argv)
40 setApplicationName(QStringLiteral("unity8"));
41 setOrganizationName(QStringLiteral("Canonical"));
43 connect(this, &QGuiApplication::screenAdded, this, &ShellApplication::onScreenAdded);
44 connect(this, &QGuiApplication::screenRemoved, this, &ShellApplication::onScreenRemoved);
46 setupQmlEngine(isMirServer);
48 UnityCommandLineParser parser(*this);
50 m_qmlArgs.setMode(parser.mode());
52 // The testability driver is only loaded by QApplication but not by QGuiApplication.
53 // However, QApplication depends on QWidget which would add some unneeded overhead => Let's load the testability driver on our own.
54 if (parser.hasTestability() || getenv("QT_LOAD_TESTABILITY")) {
55 QLibrary testLib(QStringLiteral("qttestability"));
56 if (testLib.load()) {
57 typedef void (*TasInitialize)(void);
58 TasInitialize initFunction = (TasInitialize)testLib.resolve("qt_testability_init");
59 if (initFunction) {
60 initFunction();
61 } else {
62 qCritical("Library qttestability resolve failed!");
63 }
64 } else {
65 qCritical("Library qttestability load failed!");
66 }
67 }
69 bindtextdomain("unity8", translationDirectory().toUtf8().data());
70 textdomain("unity8");
72 QScopedPointer<QGSettings> gSettings(new QGSettings("com.canonical.Unity8"));
73 gSettings->reset(QStringLiteral("alwaysShowOsk"));
75 m_shellView = new ShellView(m_qmlEngine, &m_qmlArgs);
77 if (parser.windowGeometry().isValid()) {
78 m_shellView->setWidth(parser.windowGeometry().width());
79 m_shellView->setHeight(parser.windowGeometry().height());
80 }
82 if (parser.hasFrameless()) {
83 m_shellView->setFlags(Qt::FramelessWindowHint);
84 }
87 #ifdef UNITY8_ENABLE_TOUCH_EMULATION
88 // You will need this if you want to interact with touch-only components using a mouse
89 // Needed only when manually testing on a desktop.
90 if (parser.hasMouseToTouch()) {
91 m_mouseTouchAdaptor = MouseTouchAdaptor::instance();
92 }
93 #endif
95 new DebuggingController(this);
97 // Some hard-coded policy for now.
98 // NB: We don't support more than two screens at the moment
99 //
100 // TODO: Support an arbitrary number of screens and different policies
101 // (eg cloned desktop, several desktops, etc)
102 if (isMirServer && screens().count() == 2) {
103 m_shellView->setScreen(screens().at(1));
105 m_secondaryWindow = new SecondaryWindow(m_qmlEngine);
106 m_secondaryWindow->setScreen(screens().at(0));
107 // QWindow::showFullScreen() also calls QWindow::requestActivate() and we don't want that!
108 m_secondaryWindow->setWindowState(Qt::WindowFullScreen);
109 m_secondaryWindow->setVisible(true);
110 }
112 if (parser.mode().compare("greeter") == 0) {
113 QSize primaryScreenSize = this->primaryScreen()->size();
114 m_shellView->setHeight(primaryScreenSize.height());
115 m_shellView->setWidth(primaryScreenSize.width());
116 m_shellView->show();
117 m_shellView->requestActivate();
118 if (!QProcess::startDetached("initctl emit --no-wait unity8-greeter-started")) {
119 qDebug() << "Unable to send unity8-greeter-started event to Upstart";
120 }
121 } else if (isMirServer || parser.hasFullscreen()) {
122 m_shellView->showFullScreen();
123 } else {
124 m_shellView->show();
125 }
128ShellApplication::~ShellApplication()
130 destroyResources();
133void ShellApplication::destroyResources()
135 // Deletion order is important. Don't use QScopedPointers and the like
136 // Otherwise the process will hang on shutdown (bug somewhere I guess).
137 delete m_shellView;
138 m_shellView = nullptr;
140 delete m_secondaryWindow;
141 m_secondaryWindow = nullptr;
143 #ifdef UNITY8_ENABLE_TOUCH_EMULATION
144 delete m_mouseTouchAdaptor;
145 m_mouseTouchAdaptor = nullptr;
146 #endif
148 delete m_qmlEngine;
149 m_qmlEngine = nullptr;
152void ShellApplication::setupQmlEngine(bool isMirServer)
154 m_qmlEngine = new QQmlEngine(this);
156 m_qmlEngine->setBaseUrl(QUrl::fromLocalFile(::qmlDirectory()));
158 prependImportPaths(m_qmlEngine, ::overrideImportPaths());
159 if (!isMirServer) {
160 prependImportPaths(m_qmlEngine, ::nonMirImportPaths());
161 }
162 appendImportPaths(m_qmlEngine, ::fallbackImportPaths());
164 m_qmlEngine->setNetworkAccessManagerFactory(new CachingNetworkManagerFactory);
166 QObject::connect(m_qmlEngine, &QQmlEngine::quit, this, &QGuiApplication::quit);
169void ShellApplication::onScreenAdded(QScreen * /*screen*/)
171 // TODO: Support an arbitrary number of screens and different policies
172 // (eg cloned desktop, several desktops, etc)
173 if (screens().count() == 2) {
174 m_shellView->setScreen(screens().at(1));
175 // QWindow::destroy() is called when it changes between screens. We have to manually make it visible again
176 // <dandrader> This bug is supposedly fixed in Qt 5.5.1, although I can still reproduce it there. :-/
177 m_shellView->setVisible(true);
179 m_secondaryWindow = new SecondaryWindow(m_qmlEngine);
180 m_secondaryWindow->setScreen(screens().at(0));
182 // QWindow::showFullScreen() also calls QWindow::requestActivate() and we don't want that!
183 m_secondaryWindow->setWindowState(Qt::WindowFullScreen);
184 m_secondaryWindow->setVisible(true);
186 // Changing the QScreen where a QWindow is drawn makes it also lose focus (besides having
187 // its backing QPlatformWindow recreated). So lets refocus it.
188 // This must be done after the creation of the SecondaryWindow, else
189 // the SecondaryWindow takes focus anyway.
190 m_shellView->requestActivate();
191 }
194void ShellApplication::onScreenRemoved(QScreen *screen)
196 // TODO: Support an arbitrary number of screens and different policies
197 // (eg cloned desktop, several desktops, etc)
198 if (screen == m_shellView->screen()) {
199 const QList<QScreen *> allScreens = screens();
200 Q_ASSERT(allScreens.count() > 1);
201 Q_ASSERT(allScreens.at(0) != screen);
202 Q_ASSERT(m_secondaryWindow);
203 delete m_secondaryWindow;
204 m_secondaryWindow = nullptr;
205 m_shellView->setScreen(allScreens.first());
206 // Changing the QScreen where a QWindow is drawn makes it also lose focus (besides having
207 // its backing QPlatformWindow recreated). So lets refocus it.
208 m_shellView->requestActivate();
209 // QWindow::destroy() is called when it changes between screens. We have to manually make it visible again
210 // <dandrader> This bug is supposedly fixed in Qt 5.5.1, although I can still reproduce it there. :-/
211 m_shellView->setVisible(true);
212 }