on 6e36a85 help sign in
unity8/src/ShellApplication.cpp
231 lines 8.5 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 if (!parser.deviceName().isEmpty()) {
51 m_deviceName = parser.deviceName();
52 } else {
53 char buffer[200];
54 property_get("ro.product.device", buffer /* value */, "desktop" /* default_value*/);
55 m_deviceName = QString(buffer);
56 }
57 m_qmlArgs.setDeviceName(m_deviceName);
59 m_qmlArgs.setMode(parser.mode());
61 {
62 char buffer[200];
63 property_get("ubuntu.unity8.interactive_blur", buffer, "false");
64 m_qmlArgs.setInteractiveBlur(QString(buffer) == QStringLiteral("true"));
65 }
67 // The testability driver is only loaded by QApplication but not by QGuiApplication.
68 // However, QApplication depends on QWidget which would add some unneeded overhead => Let's load the testability driver on our own.
69 if (parser.hasTestability() || getenv("QT_LOAD_TESTABILITY")) {
70 QLibrary testLib(QStringLiteral("qttestability"));
71 if (testLib.load()) {
72 typedef void (*TasInitialize)(void);
73 TasInitialize initFunction = (TasInitialize)testLib.resolve("qt_testability_init");
74 if (initFunction) {
75 initFunction();
76 } else {
77 qCritical("Library qttestability resolve failed!");
78 }
79 } else {
80 qCritical("Library qttestability load failed!");
81 }
82 }
84 bindtextdomain("unity8", translationDirectory().toUtf8().data());
85 textdomain("unity8");
87 QScopedPointer<QGSettings> gSettings(new QGSettings("com.canonical.Unity8"));
88 gSettings->reset(QStringLiteral("alwaysShowOsk"));
90 m_shellView = new ShellView(m_qmlEngine, &m_qmlArgs);
92 if (parser.windowGeometry().isValid()) {
93 m_shellView->setWidth(parser.windowGeometry().width());
94 m_shellView->setHeight(parser.windowGeometry().height());
95 }
97 if (parser.hasFrameless()) {
98 m_shellView->setFlags(Qt::FramelessWindowHint);
99 }
102 #ifdef UNITY8_ENABLE_TOUCH_EMULATION
103 // You will need this if you want to interact with touch-only components using a mouse
104 // Needed only when manually testing on a desktop.
105 if (parser.hasMouseToTouch()) {
106 m_mouseTouchAdaptor = MouseTouchAdaptor::instance();
107 }
108 #endif
110 new DebuggingController(this);
112 // Some hard-coded policy for now.
113 // NB: We don't support more than two screens at the moment
114 //
115 // TODO: Support an arbitrary number of screens and different policies
116 // (eg cloned desktop, several desktops, etc)
117 if (isMirServer && screens().count() == 2) {
118 m_shellView->setScreen(screens().at(1));
119 m_qmlArgs.setDeviceName(QStringLiteral("desktop"));
121 m_secondaryWindow = new SecondaryWindow(m_qmlEngine);
122 m_secondaryWindow->setScreen(screens().at(0));
123 // QWindow::showFullScreen() also calls QWindow::requestActivate() and we don't want that!
124 m_secondaryWindow->setWindowState(Qt::WindowFullScreen);
125 m_secondaryWindow->setVisible(true);
126 }
128 if (parser.mode().compare("greeter") == 0) {
129 QSize primaryScreenSize = this->primaryScreen()->size();
130 m_shellView->setHeight(primaryScreenSize.height());
131 m_shellView->setWidth(primaryScreenSize.width());
132 m_shellView->show();
133 m_shellView->requestActivate();
134 if (!QProcess::startDetached("initctl emit --no-wait unity8-greeter-started")) {
135 qDebug() << "Unable to send unity8-greeter-started event to Upstart";
136 }
137 } else if (isMirServer || parser.hasFullscreen()) {
138 m_shellView->showFullScreen();
139 } else {
140 m_shellView->show();
141 }
144ShellApplication::~ShellApplication()
146 destroyResources();
149void ShellApplication::destroyResources()
151 // Deletion order is important. Don't use QScopedPointers and the like
152 // Otherwise the process will hang on shutdown (bug somewhere I guess).
153 delete m_shellView;
154 m_shellView = nullptr;
156 delete m_secondaryWindow;
157 m_secondaryWindow = nullptr;
159 #ifdef UNITY8_ENABLE_TOUCH_EMULATION
160 delete m_mouseTouchAdaptor;
161 m_mouseTouchAdaptor = nullptr;
162 #endif
164 delete m_qmlEngine;
165 m_qmlEngine = nullptr;
168void ShellApplication::setupQmlEngine(bool isMirServer)
170 m_qmlEngine = new QQmlEngine(this);
172 m_qmlEngine->setBaseUrl(QUrl::fromLocalFile(::qmlDirectory()));
174 prependImportPaths(m_qmlEngine, ::overrideImportPaths());
175 if (!isMirServer) {
176 prependImportPaths(m_qmlEngine, ::nonMirImportPaths());
177 }
178 appendImportPaths(m_qmlEngine, ::fallbackImportPaths());
180 m_qmlEngine->setNetworkAccessManagerFactory(new CachingNetworkManagerFactory);
182 QObject::connect(m_qmlEngine, &QQmlEngine::quit, this, &QGuiApplication::quit);
185void ShellApplication::onScreenAdded(QScreen * /*screen*/)
187 // TODO: Support an arbitrary number of screens and different policies
188 // (eg cloned desktop, several desktops, etc)
189 if (screens().count() == 2) {
190 m_shellView->setScreen(screens().at(1));
191 m_qmlArgs.setDeviceName(QStringLiteral("desktop"));
192 // QWindow::destroy() is called when it changes between screens. We have to manually make it visible again
193 // <dandrader> This bug is supposedly fixed in Qt 5.5.1, although I can still reproduce it there. :-/
194 m_shellView->setVisible(true);
196 m_secondaryWindow = new SecondaryWindow(m_qmlEngine);
197 m_secondaryWindow->setScreen(screens().at(0));
199 // QWindow::showFullScreen() also calls QWindow::requestActivate() and we don't want that!
200 m_secondaryWindow->setWindowState(Qt::WindowFullScreen);
201 m_secondaryWindow->setVisible(true);
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 // This must be done after the creation of the SecondaryWindow, else
206 // the SecondaryWindow takes focus anyway.
207 m_shellView->requestActivate();
208 }
211void ShellApplication::onScreenRemoved(QScreen *screen)
213 // TODO: Support an arbitrary number of screens and different policies
214 // (eg cloned desktop, several desktops, etc)
215 if (screen == m_shellView->screen()) {
216 const QList<QScreen *> allScreens = screens();
217 Q_ASSERT(allScreens.count() > 1);
218 Q_ASSERT(allScreens.at(0) != screen);
219 Q_ASSERT(m_secondaryWindow);
220 delete m_secondaryWindow;
221 m_secondaryWindow = nullptr;
222 m_shellView->setScreen(allScreens.first());
223 m_qmlArgs.setDeviceName(m_deviceName);
224 // Changing the QScreen where a QWindow is drawn makes it also lose focus (besides having
225 // its backing QPlatformWindow recreated). So lets refocus it.
226 m_shellView->requestActivate();
227 // QWindow::destroy() is called when it changes between screens. We have to manually make it visible again
228 // <dandrader> This bug is supposedly fixed in Qt 5.5.1, although I can still reproduce it there. :-/
229 m_shellView->setVisible(true);
230 }