on ef98de7 help sign in
unity8/src/ShellApplication.cpp
206 lines 7.4 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 <QScreen>
23#include <libintl.h>
25// libandroid-properties
26#include <hybris/properties/properties.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)
38 setApplicationName(QStringLiteral("unity8"));
40 connect(this, &QGuiApplication::screenAdded, this, &ShellApplication::onScreenAdded);
42 setupQmlEngine(isMirServer);
44 UnityCommandLineParser parser(*this);
46 if (!parser.deviceName().isEmpty()) {
47 m_deviceName = parser.deviceName();
48 } else {
49 char buffer[200];
50 property_get("ro.product.device", buffer /* value */, "desktop" /* default_value*/);
51 m_deviceName = QString(buffer);
52 }
53 m_qmlArgs.setDeviceName(m_deviceName);
55 m_qmlArgs.setMode(parser.mode());
57 // The testability driver is only loaded by QApplication but not by QGuiApplication.
58 // However, QApplication depends on QWidget which would add some unneeded overhead => Let's load the testability driver on our own.
59 if (parser.hasTestability() || getenv("QT_LOAD_TESTABILITY")) {
60 QLibrary testLib(QStringLiteral("qttestability"));
61 if (testLib.load()) {
62 typedef void (*TasInitialize)(void);
63 TasInitialize initFunction = (TasInitialize)testLib.resolve("qt_testability_init");
64 if (initFunction) {
65 initFunction();
66 } else {
67 qCritical("Library qttestability resolve failed!");
68 }
69 } else {
70 qCritical("Library qttestability load failed!");
71 }
72 }
74 bindtextdomain("unity8", translationDirectory().toUtf8().data());
75 textdomain("unity8");
77 m_shellView = new ShellView(m_qmlEngine, &m_qmlArgs);
79 if (parser.windowGeometry().isValid()) {
80 m_shellView->setWidth(parser.windowGeometry().width());
81 m_shellView->setHeight(parser.windowGeometry().height());
82 }
84 if (parser.hasFrameless()) {
85 m_shellView->setFlags(Qt::FramelessWindowHint);
86 }
89 #ifdef UNITY8_ENABLE_TOUCH_EMULATION
90 // You will need this if you want to interact with touch-only components using a mouse
91 // Needed only when manually testing on a desktop.
92 if (parser.hasMouseToTouch()) {
93 m_mouseTouchAdaptor = MouseTouchAdaptor::instance();
94 }
95 #endif
97 new DebuggingController(this);
99 // Some hard-coded policy for now.
100 // NB: We don't support more than two screens at the moment
101 //
102 // TODO: Support an arbitrary number of screens and different policies
103 // (eg cloned desktop, several desktops, etc)
104 if (isMirServer && screens().count() == 2) {
105 m_shellView->setScreen(screens().at(1));
106 m_qmlArgs.setDeviceName(QStringLiteral("desktop"));
108 m_secondaryWindow = new SecondaryWindow(m_qmlEngine);
109 m_secondaryWindow->setScreen(screens().at(0));
110 // QWindow::showFullScreen() also calls QWindow::requestActivate() and we don't want that!
111 m_secondaryWindow->setWindowState(Qt::WindowFullScreen);
112 m_secondaryWindow->setVisible(true);
113 }
115 if (isMirServer || parser.hasFullscreen()) {
116 m_shellView->showFullScreen();
117 } else {
118 m_shellView->show();
119 }
122ShellApplication::~ShellApplication()
124 destroyResources();
127void ShellApplication::destroyResources()
129 // Deletion order is important. Don't use QScopedPointers and the like
130 // Otherwise the process will hang on shutdown (bug somewhere I guess).
131 delete m_shellView;
132 m_shellView = nullptr;
134 delete m_secondaryWindow;
135 m_secondaryWindow = nullptr;
137 #ifdef UNITY8_ENABLE_TOUCH_EMULATION
138 delete m_mouseTouchAdaptor;
139 m_mouseTouchAdaptor = nullptr;
140 #endif
142 delete m_qmlEngine;
143 m_qmlEngine = nullptr;
146void ShellApplication::setupQmlEngine(bool isMirServer)
148 m_qmlEngine = new QQmlEngine(this);
150 m_qmlEngine->setBaseUrl(QUrl::fromLocalFile(::qmlDirectory()));
152 prependImportPaths(m_qmlEngine, ::overrideImportPaths());
153 if (!isMirServer) {
154 prependImportPaths(m_qmlEngine, ::nonMirImportPaths());
155 }
156 appendImportPaths(m_qmlEngine, ::fallbackImportPaths());
158 m_qmlEngine->setNetworkAccessManagerFactory(new CachingNetworkManagerFactory);
160 QObject::connect(m_qmlEngine, &QQmlEngine::quit, this, &QGuiApplication::quit);
163void ShellApplication::onScreenAdded(QScreen * /*screen*/)
165 // TODO: Support an arbitrary number of screens and different policies
166 // (eg cloned desktop, several desktops, etc)
167 if (screens().count() == 2) {
168 m_shellView->setScreen(screens().at(1));
169 m_qmlArgs.setDeviceName(QStringLiteral("desktop"));
170 // Changing the QScreen where a QWindow is drawn makes it also lose focus (besides having
171 // its backing QPlatformWindow recreated). So lets refocus it.
172 m_shellView->requestActivate();
173 // QWindow::destroy() is called when it changes between screens. We have to manually make it visible again
174 // <dandrader> This bug is supposedly fixed in Qt 5.5.1, although I can still reproduce it there. :-/
175 m_shellView->setVisible(true);
177 m_secondaryWindow = new SecondaryWindow(m_qmlEngine);
178 m_secondaryWindow->setScreen(screens().at(0));
180 // QWindow::showFullScreen() also calls QWindow::requestActivate() and we don't want that!
181 m_secondaryWindow->setWindowState(Qt::WindowFullScreen);
182 m_secondaryWindow->setVisible(true);
183 }
186void ShellApplication::onScreenAboutToBeRemoved(QScreen *screen)
188 // TODO: Support an arbitrary number of screens and different policies
189 // (eg cloned desktop, several desktops, etc)
190 if (screen == m_shellView->screen()) {
191 const QList<QScreen *> allScreens = screens();
192 Q_ASSERT(allScreens.count() > 1);
193 Q_ASSERT(allScreens.at(0) != screen);
194 Q_ASSERT(m_secondaryWindow);
195 delete m_secondaryWindow;
196 m_secondaryWindow = nullptr;
197 m_shellView->setScreen(allScreens.first());
198 m_qmlArgs.setDeviceName(m_deviceName);
199 // Changing the QScreen where a QWindow is drawn makes it also lose focus (besides having
200 // its backing QPlatformWindow recreated). So lets refocus it.
201 m_shellView->requestActivate();
202 // QWindow::destroy() is called when it changes between screens. We have to manually make it visible again
203 // <dandrader> This bug is supposedly fixed in Qt 5.5.1, although I can still reproduce it there. :-/
204 m_shellView->setVisible(true);
205 }