Fix hardcoded home directory (pass -DHOME=$HOME to makensis).
[pulseview.git] / main.cpp
blob07cfa2facfb9eefce062c243ad4f6d7ead0a417b
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 #include <libsigrokdecode/libsigrokdecode.h> /* First, so we avoid a _POSIX_C_SOURCE warning. */
23 #include <stdint.h>
24 #include <libsigrok/libsigrok.h>
26 #include <getopt.h>
28 #include <QtGui/QApplication>
29 #include <QDebug>
31 #ifdef ENABLE_SIGNALS
32 #include "signalhandler.h"
33 #endif
35 #include "pv/devicemanager.h"
36 #include "pv/mainwindow.h"
38 #include "config.h"
40 void usage()
42 fprintf(stdout,
43 "Usage:\n"
44 " %s [OPTION…] [FILE] — %s\n"
45 "\n"
46 "Help Options:\n"
47 " -l, --loglevel Set libsigrok/libsigrokdecode loglevel\n"
48 " -V, --version Show release version\n"
49 " -h, -?, --help Show help option\n"
50 "\n", PV_BIN_NAME, PV_DESCRIPTION);
53 int main(int argc, char *argv[])
55 int ret = 0;
56 struct sr_context *sr_ctx = NULL;
57 const char *open_file = NULL;
59 QApplication a(argc, argv);
61 // Set some application metadata
62 QApplication::setApplicationVersion(PV_VERSION_STRING);
63 QApplication::setApplicationName("PulseView");
64 QApplication::setOrganizationDomain("http://www.sigrok.org");
66 // Parse arguments
67 while (1) {
68 static const struct option long_options[] = {
69 {"loglevel", required_argument, 0, 'l'},
70 {"version", no_argument, 0, 'V'},
71 {"help", no_argument, 0, 'h'},
72 {0, 0, 0, 0}
75 const int c = getopt_long(argc, argv,
76 "l:Vh?", long_options, NULL);
77 if (c == -1)
78 break;
80 switch (c) {
81 case 'l':
83 const int loglevel = atoi(optarg);
84 sr_log_loglevel_set(loglevel);
85 srd_log_loglevel_set(loglevel);
87 break;
90 case 'V':
91 // Print version info
92 fprintf(stdout, "%s %s\n", PV_TITLE, PV_VERSION_STRING);
93 return 0;
95 case 'h':
96 case '?':
97 usage();
98 return 0;
102 if (argc - optind > 1) {
103 fprintf(stderr, "Only one file can be openened.\n");
104 return 1;
105 } else if (argc - optind == 1)
106 open_file = argv[argc - 1];
108 // Initialise libsigrok
109 if (sr_init(&sr_ctx) != SR_OK) {
110 qDebug() << "ERROR: libsigrok init failed.";
111 return 1;
114 do {
116 // Initialise libsigrokdecode
117 if (srd_init(NULL) != SRD_OK) {
118 qDebug() << "ERROR: libsigrokdecode init failed.";
119 break;
122 // Load the protocol decoders
123 srd_decoder_load_all();
125 try {
126 // Create the device manager, initialise the drivers
127 pv::DeviceManager device_manager(sr_ctx);
129 // Initialise the main window
130 pv::MainWindow w(device_manager, open_file);
131 w.show();
133 #ifdef ENABLE_SIGNALS
134 if(SignalHandler::prepare_signals()) {
135 SignalHandler *const handler =
136 new SignalHandler(&w);
137 QObject::connect(handler,
138 SIGNAL(int_received()),
139 &w, SLOT(close()));
140 QObject::connect(handler,
141 SIGNAL(term_received()),
142 &w, SLOT(close()));
143 } else {
144 qWarning() <<
145 "Could not prepare signal handler.";
147 #endif
149 // Run the application
150 ret = a.exec();
152 } catch(std::exception e) {
153 qDebug() << e.what();
156 // Destroy libsigrokdecode
157 srd_exit();
159 } while (0);
161 // Destroy libsigrok
162 if (sr_ctx)
163 sr_exit(sr_ctx);
165 return ret;