]> sigrok.org Git - pulseview.git/blob - main.cpp
feff17aaf47cd54299ff114871e32247aceedce1
[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 <libsigrokcxx/libsigrokcxx.hpp>
26
27 #include <getopt.h>
28
29 #include <QDebug>
30 #include <QSettings>
31
32 #ifdef ENABLE_SIGNALS
33 #include "signalhandler.hpp"
34 #endif
35
36 #ifdef ENABLE_STACKTRACE
37 #include <signal.h>
38 #include <boost/stacktrace.hpp>
39 #include <QStandardPaths>
40 #endif
41
42 #include "pv/application.hpp"
43 #include "pv/devicemanager.hpp"
44 #include "pv/globalsettings.hpp"
45 #include "pv/logging.hpp"
46 #include "pv/mainwindow.hpp"
47 #include "pv/session.hpp"
48
49 #ifdef ANDROID
50 #include <libsigrokandroidutils/libsigrokandroidutils.h>
51 #include "android/assetreader.hpp"
52 #include "android/loghandler.hpp"
53 #endif
54
55 #include "config.h"
56
57 #ifdef _WIN32
58 #include <QtPlugin>
59 Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
60 Q_IMPORT_PLUGIN(QSvgPlugin)
61 #endif
62
63 using std::exception;
64 using std::shared_ptr;
65 using std::string;
66
67 #if ENABLE_STACKTRACE
68 QString stacktrace_filename;
69
70 void signal_handler(int signum)
71 {
72         ::signal(signum, SIG_DFL);
73         boost::stacktrace::safe_dump_to(stacktrace_filename.toLocal8Bit().data());
74         ::raise(SIGABRT);
75 }
76 #endif
77
78 void usage()
79 {
80         fprintf(stdout,
81                 "Usage:\n"
82                 "  %s [OPTIONS] [FILE]\n"
83                 "\n"
84                 "Help Options:\n"
85                 "  -h, -?, --help                  Show help option\n"
86                 "\n"
87                 "Application Options:\n"
88                 "  -V, --version                   Show release version\n"
89                 "  -l, --loglevel                  Set libsigrok/libsigrokdecode loglevel\n"
90                 "  -d, --driver                    Specify the device driver to use\n"
91                 "  -D, --no-scan                   Don't auto-scan for devices, use -d spec only\n"
92                 "  -i, --input-file                Load input from file\n"
93                 "  -I, --input-format              Input format\n"
94                 "  -c, --clean                     Don't restore previous sessions on startup\n"
95                 "\n", PV_BIN_NAME);
96 }
97
98 int main(int argc, char *argv[])
99 {
100         int ret = 0;
101         shared_ptr<sigrok::Context> context;
102         string open_file, open_file_format, driver;
103         bool restore_sessions = true;
104         bool do_scan = true;
105
106         Application a(argc, argv);
107
108 #ifdef ANDROID
109         srau_init_environment();
110         pv::AndroidLogHandler::install_callbacks();
111         pv::AndroidAssetReader asset_reader;
112 #endif
113
114         // Parse arguments
115         while (true) {
116                 static const struct option long_options[] = {
117                         {"help", no_argument, nullptr, 'h'},
118                         {"version", no_argument, nullptr, 'V'},
119                         {"loglevel", required_argument, nullptr, 'l'},
120                         {"driver", required_argument, nullptr, 'd'},
121                         {"input-file", required_argument, nullptr, 'i'},
122                         {"input-format", required_argument, nullptr, 'I'},
123                         {"clean", no_argument, nullptr, 'c'},
124                         {nullptr, 0, nullptr, 0}
125                 };
126
127                 const int c = getopt_long(argc, argv,
128                         "l:Vhc?d:Di:I:", long_options, nullptr);
129                 if (c == -1)
130                         break;
131
132                 switch (c) {
133                 case 'h':
134                 case '?':
135                         usage();
136                         return 0;
137
138                 case 'V':
139                         // Print version info
140                         fprintf(stdout, "%s %s\n", PV_TITLE, PV_VERSION_STRING);
141                         return 0;
142
143                 case 'l':
144                 {
145                         const int loglevel = atoi(optarg);
146                         if (loglevel < 0 || loglevel > 5) {
147                                 qDebug() << "ERROR: invalid log level spec.";
148                                 break;
149                         }
150                         context->set_log_level(sigrok::LogLevel::get(loglevel));
151
152 #ifdef ENABLE_DECODE
153                         srd_log_loglevel_set(loglevel);
154 #endif
155
156                         if (loglevel >= 5) {
157                                 const QSettings settings;
158                                 qDebug() << "Settings:" << settings.fileName()
159                                         << "format" << settings.format();
160                         }
161                         break;
162                 }
163
164                 case 'd':
165                         driver = optarg;
166                         break;
167
168                 case 'D':
169                         do_scan = false;
170                         break;
171
172                 case 'i':
173                         open_file = optarg;
174                         break;
175
176                 case 'I':
177                         open_file_format = optarg;
178                         break;
179
180                 case 'c':
181                         restore_sessions = false;
182                         break;
183                 }
184         }
185
186         if (argc - optind > 1) {
187                 fprintf(stderr, "Only one file can be opened.\n");
188                 return 1;
189         }
190
191         if (argc - optind == 1)
192                 open_file = argv[argc - 1];
193
194         // Prepare the global settings since logging needs them early on
195         pv::GlobalSettings settings;
196         settings.set_defaults_where_needed();
197
198         pv::logging.init();
199
200         // Initialise libsigrok
201         context = sigrok::Context::create();
202         pv::Session::sr_context = context;
203
204 #if ENABLE_STACKTRACE
205         QString temp_path = QStandardPaths::standardLocations(
206                 QStandardPaths::TempLocation).at(0);
207         stacktrace_filename = temp_path + "/pv_stacktrace.dmp";
208         qDebug() << "Stack trace file is" << stacktrace_filename;
209
210         ::signal(SIGSEGV, &signal_handler);
211         ::signal(SIGABRT, &signal_handler);
212 #endif
213
214 #ifdef ANDROID
215         context->set_resource_reader(&asset_reader);
216 #endif
217         do {
218
219 #ifdef ENABLE_DECODE
220                 // Initialise libsigrokdecode
221                 if (srd_init(nullptr) != SRD_OK) {
222                         qDebug() << "ERROR: libsigrokdecode init failed.";
223                         break;
224                 }
225
226                 // Load the protocol decoders
227                 srd_decoder_load_all();
228 #endif
229
230 #ifndef ENABLE_STACKTRACE
231                 try {
232 #endif
233
234                 // Create the device manager, initialise the drivers
235                 pv::DeviceManager device_manager(context, driver, do_scan);
236
237                 // Initialise the main window
238                 pv::MainWindow w(device_manager);
239                 w.show();
240
241                 if (restore_sessions)
242                         w.restore_sessions();
243
244                 if (!open_file.empty())
245                         w.add_session_with_file(open_file, open_file_format);
246                 else
247                         w.add_default_session();
248
249 #ifdef ENABLE_SIGNALS
250                 if (SignalHandler::prepare_signals()) {
251                         SignalHandler *const handler = new SignalHandler(&w);
252                         QObject::connect(handler, SIGNAL(int_received()),
253                                 &w, SLOT(close()));
254                         QObject::connect(handler, SIGNAL(term_received()),
255                                 &w, SLOT(close()));
256                 } else
257                         qWarning() << "Could not prepare signal handler.";
258 #endif
259
260                 // Run the application
261                 ret = a.exec();
262
263 #ifndef ENABLE_STACKTRACE
264                 } catch (exception& e) {
265                         qDebug() << "Exception:" << e.what();
266                 }
267 #endif
268
269 #ifdef ENABLE_DECODE
270                 // Destroy libsigrokdecode
271                 srd_exit();
272 #endif
273
274         } while (false);
275
276         return ret;
277 }