summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2020-11-29 10:41:28 +0100
committerDavid Robillard <d@drobilla.net>2020-11-29 12:00:37 +0100
commit6d71c2f83b7fef4956f04e277a905d45de249745 (patch)
tree39b11d92856d98d798529061ac0f8957dc81109d
parent15004f61bf9f330b959a815465bdda09c66ec9a4 (diff)
downloadpatchage-6d71c2f83b7fef4956f04e277a905d45de249745.tar.gz
patchage-6d71c2f83b7fef4956f04e277a905d45de249745.tar.bz2
patchage-6d71c2f83b7fef4956f04e277a905d45de249745.zip
Move command line handling to main
-rw-r--r--src/Options.hpp26
-rw-r--r--src/Patchage.cpp42
-rw-r--r--src/Patchage.hpp12
-rw-r--r--src/main.cpp46
4 files changed, 80 insertions, 46 deletions
diff --git a/src/Options.hpp b/src/Options.hpp
new file mode 100644
index 0000000..df9f220
--- /dev/null
+++ b/src/Options.hpp
@@ -0,0 +1,26 @@
+/* This file is part of Patchage.
+ * Copyright 2007-2020 David Robillard <d@drobilla.net>
+ *
+ * Patchage is free software: you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License as published by the Free
+ * Software Foundation, either version 3 of the License, or (at your option)
+ * any later version.
+ *
+ * Patchage is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Patchage. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef PATCHAGE_OPTIONS_HPP
+#define PATCHAGE_OPTIONS_HPP
+
+struct Options
+{
+ bool alsa_driver_autoattach = true;
+ bool jack_driver_autoattach = true;
+};
+
+#endif // PATCHAGE_OPTIONS_HPP
diff --git a/src/Patchage.cpp b/src/Patchage.cpp
index 641714a..01eda37 100644
--- a/src/Patchage.cpp
+++ b/src/Patchage.cpp
@@ -61,7 +61,7 @@ PATCHAGE_RESTORE_WARNINGS
#include <cmath>
#include <cstdint>
#include <cstdlib>
-#include <fstream>
+#include <sstream>
#ifdef PATCHAGE_GTK_OSX
@@ -123,7 +123,7 @@ port_order(const GanvPort* a, const GanvPort* b, void*)
#define INIT_WIDGET(x) x(_xml, (#x) + 1)
-Patchage::Patchage(int argc, char** argv)
+Patchage::Patchage(Options options)
: _xml(UIFile::open("patchage"))
, _gtk_main(nullptr)
, INIT_WIDGET(_about_win)
@@ -164,40 +164,12 @@ Patchage::Patchage(int argc, char** argv)
, _legend(nullptr)
, _log(_status_text)
, _connector(_log)
+ , _options{options}
, _pane_initialized(false)
, _attach(true)
- , _jack_driver_autoattach(true)
-#ifdef HAVE_ALSA
- , _alsa_driver_autoattach(true)
-#endif
{
_canvas = std::make_shared<PatchageCanvas>(_connector, 1600 * 2, 1200 * 2);
- while (argc > 0) {
- if (!strcmp(*argv, "-h") || !strcmp(*argv, "--help")) {
- std::cout << "Usage: patchage [OPTION]...\n";
- std::cout << "Visually connect JACK and ALSA Audio/MIDI ports.\n\n";
- std::cout << "Options:\n";
- std::cout << "\t-h --help Show this help\n";
- std::cout
- << "\t-A --no-alsa Do not automatically attach to ALSA\n";
- std::cout
- << "\t-J --no-jack Do not automatically attack to JACK\n";
- exit(0);
-#ifdef HAVE_ALSA
- } else if (!strcmp(*argv, "-A") || !strcmp(*argv, "--no-alsa")) {
- _alsa_driver_autoattach = false;
-#endif
-#if defined(PATCHAGE_LIBJACK) || defined(HAVE_JACK_DBUS)
- } else if (!strcmp(*argv, "-J") || !strcmp(*argv, "--no-jack")) {
- _jack_driver_autoattach = false;
-#endif
- }
-
- argv++;
- argc--;
- }
-
Glib::set_application_name("Patchage");
_about_win->property_program_name() = "Patchage";
_about_win->property_logo_icon_name() = "patchage";
@@ -384,17 +356,13 @@ Patchage::~Patchage()
void
Patchage::attach()
{
-#if defined(PATCHAGE_LIBJACK) || defined(HAVE_JACK_DBUS)
- if (_jack_driver_autoattach) {
+ if (_jack_driver && _options.jack_driver_autoattach) {
_jack_driver->attach(true);
}
-#endif
-#ifdef HAVE_ALSA
- if (_alsa_driver_autoattach) {
+ if (_alsa_driver && _options.alsa_driver_autoattach) {
_alsa_driver->attach(false);
}
-#endif
process_events();
refresh();
diff --git a/src/Patchage.hpp b/src/Patchage.hpp
index f153bd3..a6cfec0 100644
--- a/src/Patchage.hpp
+++ b/src/Patchage.hpp
@@ -43,6 +43,7 @@
#include "ILog.hpp"
#include "Legend.hpp"
#include "Metadata.hpp"
+#include "Options.hpp"
#include "PatchageEvent.hpp"
#include "TextViewLog.hpp"
#include "Widget.hpp"
@@ -65,7 +66,7 @@ class Module;
class Patchage
{
public:
- Patchage(int argc, char** argv);
+ explicit Patchage(Options options);
~Patchage();
Patchage(const Patchage&) = delete;
@@ -209,12 +210,9 @@ protected:
Glib::RefPtr<Gtk::TextTag> _error_tag;
Glib::RefPtr<Gtk::TextTag> _warning_tag;
- bool _pane_initialized;
- bool _attach;
- bool _jack_driver_autoattach;
-#ifdef HAVE_ALSA
- bool _alsa_driver_autoattach;
-#endif
+ Options _options;
+ bool _pane_initialized;
+ bool _attach;
};
#endif // PATCHAGE_PATCHAGE_HPP
diff --git a/src/main.cpp b/src/main.cpp
index 6ad255f..db4721b 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -25,12 +25,16 @@
# include <string>
#endif
+#include "Options.hpp"
#include "Patchage.hpp"
+#include "patchage_config.h"
#include <glibmm/exception.h>
#include <iostream>
+namespace {
+
#ifdef __APPLE__
void
set_bundle_environment()
@@ -67,6 +71,19 @@ set_bundle_environment()
}
#endif
+void
+print_usage()
+{
+ std::cout << "Usage: patchage [OPTION]...\n";
+ std::cout << "Visually connect JACK and ALSA Audio and MIDI ports.\n\n";
+ std::cout << "Options:\n";
+ std::cout << " -h, --help Display this help and exit.\n";
+ std::cout << " -A, --no-alsa Do not automatically attach to ALSA.\n";
+ std::cout << " -J, --no-jack Do not automatically attack to JACK.\n";
+}
+
+} // namespace
+
int
main(int argc, char** argv)
{
@@ -79,8 +96,33 @@ main(int argc, char** argv)
Glib::thread_init();
Gtk::Main app(argc, argv);
-
- Patchage patchage(argc, argv);
+ ++argv;
+ --argc;
+
+ // Parse command line options
+ Options options;
+ while (argc > 0) {
+ if (!strcmp(*argv, "-h") || !strcmp(*argv, "--help")) {
+ print_usage();
+ return 0;
+ }
+
+ if (!strcmp(*argv, "-A") || !strcmp(*argv, "--no-alsa")) {
+ options.alsa_driver_autoattach = false;
+ } else if (!strcmp(*argv, "-J") || !strcmp(*argv, "--no-jack")) {
+ options.jack_driver_autoattach = false;
+ } else {
+ std::cerr << "patchage: invalid option -- '" << *argv << "'\n";
+ print_usage();
+ return 1;
+ }
+
+ ++argv;
+ --argc;
+ }
+
+ // Run until main loop is finished
+ Patchage patchage(options);
Gtk::Main::run(*patchage.window());
patchage.save();