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