on 524dc07 help sign in
unity8/src/UnityCommandLineParser.cpp
125 lines 4.8 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 modeOption(QStringLiteral("mode"),
55 QStringLiteral("Whether to run greeter and/or shell [full-greeter, full-shell, greeter, shell]"),
56 QStringLiteral("mode"), QStringLiteral("full-greeter"));
57 parser.addOption(modeOption);
59 // Treat args with single dashes the same as arguments with two dashes
60 // Ex: -fullscreen == --fullscreen
61 parser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions);
63 parser.process(app);
65 if (parser.isSet(windowGeometryOption))
66 {
67 QStringList geom = parser.value(windowGeometryOption).split('x');
68 if (geom.count() == 2) {
69 m_windowGeometry.rwidth() = parsePixelsValue(geom[0]);
70 m_windowGeometry.rheight() = parsePixelsValue(geom[1]);
71 }
72 }
74 m_hasTestability = parser.isSet(testabilityOption);
75 m_hasFrameless = parser.isSet(framelessOption);
77 #ifdef UNITY8_ENABLE_TOUCH_EMULATION
78 m_hasMouseToTouch = parser.isSet(mousetouchOption);
79 #endif
81 m_hasFullscreen = parser.isSet(fullscreenOption);
82 resolveMode(parser, modeOption);
85int UnityCommandLineParser::parsePixelsValue(const QString &str)
87 if (str.endsWith(QLatin1String("gu"), Qt::CaseInsensitive)) {
88 QString numStr = str;
89 numStr.remove(numStr.size() - 2, 2);
90 return numStr.toInt() * m_gridUnit;
91 } else {
92 return str.toInt();
93 }
96float UnityCommandLineParser::getenvFloat(const char* name, float defaultValue)
98 QByteArray stringValue = qgetenv(name);
99 bool ok;
100 float value = stringValue.toFloat(&ok);
101 return ok ? value : defaultValue;
104void UnityCommandLineParser::resolveMode(QCommandLineParser &parser, QCommandLineOption &modeOption)
106 // If an invalid option was specified, set it to the default
107 // If no default was provided in the QCommandLineOption constructor, abort.
108 if (!parser.isSet(modeOption) ||
109 (parser.value(modeOption) != QLatin1String("full-greeter") &&
110 parser.value(modeOption) != QLatin1String("full-shell") &&
111 parser.value(modeOption) != QLatin1String("greeter") &&
112 parser.value(modeOption) != QLatin1String("shell"))) {
114 const QStringList defaultValues = modeOption.defaultValues();
115 if (!defaultValues.isEmpty()) {
116 m_mode = defaultValues.first();
117 qWarning() << "Mode argument was not provided or was set to an illegal value."
118 " Using default value of --mode=" << m_mode;
119 } else {
120 qFatal("Shell mode argument was not provided and there is no default mode.");
121 }
122 } else {
123 m_mode = parser.value(modeOption);
124 }