]> sigrok.org Git - pulseview.git/blame_incremental - main.cpp
Connect dialog: add baud rate combobox for serial ports
[pulseview.git] / main.cpp
... / ...
CommitLineData
1/*
2 * This file is part of the PulseView project.
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
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20#ifdef ENABLE_DECODE
21#include <libsigrokdecode/libsigrokdecode.h> /* First, so we avoid a _POSIX_C_SOURCE warning. */
22#endif
23
24#include <cstdint>
25#include <fstream>
26#include <getopt.h>
27#include <vector>
28
29#include <libsigrokcxx/libsigrokcxx.hpp>
30
31#include <QCheckBox>
32#include <QDebug>
33#include <QFile>
34#include <QFileInfo>
35#include <QMessageBox>
36#include <QSettings>
37#include <QTextStream>
38
39#include "config.h"
40
41#ifdef ENABLE_SIGNALS
42#include "signalhandler.hpp"
43#endif
44
45#ifdef ENABLE_STACKTRACE
46#include <signal.h>
47#include <boost/stacktrace.hpp>
48#include <QStandardPaths>
49#endif
50
51#include "pv/application.hpp"
52#include "pv/devicemanager.hpp"
53#include "pv/globalsettings.hpp"
54#include "pv/logging.hpp"
55#include "pv/mainwindow.hpp"
56#include "pv/session.hpp"
57#include "pv/util.hpp"
58
59#ifdef ANDROID
60#include <libsigrokandroidutils/libsigrokandroidutils.h>
61#include "android/assetreader.hpp"
62#include "android/loghandler.hpp"
63#endif
64
65#ifdef _WIN32
66#include <QtPlugin>
67Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
68Q_IMPORT_PLUGIN(QSvgPlugin)
69#endif
70
71using std::exception;
72using std::ifstream;
73using std::ofstream;
74using std::shared_ptr;
75using std::string;
76
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}
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}
140#endif
141
142void usage()
143{
144 fprintf(stdout,
145 "Usage:\n"
146 " %s [OPTIONS] [FILE]\n"
147 "\n"
148 "Help Options:\n"
149 " -h, -?, --help Show help option\n"
150 "\n"
151 "Application Options:\n"
152 " -V, --version Show release version\n"
153 " -l, --loglevel Set libsigrok/libsigrokdecode loglevel\n"
154 " -d, --driver Specify the device driver to use\n"
155 " -D, --dont-scan Don't auto-scan for devices, use -d spec only\n"
156 " -i, --input-file Load input from file\n"
157 " -I, --input-format Input format\n"
158 " -c, --clean Don't restore previous sessions on startup\n"
159 "\n", PV_BIN_NAME);
160}
161
162int main(int argc, char *argv[])
163{
164 int ret = 0;
165 shared_ptr<sigrok::Context> context;
166 string open_file_format, driver;
167 vector<string> open_files;
168 bool restore_sessions = true;
169 bool do_scan = true;
170 bool show_version = false;
171
172 Application a(argc, argv);
173
174#ifdef ANDROID
175 srau_init_environment();
176 pv::AndroidLogHandler::install_callbacks();
177 pv::AndroidAssetReader asset_reader;
178#endif
179
180 // Parse arguments
181 while (true) {
182 static const struct option long_options[] = {
183 {"help", no_argument, nullptr, 'h'},
184 {"version", no_argument, nullptr, 'V'},
185 {"loglevel", required_argument, nullptr, 'l'},
186 {"driver", required_argument, nullptr, 'd'},
187 {"dont-scan", no_argument, nullptr, 'D'},
188 {"input-file", required_argument, nullptr, 'i'},
189 {"input-format", required_argument, nullptr, 'I'},
190 {"clean", no_argument, nullptr, 'c'},
191 {"log-to-stdout", no_argument, nullptr, 's'},
192 {nullptr, 0, nullptr, 0}
193 };
194
195 const int c = getopt_long(argc, argv,
196 "h?VDcl:d:i:I:", long_options, nullptr);
197 if (c == -1)
198 break;
199
200 switch (c) {
201 case 'h':
202 case '?':
203 usage();
204 return 0;
205
206 case 'V':
207 show_version = true;
208 break;
209
210 case 'l':
211 {
212 const int loglevel = atoi(optarg);
213 if (loglevel < 0 || loglevel > 5) {
214 qDebug() << "ERROR: invalid log level spec.";
215 break;
216 }
217 context->set_log_level(sigrok::LogLevel::get(loglevel));
218
219#ifdef ENABLE_DECODE
220 srd_log_loglevel_set(loglevel);
221#endif
222
223 if (loglevel >= 5) {
224 const QSettings settings;
225 qDebug() << "Settings:" << settings.fileName()
226 << "format" << settings.format();
227 }
228 break;
229 }
230
231 case 'd':
232 driver = optarg;
233 break;
234
235 case 'D':
236 do_scan = false;
237 break;
238
239 case 'i':
240 open_files.emplace_back(optarg);
241 break;
242
243 case 'I':
244 open_file_format = optarg;
245 break;
246
247 case 'c':
248 restore_sessions = false;
249 break;
250 }
251 }
252 argc -= optind;
253 argv += optind;
254
255 for (int i = 0; i < argc; i++)
256 open_files.emplace_back(argv[i]);
257
258 qRegisterMetaType<pv::util::Timestamp>("util::Timestamp");
259 qRegisterMetaType<uint64_t>("uint64_t");
260
261 // Prepare the global settings since logging needs them early on
262 pv::GlobalSettings settings;
263 settings.save_internal_defaults();
264 settings.set_defaults_where_needed();
265 settings.apply_theme();
266
267 pv::logging.init();
268
269 // Initialise libsigrok
270 context = sigrok::Context::create();
271 pv::Session::sr_context = context;
272
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);
281
282 if (QFileInfo::exists(stacktrace_filename))
283 process_stacktrace(temp_path);
284#endif
285
286#ifdef ANDROID
287 context->set_resource_reader(&asset_reader);
288#endif
289 do {
290
291#ifdef ENABLE_DECODE
292 // Initialise libsigrokdecode
293 if (srd_init(nullptr) != SRD_OK) {
294 qDebug() << "ERROR: libsigrokdecode init failed.";
295 break;
296 }
297
298 // Load the protocol decoders
299 srd_decoder_load_all();
300#endif
301
302#ifndef ENABLE_STACKTRACE
303 try {
304#endif
305
306 // Create the device manager, initialise the drivers
307 pv::DeviceManager device_manager(context, driver, do_scan);
308
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();
316
317 if (restore_sessions)
318 w.restore_sessions();
319
320 if (open_files.empty())
321 w.add_default_session();
322 else
323 for (string& open_file : open_files)
324 w.add_session_with_file(open_file, open_file_format);
325
326#ifdef ENABLE_SIGNALS
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.";
335#endif
336
337 // Run the application
338 ret = a.exec();
339 }
340
341#ifndef ENABLE_STACKTRACE
342 } catch (exception& e) {
343 qDebug() << "Exception:" << e.what();
344 }
345#endif
346
347#ifdef ENABLE_DECODE
348 // Destroy libsigrokdecode
349 srd_exit();
350#endif
351
352 } while (false);
353
354 return ret;
355}