on cf40464 help sign in
unity8/src/main.cpp
243 lines 8.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 <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 <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;
49 QCommandLineParser parser;
50 parser.setApplicationDescription("Description: Unity 8 Shell");
51 parser.addHelpOption();
53 QCommandLineOption fullscreenOption("fullscreen",
54 "Run in fullscreen");
55 parser.addOption(fullscreenOption);
57 QCommandLineOption framelessOption("frameless",
58 "Run without window borders");
59 parser.addOption(framelessOption);
61 QCommandLineOption mousetouchOption("mousetouch",
62 "Allow the mouse to provide touch input");
63 parser.addOption(mousetouchOption);
65 QCommandLineOption windowGeometryOption(QStringList() << "windowgeometry",
66 "Specify the window geometry as [<width>x<height>]", "windowgeometry", "1");
67 parser.addOption(windowGeometryOption);
69 QCommandLineOption testabilityOption("testability",
70 "DISCOURAGED: Please set QT_LOAD_TESTABILITY instead. \n \
71Load the testability driver");
72 parser.addOption(testabilityOption);
74 if (isUbuntuMirServer) {
75 QLibrary unityMir("unity-mir", 1);
76 unityMir.load();
78 typedef QGuiApplication* (*createServerApplication_t)(int&, const char **, void*);
79 createServerApplication_t createQMirServerApplication = (createServerApplication_t) unityMir.resolve("createQMirServerApplication");
80 if (!createQMirServerApplication) {
81 qDebug() << "unable to resolve symbol: createQMirServerApplication";
82 return 4;
83 }
85 application = createQMirServerApplication(argc, argv, server);
86 } else {
87 application = new QGuiApplication(argc, (char**)argv);
88 }
90 // Treat args with single dashes the same as arguments with two dashes
91 // Ex: -fullscreen == --fullscreen
92 parser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions);
93 parser.process(*application);
95 QString indicatorProfile = qgetenv("UNITY_INDICATOR_PROFILE");
96 if (indicatorProfile.isEmpty()) {
97 indicatorProfile = UNITY8_GREETER ? "phone_greeter" : "phone";
98 }
100 resolveIconTheme();
102 ApplicationArguments qmlArgs;
103 if (parser.isSet(windowGeometryOption) &&
104 parser.value(windowGeometryOption).split('x').size() == 2)
105 {
106 QStringList geom = parser.value(windowGeometryOption).split('x');
107 qmlArgs.setSize(geom.at(0).toInt(), geom.at(1).toInt());
108 }
110 // The testability driver is only loaded by QApplication but not by QGuiApplication.
111 // However, QApplication depends on QWidget which would add some unneeded overhead => Let's load the testability driver on our own.
112 if (parser.isSet(testabilityOption) || getenv("QT_LOAD_TESTABILITY")) {
113 QLibrary testLib(QLatin1String("qttestability"));
114 if (testLib.load()) {
115 typedef void (*TasInitialize)(void);
116 TasInitialize initFunction = (TasInitialize)testLib.resolve("qt_testability_init");
117 if (initFunction) {
118 initFunction();
119 } else {
120 qCritical("Library qttestability resolve failed!");
121 }
122 } else {
123 qCritical("Library qttestability load failed!");
124 }
125 }
127 bindtextdomain("unity8", translationDirectory().toUtf8().data());
129 QQuickView* view = new QQuickView();
130 view->setResizeMode(QQuickView::SizeRootObjectToView);
131 view->setTitle("Qml Phone Shell");
132 view->engine()->setBaseUrl(QUrl::fromLocalFile(::qmlDirectory()));
133 view->rootContext()->setContextProperty("applicationArguments", &qmlArgs);
134 view->rootContext()->setContextProperty("indicatorProfile", indicatorProfile);
135 if (parser.isSet(framelessOption)) {
136 view->setFlags(Qt::FramelessWindowHint);
137 }
139 // You will need this if you want to interact with touch-only components using a mouse
140 // Needed only when manually testing on a desktop.
141 MouseTouchAdaptor *mouseTouchAdaptor = 0;
142 if (parser.isSet(mousetouchOption)) {
143 mouseTouchAdaptor = new MouseTouchAdaptor;
144 application->installNativeEventFilter(mouseTouchAdaptor);
145 }
147 QPlatformNativeInterface* nativeInterface = QGuiApplication::platformNativeInterface();
148 /* Shell is declared as a system session so that it always receives all
149 input events.
150 FIXME: use the enum value corresponding to SYSTEM_SESSION_TYPE (= 1)
151 when it becomes available.
152 */
153 nativeInterface->setProperty("ubuntuSessionType", 1);
154 view->setProperty("role", 2); // INDICATOR_ACTOR_ROLE
156 QUrl source(::qmlDirectory() + (UNITY8_GREETER ? "GreeterShell.qml" : "Shell.qml"));
157 prependImportPaths(view->engine(), ::overrideImportPaths());
158 if (!isUbuntuMirServer) {
159 prependImportPaths(view->engine(), ::nonMirImportPaths());
160 }
161 appendImportPaths(view->engine(), ::fallbackImportPaths());
163 view->setSource(source);
164 view->setColor(Qt::transparent);
166 if (qgetenv("QT_QPA_PLATFORM") == "ubuntu" || isUbuntuMirServer || parser.isSet(fullscreenOption)) {
167 view->showFullScreen();
168 } else {
169 view->show();
170 }
172#if UNITY8_GREETER
173 if (isUbuntuMirServer) {
174 // Add alpha to surface, so that the greeter can bleed through
175 QSurfaceFormat format;
176 format.setAlphaBufferSize(8);
177 view->setFormat(format);
178 }
180 if (!QProcess::startDetached("/sbin/initctl emit --no-wait unity8-greeter-started")) {
181 qDebug() << "Unable to send unity8-greeter-started event to Upstart";
182 }
183#endif
185 int result = application->exec();
187 delete view;
188 delete mouseTouchAdaptor;
189 delete application;
191 return result;
194int main(int argc, const char *argv[])
196 /* Workaround Qt platform integration plugin not advertising itself
197 as having the following capabilities:
198 - QPlatformIntegration::ThreadedOpenGL
199 - QPlatformIntegration::BufferQueueingOpenGL
200 */
201 setenv("QML_FORCE_THREADED_RENDERER", "1", 1);
202 setenv("QML_FIXED_ANIMATION_STEP", "1", 1);
204 // For ubuntumirserver/ubuntumirclient
205 if (qgetenv("QT_QPA_PLATFORM").startsWith("ubuntumir")) {
206 setenv("QT_QPA_PLATFORM", "ubuntumirserver", 1);
208 // If we use unity-mir directly, we automatically link to the Mir-server
209 // platform-api bindings, which result in unexpected behaviour when
210 // running the non-Mir scenario.
211 QLibrary unityMir("unity-mir", 1);
212 unityMir.load();
213 if (!unityMir.isLoaded()) {
214 qDebug() << "Library unity-mir not found/loaded";
215 return 1;
216 }
218 typedef QMirServer* (*createServer_t)(int, const char **);
219 createServer_t createQMirServer = (createServer_t) unityMir.resolve("createQMirServer");
220 if (!createQMirServer) {
221 qDebug() << "unable to resolve symbol: createQMirServer";
222 return 2;
223 }
225 QMirServer* mirServer = createQMirServer(argc, argv);
227 typedef int (*runWithClient_t)(QMirServer*, std::function<int(int, const char**, void*)>);
228 runWithClient_t runWithClient = (runWithClient_t) unityMir.resolve("runQMirServerWithClient");
229 if (!runWithClient) {
230 qDebug() << "unable to resolve symbol: runWithClient";
231 return 3;
232 }
234 return runWithClient(mirServer, startShell);
235 } else {
236 if (qgetenv("UPSTART_JOB") == "unity8") {
237 // Emit SIGSTOP as expected by upstart, under Mir it's unity-mir that will raise it.
238 // see http://upstart.ubuntu.com/cookbook/#expect-stop
239 raise(SIGSTOP);
240 }
241 return startShell(argc, argv, nullptr);
242 }