Unity 8
main.cpp
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  */
16 
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>
27 
28 // libandroid-properties
29 #include <hybris/properties/properties.h>
30 
31 // local
32 #include <paths.h>
33 #include "MouseTouchAdaptor.h"
34 #include "ApplicationArguments.h"
35 #include "CachingNetworkManagerFactory.h"
36 #include "UnityCommandLineParser.h"
37 
38 int main(int argc, const char *argv[])
39 {
40  bool isMirServer = false;
41  if (qgetenv("QT_QPA_PLATFORM") == "ubuntumirclient") {
42  setenv("QT_QPA_PLATFORM", "mirserver", 1 /* overwrite */);
43  isMirServer = true;
44  }
45 
46  QGuiApplication::setApplicationName("unity8");
47  QGuiApplication *application;
48 
49  application = new QGuiApplication(argc, (char**)argv);
50 
51  UnityCommandLineParser parser(*application);
52 
53  ApplicationArguments qmlArgs;
54 
55  if (!parser.deviceName().isEmpty()) {
56  qmlArgs.setDeviceName(parser.deviceName());
57  } else {
58  char buffer[200];
59  property_get("ro.product.device", buffer /* value */, "desktop" /* default_value*/);
60  qmlArgs.setDeviceName(QString(buffer));
61  }
62 
63  qmlArgs.setMode(parser.mode());
64 
65  // The testability driver is only loaded by QApplication but not by QGuiApplication.
66  // However, QApplication depends on QWidget which would add some unneeded overhead => Let's load the testability driver on our own.
67  if (parser.hasTestability() || getenv("QT_LOAD_TESTABILITY")) {
68  QLibrary testLib(QLatin1String("qttestability"));
69  if (testLib.load()) {
70  typedef void (*TasInitialize)(void);
71  TasInitialize initFunction = (TasInitialize)testLib.resolve("qt_testability_init");
72  if (initFunction) {
73  initFunction();
74  } else {
75  qCritical("Library qttestability resolve failed!");
76  }
77  } else {
78  qCritical("Library qttestability load failed!");
79  }
80  }
81 
82  bindtextdomain("unity8", translationDirectory().toUtf8().data());
83  textdomain("unity8");
84 
85  QQuickView* view = new QQuickView();
86  view->setResizeMode(QQuickView::SizeRootObjectToView);
87  view->setColor("black");
88  view->setTitle("Unity8 Shell");
89 
90  if (parser.windowGeometry().isValid()) {
91  view->setWidth(parser.windowGeometry().width());
92  view->setHeight(parser.windowGeometry().height());
93  }
94 
95  view->engine()->setBaseUrl(QUrl::fromLocalFile(::qmlDirectory()));
96  view->rootContext()->setContextProperty("applicationArguments", &qmlArgs);
97  if (parser.hasFrameless()) {
98  view->setFlags(Qt::FramelessWindowHint);
99  }
100 
101  // You will need this if you want to interact with touch-only components using a mouse
102  // Needed only when manually testing on a desktop.
103  MouseTouchAdaptor *mouseTouchAdaptor = 0;
104  if (parser.hasMouseToTouch()) {
105  mouseTouchAdaptor = MouseTouchAdaptor::instance();
106  }
107 
108  QUrl source(::qmlDirectory()+"OrientedShell.qml");
109  prependImportPaths(view->engine(), ::overrideImportPaths());
110  if (!isMirServer) {
111  prependImportPaths(view->engine(), ::nonMirImportPaths());
112  }
113  appendImportPaths(view->engine(), ::fallbackImportPaths());
114 
115  CachingNetworkManagerFactory *managerFactory = new CachingNetworkManagerFactory();
116  view->engine()->setNetworkAccessManagerFactory(managerFactory);
117 
118  view->setSource(source);
119  QObject::connect(view->engine(), SIGNAL(quit()), application, SLOT(quit()));
120 
121  if (isMirServer || parser.hasFullscreen()) {
122  view->showFullScreen();
123  } else {
124  view->show();
125  }
126 
127  int result = application->exec();
128 
129  delete view;
130  delete mouseTouchAdaptor;
131  delete application;
132 
133  return result;
134 }