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