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