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  #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
45 
46  QCommandLineOption windowGeometryOption(QStringList() << QStringLiteral("windowgeometry"),
47  QStringLiteral("Specify the window geometry as [<width>x<height>]"), QStringLiteral("windowgeometry"), QStringLiteral("1"));
48  parser.addOption(windowGeometryOption);
49 
50  QCommandLineOption testabilityOption(QStringLiteral("testability"),
51  QStringLiteral("DISCOURAGED: Please set QT_LOAD_TESTABILITY instead.\nLoad the testability driver"));
52  parser.addOption(testabilityOption);
53 
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);
57 
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);
62 
63  // Treat args with single dashes the same as arguments with two dashes
64  // Ex: -fullscreen == --fullscreen
65  parser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions);
66 
67  parser.process(app);
68 
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  }
77 
78  m_hasTestability = parser.isSet(testabilityOption);
79  m_hasFrameless = parser.isSet(framelessOption);
80 
81  #ifdef UNITY8_ENABLE_TOUCH_EMULATION
82  m_hasMouseToTouch = parser.isSet(mousetouchOption);
83  #endif
84 
85  m_hasFullscreen = parser.isSet(fullscreenOption);
86  m_deviceName = parser.value(devicenameOption);
87  resolveMode(parser, modeOption);
88 }
89 
90 int UnityCommandLineParser::parsePixelsValue(const QString &str)
91 {
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  }
99 }
100 
101 float UnityCommandLineParser::getenvFloat(const char* name, float defaultValue)
102 {
103  QByteArray stringValue = qgetenv(name);
104  bool ok;
105  float value = stringValue.toFloat(&ok);
106  return ok ? value : defaultValue;
107 }
108 
109 void UnityCommandLineParser::resolveMode(QCommandLineParser &parser, QCommandLineOption &modeOption)
110 {
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"))) {
118 
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  }
130 }