Themes: Fix icon shift bug when opening QToolButton menu
[pulseview.git] / main.cpp
blob12d3bb6e30748ca9278e39477a1c7b2042c18848
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 #include <libsigrokcxx/libsigrokcxx.hpp>
31 #include <QCheckBox>
32 #include <QDebug>
33 #include <QFile>
34 #include <QFileInfo>
35 #include <QMessageBox>
36 #include <QSettings>
37 #include <QTextStream>
39 #include "config.h"
41 #ifdef ENABLE_SIGNALS
42 #include "signalhandler.hpp"
43 #endif
45 #ifdef ENABLE_STACKTRACE
46 #include <signal.h>
47 #include <boost/stacktrace.hpp>
48 #include <QStandardPaths>
49 #endif
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"
58 #ifdef ANDROID
59 #include <libsigrokandroidutils/libsigrokandroidutils.h>
60 #include "android/assetreader.hpp"
61 #include "android/loghandler.hpp"
62 #endif
64 #ifdef _WIN32
65 #include <QtPlugin>
66 Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
67 Q_IMPORT_PLUGIN(QSvgPlugin)
68 #endif
70 using std::exception;
71 using std::ifstream;
72 using std::ofstream;
73 using std::shared_ptr;
74 using std::string;
76 #if ENABLE_STACKTRACE
77 QString stacktrace_filename;
79 void signal_handler(int signum)
81 ::signal(signum, SIG_DFL);
82 boost::stacktrace::safe_dump_to(stacktrace_filename.toLocal8Bit().data());
83 ::raise(SIGABRT);
86 void process_stacktrace(QString temp_path)
88 const QString stacktrace_outfile = temp_path + "/pv_stacktrace.txt";
90 ifstream ifs(stacktrace_filename.toLocal8Bit().data());
91 ofstream ofs(stacktrace_outfile.toLocal8Bit().data(),
92 ofstream::out | ofstream::trunc);
94 boost::stacktrace::stacktrace st =
95 boost::stacktrace::stacktrace::from_dump(ifs);
96 ofs << st;
98 ofs.close();
99 ifs.close();
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 />");
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() << "---------------------------------------------------------";
113 f.close();
115 // Remove stack trace so we don't process it again the next time we run
116 QFile::remove(stacktrace_filename.toLocal8Bit().data());
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"));
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);
131 QObject::connect(cb, &QCheckBox::stateChanged, [](int state){
132 pv::GlobalSettings settings;
133 settings.setValue(pv::GlobalSettings::Key_Log_NotifyOfStacktrace,
134 !state); });
136 msgbox.exec();
139 #endif
141 void usage()
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, --dont-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);
161 int main(int argc, char *argv[])
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 bool show_version = false;
171 Application a(argc, argv);
173 #ifdef ANDROID
174 srau_init_environment();
175 pv::AndroidLogHandler::install_callbacks();
176 pv::AndroidAssetReader asset_reader;
177 #endif
179 // Parse arguments
180 while (true) {
181 static const struct option long_options[] = {
182 {"help", no_argument, nullptr, 'h'},
183 {"version", no_argument, nullptr, 'V'},
184 {"loglevel", required_argument, nullptr, 'l'},
185 {"driver", required_argument, nullptr, 'd'},
186 {"dont-scan", no_argument, nullptr, 'D'},
187 {"input-file", required_argument, nullptr, 'i'},
188 {"input-format", required_argument, nullptr, 'I'},
189 {"clean", no_argument, nullptr, 'c'},
190 {"log-to-stdout", no_argument, nullptr, 's'},
191 {nullptr, 0, nullptr, 0}
194 const int c = getopt_long(argc, argv,
195 "h?VDcl:d:i:I:", long_options, nullptr);
196 if (c == -1)
197 break;
199 switch (c) {
200 case 'h':
201 case '?':
202 usage();
203 return 0;
205 case 'V':
206 show_version = true;
207 break;
209 case 'l':
211 const int loglevel = atoi(optarg);
212 if (loglevel < 0 || loglevel > 5) {
213 qDebug() << "ERROR: invalid log level spec.";
214 break;
216 context->set_log_level(sigrok::LogLevel::get(loglevel));
218 #ifdef ENABLE_DECODE
219 srd_log_loglevel_set(loglevel);
220 #endif
222 if (loglevel >= 5) {
223 const QSettings settings;
224 qDebug() << "Settings:" << settings.fileName()
225 << "format" << settings.format();
227 break;
230 case 'd':
231 driver = optarg;
232 break;
234 case 'D':
235 do_scan = false;
236 break;
238 case 'i':
239 open_files.emplace_back(optarg);
240 break;
242 case 'I':
243 open_file_format = optarg;
244 break;
246 case 'c':
247 restore_sessions = false;
248 break;
251 argc -= optind;
252 argv += optind;
254 for (int i = 0; i < argc; i++)
255 open_files.emplace_back(argv[i]);
257 // Prepare the global settings since logging needs them early on
258 pv::GlobalSettings settings;
259 settings.save_internal_defaults();
260 settings.set_defaults_where_needed();
261 settings.apply_theme();
263 pv::logging.init();
265 // Initialise libsigrok
266 context = sigrok::Context::create();
267 pv::Session::sr_context = context;
269 #if ENABLE_STACKTRACE
270 QString temp_path = QStandardPaths::standardLocations(
271 QStandardPaths::TempLocation).at(0);
272 stacktrace_filename = temp_path + "/pv_stacktrace.dmp";
273 qDebug() << "Stack trace file is" << stacktrace_filename;
275 ::signal(SIGSEGV, &signal_handler);
276 ::signal(SIGABRT, &signal_handler);
278 if (QFileInfo::exists(stacktrace_filename))
279 process_stacktrace(temp_path);
280 #endif
282 #ifdef ANDROID
283 context->set_resource_reader(&asset_reader);
284 #endif
285 do {
287 #ifdef ENABLE_DECODE
288 // Initialise libsigrokdecode
289 if (srd_init(nullptr) != SRD_OK) {
290 qDebug() << "ERROR: libsigrokdecode init failed.";
291 break;
294 // Load the protocol decoders
295 srd_decoder_load_all();
296 #endif
298 #ifndef ENABLE_STACKTRACE
299 try {
300 #endif
302 // Create the device manager, initialise the drivers
303 pv::DeviceManager device_manager(context, driver, do_scan);
305 a.collect_version_info(context);
306 if (show_version) {
307 a.print_version_info();
308 } else {
309 // Initialise the main window
310 pv::MainWindow w(device_manager);
311 w.show();
313 if (restore_sessions)
314 w.restore_sessions();
316 if (open_files.empty())
317 w.add_default_session();
318 else
319 for (string& open_file : open_files)
320 w.add_session_with_file(open_file, open_file_format);
322 #ifdef ENABLE_SIGNALS
323 if (SignalHandler::prepare_signals()) {
324 SignalHandler *const handler = new SignalHandler(&w);
325 QObject::connect(handler, SIGNAL(int_received()),
326 &w, SLOT(close()));
327 QObject::connect(handler, SIGNAL(term_received()),
328 &w, SLOT(close()));
329 } else
330 qWarning() << "Could not prepare signal handler.";
331 #endif
333 // Run the application
334 ret = a.exec();
337 #ifndef ENABLE_STACKTRACE
338 } catch (exception& e) {
339 qDebug() << "Exception:" << e.what();
341 #endif
343 #ifdef ENABLE_DECODE
344 // Destroy libsigrokdecode
345 srd_exit();
346 #endif
348 } while (false);
350 return ret;