on 552d304 help sign in
unity8/main.cpp
223 lines 8.1 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 const bool isUbuntuMirServer = qgetenv("QT_QPA_PLATFORM") == "ubuntumirserver";
83 QGuiApplication::setApplicationName("Unity 8");
84 QGuiApplication *application;
85 if (isUbuntuMirServer) {
86 QLibrary unityMir("unity-mir", 1);
87 unityMir.load();
89 typedef QGuiApplication* (*createServerApplication_t)(int&, const char **, void*);
90 createServerApplication_t createQMirServerApplication = (createServerApplication_t) unityMir.resolve("createQMirServerApplication");
91 if (!createQMirServerApplication) {
92 qDebug() << "unable to resolve symbol: createQMirServerApplication";
93 return 4;
94 }
96 application = createQMirServerApplication(argc, argv, server);
97 } else {
98 application = new QGuiApplication(argc, (char**)argv);
99 }
101 resolveIconTheme();
103 QStringList args = application->arguments();
104 ApplicationArguments qmlArgs(args);
106 // The testability driver is only loaded by QApplication but not by QGuiApplication.
107 // However, QApplication depends on QWidget which would add some unneeded overhead => Let's load the testability driver on our own.
108 if (args.contains(QLatin1String("-testability")) || getenv("QT_LOAD_TESTABILITY")) {
109 QLibrary testLib(QLatin1String("qttestability"));
110 if (testLib.load()) {
111 typedef void (*TasInitialize)(void);
112 TasInitialize initFunction = (TasInitialize)testLib.resolve("qt_testability_init");
113 if (initFunction) {
114 initFunction();
115 } else {
116 qCritical("Library qttestability resolve failed!");
117 }
118 } else {
119 qCritical("Library qttestability load failed!");
120 }
121 }
123 bindtextdomain("unity8", translationDirectory().toUtf8().data());
125 QQuickView* view = new QQuickView();
126 view->setResizeMode(QQuickView::SizeRootObjectToView);
127 view->setTitle("Qml Phone Shell");
128 view->engine()->setBaseUrl(QUrl::fromLocalFile(::shellAppDirectory()));
129 view->rootContext()->setContextProperty("applicationArguments", &qmlArgs);
130 if (args.contains(QLatin1String("-frameless"))) {
131 view->setFlags(Qt::FramelessWindowHint);
132 }
134 // You will need this if you want to interact with touch-only components using a mouse
135 // Needed only when manually testing on a desktop.
136 MouseTouchAdaptor *mouseTouchAdaptor = 0;
137 if (args.contains(QLatin1String("-mousetouch"))) {
138 mouseTouchAdaptor = new MouseTouchAdaptor;
139 application->installNativeEventFilter(mouseTouchAdaptor);
140 }
142 QPlatformNativeInterface* nativeInterface = QGuiApplication::platformNativeInterface();
143 /* Shell is declared as a system session so that it always receives all
144 input events.
145 FIXME: use the enum value corresponding to SYSTEM_SESSION_TYPE (= 1)
146 when it becomes available.
147 */
148 nativeInterface->setProperty("ubuntuSessionType", 1);
149 view->setProperty("role", 2); // INDICATOR_ACTOR_ROLE
151 QUrl source("Shell.qml");
152 prependImportPaths(view->engine(), ::overrideImportPaths());
153 appendImportPaths(view->engine(), ::fallbackImportPaths());
155 if (isUbuntuMirServer) {
156 QStringList importPaths = view->engine()->importPathList();
157 importPaths.replaceInStrings(QRegExp("qt5/imports$"), "qt5/imports/Unity-Mir");
158 view->engine()->setImportPathList(importPaths);
159 }
161 view->setSource(source);
162 view->setColor("transparent");
164 if (qgetenv("QT_QPA_PLATFORM") == "ubuntu" || isUbuntuMirServer || args.contains(QLatin1String("-fullscreen"))) {
165 view->showFullScreen();
166 } else {
167 view->show();
168 }
170 int result = application->exec();
172 delete view;
173 delete mouseTouchAdaptor;
174 delete application;
176 return result;
179int main(int argc, const char *argv[])
181 /* Workaround Qt platform integration plugin not advertising itself
182 as having the following capabilities:
183 - QPlatformIntegration::ThreadedOpenGL
184 - QPlatformIntegration::BufferQueueingOpenGL
185 */
186 setenv("QML_FORCE_THREADED_RENDERER", "1", 1);
187 setenv("QML_FIXED_ANIMATION_STEP", "1", 1);
189 // For ubuntumirserver/ubuntumirclient
190 if (qgetenv("QT_QPA_PLATFORM").startsWith("ubuntumir")) {
191 setenv("QT_QPA_PLATFORM", "ubuntumirserver", 1);
193 // If we use unity-mir directly, we automatically link to the Mir-server
194 // platform-api bindings, which result in unexpected behaviour when
195 // running the non-Mir scenario.
196 QLibrary unityMir("unity-mir", 1);
197 unityMir.load();
198 if (!unityMir.isLoaded()) {
199 qDebug() << "Library unity-mir not found/loaded";
200 return 1;
201 }
203 typedef QMirServer* (*createServer_t)(int, const char **);
204 createServer_t createQMirServer = (createServer_t) unityMir.resolve("createQMirServer");
205 if (!createQMirServer) {
206 qDebug() << "unable to resolve symbol: createQMirServer";
207 return 2;
208 }
210 QMirServer* mirServer = createQMirServer(argc, argv);
212 typedef int (*runWithClient_t)(QMirServer*, std::function<int(int, const char**, void*)>);
213 runWithClient_t runWithClient = (runWithClient_t) unityMir.resolve("runQMirServerWithClient");
214 if (!runWithClient) {
215 qDebug() << "unable to resolve symbol: runWithClient";
216 return 3;
217 }
219 return runWithClient(mirServer, startShell);
220 } else {
221 return startShell(argc, argv, nullptr);
222 }