Unity 8
 All Classes Functions Properties
main.cpp
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  */
19 
20 // Qt
21 #include <QtQuick/QQuickView>
22 #include <QtGui/QGuiApplication>
23 #include <QtQml/QQmlEngine>
24 #include <QtQml/QQmlContext>
25 #include <qpa/qplatformnativeinterface.h>
26 #include <QLibrary>
27 #include <QDebug>
28 #include <libintl.h>
29 #include <dlfcn.h>
30 #include <csignal>
31 
32 // local
33 #include <paths.h>
34 #include "MouseTouchAdaptor.h"
35 #include "ApplicationArguments.h"
36 
37 #include <unity-mir/qmirserver.h>
38 
39 
40 int startShell(int argc, const char** argv, void* server)
41 {
42  const bool isUbuntuMirServer = qgetenv("QT_QPA_PLATFORM") == "ubuntumirserver";
43 
44  QGuiApplication::setApplicationName("Unity 8");
45  QGuiApplication *application;
46  if (isUbuntuMirServer) {
47  QLibrary unityMir("unity-mir", 1);
48  unityMir.load();
49 
50  typedef QGuiApplication* (*createServerApplication_t)(int&, const char **, void*);
51  createServerApplication_t createQMirServerApplication = (createServerApplication_t) unityMir.resolve("createQMirServerApplication");
52  if (!createQMirServerApplication) {
53  qDebug() << "unable to resolve symbol: createQMirServerApplication";
54  return 4;
55  }
56 
57  application = createQMirServerApplication(argc, argv, server);
58  } else {
59  application = new QGuiApplication(argc, (char**)argv);
60  }
61 
62  QString indicatorProfile = qgetenv("UNITY_INDICATOR_PROFILE");
63  if (indicatorProfile.isEmpty()) {
64  indicatorProfile = "phone";
65  }
66 
67  resolveIconTheme();
68 
69  QStringList args = application->arguments();
70  ApplicationArguments qmlArgs(args);
71 
72  // The testability driver is only loaded by QApplication but not by QGuiApplication.
73  // However, QApplication depends on QWidget which would add some unneeded overhead => Let's load the testability driver on our own.
74  if (args.contains(QLatin1String("-testability")) || getenv("QT_LOAD_TESTABILITY")) {
75  QLibrary testLib(QLatin1String("qttestability"));
76  if (testLib.load()) {
77  typedef void (*TasInitialize)(void);
78  TasInitialize initFunction = (TasInitialize)testLib.resolve("qt_testability_init");
79  if (initFunction) {
80  initFunction();
81  } else {
82  qCritical("Library qttestability resolve failed!");
83  }
84  } else {
85  qCritical("Library qttestability load failed!");
86  }
87  }
88 
89  bindtextdomain("unity8", translationDirectory().toUtf8().data());
90 
91  QQuickView* view = new QQuickView();
92  view->setResizeMode(QQuickView::SizeRootObjectToView);
93  view->setTitle("Qml Phone Shell");
94  view->engine()->setBaseUrl(QUrl::fromLocalFile(::qmlDirectory()));
95  view->rootContext()->setContextProperty("applicationArguments", &qmlArgs);
96  view->rootContext()->setContextProperty("indicatorProfile", indicatorProfile);
97  if (args.contains(QLatin1String("-frameless"))) {
98  view->setFlags(Qt::FramelessWindowHint);
99  }
100 
101  // You will need this if you want to interact with touch-only components using a mouse
102  // Needed only when manually testing on a desktop.
103  MouseTouchAdaptor *mouseTouchAdaptor = 0;
104  if (args.contains(QLatin1String("-mousetouch"))) {
105  mouseTouchAdaptor = new MouseTouchAdaptor;
106  application->installNativeEventFilter(mouseTouchAdaptor);
107  }
108 
109  QPlatformNativeInterface* nativeInterface = QGuiApplication::platformNativeInterface();
110  /* Shell is declared as a system session so that it always receives all
111  input events.
112  FIXME: use the enum value corresponding to SYSTEM_SESSION_TYPE (= 1)
113  when it becomes available.
114  */
115  nativeInterface->setProperty("ubuntuSessionType", 1);
116  view->setProperty("role", 2); // INDICATOR_ACTOR_ROLE
117 
118  QUrl source(::qmlDirectory()+"Shell.qml");
119  prependImportPaths(view->engine(), ::overrideImportPaths());
120  if (!isUbuntuMirServer) {
121  prependImportPaths(view->engine(), ::nonMirImportPaths());
122  }
123  appendImportPaths(view->engine(), ::fallbackImportPaths());
124 
125  view->setSource(source);
126  view->setColor("transparent");
127 
128  if (qgetenv("QT_QPA_PLATFORM") == "ubuntu" || isUbuntuMirServer || args.contains(QLatin1String("-fullscreen"))) {
129  view->showFullScreen();
130  } else {
131  view->show();
132  }
133 
134  int result = application->exec();
135 
136  delete view;
137  delete mouseTouchAdaptor;
138  delete application;
139 
140  return result;
141 }
142 
143 int main(int argc, const char *argv[])
144 {
145  /* Workaround Qt platform integration plugin not advertising itself
146  as having the following capabilities:
147  - QPlatformIntegration::ThreadedOpenGL
148  - QPlatformIntegration::BufferQueueingOpenGL
149  */
150  setenv("QML_FORCE_THREADED_RENDERER", "1", 1);
151  setenv("QML_FIXED_ANIMATION_STEP", "1", 1);
152 
153  // For ubuntumirserver/ubuntumirclient
154  if (qgetenv("QT_QPA_PLATFORM").startsWith("ubuntumir")) {
155  setenv("QT_QPA_PLATFORM", "ubuntumirserver", 1);
156 
157  // If we use unity-mir directly, we automatically link to the Mir-server
158  // platform-api bindings, which result in unexpected behaviour when
159  // running the non-Mir scenario.
160  QLibrary unityMir("unity-mir", 1);
161  unityMir.load();
162  if (!unityMir.isLoaded()) {
163  qDebug() << "Library unity-mir not found/loaded";
164  return 1;
165  }
166 
167  typedef QMirServer* (*createServer_t)(int, const char **);
168  createServer_t createQMirServer = (createServer_t) unityMir.resolve("createQMirServer");
169  if (!createQMirServer) {
170  qDebug() << "unable to resolve symbol: createQMirServer";
171  return 2;
172  }
173 
174  QMirServer* mirServer = createQMirServer(argc, argv);
175 
176  typedef int (*runWithClient_t)(QMirServer*, std::function<int(int, const char**, void*)>);
177  runWithClient_t runWithClient = (runWithClient_t) unityMir.resolve("runQMirServerWithClient");
178  if (!runWithClient) {
179  qDebug() << "unable to resolve symbol: runWithClient";
180  return 3;
181  }
182 
183  return runWithClient(mirServer, startShell);
184  } else {
185  if (qgetenv("UPSTART_JOB") == "unity8") {
186  // Emit SIGSTOP as expected by upstart, under Mir it's unity-mir that will raise it.
187  // see http://upstart.ubuntu.com/cookbook/#expect-stop
188  raise(SIGSTOP);
189  }
190  return startShell(argc, argv, nullptr);
191  }
192 }