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