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