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