on 620a196 help sign in
unity8/src/ShellApplication.cpp
225 lines 8.3 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 // The testability driver is only loaded by QApplication but not by QGuiApplication.
62 // However, QApplication depends on QWidget which would add some unneeded overhead => Let's load the testability driver on our own.
63 if (parser.hasTestability() || getenv("QT_LOAD_TESTABILITY")) {
64 QLibrary testLib(QStringLiteral("qttestability"));
65 if (testLib.load()) {
66 typedef void (*TasInitialize)(void);
67 TasInitialize initFunction = (TasInitialize)testLib.resolve("qt_testability_init");
68 if (initFunction) {
69 initFunction();
70 } else {
71 qCritical("Library qttestability resolve failed!");
72 }
73 } else {
74 qCritical("Library qttestability load failed!");
75 }
76 }
78 bindtextdomain("unity8", translationDirectory().toUtf8().data());
79 textdomain("unity8");
81 QScopedPointer<QGSettings> gSettings(new QGSettings("com.canonical.Unity8"));
82 gSettings->reset(QStringLiteral("alwaysShowOsk"));
84 m_shellView = new ShellView(m_qmlEngine, &m_qmlArgs);
86 if (parser.windowGeometry().isValid()) {
87 m_shellView->setWidth(parser.windowGeometry().width());
88 m_shellView->setHeight(parser.windowGeometry().height());
89 }
91 if (parser.hasFrameless()) {
92 m_shellView->setFlags(Qt::FramelessWindowHint);
93 }
96 #ifdef UNITY8_ENABLE_TOUCH_EMULATION
97 // You will need this if you want to interact with touch-only components using a mouse
98 // Needed only when manually testing on a desktop.
99 if (parser.hasMouseToTouch()) {
100 m_mouseTouchAdaptor = MouseTouchAdaptor::instance();
101 }
102 #endif
104 new DebuggingController(this);
106 // Some hard-coded policy for now.
107 // NB: We don't support more than two screens at the moment
108 //
109 // TODO: Support an arbitrary number of screens and different policies
110 // (eg cloned desktop, several desktops, etc)
111 if (isMirServer && screens().count() == 2) {
112 m_shellView->setScreen(screens().at(1));
113 m_qmlArgs.setDeviceName(QStringLiteral("desktop"));
115 m_secondaryWindow = new SecondaryWindow(m_qmlEngine);
116 m_secondaryWindow->setScreen(screens().at(0));
117 // QWindow::showFullScreen() also calls QWindow::requestActivate() and we don't want that!
118 m_secondaryWindow->setWindowState(Qt::WindowFullScreen);
119 m_secondaryWindow->setVisible(true);
120 }
122 if (parser.mode().compare("greeter") == 0) {
123 QSize primaryScreenSize = this->primaryScreen()->size();
124 m_shellView->setHeight(primaryScreenSize.height());
125 m_shellView->setWidth(primaryScreenSize.width());
126 m_shellView->show();
127 m_shellView->requestActivate();
128 if (!QProcess::startDetached("initctl emit --no-wait unity8-greeter-started")) {
129 qDebug() << "Unable to send unity8-greeter-started event to Upstart";
130 }
131 } else if (isMirServer || parser.hasFullscreen()) {
132 m_shellView->showFullScreen();
133 } else {
134 m_shellView->show();
135 }
138ShellApplication::~ShellApplication()
140 destroyResources();
143void ShellApplication::destroyResources()
145 // Deletion order is important. Don't use QScopedPointers and the like
146 // Otherwise the process will hang on shutdown (bug somewhere I guess).
147 delete m_shellView;
148 m_shellView = nullptr;
150 delete m_secondaryWindow;
151 m_secondaryWindow = nullptr;
153 #ifdef UNITY8_ENABLE_TOUCH_EMULATION
154 delete m_mouseTouchAdaptor;
155 m_mouseTouchAdaptor = nullptr;
156 #endif
158 delete m_qmlEngine;
159 m_qmlEngine = nullptr;
162void ShellApplication::setupQmlEngine(bool isMirServer)
164 m_qmlEngine = new QQmlEngine(this);
166 m_qmlEngine->setBaseUrl(QUrl::fromLocalFile(::qmlDirectory()));
168 prependImportPaths(m_qmlEngine, ::overrideImportPaths());
169 if (!isMirServer) {
170 prependImportPaths(m_qmlEngine, ::nonMirImportPaths());
171 }
172 appendImportPaths(m_qmlEngine, ::fallbackImportPaths());
174 m_qmlEngine->setNetworkAccessManagerFactory(new CachingNetworkManagerFactory);
176 QObject::connect(m_qmlEngine, &QQmlEngine::quit, this, &QGuiApplication::quit);
179void ShellApplication::onScreenAdded(QScreen * /*screen*/)
181 // TODO: Support an arbitrary number of screens and different policies
182 // (eg cloned desktop, several desktops, etc)
183 if (screens().count() == 2) {
184 m_shellView->setScreen(screens().at(1));
185 m_qmlArgs.setDeviceName(QStringLiteral("desktop"));
186 // QWindow::destroy() is called when it changes between screens. We have to manually make it visible again
187 // <dandrader> This bug is supposedly fixed in Qt 5.5.1, although I can still reproduce it there. :-/
188 m_shellView->setVisible(true);
190 m_secondaryWindow = new SecondaryWindow(m_qmlEngine);
191 m_secondaryWindow->setScreen(screens().at(0));
193 // QWindow::showFullScreen() also calls QWindow::requestActivate() and we don't want that!
194 m_secondaryWindow->setWindowState(Qt::WindowFullScreen);
195 m_secondaryWindow->setVisible(true);
197 // Changing the QScreen where a QWindow is drawn makes it also lose focus (besides having
198 // its backing QPlatformWindow recreated). So lets refocus it.
199 // This must be done after the creation of the SecondaryWindow, else
200 // the SecondaryWindow takes focus anyway.
201 m_shellView->requestActivate();
202 }
205void ShellApplication::onScreenRemoved(QScreen *screen)
207 // TODO: Support an arbitrary number of screens and different policies
208 // (eg cloned desktop, several desktops, etc)
209 if (screen == m_shellView->screen()) {
210 const QList<QScreen *> allScreens = screens();
211 Q_ASSERT(allScreens.count() > 1);
212 Q_ASSERT(allScreens.at(0) != screen);
213 Q_ASSERT(m_secondaryWindow);
214 delete m_secondaryWindow;
215 m_secondaryWindow = nullptr;
216 m_shellView->setScreen(allScreens.first());
217 m_qmlArgs.setDeviceName(m_deviceName);
218 // Changing the QScreen where a QWindow is drawn makes it also lose focus (besides having
219 // its backing QPlatformWindow recreated). So lets refocus it.
220 m_shellView->requestActivate();
221 // QWindow::destroy() is called when it changes between screens. We have to manually make it visible again
222 // <dandrader> This bug is supposedly fixed in Qt 5.5.1, although I can still reproduce it there. :-/
223 m_shellView->setVisible(true);
224 }