on 090a35c help sign in
unity8/src/main.cpp
210 lines 7.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/QGuiApplication>
23#include <QtGui/QScreen>
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>
31#include <csignal>
33// local
34#include <paths.h>
35#include "MouseTouchAdaptor.h"
36#include "ApplicationArguments.h"
38#include <unity-mir/qmirserver.h>
41int startShell(int argc, const char** argv, void* server)
43 const bool isUbuntuMirServer = qgetenv("QT_QPA_PLATFORM") == "ubuntumirserver";
45 QGuiApplication::setApplicationName(UNITY8_SHELL ? "Unity 8" : "Unity 8 Greeter");
46 QGuiApplication *application;
47 if (isUbuntuMirServer) {
48 QLibrary unityMir("unity-mir", 1);
49 unityMir.load();
51 typedef QGuiApplication* (*createServerApplication_t)(int&, const char **, void*);
52 createServerApplication_t createQMirServerApplication = (createServerApplication_t) unityMir.resolve("createQMirServerApplication");
53 if (!createQMirServerApplication) {
54 qDebug() << "unable to resolve symbol: createQMirServerApplication";
55 return 4;
56 }
58 application = createQMirServerApplication(argc, argv, server);
59 } else {
60 application = new QGuiApplication(argc, (char**)argv);
61 }
63 QString indicatorProfile = qgetenv("UNITY_INDICATOR_PROFILE");
64 if (indicatorProfile.isEmpty()) {
65 indicatorProfile = "phone";
66 }
68 resolveIconTheme();
70 QStringList args = application->arguments();
71 ApplicationArguments qmlArgs(args);
73 // The testability driver is only loaded by QApplication but not by QGuiApplication.
74 // However, QApplication depends on QWidget which would add some unneeded overhead => Let's load the testability driver on our own.
75 if (args.contains(QLatin1String("-testability")) || getenv("QT_LOAD_TESTABILITY")) {
76 QLibrary testLib(QLatin1String("qttestability"));
77 if (testLib.load()) {
78 typedef void (*TasInitialize)(void);
79 TasInitialize initFunction = (TasInitialize)testLib.resolve("qt_testability_init");
80 if (initFunction) {
81 initFunction();
82 } else {
83 qCritical("Library qttestability resolve failed!");
84 }
85 } else {
86 qCritical("Library qttestability load failed!");
87 }
88 }
90 bindtextdomain("unity8", translationDirectory().toUtf8().data());
92 // Tell indicator plugin what profile to use
93 qputenv("UNITY8_INDICATOR_PROFILE", INDICATOR_PROFILE);
95 QQuickView* view = new QQuickView();
96 view->setResizeMode(QQuickView::SizeRootObjectToView);
97 view->setTitle("Qml Phone Shell");
98 view->engine()->setBaseUrl(QUrl::fromLocalFile(::qmlDirectory()));
99 view->rootContext()->setContextProperty("applicationArguments", &qmlArgs);
100 view->rootContext()->setContextProperty("indicatorProfile", indicatorProfile);
101 if (args.contains(QLatin1String("-frameless"))) {
102 view->setFlags(Qt::FramelessWindowHint);
103 }
105 // You will need this if you want to interact with touch-only components using a mouse
106 // Needed only when manually testing on a desktop.
107 MouseTouchAdaptor *mouseTouchAdaptor = 0;
108 if (args.contains(QLatin1String("-mousetouch"))) {
109 mouseTouchAdaptor = new MouseTouchAdaptor;
110 application->installNativeEventFilter(mouseTouchAdaptor);
111 }
113 QPlatformNativeInterface* nativeInterface = QGuiApplication::platformNativeInterface();
114 /* Shell is declared as a system session so that it always receives all
115 input events.
116 FIXME: use the enum value corresponding to SYSTEM_SESSION_TYPE (= 1)
117 when it becomes available.
118 */
119 nativeInterface->setProperty("ubuntuSessionType", 1);
120 view->setProperty("role", 2); // INDICATOR_ACTOR_ROLE
122 QUrl source(::qmlDirectory() + (UNITY8_SHELL ? "Shell.qml" : "GreeterShell.qml"));
123 prependImportPaths(view->engine(), ::overrideImportPaths());
124 appendImportPaths(view->engine(), ::fallbackImportPaths());
126 if (isUbuntuMirServer) {
127 QStringList importPaths = view->engine()->importPathList();
128 importPaths.replaceInStrings(QRegExp("qt5/imports$"), "qt5/imports/Unity-Mir");
129 view->engine()->setImportPathList(importPaths);
131#if !UNITY8_SHELL
132 // Make background transparent, so that the greeter can bleed through.
133 QSurfaceFormat format;
134 format.setAlphaBufferSize(8);
135 view->setFormat(format);
136 view->setColor(Qt::transparent);
137#endif
138 }
140 view->setSource(source);
142 if (qgetenv("QT_QPA_PLATFORM") == "ubuntu" || isUbuntuMirServer || args.contains(QLatin1String("-fullscreen"))) {
143 // First, size window equal to screen (fake a real WM fullscreen mode).
144 // Then, in case we are actually running inside a windowing system,
145 // nicely set actual WM fullscreen hint for its benefit.
146 view->resize(view->screen()->size());
147 view->showFullScreen();
148 } else {
149 view->show();
150 }
152 int result = application->exec();
154 delete view;
155 delete mouseTouchAdaptor;
156 delete application;
158 return result;
161int main(int argc, const char *argv[])
163 /* Workaround Qt platform integration plugin not advertising itself
164 as having the following capabilities:
165 - QPlatformIntegration::ThreadedOpenGL
166 - QPlatformIntegration::BufferQueueingOpenGL
167 */
168 setenv("QML_FORCE_THREADED_RENDERER", "1", 1);
169 setenv("QML_FIXED_ANIMATION_STEP", "1", 1);
171 // For ubuntumirserver/ubuntumirclient
172 if (qgetenv("QT_QPA_PLATFORM").startsWith("ubuntumir")) {
173 setenv("QT_QPA_PLATFORM", "ubuntumirserver", 1);
175 // If we use unity-mir directly, we automatically link to the Mir-server
176 // platform-api bindings, which result in unexpected behaviour when
177 // running the non-Mir scenario.
178 QLibrary unityMir("unity-mir", 1);
179 unityMir.load();
180 if (!unityMir.isLoaded()) {
181 qDebug() << "Library unity-mir not found/loaded";
182 return 1;
183 }
185 typedef QMirServer* (*createServer_t)(int, const char **);
186 createServer_t createQMirServer = (createServer_t) unityMir.resolve("createQMirServer");
187 if (!createQMirServer) {
188 qDebug() << "unable to resolve symbol: createQMirServer";
189 return 2;
190 }
192 QMirServer* mirServer = createQMirServer(argc, argv);
194 typedef int (*runWithClient_t)(QMirServer*, std::function<int(int, const char**, void*)>);
195 runWithClient_t runWithClient = (runWithClient_t) unityMir.resolve("runQMirServerWithClient");
196 if (!runWithClient) {
197 qDebug() << "unable to resolve symbol: runWithClient";
198 return 3;
199 }
201 return runWithClient(mirServer, startShell);
202 } else {
203 if (qgetenv("UPSTART_JOB") == "unity8") {
204 // Emit SIGSTOP as expected by upstart, under Mir it's unity-mir that will raise it.
205 // see http://upstart.ubuntu.com/cookbook/#expect-stop
206 raise(SIGSTOP);
207 }
208 return startShell(argc, argv, nullptr);
209 }