AnalogSignal: Apply scale handle dragging
[pulseview.git] / main.cpp
blobdaec62d2af09055409677d90885bc9faac5ce4af
1 /*
2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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
21 #ifdef ENABLE_DECODE
22 #include <libsigrokdecode/libsigrokdecode.h> /* First, so we avoid a _POSIX_C_SOURCE warning. */
23 #endif
25 #include <stdint.h>
26 #include <libsigrokcxx/libsigrokcxx.hpp>
28 #include <getopt.h>
30 #include <QDebug>
32 #ifdef ENABLE_SIGNALS
33 #include "signalhandler.hpp"
34 #endif
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
45 #include "config.h"
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
53 void usage()
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);
70 int main(int argc, char *argv[])
72 int ret = 0;
73 std::shared_ptr<sigrok::Context> context;
74 std::string open_file, open_file_format;
76 Application a(argc, argv);
78 #ifdef ANDROID
79 srau_init_environment();
80 pv::AndroidLogHandler::install_callbacks();
81 pv::AndroidAssetReader asset_reader;
82 #endif
84 // Parse arguments
85 while (1) {
86 static const struct option long_options[] = {
87 {"help", no_argument, 0, 'h'},
88 {"version", no_argument, 0, 'V'},
89 {"loglevel", required_argument, 0, 'l'},
90 {"input-file", required_argument, 0, 'i'},
91 {"input-format", required_argument, 0, 'I'},
92 {0, 0, 0, 0}
95 const int c = getopt_long(argc, argv,
96 "l:Vh?i:I:", long_options, nullptr);
97 if (c == -1)
98 break;
100 switch (c) {
101 case 'h':
102 case '?':
103 usage();
104 return 0;
106 case 'V':
107 // Print version info
108 fprintf(stdout, "%s %s\n", PV_TITLE, PV_VERSION_STRING);
109 return 0;
111 case 'l':
113 const int loglevel = atoi(optarg);
114 context->set_log_level(sigrok::LogLevel::get(loglevel));
116 #ifdef ENABLE_DECODE
117 srd_log_loglevel_set(loglevel);
118 #endif
120 break;
123 case 'i':
124 open_file = optarg;
125 break;
127 case 'I':
128 open_file_format = optarg;
129 break;
133 if (argc - optind > 1) {
134 fprintf(stderr, "Only one file can be openened.\n");
135 return 1;
136 } else if (argc - optind == 1) {
137 open_file = argv[argc - 1];
140 // Initialise libsigrok
141 context = sigrok::Context::create();
142 #ifdef ANDROID
143 context->set_resource_reader(&asset_reader);
144 #endif
145 do {
147 #ifdef ENABLE_DECODE
148 // Initialise libsigrokdecode
149 if (srd_init(nullptr) != SRD_OK) {
150 qDebug() << "ERROR: libsigrokdecode init failed.";
151 break;
154 // Load the protocol decoders
155 srd_decoder_load_all();
156 #endif
158 try {
159 // Create the device manager, initialise the drivers
160 pv::DeviceManager device_manager(context);
162 // Initialise the main window
163 pv::MainWindow w(device_manager,
164 open_file, open_file_format);
165 w.show();
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.";
181 #endif
183 // Run the application
184 ret = a.exec();
186 } catch(std::exception e) {
187 qDebug() << e.what();
190 #ifdef ENABLE_DECODE
191 // Destroy libsigrokdecode
192 srd_exit();
193 #endif
195 } while (0);
197 return ret;