QCodeEditor: Update to current cpeditor/QCodeEditor fork, commit ed1196a
[smuview.git] / main.cpp
blobd985b8fd3d8e6ea1c4877ee49be77788e2e1163b
1 /*
2 * This file is part of the SmuView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
5 * Copyright (C) 2017-2021 Frank Stettner <frank-stettner@gmx.net>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <getopt.h>
22 #include <memory>
23 #include <unistd.h>
25 #include <libsigrokcxx/libsigrokcxx.hpp>
27 #include <QDateTime>
28 #include <QDebug>
29 #include <QSettings>
31 #include "config.h"
32 #include "src/application.hpp"
33 #include "src/devicemanager.hpp"
34 #include "src/session.hpp"
35 #include "src/settingsmanager.hpp"
36 #include "src/mainwindow.hpp"
37 #include "src/ui/tabs/smuscripttab.hpp"
39 #ifdef ENABLE_SIGNALS
40 #include "signalhandler.hpp"
41 #endif
43 #ifdef _WIN32
44 #include <QtPlugin>
45 Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
46 Q_IMPORT_PLUGIN(QSvgPlugin)
47 #endif
49 using std::exception;
50 using std::make_shared;
51 using std::shared_ptr;
52 using std::string;
53 using std::vector;
55 void usage()
57 fprintf(stdout,
58 "Usage:\n"
59 " %s [OPTIONS] [FILE]\n"
60 "\n"
61 "Help Options:\n"
62 " -h, -?, --help Show help option\n"
63 "\n"
64 "Application Options:\n"
65 " -V, --version Show release version\n"
66 " -l, --loglevel Set libsigrok loglevel (0-5, default: 2)\n"
67 " -d, --driver Specify the device driver(s) to use\n"
68 " -D, --dont-scan Don't auto-scan for devices, use -d spec only\n"
69 " -s, --script Specify the SmuScript to load and execute\n"
70 " -c, --clean Don't restore previous settings on startup\n"
71 /* Disable cmd line options i and I
72 " -i, --input-file Load input from file\n"
73 " -I, --input-format Input format\n"
75 "\n"
76 "Examples:\n"
77 " %s --driver tecpel-dmm-8061-ser:conn=/dev/ttyUSB0\n"
78 "\n"
79 " %s --driver uni-t-ut61e:conn=1a86.e008\n"
80 "\n"
81 " %s --driver voltcraft-k204:conn=/dev/ttyUSB0 \\\n"
82 " --driver uni-t-ut61d:conn=1a86.e008 \\\n"
83 " --driver uni-t-ut61e-ser:conn=/dev/ttyUSB1\n",
84 SV_BIN_NAME, SV_BIN_NAME, SV_BIN_NAME, SV_BIN_NAME);
87 int main(int argc, char *argv[])
89 int ret = 0;
90 shared_ptr<sigrok::Context> context;
91 int loglevel = -1;
92 vector<string> drivers;
93 //string open_file;
94 //string open_file_format;
95 bool do_scan = true;
96 string script_file;
97 bool restore_settings = true;
99 Application app(argc, argv);
101 // Parse arguments
102 while (true) {
103 static const struct option long_options[] = {
104 { "help", no_argument, nullptr, 'h' },
105 { "version", no_argument, nullptr, 'V' },
106 { "loglevel", required_argument, nullptr, 'l' },
107 { "driver", required_argument, nullptr, 'd' },
108 { "dont-scan", no_argument, nullptr, 'D' },
109 { "script", required_argument, nullptr, 's' },
110 { "clean", no_argument, nullptr, 'c' },
111 /* Disable cmd line options i and I
112 { "input-file", required_argument, nullptr, 'i' },
113 { "input-format", required_argument, nullptr, 'I' },
115 { nullptr, 0, nullptr, 0 }
118 /* Disable cmd line options i and I
119 const int arg_char = getopt_long(argc, argv,
120 "l:Vhc?d:i:I:", long_options, nullptr);
122 const int arg_char = getopt_long(argc, argv,
123 "h?VDl:d:s:c", long_options, nullptr);
125 if (arg_char == -1)
126 break;
128 switch (arg_char) {
129 case 'h':
130 case '?':
131 usage();
132 return 0;
134 case 'V':
135 // Print version info
136 fprintf(stdout, "%s %s\n", SV_TITLE, SV_VERSION_STRING);
137 return 0;
139 case 'l':
141 loglevel = atoi(optarg);
142 if (loglevel >= 5) {
143 const QSettings settings;
144 qDebug() << "Settings:" << settings.fileName()
145 << "format" << settings.format();
147 break;
150 case 'd':
151 drivers.push_back(optarg);
152 break;
154 case 'D':
155 do_scan = false;
156 break;
158 case 's':
159 script_file = optarg;
160 break;
162 case 'c':
163 restore_settings = false;
164 break;
166 /* Disable cmd line options i and I
167 case 'i':
168 open_file = optarg;
169 break;
171 case 'I':
172 open_file_format = optarg;
173 break;
178 /* Disable cmd line options i and I
179 if (argc - optind > 1) {
180 fprintf(stderr, "Only one file can be opened.\n");
181 return 1;
184 if (argc - optind == 1)
185 open_file = argv[argc - 1];
188 // Initialise libsigrok
189 context = sigrok::Context::create();
190 sv::Session::sr_context = context;
192 do {
193 try {
194 if (loglevel >= 0)
195 context->set_log_level(sigrok::LogLevel::get(loglevel));
197 sv::SettingsManager::set_restore_settings(restore_settings);
199 // Initialize global start timestamp
200 // TODO: use std::chrono / std::time
201 sv::Session::session_start_timestamp =
202 (double)QDateTime::currentMSecsSinceEpoch() / (double)1000;
204 // Create the device manager, initialise the drivers
205 sv::DeviceManager device_manager(context, drivers, do_scan);
207 // Initialise the session.
208 auto session = make_shared<sv::Session>(device_manager);
210 // Initialise the main window.
211 sv::MainWindow main_window(device_manager, session);
212 main_window.show();
214 if (!script_file.empty())
215 main_window.add_smuscript_tab(script_file)->run_script();
217 #ifdef ENABLE_SIGNALS
218 if (SignalHandler::prepare_signals()) {
219 SignalHandler *const handler = new SignalHandler(&main_window);
220 QObject::connect(handler, &SignalHandler::int_received,
221 &main_window, &sv::MainWindow::close);
222 QObject::connect(handler, &SignalHandler::term_received,
223 &main_window, &sv::MainWindow::close);
225 else {
226 qWarning() << "Could not prepare signal handler.";
228 #endif
230 // Run the application
231 ret = Application::exec();
233 catch (exception &e) {
234 qCritical() << "main() failed: " << e.what();
238 while (false);
240 return ret;