+ int ret = 0;
+ shared_ptr<sigrok::Context> context;
+ string open_file_format, driver;
+ vector<string> open_files;
+ bool restore_sessions = true;
+ bool do_scan = true;
+ bool show_version = false;
+
+#ifdef ENABLE_FLOW
+ // Initialise gstreamermm. Must be called before any other GLib stuff.
+ Gst::init();
+
+ // Initialize libsigrokflow. Must be called after Gst::init().
+ Srf::init();
+#endif
+
+ Application a(argc, argv);
+
+#ifdef ANDROID
+ srau_init_environment();
+ pv::AndroidLogHandler::install_callbacks();
+ pv::AndroidAssetReader asset_reader;
+#endif
+
+ // Parse arguments
+ while (true) {
+ static const struct option long_options[] = {
+ {"help", no_argument, nullptr, 'h'},
+ {"version", no_argument, nullptr, 'V'},
+ {"loglevel", required_argument, nullptr, 'l'},
+ {"driver", required_argument, nullptr, 'd'},
+ {"dont-scan", no_argument, nullptr, 'D'},
+ {"input-file", required_argument, nullptr, 'i'},
+ {"input-format", required_argument, nullptr, 'I'},
+ {"clean", no_argument, nullptr, 'c'},
+ {"log-to-stdout", no_argument, nullptr, 's'},
+ {nullptr, 0, nullptr, 0}
+ };
+
+ const int c = getopt_long(argc, argv,
+ "h?VDcl:d:i:I:", long_options, nullptr);
+ if (c == -1)
+ break;
+
+ switch (c) {
+ case 'h':
+ case '?':
+ usage();
+ return 0;
+
+ case 'V':
+ show_version = true;
+ break;
+
+ case 'l':
+ {
+ const int loglevel = atoi(optarg);
+ if (loglevel < 0 || loglevel > 5) {
+ qDebug() << "ERROR: invalid log level spec.";
+ break;
+ }
+ context->set_log_level(sigrok::LogLevel::get(loglevel));
+
+#ifdef ENABLE_DECODE
+ srd_log_loglevel_set(loglevel);
+#endif
+
+ if (loglevel >= 5) {
+ const QSettings settings;
+ qDebug() << "Settings:" << settings.fileName()
+ << "format" << settings.format();
+ }
+ break;
+ }
+
+ case 'd':
+ driver = optarg;
+ break;
+
+ case 'D':
+ do_scan = false;
+ break;
+
+ case 'i':
+ open_files.emplace_back(optarg);
+ break;
+
+ case 'I':
+ open_file_format = optarg;
+ break;
+
+ case 'c':
+ restore_sessions = false;
+ break;
+ }
+ }
+ argc -= optind;
+ argv += optind;
+
+ for (int i = 0; i < argc; i++)
+ open_files.emplace_back(argv[i]);
+
+ qRegisterMetaType<pv::util::Timestamp>("util::Timestamp");
+ qRegisterMetaType<uint64_t>("uint64_t");
+
+ // Prepare the global settings since logging needs them early on
+ pv::GlobalSettings settings;
+ settings.save_internal_defaults();
+ settings.set_defaults_where_needed();
+ settings.apply_theme();
+
+ pv::logging.init();
+
+ // Initialise libsigrok
+ context = sigrok::Context::create();
+ pv::Session::sr_context = context;
+
+#if ENABLE_STACKTRACE
+ QString temp_path = QStandardPaths::standardLocations(
+ QStandardPaths::TempLocation).at(0);
+ stacktrace_filename = temp_path + "/pv_stacktrace.dmp";
+ qDebug() << "Stack trace file is" << stacktrace_filename;
+
+ ::signal(SIGSEGV, &signal_handler);
+ ::signal(SIGABRT, &signal_handler);
+
+ if (QFileInfo::exists(stacktrace_filename))
+ process_stacktrace(temp_path);
+#endif
+
+#ifdef ANDROID
+ context->set_resource_reader(&asset_reader);
+#endif
+ do {
+
+#ifdef ENABLE_DECODE
+ // Initialise libsigrokdecode
+ if (srd_init(nullptr) != SRD_OK) {
+ qDebug() << "ERROR: libsigrokdecode init failed.";
+ break;
+ }
+
+ // Load the protocol decoders
+ srd_decoder_load_all();
+#endif
+
+#ifndef ENABLE_STACKTRACE
+ try {
+#endif
+
+ // Create the device manager, initialise the drivers
+ pv::DeviceManager device_manager(context, driver, do_scan);
+
+ a.collect_version_info(context);
+ if (show_version) {
+ a.print_version_info();
+ } else {
+ // Initialise the main window
+ pv::MainWindow w(device_manager);
+ w.show();
+
+ if (restore_sessions)
+ w.restore_sessions();
+
+ if (open_files.empty())
+ w.add_default_session();
+ else
+ for (string& open_file : open_files)
+ w.add_session_with_file(open_file, open_file_format);
+
+#ifdef ENABLE_SIGNALS
+ if (SignalHandler::prepare_signals()) {
+ SignalHandler *const handler = new SignalHandler(&w);
+ QObject::connect(handler, SIGNAL(int_received()),
+ &w, SLOT(close()));
+ QObject::connect(handler, SIGNAL(term_received()),
+ &w, SLOT(close()));
+ } else
+ qWarning() << "Could not prepare signal handler.";
+#endif
+
+ // Run the application
+ ret = a.exec();
+ }
+
+#ifndef ENABLE_STACKTRACE
+ } catch (exception& e) {
+ qDebug() << "Exception:" << e.what();
+ }
+#endif
+
+#ifdef ENABLE_DECODE
+ // Destroy libsigrokdecode
+ srd_exit();
+#endif