on 17872af help sign in
unity8/src/ShellApplication.cpp
151 lines 4.8 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>
23#include <QQmlContext>
25#include <libintl.h>
27// libandroid-properties
28#include <hybris/properties/properties.h>
30// local
31#include <paths.h>
32#include "CachingNetworkManagerFactory.h"
33#include "UnityCommandLineParser.h"
34#include "DebuggingController.h"
36#include <QDebug>
38#include <qtmir/windowmanagementpolicy.h>
40class WindowManagementPolicy : public qtmir::WindowManagementPolicy
42public:
43 WindowManagementPolicy(const miral::WindowManagerTools &tools, qtmir::WindowManagementPolicyPrivate& dd)
44 : qtmir::WindowManagementPolicy(tools, dd)
45 {}
47// virtual void advise_adding_to_workspace(
48// std::shared_ptr<miral::Workspace> const& workspace,
49// std::vector<miral::Window> const& windows) override
50// {
51// }
53// virtual void advise_removing_from_workspace(
54// std::shared_ptr<miral::Workspace> const& workspace,
55// std::vector<miral::Window> const& windows) override
56// {
57// }
58};
61ShellApplication::ShellApplication(int & argc, char ** argv)
62 : qtmir::MirServerApplication(argc, argv, {})
63 , m_qmlArgs(this)
65 setApplicationName(QStringLiteral("unity8"));
66 setOrganizationName(QStringLiteral("Canonical"));
68 setupQmlEngine();
70 if (m_qmlArgs.deviceName().isEmpty()) {
71 char buffer[200];
72 property_get("ro.product.device", buffer /* value */, "desktop" /* default_value*/);
73 m_qmlArgs.setDeviceName(QString(buffer));
74 }
76 // The testability driver is only loaded by QApplication but not by QGuiApplication.
77 // However, QApplication depends on QWidget which would add some unneeded overhead => Let's load the testability driver on our own.
78 if (m_qmlArgs.hasTestability() || getenv("QT_LOAD_TESTABILITY")) {
79 QLibrary testLib(QStringLiteral("qttestability"));
80 if (testLib.load()) {
81 typedef void (*TasInitialize)(void);
82 TasInitialize initFunction = (TasInitialize)testLib.resolve("qt_testability_init");
83 if (initFunction) {
84 initFunction();
85 } else {
86 qCritical("Library qttestability resolve failed!");
87 }
88 } else {
89 qCritical("Library qttestability load failed!");
90 }
91 }
93 bindtextdomain("unity8", translationDirectory().toUtf8().data());
94 textdomain("unity8");
96 m_qmlEngine->rootContext()->setContextProperty(QStringLiteral("applicationArguments"), &m_qmlArgs);
97 m_qmlEngine->rootContext()->setContextProperty("DebuggingController", new DebuggingController(this));
99 QByteArray pxpguEnv = qgetenv("GRID_UNIT_PX");
100 bool ok;
101 int pxpgu = pxpguEnv.toInt(&ok);
102 if (!ok) {
103 pxpgu = 8;
104 }
105 m_qmlEngine->rootContext()->setContextProperty("internalGu", pxpgu);
106 m_qmlEngine->load(::qmlDirectory() + "/ShellApplication.qml");
108 #ifdef UNITY8_ENABLE_TOUCH_EMULATION
109 // You will need this if you want to interact with touch-only components using a mouse
110 // Needed only when manually testing on a desktop.
111 if (m_qmlArgs.hasMouseToTouch()) {
112 m_mouseTouchAdaptor = MouseTouchAdaptor::instance();
113 }
114 #endif
116 if (m_qmlArgs.mode().compare("greeter") == 0) {
117 if (!QProcess::startDetached("initctl emit --no-wait unity8-greeter-started")) {
118 qDebug() << "Unable to send unity8-greeter-started event to Upstart";
119 }
120 }
123ShellApplication::~ShellApplication()
125 destroyResources();
128void ShellApplication::destroyResources()
130 #ifdef UNITY8_ENABLE_TOUCH_EMULATION
131 delete m_mouseTouchAdaptor;
132 m_mouseTouchAdaptor = nullptr;
133 #endif
135 delete m_qmlEngine;
136 m_qmlEngine = nullptr;
139void ShellApplication::setupQmlEngine()
141 m_qmlEngine = new QQmlApplicationEngine(this);
143 m_qmlEngine->setBaseUrl(QUrl::fromLocalFile(::qmlDirectory()));
145 prependImportPaths(m_qmlEngine, ::overrideImportPaths());
146 appendImportPaths(m_qmlEngine, ::fallbackImportPaths());
148 m_qmlEngine->setNetworkAccessManagerFactory(new CachingNetworkManagerFactory);
150 QObject::connect(m_qmlEngine, &QQmlEngine::quit, this, &QGuiApplication::quit);