on e0b237a help sign in
unity8/main.cpp
155 lines 5.5 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// local
31#include "paths.h"
32#include "MouseTouchAdaptor.h"
33#include "ApplicationArguments.h"
35namespace {
37void prependImportPaths(QQmlEngine *engine, const QStringList &paths)
39 QStringList importPathList = engine->importPathList();
40 for (int i = paths.count() -1; i >= 0; i--) {
41 // don't duplicate
42 const QString& path = paths[i];
43 QStringList::iterator iter = qFind(importPathList.begin(), importPathList.end(), path);
44 if (iter == importPathList.end()) {
45 engine->addImportPath(path);
46 }
47 }
50/* When you append and import path to the list of import paths it will be the *last*
51 place where Qt will search for QML modules.
52 The usual QQmlEngine::addImportPath() actually prepends the given path.*/
53void appendImportPaths(QQmlEngine *engine, const QStringList &paths)
55 QStringList importPathList = engine->importPathList();
56 Q_FOREACH(const QString& path, paths) {
57 // don't duplicate
58 QStringList::iterator iter = qFind(importPathList.begin(), importPathList.end(), path);
59 if (iter == importPathList.end()) {
60 importPathList.append(path);
61 }
62 }
63 engine->setImportPathList(importPathList);
66void resolveIconTheme() {
67 const char *ubuntuIconTheme = getenv("UBUNTU_ICON_THEME");
68 if (ubuntuIconTheme != NULL) {
69 QIcon::setThemeName(ubuntuIconTheme);
70 }
72} // namespace {
74int main(int argc, char** argv)
76 /* Workaround Qt platform integration plugin not advertising itself
77 as having the following capabilities:
78 - QPlatformIntegration::ThreadedOpenGL
79 - QPlatformIntegration::BufferQueueingOpenGL
80 */
81 setenv("QML_FORCE_THREADED_RENDERER", "1", 1);
82 setenv("QML_FIXED_ANIMATION_STEP", "1", 1);
84 QGuiApplication::setApplicationName("Unity 8");
85 QGuiApplication application(argc, argv);
87 resolveIconTheme();
89 QStringList args = application.arguments();
90 ApplicationArguments qmlArgs(args);
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 (args.contains(QLatin1String("-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->setTitle("Qml Phone Shell");
114 view->engine()->setBaseUrl(QUrl::fromLocalFile(::shellAppDirectory()));
115 view->rootContext()->setContextProperty("applicationArguments", &qmlArgs);
116 if (args.contains(QLatin1String("-frameless"))) {
117 view->setFlags(Qt::FramelessWindowHint);
118 }
120 // You will need this if you want to interact with touch-only components using a mouse
121 // Needed only when manually testing on a desktop.
122 MouseTouchAdaptor *mouseTouchAdaptor = 0;
123 if (args.contains(QLatin1String("-mousetouch"))) {
124 mouseTouchAdaptor = new MouseTouchAdaptor;
125 mouseTouchAdaptor->setTargetWindow(view);
126 application.installNativeEventFilter(mouseTouchAdaptor);
127 }
129 QPlatformNativeInterface* nativeInterface = QGuiApplication::platformNativeInterface();
130 /* Shell is declared as a system session so that it always receives all
131 input events.
132 FIXME: use the enum value corresponding to SYSTEM_SESSION_TYPE (= 1)
133 when it becomes available.
134 */
135 nativeInterface->setProperty("ubuntuSessionType", 1);
136 view->setProperty("role", 2); // INDICATOR_ACTOR_ROLE
138 QUrl source("Shell.qml");
139 prependImportPaths(view->engine(), ::overrideImportPaths());
140 appendImportPaths(view->engine(), ::fallbackImportPaths());
141 view->setSource(source);
142 view->setColor("transparent");
144 if (qgetenv("QT_QPA_PLATFORM") == "ubuntu" || args.contains(QLatin1String("-fullscreen"))) {
145 view->showFullScreen();
146 } else {
147 view->show();
148 }
150 int result = application.exec();
152 delete mouseTouchAdaptor;
154 return result;