Skip to content

Commit

Permalink
VST3: Fix state messages
Browse files Browse the repository at this point in the history
Signed-off-by: falkTX <falktx@falktx.com>
  • Loading branch information
falkTX committed Oct 4, 2021
1 parent 7069b02 commit ab236b8
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 16 deletions.
40 changes: 31 additions & 9 deletions distrho/src/DistrhoPluginVST3.cpp
Expand Up @@ -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
Expand Down Expand Up @@ -1836,26 +1836,42 @@ class PluginVst3
# 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
Expand All @@ -1868,13 +1884,17 @@ class PluginVst3
if (dkey == key)
{
it->second = value;
std::free(key16);
std::free(value16);
return V3_OK;
}
}

d_stderr("Failed to find plugin state with key \"%s\"", key);
}

std::free(key16);
std::free(value16);
return V3_OK;
}
# endif
Expand Down Expand Up @@ -2043,6 +2063,8 @@ class PluginVst3
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);
Expand Down
35 changes: 28 additions & 7 deletions distrho/src/DistrhoUIVST3.cpp
Expand Up @@ -299,27 +299,46 @@ class UIVst3
#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
Expand Down Expand Up @@ -549,6 +568,8 @@ class UIVst3
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);
Expand Down

0 comments on commit ab236b8

Please sign in to comment.