on 428ff55 help sign in
unity8/main.cpp
228 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 <QtGui/QScreen>
25#include <QtQml/QQmlEngine>
26#include <QtQml/QQmlContext>
27#include <qpa/qplatformnativeinterface.h>
28#include <QLibrary>
29#include <QDebug>
30#include <libintl.h>
31#include <dlfcn.h>
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 /* Workaround Qt platform integration plugin not advertising itself
83 as having the following capabilities:
84 - QPlatformIntegration::ThreadedOpenGL
85 - QPlatformIntegration::BufferQueueingOpenGL
86 */
87 setenv("QML_FORCE_THREADED_RENDERER", "1", 1);
88 setenv("QML_FIXED_ANIMATION_STEP", "1", 1);
90 const bool isUbuntuMirServer = qgetenv("QT_QPA_PLATFORM") == "ubuntumirserver";
92 QGuiApplication::setApplicationName(APP_NAME);
93 QGuiApplication *application;
94 if (isUbuntuMirServer) {
95 QLibrary unityMir("unity-mir", 1);
96 unityMir.load();
98 typedef QGuiApplication* (*createServerApplication_t)(int&, const char **, void*);
99 createServerApplication_t createQMirServerApplication = (createServerApplication_t) unityMir.resolve("createQMirServerApplication");
100 if (!createQMirServerApplication) {
101 qDebug() << "unable to resolve symbol: createQMirServerApplication";
102 return 4;
103 }
105 application = createQMirServerApplication(argc, argv, server);
106 } else {
107 application = new QGuiApplication(argc, (char**)argv);
108 }
110 resolveIconTheme();
112 QStringList args = application->arguments();
113 ApplicationArguments qmlArgs(args);
115 // The testability driver is only loaded by QApplication but not by QGuiApplication.
116 // However, QApplication depends on QWidget which would add some unneeded overhead => Let's load the testability driver on our own.
117 if (args.contains(QLatin1String("-testability"))) {
118 QLibrary testLib(QLatin1String("qttestability"));
119 if (testLib.load()) {
120 typedef void (*TasInitialize)(void);
121 TasInitialize initFunction = (TasInitialize)testLib.resolve("qt_testability_init");
122 if (initFunction) {
123 initFunction();
124 } else {
125 qCritical("Library qttestability resolve failed!");
126 }
127 } else {
128 qCritical("Library qttestability load failed!");
129 }
130 }
132 bindtextdomain("unity8", translationDirectory().toUtf8().data());
134 QQuickView* view = new QQuickView();
135 view->setResizeMode(QQuickView::SizeRootObjectToView);
136 view->setTitle(APP_NAME);
137 view->engine()->setBaseUrl(QUrl::fromLocalFile(::shellAppDirectory()));
138 view->rootContext()->setContextProperty("applicationArguments", &qmlArgs);
139 if (args.contains(QLatin1String("-frameless"))) {
140 view->setFlags(Qt::FramelessWindowHint);
141 }
143 // You will need this if you want to interact with touch-only components using a mouse
144 // Needed only when manually testing on a desktop.
145 MouseTouchAdaptor *mouseTouchAdaptor = 0;
146 if (args.contains(QLatin1String("-mousetouch"))) {
147 mouseTouchAdaptor = new MouseTouchAdaptor;
148 mouseTouchAdaptor->setTargetWindow(view);
149 application->installNativeEventFilter(mouseTouchAdaptor);
150 }
152 QPlatformNativeInterface* nativeInterface = QGuiApplication::platformNativeInterface();
153 /* Shell is declared as a system session so that it always receives all
154 input events.
155 FIXME: use the enum value corresponding to SYSTEM_SESSION_TYPE (= 1)
156 when it becomes available.
157 */
158 nativeInterface->setProperty("ubuntuSessionType", 1);
159 view->setProperty("role", 2); // INDICATOR_ACTOR_ROLE
161 QUrl source(QML_FILE);
162 prependImportPaths(view->engine(), ::overrideImportPaths());
163 appendImportPaths(view->engine(), ::fallbackImportPaths());
165 QStringList importPath = view->engine()->importPathList().filter("qt5/imports");
166 if (isUbuntuMirServer) {
167 importPath.first().append("/Unity-Mir");
168 view->engine()->addImportPath(importPath.first());
169 }
171 view->setSource(source);
172 view->setColor("transparent");
174 if (qgetenv("QT_QPA_PLATFORM") == "ubuntu" || isUbuntuMirServer || args.contains(QLatin1String("-fullscreen"))) {
175 // First, size window equal to screen (fake a real WM fullscreen mode).
176 // Then, in case we are actually running inside a windowing system,
177 // nicely set actual WM fullscreen hint for its benefit.
178 view->resize(view->screen()->size());
179 view->showFullScreen();
180 } else {
181 view->show();
182 }
184 int result = application->exec();
186 delete mouseTouchAdaptor;
187 delete application;
189 return result;
192int main(int argc, const char *argv[])
194 // For ubuntumirserver/ubuntumirclient
195 if (qgetenv("QT_QPA_PLATFORM").startsWith("ubuntumir")) {
196 setenv("QT_QPA_PLATFORM", "ubuntumirserver", 1);
198 // If we use unity-mir directly, we automatically link to the Mir-server
199 // platform-api bindings, which result in unexpected behaviour when
200 // running the non-Mir scenario.
201 QLibrary unityMir("unity-mir", 1);
202 unityMir.load();
203 if (!unityMir.isLoaded()) {
204 qDebug() << "Library unity-mir not found/loaded";
205 return 1;
206 }
208 typedef QMirServer* (*createServer_t)(int, const char **);
209 createServer_t createQMirServer = (createServer_t) unityMir.resolve("createQMirServer");
210 if (!createQMirServer) {
211 qDebug() << "unable to resolve symbol: createQMirServer";
212 return 2;
213 }
215 QMirServer* mirServer = createQMirServer(argc, argv);
217 typedef int (*runWithClient_t)(QMirServer*, std::function<int(int, const char**, void*)>);
218 runWithClient_t runWithClient = (runWithClient_t) unityMir.resolve("runQMirServerWithClient");
219 if (!runWithClient) {
220 qDebug() << "unable to resolve symbol: runWithClient";
221 return 3;
222 }
224 return runWithClient(mirServer, startShell);
225 } else {
226 return startShell(argc, argv, nullptr);
227 }