on a67b0cb help sign in
unity8/main.cpp
229 lines 8.3 kB history raw
1/*
2 * Copyright (C) 2012 Canonical, Ltd.
3 *
4 * Authors:
5 * Gerry Boland <gerry.boland@canonical.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 3.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
20// Qt
21#include <QtQuick/QQuickView>
22#include <QtGui/QIcon>
23#include <QtGui/QGuiApplication>
24#include <QtQml/QQmlEngine>
25#include <QtQml/QQmlContext>
26#include <qpa/qplatformnativeinterface.h>
27#include <QLibrary>
28#include <QDebug>
29#include <libintl.h>
30#include <dlfcn.h>
31#include <csignal>
33// local
34#include "paths.h"
35#include "MouseTouchAdaptor.h"
36#include "ApplicationArguments.h"
38#include <unity-mir/qmirserver.h>
40namespace {
42void prependImportPaths(QQmlEngine *engine, const QStringList &paths)
44 QStringList importPathList = engine->importPathList();
45 for (int i = paths.count() -1; i >= 0; i--) {
46 // don't duplicate
47 const QString& path = paths[i];
48 QStringList::iterator iter = qFind(importPathList.begin(), importPathList.end(), path);
49 if (iter == importPathList.end()) {
50 engine->addImportPath(path);
51 }
52 }
55/* When you append and import path to the list of import paths it will be the *last*
56 place where Qt will search for QML modules.
57 The usual QQmlEngine::addImportPath() actually prepends the given path.*/
58void appendImportPaths(QQmlEngine *engine, const QStringList &paths)
60 QStringList importPathList = engine->importPathList();
61 Q_FOREACH(const QString& path, paths) {
62 // don't duplicate
63 QStringList::iterator iter = qFind(importPathList.begin(), importPathList.end(), path);
64 if (iter == importPathList.end()) {
65 importPathList.append(path);
66 }
67 }
68 engine->setImportPathList(importPathList);
71void resolveIconTheme() {
72 const char *ubuntuIconTheme = getenv("UBUNTU_ICON_THEME");
73 if (ubuntuIconTheme == NULL) {
74 ubuntuIconTheme = "ubuntu-mobile";
75 }
76 QIcon::setThemeName(ubuntuIconTheme);
78} // namespace {
80int startShell(int argc, const char** argv, void* server)
82 const bool isUbuntuMirServer = qgetenv("QT_QPA_PLATFORM") == "ubuntumirserver";
84 QGuiApplication::setApplicationName("Unity 8");
85 QGuiApplication *application;
86 if (isUbuntuMirServer) {
87 QLibrary unityMir("unity-mir", 1);
88 unityMir.load();
90 typedef QGuiApplication* (*createServerApplication_t)(int&, const char **, void*);
91 createServerApplication_t createQMirServerApplication = (createServerApplication_t) unityMir.resolve("createQMirServerApplication");
92 if (!createQMirServerApplication) {
93 qDebug() << "unable to resolve symbol: createQMirServerApplication";
94 return 4;
95 }
97 application = createQMirServerApplication(argc, argv, server);
98 } else {
99 application = new QGuiApplication(argc, (char**)argv);
100 }
102 resolveIconTheme();
104 QStringList args = application->arguments();
105 ApplicationArguments qmlArgs(args);
107 // The testability driver is only loaded by QApplication but not by QGuiApplication.
108 // However, QApplication depends on QWidget which would add some unneeded overhead => Let's load the testability driver on our own.
109 if (args.contains(QLatin1String("-testability")) || getenv("QT_LOAD_TESTABILITY")) {
110 QLibrary testLib(QLatin1String("qttestability"));
111 if (testLib.load()) {
112 typedef void (*TasInitialize)(void);
113 TasInitialize initFunction = (TasInitialize)testLib.resolve("qt_testability_init");
114 if (initFunction) {
115 initFunction();
116 } else {
117 qCritical("Library qttestability resolve failed!");
118 }
119 } else {
120 qCritical("Library qttestability load failed!");
121 }
122 }
124 bindtextdomain("unity8", translationDirectory().toUtf8().data());
126 QQuickView* view = new QQuickView();
127 view->setResizeMode(QQuickView::SizeRootObjectToView);
128 view->setTitle("Qml Phone Shell");
129 view->engine()->setBaseUrl(QUrl::fromLocalFile(::shellAppDirectory()));
130 view->rootContext()->setContextProperty("applicationArguments", &qmlArgs);
131 if (args.contains(QLatin1String("-frameless"))) {
132 view->setFlags(Qt::FramelessWindowHint);
133 }
135 // You will need this if you want to interact with touch-only components using a mouse
136 // Needed only when manually testing on a desktop.
137 MouseTouchAdaptor *mouseTouchAdaptor = 0;
138 if (args.contains(QLatin1String("-mousetouch"))) {
139 mouseTouchAdaptor = new MouseTouchAdaptor;
140 application->installNativeEventFilter(mouseTouchAdaptor);
141 }
143 QPlatformNativeInterface* nativeInterface = QGuiApplication::platformNativeInterface();
144 /* Shell is declared as a system session so that it always receives all
145 input events.
146 FIXME: use the enum value corresponding to SYSTEM_SESSION_TYPE (= 1)
147 when it becomes available.
148 */
149 nativeInterface->setProperty("ubuntuSessionType", 1);
150 view->setProperty("role", 2); // INDICATOR_ACTOR_ROLE
152 QUrl source("Shell.qml");
153 prependImportPaths(view->engine(), ::overrideImportPaths());
154 appendImportPaths(view->engine(), ::fallbackImportPaths());
156 if (isUbuntuMirServer) {
157 QStringList importPaths = view->engine()->importPathList();
158 importPaths.replaceInStrings(QRegExp("qt5/imports$"), "qt5/imports/Unity-Mir");
159 view->engine()->setImportPathList(importPaths);
160 }
162 view->setSource(source);
163 view->setColor("transparent");
165 if (qgetenv("QT_QPA_PLATFORM") == "ubuntu" || isUbuntuMirServer || args.contains(QLatin1String("-fullscreen"))) {
166 view->showFullScreen();
167 } else {
168 view->show();
169 }
171 int result = application->exec();
173 delete view;
174 delete mouseTouchAdaptor;
175 delete application;
177 return result;
180int main(int argc, const char *argv[])
182 /* Workaround Qt platform integration plugin not advertising itself
183 as having the following capabilities:
184 - QPlatformIntegration::ThreadedOpenGL
185 - QPlatformIntegration::BufferQueueingOpenGL
186 */
187 setenv("QML_FORCE_THREADED_RENDERER", "1", 1);
188 setenv("QML_FIXED_ANIMATION_STEP", "1", 1);
190 // For ubuntumirserver/ubuntumirclient
191 if (qgetenv("QT_QPA_PLATFORM").startsWith("ubuntumir")) {
192 setenv("QT_QPA_PLATFORM", "ubuntumirserver", 1);
194 // If we use unity-mir directly, we automatically link to the Mir-server
195 // platform-api bindings, which result in unexpected behaviour when
196 // running the non-Mir scenario.
197 QLibrary unityMir("unity-mir", 1);
198 unityMir.load();
199 if (!unityMir.isLoaded()) {
200 qDebug() << "Library unity-mir not found/loaded";
201 return 1;
202 }
204 typedef QMirServer* (*createServer_t)(int, const char **);
205 createServer_t createQMirServer = (createServer_t) unityMir.resolve("createQMirServer");
206 if (!createQMirServer) {
207 qDebug() << "unable to resolve symbol: createQMirServer";
208 return 2;
209 }
211 QMirServer* mirServer = createQMirServer(argc, argv);
213 typedef int (*runWithClient_t)(QMirServer*, std::function<int(int, const char**, void*)>);
214 runWithClient_t runWithClient = (runWithClient_t) unityMir.resolve("runQMirServerWithClient");
215 if (!runWithClient) {
216 qDebug() << "unable to resolve symbol: runWithClient";
217 return 3;
218 }
220 return runWithClient(mirServer, startShell);
221 } else {
222 if (qgetenv("UPSTART_JOB") == "unity8") {
223 // Emit SIGSTOP as expected by upstart, under Mir it's unity-mir that will raise it.
224 // see http://upstart.ubuntu.com/cookbook/#expect-stop
225 raise(SIGSTOP);
226 }
227 return startShell(argc, argv, nullptr);
228 }