]> sigrok.org Git - pulseview.git/blob - main.cpp
Tolerate unexpected packets rather than asserting
[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, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20
21 #include <sigrokdecode.h> /* First, so we avoid a _POSIX_C_SOURCE warning. */
22 #include <signal.h>
23 #include <stdint.h>
24 #include <libsigrok/libsigrok.h>
25
26 #include <getopt.h>
27
28 #include <QtGui/QApplication>
29 #include <QDebug>
30
31 #include "pv/mainwindow.h"
32
33 #include "config.h"
34
35 // Global pointer to our QApplication
36 QApplication *g_app = NULL;
37
38 void usage()
39 {
40         fprintf(stdout,
41                 "Usage:\n"
42                 "  %s [OPTION…] [FILE] — %s\n"
43                 "\n"
44                 "Help Options:\n"
45                 "  -V, --version                   Show release version\n"
46                 "  -h, -?, --help                  Show help option\n"
47                 "\n", PV_BIN_NAME, PV_DESCRIPTION);
48 }
49
50 /*
51  * SIGINT handler (likely received Ctrl-C from terminal)
52  */
53 void sigint_handler(int param)
54 {
55         (void)param;
56
57         qDebug("Received SIGINT.");
58
59         if (g_app)
60                 g_app->quit();
61 }
62
63 int main(int argc, char *argv[])
64 {
65         int ret = 0;
66         struct sr_context *sr_ctx = NULL;
67         const char *open_file = NULL;
68
69         // Register a SIGINT handler
70         signal(SIGINT, sigint_handler);
71
72         QApplication a(argc, argv);
73         // Now we have an application to populate our global pointer
74         g_app = &a;
75
76         // Set some application metadata
77         QApplication::setApplicationVersion(PV_VERSION_STRING);
78         QApplication::setApplicationName("PulseView");
79         QApplication::setOrganizationDomain("http://www.sigrok.org");
80
81         // Parse arguments
82         while (1) {
83                 static const struct option long_options[] = {
84                         {"version", no_argument, 0, 'V'},
85                         {"help", no_argument, 0, 'h'},
86                         {0, 0, 0, 0}
87                 };
88
89                 const int c = getopt_long(argc, argv,
90                         "Vh?", long_options, NULL);
91                 if (c == -1)
92                         break;
93
94                 switch (c) {
95                 case 'V':
96                         // Print version info
97                         fprintf(stdout, "%s %s\n", PV_TITLE, PV_VERSION_STRING);
98                         return 0;
99
100                 case 'h':
101                 case '?':
102                         usage();
103                         return 0;
104                 }
105         }
106
107         if (argc - optind > 1) {
108                 fprintf(stderr, "Only one file can be openened.\n");
109                 return 1;
110         } else if (argc - optind == 1)
111                 open_file = argv[argc - 1];
112
113         // Initialise libsigrok
114         if (sr_init(&sr_ctx) != SR_OK) {
115                 qDebug() << "ERROR: libsigrok init failed.";
116                 return 1;
117         }
118
119         // Initialise libsigrokdecode
120         if (srd_init(NULL) == SRD_OK) {
121
122                 // Load the protocol decoders
123                 srd_decoder_load_all();
124
125                 // Initialize all libsigrok drivers
126                 sr_dev_driver **const drivers = sr_driver_list();
127                 for (sr_dev_driver **driver = drivers; *driver; driver++) {
128                         if (sr_driver_init(sr_ctx, *driver) != SR_OK) {
129                                 qDebug("Failed to initialize driver %s",
130                                         (*driver)->name);
131                                 ret = 1;
132                                 break;
133                         }
134                 }
135
136                 if (ret == 0) {
137                         // Initialise the main window
138                         pv::MainWindow w(open_file);
139                         w.show();
140
141                         // Run the application
142                         ret = a.exec();
143                 }
144
145                 // Destroy libsigrokdecode and libsigrok
146                 srd_exit();
147
148         } else {
149                 qDebug() << "ERROR: libsigrokdecode init failed.";
150         }
151
152         if (sr_ctx)
153                 sr_exit(sr_ctx);
154
155         return ret;
156 }