on 493e111 help sign in
unity8/src/ShellApplication.cpp
217 lines 7.9 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 <libintl.h>
26// libandroid-properties
27#include <hybris/properties/properties.h>
29// local
30#include <paths.h>
31#include "CachingNetworkManagerFactory.h"
32#include "UnityCommandLineParser.h"
33#include "DebuggingController.h"
35ShellApplication::ShellApplication(int & argc, char ** argv, bool isMirServer)
36 : QGuiApplication(argc, argv)
39 setApplicationName(QStringLiteral("unity8"));
40 setOrganizationName(QStringLiteral("Canonical"));
42 connect(this, &QGuiApplication::screenAdded, this, &ShellApplication::onScreenAdded);
44 setupQmlEngine(isMirServer);
46 UnityCommandLineParser parser(*this);
48 if (!parser.deviceName().isEmpty()) {
49 m_deviceName = parser.deviceName();
50 } else {
51 char buffer[200];
52 property_get("ro.product.device", buffer /* value */, "desktop" /* default_value*/);
53 m_deviceName = QString(buffer);
54 }
55 m_qmlArgs.setDeviceName(m_deviceName);
57 m_qmlArgs.setMode(parser.mode());
59 // The testability driver is only loaded by QApplication but not by QGuiApplication.
60 // However, QApplication depends on QWidget which would add some unneeded overhead => Let's load the testability driver on our own.
61 if (parser.hasTestability() || getenv("QT_LOAD_TESTABILITY")) {
62 QLibrary testLib(QStringLiteral("qttestability"));
63 if (testLib.load()) {
64 typedef void (*TasInitialize)(void);
65 TasInitialize initFunction = (TasInitialize)testLib.resolve("qt_testability_init");
66 if (initFunction) {
67 initFunction();
68 } else {
69 qCritical("Library qttestability resolve failed!");
70 }
71 } else {
72 qCritical("Library qttestability load failed!");
73 }
74 }
76 bindtextdomain("unity8", translationDirectory().toUtf8().data());
77 textdomain("unity8");
79 m_shellView = new ShellView(m_qmlEngine, &m_qmlArgs);
81 if (parser.windowGeometry().isValid()) {
82 m_shellView->setWidth(parser.windowGeometry().width());
83 m_shellView->setHeight(parser.windowGeometry().height());
84 }
86 if (parser.hasFrameless()) {
87 m_shellView->setFlags(Qt::FramelessWindowHint);
88 }
91 #ifdef UNITY8_ENABLE_TOUCH_EMULATION
92 // You will need this if you want to interact with touch-only components using a mouse
93 // Needed only when manually testing on a desktop.
94 if (parser.hasMouseToTouch()) {
95 m_mouseTouchAdaptor = MouseTouchAdaptor::instance();
96 }
97 #endif
99 new DebuggingController(this);
101 // Some hard-coded policy for now.
102 // NB: We don't support more than two screens at the moment
103 //
104 // TODO: Support an arbitrary number of screens and different policies
105 // (eg cloned desktop, several desktops, etc)
106 if (isMirServer && screens().count() == 2) {
107 m_shellView->setScreen(screens().at(1));
108 m_qmlArgs.setDeviceName(QStringLiteral("desktop"));
110 m_secondaryWindow = new SecondaryWindow(m_qmlEngine);
111 m_secondaryWindow->setScreen(screens().at(0));
112 // QWindow::showFullScreen() also calls QWindow::requestActivate() and we don't want that!
113 m_secondaryWindow->setWindowState(Qt::WindowFullScreen);
114 m_secondaryWindow->setVisible(true);
115 }
117 if (parser.mode().compare("greeter") == 0) {
118 QSize primaryScreenSize = this->primaryScreen()->size();
119 m_shellView->setHeight(primaryScreenSize.height());
120 m_shellView->setWidth(primaryScreenSize.width());
121 m_shellView->show();
122 m_shellView->requestActivate();
123 if (!QProcess::startDetached("initctl emit --no-wait unity8-greeter-started")) {
124 qDebug() << "Unable to send unity8-greeter-started event to Upstart";
125 }
126 } else if (isMirServer || parser.hasFullscreen()) {
127 m_shellView->showFullScreen();
128 } else {
129 m_shellView->show();
130 }
133ShellApplication::~ShellApplication()
135 destroyResources();
138void ShellApplication::destroyResources()
140 // Deletion order is important. Don't use QScopedPointers and the like
141 // Otherwise the process will hang on shutdown (bug somewhere I guess).
142 delete m_shellView;
143 m_shellView = nullptr;
145 delete m_secondaryWindow;
146 m_secondaryWindow = nullptr;
148 #ifdef UNITY8_ENABLE_TOUCH_EMULATION
149 delete m_mouseTouchAdaptor;
150 m_mouseTouchAdaptor = nullptr;
151 #endif
153 delete m_qmlEngine;
154 m_qmlEngine = nullptr;
157void ShellApplication::setupQmlEngine(bool isMirServer)
159 m_qmlEngine = new QQmlEngine(this);
161 m_qmlEngine->setBaseUrl(QUrl::fromLocalFile(::qmlDirectory()));
163 prependImportPaths(m_qmlEngine, ::overrideImportPaths());
164 if (!isMirServer) {
165 prependImportPaths(m_qmlEngine, ::nonMirImportPaths());
166 }
167 appendImportPaths(m_qmlEngine, ::fallbackImportPaths());
169 m_qmlEngine->setNetworkAccessManagerFactory(new CachingNetworkManagerFactory);
171 QObject::connect(m_qmlEngine, &QQmlEngine::quit, this, &QGuiApplication::quit);
174void ShellApplication::onScreenAdded(QScreen * /*screen*/)
176 // TODO: Support an arbitrary number of screens and different policies
177 // (eg cloned desktop, several desktops, etc)
178 if (screens().count() == 2) {
179 m_shellView->setScreen(screens().at(1));
180 m_qmlArgs.setDeviceName(QStringLiteral("desktop"));
181 // Changing the QScreen where a QWindow is drawn makes it also lose focus (besides having
182 // its backing QPlatformWindow recreated). So lets refocus it.
183 m_shellView->requestActivate();
184 // QWindow::destroy() is called when it changes between screens. We have to manually make it visible again
185 // <dandrader> This bug is supposedly fixed in Qt 5.5.1, although I can still reproduce it there. :-/
186 m_shellView->setVisible(true);
188 m_secondaryWindow = new SecondaryWindow(m_qmlEngine);
189 m_secondaryWindow->setScreen(screens().at(0));
191 // QWindow::showFullScreen() also calls QWindow::requestActivate() and we don't want that!
192 m_secondaryWindow->setWindowState(Qt::WindowFullScreen);
193 m_secondaryWindow->setVisible(true);
194 }
197void ShellApplication::onScreenAboutToBeRemoved(QScreen *screen)
199 // TODO: Support an arbitrary number of screens and different policies
200 // (eg cloned desktop, several desktops, etc)
201 if (screen == m_shellView->screen()) {
202 const QList<QScreen *> allScreens = screens();
203 Q_ASSERT(allScreens.count() > 1);
204 Q_ASSERT(allScreens.at(0) != screen);
205 Q_ASSERT(m_secondaryWindow);
206 delete m_secondaryWindow;
207 m_secondaryWindow = nullptr;
208 m_shellView->setScreen(allScreens.first());
209 m_qmlArgs.setDeviceName(m_deviceName);
210 // Changing the QScreen where a QWindow is drawn makes it also lose focus (besides having
211 // its backing QPlatformWindow recreated). So lets refocus it.
212 m_shellView->requestActivate();
213 // QWindow::destroy() is called when it changes between screens. We have to manually make it visible again
214 // <dandrader> This bug is supposedly fixed in Qt 5.5.1, although I can still reproduce it there. :-/
215 m_shellView->setVisible(true);
216 }