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