configurable JACK client name v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Tue, 29 Sep 2020 22:53:08 +0200
branchv_0
changeset 8 8ef1980db907
parent 7 a6337902e2ca
child 9 0d362165241e
configurable JACK client name
bash-completion.sh
src/CLIParser.h
src/Configuration.h
src/JackCommand.h
src/relpipe-in-jack.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bash-completion.sh	Tue Sep 29 22:53:08 2020 +0200
@@ -0,0 +1,40 @@
+# Relational pipes
+# Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
+#
+# This program 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, version 3 of the License.
+#
+# This program 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 more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+_relpipe_in_jack_completion() {
+	local w0 w1 w2 w3
+
+	COMPREPLY=()
+	w0=${COMP_WORDS[COMP_CWORD]}
+	w1=${COMP_WORDS[COMP_CWORD-1]}
+	w2=${COMP_WORDS[COMP_CWORD-2]}
+	w3=${COMP_WORDS[COMP_CWORD-3]}
+
+	BOOLEAN=(
+		"true"
+		"false"
+	)
+
+	if   [[ "$w1" == "--TODO"                                          ]];    then COMPREPLY=($(compgen -W "${BOOLEAN[*]}" -- "$w0"))
+	elif [[ "$w1" == "--jack-client-name"             && "x$w0" == "x" ]];    then COMPREPLY=("'relpipe-in-jack'")
+	else
+		OPTIONS=(
+			"--jack-client-name"
+		)
+		COMPREPLY=($(compgen -W "${OPTIONS[*]}" -- "$w0"))
+	fi
+}
+
+complete -F _relpipe_in_jack_completion relpipe-in-jack
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/CLIParser.h	Tue Sep 29 22:53:08 2020 +0200
@@ -0,0 +1,75 @@
+/**
+ * Relational pipes
+ * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
+ *
+ * This program 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, version 3 of the License.
+ *
+ * This program 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 more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#pragma once
+
+#include <vector>
+#include <iostream>
+
+#include <relpipe/common/type/typedefs.h>
+#include <relpipe/cli/CLI.h>
+#include <relpipe/cli/RelpipeCLIException.h>
+
+#include "Configuration.h"
+
+namespace relpipe {
+namespace in {
+namespace jack {
+
+class CLIParser {
+private:
+
+	relpipe::common::type::StringX readNext(const std::vector<relpipe::common::type::StringX>& arguments, int& i) {
+		if (i < arguments.size()) return arguments[i++];
+		else throw relpipe::cli::RelpipeCLIException(L"Missing CLI argument" + (i > 0 ? (L" after " + arguments[i - 1]) : L""), relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
+	}
+
+	/**
+	 * TODO: use a common method
+	 */
+	bool parseBoolean(const relpipe::common::type::StringX& value) {
+		if (value == L"true") return true;
+		else if (value == L"false") return false;
+		else throw relpipe::cli::RelpipeCLIException(L"Unable to parse boolean value: " + value + L" (expecting true or false)", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
+	}
+
+public:
+
+	static const relpipe::common::type::StringX OPTION_JACK_CLIENT_NAME;
+
+	Configuration parse(const std::vector<relpipe::common::type::StringX>& arguments) {
+		Configuration c;
+
+		for (int i = 0; i < arguments.size();) {
+			relpipe::common::type::StringX option = readNext(arguments, i);
+
+			if (option == OPTION_JACK_CLIENT_NAME) {
+				c.jackClientName = readNext(arguments, i);
+			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
+		}
+
+		return c;
+	}
+
+	virtual ~CLIParser() {
+	}
+};
+
+const relpipe::common::type::StringX CLIParser::OPTION_JACK_CLIENT_NAME = L"--jack-client-name";
+
+}
+}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/Configuration.h	Tue Sep 29 22:53:08 2020 +0200
@@ -0,0 +1,39 @@
+/**
+ * Relational pipes
+ * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
+ *
+ * This program 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, version 3 of the License.
+ *
+ * This program 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 more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#pragma once
+
+#include <vector>
+#include <iostream>
+
+#include <relpipe/common/type/typedefs.h>
+
+
+namespace relpipe {
+namespace in {
+namespace jack {
+
+class Configuration {
+public:
+	relpipe::common::type::StringX jackClientName = L"relpipe-in-jack";
+
+	virtual ~Configuration() {
+	}
+};
+
+}
+}
+}
\ No newline at end of file
--- a/src/JackCommand.h	Sat Jun 06 01:50:42 2020 +0200
+++ b/src/JackCommand.h	Tue Sep 29 22:53:08 2020 +0200
@@ -50,6 +50,9 @@
 
 class JackCommand {
 private:
+	Configuration& configuration;
+	std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // TODO: local system encoding
+
 	jack_port_t* jackPort = nullptr;
 	jack_ringbuffer_t* ringBuffer = nullptr;
 	pthread_mutex_t messageThreadLock = PTHREAD_MUTEX_INITIALIZER;
@@ -155,6 +158,9 @@
 
 public:
 
+	JackCommand(Configuration& configuration) : configuration(configuration) {
+	}
+
 	void finish(int sig) {
 		continueProcessing = false;
 	}
@@ -174,7 +180,7 @@
 		relationalWriterFlush();
 
 		// Initialize JACK connection:
-		std::string clientName = "relpipe-in-jack";
+		std::string clientName = convertor.to_bytes(configuration.jackClientName);
 		jack_client_t* client = jack_client_open(clientName.c_str(), JackNullOption, nullptr);
 		if (client == nullptr) throw JackException(L"Could not create JACK client.");
 
--- a/src/relpipe-in-jack.cpp	Sat Jun 06 01:50:42 2020 +0200
+++ b/src/relpipe-in-jack.cpp	Tue Sep 29 22:53:08 2020 +0200
@@ -21,6 +21,8 @@
 #include <relpipe/writer/Factory.h>
 #include <relpipe/cli/CLI.h>
 
+#include "Configuration.h"
+#include "CLIParser.h"
 #include "JackException.h"
 #include "JackCommand.h"
 
@@ -44,7 +46,9 @@
 	try {
 		signal(SIGHUP, finish);
 		signal(SIGINT, finish);
-		jackCommand.reset(new JackCommand());
+		CLIParser cliParser;
+		Configuration configuration = cliParser.parse(cli.arguments());
+		jackCommand.reset(new JackCommand(configuration));
 		std::shared_ptr<RelationalWriter> writer(Factory::create(std::cout));
 		jackCommand->processJackStream(writer, std::bind(fflush, stdout)); // std::bind(fflush, XXX) Factory::create(XXX) must be the same stream XXX
 		resultCode = CLI::EXIT_CODE_SUCCESS;