on 4f2dddb help sign in
unity8/src/main.cpp
204 lines 7.6 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 resolveIconTheme();
65 QStringList args = application->arguments();
66 ApplicationArguments qmlArgs(args);
68 // The testability driver is only loaded by QApplication but not by QGuiApplication.
69 // However, QApplication depends on QWidget which would add some unneeded overhead => Let's load the testability driver on our own.
70 if (args.contains(QLatin1String("-testability")) || getenv("QT_LOAD_TESTABILITY")) {
71 QLibrary testLib(QLatin1String("qttestability"));
72 if (testLib.load()) {
73 typedef void (*TasInitialize)(void);
74 TasInitialize initFunction = (TasInitialize)testLib.resolve("qt_testability_init");
75 if (initFunction) {
76 initFunction();
77 } else {
78 qCritical("Library qttestability resolve failed!");
79 }
80 } else {
81 qCritical("Library qttestability load failed!");
82 }
83 }
85 bindtextdomain("unity8", translationDirectory().toUtf8().data());
87 // Tell indicator plugin what profile to use
88 qputenv("UNITY8_INDICATOR_PROFILE", INDICATOR_PROFILE);
90 QQuickView* view = new QQuickView();
91 view->setResizeMode(QQuickView::SizeRootObjectToView);
92 view->setTitle("Qml Phone Shell");
93 view->engine()->setBaseUrl(QUrl::fromLocalFile(::qmlDirectory()));
94 view->rootContext()->setContextProperty("applicationArguments", &qmlArgs);
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() + (UNITY8_SHELL ? "Shell.qml" : "GreeterShell.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);
125#if !UNITY8_SHELL
126 // Make background transparent, so that the greeter can bleed through.
127 QSurfaceFormat format;
128 format.setAlphaBufferSize(8);
129 view->setFormat(format);
130 view->setColor(Qt::transparent);
131#endif
132 }
134 view->setSource(source);
136 if (qgetenv("QT_QPA_PLATFORM") == "ubuntu" || isUbuntuMirServer || args.contains(QLatin1String("-fullscreen"))) {
137 // First, size window equal to screen (fake a real WM fullscreen mode).
138 // Then, in case we are actually running inside a windowing system,
139 // nicely set actual WM fullscreen hint for its benefit.
140 view->resize(view->screen()->size());
141 view->showFullScreen();
142 } else {
143 view->show();
144 }
146 int result = application->exec();
148 delete view;
149 delete mouseTouchAdaptor;
150 delete application;
152 return result;
155int main(int argc, const char *argv[])
157 /* Workaround Qt platform integration plugin not advertising itself
158 as having the following capabilities:
159 - QPlatformIntegration::ThreadedOpenGL
160 - QPlatformIntegration::BufferQueueingOpenGL
161 */
162 setenv("QML_FORCE_THREADED_RENDERER", "1", 1);
163 setenv("QML_FIXED_ANIMATION_STEP", "1", 1);
165 // For ubuntumirserver/ubuntumirclient
166 if (qgetenv("QT_QPA_PLATFORM").startsWith("ubuntumir")) {
167 setenv("QT_QPA_PLATFORM", "ubuntumirserver", 1);
169 // If we use unity-mir directly, we automatically link to the Mir-server
170 // platform-api bindings, which result in unexpected behaviour when
171 // running the non-Mir scenario.
172 QLibrary unityMir("unity-mir", 1);
173 unityMir.load();
174 if (!unityMir.isLoaded()) {
175 qDebug() << "Library unity-mir not found/loaded";
176 return 1;
177 }
179 typedef QMirServer* (*createServer_t)(int, const char **);
180 createServer_t createQMirServer = (createServer_t) unityMir.resolve("createQMirServer");
181 if (!createQMirServer) {
182 qDebug() << "unable to resolve symbol: createQMirServer";
183 return 2;
184 }
186 QMirServer* mirServer = createQMirServer(argc, argv);
188 typedef int (*runWithClient_t)(QMirServer*, std::function<int(int, const char**, void*)>);
189 runWithClient_t runWithClient = (runWithClient_t) unityMir.resolve("runQMirServerWithClient");
190 if (!runWithClient) {
191 qDebug() << "unable to resolve symbol: runWithClient";
192 return 3;
193 }
195 return runWithClient(mirServer, startShell);
196 } else {
197 if (qgetenv("UPSTART_JOB") == "unity8") {
198 // Emit SIGSTOP as expected by upstart, under Mir it's unity-mir that will raise it.
199 // see http://upstart.ubuntu.com/cookbook/#expect-stop
200 raise(SIGSTOP);
201 }
202 return startShell(argc, argv, nullptr);
203 }