From 49a673e4ad2866c1f413c4a4c8b0edd2581082dd Mon Sep 17 00:00:00 2001 From: Skrylar Date: Tue, 26 Mar 2019 16:05:57 -0500 Subject: [PATCH] invalid port names detected and auto-fixed on file load fixes #14 --- src/jack-audio-module-widget.cc | 15 ++++++++++++++- src/jaq.cc | 31 ++++++++++++++++++++++--------- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/jack-audio-module-widget.cc b/src/jack-audio-module-widget.cc index 32f9d16..b57dd5b 100644 --- a/src/jack-audio-module-widget.cc +++ b/src/jack-audio-module-widget.cc @@ -252,12 +252,25 @@ json_t* jack_audio_module_widget_base::toJson() { } void jack_audio_module_widget_base::fromJson(json_t* json) { + auto module = reinterpret_cast(this->module); auto port_names = json_object_get(json, "port_names"); if (json_is_array(port_names)) { for (size_t i = 0; i < std::min(json_array_size(port_names), (size_t)8); i++) { auto item = json_array_get(port_names, i); if (json_is_string(item)) { - this->port_names[i]->setText(std::string(json_string_value(item), json_string_length(item))); + if (module->jport[i].rename(json_string_value(item))) { + this->port_names[i]->text = std::string(json_string_value(item)); + } else { + static const size_t buffer_size = 128; + char port_name[buffer_size]; + hashidsxx::Hashids hash(g_hashid_salt); + std::string id = hash.encode(reinterpret_cast(module)); + + snprintf(reinterpret_cast(&port_name), + buffer_size, + "%s:%d", id.c_str(), (int)i); + this->port_names[i]->setText(std::string(port_name)); + } } } } diff --git a/src/jaq.cc b/src/jaq.cc index 16d3bfc..7565f09 100644 --- a/src/jaq.cc +++ b/src/jaq.cc @@ -37,23 +37,36 @@ namespace jaq { m_output = (flags & JackPortIsOutput) > 0; - static const size_t buffer_size = 128; + static const size_t buffer_size = 256; char port_name[buffer_size]; snprintf (reinterpret_cast(&port_name), buffer_size, - "%s-%s", - name, // desired port name + "%s:%s-%s", + jack_get_client_name(mom.handle), + name, // desired port name m_output ? "out" : "in"); // idiomatic suffix - handle = client::x_jack_port_register( - mom.handle, - name, - JACK_DEFAULT_AUDIO_TYPE, - flags, - 0); + auto x = client::x_jack_port_by_name(mom.handle, port_name); + + if (x == NULL) { + snprintf + (reinterpret_cast(&port_name), + buffer_size, + "%s-%s", + name, // desired port name + m_output ? "out" : "in"); // idiomatic suffix + + handle = client::x_jack_port_register( + mom.handle, + name, + JACK_DEFAULT_AUDIO_TYPE, + flags, + 0); + } else return false; if (handle) return true; + else { this->mom = 0; return false;