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