pulseview/main.cpp

150 lines
3.3 KiB
C++
Raw Normal View History

2012-09-03 20:20:51 +08:00
/*
* This file is part of the PulseView project.
2012-09-03 20:20:51 +08:00
*
* Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
2012-05-11 16:48:50 +08:00
#include <sigrokdecode.h> /* First, so we avoid a _POSIX_C_SOURCE warning. */
#include <signal.h>
2012-05-11 16:48:50 +08:00
#include <stdint.h>
#include <libsigrok/libsigrok.h>
2012-10-13 07:59:37 +08:00
#include <getopt.h>
2012-09-03 20:20:51 +08:00
#include <QtGui/QApplication>
2012-05-11 16:48:50 +08:00
#include <QDebug>
#include "pv/mainwindow.h"
2012-09-03 20:20:51 +08:00
#include "config.h"
// Global pointer to our QApplication
QApplication *g_app = NULL;
2013-02-19 07:56:19 +08:00
2012-10-13 08:15:47 +08:00
void usage()
{
fprintf(stdout,
2012-10-13 08:15:47 +08:00
"Usage:\n"
" %s — %s\n"
"\n"
"Help Options:\n"
" -V, --version Show release version\n"
" -h, -?, --help Show help option\n"
"\n", PV_BIN_NAME, PV_DESCRIPTION);
}
/*
2013-02-19 07:56:19 +08:00
* SIGINT handler (likely received Ctrl-C from terminal)
*/
2013-02-19 07:56:19 +08:00
void sigint_handler(int param)
{
2013-02-19 07:56:19 +08:00
(void)param;
2013-02-19 07:56:19 +08:00
qDebug("Received SIGINT.");
if (g_app)
g_app->quit();
}
2012-09-03 20:20:51 +08:00
int main(int argc, char *argv[])
{
int ret = 0;
struct sr_context *sr_ctx = NULL;
// Register a SIGINT handler
2013-02-19 07:56:19 +08:00
signal(SIGINT, sigint_handler);
2012-09-03 20:20:51 +08:00
QApplication a(argc, argv);
// Now we have an application to populate our global pointer
g_app = &a;
2012-09-03 20:20:51 +08:00
2012-10-13 07:13:44 +08:00
// Set some application metadata
QApplication::setApplicationVersion(PV_VERSION_STRING);
QApplication::setApplicationName("PulseView");
2012-05-11 16:40:27 +08:00
QApplication::setOrganizationDomain("http://www.sigrok.org");
2012-10-13 07:59:37 +08:00
// Parse arguments
while (1) {
static const struct option long_options[] = {
2012-12-13 05:19:22 +08:00
{"version", no_argument, 0, 'V'},
{"help", no_argument, 0, 'h'},
2012-10-13 07:59:37 +08:00
{0, 0, 0, 0}
};
2012-10-13 08:15:47 +08:00
const char c = getopt_long(argc, argv,
"Vh?", long_options, NULL);
2012-10-13 07:59:37 +08:00
if (c == -1)
break;
switch (c) {
case 'V':
// Print version info
fprintf(stdout, "%s %s\n", PV_TITLE, PV_VERSION_STRING);
2012-10-13 07:59:37 +08:00
return 0;
2012-10-13 08:15:47 +08:00
case 'h':
case '?':
usage();
return 0;
2012-10-13 07:59:37 +08:00
}
}
2012-10-13 07:13:44 +08:00
// Initialise libsigrok
if (sr_init(&sr_ctx) != SR_OK) {
2012-05-11 16:48:50 +08:00
qDebug() << "ERROR: libsigrok init failed.";
return 1;
}
2012-10-13 07:13:44 +08:00
// Initialise libsigrokdecode
if (srd_init(NULL) == SRD_OK) {
// Load the protocol decoders
srd_decoder_load_all();
// Initialize all libsigrok drivers
sr_dev_driver **const drivers = sr_driver_list();
for (sr_dev_driver **driver = drivers; *driver; driver++) {
if (sr_driver_init(sr_ctx, *driver) != SR_OK) {
qDebug("Failed to initialize driver %s",
(*driver)->name);
ret = 1;
break;
}
}
2012-05-11 16:48:50 +08:00
2012-12-13 05:19:22 +08:00
if (ret == 0) {
// Initialise the main window
pv::MainWindow w;
w.show();
2012-05-11 16:48:50 +08:00
// Run the application
ret = a.exec();
2012-06-28 05:36:23 +08:00
}
// Destroy libsigrokdecode and libsigrok
srd_exit();
2012-05-11 16:48:50 +08:00
} else {
qDebug() << "ERROR: libsigrokdecode init failed.";
}
2012-05-11 16:48:50 +08:00
if (sr_ctx)
sr_exit(sr_ctx);
2012-05-11 16:48:50 +08:00
return ret;
2012-09-03 20:20:51 +08:00
}