on 574bbde help sign in
unity8/main.cpp
149 lines 5.4 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 importPathList.prepend(paths[i]);
41 }
42 engine->setImportPathList(importPathList);
45/* When you append and import path to the list of import paths it will be the *last*
46 place where Qt will search for QML modules.
47 The usual QQmlEngine::addImportPath() actually prepends the given path.*/
48void appendImportPaths(QQmlEngine *engine, const QStringList &paths)
50 QStringList importPathList = engine->importPathList();
51 importPathList.append(paths);
52 engine->setImportPathList(importPathList);
55void resolveIconTheme() {
56 const char *ubuntuIconTheme = getenv("UBUNTU_ICON_THEME");
57 if (ubuntuIconTheme != NULL) {
58 QIcon::setThemeName(ubuntuIconTheme);
59 }
61} // namespace {
63int main(int argc, char** argv)
65 /* Workaround Qt platform integration plugin not advertising itself
66 as having the following capabilities:
67 - QPlatformIntegration::ThreadedOpenGL
68 - QPlatformIntegration::BufferQueueingOpenGL
69 */
70 setenv("QML_FORCE_THREADED_RENDERER", "1", 1);
71 setenv("QML_FIXED_ANIMATION_STEP", "1", 1);
73 QGuiApplication::setApplicationName("Qml Phone Shell");
74 QGuiApplication application(argc, argv);
76 resolveIconTheme();
78 QStringList args = application.arguments();
80 // The testability driver is only loaded by QApplication but not by QGuiApplication.
81 // However, QApplication depends on QWidget which would add some unneeded overhead => Let's load the testability driver on our own.
82 if (args.contains(QLatin1String("-testability"))) {
83 QLibrary testLib(QLatin1String("qttestability"));
84 if (testLib.load()) {
85 typedef void (*TasInitialize)(void);
86 TasInitialize initFunction = (TasInitialize)testLib.resolve("qt_testability_init");
87 if (initFunction) {
88 initFunction();
89 } else {
90 qCritical("Library qttestability resolve failed!");
91 }
92 } else {
93 qCritical("Library qttestability load failed!");
94 }
95 }
97 bindtextdomain("unity8", translationDirectory().toUtf8().data());
99 QQuickView* view = new QQuickView();
100 view->setResizeMode(QQuickView::SizeRootObjectToView);
101 view->setTitle("Qml Phone Shell");
102 view->engine()->setBaseUrl(QUrl::fromLocalFile(::shellAppDirectory()));
103 if (args.contains(QLatin1String("-frameless"))) {
104 view->setFlags(Qt::FramelessWindowHint);
105 }
107 // You will need this if you want to interact with touch-only components using a mouse
108 // Needed only when manually testing on a desktop.
109 MouseTouchAdaptor *mouseTouchAdaptor = 0;
110 if (args.contains(QLatin1String("-mousetouch"))) {
111 mouseTouchAdaptor = new MouseTouchAdaptor;
112 mouseTouchAdaptor->setTargetWindow(view);
113 application.installNativeEventFilter(mouseTouchAdaptor);
114 }
116 QPlatformNativeInterface* nativeInterface = QGuiApplication::platformNativeInterface();
117 /* Shell is declared as a system session so that it always receives all
118 input events.
119 FIXME: use the enum value corresponding to SYSTEM_SESSION_TYPE (= 1)
120 when it becomes available.
121 */
122 nativeInterface->setProperty("ubuntuSessionType", 1);
123 view->setProperty("role", 2); // INDICATOR_ACTOR_ROLE
125 QUrl source("Shell.qml");
126 prependImportPaths(view->engine(), QStringList() << ::shellAppDirectory());
127 prependImportPaths(view->engine(), ::shellImportPaths());
128 appendImportPaths(view->engine(), QStringList() << ::fakePluginsImportPath());
129 view->setSource(source);
130 view->setColor("transparent");
132 if (qgetenv("QT_QPA_PLATFORM") == "ubuntu" || args.contains(QLatin1String("-fullscreen"))) {
133 view->showFullScreen();
134 } else {
135 if (args.contains(QLatin1String("-geometry")) && args.size() > args.indexOf(QLatin1String("-geometry")) + 1) {
136 QStringList geometryArg = args.at(args.indexOf(QLatin1String("-geometry")) + 1).split('x');
137 if (geometryArg.size() == 2) {
138 view->resize(geometryArg.at(0).toInt(), geometryArg.at(1).toInt());
139 }
140 }
141 view->show();
142 }
144 int result = application.exec();
146 delete mouseTouchAdaptor;
148 return result;