Fixed Header to respect the minimum drag distance
[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 <libsigrokdecode/libsigrokdecode.h> /* First, so we avoid a _POSIX_C_SOURCE warning. */
22
23 #include <stdint.h>
24 #include <libsigrok/libsigrok.h>
25
26 #include <getopt.h>
27
28 #include <QtGui/QApplication>
29 #include <QDebug>
30
31 #ifdef ENABLE_SIGNALS
32 #include "signalhandler.h"
33 #endif
34
35 #include "pv/devicemanager.h"
36 #include "pv/mainwindow.h"
37
38 #include "config.h"
39
40 void usage()
41 {
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);
51 }
52
53 int main(int argc, char *argv[])
54 {
55         int ret = 0;
56         struct sr_context *sr_ctx = NULL;
57         const char *open_file = NULL;
58
59         QApplication a(argc, argv);
60
61         // Set some application metadata
62         QApplication::setApplicationVersion(PV_VERSION_STRING);
63         QApplication::setApplicationName("PulseView");
64         QApplication::setOrganizationDomain("http://www.sigrok.org");
65
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}
73                 };
74
75                 const int c = getopt_long(argc, argv,
76                         "l:Vh?", long_options, NULL);
77                 if (c == -1)
78                         break;
79
80                 switch (c) {
81                 case 'l':
82                 {
83                         const int loglevel = atoi(optarg);
84                         sr_log_loglevel_set(loglevel);
85                         srd_log_loglevel_set(loglevel);
86
87                         break;
88                 }
89
90                 case 'V':
91                         // Print version info
92                         fprintf(stdout, "%s %s\n", PV_TITLE, PV_VERSION_STRING);
93                         return 0;
94
95                 case 'h':
96                 case '?':
97                         usage();
98                         return 0;
99                 }
100         }
101
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];
107
108         // Initialise libsigrok
109         if (sr_init(&sr_ctx) != SR_OK) {
110                 qDebug() << "ERROR: libsigrok init failed.";
111                 return 1;
112         }
113
114         do {
115
116                 // Initialise libsigrokdecode
117                 if (srd_init(NULL) != SRD_OK) {
118                         qDebug() << "ERROR: libsigrokdecode init failed.";
119                         break;
120                 }
121
122                 // Load the protocol decoders
123                 srd_decoder_load_all();
124
125                 try {
126                         // Create the device manager, initialise the drivers
127                         pv::DeviceManager device_manager(sr_ctx);
128
129                         // Initialise the main window
130                         pv::MainWindow w(device_manager, open_file);
131                         w.show();
132
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.";
146                         }
147 #endif
148
149                         // Run the application
150                         ret = a.exec();
151
152                 } catch(std::exception e) {
153                         qDebug() << e.what();
154                 }
155
156                 // Destroy libsigrokdecode
157                 srd_exit();
158
159         } while (0);
160
161         // Destroy libsigrok
162         if (sr_ctx)
163                 sr_exit(sr_ctx);
164
165         return ret;
166 }