]> sigrok.org Git - pulseview.git/blame - main.cpp
Added ENABLE_SIGROKDECODE option, disabled for now
[pulseview.git] / main.cpp
CommitLineData
d7bed479 1/*
b3f22de0 2 * This file is part of the PulseView project.
d7bed479
JH
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
d52d8455 21#ifdef ENABLE_SIGROKDECODE
664fca5c 22#include <sigrokdecode.h> /* First, so we avoid a _POSIX_C_SOURCE warning. */
d52d8455
JH
23#endif
24
664fca5c
JH
25#include <stdint.h>
26#include <libsigrok/libsigrok.h>
664fca5c 27
f7951df4
JH
28#include <getopt.h>
29
d7bed479 30#include <QtGui/QApplication>
664fca5c 31#include <QDebug>
075fb499 32
7a255aa9 33#include "signalhandler.h"
51e77110 34#include "pv/mainwindow.h"
d7bed479 35
075fb499
AG
36#include "config.h"
37
d4384c6d
JH
38void usage()
39{
9fd5bb6e 40 fprintf(stdout,
d4384c6d 41 "Usage:\n"
1d478458 42 " %s [OPTION…] [FILE] — %s\n"
d4384c6d
JH
43 "\n"
44 "Help Options:\n"
9b5099b6 45 " -l, --loglevel Set libsigrok/libsigrokdecode loglevel\n"
d4384c6d
JH
46 " -V, --version Show release version\n"
47 " -h, -?, --help Show help option\n"
48 "\n", PV_BIN_NAME, PV_DESCRIPTION);
49}
50
d7bed479
JH
51int main(int argc, char *argv[])
52{
be03620e 53 int ret = 0;
f2242929 54 struct sr_context *sr_ctx = NULL;
1d478458 55 const char *open_file = NULL;
f2242929 56
d7bed479 57 QApplication a(argc, argv);
d7bed479 58
6a6772b7 59 // Set some application metadata
075fb499 60 QApplication::setApplicationVersion(PV_VERSION_STRING);
3623c8d3 61 QApplication::setApplicationName("PulseView");
640e5565
JH
62 QApplication::setOrganizationDomain("http://www.sigrok.org");
63
f7951df4
JH
64 // Parse arguments
65 while (1) {
66 static const struct option long_options[] = {
9b5099b6 67 {"loglevel", required_argument, 0, 'l'},
333d5bbc
UH
68 {"version", no_argument, 0, 'V'},
69 {"help", no_argument, 0, 'h'},
f7951df4
JH
70 {0, 0, 0, 0}
71 };
72
815b3f26 73 const int c = getopt_long(argc, argv,
9b5099b6 74 "l:Vh?", long_options, NULL);
f7951df4
JH
75 if (c == -1)
76 break;
77
78 switch (c) {
9b5099b6
JH
79 case 'l':
80 {
81 const int loglevel = atoi(optarg);
82 sr_log_loglevel_set(loglevel);
d52d8455
JH
83
84#ifdef ENABLE_SIGROKDECODE
9b5099b6 85 srd_log_loglevel_set(loglevel);
d52d8455
JH
86#endif
87
9b5099b6
JH
88 break;
89 }
90
f7951df4
JH
91 case 'V':
92 // Print version info
9fd5bb6e 93 fprintf(stdout, "%s %s\n", PV_TITLE, PV_VERSION_STRING);
f7951df4 94 return 0;
d4384c6d
JH
95
96 case 'h':
97 case '?':
98 usage();
99 return 0;
f7951df4
JH
100 }
101 }
102
1d478458
JH
103 if (argc - optind > 1) {
104 fprintf(stderr, "Only one file can be openened.\n");
105 return 1;
106 } else if (argc - optind == 1)
107 open_file = argv[argc - 1];
108
6a6772b7 109 // Initialise libsigrok
f2242929 110 if (sr_init(&sr_ctx) != SR_OK) {
664fca5c
JH
111 qDebug() << "ERROR: libsigrok init failed.";
112 return 1;
113 }
114
d52d8455
JH
115 do {
116
117#ifdef ENABLE_SIGROKDECODE
118 // Initialise libsigrokdecode
119 if (srd_init(NULL) != SRD_OK) {
120 qDebug() << "ERROR: libsigrokdecode init failed.";
121 break;
122 }
be03620e
JH
123
124 // Load the protocol decoders
125 srd_decoder_load_all();
d52d8455 126#endif
be03620e
JH
127
128 // Initialize all libsigrok drivers
129 sr_dev_driver **const drivers = sr_driver_list();
130 for (sr_dev_driver **driver = drivers; *driver; driver++) {
24ea28e7 131 if (sr_driver_init(sr_ctx, *driver) != SR_OK) {
be03620e
JH
132 qDebug("Failed to initialize driver %s",
133 (*driver)->name);
134 ret = 1;
135 break;
136 }
137 }
664fca5c 138
333d5bbc 139 if (ret == 0) {
be03620e 140 // Initialise the main window
1d478458 141 pv::MainWindow w(open_file);
be03620e 142 w.show();
664fca5c 143
708605aa
JH
144 if(SignalHandler::prepare_signals()) {
145 SignalHandler *const handler =
7a255aa9 146 new SignalHandler(&w);
708605aa
JH
147 QObject::connect(handler,
148 SIGNAL(int_received()),
7a255aa9 149 &w, SLOT(close()));
708605aa
JH
150 QObject::connect(handler,
151 SIGNAL(term_received()),
7a255aa9
JH
152 &w, SLOT(close()));
153 } else {
154 qWarning() <<
155 "Could not prepare signal handler.";
156 }
157
be03620e
JH
158 // Run the application
159 ret = a.exec();
6fb67b27 160 }
6fb67b27 161
d52d8455
JH
162#ifdef ENABLE_SIGROKDECODE
163 // Destroy libsigrokdecode
be03620e 164 srd_exit();
d52d8455 165#endif
664fca5c 166
d52d8455 167 } while (0);
664fca5c 168
d52d8455 169 // Destroy libsigrok
f2242929
PS
170 if (sr_ctx)
171 sr_exit(sr_ctx);
664fca5c
JH
172
173 return ret;
d7bed479 174}