View: Provide a clear setup path for the initial v_offset value
[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/assetreader.hpp"
42 #include "android/loghandler.hpp"
43 #endif
44
45 #include "config.h"
46
47 #ifdef _WIN32
48 // The static qsvg lib is required for SVG graphics/icons (on Windows).
49 #include <QtPlugin>
50 Q_IMPORT_PLUGIN(qsvg)
51 #endif
52
53 void usage()
54 {
55         fprintf(stdout,
56                 "Usage:\n"
57                 "  %s [OPTION…] — %s\n"
58                 "\n"
59                 "Help Options:\n"
60                 "  -h, -?, --help                  Show help option\n"
61                 "\n"
62                 "Application Options:\n"
63                 "  -V, --version                   Show release version\n"
64                 "  -l, --loglevel                  Set libsigrok/libsigrokdecode loglevel\n"
65                 "  -i, --input-file                Load input from file\n"
66                 "  -I, --input-format              Input format\n"
67                 "\n", PV_BIN_NAME, PV_DESCRIPTION);
68 }
69
70 int main(int argc, char *argv[])
71 {
72         int ret = 0;
73         std::shared_ptr<sigrok::Context> context;
74         std::string open_file, open_file_format;
75
76         Application a(argc, argv);
77
78 #ifdef ANDROID
79         srau_init_environment();
80         pv::AndroidLogHandler::install_callbacks();
81         pv::AndroidAssetReader asset_reader;
82 #endif
83
84         // Parse arguments
85         while (1) {
86                 static const struct option long_options[] = {
87                         {"help", no_argument, nullptr, 'h'},
88                         {"version", no_argument, nullptr, 'V'},
89                         {"loglevel", required_argument, nullptr, 'l'},
90                         {"input-file", required_argument, nullptr, 'i'},
91                         {"input-format", required_argument, nullptr, 'I'},
92                         {nullptr, 0, nullptr, 0}
93                 };
94
95                 const int c = getopt_long(argc, argv,
96                         "l:Vh?i:I:", long_options, nullptr);
97                 if (c == -1)
98                         break;
99
100                 switch (c) {
101                 case 'h':
102                 case '?':
103                         usage();
104                         return 0;
105
106                 case 'V':
107                         // Print version info
108                         fprintf(stdout, "%s %s\n", PV_TITLE, PV_VERSION_STRING);
109                         return 0;
110
111                 case 'l':
112                 {
113                         const int loglevel = atoi(optarg);
114                         context->set_log_level(sigrok::LogLevel::get(loglevel));
115
116 #ifdef ENABLE_DECODE
117                         srd_log_loglevel_set(loglevel);
118 #endif
119
120                         break;
121                 }
122
123                 case 'i':
124                         open_file = optarg;
125                         break;
126
127                 case 'I':
128                         open_file_format = optarg;
129                         break;
130                 }
131         }
132
133         if (argc - optind > 1) {
134                 fprintf(stderr, "Only one file can be opened.\n");
135                 return 1;
136         } else if (argc - optind == 1) {
137                 open_file = argv[argc - 1];
138         }
139
140         // Initialise libsigrok
141         context = sigrok::Context::create();
142 #ifdef ANDROID
143         context->set_resource_reader(&asset_reader);
144 #endif
145         do {
146
147 #ifdef ENABLE_DECODE
148                 // Initialise libsigrokdecode
149                 if (srd_init(nullptr) != SRD_OK) {
150                         qDebug() << "ERROR: libsigrokdecode init failed.";
151                         break;
152                 }
153
154                 // Load the protocol decoders
155                 srd_decoder_load_all();
156 #endif
157
158                 try {
159                         // Create the device manager, initialise the drivers
160                         pv::DeviceManager device_manager(context);
161
162                         // Initialise the main window
163                         pv::MainWindow w(device_manager,
164                                 open_file, open_file_format);
165                         w.show();
166
167 #ifdef ENABLE_SIGNALS
168                         if (SignalHandler::prepare_signals()) {
169                                 SignalHandler *const handler =
170                                         new SignalHandler(&w);
171                                 QObject::connect(handler,
172                                         SIGNAL(int_received()),
173                                         &w, SLOT(close()));
174                                 QObject::connect(handler,
175                                         SIGNAL(term_received()),
176                                         &w, SLOT(close()));
177                         } else {
178                                 qWarning() <<
179                                         "Could not prepare signal handler.";
180                         }
181 #endif
182
183                         // Run the application
184                         ret = a.exec();
185
186                 } catch (std::exception e) {
187                         qDebug() << e.what();
188                 }
189
190 #ifdef ENABLE_DECODE
191                 // Destroy libsigrokdecode
192                 srd_exit();
193 #endif
194
195         } while (0);
196
197         return ret;
198 }