]> sigrok.org Git - pulseview.git/blame - main.cpp
Fix item dragging
[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
140181a4 39#ifdef ENABLE_SIGNALS
2acdb232 40#include "signalhandler.hpp"
140181a4
JH
41#endif
42
c1a688de
SA
43#ifdef ENABLE_STACKTRACE
44#include <signal.h>
45#include <boost/stacktrace.hpp>
46#include <QStandardPaths>
47#endif
48
2acdb232
JH
49#include "pv/application.hpp"
50#include "pv/devicemanager.hpp"
bcb4c327
SA
51#include "pv/globalsettings.hpp"
52#include "pv/logging.hpp"
2acdb232 53#include "pv/mainwindow.hpp"
419ec4e1 54#include "pv/session.hpp"
c1a688de 55
9137928c 56#ifdef ANDROID
aff51746 57#include <libsigrokandroidutils/libsigrokandroidutils.h>
dddff2e7 58#include "android/assetreader.hpp"
2acdb232 59#include "android/loghandler.hpp"
9137928c 60#endif
d7bed479 61
075fb499
AG
62#include "config.h"
63
11a04e2a 64#ifdef _WIN32
11a04e2a 65#include <QtPlugin>
09f55d96
UH
66Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
67Q_IMPORT_PLUGIN(QSvgPlugin)
11a04e2a
UH
68#endif
69
6f925ba9 70using std::exception;
c5d6200c
SA
71using std::ifstream;
72using std::ofstream;
6f925ba9
UH
73using std::shared_ptr;
74using std::string;
75
c1a688de
SA
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}
c5d6200c
SA
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}
c1a688de
SA
139#endif
140
d4384c6d
JH
141void usage()
142{
9fd5bb6e 143 fprintf(stdout,
d4384c6d 144 "Usage:\n"
41e2ade4 145 " %s [OPTIONS] [FILE]\n"
d4384c6d
JH
146 "\n"
147 "Help Options:\n"
d4384c6d 148 " -h, -?, --help Show help option\n"
aa90e86b
JH
149 "\n"
150 "Application Options:\n"
151 " -V, --version Show release version\n"
152 " -l, --loglevel Set libsigrok/libsigrokdecode loglevel\n"
58d8e4c6 153 " -d, --driver Specify the device driver to use\n"
1d6e35dc 154 " -D, --no-scan Don't auto-scan for devices, use -d spec only\n"
e3c79b07
JH
155 " -i, --input-file Load input from file\n"
156 " -I, --input-format Input format\n"
156f06e9 157 " -c, --clean Don't restore previous sessions on startup\n"
c062efdc 158 " -s, --log-to-stdout Don't use logging, output to stdout instead\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;
c062efdc 170 bool do_logging = true;
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'},
d0f22e70 187 {"no-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,
c062efdc 196 "h?VDcsl: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':
207 // Print version info
208 fprintf(stdout, "%s %s\n", PV_TITLE, PV_VERSION_STRING);
209 return 0;
210
9b5099b6
JH
211 case 'l':
212 {
213 const int loglevel = atoi(optarg);
ed6f8680
GS
214 if (loglevel < 0 || loglevel > 5) {
215 qDebug() << "ERROR: invalid log level spec.";
216 break;
217 }
e8d00928 218 context->set_log_level(sigrok::LogLevel::get(loglevel));
269528f5
JH
219
220#ifdef ENABLE_DECODE
9b5099b6 221 srd_log_loglevel_set(loglevel);
269528f5 222#endif
d52d8455 223
0bd39c0c
UH
224 if (loglevel >= 5) {
225 const QSettings settings;
226 qDebug() << "Settings:" << settings.fileName()
227 << "format" << settings.format();
228 }
9b5099b6
JH
229 break;
230 }
e3c79b07 231
58d8e4c6
GS
232 case 'd':
233 driver = optarg;
234 break;
235
1d6e35dc
GS
236 case 'D':
237 do_scan = false;
238 break;
239
e3c79b07 240 case 'i':
caa2b3e9 241 open_files.push_back(optarg);
e3c79b07
JH
242 break;
243
244 case 'I':
245 open_file_format = optarg;
246 break;
156f06e9
SA
247
248 case 'c':
249 restore_sessions = false;
250 break;
c062efdc
SA
251
252 case 's':
253 do_logging = false;
254 break;
f7951df4
JH
255 }
256 }
6a96e1b2
GS
257 argc -= optind;
258 argv += optind;
f7951df4 259
6a96e1b2
GS
260 for (int i = 0; i < argc; i++)
261 open_files.push_back(argv[i]);
1d478458 262
bcb4c327
SA
263 // Prepare the global settings since logging needs them early on
264 pv::GlobalSettings settings;
265 settings.set_defaults_where_needed();
266
c062efdc
SA
267 if (do_logging)
268 pv::logging.init();
bcb4c327 269
6a6772b7 270 // Initialise libsigrok
e8d00928 271 context = sigrok::Context::create();
419ec4e1
SA
272 pv::Session::sr_context = context;
273
c1a688de
SA
274#if ENABLE_STACKTRACE
275 QString temp_path = QStandardPaths::standardLocations(
276 QStandardPaths::TempLocation).at(0);
277 stacktrace_filename = temp_path + "/pv_stacktrace.dmp";
278 qDebug() << "Stack trace file is" << stacktrace_filename;
279
280 ::signal(SIGSEGV, &signal_handler);
281 ::signal(SIGABRT, &signal_handler);
c5d6200c
SA
282
283 if (QFileInfo::exists(stacktrace_filename))
284 process_stacktrace(temp_path);
c1a688de
SA
285#endif
286
dddff2e7
DE
287#ifdef ANDROID
288 context->set_resource_reader(&asset_reader);
289#endif
d52d8455
JH
290 do {
291
269528f5 292#ifdef ENABLE_DECODE
d52d8455 293 // Initialise libsigrokdecode
4c60462b 294 if (srd_init(nullptr) != SRD_OK) {
d52d8455
JH
295 qDebug() << "ERROR: libsigrokdecode init failed.";
296 break;
297 }
be03620e
JH
298
299 // Load the protocol decoders
300 srd_decoder_load_all();
269528f5 301#endif
be03620e 302
c1a688de 303#ifndef ENABLE_STACKTRACE
107ca6d3 304 try {
c1a688de 305#endif
664fca5c 306
c1a688de
SA
307 // Create the device manager, initialise the drivers
308 pv::DeviceManager device_manager(context, driver, do_scan);
664fca5c 309
c1a688de
SA
310 // Initialise the main window
311 pv::MainWindow w(device_manager);
312 w.show();
3ed18835 313
c1a688de
SA
314 if (restore_sessions)
315 w.restore_sessions();
316
caa2b3e9 317 if (open_files.empty())
c1a688de 318 w.add_default_session();
caa2b3e9
SA
319 else
320 for (string open_file : open_files)
321 w.add_session_with_file(open_file, open_file_format);
3ed18835 322
140181a4 323#ifdef ENABLE_SIGNALS
c1a688de
SA
324 if (SignalHandler::prepare_signals()) {
325 SignalHandler *const handler = new SignalHandler(&w);
326 QObject::connect(handler, SIGNAL(int_received()),
327 &w, SLOT(close()));
328 QObject::connect(handler, SIGNAL(term_received()),
329 &w, SLOT(close()));
330 } else
331 qWarning() << "Could not prepare signal handler.";
140181a4 332#endif
7a255aa9 333
c1a688de
SA
334 // Run the application
335 ret = a.exec();
107ca6d3 336
c1a688de 337#ifndef ENABLE_STACKTRACE
30677c13 338 } catch (exception& e) {
c1a688de 339 qDebug() << "Exception:" << e.what();
6fb67b27 340 }
c1a688de 341#endif
6fb67b27 342
269528f5 343#ifdef ENABLE_DECODE
d52d8455 344 // Destroy libsigrokdecode
be03620e 345 srd_exit();
269528f5 346#endif
664fca5c 347
15a69e54 348 } while (false);
664fca5c 349
664fca5c 350 return ret;
d7bed479 351}