Unity 8
UnityCommandLineParser.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 "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(QStringLiteral("Description: Unity 8 Shell"));
30  parser.addHelpOption();
31 
32  QCommandLineOption fullscreenOption(QStringLiteral("fullscreen"),
33  QStringLiteral("Run in fullscreen"));
34  parser.addOption(fullscreenOption);
35 
36  QCommandLineOption framelessOption(QStringLiteral("frameless"),
37  QStringLiteral("Run without window borders"));
38  parser.addOption(framelessOption);
39 
40  QCommandLineOption mousetouchOption(QStringLiteral("mousetouch"),
41  QStringLiteral("Allow the mouse to provide touch input"));
42  parser.addOption(mousetouchOption);
43 
44  QCommandLineOption windowGeometryOption(QStringList() << QStringLiteral("windowgeometry"),
45  QStringLiteral("Specify the window geometry as [<width>x<height>]"), QStringLiteral("windowgeometry"), QStringLiteral("1"));
46  parser.addOption(windowGeometryOption);
47 
48  QCommandLineOption testabilityOption(QStringLiteral("testability"),
49  QStringLiteral("DISCOURAGED: Please set QT_LOAD_TESTABILITY instead.\nLoad the testability driver"));
50  parser.addOption(testabilityOption);
51 
52  QCommandLineOption devicenameOption(QStringList() << QStringLiteral("devicename"),
53  QStringLiteral("Specify the device name instead of letting Unity 8 find it out"), QStringLiteral("devicename"), QLatin1String(""));
54  parser.addOption(devicenameOption);
55 
56  QCommandLineOption modeOption(QStringLiteral("mode"),
57  QStringLiteral("Whether to run greeter and/or shell [full-greeter, full-shell, greeter, shell]"),
58  QStringLiteral("mode"), QStringLiteral("full-greeter"));
59  parser.addOption(modeOption);
60 
61  // Treat args with single dashes the same as arguments with two dashes
62  // Ex: -fullscreen == --fullscreen
63  parser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions);
64 
65  parser.process(app);
66 
67  if (parser.isSet(windowGeometryOption))
68  {
69  QStringList geom = parser.value(windowGeometryOption).split('x');
70  if (geom.count() == 2) {
71  m_windowGeometry.rwidth() = parsePixelsValue(geom[0]);
72  m_windowGeometry.rheight() = parsePixelsValue(geom[1]);
73  }
74  }
75 
76  m_hasTestability = parser.isSet(testabilityOption);
77  m_hasFrameless = parser.isSet(framelessOption);
78  m_hasMouseToTouch = parser.isSet(mousetouchOption);
79  m_hasFullscreen = parser.isSet(fullscreenOption);
80  m_deviceName = parser.value(devicenameOption);
81  resolveMode(parser, modeOption);
82 }
83 
84 int UnityCommandLineParser::parsePixelsValue(const QString &str)
85 {
86  if (str.endsWith(QLatin1String("gu"), Qt::CaseInsensitive)) {
87  QString numStr = str;
88  numStr.remove(numStr.size() - 2, 2);
89  return numStr.toInt() * m_gridUnit;
90  } else {
91  return str.toInt();
92  }
93 }
94 
95 float UnityCommandLineParser::getenvFloat(const char* name, float defaultValue)
96 {
97  QByteArray stringValue = qgetenv(name);
98  bool ok;
99  float value = stringValue.toFloat(&ok);
100  return ok ? value : defaultValue;
101 }
102 
103 void UnityCommandLineParser::resolveMode(QCommandLineParser &parser, QCommandLineOption &modeOption)
104 {
105  // If an invalid option was specified, set it to the default
106  // If no default was provided in the QCommandLineOption constructor, abort.
107  if (!parser.isSet(modeOption) ||
108  (parser.value(modeOption) != QLatin1String("full-greeter") &&
109  parser.value(modeOption) != QLatin1String("full-shell") &&
110  parser.value(modeOption) != QLatin1String("greeter") &&
111  parser.value(modeOption) != QLatin1String("shell"))) {
112 
113  const QStringList defaultValues = modeOption.defaultValues();
114  if (!defaultValues.isEmpty()) {
115  m_mode = defaultValues.first();
116  qWarning() << "Mode argument was not provided or was set to an illegal value."
117  " Using default value of --mode=" << m_mode;
118  } else {
119  qFatal("Shell mode argument was not provided and there is no default mode.");
120  }
121  } else {
122  m_mode = parser.value(modeOption);
123  }
124 }