]> sigrok.org Git - pulseview.git/blame - main.cpp
Add ENABLE_GSTREAMERMM, make gstreamermm support optional.
[pulseview.git] / main.cpp
CommitLineData
d7bed479 1/*
b3f22de0 2 * This file is part of the PulseView project.
d7bed479
JH
3 *
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
efdec55a 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
d7bed479
JH
18 */
19
269528f5 20#ifdef ENABLE_DECODE
aaabd61b 21#include <libsigrokdecode/libsigrokdecode.h> /* First, so we avoid a _POSIX_C_SOURCE warning. */
269528f5 22#endif
d52d8455 23
15a69e54 24#include <cstdint>
c5d6200c 25#include <fstream>
f7951df4 26#include <getopt.h>
caa2b3e9 27#include <vector>
f7951df4 28
5252f438 29#ifdef ENABLE_GSTREAMERMM
c9f57375 30#include <gstreamermm.h>
5252f438 31#endif
c9f57375 32
c5d6200c
SA
33#include <libsigrokcxx/libsigrokcxx.hpp>
34
35#include <QCheckBox>
664fca5c 36#include <QDebug>
c5d6200c
SA
37#include <QFile>
38#include <QFileInfo>
39#include <QMessageBox>
0bd39c0c 40#include <QSettings>
c5d6200c 41#include <QTextStream>
075fb499 42
49719858
SA
43#include "config.h"
44
140181a4 45#ifdef ENABLE_SIGNALS
2acdb232 46#include "signalhandler.hpp"
140181a4
JH
47#endif
48
c1a688de
SA
49#ifdef ENABLE_STACKTRACE
50#include <signal.h>
51#include <boost/stacktrace.hpp>
52#include <QStandardPaths>
53#endif
54
2acdb232
JH
55#include "pv/application.hpp"
56#include "pv/devicemanager.hpp"
bcb4c327
SA
57#include "pv/globalsettings.hpp"
58#include "pv/logging.hpp"
2acdb232 59#include "pv/mainwindow.hpp"
419ec4e1 60#include "pv/session.hpp"
21f58646 61#include "pv/util.hpp"
c1a688de 62
9137928c 63#ifdef ANDROID
aff51746 64#include <libsigrokandroidutils/libsigrokandroidutils.h>
dddff2e7 65#include "android/assetreader.hpp"
2acdb232 66#include "android/loghandler.hpp"
9137928c 67#endif
d7bed479 68
11a04e2a 69#ifdef _WIN32
11a04e2a 70#include <QtPlugin>
09f55d96
UH
71Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
72Q_IMPORT_PLUGIN(QSvgPlugin)
11a04e2a
UH
73#endif
74
6f925ba9 75using std::exception;
c5d6200c
SA
76using std::ifstream;
77using std::ofstream;
6f925ba9
UH
78using std::shared_ptr;
79using std::string;
80
c1a688de
SA
81#if ENABLE_STACKTRACE
82QString stacktrace_filename;
83
84void signal_handler(int signum)
85{
86 ::signal(signum, SIG_DFL);
87 boost::stacktrace::safe_dump_to(stacktrace_filename.toLocal8Bit().data());
88 ::raise(SIGABRT);
89}
c5d6200c
SA
90
91void process_stacktrace(QString temp_path)
92{
93 const QString stacktrace_outfile = temp_path + "/pv_stacktrace.txt";
94
95 ifstream ifs(stacktrace_filename.toLocal8Bit().data());
96 ofstream ofs(stacktrace_outfile.toLocal8Bit().data(),
97 ofstream::out | ofstream::trunc);
98
99 boost::stacktrace::stacktrace st =
100 boost::stacktrace::stacktrace::from_dump(ifs);
101 ofs << st;
102
103 ofs.close();
104 ifs.close();
105
106 QFile f(stacktrace_outfile);
107 f.open(QFile::ReadOnly | QFile::Text);
108 QTextStream fs(&f);
109 QString stacktrace = fs.readAll();
110 stacktrace = stacktrace.trimmed().replace('\n', "<br />");
111
112 qDebug() << QObject::tr("Stack trace of previous crash:");
113 qDebug() << "---------------------------------------------------------";
114 // Note: qDebug() prints quotation marks for QString output, so we feed it char*
115 qDebug() << stacktrace.toLocal8Bit().data();
116 qDebug() << "---------------------------------------------------------";
117
118 f.close();
119
120 // Remove stack trace so we don't process it again the next time we run
121 QFile::remove(stacktrace_filename.toLocal8Bit().data());
122
123 // Show notification dialog if permitted
124 pv::GlobalSettings settings;
125 if (settings.value(pv::GlobalSettings::Key_Log_NotifyOfStacktrace).toBool()) {
126 QCheckBox *cb = new QCheckBox(QObject::tr("Don't show this message again"));
127
128 QMessageBox msgbox;
129 msgbox.setText(QObject::tr("When %1 last crashed, it created a stack trace.\n" \
130 "A human-readable form has been saved to disk and was written to " \
131 "the log. You may access it from the settings dialog.").arg(PV_TITLE));
132 msgbox.setIcon(QMessageBox::Icon::Information);
133 msgbox.addButton(QMessageBox::Ok);
134 msgbox.setCheckBox(cb);
135
136 QObject::connect(cb, &QCheckBox::stateChanged, [](int state){
137 pv::GlobalSettings settings;
138 settings.setValue(pv::GlobalSettings::Key_Log_NotifyOfStacktrace,
139 !state); });
140
141 msgbox.exec();
142 }
143}
c1a688de
SA
144#endif
145
d4384c6d
JH
146void usage()
147{
9fd5bb6e 148 fprintf(stdout,
d4384c6d 149 "Usage:\n"
41e2ade4 150 " %s [OPTIONS] [FILE]\n"
d4384c6d
JH
151 "\n"
152 "Help Options:\n"
d4384c6d 153 " -h, -?, --help Show help option\n"
aa90e86b
JH
154 "\n"
155 "Application Options:\n"
156 " -V, --version Show release version\n"
157 " -l, --loglevel Set libsigrok/libsigrokdecode loglevel\n"
58d8e4c6 158 " -d, --driver Specify the device driver to use\n"
0e96068d 159 " -D, --dont-scan Don't auto-scan for devices, use -d spec only\n"
e3c79b07
JH
160 " -i, --input-file Load input from file\n"
161 " -I, --input-format Input format\n"
156f06e9 162 " -c, --clean Don't restore previous sessions on startup\n"
41e2ade4 163 "\n", PV_BIN_NAME);
d4384c6d
JH
164}
165
d7bed479
JH
166int main(int argc, char *argv[])
167{
be03620e 168 int ret = 0;
6f925ba9 169 shared_ptr<sigrok::Context> context;
caa2b3e9
SA
170 string open_file_format, driver;
171 vector<string> open_files;
156f06e9 172 bool restore_sessions = true;
1d6e35dc 173 bool do_scan = true;
49719858 174 bool show_version = false;
f2242929 175
5252f438 176#ifdef ENABLE_GSTREAMERMM
c9f57375
UH
177 // Initialise gstreamermm. Must be called before any other GLib stuff.
178 Gst::init();
5252f438 179#endif
c9f57375 180
dac1bb97 181 Application a(argc, argv);
640e5565 182
9137928c 183#ifdef ANDROID
aff51746 184 srau_init_environment();
9137928c 185 pv::AndroidLogHandler::install_callbacks();
dddff2e7 186 pv::AndroidAssetReader asset_reader;
9137928c
MC
187#endif
188
f7951df4 189 // Parse arguments
15a69e54 190 while (true) {
f7951df4 191 static const struct option long_options[] = {
f74015db
UH
192 {"help", no_argument, nullptr, 'h'},
193 {"version", no_argument, nullptr, 'V'},
194 {"loglevel", required_argument, nullptr, 'l'},
58d8e4c6 195 {"driver", required_argument, nullptr, 'd'},
0e96068d 196 {"dont-scan", no_argument, nullptr, 'D'},
f74015db
UH
197 {"input-file", required_argument, nullptr, 'i'},
198 {"input-format", required_argument, nullptr, 'I'},
156f06e9 199 {"clean", no_argument, nullptr, 'c'},
c062efdc 200 {"log-to-stdout", no_argument, nullptr, 's'},
f74015db 201 {nullptr, 0, nullptr, 0}
f7951df4
JH
202 };
203
815b3f26 204 const int c = getopt_long(argc, argv,
4b923408 205 "h?VDcl:d:i:I:", long_options, nullptr);
f7951df4
JH
206 if (c == -1)
207 break;
208
209 switch (c) {
aa90e86b
JH
210 case 'h':
211 case '?':
212 usage();
213 return 0;
214
215 case 'V':
49719858
SA
216 show_version = true;
217 break;
aa90e86b 218
9b5099b6
JH
219 case 'l':
220 {
221 const int loglevel = atoi(optarg);
ed6f8680
GS
222 if (loglevel < 0 || loglevel > 5) {
223 qDebug() << "ERROR: invalid log level spec.";
224 break;
225 }
e8d00928 226 context->set_log_level(sigrok::LogLevel::get(loglevel));
269528f5
JH
227
228#ifdef ENABLE_DECODE
9b5099b6 229 srd_log_loglevel_set(loglevel);
269528f5 230#endif
d52d8455 231
0bd39c0c
UH
232 if (loglevel >= 5) {
233 const QSettings settings;
234 qDebug() << "Settings:" << settings.fileName()
235 << "format" << settings.format();
236 }
9b5099b6
JH
237 break;
238 }
e3c79b07 239
58d8e4c6
GS
240 case 'd':
241 driver = optarg;
242 break;
243
1d6e35dc
GS
244 case 'D':
245 do_scan = false;
246 break;
247
e3c79b07 248 case 'i':
1f2082e2 249 open_files.emplace_back(optarg);
e3c79b07
JH
250 break;
251
252 case 'I':
253 open_file_format = optarg;
254 break;
156f06e9
SA
255
256 case 'c':
257 restore_sessions = false;
258 break;
f7951df4
JH
259 }
260 }
6a96e1b2
GS
261 argc -= optind;
262 argv += optind;
f7951df4 263
6a96e1b2 264 for (int i = 0; i < argc; i++)
1f2082e2 265 open_files.emplace_back(argv[i]);
1d478458 266
21f58646
SA
267 qRegisterMetaType<pv::util::Timestamp>("util::Timestamp");
268 qRegisterMetaType<uint64_t>("uint64_t");
269
bcb4c327
SA
270 // Prepare the global settings since logging needs them early on
271 pv::GlobalSettings settings;
374c697f 272 settings.save_internal_defaults();
bcb4c327 273 settings.set_defaults_where_needed();
37b0bd35 274 settings.apply_theme();
bcb4c327 275
4b923408 276 pv::logging.init();
bcb4c327 277
6a6772b7 278 // Initialise libsigrok
e8d00928 279 context = sigrok::Context::create();
419ec4e1
SA
280 pv::Session::sr_context = context;
281
c1a688de
SA
282#if ENABLE_STACKTRACE
283 QString temp_path = QStandardPaths::standardLocations(
284 QStandardPaths::TempLocation).at(0);
285 stacktrace_filename = temp_path + "/pv_stacktrace.dmp";
286 qDebug() << "Stack trace file is" << stacktrace_filename;
287
288 ::signal(SIGSEGV, &signal_handler);
289 ::signal(SIGABRT, &signal_handler);
c5d6200c
SA
290
291 if (QFileInfo::exists(stacktrace_filename))
292 process_stacktrace(temp_path);
c1a688de
SA
293#endif
294
dddff2e7
DE
295#ifdef ANDROID
296 context->set_resource_reader(&asset_reader);
297#endif
d52d8455
JH
298 do {
299
269528f5 300#ifdef ENABLE_DECODE
d52d8455 301 // Initialise libsigrokdecode
4c60462b 302 if (srd_init(nullptr) != SRD_OK) {
d52d8455
JH
303 qDebug() << "ERROR: libsigrokdecode init failed.";
304 break;
305 }
be03620e
JH
306
307 // Load the protocol decoders
308 srd_decoder_load_all();
269528f5 309#endif
be03620e 310
c1a688de 311#ifndef ENABLE_STACKTRACE
107ca6d3 312 try {
c1a688de 313#endif
664fca5c 314
c1a688de
SA
315 // Create the device manager, initialise the drivers
316 pv::DeviceManager device_manager(context, driver, do_scan);
664fca5c 317
49719858
SA
318 a.collect_version_info(context);
319 if (show_version) {
320 a.print_version_info();
321 } else {
322 // Initialise the main window
323 pv::MainWindow w(device_manager);
324 w.show();
3ed18835 325
49719858
SA
326 if (restore_sessions)
327 w.restore_sessions();
c1a688de 328
49719858
SA
329 if (open_files.empty())
330 w.add_default_session();
331 else
f4ab4b5c 332 for (string& open_file : open_files)
49719858 333 w.add_session_with_file(open_file, open_file_format);
3ed18835 334
140181a4 335#ifdef ENABLE_SIGNALS
49719858
SA
336 if (SignalHandler::prepare_signals()) {
337 SignalHandler *const handler = new SignalHandler(&w);
338 QObject::connect(handler, SIGNAL(int_received()),
339 &w, SLOT(close()));
340 QObject::connect(handler, SIGNAL(term_received()),
341 &w, SLOT(close()));
342 } else
343 qWarning() << "Could not prepare signal handler.";
140181a4 344#endif
7a255aa9 345
49719858
SA
346 // Run the application
347 ret = a.exec();
348 }
107ca6d3 349
c1a688de 350#ifndef ENABLE_STACKTRACE
30677c13 351 } catch (exception& e) {
c1a688de 352 qDebug() << "Exception:" << e.what();
6fb67b27 353 }
c1a688de 354#endif
6fb67b27 355
269528f5 356#ifdef ENABLE_DECODE
d52d8455 357 // Destroy libsigrokdecode
be03620e 358 srd_exit();
269528f5 359#endif
664fca5c 360
15a69e54 361 } while (false);
664fca5c 362
664fca5c 363 return ret;
d7bed479 364}