on cd18e8f help sign in
unity8/main.cpp
222 lines 8.0 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>
32// local
33#include "paths.h"
34#include "MouseTouchAdaptor.h"
35#include "ApplicationArguments.h"
37#include <unity-mir/qmirserver.h>
39namespace {
41void prependImportPaths(QQmlEngine *engine, const QStringList &paths)
43 QStringList importPathList = engine->importPathList();
44 for (int i = paths.count() -1; i >= 0; i--) {
45 // don't duplicate
46 const QString& path = paths[i];
47 QStringList::iterator iter = qFind(importPathList.begin(), importPathList.end(), path);
48 if (iter == importPathList.end()) {
49 engine->addImportPath(path);
50 }
51 }
54/* When you append and import path to the list of import paths it will be the *last*
55 place where Qt will search for QML modules.
56 The usual QQmlEngine::addImportPath() actually prepends the given path.*/
57void appendImportPaths(QQmlEngine *engine, const QStringList &paths)
59 QStringList importPathList = engine->importPathList();
60 Q_FOREACH(const QString& path, paths) {
61 // don't duplicate
62 QStringList::iterator iter = qFind(importPathList.begin(), importPathList.end(), path);
63 if (iter == importPathList.end()) {
64 importPathList.append(path);
65 }
66 }
67 engine->setImportPathList(importPathList);
70void resolveIconTheme() {
71 const char *ubuntuIconTheme = getenv("UBUNTU_ICON_THEME");
72 if (ubuntuIconTheme == NULL) {
73 ubuntuIconTheme = "ubuntu-mobile";
74 }
75 QIcon::setThemeName(ubuntuIconTheme);
77} // namespace {
79int startShell(int argc, const char** argv, void* server)
81 /* Workaround Qt platform integration plugin not advertising itself
82 as having the following capabilities:
83 - QPlatformIntegration::ThreadedOpenGL
84 - QPlatformIntegration::BufferQueueingOpenGL
85 */
86 setenv("QML_FORCE_THREADED_RENDERER", "1", 1);
87 setenv("QML_FIXED_ANIMATION_STEP", "1", 1);
89 const bool isUbuntuMirServer = qgetenv("QT_QPA_PLATFORM") == "ubuntumirserver";
91 QGuiApplication::setApplicationName("Unity 8");
92 QGuiApplication *application;
93 if (isUbuntuMirServer) {
94 QLibrary unityMir("unity-mir", 1);
95 unityMir.load();
97 typedef QGuiApplication* (*createServerApplication_t)(int&, const char **, void*);
98 createServerApplication_t createQMirServerApplication = (createServerApplication_t) unityMir.resolve("createQMirServerApplication");
99 if (!createQMirServerApplication) {
100 qDebug() << "unable to resolve symbol: createQMirServerApplication";
101 return 4;
102 }
104 application = createQMirServerApplication(argc, argv, server);
105 } else {
106 application = new QGuiApplication(argc, (char**)argv);
107 }
109 resolveIconTheme();
111 QStringList args = application->arguments();
112 ApplicationArguments qmlArgs(args);
114 // The testability driver is only loaded by QApplication but not by QGuiApplication.
115 // However, QApplication depends on QWidget which would add some unneeded overhead => Let's load the testability driver on our own.
116 if (args.contains(QLatin1String("-testability"))) {
117 QLibrary testLib(QLatin1String("qttestability"));
118 if (testLib.load()) {
119 typedef void (*TasInitialize)(void);
120 TasInitialize initFunction = (TasInitialize)testLib.resolve("qt_testability_init");
121 if (initFunction) {
122 initFunction();
123 } else {
124 qCritical("Library qttestability resolve failed!");
125 }
126 } else {
127 qCritical("Library qttestability load failed!");
128 }
129 }
131 bindtextdomain("unity8", translationDirectory().toUtf8().data());
133 QQuickView* view = new QQuickView();
134 view->setResizeMode(QQuickView::SizeRootObjectToView);
135 view->setTitle("Qml Phone Shell");
136 view->engine()->setBaseUrl(QUrl::fromLocalFile(::shellAppDirectory()));
137 view->rootContext()->setContextProperty("applicationArguments", &qmlArgs);
138 if (args.contains(QLatin1String("-frameless"))) {
139 view->setFlags(Qt::FramelessWindowHint);
140 }
142 // You will need this if you want to interact with touch-only components using a mouse
143 // Needed only when manually testing on a desktop.
144 MouseTouchAdaptor *mouseTouchAdaptor = 0;
145 if (args.contains(QLatin1String("-mousetouch"))) {
146 mouseTouchAdaptor = new MouseTouchAdaptor;
147 application->installNativeEventFilter(mouseTouchAdaptor);
148 }
150 QPlatformNativeInterface* nativeInterface = QGuiApplication::platformNativeInterface();
151 /* Shell is declared as a system session so that it always receives all
152 input events.
153 FIXME: use the enum value corresponding to SYSTEM_SESSION_TYPE (= 1)
154 when it becomes available.
155 */
156 nativeInterface->setProperty("ubuntuSessionType", 1);
157 view->setProperty("role", 2); // INDICATOR_ACTOR_ROLE
159 QUrl source("Shell.qml");
160 prependImportPaths(view->engine(), ::overrideImportPaths());
161 appendImportPaths(view->engine(), ::fallbackImportPaths());
163 QStringList importPath = view->engine()->importPathList().filter("qt5/imports");
164 if (isUbuntuMirServer) {
165 importPath.first().append("/Unity-Mir");
166 view->engine()->addImportPath(importPath.first());
167 }
169 view->setSource(source);
170 view->setColor("transparent");
172 if (qgetenv("QT_QPA_PLATFORM") == "ubuntu" || isUbuntuMirServer || args.contains(QLatin1String("-fullscreen"))) {
173 view->showFullScreen();
174 } else {
175 view->show();
176 }
178 int result = application->exec();
180 delete mouseTouchAdaptor;
181 delete application;
183 return result;
186int main(int argc, const char *argv[])
188 // For ubuntumirserver/ubuntumirclient
189 if (qgetenv("QT_QPA_PLATFORM").startsWith("ubuntumir")) {
190 setenv("QT_QPA_PLATFORM", "ubuntumirserver", 1);
192 // If we use unity-mir directly, we automatically link to the Mir-server
193 // platform-api bindings, which result in unexpected behaviour when
194 // running the non-Mir scenario.
195 QLibrary unityMir("unity-mir", 1);
196 unityMir.load();
197 if (!unityMir.isLoaded()) {
198 qDebug() << "Library unity-mir not found/loaded";
199 return 1;
200 }
202 typedef QMirServer* (*createServer_t)(int, const char **);
203 createServer_t createQMirServer = (createServer_t) unityMir.resolve("createQMirServer");
204 if (!createQMirServer) {
205 qDebug() << "unable to resolve symbol: createQMirServer";
206 return 2;
207 }
209 QMirServer* mirServer = createQMirServer(argc, argv);
211 typedef int (*runWithClient_t)(QMirServer*, std::function<int(int, const char**, void*)>);
212 runWithClient_t runWithClient = (runWithClient_t) unityMir.resolve("runQMirServerWithClient");
213 if (!runWithClient) {
214 qDebug() << "unable to resolve symbol: runWithClient";
215 return 3;
216 }
218 return runWithClient(mirServer, startShell);
219 } else {
220 return startShell(argc, argv, nullptr);
221 }