Debian Codegraph @

lomiri-ui-toolkit-launcher/launcher.cpp

raw symbols SCIP
1 /*
2 * Copyright 2014-2016 Canonical Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 // Dedicated QML launcher with the ability to setup QQuickView/QQmlEngine
18 // differently and with various extensions. Used internally to write Autopilot
19 // test cases that exhibit specific behavior.
20
21 #include <iostream>
22 #include <QtCore/qdebug.h>
23 #include <QtQuick/QQuickItem>
24 #include <QtQuick/QQuickView>
25 #include <QtGui/QGuiApplication>
26 #include <QtQml/QQmlEngine>
27 #include <QtQml/QQmlContext>
28 #include <QtQml/QQmlComponent>
29 #include <QtCore/QFileInfo>
30 #include <QLibrary>
31 #include <QOpenGLContext>
32 #include <QtGui/private/qopenglcontext_p.h>
33 #include <QtQuick/private/qsgcontext_p.h>
34 #include <QtCore/QCommandLineParser>
35 #include <QtCore/QCommandLineOption>
36 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
37 #include <LomiriToolkit-Qt6/private/mousetouchadaptor_p.h>
38 #else
39 #include <LomiriToolkit/private/mousetouchadaptor_p.h>
40 #endif
41 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
42 #include <LomiriMetrics-Qt6/applicationmonitor.h>
43 #else
44 #include <LomiriMetrics/applicationmonitor.h>
45 #endif
46 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
47 #include <QtGui/QInputDevice>
48 #else
49 #include <QtGui/QTouchDevice>
50 #endif
51 #include <QtQml/qqml.h>
52
53 static QObject *s_testRootObject = 0;
54 static QObject *testRootObject(QQmlEngine *engine, QJSEngine *jsEngine)
55 {
56 Q_UNUSED(jsEngine);
57 if (!s_testRootObject) {
58 s_testRootObject = new QObject(engine);
59 }
60 return s_testRootObject;
61 }
62
63 int main(int argc, const char *argv[])
64 {
65 // QPlatformIntegration::ThreadedOpenGL
66 setenv("QML_FORCE_THREADED_RENDERER", "1", 1);
67 // QPlatformIntegration::BufferQueueingOpenGL
68 setenv("QML_FIXED_ANIMATION_STEP", "1", 1);
69 // Oxide and QWebEngine need a shared context
70 QScopedPointer<QOpenGLContext> shareContext;
71 shareContext.reset(new QOpenGLContext);
72 #if QT_VERSION >= QT_VERSION_CHECK(5, 4, 0)
73 qt_gl_set_global_share_context(shareContext.data());
74 #elif QT_VERSION >= QT_VERSION_CHECK(5, 3, 0)
75 QOpenGLContextPrivate::setGlobalShareContext(shareContext.data());
76 #else
77 QSGContext::setSharedOpenGLContext(shareContext.data());
78 #endif
79 QGuiApplication::setApplicationName("UITK Launcher");
80 QGuiApplication application(argc, (char**)argv);
81
82 QCommandLineParser args;
83 QCommandLineOption _import("I", "Add <path> to the list of import paths", "path");
84 QCommandLineOption _enableTouch("touch", "Enables mouse to touch conversion on desktop");
85 QCommandLineOption _testability("testability", "Loads the testability driver");
86 QCommandLineOption _frameless("frameless", "Run without borders");
87 QCommandLineOption _engine("engine", "Use quick engine from quick view");
88 QCommandLineOption _desktop_file_hint("desktop_file_hint", "Desktop file - ignored", "desktop_file");
89 QCommandLineOption _metricsOverlay("metrics-overlay", "Enable the metrics overlay");
90 QCommandLineOption _metricsLogging(
91 "metrics-logging", "Enable metrics logging, <device> can be 'stdout', 'lttng' (Linux "
92 "only), a local or absolute filename", "device");
93 QCommandLineOption _metricsLoggingFilter(
94 "metrics-logging-filter", "Filter metrics logging, <filter> is a list of events separated "
95 "by a comma ('window', 'process', 'frame' or '*'), events not filtered are discarded",
96 "filter");
97
98 args.addOption(_import);
99 args.addOption(_enableTouch);
100 args.addOption(_testability);
101 args.addOption(_frameless);
102 args.addOption(_engine);
103 args.addOption(_desktop_file_hint);
104 args.addOption(_metricsOverlay);
105 args.addOption(_metricsLogging);
106 args.addOption(_metricsLoggingFilter);
107 args.addPositionalArgument("filename", "Document to be viewed");
108 args.setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions);
109 args.addHelpOption();
110 if (!args.parse(application.arguments())) {
111 qWarning() << args.errorText();
112 args.showHelp(1);
113 }
114
115 QString filename;
116 if (args.positionalArguments().count() > 0) {
117 filename = args.positionalArguments()[0];
118 }
119 if (filename.isEmpty()) {
120 // show usage and exit
121 args.showHelp(1);
122 }
123
124 // Testability is only supported out of the box by QApplication not QGuiApplication
125 if (args.isSet(_testability) || getenv("QT_LOAD_TESTABILITY")) {
126 QLibrary testLib(QLatin1String("qttestability"));
127 if (testLib.load()) {
128 typedef void (*TasInitialize)(void);
129 TasInitialize initFunction = (TasInitialize)testLib.resolve("qt_testability_init");
130 if (initFunction) {
131 initFunction();
132 } else {
133 qCritical("Library qttestability resolve failed!");
134 return 1;
135 }
136 } else {
137 qCritical("Library qttestability load failed!");
138 return 1;
139 }
140 }
141
142 // Allow manual execution of unit tests using Qt.Test
143 qmlRegisterSingletonType<QObject>("Qt.test.qtestroot", 1, 0, "QTestRootObject", testRootObject);
144
145 QPointer<QQmlEngine> engine;
146 QScopedPointer<QQuickWindow> window;
147 QString testCaseImport;
148 // Let's see if the source file exists
149 QFile sourceCode(filename);
150 if (!sourceCode.open(QIODevice::ReadOnly)) {
151 qCritical("%s", qPrintable(sourceCode.errorString()));
152 return 1;
153 }
154 while (!sourceCode.atEnd()) {
155 QByteArray line(sourceCode.readLine());
156 if (line.contains("{"))
157 break;
158 // Hack to avoid assertion if QtTest or Lomiri.Test is used:
159 // ASSERT: "QTest::TestLoggers::loggerCount() != 0" in file qtestlog.cpp, line 278
160 if ((line.startsWith("import ") && QString(line).split("//")[0].split("/*")[0].contains("Test"))) {
161 testCaseImport = line;
162 break;
163 }
164 }
165 QUrl source(QUrl::fromLocalFile(filename));
166
167 // The default constructor affects the components tree (autopilot vis)
168 if (args.isSet(_engine) || !testCaseImport.isEmpty()) {
169 QQuickView *view(new QQuickView());
170 engine = view->engine();
171 if (args.isSet(_import)) {
172 QStringList paths = args.values(_import);
173 Q_FOREACH(const QString &path, paths) {
174 engine->addImportPath(path);
175 }
176 }
177
178 view->setSource(source);
179 while (view->status() == QQuickView::Loading)
180 QCoreApplication::processEvents();
181 if (view->errors().count() > 0) {
182 args.showHelp(3);
183 }
184 // An unsupported root object is not technically an error
185 if (!view->rootObject()) {
186 if (!testCaseImport.isEmpty())
187 qCritical("Note: QtTest or Lomiri.Test was detected here: %s", qPrintable(testCaseImport));
188 return 1;
189 }
190
191 window.reset(view);
192 } else {
193 engine = new QQmlEngine();
194 if (args.isSet(_import)) {
195 QStringList paths = args.values(_import);
196 Q_FOREACH(const QString &path, paths) {
197 engine->addImportPath(path);
198 }
199 }
200
201 QObject::connect(engine, SIGNAL(quit()), QCoreApplication::instance(), SLOT(quit()));
202 QPointer<QQmlComponent> component(new QQmlComponent(engine));
203 component->loadUrl(source, QQmlComponent::Asynchronous);
204 while (component->isLoading())
205 QCoreApplication::processEvents();
206 QObject *toplevel(component->create());
207 if (!toplevel && component->isError()) {
208 qCritical("%s", qPrintable(component->errorString()));
209 return 1;
210 }
211
212 window.reset(qobject_cast<QQuickWindow *>(toplevel));
213 if (window)
214 engine->setIncubationController(window->incubationController());
215 else {
216 QQuickItem *rootItem = qobject_cast<QQuickItem *>(toplevel);
217 if (rootItem) {
218 QQuickView *view(new QQuickView(engine, 0));
219 window.reset(view);
220 view->setResizeMode(QQuickView::SizeRootObjectToView);
221 view->setContent(source, component, rootItem);
222 }
223 }
224 }
225
226 // Application monitoring.
227 UMApplicationMonitor* applicationMonitor = UMApplicationMonitor::instance();
228 if (args.isSet(_metricsLoggingFilter)) {
229 QStringList filterList = QString(args.value(_metricsLoggingFilter)).split(
230 QChar(','), Qt::SkipEmptyParts);
231 UMApplicationMonitor::LoggingFilters filter;
232 const int size = filterList.size();
233 for (int i = 0; i < size; ++i) {
234 if (filterList[i] == "*") {
235 filter |= UMApplicationMonitor::AllEvents;
236 break;
237 } else if (filterList[i] == "window") {
238 filter |= UMApplicationMonitor::WindowEvent;
239 } else if (filterList[i] == "process") {
240 filter |= UMApplicationMonitor::ProcessEvent;
241 } else if (filterList[i] == "frame") {
242 filter |= UMApplicationMonitor::FrameEvent;
243 } else if (filterList[i] == "generic") {
244 filter |= UMApplicationMonitor::GenericEvent;
245 }
246 }
247 applicationMonitor->setLoggingFilter(filter);
248 }
249 if (args.isSet(_metricsLogging)) {
250 UMLogger* logger;
251 QString device = args.value(_metricsLogging);
252 if (device.isEmpty() || device == "stdout") {
253 logger = new UMFileLogger(stdout);
254 #if defined(Q_OS_LINUX) && defined(LTTNG_PLUGIN_BUILD_PATH)
255 } else if (device == "lttng") {
256 logger = new UMLTTNGLogger();
257 #endif // defined(Q_OS_LINUX) && defined(LTTNG_PLUGIN_BUILD_PATH)
258 } else {
259 logger = new UMFileLogger(device);
260 }
261 if (logger->isOpen()) {
262 applicationMonitor->installLogger(logger);
263 applicationMonitor->setLogging(true);
264 } else {
265 delete logger;
266 }
267 }
268 if (args.isSet(_metricsOverlay)) {
269 applicationMonitor->setOverlay(true);
270 }
271
272 if (window->title().isEmpty())
273 window->setTitle("UI Toolkit QQuickView");
274 if (args.isSet(_frameless)) {
275 window->setFlags(Qt::FramelessWindowHint);
276 }
277 window->show();
278
279 if (args.isSet(_enableTouch)) {
280 // has no effect if we have touch screen
281 new UT_PREPEND_NAMESPACE(MouseTouchAdaptor)(&application);
282 }
283
284 return application.exec();
285 }