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