on 8fe17f9 help sign in
unity8/main.cpp
161 lines 5.8 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 <QtQml>
22#include <QtQuick/QQuickView>
23#include <QtGui/QIcon>
24#include <QtGui/QGuiApplication>
25#include <QtGui/QScreen>
26#include <QtQml/QQmlEngine>
27#include <QtQml/QQmlContext>
28#include <qpa/qplatformnativeinterface.h>
29#include <QLibrary>
30#include <libintl.h>
32// local
33#include "paths.h"
34#include "MouseTouchAdaptor.h"
35#include "ApplicationArguments.h"
37namespace {
39void prependImportPaths(QQmlEngine *engine, const QStringList &paths)
41 QStringList importPathList = engine->importPathList();
42 for (int i = paths.count() -1; i >= 0; i--) {
43 // don't duplicate
44 const QString& path = paths[i];
45 QStringList::iterator iter = qFind(importPathList.begin(), importPathList.end(), path);
46 if (iter == importPathList.end()) {
47 engine->addImportPath(path);
48 }
49 }
52/* When you append and import path to the list of import paths it will be the *last*
53 place where Qt will search for QML modules.
54 The usual QQmlEngine::addImportPath() actually prepends the given path.*/
55void appendImportPaths(QQmlEngine *engine, const QStringList &paths)
57 QStringList importPathList = engine->importPathList();
58 Q_FOREACH(const QString& path, paths) {
59 // don't duplicate
60 QStringList::iterator iter = qFind(importPathList.begin(), importPathList.end(), path);
61 if (iter == importPathList.end()) {
62 importPathList.append(path);
63 }
64 }
65 engine->setImportPathList(importPathList);
68void resolveIconTheme() {
69 const char *ubuntuIconTheme = getenv("UBUNTU_ICON_THEME");
70 if (ubuntuIconTheme != NULL) {
71 QIcon::setThemeName(ubuntuIconTheme);
72 }
74} // namespace {
76int main(int argc, char** argv)
78 /* Workaround Qt platform integration plugin not advertising itself
79 as having the following capabilities:
80 - QPlatformIntegration::ThreadedOpenGL
81 - QPlatformIntegration::BufferQueueingOpenGL
82 */
83 setenv("QML_FORCE_THREADED_RENDERER", "1", 1);
84 setenv("QML_FIXED_ANIMATION_STEP", "1", 1);
86 QGuiApplication::setApplicationName(APP_NAME);
87 QGuiApplication application(argc, argv);
89 resolveIconTheme();
91 QStringList args = application.arguments();
92 ApplicationArguments qmlArgs(args);
94 // The testability driver is only loaded by QApplication but not by QGuiApplication.
95 // However, QApplication depends on QWidget which would add some unneeded overhead => Let's load the testability driver on our own.
96 if (args.contains(QLatin1String("-testability"))) {
97 QLibrary testLib(QLatin1String("qttestability"));
98 if (testLib.load()) {
99 typedef void (*TasInitialize)(void);
100 TasInitialize initFunction = (TasInitialize)testLib.resolve("qt_testability_init");
101 if (initFunction) {
102 initFunction();
103 } else {
104 qCritical("Library qttestability resolve failed!");
105 }
106 } else {
107 qCritical("Library qttestability load failed!");
108 }
109 }
111 bindtextdomain("unity8", translationDirectory().toUtf8().data());
113 QQuickView* view = new QQuickView();
114 view->setResizeMode(QQuickView::SizeRootObjectToView);
115 view->setTitle(APP_NAME);
116 view->engine()->setBaseUrl(QUrl::fromLocalFile(::shellAppDirectory()));
117 view->rootContext()->setContextProperty("applicationArguments", &qmlArgs);
118 if (args.contains(QLatin1String("-frameless"))) {
119 view->setFlags(Qt::FramelessWindowHint);
120 }
122 // You will need this if you want to interact with touch-only components using a mouse
123 // Needed only when manually testing on a desktop.
124 MouseTouchAdaptor *mouseTouchAdaptor = 0;
125 if (args.contains(QLatin1String("-mousetouch"))) {
126 mouseTouchAdaptor = new MouseTouchAdaptor;
127 mouseTouchAdaptor->setTargetWindow(view);
128 application.installNativeEventFilter(mouseTouchAdaptor);
129 }
131 QPlatformNativeInterface* nativeInterface = QGuiApplication::platformNativeInterface();
132 /* Shell is declared as a system session so that it always receives all
133 input events.
134 FIXME: use the enum value corresponding to SYSTEM_SESSION_TYPE (= 1)
135 when it becomes available.
136 */
137 nativeInterface->setProperty("ubuntuSessionType", 1);
138 view->setProperty("role", 2); // INDICATOR_ACTOR_ROLE
140 QUrl source(QML_FILE);
141 prependImportPaths(view->engine(), ::overrideImportPaths());
142 appendImportPaths(view->engine(), ::fallbackImportPaths());
143 view->setSource(source);
144 view->setColor("transparent");
146 if (qgetenv("QT_QPA_PLATFORM") == "ubuntu" || args.contains(QLatin1String("-fullscreen"))) {
147 // First, size window equal to screen (fake a real WM fullscreen mode).
148 // Then, in case we are actually running inside a windowing system,
149 // nicely set actual WM fullscreen hint for its benefit.
150 view->resize(view->screen()->size());
151 view->showFullScreen();
152 } else {
153 view->show();
154 }
156 int result = application.exec();
158 delete mouseTouchAdaptor;
160 return result;