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