-
Notifications
You must be signed in to change notification settings - Fork 93
/
main.cpp
252 lines (200 loc) · 5.76 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
//
// main.cpp: Entry point
// Copyright (C) 2018 Gonzalo José Carracedo Carballal
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program. If not, see
// <http://www.gnu.org/licenses/>
//
#include <QApplication>
#include <QTranslator>
#include <RMSViewer.h>
#include <iostream>
#include <QFont>
#include <QSurface>
#include "Loader.h"
#include <QtGlobal>
#include <sigutils/version.h>
#include <analyzer/version.h>
#include <FileViewer.h>
#include <cstring>
#include <getopt.h>
#define MAX_LOG_MESSAGES 20
using namespace SigDigger;
static int
runRMSViewer(QApplication &app)
{
int ret;
RMSViewer viewer;
viewer.show();
ret = app.exec();
return ret;
}
static int
runFileViewer(QApplication &app)
{
int ret;
FileViewer viewer;
ret = app.exec();
return ret;
}
static QString
getLogText(void)
{
QString text = "";
std::lock_guard<Suscan::Logger> guard(*Suscan::Logger::getInstance());
auto begin = Suscan::Logger::getInstance()->begin();
auto end = Suscan::Logger::getInstance()->end();
if (end - begin > MAX_LOG_MESSAGES) {
auto removed = end - begin - MAX_LOG_MESSAGES;
begin = end - MAX_LOG_MESSAGES;
text += "(" + QString::number(removed) + " previous messages omitted)\n";
}
for (auto p = begin; p != end; ++p) {
switch (p->severity) {
case SU_LOG_SEVERITY_CRITICAL:
text += "critical: ";
break;
case SU_LOG_SEVERITY_DEBUG:
text += "debug: ";
break;
case SU_LOG_SEVERITY_ERROR:
text += "error: ";
break;
case SU_LOG_SEVERITY_INFO:
text += "info: ";
break;
case SU_LOG_SEVERITY_WARNING:
text += "warning: ";
break;
}
text += p->message.c_str();
}
return text;
}
static int
runSigDigger(QApplication &app)
{
int ret = 1;
try {
Application main_app;
Loader loader(&main_app);
QSurfaceFormat fmt;
fmt.setSamples(16);
QSurfaceFormat::setDefaultFormat(fmt);
loader.load();
ret = app.exec();
Suscan::Singleton::get_instance()->killBackgroundTaskController();
std::cout << "Saving config..." << std::endl;
loader.saveConfig();
} catch (Suscan::Exception const &e) {
(void) QMessageBox::critical(
nullptr,
"SigDigger internal error",
QString(e.what())
+ "<pre>" + getLogText() + "</pre>",
QMessageBox::Close);
app.quit();
}
return ret;
}
static void
help(const char *argv0)
{
fprintf(stderr, "%s: SigDigger launcher binary\n", argv0);
fprintf(stderr, "Usage:\n");
fprintf(stderr, " %s [options] \n\n", argv0);
fprintf(stderr, "Options:\n\n");
fprintf(stderr, " -t, --tool=\"tool name\" Tool to launch\n");
fprintf(stderr, " -h, --help This help\n\n");
fprintf(
stderr,
"Tool name can be either one of SigDigger (default) and RMSViewer\n\n");
fprintf(
stderr,
"Using suscan version %s (%s)\n",
suscan_api_version(),
suscan_pkgversion());
fprintf(
stderr,
"Using sigutils version %s (%s)\n\n",
sigutils_api_version(),
sigutils_pkgversion());
fprintf(stderr, "Copyright © 2020 Gonzalo José Carracedo Carballal\n");
fprintf(
stderr,
"License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\n");
}
static struct option long_options[] = {
{"tool", required_argument, nullptr, 't' },
{"help", no_argument, nullptr, 'h' },
{nullptr, 0, nullptr, 0 }
};
int
main(int argc, char *argv[])
{
// This is a workaround for the poor backwards compatibility of macOS
// with software that used to work in previous releases. macOS is far
// from being a serious piece of engineering, and developing stuff
// in macOS implies handling all the stuff that Apple engineers forgot
// to do right.
#ifdef Q_OS_MACOS
qputenv("QT_MAC_WANTS_LAYER", "1");
#endif // Q_OS_MACOS
QApplication app(argc, argv);
QString appName = "SigDigger";
int ret = EXIT_FAILURE;
int c;
QTranslator translator;
if (!translator.load(QLocale(), "SigDigger", "_", ":/i18n"))
fprintf(stderr, "%s: locale %s not found, using English as default\n", argv[0], QLocale().name().toStdString().c_str());
else
app.installTranslator(&translator);
#ifdef __APPLE__
QFont::insertSubstitution("Monospace", "Monaco");
#endif // __APPLE__
while (true) {
int option_index = 0;
c = getopt_long(argc, argv, "t:h", long_options, &option_index);
if (c == -1)
break;
switch (c) {
case 't':
appName = optarg;
break;
case 'h':
help(argv[0]);
exit(EXIT_SUCCESS);
case '?':
fprintf(stderr, "%s: invalid option `-%c'.\n", argv[0], optopt);
help(argv[0]);
break;
default:
fprintf(stderr, "%s: unexpected getopt_long retcode %d\n", argv[0], c);
}
}
if (appName == "SigDigger") {
ret = runSigDigger(app);
} else if (appName == "RMSViewer") {
ret = runRMSViewer(app);
} else if (appName == "FileViewer") {
ret = runFileViewer(app);
} else {
fprintf(
stderr,
"%s: unknown tool `%s'\n",
argv[0],
appName.toStdString().c_str());
}
exit(ret);
}