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