Make AnalogSignal inherit LogicSignal
[pulseview.git] / main.cpp
blob3a5ac29fc2bca41ae597f812ee90d88040b2a02f
1 /*
2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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/>.
20 #ifdef ENABLE_DECODE
21 #include <libsigrokdecode/libsigrokdecode.h> /* First, so we avoid a _POSIX_C_SOURCE warning. */
22 #endif
24 #include <cstdint>
25 #include <fstream>
26 #include <getopt.h>
27 #include <vector>
29 #ifdef ENABLE_FLOW
30 #include <gstreamermm.h>
31 #include <libsigrokflow/libsigrokflow.hpp>
32 #endif
34 #include <libsigrokcxx/libsigrokcxx.hpp>
36 #include <QCheckBox>
37 #include <QDebug>
38 #include <QFile>
39 #include <QFileInfo>
40 #include <QMessageBox>
41 #include <QSettings>
42 #include <QTextStream>
44 #include "config.h"
46 #ifdef ENABLE_SIGNALS
47 #include "signalhandler.hpp"
48 #endif
50 #ifdef ENABLE_STACKTRACE
51 #include <signal.h>
52 #include <boost/stacktrace.hpp>
53 #include <QStandardPaths>
54 #endif
56 #include "pv/application.hpp"
57 #include "pv/devicemanager.hpp"
58 #include "pv/globalsettings.hpp"
59 #include "pv/logging.hpp"
60 #include "pv/mainwindow.hpp"
61 #include "pv/session.hpp"
62 #include "pv/util.hpp"
63 #include "pv/data/segment.hpp"
65 #ifdef ANDROID
66 #include <libsigrokandroidutils/libsigrokandroidutils.h>
67 #include "android/assetreader.hpp"
68 #include "android/loghandler.hpp"
69 #endif
71 #ifdef _WIN32
72 #include <QtPlugin>
73 Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
74 Q_IMPORT_PLUGIN(QSvgPlugin)
75 #endif
77 using std::exception;
78 using std::ifstream;
79 using std::ofstream;
80 using std::shared_ptr;
81 using std::string;
83 #if ENABLE_STACKTRACE
84 QString stacktrace_filename;
86 void signal_handler(int signum)
88 ::signal(signum, SIG_DFL);
89 boost::stacktrace::safe_dump_to(stacktrace_filename.toLocal8Bit().data());
90 ::raise(SIGABRT);
93 void process_stacktrace(QString temp_path)
95 const QString stacktrace_outfile = temp_path + "/pv_stacktrace.txt";
97 ifstream ifs(stacktrace_filename.toLocal8Bit().data());
98 ofstream ofs(stacktrace_outfile.toLocal8Bit().data(),
99 ofstream::out | ofstream::trunc);
101 boost::stacktrace::stacktrace st =
102 boost::stacktrace::stacktrace::from_dump(ifs);
103 ofs << st;
105 ofs.close();
106 ifs.close();
108 QFile f(stacktrace_outfile);
109 f.open(QFile::ReadOnly | QFile::Text);
110 QTextStream fs(&f);
111 QString stacktrace = fs.readAll();
112 stacktrace = stacktrace.trimmed().replace('\n', "<br />");
114 qDebug() << QObject::tr("Stack trace of previous crash:");
115 qDebug() << "---------------------------------------------------------";
116 // Note: qDebug() prints quotation marks for QString output, so we feed it char*
117 qDebug() << stacktrace.toLocal8Bit().data();
118 qDebug() << "---------------------------------------------------------";
120 f.close();
122 // Remove stack trace so we don't process it again the next time we run
123 QFile::remove(stacktrace_filename.toLocal8Bit().data());
125 // Show notification dialog if permitted
126 pv::GlobalSettings settings;
127 if (settings.value(pv::GlobalSettings::Key_Log_NotifyOfStacktrace).toBool()) {
128 QCheckBox *cb = new QCheckBox(QObject::tr("Don't show this message again"));
130 QMessageBox msgbox;
131 msgbox.setText(QObject::tr("When %1 last crashed, it created a stack trace.\n" \
132 "A human-readable form has been saved to disk and was written to " \
133 "the log. You may access it from the settings dialog.").arg(PV_TITLE));
134 msgbox.setIcon(QMessageBox::Icon::Information);
135 msgbox.addButton(QMessageBox::Ok);
136 msgbox.setCheckBox(cb);
138 QObject::connect(cb, &QCheckBox::stateChanged, [](int state){
139 pv::GlobalSettings settings;
140 settings.setValue(pv::GlobalSettings::Key_Log_NotifyOfStacktrace,
141 !state); });
143 msgbox.exec();
146 #endif
148 void usage()
150 fprintf(stdout,
151 "Usage:\n"
152 " %s [OPTIONS] [FILE]\n"
153 "\n"
154 "Help Options:\n"
155 " -h, -?, --help Show help option\n"
156 "\n"
157 "Application Options:\n"
158 " -V, --version Show release version\n"
159 " -l, --loglevel Set libsigrok/libsigrokdecode loglevel\n"
160 " -d, --driver Specify the device driver to use\n"
161 " -D, --dont-scan Don't auto-scan for devices, use -d spec only\n"
162 " -i, --input-file Load input from file\n"
163 " -s, --settings Load PulseView session setup from file\n"
164 " -I, --input-format Input format\n"
165 " -c, --clean Don't restore previous sessions on startup\n"
166 "\n", PV_BIN_NAME);
169 int main(int argc, char *argv[])
171 int ret = 0;
172 shared_ptr<sigrok::Context> context;
173 string open_file_format, open_setup_file, driver;
174 vector<string> open_files;
175 bool restore_sessions = true;
176 bool do_scan = true;
177 bool show_version = false;
179 #ifdef ENABLE_FLOW
180 // Initialise gstreamermm. Must be called before any other GLib stuff.
181 Gst::init();
183 // Initialize libsigrokflow. Must be called after Gst::init().
184 Srf::init();
185 #endif
187 Application a(argc, argv);
189 #ifdef ANDROID
190 srau_init_environment();
191 pv::AndroidLogHandler::install_callbacks();
192 pv::AndroidAssetReader asset_reader;
193 #endif
195 // Parse arguments
196 while (true) {
197 static const struct option long_options[] = {
198 {"help", no_argument, nullptr, 'h'},
199 {"version", no_argument, nullptr, 'V'},
200 {"loglevel", required_argument, nullptr, 'l'},
201 {"driver", required_argument, nullptr, 'd'},
202 {"dont-scan", no_argument, nullptr, 'D'},
203 {"input-file", required_argument, nullptr, 'i'},
204 {"settings", required_argument, nullptr, 's'},
205 {"input-format", required_argument, nullptr, 'I'},
206 {"clean", no_argument, nullptr, 'c'},
207 {"log-to-stdout", no_argument, nullptr, 's'},
208 {nullptr, 0, nullptr, 0}
211 const int c = getopt_long(argc, argv,
212 "h?VDcl:d:i:s:I:", long_options, nullptr);
213 if (c == -1)
214 break;
216 switch (c) {
217 case 'h':
218 case '?':
219 usage();
220 return 0;
222 case 'V':
223 show_version = true;
224 break;
226 case 'l':
228 const int loglevel = atoi(optarg);
229 if (loglevel < 0 || loglevel > 5) {
230 qDebug() << "ERROR: invalid log level spec.";
231 break;
233 context->set_log_level(sigrok::LogLevel::get(loglevel));
235 #ifdef ENABLE_DECODE
236 srd_log_loglevel_set(loglevel);
237 #endif
239 if (loglevel >= 5) {
240 const QSettings settings;
241 qDebug() << "Settings:" << settings.fileName()
242 << "format" << settings.format();
244 break;
247 case 'd':
248 driver = optarg;
249 break;
251 case 'D':
252 do_scan = false;
253 break;
255 case 'i':
256 open_files.emplace_back(optarg);
257 break;
259 case 's':
260 open_setup_file = optarg;
261 break;
263 case 'I':
264 open_file_format = optarg;
265 break;
267 case 'c':
268 restore_sessions = false;
269 break;
272 argc -= optind;
273 argv += optind;
275 for (int i = 0; i < argc; i++)
276 open_files.emplace_back(argv[i]);
278 qRegisterMetaType<uint64_t>("uint64_t");
279 qRegisterMetaType<pv::util::Timestamp>("util::Timestamp");
280 qRegisterMetaType<SharedPtrToSegment>("SharedPtrToSegment");
281 qRegisterMetaType<shared_ptr<pv::data::SignalBase>>("shared_ptr<SignalBase>");
283 // Prepare the global settings since logging needs them early on
284 pv::GlobalSettings settings;
285 settings.add_change_handler(&a); // Only the application object can't register itself
286 settings.save_internal_defaults();
287 settings.set_defaults_where_needed();
288 settings.apply_language();
289 settings.apply_theme();
291 pv::logging.init();
293 // Initialise libsigrok
294 context = sigrok::Context::create();
295 pv::Session::sr_context = context;
297 #if ENABLE_STACKTRACE
298 QString temp_path = QStandardPaths::standardLocations(
299 QStandardPaths::TempLocation).at(0);
300 stacktrace_filename = temp_path + "/pv_stacktrace.dmp";
301 qDebug() << "Stack trace file is" << stacktrace_filename;
303 ::signal(SIGSEGV, &signal_handler);
304 ::signal(SIGABRT, &signal_handler);
306 if (QFileInfo::exists(stacktrace_filename))
307 process_stacktrace(temp_path);
308 #endif
310 #ifdef ANDROID
311 context->set_resource_reader(&asset_reader);
312 #endif
313 do {
315 #ifdef ENABLE_DECODE
316 // Initialise libsigrokdecode
317 if (srd_init(nullptr) != SRD_OK) {
318 qDebug() << "ERROR: libsigrokdecode init failed.";
319 break;
322 // Load the protocol decoders
323 srd_decoder_load_all();
324 #endif
326 #ifndef ENABLE_STACKTRACE
327 try {
328 #endif
330 // Create the device manager, initialise the drivers
331 pv::DeviceManager device_manager(context, driver, do_scan);
333 a.collect_version_info(context);
334 if (show_version) {
335 a.print_version_info();
336 } else {
337 // Initialise the main window
338 pv::MainWindow w(device_manager);
339 w.show();
341 if (restore_sessions)
342 w.restore_sessions();
344 if (open_files.empty())
345 w.add_default_session();
346 else
347 for (string& open_file : open_files)
348 w.add_session_with_file(open_file, open_file_format, open_setup_file);
350 #ifdef ENABLE_SIGNALS
351 if (SignalHandler::prepare_signals()) {
352 SignalHandler *const handler = new SignalHandler(&w);
353 QObject::connect(handler, SIGNAL(int_received()), &w, SLOT(close()));
354 QObject::connect(handler, SIGNAL(term_received()), &w, SLOT(close()));
355 QObject::connect(handler, SIGNAL(usr1_received()), &w, SLOT(on_run_stop_clicked()));
356 } else
357 qWarning() << "Could not prepare signal handler.";
358 #endif
360 // Run the application
361 ret = a.exec();
364 #ifndef ENABLE_STACKTRACE
365 } catch (exception& e) {
366 qDebug() << "Exception:" << e.what();
368 #endif
370 #ifdef ENABLE_DECODE
371 // Destroy libsigrokdecode
372 srd_exit();
373 #endif
375 } while (false);
377 return ret;