on 97d84ce help sign in
unity8/src/main.cpp
220 lines 8.0 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"
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("Unity 8");
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 testabilityOption("testability",
65 "DISCOURAGED: Please set QT_LOAD_TESTABILITY instead. \n \
66Load the testability driver");
67 parser.addOption(testabilityOption);
69 if (isUbuntuMirServer) {
70 QLibrary unityMir("unity-mir", 1);
71 unityMir.load();
73 typedef QGuiApplication* (*createServerApplication_t)(int&, const char **, void*);
74 createServerApplication_t createQMirServerApplication = (createServerApplication_t) unityMir.resolve("createQMirServerApplication");
75 if (!createQMirServerApplication) {
76 qDebug() << "unable to resolve symbol: createQMirServerApplication";
77 return 4;
78 }
80 application = createQMirServerApplication(argc, argv, server);
81 } else {
82 application = new QGuiApplication(argc, (char**)argv);
83 }
85 // Treat args with single dashes the same as arguments with two dashes
86 // Ex: -fullscreen == --fullscreen
87 parser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions);
88 parser.process(*application);
90 QString indicatorProfile = qgetenv("UNITY_INDICATOR_PROFILE");
91 if (indicatorProfile.isEmpty()) {
92 indicatorProfile = "phone";
93 }
95 resolveIconTheme();
97 QStringList args = application->arguments();
98 ApplicationArguments qmlArgs(args);
100 // The testability driver is only loaded by QApplication but not by QGuiApplication.
101 // However, QApplication depends on QWidget which would add some unneeded overhead => Let's load the testability driver on our own.
102 if (parser.isSet(testabilityOption) || getenv("QT_LOAD_TESTABILITY")) {
103 QLibrary testLib(QLatin1String("qttestability"));
104 if (testLib.load()) {
105 typedef void (*TasInitialize)(void);
106 TasInitialize initFunction = (TasInitialize)testLib.resolve("qt_testability_init");
107 if (initFunction) {
108 initFunction();
109 } else {
110 qCritical("Library qttestability resolve failed!");
111 }
112 } else {
113 qCritical("Library qttestability load failed!");
114 }
115 }
117 bindtextdomain("unity8", translationDirectory().toUtf8().data());
119 QQuickView* view = new QQuickView();
120 view->setResizeMode(QQuickView::SizeRootObjectToView);
121 view->setTitle("Qml Phone Shell");
122 view->engine()->setBaseUrl(QUrl::fromLocalFile(::qmlDirectory()));
123 view->rootContext()->setContextProperty("applicationArguments", &qmlArgs);
124 view->rootContext()->setContextProperty("indicatorProfile", indicatorProfile);
125 if (parser.isSet(framelessOption)) {
126 view->setFlags(Qt::FramelessWindowHint);
127 }
129 // You will need this if you want to interact with touch-only components using a mouse
130 // Needed only when manually testing on a desktop.
131 MouseTouchAdaptor *mouseTouchAdaptor = 0;
132 if (parser.isSet(mousetouchOption)) {
133 mouseTouchAdaptor = new MouseTouchAdaptor;
134 application->installNativeEventFilter(mouseTouchAdaptor);
135 }
137 QPlatformNativeInterface* nativeInterface = QGuiApplication::platformNativeInterface();
138 /* Shell is declared as a system session so that it always receives all
139 input events.
140 FIXME: use the enum value corresponding to SYSTEM_SESSION_TYPE (= 1)
141 when it becomes available.
142 */
143 nativeInterface->setProperty("ubuntuSessionType", 1);
144 view->setProperty("role", 2); // INDICATOR_ACTOR_ROLE
146 QUrl source(::qmlDirectory()+"Shell.qml");
147 prependImportPaths(view->engine(), ::overrideImportPaths());
148 if (!isUbuntuMirServer) {
149 prependImportPaths(view->engine(), ::nonMirImportPaths());
150 }
151 appendImportPaths(view->engine(), ::fallbackImportPaths());
153 view->setSource(source);
154 view->setColor("transparent");
156 if (qgetenv("QT_QPA_PLATFORM") == "ubuntu" || isUbuntuMirServer || parser.isSet(fullscreenOption)) {
157 view->showFullScreen();
158 } else {
159 view->show();
160 }
162 int result = application->exec();
164 delete view;
165 delete mouseTouchAdaptor;
166 delete application;
168 return result;
171int main(int argc, const char *argv[])
173 /* Workaround Qt platform integration plugin not advertising itself
174 as having the following capabilities:
175 - QPlatformIntegration::ThreadedOpenGL
176 - QPlatformIntegration::BufferQueueingOpenGL
177 */
178 setenv("QML_FORCE_THREADED_RENDERER", "1", 1);
179 setenv("QML_FIXED_ANIMATION_STEP", "1", 1);
181 // For ubuntumirserver/ubuntumirclient
182 if (qgetenv("QT_QPA_PLATFORM").startsWith("ubuntumir")) {
183 setenv("QT_QPA_PLATFORM", "ubuntumirserver", 1);
185 // If we use unity-mir directly, we automatically link to the Mir-server
186 // platform-api bindings, which result in unexpected behaviour when
187 // running the non-Mir scenario.
188 QLibrary unityMir("unity-mir", 1);
189 unityMir.load();
190 if (!unityMir.isLoaded()) {
191 qDebug() << "Library unity-mir not found/loaded";
192 return 1;
193 }
195 typedef QMirServer* (*createServer_t)(int, const char **);
196 createServer_t createQMirServer = (createServer_t) unityMir.resolve("createQMirServer");
197 if (!createQMirServer) {
198 qDebug() << "unable to resolve symbol: createQMirServer";
199 return 2;
200 }
202 QMirServer* mirServer = createQMirServer(argc, argv);
204 typedef int (*runWithClient_t)(QMirServer*, std::function<int(int, const char**, void*)>);
205 runWithClient_t runWithClient = (runWithClient_t) unityMir.resolve("runQMirServerWithClient");
206 if (!runWithClient) {
207 qDebug() << "unable to resolve symbol: runWithClient";
208 return 3;
209 }
211 return runWithClient(mirServer, startShell);
212 } else {
213 if (qgetenv("UPSTART_JOB") == "unity8") {
214 // Emit SIGSTOP as expected by upstart, under Mir it's unity-mir that will raise it.
215 // see http://upstart.ubuntu.com/cookbook/#expect-stop
216 raise(SIGSTOP);
217 }
218 return startShell(argc, argv, nullptr);
219 }