on 210aff0 help sign in
unity8/main.cpp
243 lines 8.9 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>
32#include <csignal>
34// local
35#include "paths.h"
36#include "MouseTouchAdaptor.h"
37#include "ApplicationArguments.h"
39#include <unity-mir/qmirserver.h>
41namespace {
43void prependImportPaths(QQmlEngine *engine, const QStringList &paths)
45 QStringList importPathList = engine->importPathList();
46 for (int i = paths.count() -1; i >= 0; i--) {
47 // don't duplicate
48 const QString& path = paths[i];
49 QStringList::iterator iter = qFind(importPathList.begin(), importPathList.end(), path);
50 if (iter == importPathList.end()) {
51 engine->addImportPath(path);
52 }
53 }
56/* When you append and import path to the list of import paths it will be the *last*
57 place where Qt will search for QML modules.
58 The usual QQmlEngine::addImportPath() actually prepends the given path.*/
59void appendImportPaths(QQmlEngine *engine, const QStringList &paths)
61 QStringList importPathList = engine->importPathList();
62 Q_FOREACH(const QString& path, paths) {
63 // don't duplicate
64 QStringList::iterator iter = qFind(importPathList.begin(), importPathList.end(), path);
65 if (iter == importPathList.end()) {
66 importPathList.append(path);
67 }
68 }
69 engine->setImportPathList(importPathList);
72void resolveIconTheme() {
73 const char *ubuntuIconTheme = getenv("UBUNTU_ICON_THEME");
74 if (ubuntuIconTheme == NULL) {
75 ubuntuIconTheme = "ubuntu-mobile";
76 }
77 QIcon::setThemeName(ubuntuIconTheme);
79} // namespace {
81int startShell(int argc, const char** argv, void* server)
83 const bool isUbuntuMirServer = qgetenv("QT_QPA_PLATFORM") == "ubuntumirserver";
85 QGuiApplication::setApplicationName(APP_NAME);
86 QGuiApplication *application;
87 if (isUbuntuMirServer) {
88 QLibrary unityMir("unity-mir", 1);
89 unityMir.load();
91 typedef QGuiApplication* (*createServerApplication_t)(int&, const char **, void*);
92 createServerApplication_t createQMirServerApplication = (createServerApplication_t) unityMir.resolve("createQMirServerApplication");
93 if (!createQMirServerApplication) {
94 qDebug() << "unable to resolve symbol: createQMirServerApplication";
95 return 4;
96 }
98 application = createQMirServerApplication(argc, argv, server);
99 } else {
100 application = new QGuiApplication(argc, (char**)argv);
101 }
103 resolveIconTheme();
105 QStringList args = application->arguments();
106 ApplicationArguments qmlArgs(args);
108 // The testability driver is only loaded by QApplication but not by QGuiApplication.
109 // However, QApplication depends on QWidget which would add some unneeded overhead => Let's load the testability driver on our own.
110 if (args.contains(QLatin1String("-testability")) || getenv("QT_LOAD_TESTABILITY")) {
111 QLibrary testLib(QLatin1String("qttestability"));
112 if (testLib.load()) {
113 typedef void (*TasInitialize)(void);
114 TasInitialize initFunction = (TasInitialize)testLib.resolve("qt_testability_init");
115 if (initFunction) {
116 initFunction();
117 } else {
118 qCritical("Library qttestability resolve failed!");
119 }
120 } else {
121 qCritical("Library qttestability load failed!");
122 }
123 }
125 bindtextdomain("unity8", translationDirectory().toUtf8().data());
127 // Tell indicator plugin what profile to use
128 qputenv("UNITY8_INDICATOR_PROFILE", INDICATOR_PROFILE);
130 QQuickView* view = new QQuickView();
131 view->setResizeMode(QQuickView::SizeRootObjectToView);
132 view->setTitle("Qml Phone Shell");
133 view->engine()->setBaseUrl(QUrl::fromLocalFile(::shellAppDirectory()));
134 view->rootContext()->setContextProperty("applicationArguments", &qmlArgs);
135 if (args.contains(QLatin1String("-frameless"))) {
136 view->setFlags(Qt::FramelessWindowHint);
137 }
139 // You will need this if you want to interact with touch-only components using a mouse
140 // Needed only when manually testing on a desktop.
141 MouseTouchAdaptor *mouseTouchAdaptor = 0;
142 if (args.contains(QLatin1String("-mousetouch"))) {
143 mouseTouchAdaptor = new MouseTouchAdaptor;
144 application->installNativeEventFilter(mouseTouchAdaptor);
145 }
147 QPlatformNativeInterface* nativeInterface = QGuiApplication::platformNativeInterface();
148 /* Shell is declared as a system session so that it always receives all
149 input events.
150 FIXME: use the enum value corresponding to SYSTEM_SESSION_TYPE (= 1)
151 when it becomes available.
152 */
153 nativeInterface->setProperty("ubuntuSessionType", 1);
154 view->setProperty("role", 2); // INDICATOR_ACTOR_ROLE
156 QUrl source(QML_FILE);
157 prependImportPaths(view->engine(), ::overrideImportPaths());
158 appendImportPaths(view->engine(), ::fallbackImportPaths());
160 if (isUbuntuMirServer) {
161 QStringList importPaths = view->engine()->importPathList();
162 importPaths.replaceInStrings(QRegExp("qt5/imports$"), "qt5/imports/Unity-Mir");
163 view->engine()->setImportPathList(importPaths);
164 }
166 view->setSource(source);
168 // Make background transparent, so that the greeter can bleed through.
169 // In normal shell, the background is never seen.
170 QSurfaceFormat format;
171 format.setAlphaBufferSize(8);
172 view->setFormat(format);
173 view->setColor(Qt::transparent);
175 if (qgetenv("QT_QPA_PLATFORM") == "ubuntu" || isUbuntuMirServer || args.contains(QLatin1String("-fullscreen"))) {
176 // First, size window equal to screen (fake a real WM fullscreen mode).
177 // Then, in case we are actually running inside a windowing system,
178 // nicely set actual WM fullscreen hint for its benefit.
179 view->resize(view->screen()->size());
180 view->showFullScreen();
181 } else {
182 view->show();
183 }
185 int result = application->exec();
187 delete view;
188 delete mouseTouchAdaptor;
189 delete application;
191 return result;
194int main(int argc, const char *argv[])
196 /* Workaround Qt platform integration plugin not advertising itself
197 as having the following capabilities:
198 - QPlatformIntegration::ThreadedOpenGL
199 - QPlatformIntegration::BufferQueueingOpenGL
200 */
201 setenv("QML_FORCE_THREADED_RENDERER", "1", 1);
202 setenv("QML_FIXED_ANIMATION_STEP", "1", 1);
204 // For ubuntumirserver/ubuntumirclient
205 if (qgetenv("QT_QPA_PLATFORM").startsWith("ubuntumir")) {
206 setenv("QT_QPA_PLATFORM", "ubuntumirserver", 1);
208 // If we use unity-mir directly, we automatically link to the Mir-server
209 // platform-api bindings, which result in unexpected behaviour when
210 // running the non-Mir scenario.
211 QLibrary unityMir("unity-mir", 1);
212 unityMir.load();
213 if (!unityMir.isLoaded()) {
214 qDebug() << "Library unity-mir not found/loaded";
215 return 1;
216 }
218 typedef QMirServer* (*createServer_t)(int, const char **);
219 createServer_t createQMirServer = (createServer_t) unityMir.resolve("createQMirServer");
220 if (!createQMirServer) {
221 qDebug() << "unable to resolve symbol: createQMirServer";
222 return 2;
223 }
225 QMirServer* mirServer = createQMirServer(argc, argv);
227 typedef int (*runWithClient_t)(QMirServer*, std::function<int(int, const char**, void*)>);
228 runWithClient_t runWithClient = (runWithClient_t) unityMir.resolve("runQMirServerWithClient");
229 if (!runWithClient) {
230 qDebug() << "unable to resolve symbol: runWithClient";
231 return 3;
232 }
234 return runWithClient(mirServer, startShell);
235 } else {
236 if (qgetenv("UPSTART_JOB") == "unity8") {
237 // Emit SIGSTOP as expected by upstart, under Mir it's unity-mir that will raise it.
238 // see http://upstart.ubuntu.com/cookbook/#expect-stop
239 raise(SIGSTOP);
240 }
241 return startShell(argc, argv, nullptr);
242 }