on aad693f help sign in
unity8/src/main.cpp
156 lines 5.6 kB history raw
1/*
2 * Copyright (C) 2012-2014 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 <QLibrary>
27#include <QDebug>
28#include <libintl.h>
30// local
31#include <paths.h>
32#include "MouseTouchAdaptor.h"
33#include "ApplicationArguments.h"
34#include "CachingNetworkManagerFactory.h"
36int main(int argc, const char *argv[])
38 bool isMirServer = false;
39 if (qgetenv("QT_QPA_PLATFORM") == "ubuntumirclient") {
40 setenv("QT_QPA_PLATFORM", "mirserver", 1 /* overwrite */);
41 isMirServer = true;
42 }
44 QGuiApplication::setApplicationName("unity8");
45 QGuiApplication *application;
47 QCommandLineParser parser;
48 parser.setApplicationDescription("Description: Unity 8 Shell");
49 parser.addHelpOption();
51 QCommandLineOption fullscreenOption("fullscreen",
52 "Run in fullscreen");
53 parser.addOption(fullscreenOption);
55 QCommandLineOption framelessOption("frameless",
56 "Run without window borders");
57 parser.addOption(framelessOption);
59 QCommandLineOption mousetouchOption("mousetouch",
60 "Allow the mouse to provide touch input");
61 parser.addOption(mousetouchOption);
63 QCommandLineOption windowGeometryOption(QStringList() << "windowgeometry",
64 "Specify the window geometry as [<width>x<height>]", "windowgeometry", "1");
65 parser.addOption(windowGeometryOption);
67 QCommandLineOption testabilityOption("testability",
68 "DISCOURAGED: Please set QT_LOAD_TESTABILITY instead. \n \
69Load the testability driver");
70 parser.addOption(testabilityOption);
72 application = new QGuiApplication(argc, (char**)argv);
74 // Treat args with single dashes the same as arguments with two dashes
75 // Ex: -fullscreen == --fullscreen
76 parser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions);
77 parser.process(*application);
79 QString indicatorProfile = qgetenv("UNITY_INDICATOR_PROFILE");
80 if (indicatorProfile.isEmpty()) {
81 indicatorProfile = "phone";
82 }
84 ApplicationArguments qmlArgs;
85 if (parser.isSet(windowGeometryOption) &&
86 parser.value(windowGeometryOption).split('x').size() == 2)
87 {
88 QStringList geom = parser.value(windowGeometryOption).split('x');
89 qmlArgs.setSize(geom.at(0).toInt(), geom.at(1).toInt());
90 }
92 // The testability driver is only loaded by QApplication but not by QGuiApplication.
93 // However, QApplication depends on QWidget which would add some unneeded overhead => Let's load the testability driver on our own.
94 if (parser.isSet(testabilityOption) || getenv("QT_LOAD_TESTABILITY")) {
95 QLibrary testLib(QLatin1String("qttestability"));
96 if (testLib.load()) {
97 typedef void (*TasInitialize)(void);
98 TasInitialize initFunction = (TasInitialize)testLib.resolve("qt_testability_init");
99 if (initFunction) {
100 initFunction();
101 } else {
102 qCritical("Library qttestability resolve failed!");
103 }
104 } else {
105 qCritical("Library qttestability load failed!");
106 }
107 }
109 bindtextdomain("unity8", translationDirectory().toUtf8().data());
111 QQuickView* view = new QQuickView();
112 view->setResizeMode(QQuickView::SizeRootObjectToView);
113 view->setColor("black");
114 view->setTitle("Unity8 Shell");
115 view->engine()->setBaseUrl(QUrl::fromLocalFile(::qmlDirectory()));
116 view->rootContext()->setContextProperty("applicationArguments", &qmlArgs);
117 view->rootContext()->setContextProperty("indicatorProfile", indicatorProfile);
118 if (parser.isSet(framelessOption)) {
119 view->setFlags(Qt::FramelessWindowHint);
120 }
122 // You will need this if you want to interact with touch-only components using a mouse
123 // Needed only when manually testing on a desktop.
124 MouseTouchAdaptor *mouseTouchAdaptor = 0;
125 if (parser.isSet(mousetouchOption)) {
126 mouseTouchAdaptor = new MouseTouchAdaptor;
127 application->installNativeEventFilter(mouseTouchAdaptor);
128 }
130 QUrl source(::qmlDirectory()+"Shell.qml");
131 prependImportPaths(view->engine(), ::overrideImportPaths());
132 if (!isMirServer) {
133 prependImportPaths(view->engine(), ::nonMirImportPaths());
134 }
135 appendImportPaths(view->engine(), ::fallbackImportPaths());
137 CachingNetworkManagerFactory *managerFactory = new CachingNetworkManagerFactory();
138 view->engine()->setNetworkAccessManagerFactory(managerFactory);
140 view->setSource(source);
141 QObject::connect(view->engine(), SIGNAL(quit()), application, SLOT(quit()));
143 if (isMirServer || parser.isSet(fullscreenOption)) {
144 view->showFullScreen();
145 } else {
146 view->show();
147 }
149 int result = application->exec();
151 delete view;
152 delete mouseTouchAdaptor;
153 delete application;
155 return result;