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