DPF

DISTRHO Plugin Framework
Log | Files | Refs | Submodules | README | LICENSE

commit ab236b8d7aa345345376265388e127f8e8febcf0
parent 7069b02cddb7f82c152629d2f406ad9b2d95ce10
Author: falkTX <falktx@falktx.com>
Date:   Tue,  5 Oct 2021 00:04:30 +0100

VST3: Fix state messages

Signed-off-by: falkTX <falktx@falktx.com>

Diffstat:
Mdistrho/src/DistrhoPluginVST3.cpp | 40+++++++++++++++++++++++++++++++---------
Mdistrho/src/DistrhoUIVST3.cpp | 35++++++++++++++++++++++++++++-------
2 files changed, 59 insertions(+), 16 deletions(-)

diff --git a/distrho/src/DistrhoPluginVST3.cpp b/distrho/src/DistrhoPluginVST3.cpp @@ -310,9 +310,9 @@ ScopedUTF16String::ScopedUTF16String(const char* const s) noexcept : str(nullptr) { const size_t len = strlen(s); - str = (int16_t*)malloc(sizeof(int16_t) * len); + str = (int16_t*)malloc(sizeof(int16_t) * (len + 1)); DISTRHO_SAFE_ASSERT_RETURN(str != nullptr,); - strncpy_utf16(str, s, len); + strncpy_utf16(str, s, len + 1); } ScopedUTF16String::~ScopedUTF16String() noexcept @@ -1836,26 +1836,42 @@ public: # if DISTRHO_PLUGIN_WANT_STATE if (std::strcmp(msgid, "state-set") == 0) { - int16_t* key16; - int16_t* value16; - uint32_t keySize, valueSize; + int64_t keyLength = -1; + int64_t valueLength = -1; v3_result res; - res = v3_cpp_obj(attrs)->get_binary(attrs, "key", (const void**)&key16, &keySize); + res = v3_cpp_obj(attrs)->get_int(attrs, "key:length", &keyLength); DISTRHO_SAFE_ASSERT_INT_RETURN(res == V3_OK, res, res); + DISTRHO_SAFE_ASSERT_INT_RETURN(keyLength >= 0, keyLength, V3_INTERNAL_ERR); - res = v3_cpp_obj(attrs)->get_binary(attrs, "value", (const void**)&value16, &valueSize); + res = v3_cpp_obj(attrs)->get_int(attrs, "value:length", &valueLength); + DISTRHO_SAFE_ASSERT_INT_RETURN(res == V3_OK, res, res); + DISTRHO_SAFE_ASSERT_INT_RETURN(valueLength >= 0, valueLength, V3_INTERNAL_ERR); + + int16_t* const key16 = (int16_t*)std::malloc(sizeof(int16_t)*(keyLength + 1)); + DISTRHO_SAFE_ASSERT_RETURN(key16 != nullptr, V3_NOMEM); + + int16_t* const value16 = (int16_t*)std::malloc(sizeof(int16_t)*(valueLength + 1)); + DISTRHO_SAFE_ASSERT_RETURN(value16 != nullptr, V3_NOMEM); + + res = v3_cpp_obj(attrs)->get_string(attrs, "key", key16, sizeof(int16_t)*keyLength); + DISTRHO_SAFE_ASSERT_INT_RETURN(res == V3_OK, res, res); + + res = v3_cpp_obj(attrs)->get_string(attrs, "value", value16, sizeof(int16_t)*valueLength); DISTRHO_SAFE_ASSERT_INT_RETURN(res == V3_OK, res, res); // do cheap inline conversion char* const key = (char*)key16; char* const value = (char*)value16; - for (uint32_t i=0; i<keySize/sizeof(int16_t); ++i) + for (int64_t i=0; i<keyLength; ++i) key[i] = key16[i]; - for (uint32_t i=0; i<valueSize/sizeof(int16_t); ++i) + for (int64_t i=0; i<valueLength; ++i) value[i] = value16[i]; + key[keyLength] = '\0'; + value[valueLength] = '\0'; + fPlugin.setState(key, value); // save this key as needed @@ -1868,6 +1884,8 @@ public: if (dkey == key) { it->second = value; + std::free(key16); + std::free(value16); return V3_OK; } } @@ -1875,6 +1893,8 @@ public: d_stderr("Failed to find plugin state with key \"%s\"", key); } + std::free(key16); + std::free(value16); return V3_OK; } # endif @@ -2043,6 +2063,8 @@ private: DISTRHO_SAFE_ASSERT_RETURN(attrlist != nullptr,); v3_cpp_obj(attrlist)->set_int(attrlist, "__dpf_msg_target__", 2); + v3_cpp_obj(attrlist)->set_int(attrlist, "key:length", std::strlen(key)); + v3_cpp_obj(attrlist)->set_int(attrlist, "value:length", std::strlen(value)); v3_cpp_obj(attrlist)->set_string(attrlist, "key", ScopedUTF16String(key)); v3_cpp_obj(attrlist)->set_string(attrlist, "value", ScopedUTF16String(value)); v3_cpp_obj(fConnection)->notify(fConnection, message); diff --git a/distrho/src/DistrhoUIVST3.cpp b/distrho/src/DistrhoUIVST3.cpp @@ -299,27 +299,46 @@ public: #if DISTRHO_PLUGIN_WANT_STATE if (std::strcmp(msgid, "state-set") == 0) { - int16_t* key16; - int16_t* value16; - uint32_t keySize, valueSize; + int64_t keyLength = -1; + int64_t valueLength = -1; v3_result res; - res = v3_cpp_obj(attrs)->get_binary(attrs, "key", (const void**)&key16, &keySize); + res = v3_cpp_obj(attrs)->get_int(attrs, "key:length", &keyLength); DISTRHO_SAFE_ASSERT_INT_RETURN(res == V3_OK, res, res); + DISTRHO_SAFE_ASSERT_INT_RETURN(keyLength >= 0, keyLength, V3_INTERNAL_ERR); - res = v3_cpp_obj(attrs)->get_binary(attrs, "value", (const void**)&value16, &valueSize); + res = v3_cpp_obj(attrs)->get_int(attrs, "value:length", &valueLength); + DISTRHO_SAFE_ASSERT_INT_RETURN(res == V3_OK, res, res); + DISTRHO_SAFE_ASSERT_INT_RETURN(valueLength >= 0, valueLength, V3_INTERNAL_ERR); + + int16_t* const key16 = (int16_t*)std::malloc(sizeof(int16_t)*(keyLength + 1)); + DISTRHO_SAFE_ASSERT_RETURN(key16 != nullptr, V3_NOMEM); + + int16_t* const value16 = (int16_t*)std::malloc(sizeof(int16_t)*(valueLength + 1)); + DISTRHO_SAFE_ASSERT_RETURN(value16 != nullptr, V3_NOMEM); + + res = v3_cpp_obj(attrs)->get_string(attrs, "key", key16, sizeof(int16_t)*keyLength); + DISTRHO_SAFE_ASSERT_INT_RETURN(res == V3_OK, res, res); + + res = v3_cpp_obj(attrs)->get_string(attrs, "value", value16, sizeof(int16_t)*valueLength); DISTRHO_SAFE_ASSERT_INT_RETURN(res == V3_OK, res, res); // do cheap inline conversion char* const key = (char*)key16; char* const value = (char*)value16; - for (uint32_t i=0; i<keySize/sizeof(int16_t); ++i) + for (int64_t i=0; i<keyLength; ++i) key[i] = key16[i]; - for (uint32_t i=0; i<valueSize/sizeof(int16_t); ++i) + for (int64_t i=0; i<valueLength; ++i) value[i] = value16[i]; + key[keyLength] = '\0'; + value[valueLength] = '\0'; + fUI.stateChanged(key, value); + + std::free(key16); + std::free(value16); return V3_OK; } #endif @@ -549,6 +568,8 @@ private: DISTRHO_SAFE_ASSERT_RETURN(attrlist != nullptr,); v3_cpp_obj(attrlist)->set_int(attrlist, "__dpf_msg_target__", 1); + v3_cpp_obj(attrlist)->set_int(attrlist, "key:length", std::strlen(key)); + v3_cpp_obj(attrlist)->set_int(attrlist, "value:length", std::strlen(value)); v3_cpp_obj(attrlist)->set_string(attrlist, "key", ScopedUTF16String(key)); v3_cpp_obj(attrlist)->set_string(attrlist, "value", ScopedUTF16String(value)); v3_cpp_obj(fConnection)->notify(fConnection, message);