on 42848ee help sign in
unity8/main.cpp
159 lines 5.7 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 <qpa/qplatformnativeinterface.h>
26#include <QLibrary>
27#include <libintl.h>
29// local
30#include "paths.h"
31#include "MouseTouchAdaptor.h"
33namespace {
35void prependImportPaths(QQmlEngine *engine, const QStringList &paths)
37 QStringList importPathList = engine->importPathList();
38 for (int i = paths.count() -1; i >= 0; i--) {
39 // don't duplicate
40 const QString& path = paths[i];
41 QStringList::iterator iter = qFind(importPathList.begin(), importPathList.end(), path);
42 if (iter == importPathList.end()) {
43 engine->addImportPath(path);
44 }
45 }
48/* When you append and import path to the list of import paths it will be the *last*
49 place where Qt will search for QML modules.
50 The usual QQmlEngine::addImportPath() actually prepends the given path.*/
51void appendImportPaths(QQmlEngine *engine, const QStringList &paths)
53 QStringList importPathList = engine->importPathList();
54 Q_FOREACH(const QString& path, paths) {
55 // don't duplicate
56 QStringList::iterator iter = qFind(importPathList.begin(), importPathList.end(), path);
57 if (iter == importPathList.end()) {
58 importPathList.append(path);
59 }
60 }
61 engine->setImportPathList(importPathList);
64void resolveIconTheme() {
65 const char *ubuntuIconTheme = getenv("UBUNTU_ICON_THEME");
66 if (ubuntuIconTheme != NULL) {
67 QIcon::setThemeName(ubuntuIconTheme);
68 }
70} // namespace {
72int main(int argc, char** argv)
74 /* Workaround Qt platform integration plugin not advertising itself
75 as having the following capabilities:
76 - QPlatformIntegration::ThreadedOpenGL
77 - QPlatformIntegration::BufferQueueingOpenGL
78 */
79 setenv("QML_FORCE_THREADED_RENDERER", "1", 1);
80 setenv("QML_FIXED_ANIMATION_STEP", "1", 1);
82 QGuiApplication::setApplicationName("Unity 8");
83 QGuiApplication application(argc, argv);
85 resolveIconTheme();
87 QStringList args = application.arguments();
89 // The testability driver is only loaded by QApplication but not by QGuiApplication.
90 // However, QApplication depends on QWidget which would add some unneeded overhead => Let's load the testability driver on our own.
91 if (args.contains(QLatin1String("-testability"))) {
92 QLibrary testLib(QLatin1String("qttestability"));
93 if (testLib.load()) {
94 typedef void (*TasInitialize)(void);
95 TasInitialize initFunction = (TasInitialize)testLib.resolve("qt_testability_init");
96 if (initFunction) {
97 initFunction();
98 } else {
99 qCritical("Library qttestability resolve failed!");
100 }
101 } else {
102 qCritical("Library qttestability load failed!");
103 }
104 }
106 bindtextdomain("unity8", translationDirectory().toUtf8().data());
108 QQuickView* view = new QQuickView();
109 view->setResizeMode(QQuickView::SizeRootObjectToView);
110 view->setTitle("Qml Phone Shell");
111 view->engine()->setBaseUrl(QUrl::fromLocalFile(::shellAppDirectory()));
112 if (args.contains(QLatin1String("-frameless"))) {
113 view->setFlags(Qt::FramelessWindowHint);
114 }
116 // You will need this if you want to interact with touch-only components using a mouse
117 // Needed only when manually testing on a desktop.
118 MouseTouchAdaptor *mouseTouchAdaptor = 0;
119 if (args.contains(QLatin1String("-mousetouch"))) {
120 mouseTouchAdaptor = new MouseTouchAdaptor;
121 mouseTouchAdaptor->setTargetWindow(view);
122 application.installNativeEventFilter(mouseTouchAdaptor);
123 }
125 QPlatformNativeInterface* nativeInterface = QGuiApplication::platformNativeInterface();
126 /* Shell is declared as a system session so that it always receives all
127 input events.
128 FIXME: use the enum value corresponding to SYSTEM_SESSION_TYPE (= 1)
129 when it becomes available.
130 */
131 nativeInterface->setProperty("ubuntuSessionType", 1);
132 view->setProperty("role", 2); // INDICATOR_ACTOR_ROLE
134 QObject::connect(view->engine(), SIGNAL(quit()), qApp, SLOT(quit()));
136 QUrl source("Shell.qml");
137 prependImportPaths(view->engine(), ::overrideImportPaths());
138 appendImportPaths(view->engine(), ::fallbackImportPaths());
139 view->setSource(source);
140 view->setColor("transparent");
142 if (qgetenv("QT_QPA_PLATFORM") == "ubuntu" || args.contains(QLatin1String("-fullscreen"))) {
143 view->showFullScreen();
144 } else {
145 if (args.contains(QLatin1String("-geometry")) && args.size() > args.indexOf(QLatin1String("-geometry")) + 1) {
146 QStringList geometryArg = args.at(args.indexOf(QLatin1String("-geometry")) + 1).split('x');
147 if (geometryArg.size() == 2) {
148 view->resize(geometryArg.at(0).toInt(), geometryArg.at(1).toInt());
149 }
150 }
151 view->show();
152 }
154 int result = application.exec();
156 delete mouseTouchAdaptor;
158 return result;