on cddf513 help sign in
unity8/main.cpp
187 lines 6.6 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 <QtQuick/QQuickView>
22#include <QtGui/QIcon>
23#include <QtGui/QGuiApplication>
24#include <QtQml/QQmlEngine>
25#include <QtQml/QQmlContext>
26#include <qpa/qplatformnativeinterface.h>
27#include <QLibrary>
28#include <libintl.h>
30// Unity-Mir
31#include <unity-mir/qmirserver.h>
32#include <unity-mir/qmirserverapplication.h>
34// local
35#include "paths.h"
36#include "MouseTouchAdaptor.h"
37#include "ApplicationArguments.h"
39namespace {
41void prependImportPaths(QQmlEngine *engine, const QStringList &paths)
43 QStringList importPathList = engine->importPathList();
44 for (int i = paths.count() -1; i >= 0; i--) {
45 // don't duplicate
46 const QString& path = paths[i];
47 QStringList::iterator iter = qFind(importPathList.begin(), importPathList.end(), path);
48 if (iter == importPathList.end()) {
49 engine->addImportPath(path);
50 }
51 }
54/* When you append and import path to the list of import paths it will be the *last*
55 place where Qt will search for QML modules.
56 The usual QQmlEngine::addImportPath() actually prepends the given path.*/
57void appendImportPaths(QQmlEngine *engine, const QStringList &paths)
59 QStringList importPathList = engine->importPathList();
60 Q_FOREACH(const QString& path, paths) {
61 // don't duplicate
62 QStringList::iterator iter = qFind(importPathList.begin(), importPathList.end(), path);
63 if (iter == importPathList.end()) {
64 importPathList.append(path);
65 }
66 }
67 engine->setImportPathList(importPathList);
70void resolveIconTheme() {
71 const char *ubuntuIconTheme = getenv("UBUNTU_ICON_THEME");
72 if (ubuntuIconTheme == NULL) {
73 ubuntuIconTheme = "ubuntu-mobile";
74 }
75 QIcon::setThemeName(ubuntuIconTheme);
77} // namespace {
79int startShell(int argc, const char** argv, ShellServerConfiguration* server)
81 /* Workaround Qt platform integration plugin not advertising itself
82 as having the following capabilities:
83 - QPlatformIntegration::ThreadedOpenGL
84 - QPlatformIntegration::BufferQueueingOpenGL
85 */
86 setenv("QML_FORCE_THREADED_RENDERER", "1", 1);
87 setenv("QML_FIXED_ANIMATION_STEP", "1", 1);
89 const bool isUbuntuMirServer = qgetenv("QT_QPA_PLATFORM") == "ubuntumirserver";
91 QGuiApplication::setApplicationName("Unity 8");
92 QGuiApplication *application;
93 if (isUbuntuMirServer) {
94 application = new QMirServerApplication(argc, (char**)argv, server);
95 } else {
96 application = new QGuiApplication(argc, (char**)argv);
97 }
99 resolveIconTheme();
101 QStringList args = application->arguments();
102 ApplicationArguments qmlArgs(args);
104 // The testability driver is only loaded by QApplication but not by QGuiApplication.
105 // However, QApplication depends on QWidget which would add some unneeded overhead => Let's load the testability driver on our own.
106 if (args.contains(QLatin1String("-testability"))) {
107 QLibrary testLib(QLatin1String("qttestability"));
108 if (testLib.load()) {
109 typedef void (*TasInitialize)(void);
110 TasInitialize initFunction = (TasInitialize)testLib.resolve("qt_testability_init");
111 if (initFunction) {
112 initFunction();
113 } else {
114 qCritical("Library qttestability resolve failed!");
115 }
116 } else {
117 qCritical("Library qttestability load failed!");
118 }
119 }
121 bindtextdomain("unity8", translationDirectory().toUtf8().data());
123 QQuickView* view = new QQuickView();
124 view->setResizeMode(QQuickView::SizeRootObjectToView);
125 view->setTitle("Qml Phone Shell");
126 view->engine()->setBaseUrl(QUrl::fromLocalFile(::shellAppDirectory()));
127 view->rootContext()->setContextProperty("applicationArguments", &qmlArgs);
128 if (args.contains(QLatin1String("-frameless"))) {
129 view->setFlags(Qt::FramelessWindowHint);
130 }
132 // You will need this if you want to interact with touch-only components using a mouse
133 // Needed only when manually testing on a desktop.
134 MouseTouchAdaptor *mouseTouchAdaptor = 0;
135 if (args.contains(QLatin1String("-mousetouch"))) {
136 mouseTouchAdaptor = new MouseTouchAdaptor;
137 mouseTouchAdaptor->setTargetWindow(view);
138 application->installNativeEventFilter(mouseTouchAdaptor);
139 }
141 QPlatformNativeInterface* nativeInterface = QGuiApplication::platformNativeInterface();
142 /* Shell is declared as a system session so that it always receives all
143 input events.
144 FIXME: use the enum value corresponding to SYSTEM_SESSION_TYPE (= 1)
145 when it becomes available.
146 */
147 nativeInterface->setProperty("ubuntuSessionType", 1);
148 view->setProperty("role", 2); // INDICATOR_ACTOR_ROLE
150 QUrl source("Shell.qml");
151 prependImportPaths(view->engine(), ::overrideImportPaths());
152 appendImportPaths(view->engine(), ::fallbackImportPaths());
154 QStringList importPath = view->engine()->importPathList().filter("qt5/imports");
155 if (isUbuntuMirServer) {
156 importPath.first().append("/Unity-Mir");
157 view->engine()->addImportPath(importPath.first());
158 }
160 view->setSource(source);
161 view->setColor("transparent");
163 if (qgetenv("QT_QPA_PLATFORM") == "ubuntu" || isUbuntuMirServer || args.contains(QLatin1String("-fullscreen"))) {
164 view->showFullScreen();
165 } else {
166 view->show();
167 }
169 int result = application->exec();
171 delete mouseTouchAdaptor;
172 delete application;
174 return result;
177int main(int argc, const char *argv[])
179 // For ubuntumirserver/ubuntumirclient
180 if (qgetenv("QT_QPA_PLATFORM").startsWith("ubuntumir")) {
181 setenv("QT_QPA_PLATFORM", "ubuntumirserver", 1);
182 QMirServer mirServer(argc, argv);
183 return mirServer.runWithClient(startShell);
184 } else {
185 return startShell(argc, argv, nullptr);
186 }