on dde65cb help sign in
unity8/src/main.cpp
193 lines 7.2 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/QGuiApplication>
23#include <QtQml/QQmlEngine>
24#include <QtQml/QQmlContext>
25#include <qpa/qplatformnativeinterface.h>
26#include <QLibrary>
27#include <QDebug>
28#include <libintl.h>
29#include <dlfcn.h>
30#include <csignal>
32// local
33#include <paths.h>
34#include "MouseTouchAdaptor.h"
35#include "ApplicationArguments.h"
37#include <unity-mir/qmirserver.h>
40int startShell(int argc, const char** argv, void* server)
42 const bool isUbuntuMirServer = qgetenv("QT_QPA_PLATFORM") == "ubuntumirserver";
44 QGuiApplication::setApplicationName("Unity 8");
45 QGuiApplication *application;
46 if (isUbuntuMirServer) {
47 QLibrary unityMir("unity-mir", 1);
48 unityMir.load();
50 typedef QGuiApplication* (*createServerApplication_t)(int&, const char **, void*);
51 createServerApplication_t createQMirServerApplication = (createServerApplication_t) unityMir.resolve("createQMirServerApplication");
52 if (!createQMirServerApplication) {
53 qDebug() << "unable to resolve symbol: createQMirServerApplication";
54 return 4;
55 }
57 application = createQMirServerApplication(argc, argv, server);
58 } else {
59 application = new QGuiApplication(argc, (char**)argv);
60 }
62 QString indicatorProfile = qgetenv("UNITY_INDICATOR_PROFILE");
63 if (indicatorProfile.isEmpty()) {
64 indicatorProfile = "phone";
65 }
67 QStringList args = application->arguments();
68 ApplicationArguments qmlArgs(args);
70 // The testability driver is only loaded by QApplication but not by QGuiApplication.
71 // However, QApplication depends on QWidget which would add some unneeded overhead => Let's load the testability driver on our own.
72 if (args.contains(QLatin1String("-testability")) || getenv("QT_LOAD_TESTABILITY")) {
73 QLibrary testLib(QLatin1String("qttestability"));
74 if (testLib.load()) {
75 typedef void (*TasInitialize)(void);
76 TasInitialize initFunction = (TasInitialize)testLib.resolve("qt_testability_init");
77 if (initFunction) {
78 initFunction();
79 } else {
80 qCritical("Library qttestability resolve failed!");
81 }
82 } else {
83 qCritical("Library qttestability load failed!");
84 }
85 }
87 bindtextdomain("unity8", translationDirectory().toUtf8().data());
89 QQuickView* view = new QQuickView();
90 view->setResizeMode(QQuickView::SizeRootObjectToView);
91 view->setTitle("Qml Phone Shell");
92 view->engine()->setBaseUrl(QUrl::fromLocalFile(::qmlDirectory()));
93 view->rootContext()->setContextProperty("applicationArguments", &qmlArgs);
94 view->rootContext()->setContextProperty("indicatorProfile", indicatorProfile);
95 if (args.contains(QLatin1String("-frameless"))) {
96 view->setFlags(Qt::FramelessWindowHint);
97 }
99 // You will need this if you want to interact with touch-only components using a mouse
100 // Needed only when manually testing on a desktop.
101 MouseTouchAdaptor *mouseTouchAdaptor = 0;
102 if (args.contains(QLatin1String("-mousetouch"))) {
103 mouseTouchAdaptor = new MouseTouchAdaptor;
104 application->installNativeEventFilter(mouseTouchAdaptor);
105 }
107 QPlatformNativeInterface* nativeInterface = QGuiApplication::platformNativeInterface();
108 /* Shell is declared as a system session so that it always receives all
109 input events.
110 FIXME: use the enum value corresponding to SYSTEM_SESSION_TYPE (= 1)
111 when it becomes available.
112 */
113 nativeInterface->setProperty("ubuntuSessionType", 1);
114 view->setProperty("role", 2); // INDICATOR_ACTOR_ROLE
116 QUrl source(::qmlDirectory()+"Shell.qml");
117 prependImportPaths(view->engine(), ::overrideImportPaths());
118 appendImportPaths(view->engine(), ::fallbackImportPaths());
120 if (isUbuntuMirServer) {
121 QStringList importPaths = view->engine()->importPathList();
122 importPaths.replaceInStrings(QRegExp("qt5/imports$"), "qt5/imports/Unity-Mir");
123 view->engine()->setImportPathList(importPaths);
124 }
126 view->setSource(source);
127 view->setColor("transparent");
129 if (qgetenv("QT_QPA_PLATFORM") == "ubuntu" || isUbuntuMirServer || args.contains(QLatin1String("-fullscreen"))) {
130 view->showFullScreen();
131 } else {
132 view->show();
133 }
135 int result = application->exec();
137 delete view;
138 delete mouseTouchAdaptor;
139 delete application;
141 return result;
144int main(int argc, const char *argv[])
146 /* Workaround Qt platform integration plugin not advertising itself
147 as having the following capabilities:
148 - QPlatformIntegration::ThreadedOpenGL
149 - QPlatformIntegration::BufferQueueingOpenGL
150 */
151 setenv("QML_FORCE_THREADED_RENDERER", "1", 1);
152 setenv("QML_FIXED_ANIMATION_STEP", "1", 1);
154 // For ubuntumirserver/ubuntumirclient
155 if (qgetenv("QT_QPA_PLATFORM").startsWith("ubuntumir")) {
156 setenv("QT_QPA_PLATFORM", "ubuntumirserver", 1);
158 // If we use unity-mir directly, we automatically link to the Mir-server
159 // platform-api bindings, which result in unexpected behaviour when
160 // running the non-Mir scenario.
161 QLibrary unityMir("unity-mir", 1);
162 unityMir.load();
163 if (!unityMir.isLoaded()) {
164 qDebug() << "Library unity-mir not found/loaded";
165 return 1;
166 }
168 typedef QMirServer* (*createServer_t)(int, const char **);
169 createServer_t createQMirServer = (createServer_t) unityMir.resolve("createQMirServer");
170 if (!createQMirServer) {
171 qDebug() << "unable to resolve symbol: createQMirServer";
172 return 2;
173 }
175 QMirServer* mirServer = createQMirServer(argc, argv);
177 typedef int (*runWithClient_t)(QMirServer*, std::function<int(int, const char**, void*)>);
178 runWithClient_t runWithClient = (runWithClient_t) unityMir.resolve("runQMirServerWithClient");
179 if (!runWithClient) {
180 qDebug() << "unable to resolve symbol: runWithClient";
181 return 3;
182 }
184 return runWithClient(mirServer, startShell);
185 } else {
186 if (qgetenv("UPSTART_JOB") == "unity8") {
187 // Emit SIGSTOP as expected by upstart, under Mir it's unity-mir that will raise it.
188 // see http://upstart.ubuntu.com/cookbook/#expect-stop
189 raise(SIGSTOP);
190 }
191 return startShell(argc, argv, nullptr);
192 }