on xenial help sign in
Move handling of command line options to a separate class
f455422Daniel d'Andrada
src/CMakeLists.txt+1
@@ -21,6 +21,7 @@ add_executable(${SHELL_APP}
21 main.cpp
21 main.cpp
22 MouseTouchAdaptor.cpp
22 MouseTouchAdaptor.cpp
23 CachingNetworkManagerFactory.cpp
23 CachingNetworkManagerFactory.cpp
24+ UnityCommandLineParser.cpp
24 ${QML_FILES} # This is to make qml and image files appear in the IDE's project tree
25 ${QML_FILES} # This is to make qml and image files appear in the IDE's project tree
25)
26)
26
27
src/UnityCommandLineParser.cppnew file+118
@@ -0,0 +1,118 @@
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 "UnityCommandLineParser.h"
18+
19+#include <QDebug>
20+
21+#define ENV_GRID_UNIT_PX "GRID_UNIT_PX"
22+#define DEFAULT_GRID_UNIT_PX 8
23+
24+UnityCommandLineParser::UnityCommandLineParser(const QCoreApplication &app)
25+{
26+ m_gridUnit = getenvFloat(ENV_GRID_UNIT_PX, DEFAULT_GRID_UNIT_PX);
27+
28+ QCommandLineParser parser;
29+ parser.setApplicationDescription("Description: Unity 8 Shell");
30+ parser.addHelpOption();
31+
32+ QCommandLineOption fullscreenOption("fullscreen",
33+ "Run in fullscreen");
34+ parser.addOption(fullscreenOption);
35+
36+ QCommandLineOption framelessOption("frameless",
37+ "Run without window borders");
38+ parser.addOption(framelessOption);
39+
40+ QCommandLineOption mousetouchOption("mousetouch",
41+ "Allow the mouse to provide touch input");
42+ parser.addOption(mousetouchOption);
43+
44+ QCommandLineOption windowGeometryOption(QStringList() << "windowgeometry",
45+ "Specify the window geometry as [<width>x<height>]", "windowgeometry", "1");
46+ parser.addOption(windowGeometryOption);
47+
48+ QCommandLineOption testabilityOption("testability",
49+ "DISCOURAGED: Please set QT_LOAD_TESTABILITY instead.\nLoad the testability driver");
50+ parser.addOption(testabilityOption);
51+
52+ QCommandLineOption modeOption("mode",
53+ "Whether to run greeter and/or shell [full-greeter, full-shell, greeter, shell]",
54+ "mode", "full-greeter");
55+ parser.addOption(modeOption);
56+
57+ // Treat args with single dashes the same as arguments with two dashes
58+ // Ex: -fullscreen == --fullscreen
59+ parser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions);
60+
61+ parser.process(app);
62+
63+ if (parser.isSet(windowGeometryOption))
64+ {
65+ QStringList geom = parser.value(windowGeometryOption).split('x');
66+ if (geom.count() == 2) {
67+ m_windowGeometry.rwidth() = parsePixelsValue(geom[0]);
68+ m_windowGeometry.rheight() = parsePixelsValue(geom[1]);
69+ }
70+ }
71+
72+ m_hasTestability = parser.isSet(testabilityOption);
73+ m_hasFrameless = parser.isSet(framelessOption);
74+ m_hasMouseToTouch = parser.isSet(mousetouchOption);
75+ m_hasFullscreen = parser.isSet(fullscreenOption);
76+ resolveMode(parser, modeOption);
77+}
78+
79+int UnityCommandLineParser::parsePixelsValue(const QString &str)
80+{
81+ if (str.endsWith("gu", Qt::CaseInsensitive)) {
82+ QString numStr = str;
83+ numStr.remove(numStr.size() - 2, 2);
84+ return numStr.toInt() * m_gridUnit;
85+ } else {
86+ return str.toInt();
87+ }
88+}
89+
90+float UnityCommandLineParser::getenvFloat(const char* name, float defaultValue)
91+{
92+ QByteArray stringValue = qgetenv(name);
93+ bool ok;
94+ float value = stringValue.toFloat(&ok);
95+ return ok ? value : defaultValue;
96+}
97+
98+void UnityCommandLineParser::resolveMode(QCommandLineParser &parser, QCommandLineOption &modeOption)
99+{
100+ // If an invalid option was specified, set it to the default
101+ // If no default was provided in the QCommandLineOption constructor, abort.
102+ if (!parser.isSet(modeOption) ||
103+ (parser.value(modeOption) != "full-greeter" &&
104+ parser.value(modeOption) != "full-shell" &&
105+ parser.value(modeOption) != "greeter" &&
106+ parser.value(modeOption) != "shell")) {
107+
108+ if (modeOption.defaultValues().first() != nullptr) {
109+ m_mode = modeOption.defaultValues().first();
110+ qWarning() << "Mode argument was not provided or was set to an illegal value."
111+ " Using default value of --mode=" << m_mode;
112+ } else {
113+ qFatal("Shell mode argument was not provided and there is no default mode.");
114+ }
115+ } else {
116+ m_mode = parser.value(modeOption);
117+ }
118+}
src/UnityCommandLineParser.hnew file+50
@@ -0,0 +1,50 @@
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+#ifndef UNITY_COMMAND_LINE_PARSER_H
18+#define UNITY_COMMAND_LINE_PARSER_H
19+
20+#include <QCommandLineParser>
21+#include <QSize>
22+#include <QString>
23+
24+class UnityCommandLineParser {
25+public:
26+ UnityCommandLineParser(const QCoreApplication &app);
27+
28+ QSize windowGeometry() const { return m_windowGeometry; }
29+ bool hasTestability() const { return m_hasTestability; }
30+ bool hasFrameless() const { return m_hasFrameless; }
31+ bool hasMouseToTouch() const { return m_hasMouseToTouch; }
32+ bool hasFullscreen() const { return m_hasFullscreen; }
33+ QString mode() const { return m_mode; }
34+private:
35+
36+ int parsePixelsValue(const QString &str);
37+ static float getenvFloat(const char* name, float defaultValue);
38+ void resolveMode(QCommandLineParser &parser, QCommandLineOption &modeOption);
39+
40+ float m_gridUnit;
41+
42+ QSize m_windowGeometry;
43+ bool m_hasTestability;
44+ bool m_hasFrameless;
45+ bool m_hasMouseToTouch;
46+ bool m_hasFullscreen;
47+ QString m_mode;
48+};
49+
50+#endif // UNITY_COMMAND_LINE_PARSER_H
src/main.cpp+11−65
@@ -30,6 +30,7 @@
30#include "MouseTouchAdaptor.h"
30#include "MouseTouchAdaptor.h"
31#include "ApplicationArguments.h"
31#include "ApplicationArguments.h"
32#include "CachingNetworkManagerFactory.h"
32#include "CachingNetworkManagerFactory.h"
33+#include "UnityCommandLineParser.h"
33
34
34// Ubuntu Gestures
35// Ubuntu Gestures
35#include <TouchRegistry.h>
36#include <TouchRegistry.h>
@@ -45,42 +46,9 @@ int main(int argc, const char *argv[])
45 QGuiApplication::setApplicationName("unity8");
46 QGuiApplication::setApplicationName("unity8");
46 QGuiApplication *application;
47 QGuiApplication *application;
47
48
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− QCommandLineOption modeOption("mode",
74− "Whether to run greeter and/or shell [full-greeter, full-shell, greeter, shell]",
75− "mode", "full-greeter");
76− parser.addOption(modeOption);
77−
78 application = new QGuiApplication(argc, (char**)argv);
49 application = new QGuiApplication(argc, (char**)argv);
79
50
80− // Treat args with single dashes the same as arguments with two dashes
51+ UnityCommandLineParser parser(*application);
81− // Ex: -fullscreen == --fullscreen
82− parser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions);
83− parser.process(*application);
84
52
85 QString indicatorProfile = qgetenv("UNITY_INDICATOR_PROFILE");
53 QString indicatorProfile = qgetenv("UNITY_INDICATOR_PROFILE");
86 if (indicatorProfile.isEmpty()) {
54 if (indicatorProfile.isEmpty()) {
@@ -88,36 +56,10 @@ Load the testability driver");
88 }
56 }
89
57
90 ApplicationArguments qmlArgs;
58 ApplicationArguments qmlArgs;
91− if (parser.isSet(windowGeometryOption) &&
92− parser.value(windowGeometryOption).split('x').size() == 2)
93− {
94− QStringList geom = parser.value(windowGeometryOption).split('x');
95− qmlArgs.setSize(geom.at(0).toInt(), geom.at(1).toInt());
96− }
97−
98− // If an invalid option was specified, set it to the default
99− // If no default was provided in the QCommandLineOption constructor, abort.
100− QString shellMode;
101− if(!parser.isSet(modeOption) ||
102− (parser.value(modeOption) != "full-greeter" &&
103− parser.value(modeOption) != "full-shell" &&
104− parser.value(modeOption) != "greeter" &&
105− parser.value(modeOption) != "shell"))
106− {
107− if (modeOption.defaultValues().first() != nullptr) {
108− shellMode = modeOption.defaultValues().first();
109− qWarning() << "Mode argument was not provided or was set to an illegal value. Using default value of --mode=" << shellMode;
110− } else {
111− qFatal("Shell mode argument was not provided and there is no default mode,");
112− }
113−
114− } else {
115− shellMode = parser.value(modeOption);
116− }
117
59
118 // The testability driver is only loaded by QApplication but not by QGuiApplication.
60 // The testability driver is only loaded by QApplication but not by QGuiApplication.
119 // However, QApplication depends on QWidget which would add some unneeded overhead => Let's load the testability driver on our own.
61 // However, QApplication depends on QWidget which would add some unneeded overhead => Let's load the testability driver on our own.
120− if (parser.isSet(testabilityOption) || getenv("QT_LOAD_TESTABILITY")) {
62+ if (parser.hasTestability() || getenv("QT_LOAD_TESTABILITY")) {
121 QLibrary testLib(QLatin1String("qttestability"));
63 QLibrary testLib(QLatin1String("qttestability"));
122 if (testLib.load()) {
64 if (testLib.load()) {
123 typedef void (*TasInitialize)(void);
65 typedef void (*TasInitialize)(void);
@@ -142,17 +84,21 @@ Load the testability driver");
142 view->engine()->setBaseUrl(QUrl::fromLocalFile(::qmlDirectory()));
84 view->engine()->setBaseUrl(QUrl::fromLocalFile(::qmlDirectory()));
143 view->rootContext()->setContextProperty("applicationArguments", &qmlArgs);
85 view->rootContext()->setContextProperty("applicationArguments", &qmlArgs);
144 view->rootContext()->setContextProperty("indicatorProfile", indicatorProfile);
86 view->rootContext()->setContextProperty("indicatorProfile", indicatorProfile);
145− view->rootContext()->setContextProperty("shellMode", shellMode);
87+ view->rootContext()->setContextProperty("shellMode", parser.mode());
146− if (parser.isSet(framelessOption)) {
88+ if (parser.hasFrameless()) {
147 view->setFlags(Qt::FramelessWindowHint);
89 view->setFlags(Qt::FramelessWindowHint);
148 }
90 }
149 TouchRegistry touchRegistry;
91 TouchRegistry touchRegistry;
150 view->installEventFilter(&touchRegistry);
92 view->installEventFilter(&touchRegistry);
93+ if (parser.windowGeometry().isValid()) {
94+ view->setWidth(parser.windowGeometry().width());
95+ view->setHeight(parser.windowGeometry().height());
96+ }
151
97
152 // You will need this if you want to interact with touch-only components using a mouse
98 // You will need this if you want to interact with touch-only components using a mouse
153 // Needed only when manually testing on a desktop.
99 // Needed only when manually testing on a desktop.
154 MouseTouchAdaptor *mouseTouchAdaptor = 0;
100 MouseTouchAdaptor *mouseTouchAdaptor = 0;
155− if (parser.isSet(mousetouchOption)) {
101+ if (parser.hasMouseToTouch()) {
156 mouseTouchAdaptor = MouseTouchAdaptor::instance();
102 mouseTouchAdaptor = MouseTouchAdaptor::instance();
157 }
103 }
158
104
@@ -169,7 +115,7 @@ Load the testability driver");
169 view->setSource(source);
115 view->setSource(source);
170 QObject::connect(view->engine(), SIGNAL(quit()), application, SLOT(quit()));
116 QObject::connect(view->engine(), SIGNAL(quit()), application, SLOT(quit()));
171
117
172− if (isMirServer || parser.isSet(fullscreenOption)) {
118+ if (isMirServer || parser.hasFullscreen()) {
173 view->showFullScreen();
119 view->showFullScreen();
174 } else {
120 } else {
175 view->show();
121 view->show();