on 37dc525 help sign in
unity8/src/main.cpp
207 lines 7.8 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_GREETER ? "Unity 8 Greeter" : "Unity 8");
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 = UNITY8_GREETER ? "phone_greeter" : "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 QQuickView* view = new QQuickView();
93 view->setResizeMode(QQuickView::SizeRootObjectToView);
94 view->setTitle("Qml Phone Shell");
95 view->engine()->setBaseUrl(QUrl::fromLocalFile(::qmlDirectory()));
96 view->rootContext()->setContextProperty("applicationArguments", &qmlArgs);
97 view->rootContext()->setContextProperty("indicatorProfile", indicatorProfile);
98 if (args.contains(QLatin1String("-frameless"))) {
99 view->setFlags(Qt::FramelessWindowHint);
100 }
102 // You will need this if you want to interact with touch-only components using a mouse
103 // Needed only when manually testing on a desktop.
104 MouseTouchAdaptor *mouseTouchAdaptor = 0;
105 if (args.contains(QLatin1String("-mousetouch"))) {
106 mouseTouchAdaptor = new MouseTouchAdaptor;
107 application->installNativeEventFilter(mouseTouchAdaptor);
108 }
110 QPlatformNativeInterface* nativeInterface = QGuiApplication::platformNativeInterface();
111 /* Shell is declared as a system session so that it always receives all
112 input events.
113 FIXME: use the enum value corresponding to SYSTEM_SESSION_TYPE (= 1)
114 when it becomes available.
115 */
116 nativeInterface->setProperty("ubuntuSessionType", 1);
117 view->setProperty("role", 2); // INDICATOR_ACTOR_ROLE
119 QUrl source(::qmlDirectory() + (UNITY8_GREETER ? "GreeterShell.qml" : "Shell.qml"));
120 prependImportPaths(view->engine(), ::overrideImportPaths());
121 appendImportPaths(view->engine(), ::fallbackImportPaths());
123 if (isUbuntuMirServer) {
124 QStringList importPaths = view->engine()->importPathList();
125 importPaths.replaceInStrings(QRegExp("qt5/imports$"), "qt5/imports/Unity-Mir");
126 view->engine()->setImportPathList(importPaths);
128#if UNITY8_GREETER
129 // Add alpha to surface, so that the greeter can bleed through
130 QSurfaceFormat format;
131 format.setAlphaBufferSize(8);
132 view->setFormat(format);
133#endif
134 }
136 view->setSource(source);
137 view->setColor(Qt::transparent);
139 if (qgetenv("QT_QPA_PLATFORM") == "ubuntu" || isUbuntuMirServer || args.contains(QLatin1String("-fullscreen"))) {
140 // First, size window equal to screen (fake a real WM fullscreen mode).
141 // Then, in case we are actually running inside a windowing system,
142 // nicely set actual WM fullscreen hint for its benefit.
143 view->resize(view->screen()->size());
144 view->showFullScreen();
145 } else {
146 view->show();
147 }
149 int result = application->exec();
151 delete view;
152 delete mouseTouchAdaptor;
153 delete application;
155 return result;
158int main(int argc, const char *argv[])
160 /* Workaround Qt platform integration plugin not advertising itself
161 as having the following capabilities:
162 - QPlatformIntegration::ThreadedOpenGL
163 - QPlatformIntegration::BufferQueueingOpenGL
164 */
165 setenv("QML_FORCE_THREADED_RENDERER", "1", 1);
166 setenv("QML_FIXED_ANIMATION_STEP", "1", 1);
168 // For ubuntumirserver/ubuntumirclient
169 if (qgetenv("QT_QPA_PLATFORM").startsWith("ubuntumir")) {
170 setenv("QT_QPA_PLATFORM", "ubuntumirserver", 1);
172 // If we use unity-mir directly, we automatically link to the Mir-server
173 // platform-api bindings, which result in unexpected behaviour when
174 // running the non-Mir scenario.
175 QLibrary unityMir("unity-mir", 1);
176 unityMir.load();
177 if (!unityMir.isLoaded()) {
178 qDebug() << "Library unity-mir not found/loaded";
179 return 1;
180 }
182 typedef QMirServer* (*createServer_t)(int, const char **);
183 createServer_t createQMirServer = (createServer_t) unityMir.resolve("createQMirServer");
184 if (!createQMirServer) {
185 qDebug() << "unable to resolve symbol: createQMirServer";
186 return 2;
187 }
189 QMirServer* mirServer = createQMirServer(argc, argv);
191 typedef int (*runWithClient_t)(QMirServer*, std::function<int(int, const char**, void*)>);
192 runWithClient_t runWithClient = (runWithClient_t) unityMir.resolve("runQMirServerWithClient");
193 if (!runWithClient) {
194 qDebug() << "unable to resolve symbol: runWithClient";
195 return 3;
196 }
198 return runWithClient(mirServer, startShell);
199 } else {
200 if (qgetenv("UPSTART_JOB") == "unity8") {
201 // Emit SIGSTOP as expected by upstart, under Mir it's unity-mir that will raise it.
202 // see http://upstart.ubuntu.com/cookbook/#expect-stop
203 raise(SIGSTOP);
204 }
205 return startShell(argc, argv, nullptr);
206 }