on b6fd562 help sign in
unity8/src/main.cpp
211 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 <QProcess>
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>
42int startShell(int argc, const char** argv, void* server)
44 const bool isUbuntuMirServer = qgetenv("QT_QPA_PLATFORM") == "ubuntumirserver";
46 QGuiApplication::setApplicationName(UNITY8_GREETER ? "Unity 8 Greeter" : "Unity 8");
47 QGuiApplication *application;
48 if (isUbuntuMirServer) {
49 QLibrary unityMir("unity-mir", 1);
50 unityMir.load();
52 typedef QGuiApplication* (*createServerApplication_t)(int&, const char **, void*);
53 createServerApplication_t createQMirServerApplication = (createServerApplication_t) unityMir.resolve("createQMirServerApplication");
54 if (!createQMirServerApplication) {
55 qDebug() << "unable to resolve symbol: createQMirServerApplication";
56 return 4;
57 }
59 application = createQMirServerApplication(argc, argv, server);
60 } else {
61 application = new QGuiApplication(argc, (char**)argv);
62 }
64 QString indicatorProfile = qgetenv("UNITY_INDICATOR_PROFILE");
65 if (indicatorProfile.isEmpty()) {
66 indicatorProfile = UNITY8_GREETER ? "phone_greeter" : "phone";
67 }
69 resolveIconTheme();
71 QStringList args = application->arguments();
72 ApplicationArguments qmlArgs(args);
74 // The testability driver is only loaded by QApplication but not by QGuiApplication.
75 // However, QApplication depends on QWidget which would add some unneeded overhead => Let's load the testability driver on our own.
76 if (args.contains(QLatin1String("-testability")) || getenv("QT_LOAD_TESTABILITY")) {
77 QLibrary testLib(QLatin1String("qttestability"));
78 if (testLib.load()) {
79 typedef void (*TasInitialize)(void);
80 TasInitialize initFunction = (TasInitialize)testLib.resolve("qt_testability_init");
81 if (initFunction) {
82 initFunction();
83 } else {
84 qCritical("Library qttestability resolve failed!");
85 }
86 } else {
87 qCritical("Library qttestability load failed!");
88 }
89 }
91 bindtextdomain("unity8", translationDirectory().toUtf8().data());
93 QQuickView* view = new QQuickView();
94 view->setResizeMode(QQuickView::SizeRootObjectToView);
95 view->setTitle("Qml Phone Shell");
96 view->engine()->setBaseUrl(QUrl::fromLocalFile(::qmlDirectory()));
97 view->rootContext()->setContextProperty("applicationArguments", &qmlArgs);
98 view->rootContext()->setContextProperty("indicatorProfile", indicatorProfile);
99 if (args.contains(QLatin1String("-frameless"))) {
100 view->setFlags(Qt::FramelessWindowHint);
101 }
103 // You will need this if you want to interact with touch-only components using a mouse
104 // Needed only when manually testing on a desktop.
105 MouseTouchAdaptor *mouseTouchAdaptor = 0;
106 if (args.contains(QLatin1String("-mousetouch"))) {
107 mouseTouchAdaptor = new MouseTouchAdaptor;
108 application->installNativeEventFilter(mouseTouchAdaptor);
109 }
111 QPlatformNativeInterface* nativeInterface = QGuiApplication::platformNativeInterface();
112 /* Shell is declared as a system session so that it always receives all
113 input events.
114 FIXME: use the enum value corresponding to SYSTEM_SESSION_TYPE (= 1)
115 when it becomes available.
116 */
117 nativeInterface->setProperty("ubuntuSessionType", 1);
118 view->setProperty("role", 2); // INDICATOR_ACTOR_ROLE
120 QUrl source(::qmlDirectory() + (UNITY8_GREETER ? "GreeterShell.qml" : "Shell.qml"));
121 prependImportPaths(view->engine(), ::overrideImportPaths());
122 if (!isUbuntuMirServer) {
123 prependImportPaths(view->engine(), ::nonMirImportPaths());
124 }
125 appendImportPaths(view->engine(), ::fallbackImportPaths());
127#if UNITY8_GREETER
128 if (isUbuntuMirServer) {
129 // Add alpha to surface, so that the greeter can bleed through
130 QSurfaceFormat format;
131 format.setAlphaBufferSize(8);
132 view->setFormat(format);
133 }
135 if (!QProcess::startDetached("/sbin/initctl emit --no-wait unity8-greeter-started")) {
136 qDebug() << "Unable to send unity8-greeter-started event to Upstart";
137 }
138#endif
140 view->setSource(source);
141 view->setColor(Qt::transparent);
143 if (qgetenv("QT_QPA_PLATFORM") == "ubuntu" || isUbuntuMirServer || args.contains(QLatin1String("-fullscreen"))) {
144 // First, size window equal to screen (fake a real WM fullscreen mode).
145 // Then, in case we are actually running inside a windowing system,
146 // nicely set actual WM fullscreen hint for its benefit.
147 view->resize(view->screen()->size());
148 view->showFullScreen();
149 } else {
150 view->show();
151 }
153 int result = application->exec();
155 delete view;
156 delete mouseTouchAdaptor;
157 delete application;
159 return result;
162int main(int argc, const char *argv[])
164 /* Workaround Qt platform integration plugin not advertising itself
165 as having the following capabilities:
166 - QPlatformIntegration::ThreadedOpenGL
167 - QPlatformIntegration::BufferQueueingOpenGL
168 */
169 setenv("QML_FORCE_THREADED_RENDERER", "1", 1);
170 setenv("QML_FIXED_ANIMATION_STEP", "1", 1);
172 // For ubuntumirserver/ubuntumirclient
173 if (qgetenv("QT_QPA_PLATFORM").startsWith("ubuntumir")) {
174 setenv("QT_QPA_PLATFORM", "ubuntumirserver", 1);
176 // If we use unity-mir directly, we automatically link to the Mir-server
177 // platform-api bindings, which result in unexpected behaviour when
178 // running the non-Mir scenario.
179 QLibrary unityMir("unity-mir", 1);
180 unityMir.load();
181 if (!unityMir.isLoaded()) {
182 qDebug() << "Library unity-mir not found/loaded";
183 return 1;
184 }
186 typedef QMirServer* (*createServer_t)(int, const char **);
187 createServer_t createQMirServer = (createServer_t) unityMir.resolve("createQMirServer");
188 if (!createQMirServer) {
189 qDebug() << "unable to resolve symbol: createQMirServer";
190 return 2;
191 }
193 QMirServer* mirServer = createQMirServer(argc, argv);
195 typedef int (*runWithClient_t)(QMirServer*, std::function<int(int, const char**, void*)>);
196 runWithClient_t runWithClient = (runWithClient_t) unityMir.resolve("runQMirServerWithClient");
197 if (!runWithClient) {
198 qDebug() << "unable to resolve symbol: runWithClient";
199 return 3;
200 }
202 return runWithClient(mirServer, startShell);
203 } else {
204 if (qgetenv("UPSTART_JOB") == "unity8") {
205 // Emit SIGSTOP as expected by upstart, under Mir it's unity-mir that will raise it.
206 // see http://upstart.ubuntu.com/cookbook/#expect-stop
207 raise(SIGSTOP);
208 }
209 return startShell(argc, argv, nullptr);
210 }