]>
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 | ||
2acdb232 JH |
36 | #include "pv/application.hpp" |
37 | #include "pv/devicemanager.hpp" | |
38 | #include "pv/mainwindow.hpp" | |
9137928c | 39 | #ifdef ANDROID |
aff51746 | 40 | #include <libsigrokandroidutils/libsigrokandroidutils.h> |
dddff2e7 | 41 | #include "android/assetreader.hpp" |
2acdb232 | 42 | #include "android/loghandler.hpp" |
9137928c | 43 | #endif |
d7bed479 | 44 | |
075fb499 AG |
45 | #include "config.h" |
46 | ||
11a04e2a | 47 | #ifdef _WIN32 |
11a04e2a | 48 | #include <QtPlugin> |
09f55d96 UH |
49 | Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin) |
50 | Q_IMPORT_PLUGIN(QSvgPlugin) | |
11a04e2a UH |
51 | #endif |
52 | ||
6f925ba9 UH |
53 | using std::exception; |
54 | using std::shared_ptr; | |
55 | using std::string; | |
56 | ||
d4384c6d JH |
57 | void usage() |
58 | { | |
9fd5bb6e | 59 | fprintf(stdout, |
d4384c6d | 60 | "Usage:\n" |
e3c79b07 | 61 | " %s [OPTION…] — %s\n" |
d4384c6d JH |
62 | "\n" |
63 | "Help Options:\n" | |
d4384c6d | 64 | " -h, -?, --help Show help option\n" |
aa90e86b JH |
65 | "\n" |
66 | "Application Options:\n" | |
67 | " -V, --version Show release version\n" | |
68 | " -l, --loglevel Set libsigrok/libsigrokdecode loglevel\n" | |
e3c79b07 JH |
69 | " -i, --input-file Load input from file\n" |
70 | " -I, --input-format Input format\n" | |
d4384c6d JH |
71 | "\n", PV_BIN_NAME, PV_DESCRIPTION); |
72 | } | |
73 | ||
d7bed479 JH |
74 | int main(int argc, char *argv[]) |
75 | { | |
be03620e | 76 | int ret = 0; |
6f925ba9 UH |
77 | shared_ptr<sigrok::Context> context; |
78 | string open_file, open_file_format; | |
f2242929 | 79 | |
dac1bb97 | 80 | Application a(argc, argv); |
640e5565 | 81 | |
9137928c | 82 | #ifdef ANDROID |
aff51746 | 83 | srau_init_environment(); |
9137928c | 84 | pv::AndroidLogHandler::install_callbacks(); |
dddff2e7 | 85 | pv::AndroidAssetReader asset_reader; |
9137928c MC |
86 | #endif |
87 | ||
f7951df4 | 88 | // Parse arguments |
15a69e54 | 89 | while (true) { |
f7951df4 | 90 | static const struct option long_options[] = { |
f74015db UH |
91 | {"help", no_argument, nullptr, 'h'}, |
92 | {"version", no_argument, nullptr, 'V'}, | |
93 | {"loglevel", required_argument, nullptr, 'l'}, | |
94 | {"input-file", required_argument, nullptr, 'i'}, | |
95 | {"input-format", required_argument, nullptr, 'I'}, | |
96 | {nullptr, 0, nullptr, 0} | |
f7951df4 JH |
97 | }; |
98 | ||
815b3f26 | 99 | const int c = getopt_long(argc, argv, |
e3c79b07 | 100 | "l:Vh?i:I:", long_options, nullptr); |
f7951df4 JH |
101 | if (c == -1) |
102 | break; | |
103 | ||
104 | switch (c) { | |
aa90e86b JH |
105 | case 'h': |
106 | case '?': | |
107 | usage(); | |
108 | return 0; | |
109 | ||
110 | case 'V': | |
111 | // Print version info | |
112 | fprintf(stdout, "%s %s\n", PV_TITLE, PV_VERSION_STRING); | |
113 | return 0; | |
114 | ||
9b5099b6 JH |
115 | case 'l': |
116 | { | |
117 | const int loglevel = atoi(optarg); | |
e8d00928 | 118 | context->set_log_level(sigrok::LogLevel::get(loglevel)); |
269528f5 JH |
119 | |
120 | #ifdef ENABLE_DECODE | |
9b5099b6 | 121 | srd_log_loglevel_set(loglevel); |
269528f5 | 122 | #endif |
d52d8455 | 123 | |
0bd39c0c UH |
124 | if (loglevel >= 5) { |
125 | const QSettings settings; | |
126 | qDebug() << "Settings:" << settings.fileName() | |
127 | << "format" << settings.format(); | |
128 | } | |
9b5099b6 JH |
129 | break; |
130 | } | |
e3c79b07 JH |
131 | |
132 | case 'i': | |
133 | open_file = optarg; | |
134 | break; | |
135 | ||
136 | case 'I': | |
137 | open_file_format = optarg; | |
138 | break; | |
f7951df4 JH |
139 | } |
140 | } | |
141 | ||
3e5bc268 | 142 | if (argc - optind > 1) { |
39ccf9c3 | 143 | fprintf(stderr, "Only one file can be opened.\n"); |
1d478458 | 144 | return 1; |
e3c79b07 | 145 | } |
1d478458 | 146 | |
c063290a UH |
147 | if (argc - optind == 1) |
148 | open_file = argv[argc - 1]; | |
149 | ||
6a6772b7 | 150 | // Initialise libsigrok |
e8d00928 | 151 | context = sigrok::Context::create(); |
dddff2e7 DE |
152 | #ifdef ANDROID |
153 | context->set_resource_reader(&asset_reader); | |
154 | #endif | |
d52d8455 JH |
155 | do { |
156 | ||
269528f5 | 157 | #ifdef ENABLE_DECODE |
d52d8455 | 158 | // Initialise libsigrokdecode |
4c60462b | 159 | if (srd_init(nullptr) != SRD_OK) { |
d52d8455 JH |
160 | qDebug() << "ERROR: libsigrokdecode init failed."; |
161 | break; | |
162 | } | |
be03620e JH |
163 | |
164 | // Load the protocol decoders | |
165 | srd_decoder_load_all(); | |
269528f5 | 166 | #endif |
be03620e | 167 | |
107ca6d3 JH |
168 | try { |
169 | // Create the device manager, initialise the drivers | |
e8d00928 | 170 | pv::DeviceManager device_manager(context); |
664fca5c | 171 | |
be03620e | 172 | // Initialise the main window |
e3c79b07 JH |
173 | pv::MainWindow w(device_manager, |
174 | open_file, open_file_format); | |
be03620e | 175 | w.show(); |
664fca5c | 176 | |
140181a4 | 177 | #ifdef ENABLE_SIGNALS |
f3290553 | 178 | if (SignalHandler::prepare_signals()) { |
708605aa | 179 | SignalHandler *const handler = |
7a255aa9 | 180 | new SignalHandler(&w); |
708605aa JH |
181 | QObject::connect(handler, |
182 | SIGNAL(int_received()), | |
7a255aa9 | 183 | &w, SLOT(close())); |
708605aa JH |
184 | QObject::connect(handler, |
185 | SIGNAL(term_received()), | |
7a255aa9 | 186 | &w, SLOT(close())); |
360ab9be | 187 | } else { |
7a255aa9 JH |
188 | qWarning() << |
189 | "Could not prepare signal handler."; | |
190 | } | |
140181a4 | 191 | #endif |
7a255aa9 | 192 | |
be03620e JH |
193 | // Run the application |
194 | ret = a.exec(); | |
107ca6d3 | 195 | |
6f925ba9 | 196 | } catch (exception e) { |
107ca6d3 | 197 | qDebug() << e.what(); |
6fb67b27 | 198 | } |
6fb67b27 | 199 | |
269528f5 | 200 | #ifdef ENABLE_DECODE |
d52d8455 | 201 | // Destroy libsigrokdecode |
be03620e | 202 | srd_exit(); |
269528f5 | 203 | #endif |
664fca5c | 204 | |
15a69e54 | 205 | } while (false); |
664fca5c | 206 | |
664fca5c | 207 | return ret; |
d7bed479 | 208 | } |