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