]> sigrok.org Git - pulseview.git/blob - main.cpp
Support specifying input formats on the command line
[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, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20
21 #ifdef ENABLE_DECODE
22 #include <libsigrokdecode/libsigrokdecode.h> /* First, so we avoid a _POSIX_C_SOURCE warning. */
23 #endif
24
25 #include <stdint.h>
26 #include <libsigrokcxx/libsigrokcxx.hpp>
27
28 #include <getopt.h>
29
30 #include <QDebug>
31
32 #ifdef ENABLE_SIGNALS
33 #include "signalhandler.hpp"
34 #endif
35
36 #include "pv/application.hpp"
37 #include "pv/devicemanager.hpp"
38 #include "pv/mainwindow.hpp"
39 #ifdef ANDROID
40 #include <libsigrokandroidutils/libsigrokandroidutils.h>
41 #include "android/loghandler.hpp"
42 #endif
43
44 #include "config.h"
45
46 #ifdef _WIN32
47 // The static qsvg lib is required for SVG graphics/icons (on Windows).
48 #include <QtPlugin>
49 Q_IMPORT_PLUGIN(qsvg)
50 #endif
51
52 void usage()
53 {
54         fprintf(stdout,
55                 "Usage:\n"
56                 "  %s [OPTION…] — %s\n"
57                 "\n"
58                 "Help Options:\n"
59                 "  -h, -?, --help                  Show help option\n"
60                 "\n"
61                 "Application Options:\n"
62                 "  -V, --version                   Show release version\n"
63                 "  -l, --loglevel                  Set libsigrok/libsigrokdecode loglevel\n"
64                 "  -i, --input-file                Load input from file\n"
65                 "  -I, --input-format              Input format\n"
66                 "\n", PV_BIN_NAME, PV_DESCRIPTION);
67 }
68
69 int main(int argc, char *argv[])
70 {
71         int ret = 0;
72         std::shared_ptr<sigrok::Context> context;
73         std::string open_file, open_file_format;
74
75         Application a(argc, argv);
76
77 #ifdef ANDROID
78         srau_init_environment();
79         pv::AndroidLogHandler::install_callbacks();
80 #endif
81
82         // Parse arguments
83         while (1) {
84                 static const struct option long_options[] = {
85                         {"help", no_argument, 0, 'h'},
86                         {"version", no_argument, 0, 'V'},
87                         {"loglevel", required_argument, 0, 'l'},
88                         {"input-file", required_argument, 0, 'i'},
89                         {"input-format", required_argument, 0, 'I'},
90                         {0, 0, 0, 0}
91                 };
92
93                 const int c = getopt_long(argc, argv,
94                         "l:Vh?i:I:", long_options, nullptr);
95                 if (c == -1)
96                         break;
97
98                 switch (c) {
99                 case 'h':
100                 case '?':
101                         usage();
102                         return 0;
103
104                 case 'V':
105                         // Print version info
106                         fprintf(stdout, "%s %s\n", PV_TITLE, PV_VERSION_STRING);
107                         return 0;
108
109                 case 'l':
110                 {
111                         const int loglevel = atoi(optarg);
112                         context->set_log_level(sigrok::LogLevel::get(loglevel));
113
114 #ifdef ENABLE_DECODE
115                         srd_log_loglevel_set(loglevel);
116 #endif
117
118                         break;
119                 }
120
121                 case 'i':
122                         open_file = optarg;
123                         break;
124
125                 case 'I':
126                         open_file_format = optarg;
127                         break;
128                 }
129         }
130
131         if (argc != optind) {
132                 fprintf(stderr, "Unexpected argument: %s\n", argv[optind]);
133                 return 1;
134         }
135
136         // Initialise libsigrok
137         context = sigrok::Context::create();
138
139         do {
140
141 #ifdef ENABLE_DECODE
142                 // Initialise libsigrokdecode
143                 if (srd_init(nullptr) != SRD_OK) {
144                         qDebug() << "ERROR: libsigrokdecode init failed.";
145                         break;
146                 }
147
148                 // Load the protocol decoders
149                 srd_decoder_load_all();
150 #endif
151
152                 try {
153                         // Create the device manager, initialise the drivers
154                         pv::DeviceManager device_manager(context);
155
156                         // Initialise the main window
157                         pv::MainWindow w(device_manager,
158                                 open_file, open_file_format);
159                         w.show();
160
161 #ifdef ENABLE_SIGNALS
162                         if(SignalHandler::prepare_signals()) {
163                                 SignalHandler *const handler =
164                                         new SignalHandler(&w);
165                                 QObject::connect(handler,
166                                         SIGNAL(int_received()),
167                                         &w, SLOT(close()));
168                                 QObject::connect(handler,
169                                         SIGNAL(term_received()),
170                                         &w, SLOT(close()));
171                         } else {
172                                 qWarning() <<
173                                         "Could not prepare signal handler.";
174                         }
175 #endif
176
177                         // Run the application
178                         ret = a.exec();
179
180                 } catch(std::exception e) {
181                         qDebug() << e.what();
182                 }
183
184 #ifdef ENABLE_DECODE
185                 // Destroy libsigrokdecode
186                 srd_exit();
187 #endif
188
189         } while (0);
190
191         return ret;
192 }