Unity 8
ShellApplication.cpp
1 /*
2  * Copyright (C) 2015 Canonical, Ltd.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 3.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 #include "ShellApplication.h"
18 
19 // Qt
20 #include <QLibrary>
21 #include <QProcess>
22 #include <QScreen>
23 
24 #include <libintl.h>
25 
26 // libandroid-properties
27 #include <hybris/properties/properties.h>
28 
29 // local
30 #include <paths.h>
31 #include "CachingNetworkManagerFactory.h"
32 #include "UnityCommandLineParser.h"
33 
34 ShellApplication::ShellApplication(int & argc, char ** argv, bool isMirServer)
35  : QGuiApplication(argc, argv)
36 {
37 
38  setApplicationName(QStringLiteral("unity8"));
39  setOrganizationName(QStringLiteral("Canonical"));
40 
41  connect(this, &QGuiApplication::screenAdded, this, &ShellApplication::onScreenAdded);
42 
43  setupQmlEngine(isMirServer);
44 
45  UnityCommandLineParser parser(*this);
46 
47  if (!parser.deviceName().isEmpty()) {
48  m_deviceName = parser.deviceName();
49  } else {
50  char buffer[200];
51  property_get("ro.product.device", buffer /* value */, "desktop" /* default_value*/);
52  m_deviceName = QString(buffer);
53  }
54  m_qmlArgs.setDeviceName(m_deviceName);
55 
56  m_qmlArgs.setMode(parser.mode());
57 
58  // The testability driver is only loaded by QApplication but not by QGuiApplication.
59  // However, QApplication depends on QWidget which would add some unneeded overhead => Let's load the testability driver on our own.
60  if (parser.hasTestability() || getenv("QT_LOAD_TESTABILITY")) {
61  QLibrary testLib(QStringLiteral("qttestability"));
62  if (testLib.load()) {
63  typedef void (*TasInitialize)(void);
64  TasInitialize initFunction = (TasInitialize)testLib.resolve("qt_testability_init");
65  if (initFunction) {
66  initFunction();
67  } else {
68  qCritical("Library qttestability resolve failed!");
69  }
70  } else {
71  qCritical("Library qttestability load failed!");
72  }
73  }
74 
75  bindtextdomain("unity8", translationDirectory().toUtf8().data());
76  textdomain("unity8");
77 
78  m_shellView = new ShellView(m_qmlEngine, &m_qmlArgs);
79 
80  if (parser.windowGeometry().isValid()) {
81  m_shellView->setWidth(parser.windowGeometry().width());
82  m_shellView->setHeight(parser.windowGeometry().height());
83  }
84 
85  if (parser.hasFrameless()) {
86  m_shellView->setFlags(Qt::FramelessWindowHint);
87  }
88 
89 
90  #ifdef UNITY8_ENABLE_TOUCH_EMULATION
91  // You will need this if you want to interact with touch-only components using a mouse
92  // Needed only when manually testing on a desktop.
93  if (parser.hasMouseToTouch()) {
94  m_mouseTouchAdaptor = MouseTouchAdaptor::instance();
95  }
96  #endif
97 
98 
99  // Some hard-coded policy for now.
100  // NB: We don't support more than two screens at the moment
101  //
102  // TODO: Support an arbitrary number of screens and different policies
103  // (eg cloned desktop, several desktops, etc)
104  if (isMirServer && screens().count() == 2) {
105  m_shellView->setScreen(screens().at(1));
106  m_qmlArgs.setDeviceName(QStringLiteral("desktop"));
107 
108  m_secondaryWindow = new SecondaryWindow(m_qmlEngine);
109  m_secondaryWindow->setScreen(screens().at(0));
110  // QWindow::showFullScreen() also calls QWindow::requestActivate() and we don't want that!
111  m_secondaryWindow->setWindowState(Qt::WindowFullScreen);
112  m_secondaryWindow->setVisible(true);
113  }
114 
115  if (parser.mode().compare("greeter") == 0) {
116  QSize primaryScreenSize = this->primaryScreen()->size();
117  m_shellView->setHeight(primaryScreenSize.height());
118  m_shellView->setWidth(primaryScreenSize.width());
119  m_shellView->show();
120  m_shellView->requestActivate();
121  if (!QProcess::startDetached("initctl emit --no-wait unity8-greeter-started")) {
122  qDebug() << "Unable to send unity8-greeter-started event to Upstart";
123  }
124  } else if (isMirServer || parser.hasFullscreen()) {
125  m_shellView->showFullScreen();
126  } else {
127  m_shellView->show();
128  }
129 }
130 
131 ShellApplication::~ShellApplication()
132 {
133  destroyResources();
134 }
135 
136 void ShellApplication::destroyResources()
137 {
138  // Deletion order is important. Don't use QScopedPointers and the like
139  // Otherwise the process will hang on shutdown (bug somewhere I guess).
140  delete m_shellView;
141  m_shellView = nullptr;
142 
143  delete m_secondaryWindow;
144  m_secondaryWindow = nullptr;
145 
146  #ifdef UNITY8_ENABLE_TOUCH_EMULATION
147  delete m_mouseTouchAdaptor;
148  m_mouseTouchAdaptor = nullptr;
149  #endif
150 
151  delete m_qmlEngine;
152  m_qmlEngine = nullptr;
153 }
154 
155 void ShellApplication::setupQmlEngine(bool isMirServer)
156 {
157  m_qmlEngine = new QQmlEngine(this);
158 
159  m_qmlEngine->setBaseUrl(QUrl::fromLocalFile(::qmlDirectory()));
160 
161  prependImportPaths(m_qmlEngine, ::overrideImportPaths());
162  if (!isMirServer) {
163  prependImportPaths(m_qmlEngine, ::nonMirImportPaths());
164  }
165  appendImportPaths(m_qmlEngine, ::fallbackImportPaths());
166 
167  m_qmlEngine->setNetworkAccessManagerFactory(new CachingNetworkManagerFactory);
168 
169  QObject::connect(m_qmlEngine, &QQmlEngine::quit, this, &QGuiApplication::quit);
170 }
171 
172 void ShellApplication::onScreenAdded(QScreen * /*screen*/)
173 {
174  // TODO: Support an arbitrary number of screens and different policies
175  // (eg cloned desktop, several desktops, etc)
176  if (screens().count() == 2) {
177  m_shellView->setScreen(screens().at(1));
178  m_qmlArgs.setDeviceName(QStringLiteral("desktop"));
179  // Changing the QScreen where a QWindow is drawn makes it also lose focus (besides having
180  // its backing QPlatformWindow recreated). So lets refocus it.
181  m_shellView->requestActivate();
182  // QWindow::destroy() is called when it changes between screens. We have to manually make it visible again
183  // <dandrader> This bug is supposedly fixed in Qt 5.5.1, although I can still reproduce it there. :-/
184  m_shellView->setVisible(true);
185 
186  m_secondaryWindow = new SecondaryWindow(m_qmlEngine);
187  m_secondaryWindow->setScreen(screens().at(0));
188 
189  // QWindow::showFullScreen() also calls QWindow::requestActivate() and we don't want that!
190  m_secondaryWindow->setWindowState(Qt::WindowFullScreen);
191  m_secondaryWindow->setVisible(true);
192  }
193 }
194 
195 void ShellApplication::onScreenAboutToBeRemoved(QScreen *screen)
196 {
197  // TODO: Support an arbitrary number of screens and different policies
198  // (eg cloned desktop, several desktops, etc)
199  if (screen == m_shellView->screen()) {
200  const QList<QScreen *> allScreens = screens();
201  Q_ASSERT(allScreens.count() > 1);
202  Q_ASSERT(allScreens.at(0) != screen);
203  Q_ASSERT(m_secondaryWindow);
204  delete m_secondaryWindow;
205  m_secondaryWindow = nullptr;
206  m_shellView->setScreen(allScreens.first());
207  m_qmlArgs.setDeviceName(m_deviceName);
208  // Changing the QScreen where a QWindow is drawn makes it also lose focus (besides having
209  // its backing QPlatformWindow recreated). So lets refocus it.
210  m_shellView->requestActivate();
211  // QWindow::destroy() is called when it changes between screens. We have to manually make it visible again
212  // <dandrader> This bug is supposedly fixed in Qt 5.5.1, although I can still reproduce it there. :-/
213  m_shellView->setVisible(true);
214  }
215 }