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