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