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  resolveIconTheme();
100 
101  ApplicationArguments qmlArgs;
102  if (parser.isSet(windowGeometryOption) &&
103  parser.value(windowGeometryOption).split('x').size() == 2)
104  {
105  QStringList geom = parser.value(windowGeometryOption).split('x');
106  qmlArgs.setSize(geom.at(0).toInt(), geom.at(1).toInt());
107  }
108 
109  // The testability driver is only loaded by QApplication but not by QGuiApplication.
110  // However, QApplication depends on QWidget which would add some unneeded overhead => Let's load the testability driver on our own.
111  if (parser.isSet(testabilityOption) || getenv("QT_LOAD_TESTABILITY")) {
112  QLibrary testLib(QLatin1String("qttestability"));
113  if (testLib.load()) {
114  typedef void (*TasInitialize)(void);
115  TasInitialize initFunction = (TasInitialize)testLib.resolve("qt_testability_init");
116  if (initFunction) {
117  initFunction();
118  } else {
119  qCritical("Library qttestability resolve failed!");
120  }
121  } else {
122  qCritical("Library qttestability load failed!");
123  }
124  }
125 
126  bindtextdomain("unity8", translationDirectory().toUtf8().data());
127 
128  QQuickView* view = new QQuickView();
129  view->setResizeMode(QQuickView::SizeRootObjectToView);
130  view->setTitle("Qml Phone Shell");
131  view->engine()->setBaseUrl(QUrl::fromLocalFile(::qmlDirectory()));
132  view->rootContext()->setContextProperty("applicationArguments", &qmlArgs);
133  view->rootContext()->setContextProperty("indicatorProfile", indicatorProfile);
134  if (parser.isSet(framelessOption)) {
135  view->setFlags(Qt::FramelessWindowHint);
136  }
137 
138  // You will need this if you want to interact with touch-only components using a mouse
139  // Needed only when manually testing on a desktop.
140  MouseTouchAdaptor *mouseTouchAdaptor = 0;
141  if (parser.isSet(mousetouchOption)) {
142  mouseTouchAdaptor = new MouseTouchAdaptor;
143  application->installNativeEventFilter(mouseTouchAdaptor);
144  }
145 
146  QPlatformNativeInterface* nativeInterface = QGuiApplication::platformNativeInterface();
147  /* Shell is declared as a system session so that it always receives all
148  input events.
149  FIXME: use the enum value corresponding to SYSTEM_SESSION_TYPE (= 1)
150  when it becomes available.
151  */
152  nativeInterface->setProperty("ubuntuSessionType", 1);
153  view->setProperty("role", 2); // INDICATOR_ACTOR_ROLE
154 
155  QUrl source(::qmlDirectory()+"Shell.qml");
156  prependImportPaths(view->engine(), ::overrideImportPaths());
157  if (!isUbuntuMirServer) {
158  prependImportPaths(view->engine(), ::nonMirImportPaths());
159  }
160  appendImportPaths(view->engine(), ::fallbackImportPaths());
161 
162  view->setSource(source);
163  view->setColor("transparent");
164 
165  if (qgetenv("QT_QPA_PLATFORM") == "ubuntu" || isUbuntuMirServer || parser.isSet(fullscreenOption)) {
166  view->showFullScreen();
167  } else {
168  view->show();
169  }
170 
171  int result = application->exec();
172 
173  delete view;
174  delete mouseTouchAdaptor;
175  delete application;
176 
177  return result;
178 }
179 
180 int main(int argc, const char *argv[])
181 {
182  /* Workaround Qt platform integration plugin not advertising itself
183  as having the following capabilities:
184  - QPlatformIntegration::ThreadedOpenGL
185  - QPlatformIntegration::BufferQueueingOpenGL
186  */
187  setenv("QML_FORCE_THREADED_RENDERER", "1", 1);
188  setenv("QML_FIXED_ANIMATION_STEP", "1", 1);
189 
190  // For ubuntumirserver/ubuntumirclient
191  if (qgetenv("QT_QPA_PLATFORM").startsWith("ubuntumir")) {
192  setenv("QT_QPA_PLATFORM", "ubuntumirserver", 1);
193 
194  // If we use unity-mir directly, we automatically link to the Mir-server
195  // platform-api bindings, which result in unexpected behaviour when
196  // running the non-Mir scenario.
197  QLibrary unityMir("unity-mir", 1);
198  unityMir.load();
199  if (!unityMir.isLoaded()) {
200  qDebug() << "Library unity-mir not found/loaded";
201  return 1;
202  }
203 
204  typedef QMirServer* (*createServer_t)(int, const char **);
205  createServer_t createQMirServer = (createServer_t) unityMir.resolve("createQMirServer");
206  if (!createQMirServer) {
207  qDebug() << "unable to resolve symbol: createQMirServer";
208  return 2;
209  }
210 
211  QMirServer* mirServer = createQMirServer(argc, argv);
212 
213  typedef int (*runWithClient_t)(QMirServer*, std::function<int(int, const char**, void*)>);
214  runWithClient_t runWithClient = (runWithClient_t) unityMir.resolve("runQMirServerWithClient");
215  if (!runWithClient) {
216  qDebug() << "unable to resolve symbol: runWithClient";
217  return 3;
218  }
219 
220  return runWithClient(mirServer, startShell);
221  } else {
222  if (qgetenv("UPSTART_JOB") == "unity8") {
223  // Emit SIGSTOP as expected by upstart, under Mir it's unity-mir that will raise it.
224  // see http://upstart.ubuntu.com/cookbook/#expect-stop
225  raise(SIGSTOP);
226  }
227  return startShell(argc, argv, nullptr);
228  }
229 }