on fcfe3a0 help sign in
unity8/src/main.cpp
180 lines 6.6 kB history raw
1/*
2 * Copyright (C) 2012-2015 Canonical, Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
17// Qt
18#include <QCommandLineParser>
19#include <QtQuick/QQuickView>
20#include <QtGui/QGuiApplication>
21#include <QtQml/QQmlEngine>
22#include <QtQml/QQmlContext>
23#include <QLibrary>
24#include <QDebug>
25#include <csignal>
26#include <libintl.h>
28// local
29#include <paths.h>
30#include "MouseTouchAdaptor.h"
31#include "ApplicationArguments.h"
32#include "CachingNetworkManagerFactory.h"
34int main(int argc, const char *argv[])
36 bool isMirServer = false;
37 if (qgetenv("QT_QPA_PLATFORM") == "ubuntumirclient") {
38 setenv("QT_QPA_PLATFORM", "mirserver", 1 /* overwrite */);
39 isMirServer = true;
40 }
42 QGuiApplication::setApplicationName("unity8");
43 QGuiApplication *application;
45 QCommandLineParser parser;
46 parser.setApplicationDescription("Description: Unity 8 Shell");
47 parser.addHelpOption();
49 QCommandLineOption fullscreenOption("fullscreen",
50 "Run in fullscreen");
51 parser.addOption(fullscreenOption);
53 QCommandLineOption framelessOption("frameless",
54 "Run without window borders");
55 parser.addOption(framelessOption);
57 QCommandLineOption mousetouchOption("mousetouch",
58 "Allow the mouse to provide touch input");
59 parser.addOption(mousetouchOption);
61 QCommandLineOption windowGeometryOption(QStringList() << "windowgeometry",
62 "Specify the window geometry as [<width>x<height>]", "windowgeometry", "1");
63 parser.addOption(windowGeometryOption);
65 QCommandLineOption testabilityOption("testability",
66 "DISCOURAGED: Please set QT_LOAD_TESTABILITY instead. \n \
67Load the testability driver");
68 parser.addOption(testabilityOption);
70 QCommandLineOption modeOption("mode",
71 "Whether to run greeter and/or shell [full-greeter, full-shell, greeter, shell]",
72 "mode", "full-greeter");
73 parser.addOption(modeOption);
75 application = new QGuiApplication(argc, (char**)argv);
77 // Treat args with single dashes the same as arguments with two dashes
78 // Ex: -fullscreen == --fullscreen
79 parser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions);
80 parser.process(*application);
82 QString indicatorProfile = qgetenv("UNITY_INDICATOR_PROFILE");
83 if (indicatorProfile.isEmpty()) {
84 indicatorProfile = "phone";
85 }
87 ApplicationArguments qmlArgs;
88 if (parser.isSet(windowGeometryOption) &&
89 parser.value(windowGeometryOption).split('x').size() == 2)
90 {
91 QStringList geom = parser.value(windowGeometryOption).split('x');
92 qmlArgs.setSize(geom.at(0).toInt(), geom.at(1).toInt());
93 }
95 // If an invalid option was specified, set it to the default
96 // If no default was provided in the QCommandLineOption constructor, abort.
97 QString shellMode;
98 if(!parser.isSet(modeOption) ||
99 (parser.value(modeOption) != "full-greeter" &&
100 parser.value(modeOption) != "full-shell" &&
101 parser.value(modeOption) != "greeter" &&
102 parser.value(modeOption) != "shell"))
103 {
104 if (modeOption.defaultValues().first() != nullptr) {
105 shellMode = modeOption.defaultValues().first();
106 qWarning() << "Mode argument was not provided or was set to an illegal value. Using default value of --mode=" << shellMode;
107 } else {
108 qFatal("Shell mode argument was not provided and there is no default mode,");
109 }
111 } else {
112 shellMode = parser.value(modeOption);
113 }
115 // The testability driver is only loaded by QApplication but not by QGuiApplication.
116 // However, QApplication depends on QWidget which would add some unneeded overhead => Let's load the testability driver on our own.
117 if (parser.isSet(testabilityOption) || getenv("QT_LOAD_TESTABILITY")) {
118 QLibrary testLib(QLatin1String("qttestability"));
119 if (testLib.load()) {
120 typedef void (*TasInitialize)(void);
121 TasInitialize initFunction = (TasInitialize)testLib.resolve("qt_testability_init");
122 if (initFunction) {
123 initFunction();
124 } else {
125 qCritical("Library qttestability resolve failed!");
126 }
127 } else {
128 qCritical("Library qttestability load failed!");
129 }
130 }
132 bindtextdomain("unity8", translationDirectory().toUtf8().data());
133 textdomain("unity8");
135 QQuickView* view = new QQuickView();
136 view->setResizeMode(QQuickView::SizeRootObjectToView);
137 view->setColor("black");
138 view->setTitle("Unity8 Shell");
139 view->engine()->setBaseUrl(QUrl::fromLocalFile(::qmlDirectory()));
140 view->rootContext()->setContextProperty("applicationArguments", &qmlArgs);
141 view->rootContext()->setContextProperty("indicatorProfile", indicatorProfile);
142 view->rootContext()->setContextProperty("shellMode", shellMode);
143 if (parser.isSet(framelessOption)) {
144 view->setFlags(Qt::FramelessWindowHint);
145 }
147 // You will need this if you want to interact with touch-only components using a mouse
148 // Needed only when manually testing on a desktop.
149 MouseTouchAdaptor *mouseTouchAdaptor = 0;
150 if (parser.isSet(mousetouchOption)) {
151 mouseTouchAdaptor = MouseTouchAdaptor::instance();
152 }
154 QUrl source(::qmlDirectory()+"Shell.qml");
155 prependImportPaths(view->engine(), ::overrideImportPaths());
156 if (!isMirServer) {
157 prependImportPaths(view->engine(), ::nonMirImportPaths());
158 }
159 appendImportPaths(view->engine(), ::fallbackImportPaths());
161 CachingNetworkManagerFactory *managerFactory = new CachingNetworkManagerFactory();
162 view->engine()->setNetworkAccessManagerFactory(managerFactory);
164 view->setSource(source);
165 QObject::connect(view->engine(), SIGNAL(quit()), application, SLOT(quit()));
167 if (isMirServer || 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;