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/>.
21 #include <libsigrokdecode/libsigrokdecode.h> /* First, so we avoid a _POSIX_C_SOURCE warning. */
25 #include <libsigrokcxx/libsigrokcxx.hpp>
33 #include "signalhandler.hpp"
36 #ifdef ENABLE_STACKTRACE
38 #include <boost/stacktrace.hpp>
39 #include <QStandardPaths>
42 #include "pv/application.hpp"
43 #include "pv/devicemanager.hpp"
44 #include "pv/mainwindow.hpp"
45 #include "pv/session.hpp"
48 #include <libsigrokandroidutils/libsigrokandroidutils.h>
49 #include "android/assetreader.hpp"
50 #include "android/loghandler.hpp"
57 Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
58 Q_IMPORT_PLUGIN(QSvgPlugin)
62 using std::shared_ptr;
66 QString stacktrace_filename;
68 void signal_handler(int signum)
70 ::signal(signum, SIG_DFL);
71 boost::stacktrace::safe_dump_to(stacktrace_filename.toLocal8Bit().data());
80 " %s [OPTIONS] [FILE]\n"
83 " -h, -?, --help Show help option\n"
85 "Application Options:\n"
86 " -V, --version Show release version\n"
87 " -l, --loglevel Set libsigrok/libsigrokdecode loglevel\n"
88 " -d, --driver Specify the device driver to use\n"
89 " -D, --no-scan Don't auto-scan for devices, use -d spec only\n"
90 " -i, --input-file Load input from file\n"
91 " -I, --input-format Input format\n"
92 " -c, --clean Don't restore previous sessions on startup\n"
96 int main(int argc, char *argv[])
99 shared_ptr<sigrok::Context> context;
100 string open_file, open_file_format, driver;
101 bool restore_sessions = true;
104 Application a(argc, argv);
107 srau_init_environment();
108 pv::AndroidLogHandler::install_callbacks();
109 pv::AndroidAssetReader asset_reader;
114 static const struct option long_options[] = {
115 {"help", no_argument, nullptr, 'h'},
116 {"version", no_argument, nullptr, 'V'},
117 {"loglevel", required_argument, nullptr, 'l'},
118 {"driver", required_argument, nullptr, 'd'},
119 {"input-file", required_argument, nullptr, 'i'},
120 {"input-format", required_argument, nullptr, 'I'},
121 {"clean", no_argument, nullptr, 'c'},
122 {nullptr, 0, nullptr, 0}
125 const int c = getopt_long(argc, argv,
126 "l:Vhc?d:Di:I:", long_options, nullptr);
137 // Print version info
138 fprintf(stdout, "%s %s\n", PV_TITLE, PV_VERSION_STRING);
143 const int loglevel = atoi(optarg);
144 if (loglevel < 0 || loglevel > 5) {
145 qDebug() << "ERROR: invalid log level spec.";
148 context->set_log_level(sigrok::LogLevel::get(loglevel));
151 srd_log_loglevel_set(loglevel);
155 const QSettings settings;
156 qDebug() << "Settings:" << settings.fileName()
157 << "format" << settings.format();
175 open_file_format = optarg;
179 restore_sessions = false;
184 if (argc - optind > 1) {
185 fprintf(stderr, "Only one file can be opened.\n");
189 if (argc - optind == 1)
190 open_file = argv[argc - 1];
192 // Initialise libsigrok
193 context = sigrok::Context::create();
194 pv::Session::sr_context = context;
196 #if ENABLE_STACKTRACE
197 QString temp_path = QStandardPaths::standardLocations(
198 QStandardPaths::TempLocation).at(0);
199 stacktrace_filename = temp_path + "/pv_stacktrace.dmp";
200 qDebug() << "Stack trace file is" << stacktrace_filename;
202 ::signal(SIGSEGV, &signal_handler);
203 ::signal(SIGABRT, &signal_handler);
207 context->set_resource_reader(&asset_reader);
212 // Initialise libsigrokdecode
213 if (srd_init(nullptr) != SRD_OK) {
214 qDebug() << "ERROR: libsigrokdecode init failed.";
218 // Load the protocol decoders
219 srd_decoder_load_all();
222 #ifndef ENABLE_STACKTRACE
226 // Create the device manager, initialise the drivers
227 pv::DeviceManager device_manager(context, driver, do_scan);
229 // Initialise the main window
230 pv::MainWindow w(device_manager);
233 if (restore_sessions)
234 w.restore_sessions();
236 if (!open_file.empty())
237 w.add_session_with_file(open_file, open_file_format);
239 w.add_default_session();
241 #ifdef ENABLE_SIGNALS
242 if (SignalHandler::prepare_signals()) {
243 SignalHandler *const handler = new SignalHandler(&w);
244 QObject::connect(handler, SIGNAL(int_received()),
246 QObject::connect(handler, SIGNAL(term_received()),
249 qWarning() << "Could not prepare signal handler.";
252 // Run the application
255 #ifndef ENABLE_STACKTRACE
256 } catch (exception& e) {
257 qDebug() << "Exception:" << e.what();
262 // Destroy libsigrokdecode