on bionic help sign in
unity8/src/UnityCommandLineParser.cpp
130 lines 5.1 kB history raw
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 */
17#include "UnityCommandLineParser.h"
19#include <QDebug>
21#define ENV_GRID_UNIT_PX "GRID_UNIT_PX"
22#define DEFAULT_GRID_UNIT_PX 8
24UnityCommandLineParser::UnityCommandLineParser(const QCoreApplication &app)
26 m_gridUnit = getenvFloat(ENV_GRID_UNIT_PX, DEFAULT_GRID_UNIT_PX);
28 QCommandLineParser parser;
29 parser.setApplicationDescription(QStringLiteral("Description: Unity 8 Shell"));
30 parser.addHelpOption();
32 QCommandLineOption fullscreenOption(QStringLiteral("fullscreen"),
33 QStringLiteral("Run in fullscreen"));
34 parser.addOption(fullscreenOption);
36 QCommandLineOption framelessOption(QStringLiteral("frameless"),
37 QStringLiteral("Run without window borders"));
38 parser.addOption(framelessOption);
40 #ifdef UNITY8_ENABLE_TOUCH_EMULATION
41 QCommandLineOption mousetouchOption(QStringLiteral("mousetouch"),
42 QStringLiteral("Allow the mouse to provide touch input"));
43 parser.addOption(mousetouchOption);
44 #endif
46 QCommandLineOption windowGeometryOption(QStringList() << QStringLiteral("windowgeometry"),
47 QStringLiteral("Specify the window geometry as [<width>x<height>]"), QStringLiteral("windowgeometry"), QStringLiteral("1"));
48 parser.addOption(windowGeometryOption);
50 QCommandLineOption testabilityOption(QStringLiteral("testability"),
51 QStringLiteral("DISCOURAGED: Please set QT_LOAD_TESTABILITY instead.\nLoad the testability driver"));
52 parser.addOption(testabilityOption);
54 QCommandLineOption devicenameOption(QStringList() << QStringLiteral("devicename"),
55 QStringLiteral("Specify the device name instead of letting Unity 8 find it out"), QStringLiteral("devicename"), QLatin1String(""));
56 parser.addOption(devicenameOption);
58 QCommandLineOption modeOption(QStringLiteral("mode"),
59 QStringLiteral("Whether to run greeter and/or shell [full-greeter, full-shell, greeter, shell]"),
60 QStringLiteral("mode"), QStringLiteral("full-greeter"));
61 parser.addOption(modeOption);
63 // Treat args with single dashes the same as arguments with two dashes
64 // Ex: -fullscreen == --fullscreen
65 parser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions);
67 parser.process(app);
69 if (parser.isSet(windowGeometryOption))
70 {
71 QStringList geom = parser.value(windowGeometryOption).split('x');
72 if (geom.count() == 2) {
73 m_windowGeometry.rwidth() = parsePixelsValue(geom[0]);
74 m_windowGeometry.rheight() = parsePixelsValue(geom[1]);
75 }
76 }
78 m_hasTestability = parser.isSet(testabilityOption);
79 m_hasFrameless = parser.isSet(framelessOption);
81 #ifdef UNITY8_ENABLE_TOUCH_EMULATION
82 m_hasMouseToTouch = parser.isSet(mousetouchOption);
83 #endif
85 m_hasFullscreen = parser.isSet(fullscreenOption);
86 m_deviceName = parser.value(devicenameOption);
87 resolveMode(parser, modeOption);
90int UnityCommandLineParser::parsePixelsValue(const QString &str)
92 if (str.endsWith(QLatin1String("gu"), Qt::CaseInsensitive)) {
93 QString numStr = str;
94 numStr.remove(numStr.size() - 2, 2);
95 return numStr.toInt() * m_gridUnit;
96 } else {
97 return str.toInt();
98 }
101float UnityCommandLineParser::getenvFloat(const char* name, float defaultValue)
103 QByteArray stringValue = qgetenv(name);
104 bool ok;
105 float value = stringValue.toFloat(&ok);
106 return ok ? value : defaultValue;
109void UnityCommandLineParser::resolveMode(QCommandLineParser &parser, QCommandLineOption &modeOption)
111 // If an invalid option was specified, set it to the default
112 // If no default was provided in the QCommandLineOption constructor, abort.
113 if (!parser.isSet(modeOption) ||
114 (parser.value(modeOption) != QLatin1String("full-greeter") &&
115 parser.value(modeOption) != QLatin1String("full-shell") &&
116 parser.value(modeOption) != QLatin1String("greeter") &&
117 parser.value(modeOption) != QLatin1String("shell"))) {
119 const QStringList defaultValues = modeOption.defaultValues();
120 if (!defaultValues.isEmpty()) {
121 m_mode = defaultValues.first();
122 qWarning() << "Mode argument was not provided or was set to an illegal value."
123 " Using default value of --mode=" << m_mode;
124 } else {
125 qFatal("Shell mode argument was not provided and there is no default mode.");
126 }
127 } else {
128 m_mode = parser.value(modeOption);
129 }