on be06bf0 help sign in
unity8/src/main.cpp
231 lines 8.7 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 <QCommandLineParser>
22#include <QtQuick/QQuickView>
23#include <QtGui/QGuiApplication>
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"
37#include "CachingNetworkManagerFactory.h"
39#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");
46 QGuiApplication *application;
48 QCommandLineParser parser;
49 parser.setApplicationDescription("Description: Unity 8 Shell");
50 parser.addHelpOption();
52 QCommandLineOption fullscreenOption("fullscreen",
53 "Run in fullscreen");
54 parser.addOption(fullscreenOption);
56 QCommandLineOption framelessOption("frameless",
57 "Run without window borders");
58 parser.addOption(framelessOption);
60 QCommandLineOption mousetouchOption("mousetouch",
61 "Allow the mouse to provide touch input");
62 parser.addOption(mousetouchOption);
64 QCommandLineOption windowGeometryOption(QStringList() << "windowgeometry",
65 "Specify the window geometry as [<width>x<height>]", "windowgeometry", "1");
66 parser.addOption(windowGeometryOption);
68 QCommandLineOption testabilityOption("testability",
69 "DISCOURAGED: Please set QT_LOAD_TESTABILITY instead. \n \
70Load the testability driver");
71 parser.addOption(testabilityOption);
73 if (isUbuntuMirServer) {
74 QLibrary unityMir("unity-mir", 1);
75 unityMir.load();
77 typedef QGuiApplication* (*createServerApplication_t)(int&, const char **, void*);
78 createServerApplication_t createQMirServerApplication = (createServerApplication_t) unityMir.resolve("createQMirServerApplication");
79 if (!createQMirServerApplication) {
80 qDebug() << "unable to resolve symbol: createQMirServerApplication";
81 return 4;
82 }
84 application = createQMirServerApplication(argc, argv, server);
85 } else {
86 application = new QGuiApplication(argc, (char**)argv);
87 }
89 // Treat args with single dashes the same as arguments with two dashes
90 // Ex: -fullscreen == --fullscreen
91 parser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions);
92 parser.process(*application);
94 QString indicatorProfile = qgetenv("UNITY_INDICATOR_PROFILE");
95 if (indicatorProfile.isEmpty()) {
96 indicatorProfile = "phone";
97 }
99 ApplicationArguments qmlArgs;
100 if (parser.isSet(windowGeometryOption) &&
101 parser.value(windowGeometryOption).split('x').size() == 2)
102 {
103 QStringList geom = parser.value(windowGeometryOption).split('x');
104 qmlArgs.setSize(geom.at(0).toInt(), geom.at(1).toInt());
105 }
107 // The testability driver is only loaded by QApplication but not by QGuiApplication.
108 // However, QApplication depends on QWidget which would add some unneeded overhead => Let's load the testability driver on our own.
109 if (parser.isSet(testabilityOption) || getenv("QT_LOAD_TESTABILITY")) {
110 QLibrary testLib(QLatin1String("qttestability"));
111 if (testLib.load()) {
112 typedef void (*TasInitialize)(void);
113 TasInitialize initFunction = (TasInitialize)testLib.resolve("qt_testability_init");
114 if (initFunction) {
115 initFunction();
116 } else {
117 qCritical("Library qttestability resolve failed!");
118 }
119 } else {
120 qCritical("Library qttestability load failed!");
121 }
122 }
124 bindtextdomain("unity8", translationDirectory().toUtf8().data());
126 QQuickView* view = new QQuickView();
127 view->setResizeMode(QQuickView::SizeRootObjectToView);
128 view->setTitle("Qml Phone Shell");
129 view->engine()->setBaseUrl(QUrl::fromLocalFile(::qmlDirectory()));
130 view->rootContext()->setContextProperty("applicationArguments", &qmlArgs);
131 view->rootContext()->setContextProperty("indicatorProfile", indicatorProfile);
132 if (parser.isSet(framelessOption)) {
133 view->setFlags(Qt::FramelessWindowHint);
134 }
136 // You will need this if you want to interact with touch-only components using a mouse
137 // Needed only when manually testing on a desktop.
138 MouseTouchAdaptor *mouseTouchAdaptor = 0;
139 if (parser.isSet(mousetouchOption)) {
140 mouseTouchAdaptor = new MouseTouchAdaptor;
141 application->installNativeEventFilter(mouseTouchAdaptor);
142 }
144 QPlatformNativeInterface* nativeInterface = QGuiApplication::platformNativeInterface();
145 /* Shell is declared as a system session so that it always receives all
146 input events.
147 FIXME: use the enum value corresponding to SYSTEM_SESSION_TYPE (= 1)
148 when it becomes available.
149 */
150 nativeInterface->setProperty("ubuntuSessionType", 1);
151 view->setProperty("role", 2); // INDICATOR_ACTOR_ROLE
153 QUrl source(::qmlDirectory()+"Shell.qml");
154 prependImportPaths(view->engine(), ::overrideImportPaths());
155 if (!isUbuntuMirServer) {
156 prependImportPaths(view->engine(), ::nonMirImportPaths());
157 }
158 appendImportPaths(view->engine(), ::fallbackImportPaths());
160 CachingNetworkManagerFactory *managerFactory = new CachingNetworkManagerFactory();
161 view->engine()->setNetworkAccessManagerFactory(managerFactory);
163 view->setSource(source);
164 view->setColor("transparent");
165 QObject::connect(view->engine(), SIGNAL(quit()), application, SLOT(quit()));
167 if (qgetenv("QT_QPA_PLATFORM") == "ubuntu" || isUbuntuMirServer || parser.isSet(fullscreenOption)) {
168 view->showFullScreen();
169 } else {
170 view->show();
171 }
173 int result = application->exec();
175 delete view;
176 delete mouseTouchAdaptor;
177 delete application;
179 return result;
182int main(int argc, const char *argv[])
184 /* Workaround Qt platform integration plugin not advertising itself
185 as having the following capabilities:
186 - QPlatformIntegration::ThreadedOpenGL
187 - QPlatformIntegration::BufferQueueingOpenGL
188 */
189 setenv("QML_FORCE_THREADED_RENDERER", "1", 1);
190 setenv("QML_FIXED_ANIMATION_STEP", "1", 1);
192 // For ubuntumirserver/ubuntumirclient
193 if (qgetenv("QT_QPA_PLATFORM").startsWith("ubuntumir")) {
194 setenv("QT_QPA_PLATFORM", "ubuntumirserver", 1);
196 // If we use unity-mir directly, we automatically link to the Mir-server
197 // platform-api bindings, which result in unexpected behaviour when
198 // running the non-Mir scenario.
199 QLibrary unityMir("unity-mir", 1);
200 unityMir.load();
201 if (!unityMir.isLoaded()) {
202 qDebug() << "Library unity-mir not found/loaded";
203 return 1;
204 }
206 typedef QMirServer* (*createServer_t)(int, const char **);
207 createServer_t createQMirServer = (createServer_t) unityMir.resolve("createQMirServer");
208 if (!createQMirServer) {
209 qDebug() << "unable to resolve symbol: createQMirServer";
210 return 2;
211 }
213 QMirServer* mirServer = createQMirServer(argc, argv);
215 typedef int (*runWithClient_t)(QMirServer*, std::function<int(int, const char**, void*)>);
216 runWithClient_t runWithClient = (runWithClient_t) unityMir.resolve("runQMirServerWithClient");
217 if (!runWithClient) {
218 qDebug() << "unable to resolve symbol: runWithClient";
219 return 3;
220 }
222 return runWithClient(mirServer, startShell);
223 } else {
224 if (!qgetenv("UNITY_MIR_EMITS_SIGSTOP").isEmpty()) {
225 // Emit SIGSTOP as expected by upstart, under Mir it's unity-mir that will raise it.
226 // see http://upstart.ubuntu.com/cookbook/#expect-stop
227 raise(SIGSTOP);
228 }
229 return startShell(argc, argv, nullptr);
230 }